48 public $phpmin = array(7, 0);
53 public $version =
'dolibarr';
63 global $langs, $mysoc;
66 $langs->loadLangs(array(
"main",
"companies"));
69 $this->
name =
"ODT templates";
70 $this->
description = $langs->trans(
"DocumentModelOdt");
71 $this->scandir =
'BOM_ADDON_PDF_ODT_PATH';
75 $this->page_largeur = 0;
76 $this->page_hauteur = 0;
77 $this->format = array($this->page_largeur, $this->page_hauteur);
78 $this->marge_gauche = 0;
79 $this->marge_droite = 0;
80 $this->marge_haute = 0;
81 $this->marge_basse = 0;
83 $this->option_multilang = 1;
84 $this->option_freetext = 1;
85 $this->option_draft_watermark = 0;
87 if ($mysoc ===
null) {
92 $this->emetteur = $mysoc;
93 if (!$this->emetteur->country_code) {
94 $this->emetteur->country_code = substr($langs->defaultlang, -2);
110 $langs->loadLangs(array(
"errors",
"companies"));
112 $form =
new Form($this->db);
117 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
118 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
119 $texte .=
'<input type="hidden" name="page_y" value="">';
120 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
121 $texte .=
'<input type="hidden" name="param1" value="BOM_ADDON_PDF_ODT_PATH">';
123 $texte .=
'<table class="nobordernopadding centpercent">';
126 $texte .=
'<tr><td>';
127 $texttitle = $langs->trans(
"ListOfDirectories");
128 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
129 $listoffiles = array();
130 foreach ($listofdir as $key => $tmpdir) {
131 $tmpdir = trim($tmpdir);
132 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
134 unset($listofdir[$key]);
137 if (!is_dir($tmpdir)) {
138 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
140 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
141 if (count($tmpfiles)) {
142 $listoffiles = array_merge($listoffiles, $tmpfiles);
146 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
147 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
149 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
150 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
153 $nbofiles = count($listoffiles);
155 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
157 $texte .= count($listoffiles);
163 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
165 foreach ($listoffiles as $file) {
166 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=boms/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
167 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=BOM_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
175 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
176 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
177 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
179 $texte .=
'</textarea>';
180 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
181 $texte .=
'<input type="submit" class="button button-edit smallpaddingimp reposition" name="modify" value="'.dolPrintHTMLForAttribute($langs->trans(
"Modify")).
'">';
182 $texte .=
'<br></div></div>';
185 $texte .=
'<input type="hidden" name="value1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
189 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
191 $maxmin = $maxfilesizearray[
'maxmin'];
193 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
195 $texte .=
' <input type="file" name="uploadfile">';
196 $texte .=
'<input type="hidden" value="BOM_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
197 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
204 $texte .=
'</table>';
222 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
225 global $user, $langs,
$conf, $mysoc, $hookmanager;
228 if (empty($srctemplatepath)) {
229 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
234 if (!is_object($hookmanager)) {
235 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
238 $hookmanager->initHooks(array(
'odtgeneration'));
241 if (!is_object($outputlangs)) {
242 $outputlangs = $langs;
244 $sav_charset_output = $outputlangs->charset_output;
245 $outputlangs->charset_output =
'UTF-8';
247 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
249 if (
$conf->bom->dir_output) {
267 if (!preg_match(
'/specimen/i', $objectref)) {
268 $dir .=
"/".$objectref;
270 $file = $dir.
"/".$objectref.
".odt";
272 if (!file_exists($dir)) {
274 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
279 if (file_exists($dir)) {
281 $newfile = basename($srctemplatepath);
282 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
283 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
284 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
285 $newfiletmp = $objectref .
'_' . $newfiletmp;
288 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
291 if ($format ==
'1') {
292 $format =
'%Y%m%d%H%M%S';
296 $filename = $newfiletmp .
'.' . $newfileformat;
298 $file = $dir .
'/' . $filename;
301 if (!is_writable(
$conf->bom->dir_temp)) {
302 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->bom->dir_temp);
303 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
309 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
310 if (count($arrayidcontact) > 0) {
312 $result =
$object->fetch_contact($arrayidcontact[0]);
316 $contactobject =
null;
317 if (!empty($usecontact)) {
320 $object->contact->fetch_thirdparty();
321 $socobject =
$object->contact->thirdparty;
322 $contactobject =
$object->contact;
324 $socobject =
$object->thirdparty;
326 $contactobject =
$object->contact;
329 $socobject =
$object->thirdparty;
333 $substitutionarray = array(
334 '__QTY_TO_PRODUCE__' =>
$object->qty,
338 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
339 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
343 $paramfreetext =
'BOM_FREE_TEXT';
349 require_once ODTPHP_PATH.
'odf.php';
351 $odfHandler =
new Odf(
354 'PATH_TO_TMP' =>
$conf->bom->dir_temp,
356 'DELIMITER_LEFT' =>
'{',
357 'DELIMITER_RIGHT' =>
'}'
361 $this->error = $e->getMessage();
371 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
372 }
catch (OdfException $e) {
377 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
379 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
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_objet, $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);
404 foreach ($tmparray as $key => $value) {
406 if (preg_match(
'/logo$/', $key)) {
408 if (file_exists($value)) {
409 $odfHandler->setImage($key, $value, $ratio);
411 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
415 $odfHandler->setVars($key, $value,
true,
'UTF-8');
417 }
catch (OdfException $e) {
422 $foundtagforlines = 1;
424 $listlines = $odfHandler->setSegment(
'lines');
425 }
catch (OdfExceptionSegmentNotFound $e) {
427 $foundtagforlines = 0;
430 if ($foundtagforlines) {
432 foreach (
$object->lines as $line) {
436 if ($line->fk_product > 0) {
437 $line->fetch_product();
439 $line->product_ref = $line->product->ref;
440 $line->product_desc = $line->product->description;
441 $line->product_label = $line->product->label;
442 $line->product_type = $line->product->type;
443 $line->product_barcode = $line->product->barcode;
449 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
450 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
451 foreach ($tmparray as $key => $val) {
453 $listlines->setVars($key, $val,
true,
'UTF-8');
454 }
catch (SegmentException $e) {
461 $odfHandler->mergeSegment($listlines);
462 }
catch (OdfException $e) {
463 $this->error = $e->getMessage();
470 $tmparray = $outputlangs->get_translations_for_substitutions();
471 foreach ($tmparray as $key => $value) {
473 $odfHandler->setVars($key, $value,
true,
'UTF-8');
474 }
catch (OdfException $e) {
480 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
481 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
486 $odfHandler->exportAsAttachedPDF($file);
488 $this->error = $e->getMessage();
494 $odfHandler->saveToDisk($file);
496 $this->error = $e->getMessage();
502 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
503 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
509 $this->result = array(
'fullpath' => $file);
513 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);