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(5, 6);
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><br>';
183 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
185 $maxmin = $maxfilesizearray[
'maxmin'];
187 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
189 $texte .=
' <input type="file" name="uploadfile">';
190 $texte .=
'<input type="hidden" value="EXPEDITION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
191 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
195 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
196 $texte .=
'<span class="opacitymedium">';
197 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
202 $texte .=
'</table>';
220 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
223 global $user, $langs, $conf, $mysoc, $hookmanager;
225 if (empty($srctemplatepath)) {
226 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
231 if (!is_object($hookmanager)) {
232 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
235 $hookmanager->initHooks(array(
'odtgeneration'));
238 if (!is_object($outputlangs)) {
239 $outputlangs = $langs;
241 $sav_charset_output = $outputlangs->charset_output;
242 $outputlangs->charset_output =
'UTF-8';
245 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
247 if ($conf->expedition->dir_output.
"/sending") {
249 if (!is_object($object)) {
252 $result = $object->fetch($id);
259 $object->fetch_thirdparty();
261 $dir = $conf->expedition->dir_output.
"/sending";
263 if (!preg_match(
'/specimen/i', $objectref)) {
264 $dir .=
"/".$objectref;
266 $file = $dir.
"/".$objectref.
".odt";
268 if (!file_exists($dir)) {
270 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
275 if (file_exists($dir)) {
277 $newfile = basename($srctemplatepath);
278 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
279 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
280 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
281 $newfiletmp = $objectref.
'_'.$newfiletmp;
284 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
285 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
286 $format = $conf->global->MAIN_DOC_USE_TIMING;
287 if ($format ==
'1') {
288 $format =
'%Y%m%d%H%M%S';
292 $filename = $newfiletmp.
'.'.$newfileformat;
294 $file = $dir.
'/'.$filename;
301 if (!is_writable($conf->expedition->dir_temp)) {
302 $this->error =
"Failed to write in temp directory ".$conf->expedition->dir_temp;
303 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
309 $arrayidcontact = $object->getIdContact(
'external',
'SHIPPING');
310 if (count($arrayidcontact) > 0) {
312 $result = $object->fetch_contact($arrayidcontact[0]);
316 $contactobject =
null;
317 if (!empty($usecontact)) {
319 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))) {
320 $object->contact->fetch_thirdparty();
321 $socobject = $object->contact->thirdparty;
322 $contactobject = $object->contact;
324 $socobject = $object->thirdparty;
326 $contactobject = $object->contact;
329 $socobject = $object->thirdparty;
333 $substitutionarray = array(
334 '__FROM_NAME__' => $this->emetteur->name,
335 '__FROM_EMAIL__' => $this->emetteur->email,
336 '__TOTAL_TTC__' => $object->total_ttc,
337 '__TOTAL_HT__' => $object->total_ht,
338 '__TOTAL_VAT__' => $object->total_tva
342 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
343 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
347 $paramfreetext =
'EXPEDITION_FREE_TEXT';
348 if (!empty($conf->global->$paramfreetext)) {
353 require_once ODTPHP_PATH.
'odf.php';
355 $odfHandler =
new odf(
358 'PATH_TO_TMP' => $conf->expedition->dir_temp,
359 'ZIP_PROXY' =>
'PclZipProxy',
360 'DELIMITER_LEFT' =>
'{',
361 'DELIMITER_RIGHT' =>
'}'
365 $this->error = $e->getMessage();
378 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
379 }
catch (OdfException $e) {
386 foreach ($tmparray as $key => $value) {
388 if (preg_match(
'/logo$/', $key)) {
390 if (file_exists($value)) {
391 $odfHandler->setImage($key, $value);
393 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
397 $odfHandler->setVars($key, $value,
true,
'UTF-8');
399 }
catch (OdfException $e) {
406 foreach ($tmparray as $key => $value) {
408 if (preg_match(
'/logo$/', $key)) {
410 if (file_exists($value)) {
411 $odfHandler->setImage($key, $value);
413 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
417 $odfHandler->setVars($key, $value,
true,
'UTF-8');
419 }
catch (OdfException $e) {
424 if ($socobject->element ==
'contact') {
429 foreach ($tmparray as $key => $value) {
431 if (preg_match(
'/logo$/', $key)) {
432 if (file_exists($value)) {
433 $odfHandler->setImage($key, $value);
435 $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);
454 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
458 $odfHandler->setVars($key, $value,
true,
'UTF-8');
460 }
catch (OdfException $e) {
471 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
472 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
473 foreach ($tmparray as $key => $value) {
475 if (preg_match(
'/logo$/', $key)) {
476 if (file_exists($value)) {
477 $odfHandler->setImage($key, $value);
479 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
483 $odfHandler->setVars($key, $value,
true,
'UTF-8');
485 }
catch (OdfException $e) {
491 $foundtagforlines = 1;
493 $listlines = $odfHandler->setSegment(
'lines');
494 }
catch (OdfException $e) {
496 $foundtagforlines = 0;
499 if ($foundtagforlines) {
500 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 (OdfException $e) {
511 }
catch (SegmentException $e) {
517 $odfHandler->mergeSegment($listlines);
519 }
catch (OdfException $e) {
520 $this->error = $e->getMessage();
526 $tmparray = $outputlangs->get_translations_for_substitutions();
527 foreach ($tmparray as $key => $value) {
529 $odfHandler->setVars($key, $value,
true,
'UTF-8');
530 }
catch (OdfException $e) {
536 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
537 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
540 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
542 $odfHandler->exportAsAttachedPDF($file);
544 $this->error = $e->getMessage();
550 $odfHandler->saveToDisk($file);
552 $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);
560 if (!empty($conf->global->MAIN_UMASK)) {
561 @chmod($file, octdec($conf->global->MAIN_UMASK));
566 $this->result = array(
'fullpath'=>$file);
570 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);