26 require_once DOL_DOCUMENT_ROOT.
'/core/modules/reception/modules_reception.php';
27 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
47 public $version =
'dolibarr';
57 global $conf, $langs, $mysoc;
59 $langs->loadLangs(array(
"main",
"companies"));
62 $this->
name =
"ODT templates";
63 $this->
description = $langs->trans(
"DocumentModelOdt");
64 $this->scandir =
'RECEPTION_ADDON_PDF_ODT_PATH';
68 $this->page_largeur = 0;
69 $this->page_hauteur = 0;
70 $this->format = array($this->page_largeur, $this->page_hauteur);
71 $this->marge_gauche = 0;
72 $this->marge_droite = 0;
73 $this->marge_haute = 0;
74 $this->marge_basse = 0;
76 $this->option_logo = 1;
77 $this->option_tva = 0;
78 $this->option_modereg = 0;
79 $this->option_condreg = 0;
80 $this->option_multilang = 1;
81 $this->option_escompte = 0;
82 $this->option_credit_note = 0;
83 $this->option_freetext = 1;
84 $this->option_draft_watermark = 0;
87 $this->emetteur = $mysoc;
88 if (!$this->emetteur->country_code) {
89 $this->emetteur->country_code = substr($langs->defaultlang, -2);
102 global $conf, $langs;
104 $langs->load(
"companies");
105 $langs->load(
"errors");
110 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
111 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
112 $texte .=
'<input type="hidden" name="page_y" value="">';
113 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
114 $texte .=
'<input type="hidden" name="param1" value="RECEPTION_ADDON_PDF_ODT_PATH">';
115 $texte .=
'<table class="nobordernopadding centpercent">';
118 $texte .=
'<tr><td>';
119 $texttitle = $langs->trans(
"ListOfDirectories");
120 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->RECEPTION_ADDON_PDF_ODT_PATH)));
121 $listoffiles = array();
122 foreach ($listofdir as $key => $tmpdir) {
123 $tmpdir = trim($tmpdir);
124 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
126 unset($listofdir[$key]);
129 if (!is_dir($tmpdir)) {
130 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
132 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
133 if (count($tmpfiles)) {
134 $listoffiles = array_merge($listoffiles, $tmpfiles);
138 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
140 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
141 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
143 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
144 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
145 $texte .=
'<textarea class="flat" cols="60" name="value1">';
146 $texte .= $conf->global->RECEPTION_ADDON_PDF_ODT_PATH;
147 $texte .=
'</textarea>';
148 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
149 $texte .=
'<input type="submit" class="button button-edit small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
150 $texte .=
'<br></div></div>';
153 $nbofiles = count($listoffiles);
154 if (!empty($conf->global->RECEPTION_ADDON_PDF_ODT_PATH)) {
155 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
157 $texte .= count($listoffiles);
162 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
164 foreach ($listoffiles as $file) {
165 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=receptions/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
166 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=RECEPTION_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
172 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
174 $maxmin = $maxfilesizearray[
'maxmin'];
176 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
178 $texte .=
' <input type="file" name="uploadfile">';
179 $texte .=
'<input type="hidden" value="RECEPTION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
180 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
185 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
186 $texte .=
'<span class="opacitymedium">';
187 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
192 $texte .=
'</table>';
210 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
213 global $user, $langs, $conf, $mysoc, $hookmanager;
215 if (empty($srctemplatepath)) {
216 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
221 if (!is_object($hookmanager)) {
222 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
225 $hookmanager->initHooks(array(
'odtgeneration'));
228 if (!is_object($outputlangs)) {
229 $outputlangs = $langs;
231 $sav_charset_output = $outputlangs->charset_output;
232 $outputlangs->charset_output =
'UTF-8';
234 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
236 if ($conf->reception->dir_output.
"/reception") {
238 if (!is_object($object)) {
241 $result = $object->fetch($id);
248 $object->fetch_thirdparty();
250 $dir = $conf->reception->dir_output.
"/reception";
252 if (!preg_match(
'/specimen/i', $objectref)) {
253 $dir .=
"/".$objectref;
255 $file = $dir.
"/".$objectref.
".odt";
257 if (!file_exists($dir)) {
259 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
264 if (file_exists($dir)) {
266 $newfile = basename($srctemplatepath);
267 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
268 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
269 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
270 $newfiletmp = $objectref .
'_' . $newfiletmp;
273 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
276 if ($format ==
'1') {
277 $format =
'%Y%m%d%H%M%S';
281 $filename = $newfiletmp .
'.' . $newfileformat;
283 $file = $dir .
'/' . $filename;
290 if (!is_writable($conf->reception->dir_temp)) {
291 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->reception->dir_temp);
292 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
298 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
299 if (count($arrayidcontact) > 0) {
301 $result = $object->fetch_contact($arrayidcontact[0]);
305 $contactobject =
null;
306 if (!empty($usecontact)) {
308 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))) {
309 $object->contact->fetch_thirdparty();
310 $socobject = $object->contact->thirdparty;
311 $contactobject = $object->contact;
313 $socobject = $object->thirdparty;
315 $contactobject = $object->contact;
318 $socobject = $object->thirdparty;
322 $substitutionarray = array(
323 '__FROM_NAME__' => $this->emetteur->name,
324 '__FROM_EMAIL__' => $this->emetteur->email,
325 '__TOTAL_TTC__' => $object->total_ttc,
326 '__TOTAL_HT__' => $object->total_ht,
327 '__TOTAL_VAT__' => $object->total_tva
332 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
333 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
337 $paramfreetext =
'RECEPTION_FREE_TEXT';
338 if (!empty($conf->global->$paramfreetext)) {
343 require_once ODTPHP_PATH.
'odf.php';
345 $odfHandler =
new Odf(
348 'PATH_TO_TMP' => $conf->reception->dir_temp,
349 'ZIP_PROXY' =>
'PclZipProxy',
350 'DELIMITER_LEFT' =>
'{',
351 'DELIMITER_RIGHT' =>
'}'
355 $this->error = $e->getMessage();
368 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
369 }
catch (OdfException $e) {
382 $array_thirdparty_contact = array();
383 if ($usecontact && is_object($contactobject)) {
387 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
391 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
392 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
393 foreach ($tmparray as $key => $value) {
395 if (preg_match(
'/logo$/', $key)) {
396 if (file_exists($value)) {
397 $odfHandler->setImage($key, $value);
399 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
403 $odfHandler->setVars($key, $value,
true,
'UTF-8');
405 }
catch (OdfException $e) {
411 $foundtagforlines = 1;
413 $listlines = $odfHandler->setSegment(
'lines');
414 }
catch (OdfException $e) {
416 $foundtagforlines = 0;
419 if ($foundtagforlines) {
421 foreach ($object->lines as $line) {
423 $tmparray = $this->get_substitutionarray_reception_lines($line, $outputlangs);
426 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
427 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
428 foreach ($tmparray as $key => $val) {
430 $listlines->setVars($key, $val,
true,
'UTF-8');
431 }
catch (OdfException $e) {
433 }
catch (SegmentException $e) {
439 $odfHandler->mergeSegment($listlines);
441 }
catch (OdfException $e) {
442 $this->error = $e->getMessage();
448 $tmparray = $outputlangs->get_translations_for_substitutions();
449 foreach ($tmparray as $key => $value) {
451 $odfHandler->setVars($key, $value,
true,
'UTF-8');
452 }
catch (OdfException $e) {
458 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
459 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
462 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
464 $odfHandler->exportAsAttachedPDF($file);
466 $this->error = $e->getMessage();
472 $odfHandler->saveToDisk($file);
474 $this->error = $e->getMessage();
479 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
480 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
486 $this->result = array(
'fullpath'=>$file);
490 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);