Unreachable else statement removed, move some conditions to reduce indentation and improve code readability
This commit is contained in:
parent
6d21fd1c46
commit
26eb4729f4
@ -5466,171 +5466,165 @@ abstract class CommonObject
|
|||||||
$parameters = array('modelspath'=>$modelspath, 'modele'=>$modele, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'hidedesc'=>$hidedesc, 'hideref'=>$hideref, 'moreparams'=>$moreparams);
|
$parameters = array('modelspath'=>$modelspath, 'modele'=>$modele, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'hidedesc'=>$hidedesc, 'hideref'=>$hideref, 'moreparams'=>$moreparams);
|
||||||
$reshook = $hookmanager->executeHooks('commonGenerateDocument', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
|
$reshook = $hookmanager->executeHooks('commonGenerateDocument', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
|
||||||
|
|
||||||
if (empty($reshook)) {
|
if (!empty($reshook)) {
|
||||||
dol_syslog("commonGenerateDocument modele=".$modele." outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang : 'null'));
|
return $reshook;
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($modele)) {
|
dol_syslog("commonGenerateDocument modele=".$modele." outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang : 'null'));
|
||||||
$this->error = 'BadValueForParameterModele';
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Increase limit for PDF build
|
if (empty($modele)) {
|
||||||
$err = error_reporting();
|
$this->error = 'BadValueForParameterModele';
|
||||||
error_reporting(0);
|
return -1;
|
||||||
@set_time_limit(120);
|
}
|
||||||
error_reporting($err);
|
|
||||||
|
|
||||||
// If selected model is a filename template (then $modele="modelname" or "modelname:filename")
|
// Increase limit for PDF build
|
||||||
$tmp = explode(':', $modele, 2);
|
$err = error_reporting();
|
||||||
if (!empty($tmp[1])) {
|
error_reporting(0);
|
||||||
$modele = $tmp[0];
|
@set_time_limit(120);
|
||||||
$srctemplatepath = $tmp[1];
|
error_reporting($err);
|
||||||
}
|
|
||||||
|
|
||||||
// Search template files
|
// If selected model is a filename template (then $modele="modelname" or "modelname:filename")
|
||||||
$file = '';
|
$tmp = explode(':', $modele, 2);
|
||||||
$classname = '';
|
if (!empty($tmp[1])) {
|
||||||
$filefound = '';
|
$modele = $tmp[0];
|
||||||
$dirmodels = array('/');
|
$srctemplatepath = $tmp[1];
|
||||||
if (is_array($conf->modules_parts['models'])) {
|
}
|
||||||
$dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
|
|
||||||
}
|
|
||||||
foreach ($dirmodels as $reldir) {
|
|
||||||
foreach (array('doc', 'pdf') as $prefix) {
|
|
||||||
if (in_array(get_class($this), array('Adherent'))) {
|
|
||||||
// Member module use prefix_modele.class.php
|
|
||||||
$file = $prefix."_".$modele.".class.php";
|
|
||||||
} else {
|
|
||||||
// Other module use prefix_modele.modules.php
|
|
||||||
$file = $prefix."_".$modele.".modules.php";
|
|
||||||
}
|
|
||||||
|
|
||||||
// On verifie l'emplacement du modele
|
// Search template files
|
||||||
$file = dol_buildpath($reldir.$modelspath.$file, 0);
|
$file = '';
|
||||||
if (file_exists($file)) {
|
$classname = '';
|
||||||
$filefound = $file;
|
$filefound = '';
|
||||||
$classname = $prefix.'_'.$modele;
|
$dirmodels = array('/');
|
||||||
break;
|
if (is_array($conf->modules_parts['models'])) {
|
||||||
}
|
$dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
|
||||||
|
}
|
||||||
|
foreach ($dirmodels as $reldir) {
|
||||||
|
foreach (array('doc', 'pdf') as $prefix) {
|
||||||
|
if (in_array(get_class($this), array('Adherent'))) {
|
||||||
|
// Member module use prefix_modele.class.php
|
||||||
|
$file = $prefix."_".$modele.".class.php";
|
||||||
|
} else {
|
||||||
|
// Other module use prefix_modele.modules.php
|
||||||
|
$file = $prefix."_".$modele.".modules.php";
|
||||||
}
|
}
|
||||||
if ($filefound) {
|
|
||||||
|
// On verifie l'emplacement du modele
|
||||||
|
$file = dol_buildpath($reldir.$modelspath.$file, 0);
|
||||||
|
if (file_exists($file)) {
|
||||||
|
$filefound = $file;
|
||||||
|
$classname = $prefix.'_'.$modele;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If generator was found
|
|
||||||
if ($filefound) {
|
if ($filefound) {
|
||||||
global $db; // Required to solve a conception default making an include of code using $db instead of $this->db just after.
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
require_once $file;
|
if (!$filefound) {
|
||||||
|
$this->error = $langs->trans("Error").' Failed to load doc generator with modelpaths='.$modelspath.' - modele='.$modele;
|
||||||
|
$this->errors[] = $this->error;
|
||||||
|
dol_syslog($this->error, LOG_ERR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
$obj = new $classname($this->db);
|
// If generator was found
|
||||||
|
global $db; // Required to solve a conception default making an include of code using $db instead of $this->db just after.
|
||||||
|
|
||||||
// If generator is ODT, we must have srctemplatepath defined, if not we set it.
|
require_once $file;
|
||||||
if ($obj->type == 'odt' && empty($srctemplatepath)) {
|
|
||||||
$varfortemplatedir = $obj->scandir;
|
|
||||||
if ($varfortemplatedir && !empty($conf->global->$varfortemplatedir)) {
|
|
||||||
$dirtoscan = $conf->global->$varfortemplatedir;
|
|
||||||
|
|
||||||
$listoffiles = array();
|
$obj = new $classname($this->db);
|
||||||
|
|
||||||
// Now we add first model found in directories scanned
|
// If generator is ODT, we must have srctemplatepath defined, if not we set it.
|
||||||
$listofdir = explode(',', $dirtoscan);
|
if ($obj->type == 'odt' && empty($srctemplatepath)) {
|
||||||
foreach ($listofdir as $key => $tmpdir) {
|
$varfortemplatedir = $obj->scandir;
|
||||||
$tmpdir = trim($tmpdir);
|
if ($varfortemplatedir && !empty($conf->global->$varfortemplatedir)) {
|
||||||
$tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
|
$dirtoscan = $conf->global->$varfortemplatedir;
|
||||||
if (!$tmpdir) {
|
|
||||||
unset($listofdir[$key]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (is_dir($tmpdir)) {
|
|
||||||
$tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.od(s|t)$', '', 'name', SORT_ASC, 0);
|
|
||||||
if (count($tmpfiles)) {
|
|
||||||
$listoffiles = array_merge($listoffiles, $tmpfiles);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($listoffiles)) {
|
$listoffiles = array();
|
||||||
foreach ($listoffiles as $record) {
|
|
||||||
$srctemplatepath = $record['fullname'];
|
// Now we add first model found in directories scanned
|
||||||
break;
|
$listofdir = explode(',', $dirtoscan);
|
||||||
}
|
foreach ($listofdir as $key => $tmpdir) {
|
||||||
|
$tmpdir = trim($tmpdir);
|
||||||
|
$tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
|
||||||
|
if (!$tmpdir) {
|
||||||
|
unset($listofdir[$key]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (is_dir($tmpdir)) {
|
||||||
|
$tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.od(s|t)$', '', 'name', SORT_ASC, 0);
|
||||||
|
if (count($tmpfiles)) {
|
||||||
|
$listoffiles = array_merge($listoffiles, $tmpfiles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($srctemplatepath)) {
|
if (count($listoffiles)) {
|
||||||
$this->error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotDefined';
|
foreach ($listoffiles as $record) {
|
||||||
return -1;
|
$srctemplatepath = $record['fullname'];
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($obj->type == 'odt' && !empty($srctemplatepath)) {
|
if (empty($srctemplatepath)) {
|
||||||
if (!dol_is_file($srctemplatepath)) {
|
$this->error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotDefined';
|
||||||
dol_syslog("Failed to locate template file ".$srctemplatepath, LOG_WARNING);
|
|
||||||
$this->error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound';
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 = empty($outputlangs->charset_output) ? '' : $outputlangs->charset_output;
|
|
||||||
|
|
||||||
if (in_array(get_class($this), array('Adherent'))) {
|
|
||||||
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, 'member', 1, 'tmp_cards', $moreparams);
|
|
||||||
} else {
|
|
||||||
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams);
|
|
||||||
}
|
|
||||||
// After call of write_file $obj->result['fullpath'] is set with generated file. It will be used to update the ECM database index.
|
|
||||||
|
|
||||||
if ($resultwritefile > 0) {
|
|
||||||
$outputlangs->charset_output = $sav_charset_output;
|
|
||||||
|
|
||||||
// We delete old preview
|
|
||||||
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
|
|
||||||
dol_delete_preview($this);
|
|
||||||
|
|
||||||
// Index file in database
|
|
||||||
if (!empty($obj->result['fullpath'])) {
|
|
||||||
$destfull = $obj->result['fullpath'];
|
|
||||||
|
|
||||||
// Update the last_main_doc field into main object (if document generator has property ->update_main_doc_field set)
|
|
||||||
$update_main_doc_field = 0;
|
|
||||||
if (!empty($obj->update_main_doc_field)) {
|
|
||||||
$update_main_doc_field = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->indexFile($destfull, $update_main_doc_field);
|
|
||||||
} else {
|
|
||||||
dol_syslog('Method ->write_file was called on object '.get_class($obj).' and return a success but the return array ->result["fullpath"] was not set.', LOG_WARNING);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Success in building document. We build meta file.
|
|
||||||
dol_meta_create($this);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
$outputlangs->charset_output = $sav_charset_output;
|
|
||||||
$this->error = $obj->error;
|
|
||||||
$this->errors = $obj->errors;
|
|
||||||
dol_syslog("Error generating document for ".__CLASS__.". Error: ".$obj->error, LOG_ERR);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!$filefound) {
|
|
||||||
$this->error = $langs->trans("Error").' Failed to load doc generator with modelpaths='.$modelspath.' - modele='.$modele;
|
|
||||||
$this->errors[] = $this->error;
|
|
||||||
dol_syslog($this->error, LOG_ERR);
|
|
||||||
} else {
|
|
||||||
$this->error = $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $filefound);
|
|
||||||
$this->errors[] = $this->error;
|
|
||||||
dol_syslog($this->error, LOG_ERR);
|
|
||||||
}
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($obj->type == 'odt' && !empty($srctemplatepath)) {
|
||||||
|
if (!dol_is_file($srctemplatepath)) {
|
||||||
|
dol_syslog("Failed to locate template file ".$srctemplatepath, LOG_WARNING);
|
||||||
|
$this->error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound';
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 = empty($outputlangs->charset_output) ? '' : $outputlangs->charset_output;
|
||||||
|
|
||||||
|
if (in_array(get_class($this), array('Adherent'))) {
|
||||||
|
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, 'member', 1, 'tmp_cards', $moreparams);
|
||||||
} else {
|
} else {
|
||||||
return $reshook;
|
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams);
|
||||||
|
}
|
||||||
|
// After call of write_file $obj->result['fullpath'] is set with generated file. It will be used to update the ECM database index.
|
||||||
|
|
||||||
|
if ($resultwritefile > 0) {
|
||||||
|
$outputlangs->charset_output = $sav_charset_output;
|
||||||
|
|
||||||
|
// We delete old preview
|
||||||
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
|
||||||
|
dol_delete_preview($this);
|
||||||
|
|
||||||
|
// Index file in database
|
||||||
|
if (!empty($obj->result['fullpath'])) {
|
||||||
|
$destfull = $obj->result['fullpath'];
|
||||||
|
|
||||||
|
// Update the last_main_doc field into main object (if document generator has property ->update_main_doc_field set)
|
||||||
|
$update_main_doc_field = 0;
|
||||||
|
if (!empty($obj->update_main_doc_field)) {
|
||||||
|
$update_main_doc_field = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->indexFile($destfull, $update_main_doc_field);
|
||||||
|
} else {
|
||||||
|
dol_syslog('Method ->write_file was called on object '.get_class($obj).' and return a success but the return array ->result["fullpath"] was not set.', LOG_WARNING);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Success in building document. We build meta file.
|
||||||
|
dol_meta_create($this);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
$outputlangs->charset_output = $sav_charset_output;
|
||||||
|
$this->error = $obj->error;
|
||||||
|
$this->errors = $obj->errors;
|
||||||
|
dol_syslog("Error generating document for ".__CLASS__.". Error: ".$obj->error, LOG_ERR);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user