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 .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
159 $texte .= count($listoffiles);
161 $texte .=
'</b><br>';
164 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
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>';
176 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
177 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
178 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
180 $texte .=
'</textarea>';
181 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
182 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
183 $texte .=
'<br></div></div>';
186 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
188 $maxmin = $maxfilesizearray[
'maxmin'];
190 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
192 $texte .=
' <input type="file" name="uploadfile">';
193 $texte .=
'<input type="hidden" value="EXPEDITION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
194 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
200 $texte .=
'</table>';
218 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
223 if (empty($srctemplatepath)) {
224 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
229 if (!is_object($hookmanager)) {
230 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
233 $hookmanager->initHooks(array(
'odtgeneration'));
236 if (!is_object($outputlangs)) {
237 $outputlangs = $langs;
239 $sav_charset_output = $outputlangs->charset_output;
240 $outputlangs->charset_output =
'UTF-8';
243 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
245 if (
$conf->expedition->multidir_output[
$object->entity ??
$conf->entity].
"/sending") {
259 $dir =
$conf->expedition->multidir_output[
$object->entity ??
$conf->entity].
"/sending";
261 if (!preg_match(
'/specimen/i', $objectref)) {
262 $dir .=
"/".$objectref;
264 $file = $dir.
"/".$objectref.
".odt";
266 if (!file_exists($dir)) {
268 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
273 if (file_exists($dir)) {
275 $newfile = basename($srctemplatepath);
276 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
277 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
278 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
279 $newfiletmp = $objectref .
'_' . $newfiletmp;
282 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
285 if ($format ==
'1') {
286 $format =
'%Y%m%d%H%M%S';
290 $filename = $newfiletmp .
'.' . $newfileformat;
292 $file = $dir .
'/' . $filename;
299 if (!is_writable(
$conf->expedition->dir_temp)) {
300 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->expedition->dir_temp);
301 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
307 $arrayidcontact =
$object->getIdContact(
'external',
'SHIPPING');
308 if (count($arrayidcontact) > 0) {
310 $result =
$object->fetch_contact($arrayidcontact[0]);
314 $contactobject =
null;
315 if (!empty($usecontact)) {
318 $object->contact->fetch_thirdparty();
319 $socobject =
$object->contact->thirdparty;
320 $contactobject =
$object->contact;
322 $socobject =
$object->thirdparty;
324 $contactobject =
$object->contact;
327 $socobject =
$object->thirdparty;
331 $substitutionarray = array(
332 '__FROM_NAME__' => $this->emetteur->name,
333 '__FROM_EMAIL__' => $this->emetteur->email,
334 '__TOTAL_TTC__' =>
$object->total_ttc,
335 '__TOTAL_HT__' =>
$object->total_ht,
336 '__TOTAL_VAT__' =>
$object->total_tva
340 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
341 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
345 $paramfreetext =
'EXPEDITION_FREE_TEXT';
351 require_once ODTPHP_PATH.
'odf.php';
353 $odfHandler =
new Odf(
356 'PATH_TO_TMP' =>
$conf->expedition->dir_temp,
358 'DELIMITER_LEFT' =>
'{',
359 'DELIMITER_RIGHT' =>
'}'
363 $this->error = $e->getMessage();
376 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
377 }
catch (OdfException $e) {
390 foreach ($tmparray as $key => $value) {
392 if (preg_match(
'/logo$/', $key)) {
394 if (file_exists($value)) {
395 $odfHandler->setImage($key, $value, $ratio);
397 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
400 $odfHandler->setVars($key, $value,
true,
'UTF-8');
402 }
catch (OdfException $e) {
408 foreach ($tmparray as $key => $value) {
410 if (preg_match(
'/logo$/', $key)) {
412 if (file_exists($value)) {
413 $odfHandler->setImage($key, $value, $ratio);
415 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
418 $odfHandler->setVars($key, $value,
true,
'UTF-8');
420 }
catch (OdfException $e) {
425 if ($socobject->element ==
'contact') {
431 foreach ($tmparray as $key => $value) {
433 if (preg_match(
'/logo$/', $key)) {
434 if (file_exists($value)) {
435 $odfHandler->setImage($key, $value, $ratio);
437 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
440 $odfHandler->setVars($key, $value,
true,
'UTF-8');
442 }
catch (OdfException $e) {
447 if ($usecontact && is_object($contactobject)) {
449 foreach ($tmparray as $key => $value) {
451 if (preg_match(
'/logo$/', $key)) {
452 if (file_exists($value)) {
453 $odfHandler->setImage($key, $value, $ratio);
455 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
458 $odfHandler->setVars($key, $value,
true,
'UTF-8');
460 }
catch (OdfException $e) {
472 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
473 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
475 foreach ($tmparray as $key => $value) {
477 if (preg_match(
'/logo$/', $key)) {
479 if (file_exists($value)) {
480 $odfHandler->setImage($key, $value, $ratio);
482 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
486 $odfHandler->setVars($key, $value,
true,
'UTF-8');
488 }
catch (OdfException $e) {
494 $foundtagforlines = 1;
496 $listlines = $odfHandler->setSegment(
'lines');
497 }
catch (OdfExceptionSegmentNotFound $e) {
499 $foundtagforlines = 0;
502 if ($foundtagforlines) {
504 foreach (
$object->lines as $line) {
509 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
510 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
511 foreach ($tmparray as $key => $val) {
513 $listlines->setVars($key, $val,
true,
'UTF-8');
514 }
catch (SegmentException $e) {
521 $odfHandler->mergeSegment($listlines);
522 }
catch (OdfException $e) {
523 $this->error = $e->getMessage();
530 $tmparray = $outputlangs->get_translations_for_substitutions();
531 foreach ($tmparray as $key => $value) {
533 $odfHandler->setVars($key, $value,
true,
'UTF-8');
534 }
catch (OdfException $e) {
540 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
541 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
546 $odfHandler->exportAsAttachedPDF($file);
548 $this->error = $e->getMessage();
554 $odfHandler->saveToDisk($file);
556 $this->error = $e->getMessage();
562 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
563 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
569 $this->result = array(
'fullpath' => $file);
573 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);