48 public $version =
'dolibarr';
61 $langs->loadLangs(array(
"main",
"companies"));
64 $this->
name =
"ODT templates";
65 $this->
description = $langs->trans(
"DocumentModelOdt");
66 $this->update_main_doc_field = 1;
67 $this->scandir =
'EXPEDITION_ADDON_PDF_ODT_PATH';
71 $this->page_largeur = 0;
72 $this->page_hauteur = 0;
73 $this->format = array($this->page_largeur, $this->page_hauteur);
74 $this->marge_gauche = 0;
75 $this->marge_droite = 0;
76 $this->marge_haute = 0;
77 $this->marge_basse = 0;
79 $this->option_logo = 1;
80 $this->option_tva = 0;
81 $this->option_modereg = 0;
82 $this->option_condreg = 0;
83 $this->option_multilang = 1;
84 $this->option_escompte = 0;
85 $this->option_credit_note = 0;
86 $this->option_freetext = 1;
87 $this->option_draft_watermark = 0;
96 if (!$this->emetteur->country_code) {
97 $this->emetteur->country_code = substr($langs->defaultlang, -2);
110 global
$conf, $langs;
113 $langs->loadLangs(array(
"errors",
"companies"));
115 $form =
new Form($this->db);
120 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
121 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
122 $texte .=
'<input type="hidden" name="page_y" value="">';
123 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
124 $texte .=
'<input type="hidden" name="param1" value="EXPEDITION_ADDON_PDF_ODT_PATH">';
125 $texte .=
'<table class="nobordernopadding centpercent">';
128 $texte .=
'<tr><td>';
129 $texttitle = $langs->trans(
"ListOfDirectories");
130 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
131 $listoffiles = array();
132 foreach ($listofdir as $key => $tmpdir) {
133 $tmpdir = trim($tmpdir);
134 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
136 unset($listofdir[$key]);
139 if (!is_dir($tmpdir)) {
140 $texttitle .= img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
142 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
143 if (count($tmpfiles)) {
144 $listoffiles = array_merge($listoffiles, $tmpfiles);
148 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
149 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
151 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
152 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
155 $nbofiles = count($listoffiles);
156 if (!empty($odtPath)) {
157 $texte .=
'<span class="opacitylow">'.$langs->trans(
"NumberOfModelFilesFound").
': <b>';
159 $texte .= count($listoffiles);
161 $texte .=
'</b></span><br>';
164 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx opacitylow">';
166 foreach ($listoffiles as $file) {
167 $texte .=
'- '.$file[
'name'];
168 $texte .=
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=shipments/'.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'listlight').
'</a>';
169 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=EXPEDITION_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'delete').
'</a>';
177 $texte .=
'<span class="classlink cursorpointer" onclick="javascript: jQuery(\'.div_odtmodelsof'.get_class($this).
'\').toggle();
">'.img_picto('', 'add').' '.$langs->trans("UploadNewTemplate
").'...</span>';
178 $texte .= '<div class="div_odtmodelsof
'.get_class($this).'" style="display: none; margin-top: 6px;
">';
179 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name);
180 $texte .= '<div><div style="display:
inline-block; min-width: 100px; vertical-align: middle;
">';
181 $texte .= '<textarea class="flat textareafordir
" spellcheck="false" cols="60
" name="value1
">';
183 $texte .= '</textarea>';
184 $texte .= '</div><div style="display:
inline-block; vertical-align: middle;
">';
185 $texte .= '<input type="submit
" class="button button-edit reposition smallpaddingimp
" name="modify
" value="'.dol_escape_htmltag($langs->trans("Modify")).'">';
186 $texte .= '<br></div></div>';
188 // Add input to upload a new template file.
190 $maxfilesizearray = getMaxFileSizeArray();
191 $maxmin = $maxfilesizearray['maxmin'];
193 $texte .= '<input type="hidden
" name="MAX_FILE_SIZE
" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
195 $texte .= ' <input type="file
" name="uploadfile
">';
196 $texte .= '<input type="hidden
" value="EXPEDITION_ADDON_PDF_ODT_PATH
" name="keyforuploaddir
">';
197 $texte .= '<input type="submit
" class="button smallpaddingimp reposition
" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload
">';
204 $texte .= '</table>';
210 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
222 public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
225 global $user, $langs, $conf, $mysoc, $hookmanager;
227 if (empty($srctemplatepath)) {
232 // Add odtgeneration hook
233 if (!is_object($hookmanager)) {
234 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
235 $hookmanager = new HookManager($this->db);
237 $hookmanager->initHooks(array('odtgeneration'));
240 if (!is_object($outputlangs)) {
241 $outputlangs = $langs;
243 $sav_charset_output = $outputlangs->charset_output;
244 $outputlangs->charset_output = 'UTF-8';
246 // Load translation files required by page
247 $outputlangs->loadLangs(array("main
", "dict
", "companies
", "bills
"));
249 if ($conf->expedition->multidir_output[$object->entity ?? $conf->entity]."/sending
") {
250 // If $object is id instead of object
251 if (!is_object($object)) {
253 $object = new Expedition($this->db);
254 $result = $object->fetch($id);
256 dol_print_error($this->db, $object->error);
261 $object->fetch_thirdparty();
263 $dir = $conf->expedition->multidir_output[$object->entity ?? $conf->entity]."/sending
";
264 $objectref = dol_sanitizeFileName($object->ref);
265 if (!preg_match('/specimen/i', $objectref)) {
266 $dir .= "/
".$objectref;
268 $file = $dir."/
".$objectref.".odt
";
270 if (!file_exists($dir)) {
271 if (dol_mkdir($dir) < 0) {
272 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
277 if (file_exists($dir)) {
278 //print "srctemplatepath=
".$srctemplatepath; // Src filename
279 $newfile = basename($srctemplatepath);
280 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
281 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
282 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
283 $newfiletmp = $objectref . '_' . $newfiletmp;
284 //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
285 // Get extension (ods or odt)
286 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
287 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
288 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
289 if ($format == '1') {
290 $format = '%Y%m%d%H%M%S';
292 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
294 $filename = $newfiletmp . '.' . $newfileformat;
296 $file = $dir . '/' . $filename;
297 //print "newdir=
".$dir;
298 //print "newfile=
".$newfile;
299 //print "file=
".$file;
300 //print "conf->societe->dir_temp=
".$conf->societe->dir_temp;
302 dol_mkdir($conf->expedition->dir_temp);
303 if (!is_writable($conf->expedition->dir_temp)) {
304 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory
", $conf->expedition->dir_temp);
305 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
309 // If SHIPMENT contact defined on invoice, we use it
311 $arrayidcontact = $object->getIdContact('external', 'SHIPPING');
312 if (count($arrayidcontact) > 0) {
314 $result = $object->fetch_contact($arrayidcontact[0]);
318 $contactobject = null;
319 if (!empty($usecontact)) {
320 // We can use the company of contact instead of thirdparty company
321 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || getDolGlobalString('MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
322 $object->contact->fetch_thirdparty();
323 $socobject = $object->contact->thirdparty;
324 $contactobject = $object->contact;
326 $socobject = $object->thirdparty;
327 // if we have a SHIPPING contact and we don't use it as thirdparty recipient we store the contact object for later use
328 $contactobject = $object->contact;
331 $socobject = $object->thirdparty;
335 $substitutionarray = array(
336 '__FROM_NAME__' => $this->emetteur->name,
337 '__FROM_EMAIL__' => $this->emetteur->email,
338 '__TOTAL_TTC__' => $object->total_ttc,
339 '__TOTAL_HT__' => $object->total_ht,
340 '__TOTAL_VAT__' => $object->total_tva
342 complete_substitutions_array($substitutionarray, $langs, $object);
343 // Call the ODTSubstitution hook
344 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$substitutionarray);
345 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
349 $paramfreetext = 'EXPEDITION_FREE_TEXT';
350 if (getDolGlobalString($paramfreetext)) {
351 $newfreetext = make_substitutions(getDolGlobalString($paramfreetext), $substitutionarray);
354 // Open and load template
355 require_once ODTPHP_PATH.'odf.php';
357 $odfHandler = new Odf(
360 'PATH_TO_TMP' => $conf->expedition->dir_temp,
361 'ZIP_PROXY' => getDolGlobalString('MAIN_ODF_ZIP_PROXY', 'PclZipProxy'), // PhpZipProxy or PclZipProxy. Got "bad compression method
" error when using PhpZipProxy.
362 'DELIMITER_LEFT' => '{',
363 'DELIMITER_RIGHT' => '}'
366 } catch (Exception $e) {
367 $this->error = $e->getMessage();
368 dol_syslog($e->getMessage(), LOG_INFO);
371 // After construction $odfHandler->contentXml contains content and
372 // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
373 // [!-- BEGIN lines --]*[!-- END lines --]
374 //print html_entity_decode($odfHandler->__toString());
378 // Make substitutions into odt of freetext
380 $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
381 } catch (OdfException $e) {
382 dol_syslog($e->getMessage(), LOG_INFO);
385 // retrieve the constant to apply a ratio for image size or set the ratio to 1
386 if (getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO')) {
387 $ratio = (float) getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO');
392 // Make substitutions into odt of user info
393 $tmparray = $this->get_substitutionarray_user($user, $outputlangs);
394 foreach ($tmparray as $key => $value) {
396 if (preg_match('/logo$/', $key)) { // Image
397 //var_dump($value);exit;
398 if (file_exists($value)) {
399 $odfHandler->setImage($key, $value, $ratio);
401 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
404 $odfHandler->setVars($key, $value, true, 'UTF-8');
406 } catch (OdfException $e) {
407 dol_syslog($e->getMessage(), LOG_INFO);
410 // Make substitutions into odt of mysoc
411 $tmparray = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
412 foreach ($tmparray as $key => $value) {
414 if (preg_match('/logo$/', $key)) { // Image
415 //var_dump($value);exit;
416 if (file_exists($value)) {
417 $odfHandler->setImage($key, $value, $ratio);
419 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
422 $odfHandler->setVars($key, $value, true, 'UTF-8');
424 } catch (OdfException $e) {
425 dol_syslog($e->getMessage(), LOG_INFO);
428 // Make substitutions into odt of thirdparty
429 if ($socobject->element == 'contact') {
431 $tmparray = $this->get_substitutionarray_contact($socobject, $outputlangs);
433 $tmparray = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
435 foreach ($tmparray as $key => $value) {
437 if (preg_match('/logo$/', $key)) { // Image
438 if (file_exists($value)) {
439 $odfHandler->setImage($key, $value, $ratio);
441 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
444 $odfHandler->setVars($key, $value, true, 'UTF-8');
446 } catch (OdfException $e) {
447 dol_syslog($e->getMessage(), LOG_INFO);
451 if ($usecontact && is_object($contactobject)) {
452 $tmparray = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
453 foreach ($tmparray as $key => $value) {
455 if (preg_match('/logo$/', $key)) { // Image
456 if (file_exists($value)) {
457 $odfHandler->setImage($key, $value, $ratio);
459 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
462 $odfHandler->setVars($key, $value, true, 'UTF-8');
464 } catch (OdfException $e) {
465 dol_syslog($e->getMessage(), LOG_INFO);
470 // Replace tags of object + external modules
471 $tmparray = array_merge($tmparray, $this->get_substitutionarray_shipment($object, $outputlangs));
472 $tmparray = array_merge($tmparray, $this->get_substitutionarray_other($outputlangs));
474 complete_substitutions_array($tmparray, $outputlangs, $object);
475 // Call the ODTSubstitution hook
476 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
477 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
479 foreach ($tmparray as $key => $value) {
481 if (preg_match('/logo$/', $key)) {
483 if (file_exists($value)) {
484 $odfHandler->setImage($key, $value, $ratio);
486 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
490 $odfHandler->setVars($key, $value, true, 'UTF-8');
492 } catch (OdfException $e) {
493 dol_syslog($e->getMessage(), LOG_INFO);
497 // Replace tags of lines
498 $foundtagforlines = 1;
500 $listlines = $odfHandler->setSegment('lines');
501 } catch (OdfExceptionSegmentNotFound $e) {
502 // We may arrive here if tags for lines not present into template
503 $foundtagforlines = 0;
504 dol_syslog($e->getMessage(), LOG_INFO);
506 if ($foundtagforlines) {
508 foreach ($object->lines as $line) {
510 $tmparray = $this->get_substitutionarray_lines($line, $outputlangs, $linenumber);
511 complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines
");
512 // Call the ODTSubstitutionLine hook
513 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray, 'line' => $line);
514 $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
515 foreach ($tmparray as $key => $val) {
517 $listlines->setVars($key, $val, true, 'UTF-8');
518 } catch (SegmentException $e) {
519 dol_syslog($e->getMessage(), LOG_INFO);
525 $odfHandler->mergeSegment($listlines);
526 } catch (OdfException $e) {
527 $this->error = $e->getMessage();
528 dol_syslog($this->error, LOG_WARNING);
533 // Replace labels translated
534 $tmparray = $outputlangs->get_translations_for_substitutions();
535 foreach ($tmparray as $key => $value) {
537 $odfHandler->setVars($key, $value, true, 'UTF-8');
538 } catch (OdfException $e) {
539 dol_syslog($e->getMessage(), LOG_INFO);
543 // Call the beforeODTSave hook
544 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
545 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
548 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
550 $odfHandler->exportAsAttachedPDF($file);
551 } catch (Exception $e) {
552 $this->error = $e->getMessage();
553 dol_syslog($e->getMessage(), LOG_INFO);
558 $odfHandler->saveToDisk($file);
559 } catch (Exception $e) {
560 $this->error = $e->getMessage();
561 dol_syslog($e->getMessage(), LOG_INFO);
566 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
567 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
571 $odfHandler = null; // Destroy object
573 $this->result = array('fullpath' => $file);
577 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.