31require_once DOL_DOCUMENT_ROOT .
'/core/modules/asset/modules_asset.php';
32require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
33require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
34require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
35require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
36require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
54 public $phpmin = array(7, 0);
59 public $version =
'dolibarr';
71 $langs->loadLangs(array(
"main",
"companies"));
74 $this->
name =
"ODT templates";
75 $this->
description = $langs->trans(
"DocumentModelOdt");
76 $this->scandir =
'ASSET_ASSET_ADDON_PDF_ODT_PATH';
80 $this->page_largeur = 0;
81 $this->page_hauteur = 0;
82 $this->format = array($this->page_largeur, $this->page_hauteur);
83 $this->marge_gauche = 0;
84 $this->marge_droite = 0;
85 $this->marge_haute = 0;
86 $this->marge_basse = 0;
88 $this->option_logo = 1;
89 $this->option_tva = 0;
90 $this->option_modereg = 0;
91 $this->option_condreg = 0;
92 $this->option_multilang = 1;
93 $this->option_escompte = 0;
94 $this->option_credit_note = 0;
95 $this->option_freetext = 1;
96 $this->option_draft_watermark = 0;
104 if (!$this->emetteur->country_code) {
105 $this->emetteur->country_code = substr($langs->defaultlang, -2);
121 $langs->loadLangs(array(
"errors",
"companies"));
123 $form =
new Form($this->db);
126 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
127 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
128 $texte .=
'<input type="hidden" name="page_y" value="">';
129 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
130 $texte .=
'<input type="hidden" name="param1" value="ASSET_ASSET_ADDON_PDF_ODT_PATH">';
131 $texte .=
'<table class="nobordernopadding centpercent">';
134 $texte .=
'<tr><td>';
135 $texttitle = $langs->trans(
"ListOfDirectories");
136 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
getDolGlobalString(
'ASSET_ASSET_ADDON_PDF_ODT_PATH'))));
137 $listoffiles = array();
138 foreach ($listofdir as $key => $tmpdir) {
139 $tmpdir = trim($tmpdir);
140 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
142 unset($listofdir[$key]);
145 if (!is_dir($tmpdir)) {
146 $texttitle .= img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
148 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
149 if (count($tmpfiles)) {
150 $listoffiles = array_merge($listoffiles, $tmpfiles);
154 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
155 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
157 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
158 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
161 $nbofiles = count($listoffiles);
163 $texte .=
'<span class="opacitylow">'.$langs->trans(
"NumberOfModelFilesFound").
': <b>';
165 $texte .= count($listoffiles);
167 $texte .=
'</b></span><br>';
171 $texte .=
'<div id="div_'.get_class($this).
'" class="hidden opacitylow">';
173 foreach ($listoffiles as $file) {
174 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=asset_asset/'.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'listlight').
'</a>';
175 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=ASSET_ASSET_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'delete').
'</a>';
183 $texte .=
'<span class="classlink cursorpointer" onclick="javascript: jQuery(\'.div_odtmodelsof'.get_class($this).
'\').toggle();
">'.img_picto('', 'add').' '.$langs->trans("UploadNewTemplate
").'...</span>';
184 $texte .= '<div class="div_odtmodelsof
'.get_class($this).'" style="display: none; margin-top: 6px;
">';
185 if (!getDolGlobalString('MAIN_NO_MULTIDIR_FOR_ODT')) {
186 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name);
187 $texte .= '<div><div style="display:
inline-block; min-width: 100px; vertical-align: middle;
">';
188 $texte .= '<textarea class="flat textareafordir
" spellcheck="false" cols="60
" name="value1
">';
189 $texte .= getDolGlobalString('ASSET_ASSET_ADDON_PDF_ODT_PATH');
190 $texte .= '</textarea>';
191 $texte .= '</div><div style="display:
inline-block; vertical-align: middle;
">';
192 $texte .= '<input type="submit
" class="button button-edit reposition smallpaddingimp
" name="modify
" value="'.dolPrintHTMLForAttribute($langs->trans("Modify")).'">';
193 $texte .= '<br></div></div>';
196 $texte .= '<input type="hidden
" name="value1
" value="ASSET_ASSET_ADDON_PDF_ODT_PATH
">';
200 // Add input to upload a new template file.
201 $texte .= '<div class="div_odtmodelsof
'.get_class($this).'" style="display: none;
">';
202 $maxfilesizearray = getMaxFileSizeArray();
203 $maxmin = $maxfilesizearray['maxmin'];
205 $texte .= '<input type="hidden
" name="MAX_FILE_SIZE
" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
207 $texte .= ' <input type="file
" name="uploadfile
">';
208 $texte .= '<input type="hidden
" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH
" name="keyforuploaddir
">';
209 $texte .= '<input type="submit
" class="button smallpaddingimp reposition
" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload
">';
216 $texte .= '</table>';
222 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
234 public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
237 global $user, $langs, $conf, $mysoc, $hookmanager;
240 if (empty($srctemplatepath)) {
245 // Add odtgeneration hook
246 if (!is_object($hookmanager)) {
247 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
248 $hookmanager = new HookManager($this->db);
250 $hookmanager->initHooks(array('odtgeneration'));
253 if (!is_object($outputlangs)) {
254 $outputlangs = $langs;
256 $sav_charset_output = $outputlangs->charset_output;
257 $outputlangs->charset_output = 'UTF-8';
259 $outputlangs->loadLangs(array("main
", "dict
", "companies
", "bills
"));
261 if ($conf->asset->dir_output) {
262 // If $object is id instead of object
263 if (!is_object($object)) {
265 $object = new Asset($this->db);
266 $result = $object->fetch($id);
268 dol_print_error($this->db, $object->error);
273 $object->fetch_thirdparty();
275 $dir = $conf->asset->multidir_output[isset($object->entity) ? $object->entity : 1];
276 $objectref = dol_sanitizeFileName($object->ref);
277 if (!preg_match('/specimen/i', $objectref)) {
278 $dir .= "/
".$objectref;
280 $file = $dir."/
".$objectref.".odt
";
282 if (!file_exists($dir)) {
283 if (dol_mkdir($dir) < 0) {
284 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
289 if (file_exists($dir)) {
290 //print "srctemplatepath=
".$srctemplatepath; // Src filename
291 $newfile = basename($srctemplatepath);
292 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
293 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
294 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
295 $newfiletmp = $objectref.'_'.$newfiletmp;
296 //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
297 // Get extension (ods or odt)
298 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
299 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
300 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
301 if ($format == '1') {
302 $format = '%Y%m%d%H%M%S';
304 $filename = $newfiletmp.'-'.dol_print_date(dol_now(), $format).'.'.$newfileformat;
306 $filename = $newfiletmp.'.'.$newfileformat;
308 $file = $dir.'/'.$filename;
309 //print "newdir=
".$dir;
310 //print "newfile=
".$newfile;
311 //print "file=
".$file;
312 //print "conf->societe->dir_temp=
".$conf->societe->dir_temp;
314 dol_mkdir($conf->asset->dir_temp);
315 if (!is_writable($conf->asset->dir_temp)) {
316 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory
", $conf->asset->dir_temp);
317 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
321 // If CUSTOMER contact defined on object, we use it
323 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
324 if (count($arrayidcontact) > 0) {
326 $result = $object->fetch_contact($arrayidcontact[0]);
330 $contactobject = null;
331 if (!empty($usecontact)) {
332 // We can use the company of contact instead of thirdparty company
333 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || getDolGlobalString('MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
334 $object->contact->fetch_thirdparty();
335 $socobject = $object->contact->thirdparty;
336 $contactobject = $object->contact;
338 $socobject = $object->thirdparty;
339 // if we have a CUSTOMER contact and we don't use it as thirdparty recipient we store the contact object for later use
340 $contactobject = $object->contact;
343 $socobject = $object->thirdparty;
347 $substitutionarray = array(
348 '__FROM_NAME__' => $this->emetteur->name,
349 '__FROM_EMAIL__' => $this->emetteur->email,
350 '__TOTAL_TTC__' => $object->total_ttc,
351 '__TOTAL_HT__' => $object->total_ht,
352 '__TOTAL_VAT__' => $object->total_tva
354 complete_substitutions_array($substitutionarray, $langs, $object);
355 // Call the ODTSubstitution hook
356 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$substitutionarray);
357 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
361 $paramfreetext = 'ORDER_FREE_TEXT';
362 if (getDolGlobalString($paramfreetext)) {
363 $newfreetext = make_substitutions(getDolGlobalString($paramfreetext), $substitutionarray);
366 // Open and load template
367 require_once ODTPHP_PATH.'odf.php';
369 $odfHandler = new Odf(
372 'PATH_TO_TMP' => $conf->asset->dir_temp,
373 'ZIP_PROXY' => getDolGlobalString('MAIN_ODF_ZIP_PROXY', 'PclZipProxy'), // PhpZipProxy or PclZipProxy. Got "bad compression method
" error when using PhpZipProxy.
374 'DELIMITER_LEFT' => '{',
375 'DELIMITER_RIGHT' => '}'
378 } catch (Exception $e) {
379 $this->error = $e->getMessage();
380 dol_syslog($e->getMessage(), LOG_INFO);
383 // After construction $odfHandler->contentXml contains content and
384 // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
385 // [!-- BEGIN lines --]*[!-- END lines --]
387 // Make substitutions into odt of freetext
389 $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
390 } catch (OdfException $e) {
391 dol_syslog($e->getMessage(), LOG_INFO);
394 // Define substitution array
395 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
396 $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs);
397 $array_objet = $this->get_substitutionarray_object($object, $outputlangs);
398 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
399 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
400 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
401 $array_other = $this->get_substitutionarray_other($outputlangs);
402 // retrieve contact information for use in object as contact_xxx tags
403 $array_thirdparty_contact = array();
404 if ($usecontact && is_object($contactobject)) {
405 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
408 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
409 complete_substitutions_array($tmparray, $outputlangs, $object);
411 // Call the ODTSubstitution hook
412 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
413 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
415 // retrieve the constant to apply a ratio for image size or set the ratio to 1
416 if (getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO')) {
417 $ratio = (float) getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO');
422 foreach ($tmparray as $key => $value) {
424 if (preg_match('/logo$/', $key)) {
426 if (file_exists($value)) {
427 $odfHandler->setImage($key, $value, $ratio);
429 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
433 $odfHandler->setVars($key, $value, true, 'UTF-8');
435 } catch (OdfException $e) {
436 dol_syslog($e->getMessage(), LOG_INFO);
439 // Replace tags of lines
441 $foundtagforlines = 1;
443 $listlines = $odfHandler->setSegment('lines');
444 } catch (OdfExceptionSegmentNotFound $e) {
445 // We may arrive here if tags for lines not present into template
446 $foundtagforlines = 0;
447 dol_syslog($e->getMessage(), LOG_INFO);
449 if ($foundtagforlines) {
451 foreach ($object->lines as $line) {
453 $tmparray = $this->get_substitutionarray_lines($line, $outputlangs, $linenumber);
454 complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines
");
455 // Call the ODTSubstitutionLine hook
456 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray, 'line' => $line);
457 $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
458 foreach ($tmparray as $key => $val) {
460 $listlines->setVars($key, $val, true, 'UTF-8');
461 } catch (SegmentException $e) {
462 dol_syslog($e->getMessage(), LOG_INFO);
468 $odfHandler->mergeSegment($listlines);
469 } catch (OdfException $e) {
470 $this->error = $e->getMessage();
471 dol_syslog($this->error, LOG_WARNING);
476 // Replace labels translated
477 $tmparray = $outputlangs->get_translations_for_substitutions();
478 foreach ($tmparray as $key => $value) {
480 $odfHandler->setVars($key, $value, true, 'UTF-8');
481 } catch (OdfException $e) {
482 dol_syslog($e->getMessage(), LOG_INFO);
486 // Call the beforeODTSave hook
488 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
489 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
492 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
494 $odfHandler->exportAsAttachedPDF($file);
495 } catch (Exception $e) {
496 $this->error = $e->getMessage();
497 dol_syslog($e->getMessage(), LOG_INFO);
502 $odfHandler->saveToDisk($file);
503 } catch (Exception $e) {
504 $this->error = $e->getMessage();
505 dol_syslog($e->getMessage(), LOG_INFO);
510 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
511 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
515 $odfHandler = null; // Destroy object
517 $this->result = array('fullpath' => $file);
521 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Parent class for documents models.
Class to build documents using ODF templates generator.
info($langs)
Return description of a module.
__construct($db)
Constructor.
write_file($object, $outputlangs, $srctemplatepath='', $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
getCallerInfoString()
Get caller info as a string that can be appended to a log message.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
conf($dolibarr_main_document_root, $realpathconf=null)
Load conf file (file must exists)
$conf db name
Only used if Module[ID]Name translation string is not found.
multi select button
0 = Do not include form tag and submit button -1 = Do not include form tag but include submit button