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->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;
88 if ($mysoc ===
null) {
94 $this->emetteur = $mysoc;
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);
117 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
118 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
119 $texte .=
'<input type="hidden" name="page_y" value="">';
120 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
121 $texte .=
'<input type="hidden" name="param1" value="EXPEDITION_ADDON_PDF_ODT_PATH">';
122 $texte .=
'<table class="nobordernopadding" width="100%">';
125 $texte .=
'<tr><td>';
126 $texttitle = $langs->trans(
"ListOfDirectories");
127 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
$conf->global->EXPEDITION_ADDON_PDF_ODT_PATH)));
128 $listoffiles = array();
129 foreach ($listofdir as $key => $tmpdir) {
130 $tmpdir = trim($tmpdir);
131 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
133 unset($listofdir[$key]);
136 if (!is_dir($tmpdir)) {
137 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
139 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
140 if (count($tmpfiles)) {
141 $listoffiles = array_merge($listoffiles, $tmpfiles);
145 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
146 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
148 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
149 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
151 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
152 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
153 $texte .=
'<textarea class="flat" cols="60" name="value1">';
155 $texte .=
'</textarea>';
156 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
157 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans(
"Modify").
'">';
158 $texte .=
'<br></div></div>';
161 $nbofiles = count($listoffiles);
163 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
165 $texte .= count($listoffiles);
170 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
172 foreach ($listoffiles as $file) {
173 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=shipments/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
174 $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>';
180 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
182 $maxmin = $maxfilesizearray[
'maxmin'];
184 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
186 $texte .=
' <input type="file" name="uploadfile">';
187 $texte .=
'<input type="hidden" value="EXPEDITION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
188 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
194 $texte .=
'</table>';
212 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 global $user, $langs,
$conf, $mysoc, $hookmanager;
217 if (empty($srctemplatepath)) {
218 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
223 if (!is_object($hookmanager)) {
224 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
227 $hookmanager->initHooks(array(
'odtgeneration'));
230 if (!is_object($outputlangs)) {
231 $outputlangs = $langs;
233 $sav_charset_output = $outputlangs->charset_output;
234 $outputlangs->charset_output =
'UTF-8';
237 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
239 if (
$conf->expedition->multidir_output[
$object->entity].
"/sending") {
253 $dir =
$conf->expedition->multidir_output[
$object->entity].
"/sending";
255 if (!preg_match(
'/specimen/i', $objectref)) {
256 $dir .=
"/".$objectref;
258 $file = $dir.
"/".$objectref.
".odt";
260 if (!file_exists($dir)) {
262 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
267 if (file_exists($dir)) {
269 $newfile = basename($srctemplatepath);
270 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
271 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
272 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
273 $newfiletmp = $objectref .
'_' . $newfiletmp;
276 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
279 if ($format ==
'1') {
280 $format =
'%Y%m%d%H%M%S';
284 $filename = $newfiletmp .
'.' . $newfileformat;
286 $file = $dir .
'/' . $filename;
293 if (!is_writable(
$conf->expedition->dir_temp)) {
294 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->expedition->dir_temp);
295 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
301 $arrayidcontact =
$object->getIdContact(
'external',
'SHIPPING');
302 if (count($arrayidcontact) > 0) {
304 $result =
$object->fetch_contact($arrayidcontact[0]);
308 $contactobject =
null;
309 if (!empty($usecontact)) {
312 $object->contact->fetch_thirdparty();
313 $socobject =
$object->contact->thirdparty;
314 $contactobject =
$object->contact;
316 $socobject =
$object->thirdparty;
318 $contactobject =
$object->contact;
321 $socobject =
$object->thirdparty;
325 $substitutionarray = array(
326 '__FROM_NAME__' => $this->emetteur->name,
327 '__FROM_EMAIL__' => $this->emetteur->email,
328 '__TOTAL_TTC__' =>
$object->total_ttc,
329 '__TOTAL_HT__' =>
$object->total_ht,
330 '__TOTAL_VAT__' =>
$object->total_tva
334 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
335 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
339 $paramfreetext =
'EXPEDITION_FREE_TEXT';
345 require_once ODTPHP_PATH.
'odf.php';
347 $odfHandler =
new Odf(
350 'PATH_TO_TMP' =>
$conf->expedition->dir_temp,
352 'DELIMITER_LEFT' =>
'{',
353 'DELIMITER_RIGHT' =>
'}'
357 $this->error = $e->getMessage();
370 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
371 }
catch (OdfException $e) {
377 foreach ($tmparray as $key => $value) {
379 if (preg_match(
'/logo$/', $key)) {
381 if (file_exists($value)) {
382 $odfHandler->setImage($key, $value);
384 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
387 $odfHandler->setVars($key, $value,
true,
'UTF-8');
389 }
catch (OdfException $e) {
395 foreach ($tmparray as $key => $value) {
397 if (preg_match(
'/logo$/', $key)) {
399 if (file_exists($value)) {
400 $odfHandler->setImage($key, $value);
402 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
405 $odfHandler->setVars($key, $value,
true,
'UTF-8');
407 }
catch (OdfException $e) {
412 if ($socobject->element ==
'contact') {
418 foreach ($tmparray as $key => $value) {
420 if (preg_match(
'/logo$/', $key)) {
421 if (file_exists($value)) {
422 $odfHandler->setImage($key, $value);
424 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
427 $odfHandler->setVars($key, $value,
true,
'UTF-8');
429 }
catch (OdfException $e) {
434 if ($usecontact && is_object($contactobject)) {
436 foreach ($tmparray as $key => $value) {
438 if (preg_match(
'/logo$/', $key)) {
439 if (file_exists($value)) {
440 $odfHandler->setImage($key, $value);
442 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
445 $odfHandler->setVars($key, $value,
true,
'UTF-8');
447 }
catch (OdfException $e) {
459 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
460 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
462 foreach ($tmparray as $key => $value) {
464 if (preg_match(
'/logo$/', $key)) {
466 if (file_exists($value)) {
467 $odfHandler->setImage($key, $value);
469 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
473 $odfHandler->setVars($key, $value,
true,
'UTF-8');
475 }
catch (OdfException $e) {
481 $foundtagforlines = 1;
483 $listlines = $odfHandler->setSegment(
'lines');
484 }
catch (OdfExceptionSegmentNotFound $e) {
486 $foundtagforlines = 0;
489 if ($foundtagforlines) {
491 foreach (
$object->lines as $line) {
496 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
497 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
498 foreach ($tmparray as $key => $val) {
500 $listlines->setVars($key, $val,
true,
'UTF-8');
501 }
catch (SegmentException $e) {
508 $odfHandler->mergeSegment($listlines);
509 }
catch (OdfException $e) {
510 $this->error = $e->getMessage();
517 $tmparray = $outputlangs->get_translations_for_substitutions();
518 foreach ($tmparray as $key => $value) {
520 $odfHandler->setVars($key, $value,
true,
'UTF-8');
521 }
catch (OdfException $e) {
527 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
528 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
533 $odfHandler->exportAsAttachedPDF($file);
535 $this->error = $e->getMessage();
541 $odfHandler->saveToDisk($file);
543 $this->error = $e->getMessage();
549 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
550 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
556 $this->result = array(
'fullpath' => $file);
560 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);