30 require_once DOL_DOCUMENT_ROOT.
'/core/modules/bom/modules_bom.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);
58 public $version =
'dolibarr';
68 global $conf, $langs, $mysoc;
71 $langs->loadLangs(array(
"main",
"companies"));
74 $this->
name =
"ODT templates";
75 $this->
description = $langs->trans(
"DocumentModelOdt");
76 $this->scandir =
'BOM_ADDON_PDF_ODT_PATH';
80 $this->page_largeur = 0;
81 $this->page_hauteur = 0;
82 $this->format = array($this->page_largeur, $this->page_hauteur);
83 $this->marge_gauche = 0;
84 $this->marge_droite = 0;
85 $this->marge_haute = 0;
86 $this->marge_basse = 0;
88 $this->option_multilang = 1;
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;
111 $langs->loadLangs(array(
"errors",
"companies"));
116 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
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="BOM_ADDON_PDF_ODT_PATH">';
121 $texte .=
'<table class="nobordernopadding" width="100%">';
124 $texte .=
'<tr><td>';
125 $texttitle = $langs->trans(
"ListOfDirectories");
126 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->BOM_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->BOM_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 small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
156 $texte .=
'<br></div></div>';
159 $nbofiles = count($listoffiles);
160 if (!empty($conf->global->BOM_ADDON_PDF_ODT_PATH)) {
161 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
163 $texte .= count($listoffiles);
169 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
171 foreach ($listoffiles as $file) {
172 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=boms/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a><br>';
177 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
179 $maxmin = $maxfilesizearray[
'maxmin'];
181 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
183 $texte .=
' <input type="file" name="uploadfile">';
184 $texte .=
'<input type="hidden" value="BOM_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
185 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
190 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
191 $texte .=
'<span class="opacitymedium">';
192 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
197 $texte .=
'</table>';
215 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
218 global $user, $langs, $conf, $mysoc, $hookmanager;
220 if (empty($srctemplatepath)) {
221 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
226 if (!is_object($hookmanager)) {
227 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
230 $hookmanager->initHooks(array(
'odtgeneration'));
233 if (!is_object($outputlangs)) {
234 $outputlangs = $langs;
236 $sav_charset_output = $outputlangs->charset_output;
237 $outputlangs->charset_output =
'UTF-8';
240 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
242 if ($conf->bom->dir_output) {
244 if (!is_object($object)) {
246 $object =
new Bom($this->
db);
247 $result = $object->fetch($id);
254 $object->fetch_thirdparty();
255 $object->fetch_product();
257 $dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1];
259 if (!preg_match(
'/specimen/i', $objectref)) {
260 $dir .=
"/".$objectref;
262 $file = $dir.
"/".$objectref.
".odt";
264 if (!file_exists($dir)) {
266 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
271 if (file_exists($dir)) {
273 $newfile = basename($srctemplatepath);
274 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
275 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
276 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
277 $newfiletmp = $objectref.
'_'.$newfiletmp;
280 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
281 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
282 $format = $conf->global->MAIN_DOC_USE_TIMING;
283 if ($format ==
'1') {
284 $format =
'%Y%m%d%H%M%S';
288 $filename = $newfiletmp.
'.'.$newfileformat;
290 $file = $dir.
'/'.$filename;
297 if (!is_writable($conf->bom->dir_temp)) {
298 $this->error =
"Failed to write in temp directory ".$conf->bom->dir_temp;
299 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
305 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
306 if (count($arrayidcontact) > 0) {
308 $result = $object->fetch_contact($arrayidcontact[0]);
312 $contactobject =
null;
313 if (!empty($usecontact)) {
315 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))) {
316 $object->contact->fetch_thirdparty();
317 $socobject = $object->contact->thirdparty;
318 $contactobject = $object->contact;
320 $socobject = $object->thirdparty;
322 $contactobject = $object->contact;
325 $socobject = $object->thirdparty;
329 $substitutionarray = array(
330 '__QTY_TO_PRODUCE__' => $object->qty,
334 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
335 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
339 $paramfreetext =
'BOM_FREE_TEXT';
340 if (!empty($conf->global->$paramfreetext)) {
345 require_once ODTPHP_PATH.
'odf.php';
347 $odfHandler =
new odf(
350 'PATH_TO_TMP' => $conf->bom->dir_temp,
351 'ZIP_PROXY' =>
'PclZipProxy',
352 'DELIMITER_LEFT' =>
'{',
353 'DELIMITER_RIGHT' =>
'}'
357 $this->error = $e->getMessage();
370 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
371 }
catch (OdfException $e) {
384 $array_thirdparty_contact = array();
385 if ($usecontact && is_object($contactobject)) {
389 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
393 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
394 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
396 foreach ($tmparray as $key => $value) {
398 if (preg_match(
'/logo$/', $key)) {
399 if (file_exists($value)) {
400 $odfHandler->setImage($key, $value);
402 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
406 $odfHandler->setVars($key, $value,
true,
'UTF-8');
408 }
catch (OdfException $e) {
414 $foundtagforlines = 1;
416 $listlines = $odfHandler->setSegment(
'lines');
417 }
catch (OdfException $e) {
419 $foundtagforlines = 0;
423 if ($foundtagforlines) {
425 foreach ($object->lines as $line) {
428 if ($line->fk_product > 0) {
429 $line->fetch_product();
431 $line->product_ref = $line->product->ref;
432 $line->product_desc = $line->product->description;
433 $line->product_label = $line->product->label;
434 $line->product_type = $line->product->type;
435 $line->product_barcode = $line->product->barcode;
441 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
442 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
443 foreach ($tmparray as $key => $val) {
445 $listlines->setVars($key, $val,
true,
'UTF-8');
446 }
catch (OdfException $e) {
448 }
catch (SegmentException $e) {
454 $odfHandler->mergeSegment($listlines);
456 }
catch (OdfException $e) {
457 $this->error = $e->getMessage();
463 $tmparray = $outputlangs->get_translations_for_substitutions();
464 foreach ($tmparray as $key => $value) {
466 $odfHandler->setVars($key, $value,
true,
'UTF-8');
467 }
catch (OdfException $e) {
474 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
475 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
478 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
480 $odfHandler->exportAsAttachedPDF($file);
482 $this->error = $e->getMessage();
488 $odfHandler->saveToDisk($file);
490 $this->error = $e->getMessage();
496 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
497 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
499 if (!empty($conf->global->MAIN_UMASK)) {
500 @chmod($file, octdec($conf->global->MAIN_UMASK));
505 $this->result = array(
'fullpath'=>$file);
509 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);