48 public $version =
'dolibarr';
61 $langs->loadLangs(array(
"main",
"companies"));
64 $this->
name =
"ODT templates";
65 $this->
description = $langs->trans(
"DocumentModelOdt");
66 $this->scandir =
'EXPEDITION_ADDON_PDF_ODT_PATH';
70 $this->page_largeur = 0;
71 $this->page_hauteur = 0;
72 $this->format = array($this->page_largeur, $this->page_hauteur);
73 $this->marge_gauche = 0;
74 $this->marge_droite = 0;
75 $this->marge_haute = 0;
76 $this->marge_basse = 0;
78 $this->option_logo = 1;
79 $this->option_tva = 0;
80 $this->option_modereg = 0;
81 $this->option_condreg = 0;
82 $this->option_multilang = 1;
83 $this->option_escompte = 0;
84 $this->option_credit_note = 0;
85 $this->option_freetext = 1;
86 $this->option_draft_watermark = 0;
95 if (!$this->emetteur->country_code) {
96 $this->emetteur->country_code = substr($langs->defaultlang, -2);
109 global
$conf, $langs;
112 $langs->loadLangs(array(
"errors",
"companies"));
114 $form =
new Form($this->db);
119 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
120 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
121 $texte .=
'<input type="hidden" name="page_y" value="">';
122 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
123 $texte .=
'<input type="hidden" name="param1" value="EXPEDITION_ADDON_PDF_ODT_PATH">';
124 $texte .=
'<table class="nobordernopadding centpercent">';
127 $texte .=
'<tr><td>';
128 $texttitle = $langs->trans(
"ListOfDirectories");
129 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
130 $listoffiles = array();
131 foreach ($listofdir as $key => $tmpdir) {
132 $tmpdir = trim($tmpdir);
133 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
135 unset($listofdir[$key]);
138 if (!is_dir($tmpdir)) {
139 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
141 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
142 if (count($tmpfiles)) {
143 $listoffiles = array_merge($listoffiles, $tmpfiles);
147 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
148 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
150 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
151 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
154 $nbofiles = count($listoffiles);
155 if (!empty($odtPath)) {
156 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
158 $texte .= count($listoffiles);
160 $texte .=
'</b><br>';
163 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
165 foreach ($listoffiles as $file) {
166 $texte .=
'- '.$file[
'name'];
167 $texte .=
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=shipments/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
168 $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>';
175 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
176 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
177 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
179 $texte .=
'</textarea>';
180 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
181 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
182 $texte .=
'<br></div></div>';
185 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
187 $maxmin = $maxfilesizearray[
'maxmin'];
189 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
191 $texte .=
' <input type="file" name="uploadfile">';
192 $texte .=
'<input type="hidden" value="EXPEDITION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
193 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
199 $texte .=
'</table>';
217 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
222 if (empty($srctemplatepath)) {
223 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
228 if (!is_object($hookmanager)) {
229 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
232 $hookmanager->initHooks(array(
'odtgeneration'));
235 if (!is_object($outputlangs)) {
236 $outputlangs = $langs;
238 $sav_charset_output = $outputlangs->charset_output;
239 $outputlangs->charset_output =
'UTF-8';
242 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
244 if (
$conf->expedition->multidir_output[
$object->entity ??
$conf->entity].
"/sending") {
258 $dir =
$conf->expedition->multidir_output[
$object->entity ??
$conf->entity].
"/sending";
260 if (!preg_match(
'/specimen/i', $objectref)) {
261 $dir .=
"/".$objectref;
263 $file = $dir.
"/".$objectref.
".odt";
265 if (!file_exists($dir)) {
267 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
272 if (file_exists($dir)) {
274 $newfile = basename($srctemplatepath);
275 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
276 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
277 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
278 $newfiletmp = $objectref .
'_' . $newfiletmp;
281 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
284 if ($format ==
'1') {
285 $format =
'%Y%m%d%H%M%S';
289 $filename = $newfiletmp .
'.' . $newfileformat;
291 $file = $dir .
'/' . $filename;
298 if (!is_writable(
$conf->expedition->dir_temp)) {
299 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->expedition->dir_temp);
300 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
306 $arrayidcontact =
$object->getIdContact(
'external',
'SHIPPING');
307 if (count($arrayidcontact) > 0) {
309 $result =
$object->fetch_contact($arrayidcontact[0]);
313 $contactobject =
null;
314 if (!empty($usecontact)) {
317 $object->contact->fetch_thirdparty();
318 $socobject =
$object->contact->thirdparty;
319 $contactobject =
$object->contact;
321 $socobject =
$object->thirdparty;
323 $contactobject =
$object->contact;
326 $socobject =
$object->thirdparty;
330 $substitutionarray = array(
331 '__FROM_NAME__' => $this->emetteur->name,
332 '__FROM_EMAIL__' => $this->emetteur->email,
333 '__TOTAL_TTC__' =>
$object->total_ttc,
334 '__TOTAL_HT__' =>
$object->total_ht,
335 '__TOTAL_VAT__' =>
$object->total_tva
339 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
340 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
344 $paramfreetext =
'EXPEDITION_FREE_TEXT';
350 require_once ODTPHP_PATH.
'odf.php';
352 $odfHandler =
new Odf(
355 'PATH_TO_TMP' =>
$conf->expedition->dir_temp,
357 'DELIMITER_LEFT' =>
'{',
358 'DELIMITER_RIGHT' =>
'}'
362 $this->error = $e->getMessage();
375 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
376 }
catch (OdfException $e) {
389 foreach ($tmparray as $key => $value) {
391 if (preg_match(
'/logo$/', $key)) {
393 if (file_exists($value)) {
394 $odfHandler->setImage($key, $value, $ratio);
396 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
399 $odfHandler->setVars($key, $value,
true,
'UTF-8');
401 }
catch (OdfException $e) {
407 foreach ($tmparray as $key => $value) {
409 if (preg_match(
'/logo$/', $key)) {
411 if (file_exists($value)) {
412 $odfHandler->setImage($key, $value, $ratio);
414 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
417 $odfHandler->setVars($key, $value,
true,
'UTF-8');
419 }
catch (OdfException $e) {
424 if ($socobject->element ==
'contact') {
430 foreach ($tmparray as $key => $value) {
432 if (preg_match(
'/logo$/', $key)) {
433 if (file_exists($value)) {
434 $odfHandler->setImage($key, $value, $ratio);
436 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
439 $odfHandler->setVars($key, $value,
true,
'UTF-8');
441 }
catch (OdfException $e) {
446 if ($usecontact && is_object($contactobject)) {
448 foreach ($tmparray as $key => $value) {
450 if (preg_match(
'/logo$/', $key)) {
451 if (file_exists($value)) {
452 $odfHandler->setImage($key, $value, $ratio);
454 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
457 $odfHandler->setVars($key, $value,
true,
'UTF-8');
459 }
catch (OdfException $e) {
471 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
472 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
474 foreach ($tmparray as $key => $value) {
476 if (preg_match(
'/logo$/', $key)) {
478 if (file_exists($value)) {
479 $odfHandler->setImage($key, $value, $ratio);
481 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
485 $odfHandler->setVars($key, $value,
true,
'UTF-8');
487 }
catch (OdfException $e) {
493 $foundtagforlines = 1;
495 $listlines = $odfHandler->setSegment(
'lines');
496 }
catch (OdfExceptionSegmentNotFound $e) {
498 $foundtagforlines = 0;
501 if ($foundtagforlines) {
503 foreach (
$object->lines as $line) {
508 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
509 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
510 foreach ($tmparray as $key => $val) {
512 $listlines->setVars($key, $val,
true,
'UTF-8');
513 }
catch (SegmentException $e) {
520 $odfHandler->mergeSegment($listlines);
521 }
catch (OdfException $e) {
522 $this->error = $e->getMessage();
529 $tmparray = $outputlangs->get_translations_for_substitutions();
530 foreach ($tmparray as $key => $value) {
532 $odfHandler->setVars($key, $value,
true,
'UTF-8');
533 }
catch (OdfException $e) {
539 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
540 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
545 $odfHandler->exportAsAttachedPDF($file);
547 $this->error = $e->getMessage();
553 $odfHandler->saveToDisk($file);
555 $this->error = $e->getMessage();
561 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
562 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
568 $this->result = array(
'fullpath' => $file);
572 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);