add hook on commongeneratedocument

This commit is contained in:
atm-greg 2018-09-25 14:47:13 +02:00
parent 40bed96a01
commit 45bef65fc9

View File

@ -4057,236 +4057,242 @@ abstract class CommonObject
*/ */
protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null) protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
{ {
global $conf, $langs, $user; global $conf, $langs, $user, $hookmanager;
$srctemplatepath=''; $srctemplatepath='';
// Increase limit for PDF build $parameters = array('modelspath'=>$modelspath,'modele'=>$modele,'outputlangs'=>$outputlangs,'hidedetails'=>$hidedetails,'hidedesc'=>$hidedesc,'hideref'=>$hideref, 'moreparams'=>$moreparams);
$err=error_reporting(); $reshook = $hookmanager->executeHooks('commonGenerateDocument', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
error_reporting(0);
@set_time_limit(120); if(empty($reshook))
error_reporting($err);
// If selected model is a filename template (then $modele="modelname" or "modelname:filename")
$tmp=explode(':',$modele,2);
if (! empty($tmp[1]))
{ {
$modele=$tmp[0]; // Increase limit for PDF build
$srctemplatepath=$tmp[1]; $err=error_reporting();
} error_reporting(0);
@set_time_limit(120);
// Search template files error_reporting($err);
$file=''; $classname=''; $filefound=0;
$dirmodels=array('/'); // If selected model is a filename template (then $modele="modelname" or "modelname:filename")
if (is_array($conf->modules_parts['models'])) $dirmodels=array_merge($dirmodels,$conf->modules_parts['models']); $tmp=explode(':',$modele,2);
foreach($dirmodels as $reldir) if (! empty($tmp[1]))
{ {
foreach(array('doc','pdf') as $prefix) $modele=$tmp[0];
{ $srctemplatepath=$tmp[1];
if (in_array(get_class($this), array('Adherent'))) $file = $prefix."_".$modele.".class.php"; // Member module use prefix_module.class.php }
else $file = $prefix."_".$modele.".modules.php";
// Search template files
// On verifie l'emplacement du modele $file=''; $classname=''; $filefound=0;
$file=dol_buildpath($reldir.$modelspath.$file,0); $dirmodels=array('/');
if (file_exists($file)) if (is_array($conf->modules_parts['models'])) $dirmodels=array_merge($dirmodels,$conf->modules_parts['models']);
{ foreach($dirmodels as $reldir)
$filefound=1; {
$classname=$prefix.'_'.$modele; foreach(array('doc','pdf') as $prefix)
break; {
} if (in_array(get_class($this), array('Adherent'))) $file = $prefix."_".$modele.".class.php"; // Member module use prefix_module.class.php
} else $file = $prefix."_".$modele.".modules.php";
if ($filefound) break;
} // On verifie l'emplacement du modele
$file=dol_buildpath($reldir.$modelspath.$file,0);
// If generator was found if (file_exists($file))
if ($filefound) {
{ $filefound=1;
global $db; // Required to solve a conception default in commonstickergenerator.class.php making an include of code using $db $classname=$prefix.'_'.$modele;
break;
require_once $file; }
}
$obj = new $classname($this->db); if ($filefound) break;
}
// If generator is ODT, we must have srctemplatepath defined, if not we set it.
if ($obj->type == 'odt' && empty($srctemplatepath)) // If generator was found
{ if ($filefound)
$varfortemplatedir=$obj->scandir; {
if ($varfortemplatedir && ! empty($conf->global->$varfortemplatedir)) global $db; // Required to solve a conception default in commonstickergenerator.class.php making an include of code using $db
{
$dirtoscan=$conf->global->$varfortemplatedir; require_once $file;
$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); {
if (! $tmpdir) { unset($listofdir[$key]); continue; } $dirtoscan=$conf->global->$varfortemplatedir;
if (is_dir($tmpdir))
{ $listoffiles=array();
$tmpfiles=dol_dir_list($tmpdir,'files',0,'\.od(s|t)$','','name',SORT_ASC,0);
if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles); // Now we add first model found in directories scanned
} $listofdir=explode(',',$dirtoscan);
} foreach($listofdir as $key => $tmpdir)
{
if (count($listoffiles)) $tmpdir=trim($tmpdir);
{ $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
foreach($listoffiles as $record) if (! $tmpdir) { unset($listofdir[$key]); continue; }
{ if (is_dir($tmpdir))
$srctemplatepath=$record['fullname']; {
break; $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 (! dol_is_file($srctemplatepath))
{ if (empty($srctemplatepath))
$this->error='ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound'; {
return -1; $this->error='ErrorGenerationAskedForOdtTemplateWithSrcFileNotDefined';
} 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. if ($obj->type == 'odt' && ! empty($srctemplatepath))
$sav_charset_output=$outputlangs->charset_output; {
if (! dol_is_file($srctemplatepath))
if (in_array(get_class($this), array('Adherent'))) {
{ $this->error='ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound';
$arrayofrecords = array(); // The write_file of templates of adherent class need this var return -1;
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, 'member', 1, $moreparams); }
} }
else
{ // We save charset_output to restore it because write_file can change it if needed for
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams); // output format that does not support UTF8.
} $sav_charset_output=$outputlangs->charset_output;
// After call of write_file $obj->result['fullpath'] is set with generated file. It will be used to update the ECM database index.
if (in_array(get_class($this), array('Adherent')))
if ($resultwritefile > 0) {
{ $arrayofrecords = array(); // The write_file of templates of adherent class need this var
$outputlangs->charset_output=$sav_charset_output; $resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, 'member', 1, $moreparams);
}
// We delete old preview else
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; {
dol_delete_preview($this); $resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams);
}
// Index file in database // After call of write_file $obj->result['fullpath'] is set with generated file. It will be used to update the ECM database index.
if (! empty($obj->result['fullpath']))
{ if ($resultwritefile > 0)
$destfull = $obj->result['fullpath']; {
$upload_dir = dirname($destfull); $outputlangs->charset_output=$sav_charset_output;
$destfile = basename($destfull);
$rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $upload_dir); // We delete old preview
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
if (! preg_match('/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) // If not a tmp dir dol_delete_preview($this);
{
$filename = basename($destfile); // Index file in database
$rel_dir = preg_replace('/[\\/]$/', '', $rel_dir); if (! empty($obj->result['fullpath']))
$rel_dir = preg_replace('/^[\\/]/', '', $rel_dir); {
$destfull = $obj->result['fullpath'];
include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php'; $upload_dir = dirname($destfull);
$ecmfile=new EcmFiles($this->db); $destfile = basename($destfull);
$result = $ecmfile->fetch(0, '', ($rel_dir?$rel_dir.'/':'').$filename); $rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $upload_dir);
// Set the public "share" key if (! preg_match('/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) // If not a tmp dir
$setsharekey = false; {
if ($this->element == 'propal') $filename = basename($destfile);
{ $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
$useonlinesignature = $conf->global->MAIN_FEATURES_LEVEL; // Replace this with 1 when feature to make online signature is ok $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
if ($useonlinesignature) $setsharekey=true;
if (! empty($conf->global->PROPOSAL_ALLOW_EXTERNAL_DOWNLOAD)) $setsharekey=true; include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
} $ecmfile=new EcmFiles($this->db);
if ($this->element == 'commande' && ! empty($conf->global->ORDER_ALLOW_EXTERNAL_DOWNLOAD)) $setsharekey=true; $result = $ecmfile->fetch(0, '', ($rel_dir?$rel_dir.'/':'').$filename);
if ($this->element == 'facture' && ! empty($conf->global->INVOICE_ALLOW_EXTERNAL_DOWNLOAD)) $setsharekey=true;
if ($setsharekey) // Set the public "share" key
{ $setsharekey = false;
if (empty($ecmfile->share)) // Because object not found or share not set yet if ($this->element == 'propal')
{ {
require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php'; $useonlinesignature = $conf->global->MAIN_FEATURES_LEVEL; // Replace this with 1 when feature to make online signature is ok
$ecmfile->share = getRandomPassword(true); if ($useonlinesignature) $setsharekey=true;
} if (! empty($conf->global->PROPOSAL_ALLOW_EXTERNAL_DOWNLOAD)) $setsharekey=true;
} }
if ($this->element == 'commande' && ! empty($conf->global->ORDER_ALLOW_EXTERNAL_DOWNLOAD)) $setsharekey=true;
if ($result > 0) if ($this->element == 'facture' && ! empty($conf->global->INVOICE_ALLOW_EXTERNAL_DOWNLOAD)) $setsharekey=true;
{ if ($setsharekey)
$ecmfile->label = md5_file(dol_osencode($destfull)); // hash of file content {
$ecmfile->fullpath_orig = ''; if (empty($ecmfile->share)) // Because object not found or share not set yet
$ecmfile->gen_or_uploaded = 'generated'; {
$ecmfile->description = ''; // indexed content require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
$ecmfile->keyword = ''; // keyword content $ecmfile->share = getRandomPassword(true);
$result = $ecmfile->update($user); }
if ($result < 0) }
{
setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings'); if ($result > 0)
} {
} $ecmfile->label = md5_file(dol_osencode($destfull)); // hash of file content
else $ecmfile->fullpath_orig = '';
{ $ecmfile->gen_or_uploaded = 'generated';
$ecmfile->entity = $conf->entity; $ecmfile->description = ''; // indexed content
$ecmfile->filepath = $rel_dir; $ecmfile->keyword = ''; // keyword content
$ecmfile->filename = $filename; $result = $ecmfile->update($user);
$ecmfile->label = md5_file(dol_osencode($destfull)); // hash of file content if ($result < 0)
$ecmfile->fullpath_orig = ''; {
$ecmfile->gen_or_uploaded = 'generated'; setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
$ecmfile->description = ''; // indexed content }
$ecmfile->keyword = ''; // keyword content }
$result = $ecmfile->create($user); else
if ($result < 0) {
{ $ecmfile->entity = $conf->entity;
setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings'); $ecmfile->filepath = $rel_dir;
} $ecmfile->filename = $filename;
} $ecmfile->label = md5_file(dol_osencode($destfull)); // hash of file content
$ecmfile->fullpath_orig = '';
/*$this->result['fullname']=$destfull; $ecmfile->gen_or_uploaded = 'generated';
$this->result['filepath']=$ecmfile->filepath; $ecmfile->description = ''; // indexed content
$this->result['filename']=$ecmfile->filename;*/ $ecmfile->keyword = ''; // keyword content
//var_dump($obj->update_main_doc_field);exit; $result = $ecmfile->create($user);
if ($result < 0)
// Update the last_main_doc field into main object (if documenent generator has property ->update_main_doc_field set) {
$update_main_doc_field=0; setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
if (! empty($obj->update_main_doc_field)) $update_main_doc_field=1; }
if ($update_main_doc_field && ! empty($this->table_element)) }
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET last_main_doc = '".($ecmfile->filepath.'/'.$ecmfile->filename)."'"; /*$this->result['fullname']=$destfull;
$sql.= ' WHERE rowid = '.$this->id; $this->result['filepath']=$ecmfile->filepath;
$resql = $this->db->query($sql); $this->result['filename']=$ecmfile->filename;*/
if (! $resql) dol_print_error($this->db); //var_dump($obj->update_main_doc_field);exit;
}
} // Update the last_main_doc field into main object (if documenent generator has property ->update_main_doc_field set)
} $update_main_doc_field=0;
else if (! empty($obj->update_main_doc_field)) $update_main_doc_field=1;
{ if ($update_main_doc_field && ! empty($this->table_element))
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); {
} $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element." SET last_main_doc = '".($ecmfile->filepath.'/'.$ecmfile->filename)."'";
$sql.= ' WHERE rowid = '.$this->id;
// Success in building document. We build meta file. $resql = $this->db->query($sql);
dol_meta_create($this); if (! $resql) dol_print_error($this->db);
}
return 1; }
} }
else else
{ {
$outputlangs->charset_output=$sav_charset_output; 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);
dol_print_error($this->db, "Error generating document for ".__CLASS__.". Error: ".$obj->error, $obj->errors); }
return -1;
} // Success in building document. We build meta file.
dol_meta_create($this);
}
else return 1;
{ }
$this->error=$langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists",$file); else
dol_print_error('',$this->error); {
return -1; $outputlangs->charset_output=$sav_charset_output;
dol_print_error($this->db, "Error generating document for ".__CLASS__.". Error: ".$obj->error, $obj->errors);
return -1;
}
}
else
{
$this->error=$langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists",$file);
dol_print_error('',$this->error);
return -1;
}
} }
} }