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;
89 $this->emetteur = $mysoc;
90 if (!$this->emetteur->country_code) {
91 $this->emetteur->country_code = substr($langs->defaultlang, -2);
104 global $conf, $langs;
107 $langs->loadLangs(array(
"errors",
"companies"));
109 $form =
new Form($this->db);
112 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
113 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
114 $texte .=
'<input type="hidden" name="page_y" value="">';
115 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
116 $texte .=
'<input type="hidden" name="param1" value="EXPEDITION_ADDON_PDF_ODT_PATH">';
117 $texte .=
'<table class="nobordernopadding" width="100%">';
120 $texte .=
'<tr><td>';
121 $texttitle = $langs->trans(
"ListOfDirectories");
122 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->EXPEDITION_ADDON_PDF_ODT_PATH)));
123 $listoffiles = array();
124 foreach ($listofdir as $key => $tmpdir) {
125 $tmpdir = trim($tmpdir);
126 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
128 unset($listofdir[$key]);
131 if (!is_dir($tmpdir)) {
132 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
134 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
135 if (count($tmpfiles)) {
136 $listoffiles = array_merge($listoffiles, $tmpfiles);
140 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
141 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
143 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
144 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
146 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
147 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
148 $texte .=
'<textarea class="flat" cols="60" name="value1">';
150 $texte .=
'</textarea>';
151 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
152 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans(
"Modify").
'">';
153 $texte .=
'<br></div></div>';
156 $nbofiles = count($listoffiles);
158 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
160 $texte .= count($listoffiles);
165 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
167 foreach ($listoffiles as $file) {
168 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=shipments/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
169 $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 .=
'<div>'.$langs->trans(
"UploadNewTemplate");
177 $maxmin = $maxfilesizearray[
'maxmin'];
179 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
181 $texte .=
' <input type="file" name="uploadfile">';
182 $texte .=
'<input type="hidden" value="EXPEDITION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
183 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
189 $texte .=
'</table>';
207 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
210 global $user, $langs, $conf, $mysoc, $hookmanager;
212 if (empty($srctemplatepath)) {
213 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
218 if (!is_object($hookmanager)) {
219 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
222 $hookmanager->initHooks(array(
'odtgeneration'));
225 if (!is_object($outputlangs)) {
226 $outputlangs = $langs;
228 $sav_charset_output = $outputlangs->charset_output;
229 $outputlangs->charset_output =
'UTF-8';
232 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
234 if ($conf->expedition->multidir_output[
$object->entity].
"/sending") {
248 $dir = $conf->expedition->multidir_output[
$object->entity].
"/sending";
250 if (!preg_match(
'/specimen/i', $objectref)) {
251 $dir .=
"/".$objectref;
253 $file = $dir.
"/".$objectref.
".odt";
255 if (!file_exists($dir)) {
257 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
262 if (file_exists($dir)) {
264 $newfile = basename($srctemplatepath);
265 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
266 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
267 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
268 $newfiletmp = $objectref .
'_' . $newfiletmp;
271 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
274 if ($format ==
'1') {
275 $format =
'%Y%m%d%H%M%S';
279 $filename = $newfiletmp .
'.' . $newfileformat;
281 $file = $dir .
'/' . $filename;
288 if (!is_writable($conf->expedition->dir_temp)) {
289 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->expedition->dir_temp);
290 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
296 $arrayidcontact =
$object->getIdContact(
'external',
'SHIPPING');
297 if (count($arrayidcontact) > 0) {
299 $result =
$object->fetch_contact($arrayidcontact[0]);
303 $contactobject =
null;
304 if (!empty($usecontact)) {
306 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
307 $object->contact->fetch_thirdparty();
308 $socobject =
$object->contact->thirdparty;
309 $contactobject =
$object->contact;
311 $socobject =
$object->thirdparty;
313 $contactobject =
$object->contact;
316 $socobject =
$object->thirdparty;
320 $substitutionarray = array(
321 '__FROM_NAME__' => $this->emetteur->name,
322 '__FROM_EMAIL__' => $this->emetteur->email,
323 '__TOTAL_TTC__' =>
$object->total_ttc,
324 '__TOTAL_HT__' =>
$object->total_ht,
325 '__TOTAL_VAT__' =>
$object->total_tva
329 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
330 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
334 $paramfreetext =
'EXPEDITION_FREE_TEXT';
340 require_once ODTPHP_PATH.
'odf.php';
342 $odfHandler =
new Odf(
345 'PATH_TO_TMP' => $conf->expedition->dir_temp,
346 'ZIP_PROXY' =>
'PclZipProxy',
347 'DELIMITER_LEFT' =>
'{',
348 'DELIMITER_RIGHT' =>
'}'
352 $this->error = $e->getMessage();
365 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
366 }
catch (OdfException $e) {
372 foreach ($tmparray as $key => $value) {
374 if (preg_match(
'/logo$/', $key)) {
376 if (file_exists($value)) {
377 $odfHandler->setImage($key, $value);
379 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
382 $odfHandler->setVars($key, $value,
true,
'UTF-8');
384 }
catch (OdfException $e) {
390 foreach ($tmparray as $key => $value) {
392 if (preg_match(
'/logo$/', $key)) {
394 if (file_exists($value)) {
395 $odfHandler->setImage($key, $value);
397 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
400 $odfHandler->setVars($key, $value,
true,
'UTF-8');
402 }
catch (OdfException $e) {
407 if ($socobject->element ==
'contact') {
413 foreach ($tmparray as $key => $value) {
415 if (preg_match(
'/logo$/', $key)) {
416 if (file_exists($value)) {
417 $odfHandler->setImage($key, $value);
419 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
422 $odfHandler->setVars($key, $value,
true,
'UTF-8');
424 }
catch (OdfException $e) {
429 if ($usecontact && is_object($contactobject)) {
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');
440 $odfHandler->setVars($key, $value,
true,
'UTF-8');
442 }
catch (OdfException $e) {
454 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
455 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
457 foreach ($tmparray as $key => $value) {
459 if (preg_match(
'/logo$/', $key)) {
461 if (file_exists($value)) {
462 $odfHandler->setImage($key, $value);
464 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
468 $odfHandler->setVars($key, $value,
true,
'UTF-8');
470 }
catch (OdfException $e) {
476 $foundtagforlines = 1;
478 $listlines = $odfHandler->setSegment(
'lines');
479 }
catch (OdfExceptionSegmentNotFound $e) {
481 $foundtagforlines = 0;
484 if ($foundtagforlines) {
486 foreach (
$object->lines as $line) {
491 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
492 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
493 foreach ($tmparray as $key => $val) {
495 $listlines->setVars($key, $val,
true,
'UTF-8');
496 }
catch (SegmentException $e) {
503 $odfHandler->mergeSegment($listlines);
504 }
catch (OdfException $e) {
505 $this->error = $e->getMessage();
512 $tmparray = $outputlangs->get_translations_for_substitutions();
513 foreach ($tmparray as $key => $value) {
515 $odfHandler->setVars($key, $value,
true,
'UTF-8');
516 }
catch (OdfException $e) {
522 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
523 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
528 $odfHandler->exportAsAttachedPDF($file);
530 $this->error = $e->getMessage();
536 $odfHandler->saveToDisk($file);
538 $this->error = $e->getMessage();
544 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
545 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
551 $this->result = array(
'fullpath' => $file);
555 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);