Standardize code

This commit is contained in:
Laurent Destailleur
2018-05-10 14:15:52 +02:00
parent 63b9384ef6
commit 0c35bc5284
5 changed files with 135 additions and 153 deletions

View File

@@ -139,114 +139,3 @@ abstract class ModeleNumRefDons
}
}
/**
* Cree un don sur disque en fonction du modele de DON_ADDON_PDF
*
* @param DoliDB $db Databse handler
* @param int $id Id donation
* @param string $message Message
* @param string $modele Force le modele a utiliser ('' par defaut)
* @param Translate $outputlangs Object langs
* @param int $hidedetails Hide details of lines
* @param int $hidedesc Hide description
* @param int $hideref Hide ref
* @return int 0 if KO, 1 if OK
*/
function don_create($db, $id, $message, $modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
{
global $conf, $langs;
$langs->load("bills");
$eror=0;
// Increase limit for PDF build
$err=error_reporting();
error_reporting(0);
@set_time_limit(120);
error_reporting($err);
$srctemplatepath='';
// Set template to use
if (! dol_strlen($modele))
{
if (! empty($conf->global->DON_ADDON_MODEL))
{
$modele = $conf->global->DON_ADDON_MODEL;
}
else
{
print $langs->trans("Error")." ".$langs->trans("Error_DON_ADDON_MODEL_NotDefined");
return 0;
}
}
// If selected modele is a filename template (then $modele="modelname:filename")
$tmp=explode(':',$modele,2);
if (! empty($tmp[1]))
{
$modele=$tmp[0];
$srctemplatepath=$tmp[1];
}
// Search template files
$file=''; $classname=''; $filefound=0;
$dirmodels=array('/');
if (is_array($conf->modules_parts['models'])) $dirmodels=array_merge($dirmodels,$conf->modules_parts['models']);
foreach($dirmodels as $reldir)
{
foreach(array('html','doc','pdf') as $prefix)
{
$file = $prefix."_".preg_replace('/^html_/','',$modele).".modules.php";
// On verifie l'emplacement du modele
$file=dol_buildpath($reldir."core/modules/dons/".$file,0);
if (file_exists($file))
{
$filefound=1;
$classname=$prefix.'_'.$modele;
break;
}
}
if ($filefound) break;
}
// Charge le modele
if ($filefound)
{
require_once $file;
$object=new Don($db);
$object->fetch($id);
$classname = $modele;
$obj = new $classname($db);
// We save charset_output to restore it because write_file can change it if needed for
// output format that does not support UTF8.
$sav_charset_output=$outputlangs->charset_output;
if ($obj->write_file($object,$outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0)
{
$outputlangs->charset_output=$sav_charset_output;
// we delete preview files
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
dol_delete_preview($object);
return 1;
}
else
{
$outputlangs->charset_output=$sav_charset_output;
dol_syslog("Erreur dans don_create");
dol_print_error($db,$obj->error);
return 0;
}
}
else
{
print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists",$file);
return 0;
}
}

View File

@@ -242,8 +242,15 @@ else if ($action == 'classin' && $user->rights->don->creer)
$object->fetch($id);
$object->setProject($projectid);
}
// Actions to build doc
$upload_dir = $conf->don->dir_output;
$permissioncreate = $user->rights->don->creer;
include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
// Remove file in doc form
if ($action == 'remove_file')
/*if ($action == 'remove_file')
{
$object = new Don($db, 0, $_GET['id']);
if ($object->fetch($id))
@@ -261,11 +268,12 @@ if ($action == 'remove_file')
$action='';
}
}
*/
/*
* Build doc
*/
/*
if ($action == 'builddoc')
{
$object = new Don($db);
@@ -291,6 +299,7 @@ if ($action == 'builddoc')
exit;
}
}
*/
/*

View File

@@ -931,4 +931,112 @@ class Don extends CommonObject
}
}
/**
* Create a document onto disk according to template module.
*
* @param string $modele Force template to use ('' to not force)
* @param Translate $outputlangs objet lang a utiliser pour traduction
* @param int $hidedetails Hide details of lines
* @param int $hidedesc Hide description
* @param int $hideref Hide ref
* @return int 0 if KO, 1 if OK
*/
public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
{
global $conf,$langs;
$langs->load("bills");
if (! dol_strlen($modele)) {
$modele = 'html_cerfafr';
if ($this->modelpdf) {
$modele = $this->modelpdf;
} elseif (! empty($conf->global->DON_ADDON_MODEL)) {
$modele = $conf->global->DON_ADDON_MODEL;
}
}
$modelpath = "core/modules/dons/";
// TODO Restore use of commonGenerateDocument instead of dedicated code here
//return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
// Increase limit for PDF build
$err=error_reporting();
error_reporting(0);
@set_time_limit(120);
error_reporting($err);
$srctemplatepath='';
// If selected modele is a filename template (then $modele="modelname:filename")
$tmp=explode(':',$modele,2);
if (! empty($tmp[1]))
{
$modele=$tmp[0];
$srctemplatepath=$tmp[1];
}
// Search template files
$file=''; $classname=''; $filefound=0;
$dirmodels=array('/');
if (is_array($conf->modules_parts['models'])) $dirmodels=array_merge($dirmodels,$conf->modules_parts['models']);
foreach($dirmodels as $reldir)
{
foreach(array('html','doc','pdf') as $prefix)
{
$file = $prefix."_".preg_replace('/^html_/','',$modele).".modules.php";
// On verifie l'emplacement du modele
$file=dol_buildpath($reldir."core/modules/dons/".$file,0);
if (file_exists($file))
{
$filefound=1;
$classname=$prefix.'_'.$modele;
break;
}
}
if ($filefound) break;
}
// Charge le modele
if ($filefound)
{
require_once $file;
$object=$this;
$classname = $modele;
$obj = new $classname($db);
// We save charset_output to restore it because write_file can change it if needed for
// output format that does not support UTF8.
$sav_charset_output=$outputlangs->charset_output;
if ($obj->write_file($object,$outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0)
{
$outputlangs->charset_output=$sav_charset_output;
// we delete preview files
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
dol_delete_preview($object);
return 1;
}
else
{
$outputlangs->charset_output=$sav_charset_output;
dol_syslog("Erreur dans don_create");
dol_print_error($db,$obj->error);
return 0;
}
}
else
{
print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists",$file);
return 0;
}
}
}

View File

@@ -123,6 +123,11 @@ if (empty($reshook))
include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
// Actions to build doc
$upload_dir = $conf->expedition->dir_output.'/sending';
$permissioncreate = $user->rights->expedition->creer;
include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
// Reopen
if ($action == 'reopen' && $user->rights->expedition->creer)
{
@@ -533,42 +538,6 @@ if (empty($reshook))
$action="";
}
// Build document
else if ($action == 'builddoc') // En get ou en post
{
// Save last template used to generate document
if (GETPOST('model')) $object->setDocModel($user, GETPOST('model','alpha'));
// Define output language
$outputlangs = $langs;
$newlang='';
if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang=GETPOST('lang_id','aZ09');
if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$shipment->thirdparty->default_lang;
if (! empty($newlang))
{
$outputlangs = new Translate("",$conf);
$outputlangs->setDefaultLang($newlang);
}
$result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
if ($result <= 0)
{
setEventMessages($object->error, $object->errors, 'errors');
$action='';
}
}
// Delete file in doc form
elseif ($action == 'remove_file')
{
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$upload_dir = $conf->expedition->dir_output . "/sending";
$file = $upload_dir . '/' . GETPOST('file');
$ret=dol_delete_file($file,0,0,0,$object);
if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
}
elseif ($action == 'classifybilled')
{
$object->fetch($id);

View File

@@ -45,11 +45,8 @@ if (! empty($conf->projet->enabled)) {
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
}
$langs->loadLangs(array("sendings","bills",'deliveries','orders'));
$langs->load("sendings");
$langs->load("bills");
$langs->load('deliveries');
$langs->load('orders');
if (!empty($conf->incoterm->enabled)) $langs->load('incoterm');
$action=GETPOST('action', 'alpha');
@@ -77,9 +74,11 @@ include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be inclu
// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
$hookmanager->initHooks(array('deliverycard','globalcard'));
/*
* Actions
*/
$parameters=array();
$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
@@ -245,9 +244,16 @@ if ($action == 'update_extras_line')
}
// Actions to build doc
$upload_dir = $conf->expedition->dir_output.'/receipt';
$permissioncreate = $user->rights->expedition->creer;
include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
/*
* Build document
*/
/*
if ($action == 'builddoc') // En get ou en post
{
// Save last template used to generate document
@@ -283,6 +289,7 @@ elseif ($action == 'remove_file')
if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
}
*/
/*