dolibarr  13.0.2
doc_generic_odt.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2011 Laurent Destailleur <ely@users.sourceforge.net>
3  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
4  * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  * or see https://www.gnu.org/
19  */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
32 
33 
38 {
43  public $emetteur;
44 
49  public $phpmin = array(5, 6);
50 
51 
57  public function __construct($db)
58  {
59  global $conf, $langs, $mysoc;
60 
61  // Load translation files required by the page
62  $langs->loadLangs(array("main", "companies"));
63 
64  $this->db = $db;
65  $this->name = "ODT templates";
66  $this->description = $langs->trans("DocumentModelOdt");
67  $this->scandir = 'COMPANY_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
68 
69  // Page size for A4 format
70  $this->type = 'odt';
71  $this->page_largeur = 0;
72  $this->page_hauteur = 0;
73  $this->format = array($this->page_largeur, $this->page_hauteur);
74  $this->marge_gauche = 0;
75  $this->marge_droite = 0;
76  $this->marge_haute = 0;
77  $this->marge_basse = 0;
78 
79  $this->option_logo = 1; // Affiche logo
80 
81  // Retrieves transmitter
82  $this->emetteur = $mysoc;
83  if (!$this->emetteur->country_code) $this->emetteur->country_code = substr($langs->defaultlang, -2); // Par defaut, si n'etait pas defini
84  }
85 
86 
93  public function info($langs)
94  {
95  global $conf, $langs;
96 
97  // Load traductions files required by page
98  $langs->loadLangs(array("companies", "errors"));
99 
100  $form = new Form($this->db);
101 
102  $texte = $this->description.".<br>\n";
103  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">';
104  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
105  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
106  $texte .= '<input type="hidden" name="param1" value="COMPANY_ADDON_PDF_ODT_PATH">';
107  $texte .= '<table class="nobordernopadding" width="100%">';
108 
109  // List of directories area
110  $texte .= '<tr><td>';
111  $texttitle = $langs->trans("ListOfDirectories");
112  $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->COMPANY_ADDON_PDF_ODT_PATH)));
113  $listoffiles = array();
114  foreach ($listofdir as $key=>$tmpdir)
115  {
116  $tmpdir = trim($tmpdir);
117  $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
118  if (!$tmpdir) { unset($listofdir[$key]); continue; }
119  if (!is_dir($tmpdir)) $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
120  else {
121  $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.od(s|t)$', '', 'name', SORT_ASC, 0, true); // Disable hook for the moment
122  if (count($tmpfiles)) $listoffiles = array_merge($listoffiles, $tmpfiles);
123  }
124  }
125  $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
126  // Add list of substitution keys
127  $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
128  $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
129 
130  $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
131  $texte .= '<table><tr><td>';
132  $texte .= '<textarea class="flat" cols="60" name="value1">';
133  $texte .= $conf->global->COMPANY_ADDON_PDF_ODT_PATH;
134  $texte .= '</textarea>';
135  $texte .= '</td>';
136  $texte .= '<td class="center">&nbsp; ';
137  $texte .= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
138  $texte .= '</td>';
139  $texte .= '</tr>';
140  $texte .= '</table>';
141 
142  // Scan directories
143  $nbofiles = count($listoffiles);
144  if (!empty($conf->global->COMPANY_ADDON_PDF_ODT_PATH))
145  {
146  $texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
147  //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
148  $texte .= $nbofiles;
149  //$texte.=$nbofiles?'</a>':'';
150  $texte .= '</b>';
151  }
152 
153  if ($nbofiles)
154  {
155  $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
156  // Show list of found files
157  foreach ($listoffiles as $file) {
158  $texte .= '- '.$file['name'].' <a href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=thirdparties/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a><br>';
159  }
160  $texte .= '</div>';
161  }
162  // Add input to upload a new template file.
163  $texte .= '<div>'.$langs->trans("UploadNewTemplate").' <input type="file" name="uploadfile">';
164  $texte .= '<input type="hidden" value="COMPANY_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
165  $texte .= '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
166  $texte .= '</div>';
167  $texte .= '</td>';
168 
169  $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
170  $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
171  $texte .= '</td>';
172  $texte .= '</tr>';
173 
174  $texte .= '</table>';
175  $texte .= '</form>';
176 
177  return $texte;
178  }
179 
180  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
192  public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
193  {
194  // phpcs:enable
195  global $user, $langs, $conf, $mysoc, $hookmanager;
196 
197  if (empty($srctemplatepath))
198  {
199  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
200  return -1;
201  }
202 
203  // Add odtgeneration hook
204  if (!is_object($hookmanager)) {
205  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
206  $hookmanager = new HookManager($this->db);
207  }
208  $hookmanager->initHooks(array('odtgeneration'));
209  global $action;
210 
211  if (!is_object($outputlangs)) $outputlangs = $langs;
212  $sav_charset_output = $outputlangs->charset_output;
213  $outputlangs->charset_output = 'UTF-8';
214 
215  // Load translation files required by the page
216  $outputlangs->loadLangs(array("main", "dict", "companies", "projects"));
217 
218  if ($conf->societe->multidir_output[$object->entity])
219  {
220  $dir = $conf->societe->multidir_output[$object->entity];
221  $objectref = dol_sanitizeFileName($object->id);
222  if (!preg_match('/specimen/i', $objectref)) $dir .= "/".$objectref;
223 
224  if (!file_exists($dir))
225  {
226  if (dol_mkdir($dir) < 0)
227  {
228  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
229  return -1;
230  }
231  }
232 
233  if (file_exists($dir))
234  {
235  //print "srctemplatepath=".$srctemplatepath; // Src filename
236  $newfile = basename($srctemplatepath);
237  $newfiletmp = preg_replace('/\.od(s|t)/i', '', $newfile);
238  $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
239  $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
240  // Get extension (ods or odt)
241  $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
242  if (!empty($conf->global->MAIN_DOC_USE_OBJECT_THIRDPARTY_NAME))
243  {
244  $newfiletmp = dol_sanitizeFileName(dol_string_nospecial($object->name)).'-'.$newfiletmp;
245  }
246  if (!empty($conf->global->MAIN_DOC_USE_TIMING))
247  {
248  $format = $conf->global->MAIN_DOC_USE_TIMING;
249  if ($format == '1') $format = '%Y%m%d%H%M%S';
250  $filename = $newfiletmp.'-'.dol_print_date(dol_now(), $format).'.'.$newfileformat;
251  } else {
252  $filename = $newfiletmp.'.'.$newfileformat;
253  }
254  $file = $dir.'/'.$filename;
255  $object->builddoc_filename = $filename; // For triggers
256  //print "newfileformat=".$newfileformat;
257  //print "newdir=".$dir;
258  //print "newfile=".$newfile;
259  //print "file=".$file;
260  //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
261  //exit;
262 
263  dol_mkdir($conf->societe->multidir_temp[$object->entity]);
264 
265  // Open and load template
266  require_once ODTPHP_PATH.'odf.php';
267  try {
268  $odfHandler = new odf(
269  $srctemplatepath,
270  array(
271  'PATH_TO_TMP' => $conf->societe->multidir_temp[$object->entity],
272  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
273  'DELIMITER_LEFT' => '{',
274  'DELIMITER_RIGHT' => '}'
275  )
276  );
277  } catch (Exception $e)
278  {
279  $this->error = $e->getMessage();
280  dol_syslog($e->getMessage(), LOG_INFO);
281  return -1;
282  }
283  //print $odfHandler->__toString()."\n";
284 
285  // Replace tags of lines for contacts
286  $contact_arrray = array();
287 
288  $sql = "SELECT p.rowid";
289  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
290  $sql .= " WHERE p.fk_soc = ".$object->id;
291 
292  $result = $this->db->query($sql);
293  $num = $this->db->num_rows($result);
294 
295  if ($num)
296  {
297  require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
298 
299  $i = 0;
300  $contactstatic = new Contact($this->db);
301 
302  while ($i < $num)
303  {
304  $obj = $this->db->fetch_object($result);
305 
306  $contact_arrray[$i] = $obj->rowid;
307  $i++;
308  }
309  }
310  if ((is_array($contact_arrray) && count($contact_arrray) > 0))
311  {
312  try {
313  $listlines = $odfHandler->setSegment('companycontacts');
314 
315  foreach ($contact_arrray as $array_key => $contact_id)
316  {
317  $res_contact = $contactstatic->fetch($contact_id);
318  $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact');
319  foreach ($tmparray as $key => $val)
320  {
321  try {
322  $listlines->setVars($key, $val, true, 'UTF-8');
323  } catch (OdfException $e)
324  {
325  dol_syslog($e->getMessage(), LOG_INFO);
326  } catch (SegmentException $e)
327  {
328  dol_syslog($e->getMessage(), LOG_INFO);
329  }
330  }
331  $listlines->merge();
332  }
333  $odfHandler->mergeSegment($listlines);
334  } catch (OdfException $e)
335  {
336  $this->error = $e->getMessage();
337  dol_syslog($this->error, LOG_WARNING);
338  //return -1;
339  }
340  }
341 
342  // Make substitutions into odt
343  $array_user = $this->get_substitutionarray_user($user, $outputlangs);
344  $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
345  $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs);
346  $array_other = $this->get_substitutionarray_other($outputlangs);
347 
348  $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other);
349  complete_substitutions_array($tmparray, $outputlangs, $object);
350 
351  // Call the ODTSubstitution hook
352  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
353  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
354 
355  // Replace variables into document
356  foreach ($tmparray as $key=>$value)
357  {
358  try {
359  if (preg_match('/logo$/', $key)) // Image
360  {
361  if (file_exists($value)) $odfHandler->setImage($key, $value);
362  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
363  } else // Text
364  {
365  $odfHandler->setVars($key, $value, true, 'UTF-8');
366  }
367  } catch (OdfException $e)
368  {
369  // setVars failed, probably because key not found
370  dol_syslog($e->getMessage(), LOG_INFO);
371  }
372  }
373 
374  // Replace labels translated
375  $tmparray = $outputlangs->get_translations_for_substitutions();
376  foreach ($tmparray as $key=>$value)
377  {
378  try {
379  $odfHandler->setVars($key, $value, true, 'UTF-8');
380  } catch (OdfException $e)
381  {
382  dol_syslog($e->getMessage(), LOG_INFO);
383  }
384  }
385 
386  // Call the beforeODTSave hook
387  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
388  $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
389 
390  // Write new file
391  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
392  try {
393  $odfHandler->exportAsAttachedPDF($file);
394  } catch (Exception $e) {
395  $this->error = $e->getMessage();
396  dol_syslog($e->getMessage(), LOG_INFO);
397  return -1;
398  }
399  } else {
400  try {
401  $odfHandler->creator = $user->getFullName($outputlangs);
402  $odfHandler->title = $object->builddoc_filename;
403  $odfHandler->subject = $object->builddoc_filename;
404 
405  if (!empty($conf->global->ODT_ADD_DOLIBARR_ID)) {
406  $odfHandler->userdefined['dol_id'] = $object->id;
407  $odfHandler->userdefined['dol_element'] = $object->element;
408  }
409 
410  $odfHandler->saveToDisk($file);
411  } catch (Exception $e) {
412  $this->error = $e->getMessage();
413  dol_syslog($e->getMessage(), LOG_INFO);
414  return -1;
415  }
416  }
417  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
418  $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
419 
420  if (!empty($conf->global->MAIN_UMASK))
421  @chmod($file, octdec($conf->global->MAIN_UMASK));
422 
423  $odfHandler = null; // Destroy object
424 
425  $this->result = array('fullpath'=>$file);
426 
427  return 1; // Success
428  } else {
429  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
430  return -1;
431  }
432  }
433 
434  $this->error = 'UnknownError';
435  return -1;
436  }
437 }
info($langs)
Return description of a 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
Class to manage contact/addresses.
__construct($db)
Constructor.
dol_now($mode= 'auto')
Return date for now.
get_substitutionarray_user($user, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
$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.
dol_string_nospecial($str, $newstr= '_', $badcharstoreplace= '')
Clean a string from all punctuation characters to use it as a ref or login.
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.
Parent class for third parties models of doc generators.
get_substitutionarray_thirdparty($object, $outputlangs, $array_key= 'company')
Define array with couple substitution key =&gt; substitution value.
Class to build documents using ODF templates generator.
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
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
get_substitutionarray_other($outputlangs)
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
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...