Skip to content

Commit

Permalink
Fix: sharing code
Browse files Browse the repository at this point in the history
  • Loading branch information
hregis committed Apr 3, 2012
1 parent 84057d4 commit cde000e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 42 deletions.
51 changes: 10 additions & 41 deletions htdocs/admin/propal.php
Expand Up @@ -37,8 +37,11 @@

if (! $user->admin) accessforbidden();

$action =GETPOST('action','alpha');
$action = GETPOST('action','alpha');
$value = GETPOST('value','alpha');
$label = GETPOST('label','alpha');
$scandir = GETPOST('scandir','alpha');
$type='propal';

/*
* Actions
Expand Down Expand Up @@ -183,64 +186,30 @@

if ($action == 'set')
{
$label = GETPOST('label','alpha');
$scandir = GETPOST('scandir','alpha');

$type='propal';
$sql = "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity, libelle, description)";
$sql.= " VALUES ('".$db->escape($value)."','".$type."',".$conf->entity.", ";
$sql.= ($label?"'".$db->escape($label)."'":'null').", ";
$sql.= (! empty($scandir)?"'".$db->escape($scandir)."'":"null");
$sql.= ")";
$resql=$db->query($sql);
$ret = addDocumentModel($value, $type, $label, $scandir);
}

else if ($action == 'del')
{
$type='propal';
$sql = "DELETE FROM ".MAIN_DB_PREFIX."document_model";
$sql.= " WHERE nom = '".$db->escape($value)."'";
$sql.= " AND type = '".$type."'";
$sql.= " AND entity = ".$conf->entity;
if ($db->query($sql))
$ret = delDocumentModel($value, $type);
if ($ret > 0)
{
if ($conf->global->PROPALE_ADDON_PDF == "$value") dolibarr_del_const($db, 'PROPALE_ADDON_PDF',$conf->entity);
}
}

else if ($action == 'setdoc')
{
$label = GETPOST('label','alpha');
$scandir = GETPOST('scandir','alpha');

$db->begin();

if (dolibarr_set_const($db, "PROPALE_ADDON_PDF",$value,'chaine',0,'',$conf->entity))
{
$conf->global->PROPALE_ADDON_PDF = $value;
}

// On active le modele
$type='propal';
$sql_del = "DELETE FROM ".MAIN_DB_PREFIX."document_model";
$sql_del.= " WHERE nom = '".$db->escape($value)."'";
$sql_del.= " AND type = '".$type."'";
$sql_del.= " AND entity = ".$conf->entity;
$result1=$db->query($sql_del);

$sql = "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity, libelle, description)";
$sql.= " VALUES ('".$db->escape($value)."', '".$type."', ".$conf->entity.", ";
$sql.= ($value?"'".$db->escape($label)."'":'null').", ";
$sql.= (! empty($value)?"'".$db->escape($scandir)."'":"null");
$sql.= ")";
$result2=$db->query($sql);
if ($result1 && $result2)
{
$db->commit();
}
else
$ret = delDocumentModel($value, $type);
if ($ret > 0)
{
$db->rollback();
$ret = addDocumentModel($value, $type, $label, $scandir);
}
}

Expand Down
71 changes: 70 additions & 1 deletion htdocs/core/lib/admin.lib.php
@@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2005-2012 Regis Houssin <regis@dolibarr.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1121,4 +1121,73 @@ function form_constantes($tableau)
print '</table>';
}

/**
* Add document model used by doc generator
*
* @param string $name Model name
* @param string $type Model type
* @param string $label Model label
* @param string $description Model description
* @return int <0 if KO, >0 if OK
*/
function addDocumentModel($name, $type, $label='', $description='')
{
global $db, $conf;

$db->begin();

$sql = "INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity, libelle, description)";
$sql.= " VALUES ('".$db->escape($name)."','".$type."',".$conf->entity.", ";
$sql.= ($label?"'".$db->escape($label)."'":'null').", ";
$sql.= (! empty($description)?"'".$db->escape($description)."'":"null");
$sql.= ")";

dol_syslog("admin.lib::addDocumentModel sql=".$sql);
$resql=$db->query($sql);
if ($resql)
{
$db->commit();
return 1;
}
else
{
dol_print_error($db);
$db->rollback();
return -1;
}
}

/**
* Delete document model used by doc generator
*
* @param string $name Model name
* @param string $type Model type
* @return int <0 if KO, >0 if OK
*/
function delDocumentModel($name, $type)
{
global $db, $conf;

$db->begin();

$sql = "DELETE FROM ".MAIN_DB_PREFIX."document_model";
$sql.= " WHERE nom = '".$db->escape($name)."'";
$sql.= " AND type = '".$type."'";
$sql.= " AND entity = ".$conf->entity;

dol_syslog("admin.lib::delDocumentModel sql=".$sql);
$resql=$db->query($sql);
if ($resql)
{
$db->commit();
return 1;
}
else
{
dol_print_error($db);
$db->rollback();
return -1;
}
}

?>

0 comments on commit cde000e

Please sign in to comment.