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