30 require_once DOL_DOCUMENT_ROOT.
'/core/modules/expedition/modules_expedition.php';
31 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
35 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
53 public $phpmin = array(7, 0);
59 public $version =
'dolibarr';
69 global $conf, $langs, $mysoc;
72 $langs->loadLangs(array(
"main",
"companies"));
75 $this->
name =
"ODT templates";
76 $this->
description = $langs->trans(
"DocumentModelOdt");
77 $this->scandir =
'EXPEDITION_ADDON_PDF_ODT_PATH';
81 $this->page_largeur = 0;
82 $this->page_hauteur = 0;
83 $this->format = array($this->page_largeur, $this->page_hauteur);
84 $this->marge_gauche = 0;
85 $this->marge_droite = 0;
86 $this->marge_haute = 0;
87 $this->marge_basse = 0;
89 $this->option_logo = 1;
90 $this->option_tva = 0;
91 $this->option_modereg = 0;
92 $this->option_condreg = 0;
93 $this->option_multilang = 1;
94 $this->option_escompte = 0;
95 $this->option_credit_note = 0;
96 $this->option_freetext = 1;
97 $this->option_draft_watermark = 0;
100 $this->emetteur = $mysoc;
101 if (!$this->emetteur->country_code) {
102 $this->emetteur->country_code = substr($langs->defaultlang, -2);
115 global $conf, $langs;
118 $langs->loadLangs(array(
"errors",
"companies"));
123 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
124 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
125 $texte .=
'<input type="hidden" name="page_y" value="">';
126 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
127 $texte .=
'<input type="hidden" name="param1" value="EXPEDITION_ADDON_PDF_ODT_PATH">';
128 $texte .=
'<table class="nobordernopadding" width="100%">';
131 $texte .=
'<tr><td>';
132 $texttitle = $langs->trans(
"ListOfDirectories");
133 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->EXPEDITION_ADDON_PDF_ODT_PATH)));
134 $listoffiles = array();
135 foreach ($listofdir as $key => $tmpdir) {
136 $tmpdir = trim($tmpdir);
137 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
139 unset($listofdir[$key]);
142 if (!is_dir($tmpdir)) {
143 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
145 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
146 if (count($tmpfiles)) {
147 $listoffiles = array_merge($listoffiles, $tmpfiles);
151 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
153 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
154 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
156 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
157 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
158 $texte .=
'<textarea class="flat" cols="60" name="value1">';
159 $texte .= $conf->global->EXPEDITION_ADDON_PDF_ODT_PATH;
160 $texte .=
'</textarea>';
161 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
162 $texte .=
'<input type="submit" class="button button-edit reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
163 $texte .=
'<br></div></div>';
166 $nbofiles = count($listoffiles);
167 if (!empty($conf->global->EXPEDITION_ADDON_PDF_ODT_PATH)) {
168 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
170 $texte .= count($listoffiles);
175 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
177 foreach ($listoffiles as $file) {
178 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=shipments/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
179 $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>';
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 small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
197 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
198 $texte .=
'<span class="opacitymedium">';
199 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
204 $texte .=
'</table>';
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)) {
228 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
233 if (!is_object($hookmanager)) {
234 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
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';
247 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
249 if ($conf->expedition->dir_output.
"/sending") {
251 if (!is_object($object)) {
254 $result = $object->fetch($id);
261 $object->fetch_thirdparty();
263 $dir = $conf->expedition->dir_output.
"/sending";
265 if (!preg_match(
'/specimen/i', $objectref)) {
266 $dir .=
"/".$objectref;
268 $file = $dir.
"/".$objectref.
".odt";
270 if (!file_exists($dir)) {
272 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
277 if (file_exists($dir)) {
279 $newfile = basename($srctemplatepath);
280 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
281 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
282 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
283 $newfiletmp = $objectref .
'_' . $newfiletmp;
286 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
287 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
288 $format = $conf->global->MAIN_DOC_USE_TIMING;
289 if ($format ==
'1') {
290 $format =
'%Y%m%d%H%M%S';
294 $filename = $newfiletmp .
'.' . $newfileformat;
296 $file = $dir .
'/' . $filename;
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);
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)) {
321 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
322 $object->contact->fetch_thirdparty();
323 $socobject = $object->contact->thirdparty;
324 $contactobject = $object->contact;
326 $socobject = $object->thirdparty;
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
344 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
345 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
349 $paramfreetext =
'EXPEDITION_FREE_TEXT';
350 if (!empty($conf->global->$paramfreetext)) {
355 require_once ODTPHP_PATH.
'odf.php';
357 $odfHandler =
new odf(
360 'PATH_TO_TMP' => $conf->expedition->dir_temp,
361 'ZIP_PROXY' =>
'PclZipProxy',
362 'DELIMITER_LEFT' =>
'{',
363 'DELIMITER_RIGHT' =>
'}'
367 $this->error = $e->getMessage();
380 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
381 }
catch (OdfException $e) {
388 foreach ($tmparray as $key => $value) {
390 if (preg_match(
'/logo$/', $key)) {
392 if (file_exists($value)) {
393 $odfHandler->setImage($key, $value);
395 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
399 $odfHandler->setVars($key, $value,
true,
'UTF-8');
401 }
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);
415 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
419 $odfHandler->setVars($key, $value,
true,
'UTF-8');
421 }
catch (OdfException $e) {
426 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);
437 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
441 $odfHandler->setVars($key, $value,
true,
'UTF-8');
443 }
catch (OdfException $e) {
448 if ($usecontact && is_object($contactobject)) {
450 foreach ($tmparray as $key => $value) {
452 if (preg_match(
'/logo$/', $key)) {
453 if (file_exists($value)) {
454 $odfHandler->setImage($key, $value);
456 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
460 $odfHandler->setVars($key, $value,
true,
'UTF-8');
462 }
catch (OdfException $e) {
473 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
474 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
475 foreach ($tmparray as $key => $value) {
477 if (preg_match(
'/logo$/', $key)) {
478 if (file_exists($value)) {
479 $odfHandler->setImage($key, $value);
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 (OdfException $e) {
498 $foundtagforlines = 0;
501 if ($foundtagforlines) {
502 foreach ($object->lines as $line) {
506 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
507 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
508 foreach ($tmparray as $key => $val) {
510 $listlines->setVars($key, $val,
true,
'UTF-8');
511 }
catch (OdfException $e) {
513 }
catch (SegmentException $e) {
519 $odfHandler->mergeSegment($listlines);
521 }
catch (OdfException $e) {
522 $this->error = $e->getMessage();
528 $tmparray = $outputlangs->get_translations_for_substitutions();
529 foreach ($tmparray as $key => $value) {
531 $odfHandler->setVars($key, $value,
true,
'UTF-8');
532 }
catch (OdfException $e) {
538 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
539 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
542 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
544 $odfHandler->exportAsAttachedPDF($file);
546 $this->error = $e->getMessage();
552 $odfHandler->saveToDisk($file);
554 $this->error = $e->getMessage();
559 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
560 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
562 if (!empty($conf->global->MAIN_UMASK)) {
563 @chmod($file, octdec($conf->global->MAIN_UMASK));
568 $this->result = array(
'fullpath'=>$file);
572 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);