27 require_once DOL_DOCUMENT_ROOT.
'/core/modules/product/modules_product.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
50 public $version =
'dolibarr';
60 global $conf, $langs, $mysoc;
63 $langs->loadLangs(array(
"main",
"companies"));
66 $this->
name =
"ODT templates";
67 $this->
description = $langs->trans(
"DocumentModelOdt");
68 $this->scandir =
'PRODUCT_ADDON_PDF_ODT_PATH';
72 $this->page_largeur = 0;
73 $this->page_hauteur = 0;
74 $this->format = array($this->page_largeur, $this->page_hauteur);
75 $this->marge_gauche = 0;
76 $this->marge_droite = 0;
77 $this->marge_haute = 0;
78 $this->marge_basse = 0;
80 $this->option_logo = 1;
81 $this->option_tva = 0;
82 $this->option_modereg = 0;
83 $this->option_condreg = 0;
84 $this->option_multilang = 1;
85 $this->option_escompte = 0;
86 $this->option_credit_note = 0;
87 $this->option_freetext = 1;
88 $this->option_draft_watermark = 0;
91 $this->emetteur = $mysoc;
92 if (!$this->emetteur->country_code) {
93 $this->emetteur->country_code = substr($langs->defaultlang, -2);
106 global $conf, $langs;
109 $langs->loadLangs(array(
"errors",
"companies"));
114 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
115 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
116 $texte .=
'<input type="hidden" name="page_y" value="">';
117 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
118 $texte .=
'<input type="hidden" name="param1" value="PRODUCT_ADDON_PDF_ODT_PATH">';
119 $texte .=
'<table class="nobordernopadding" width="100%">';
122 $texte .=
'<tr><td>';
123 $texttitle = $langs->trans(
"ListOfDirectories");
124 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->PRODUCT_ADDON_PDF_ODT_PATH)));
125 $listoffiles = array();
126 foreach ($listofdir as $key => $tmpdir) {
127 $tmpdir = trim($tmpdir);
128 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
130 unset($listofdir[$key]);
133 if (!is_dir($tmpdir)) {
134 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
136 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
137 if (count($tmpfiles)) {
138 $listoffiles = array_merge($listoffiles, $tmpfiles);
142 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
144 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
145 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
147 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
148 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
149 $texte .=
'<textarea class="flat" cols="60" name="value1">';
151 $texte .=
'</textarea>';
152 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
153 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
154 $texte .=
'<br></div></div>';
157 $nbofiles = count($listoffiles);
158 if (!empty($conf->global->PRODUCT_ADDON_PDF_ODT_PATH)) {
159 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
163 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
165 foreach ($listoffiles as $file) {
166 $texte .=
'- '.$file[
'name'];
167 $texte .=
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=products/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
168 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=PRODUCT_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
174 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
176 $maxmin = $maxfilesizearray[
'maxmin'];
178 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
180 $texte .=
' <input type="file" name="uploadfile">';
181 $texte .=
'<input type="hidden" value="PRODUCT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
182 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
187 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
188 $texte .=
'<span class="opacitymedium">';
189 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
194 $texte .=
'</table>';
212 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 global $product, $langs, $conf, $mysoc, $hookmanager, $user;
217 if (empty($srctemplatepath)) {
218 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
223 if (!is_object($hookmanager)) {
224 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
227 $hookmanager->initHooks(array(
'odtgeneration'));
230 if (!is_object($outputlangs)) {
231 $outputlangs = $langs;
233 $sav_charset_output = $outputlangs->charset_output;
234 $outputlangs->charset_output =
'UTF-8';
237 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
239 if ($conf->product->dir_output) {
241 if (!is_object($object)) {
243 $object =
new Product($this->db);
244 $result = $object->fetch($id);
251 $supplierprices = $productFournisseur->list_product_fournisseur_price($object->id);
252 $object->supplierprices = $supplierprices;
254 $dir = $conf->product->dir_output;
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[ts]/i',
'', $newfile);
272 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
273 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
275 $newfiletmp = $objectref .
'_' . $newfiletmp;
278 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
281 if ($format ==
'1') {
282 $format =
'%Y%m%d%H%M%S';
286 $filename = $newfiletmp .
'.' . $newfileformat;
288 $file = $dir .
'/' . $filename;
295 if (!is_writable($conf->product->dir_temp)) {
296 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
297 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
303 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
304 if (count($arrayidcontact) > 0) {
306 $result = $object->fetch_contact($arrayidcontact[0]);
310 $contactobject =
null;
311 if (!empty($usecontact)) {
313 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))) {
314 $object->contact->fetch_thirdparty();
315 $socobject = $object->contact->thirdparty;
316 $contactobject = $object->contact;
318 $socobject = $object->thirdparty;
320 $contactobject = $object->contact;
323 $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
335 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
336 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
340 $paramfreetext =
'PRODUCT_FREE_TEXT';
341 if (!empty($conf->global->$paramfreetext)) {
346 require_once ODTPHP_PATH.
'odf.php';
348 $odfHandler =
new Odf(
351 'PATH_TO_TMP' => $conf->product->dir_temp,
352 'ZIP_PROXY' =>
'PclZipProxy',
353 'DELIMITER_LEFT' =>
'{',
354 'DELIMITER_RIGHT' =>
'}'
358 $this->error = $e->getMessage();
371 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
372 }
catch (OdfException $e) {
385 $array_thirdparty_contact = array();
386 if ($usecontact && is_object($contactobject)) {
390 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
394 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
395 $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 $listlines = $odfHandler->setSegment(
'supplierprices');
416 if (!empty($object->supplierprices)) {
417 foreach ($object->supplierprices as $supplierprice) {
421 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$array_lines,
'line'=>$supplierprice);
422 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
423 foreach ($array_lines as $key => $val) {
425 $listlines->setVars($key, $val,
true,
'UTF-8');
426 }
catch (OdfException $e) {
428 }
catch (SegmentException $e) {
435 $odfHandler->mergeSegment($listlines);
436 }
catch (OdfException $e) {
437 $this->error = $e->getMessage();
443 $tmparray = $outputlangs->get_translations_for_substitutions();
444 foreach ($tmparray as $key => $value) {
446 $odfHandler->setVars($key, $value,
true,
'UTF-8');
447 }
catch (OdfException $e) {
453 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
454 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
457 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
459 $odfHandler->exportAsAttachedPDF($file);
461 $this->error = $e->getMessage();
467 $odfHandler->saveToDisk($file);
469 $this->error = $e->getMessage();
475 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
476 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
482 $this->result = array(
'fullpath'=>$file);
486 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);