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';
48 public $phpmin = array(5, 6);
53 public $version =
'dolibarr';
63 global $conf, $langs, $mysoc;
65 $langs->loadLangs(array(
"main",
"companies"));
68 $this->
name =
"ODT templates";
69 $this->
description = $langs->trans(
"DocumentModelOdt");
70 $this->scandir =
'RECEPTION_ADDON_PDF_ODT_PATH';
74 $this->page_largeur = 0;
75 $this->page_hauteur = 0;
76 $this->format = array($this->page_largeur, $this->page_hauteur);
77 $this->marge_gauche = 0;
78 $this->marge_droite = 0;
79 $this->marge_haute = 0;
80 $this->marge_basse = 0;
82 $this->option_logo = 1;
83 $this->option_tva = 0;
84 $this->option_modereg = 0;
85 $this->option_condreg = 0;
86 $this->option_multilang = 1;
87 $this->option_escompte = 0;
88 $this->option_credit_note = 0;
89 $this->option_freetext = 1;
90 $this->option_draft_watermark = 0;
93 $this->emetteur = $mysoc;
94 if (!$this->emetteur->country_code) {
95 $this->emetteur->country_code = substr($langs->defaultlang, -2);
108 global $conf, $langs;
110 $langs->load(
"companies");
111 $langs->load(
"errors");
116 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
117 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
118 $texte .=
'<input type="hidden" name="page_y" value="">';
119 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
120 $texte .=
'<input type="hidden" name="param1" value="RECEPTION_ADDON_PDF_ODT_PATH">';
121 $texte .=
'<table class="nobordernopadding centpercent">';
124 $texte .=
'<tr><td>';
125 $texttitle = $langs->trans(
"ListOfDirectories");
126 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->RECEPTION_ADDON_PDF_ODT_PATH)));
127 $listoffiles = array();
128 foreach ($listofdir as $key => $tmpdir) {
129 $tmpdir = trim($tmpdir);
130 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
132 unset($listofdir[$key]);
135 if (!is_dir($tmpdir)) {
136 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
138 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
139 if (count($tmpfiles)) {
140 $listoffiles = array_merge($listoffiles, $tmpfiles);
144 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
146 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
147 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
149 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
150 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
151 $texte .=
'<textarea class="flat" cols="60" name="value1">';
152 $texte .= $conf->global->RECEPTION_ADDON_PDF_ODT_PATH;
153 $texte .=
'</textarea>';
154 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
155 $texte .=
'<input type="submit" class="button button-edit small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
156 $texte .=
'<br></div></div>';
159 $nbofiles = count($listoffiles);
160 if (!empty($conf->global->RECEPTION_ADDON_PDF_ODT_PATH)) {
161 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
163 $texte .= count($listoffiles);
168 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
170 foreach ($listoffiles as $file) {
171 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=receptions/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a><br>';
176 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
178 $maxmin = $maxfilesizearray[
'maxmin'];
180 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
182 $texte .=
' <input type="file" name="uploadfile">';
183 $texte .=
'<input type="hidden" value="RECEPTION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
184 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
189 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
190 $texte .=
'<span class="opacitymedium">';
191 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
196 $texte .=
'</table>';
214 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
217 global $user, $langs, $conf, $mysoc, $hookmanager;
219 if (empty($srctemplatepath)) {
220 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
225 if (!is_object($hookmanager)) {
226 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
229 $hookmanager->initHooks(array(
'odtgeneration'));
232 if (!is_object($outputlangs)) {
233 $outputlangs = $langs;
235 $sav_charset_output = $outputlangs->charset_output;
236 $outputlangs->charset_output =
'UTF-8';
238 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
240 if ($conf->reception->dir_output.
"/reception") {
242 if (!is_object($object)) {
245 $result = $object->fetch($id);
252 $object->fetch_thirdparty();
254 $dir = $conf->reception->dir_output.
"/reception";
256 if (!preg_match(
'/specimen/i', $objectref)) {
257 $dir .=
"/".$objectref;
259 $file = $dir.
"/".$objectref.
".odt";
261 if (!file_exists($dir)) {
263 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
268 if (file_exists($dir)) {
270 $newfile = basename($srctemplatepath);
271 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
272 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
273 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
274 $newfiletmp = $objectref.
'_'.$newfiletmp;
277 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
278 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
279 $format = $conf->global->MAIN_DOC_USE_TIMING;
280 if ($format ==
'1') {
281 $format =
'%Y%m%d%H%M%S';
285 $filename = $newfiletmp.
'.'.$newfileformat;
287 $file = $dir.
'/'.$filename;
294 if (!is_writable($conf->reception->dir_temp)) {
295 $this->error =
"Failed to write in temp directory ".$conf->reception->dir_temp;
296 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
302 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
303 if (count($arrayidcontact) > 0) {
305 $result = $object->fetch_contact($arrayidcontact[0]);
309 $contactobject =
null;
310 if (!empty($usecontact)) {
312 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))) {
313 $object->contact->fetch_thirdparty();
314 $socobject = $object->contact->thirdparty;
315 $contactobject = $object->contact;
317 $socobject = $object->thirdparty;
319 $contactobject = $object->contact;
322 $socobject = $object->thirdparty;
326 $substitutionarray = array(
327 '__FROM_NAME__' => $this->emetteur->name,
328 '__FROM_EMAIL__' => $this->emetteur->email,
329 '__TOTAL_TTC__' => $object->total_ttc,
330 '__TOTAL_HT__' => $object->total_ht,
331 '__TOTAL_VAT__' => $object->total_tva
336 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
337 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
341 $paramfreetext =
'RECEPTION_FREE_TEXT';
342 if (!empty($conf->global->$paramfreetext)) {
347 require_once ODTPHP_PATH.
'odf.php';
349 $odfHandler =
new odf(
352 'PATH_TO_TMP' => $conf->reception->dir_temp,
353 'ZIP_PROXY' =>
'PclZipProxy',
354 'DELIMITER_LEFT' =>
'{',
355 'DELIMITER_RIGHT' =>
'}'
359 $this->error = $e->getMessage();
372 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
373 }
catch (OdfException $e) {
386 $array_thirdparty_contact = array();
387 if ($usecontact && is_object($contactobject)) {
391 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
395 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
396 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
397 foreach ($tmparray as $key => $value) {
399 if (preg_match(
'/logo$/', $key)) {
400 if (file_exists($value)) {
401 $odfHandler->setImage($key, $value);
403 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
407 $odfHandler->setVars($key, $value,
true,
'UTF-8');
409 }
catch (OdfException $e) {
415 $foundtagforlines = 1;
417 $listlines = $odfHandler->setSegment(
'lines');
418 }
catch (OdfException $e) {
420 $foundtagforlines = 0;
423 if ($foundtagforlines) {
425 foreach ($object->lines as $line) {
427 $tmparray = $this->get_substitutionarray_reception_lines($line, $outputlangs);
430 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
431 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
432 foreach ($tmparray as $key => $val) {
434 $listlines->setVars($key, $val,
true,
'UTF-8');
435 }
catch (OdfException $e) {
437 }
catch (SegmentException $e) {
443 $odfHandler->mergeSegment($listlines);
445 }
catch (OdfException $e) {
446 $this->error = $e->getMessage();
452 $tmparray = $outputlangs->get_translations_for_substitutions();
453 foreach ($tmparray as $key => $value) {
455 $odfHandler->setVars($key, $value,
true,
'UTF-8');
456 }
catch (OdfException $e) {
462 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
463 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
466 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
468 $odfHandler->exportAsAttachedPDF($file);
470 $this->error = $e->getMessage();
476 $odfHandler->saveToDisk($file);
478 $this->error = $e->getMessage();
483 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
484 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
486 if (!empty($conf->global->MAIN_UMASK)) {
487 @chmod($file, octdec($conf->global->MAIN_UMASK));
492 $this->result = array(
'fullpath'=>$file);
496 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);