dolibarr  13.0.2
doc_generic_bom_odt.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
5  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
6  * Copyright (C) 2018-2019 Philippe Grand <philippe.grand@atoo-net.com>
7  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  * or see https://www.gnu.org/
22  */
23 
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';
36 
37 
42 {
47  public $emetteur;
48 
53  public $phpmin = array(5, 6);
54 
58  public $version = 'dolibarr';
59 
60 
66  public function __construct($db)
67  {
68  global $conf, $langs, $mysoc;
69 
70  // Load translation files required by the page
71  $langs->loadLangs(array("main", "companies"));
72 
73  $this->db = $db;
74  $this->name = "ODT templates";
75  $this->description = $langs->trans("DocumentModelOdt");
76  $this->scandir = 'BOM_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
77 
78  // Page size for A4 format
79  $this->type = 'odt';
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;
87 
88  $this->option_multilang = 1; // Dispo en plusieurs langues
89  $this->option_freetext = 1; // Support add of a personalised text
90  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
91 
92  // Recupere emetteur
93  $this->emetteur = $mysoc;
94  if (!$this->emetteur->country_code) $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
95  }
96 
97 
104  public function info($langs)
105  {
106  global $conf, $langs;
107 
108  // Load translation files required by the page
109  $langs->loadLangs(array("errors", "companies"));
110 
111  $form = new Form($this->db);
112 
113  $texte = $this->description.".<br>\n";
114  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
115  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
116  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
117  $texte .= '<input type="hidden" name="param1" value="BOM_ADDON_PDF_ODT_PATH">';
118  $texte .= '<table class="nobordernopadding" width="100%">';
119 
120  // List of directories area
121  $texte .= '<tr><td>';
122  $texttitle = $langs->trans("ListOfDirectories");
123  $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->BOM_ADDON_PDF_ODT_PATH)));
124  $listoffiles = array();
125  foreach ($listofdir as $key=>$tmpdir)
126  {
127  $tmpdir = trim($tmpdir);
128  $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
129  if (!$tmpdir) {
130  unset($listofdir[$key]); continue;
131  }
132  if (!is_dir($tmpdir)) $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
133  else {
134  $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
135  if (count($tmpfiles)) $listoffiles = array_merge($listoffiles, $tmpfiles);
136  }
137  }
138  $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
139  // Add list of substitution keys
140  $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
141  $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
142 
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->BOM_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" value="'.$langs->trans("Modify").'" name="Button">';
150  $texte .= '<br></div></div>';
151 
152  // Scan directories
153  $nbofiles = count($listoffiles);
154  if (!empty($conf->global->BOM_ADDON_PDF_ODT_PATH))
155  {
156  $texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
157  //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
158  $texte .= count($listoffiles);
159  //$texte.=$nbofiles?'</a>':'';
160  $texte .= '</b>';
161  }
162 
163  if ($nbofiles)
164  {
165  $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
166  // Show list of found files
167  foreach ($listoffiles as $file) {
168  $texte .= '- '.$file['name'].' <a href="'.DOL_URL_ROOT.'/document.php?modulepart=boms&file=invoices/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a><br>';
169  }
170  $texte .= '</div>';
171  }
172 
173  $texte .= '</td>';
174 
175  $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
176  $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
177  $texte .= '</td>';
178  $texte .= '</tr>';
179 
180  $texte .= '</table>';
181  $texte .= '</form>';
182 
183  return $texte;
184  }
185 
186  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
198  public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
199  {
200  // phpcs:enable
201  global $user, $langs, $conf, $mysoc, $hookmanager;
202 
203  if (empty($srctemplatepath))
204  {
205  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
206  return -1;
207  }
208 
209  // Add odtgeneration hook
210  if (!is_object($hookmanager))
211  {
212  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
213  $hookmanager = new HookManager($this->db);
214  }
215  $hookmanager->initHooks(array('odtgeneration'));
216  global $action;
217 
218  if (!is_object($outputlangs)) $outputlangs = $langs;
219  $sav_charset_output = $outputlangs->charset_output;
220  $outputlangs->charset_output = 'UTF-8';
221 
222  $outputlangs->loadLangs(array("main", "dict", "companies", "bills"));
223 
224  if ($conf->bom->dir_output)
225  {
226  // If $object is id instead of object
227  if (!is_object($object))
228  {
229  $id = $object;
230  $object = new Bom($this->db);
231  $result = $object->fetch($id);
232  if ($result < 0)
233  {
234  dol_print_error($this->db, $object->error);
235  return -1;
236  }
237  }
238 
239  $object->fetch_thirdparty();
240 
241  $dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1];
242  $objectref = dol_sanitizeFileName($object->ref);
243  if (!preg_match('/specimen/i', $objectref)) $dir .= "/".$objectref;
244  $file = $dir."/".$objectref.".odt";
245 
246  if (!file_exists($dir))
247  {
248  if (dol_mkdir($dir) < 0)
249  {
250  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
251  return -1;
252  }
253  }
254 
255  if (file_exists($dir))
256  {
257  //print "srctemplatepath=".$srctemplatepath; // Src filename
258  $newfile = basename($srctemplatepath);
259  $newfiletmp = preg_replace('/\.od(t|s)/i', '', $newfile);
260  $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
261  $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
262  $newfiletmp = $objectref.'_'.$newfiletmp;
263  //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
264  // Get extension (ods or odt)
265  $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
266  if (!empty($conf->global->MAIN_DOC_USE_TIMING))
267  {
268  $format = $conf->global->MAIN_DOC_USE_TIMING;
269  if ($format == '1') $format = '%Y%m%d%H%M%S';
270  $filename = $newfiletmp.'-'.dol_print_date(dol_now(), $format).'.'.$newfileformat;
271  } else {
272  $filename = $newfiletmp.'.'.$newfileformat;
273  }
274  $file = $dir.'/'.$filename;
275  //print "newdir=".$dir;
276  //print "newfile=".$newfile;
277  //print "file=".$file;
278  //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
279 
280  dol_mkdir($conf->bom->dir_temp);
281 
282 
283  // If CUSTOMER contact defined on order, we use it
284  $usecontact = false;
285  $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
286  if (count($arrayidcontact) > 0)
287  {
288  $usecontact = true;
289  $result = $object->fetch_contact($arrayidcontact[0]);
290  }
291 
292  // Recipient name
293  $contactobject = null;
294  if (!empty($usecontact))
295  {
296  // On peut utiliser le nom de la societe du contact
297  if ($usecontact && ($object->contact->fk_soc != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)))) {
298  $socobject = $object->contact;
299  } else {
300  $socobject = $object->thirdparty;
301  // if we have a CUSTOMER contact and we dont use as recipient we store the contact object for later use
302  $contactobject = $object->contact;
303  }
304  } else {
305  $socobject = $object->thirdparty;
306  }
307 
308  // Make substitution
309  $substitutionarray = array(
310  '__QTY_TO_PRODUCE__' => $object->qty,
311  );
312  complete_substitutions_array($substitutionarray, $langs, $object);
313  // Call the ODTSubstitution hook
314  $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray);
315  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
316 
317  // Line of free text
318  $newfreetext = '';
319  $paramfreetext = 'BOM_FREE_TEXT';
320  if (!empty($conf->global->$paramfreetext))
321  {
322  $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
323  }
324 
325  // Open and load template
326  require_once ODTPHP_PATH.'odf.php';
327  try {
328  $odfHandler = new odf(
329  $srctemplatepath,
330  array(
331  'PATH_TO_TMP' => $conf->bom->dir_temp,
332  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
333  'DELIMITER_LEFT' => '{',
334  'DELIMITER_RIGHT' => '}'
335  )
336  );
337  } catch (Exception $e)
338  {
339  $this->error = $e->getMessage();
340  dol_syslog($e->getMessage(), LOG_INFO);
341  return -1;
342  }
343  // After construction $odfHandler->contentXml contains content and
344  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
345  // [!-- BEGIN lines --]*[!-- END lines --]
346  //print html_entity_decode($odfHandler->__toString());
347  //print exit;
348 
349 
350  // Make substitutions into odt of freetext
351  try {
352  $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
353  } catch (OdfException $e)
354  {
355  dol_syslog($e->getMessage(), LOG_INFO);
356  }
357 
358  // Define substitution array
359  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
360  $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs);
361  $array_objet = $this->get_substitutionarray_object($object, $outputlangs);
362  $array_user = $this->get_substitutionarray_user($user, $outputlangs);
363  $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
364  $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
365  $array_other = $this->get_substitutionarray_other($outputlangs);
366  // retrieve contact information for use in object as contact_xxx tags
367  $array_thirdparty_contact = array();
368  if ($usecontact && is_object($contactobject)) $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
369 
370  $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
371  complete_substitutions_array($tmparray, $outputlangs, $object);
372 
373  // Call the ODTSubstitution hook
374  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
375  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
376 
377  foreach ($tmparray as $key=>$value)
378  {
379  try {
380  if (preg_match('/logo$/', $key)) // Image
381  {
382  if (file_exists($value)) $odfHandler->setImage($key, $value);
383  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
384  } else // Text
385  {
386  $odfHandler->setVars($key, $value, true, 'UTF-8');
387  }
388  } catch (OdfException $e)
389  {
390  dol_syslog($e->getMessage(), LOG_INFO);
391  }
392  }
393  // Replace tags of lines
394  try {
395  $foundtagforlines = 1;
396  try {
397  $listlines = $odfHandler->setSegment('lines');
398  } catch (OdfException $e)
399  {
400  // We may arrive here if tags for lines not present into template
401  $foundtagforlines = 0;
402  dol_syslog($e->getMessage(), LOG_INFO);
403  }
404  if ($foundtagforlines)
405  {
406  $linenumber = 0;
407  foreach ($object->lines as $line)
408  {
409  $linenumber++;
410  $tmparray = $this->get_substitutionarray_lines($line, $outputlangs, $linenumber);
411  complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
412  // Call the ODTSubstitutionLine hook
413  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$line);
414  $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
415  foreach ($tmparray as $key => $val)
416  {
417  try {
418  $listlines->setVars($key, $val, true, 'UTF-8');
419  } catch (OdfException $e)
420  {
421  dol_syslog($e->getMessage(), LOG_INFO);
422  } catch (SegmentException $e)
423  {
424  dol_syslog($e->getMessage(), LOG_INFO);
425  }
426  }
427  $listlines->merge();
428  }
429  $odfHandler->mergeSegment($listlines);
430  }
431  } catch (OdfException $e)
432  {
433  $this->error = $e->getMessage();
434  dol_syslog($this->error, LOG_WARNING);
435  return -1;
436  }
437 
438  // Replace labels translated
439  $tmparray = $outputlangs->get_translations_for_substitutions();
440  foreach ($tmparray as $key=>$value)
441  {
442  try {
443  $odfHandler->setVars($key, $value, true, 'UTF-8');
444  } catch (OdfException $e)
445  {
446  dol_syslog($e->getMessage(), LOG_INFO);
447  }
448  }
449 
450  // Call the beforeODTSave hook
451 
452  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
453  $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
454 
455  // Write new file
456  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
457  try {
458  $odfHandler->exportAsAttachedPDF($file);
459  } catch (Exception $e) {
460  $this->error = $e->getMessage();
461  dol_syslog($e->getMessage(), LOG_INFO);
462  return -1;
463  }
464  } else {
465  try {
466  $odfHandler->saveToDisk($file);
467  } catch (Exception $e) {
468  $this->error = $e->getMessage();
469  dol_syslog($e->getMessage(), LOG_INFO);
470  return -1;
471  }
472  }
473 
474  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
475  $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
476 
477  if (!empty($conf->global->MAIN_UMASK))
478  @chmod($file, octdec($conf->global->MAIN_UMASK));
479 
480  $odfHandler = null; // Destroy object
481 
482  $this->result = array('fullpath'=>$file);
483 
484  return 1; // Success
485  } else {
486  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
487  return -1;
488  }
489  }
490 
491  return -1;
492  }
493 }
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive=true)
Define array with couple substitution key =&gt; substitution value.
dol_now($mode= 'auto')
Return date for now.
get_substitutionarray_user($user, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
get_substitutionarray_object($object, $outputlangs, $array_key= 'object')
Define array with couple substitution key =&gt; substitution value.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:108
$conf db
API class for accounts.
Definition: inc.php:54
img_warning($titlealt= 'default', $moreatt= '', $morecss= 'pictowarning')
Show warning logo.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
Return array of possible common substitutions.
Class to manage hooks.
get_substitutionarray_contact($object, $outputlangs, $array_key= 'object')
Define array with couple substitution key =&gt; substitution value.
Class to manage generation of HTML components Only common components must be here.
get_substitutionarray_thirdparty($object, $outputlangs, $array_key= 'company')
Define array with couple substitution key =&gt; substitution value.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
Parent class for boms models.
Definition: modules_bom.php:39
Class to build documents using ODF templates generator.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=&gt;newva...
get_substitutionarray_other($outputlangs)
Define array with couple substitution key =&gt; substitution value.
get_substitutionarray_lines($line, $outputlangs, $linenumber=0)
Define array with couple substitution key =&gt; substitution value.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
info($langs)
Return description of a module.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the &quot;subst...