48 public $version =
'dolibarr';
58 global
$conf, $langs, $mysoc;
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;
89 if ($mysoc ===
null) {
95 $this->emetteur = $mysoc;
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);
118 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
119 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
120 $texte .=
'<input type="hidden" name="page_y" value="">';
121 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
122 $texte .=
'<input type="hidden" name="param1" value="EXPEDITION_ADDON_PDF_ODT_PATH">';
123 $texte .=
'<table class="nobordernopadding" width="100%">';
126 $texte .=
'<tr><td>';
127 $texttitle = $langs->trans(
"ListOfDirectories");
128 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
$conf->global->EXPEDITION_ADDON_PDF_ODT_PATH)));
129 $listoffiles = array();
130 foreach ($listofdir as $key => $tmpdir) {
131 $tmpdir = trim($tmpdir);
132 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
134 unset($listofdir[$key]);
137 if (!is_dir($tmpdir)) {
138 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
140 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
141 if (count($tmpfiles)) {
142 $listoffiles = array_merge($listoffiles, $tmpfiles);
146 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
147 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
149 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
150 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
152 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
153 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
154 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
156 $texte .=
'</textarea>';
157 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
158 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans(
"Modify").
'">';
159 $texte .=
'<br></div></div>';
162 $nbofiles = count($listoffiles);
164 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
166 $texte .= count($listoffiles);
171 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
173 foreach ($listoffiles as $file) {
174 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=shipments/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
175 $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>';
181 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
183 $maxmin = $maxfilesizearray[
'maxmin'];
185 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
187 $texte .=
' <input type="file" name="uploadfile">';
188 $texte .=
'<input type="hidden" value="EXPEDITION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
189 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
195 $texte .=
'</table>';
213 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
216 global $user, $langs,
$conf, $mysoc, $hookmanager;
218 if (empty($srctemplatepath)) {
219 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
224 if (!is_object($hookmanager)) {
225 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
228 $hookmanager->initHooks(array(
'odtgeneration'));
231 if (!is_object($outputlangs)) {
232 $outputlangs = $langs;
234 $sav_charset_output = $outputlangs->charset_output;
235 $outputlangs->charset_output =
'UTF-8';
238 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
240 if (
$conf->expedition->multidir_output[
$object->entity].
"/sending") {
254 $dir =
$conf->expedition->multidir_output[
$object->entity].
"/sending";
256 if (!preg_match(
'/specimen/i', $objectref)) {
257 $dir .=
"/".$objectref;
259 $file = $dir.
"/".$objectref.
".odt";
261 if (!file_exists($dir)) {
263 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
268 if (file_exists($dir)) {
270 $newfile = basename($srctemplatepath);
271 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
272 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
273 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
274 $newfiletmp = $objectref .
'_' . $newfiletmp;
277 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
280 if ($format ==
'1') {
281 $format =
'%Y%m%d%H%M%S';
285 $filename = $newfiletmp .
'.' . $newfileformat;
287 $file = $dir .
'/' . $filename;
294 if (!is_writable(
$conf->expedition->dir_temp)) {
295 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->expedition->dir_temp);
296 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
302 $arrayidcontact =
$object->getIdContact(
'external',
'SHIPPING');
303 if (count($arrayidcontact) > 0) {
305 $result =
$object->fetch_contact($arrayidcontact[0]);
309 $contactobject =
null;
310 if (!empty($usecontact)) {
313 $object->contact->fetch_thirdparty();
314 $socobject =
$object->contact->thirdparty;
315 $contactobject =
$object->contact;
317 $socobject =
$object->thirdparty;
319 $contactobject =
$object->contact;
322 $socobject =
$object->thirdparty;
326 $substitutionarray = array(
327 '__FROM_NAME__' => $this->emetteur->name,
328 '__FROM_EMAIL__' => $this->emetteur->email,
329 '__TOTAL_TTC__' =>
$object->total_ttc,
330 '__TOTAL_HT__' =>
$object->total_ht,
331 '__TOTAL_VAT__' =>
$object->total_tva
335 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
336 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
340 $paramfreetext =
'EXPEDITION_FREE_TEXT';
346 require_once ODTPHP_PATH.
'odf.php';
348 $odfHandler =
new Odf(
351 'PATH_TO_TMP' =>
$conf->expedition->dir_temp,
353 'DELIMITER_LEFT' =>
'{',
354 'DELIMITER_RIGHT' =>
'}'
358 $this->error = $e->getMessage();
371 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
372 }
catch (OdfException $e) {
385 foreach ($tmparray as $key => $value) {
387 if (preg_match(
'/logo$/', $key)) {
389 if (file_exists($value)) {
390 $odfHandler->setImage($key, $value, $ratio);
392 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
395 $odfHandler->setVars($key, $value,
true,
'UTF-8');
397 }
catch (OdfException $e) {
403 foreach ($tmparray as $key => $value) {
405 if (preg_match(
'/logo$/', $key)) {
407 if (file_exists($value)) {
408 $odfHandler->setImage($key, $value, $ratio);
410 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
413 $odfHandler->setVars($key, $value,
true,
'UTF-8');
415 }
catch (OdfException $e) {
420 if ($socobject->element ==
'contact') {
426 foreach ($tmparray as $key => $value) {
428 if (preg_match(
'/logo$/', $key)) {
429 if (file_exists($value)) {
430 $odfHandler->setImage($key, $value, $ratio);
432 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
435 $odfHandler->setVars($key, $value,
true,
'UTF-8');
437 }
catch (OdfException $e) {
442 if ($usecontact && is_object($contactobject)) {
444 foreach ($tmparray as $key => $value) {
446 if (preg_match(
'/logo$/', $key)) {
447 if (file_exists($value)) {
448 $odfHandler->setImage($key, $value, $ratio);
450 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
453 $odfHandler->setVars($key, $value,
true,
'UTF-8');
455 }
catch (OdfException $e) {
467 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
468 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
470 foreach ($tmparray as $key => $value) {
472 if (preg_match(
'/logo$/', $key)) {
474 if (file_exists($value)) {
475 $odfHandler->setImage($key, $value, $ratio);
477 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
481 $odfHandler->setVars($key, $value,
true,
'UTF-8');
483 }
catch (OdfException $e) {
489 $foundtagforlines = 1;
491 $listlines = $odfHandler->setSegment(
'lines');
492 }
catch (OdfExceptionSegmentNotFound $e) {
494 $foundtagforlines = 0;
497 if ($foundtagforlines) {
499 foreach (
$object->lines as $line) {
504 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
505 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
506 foreach ($tmparray as $key => $val) {
508 $listlines->setVars($key, $val,
true,
'UTF-8');
509 }
catch (SegmentException $e) {
516 $odfHandler->mergeSegment($listlines);
517 }
catch (OdfException $e) {
518 $this->error = $e->getMessage();
525 $tmparray = $outputlangs->get_translations_for_substitutions();
526 foreach ($tmparray as $key => $value) {
528 $odfHandler->setVars($key, $value,
true,
'UTF-8');
529 }
catch (OdfException $e) {
535 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
536 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
541 $odfHandler->exportAsAttachedPDF($file);
543 $this->error = $e->getMessage();
549 $odfHandler->saveToDisk($file);
551 $this->error = $e->getMessage();
557 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
558 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
564 $this->result = array(
'fullpath' => $file);
568 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);