dolibarr  18.0.0
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 
50  public function __construct($db)
51  {
52  global $langs, $mysoc;
53 
54  // Load translation files required by the page
55  $langs->loadLangs(array("main", "companies"));
56 
57  $this->db = $db;
58  $this->name = "ODT templates";
59  $this->description = $langs->trans("DocumentModelOdt");
60  $this->scandir = 'COMPANY_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
61 
62  // Page size for A4 format
63  $this->type = 'odt';
64  $this->page_largeur = 0;
65  $this->page_hauteur = 0;
66  $this->format = array($this->page_largeur, $this->page_hauteur);
67  $this->marge_gauche = 0;
68  $this->marge_droite = 0;
69  $this->marge_haute = 0;
70  $this->marge_basse = 0;
71 
72  $this->option_logo = 1; // Display logo
73 
74  // Retrieves transmitter
75  $this->emetteur = $mysoc;
76  if (!$this->emetteur->country_code) {
77  $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default, if was not defined
78  }
79  }
80 
81 
88  public function info($langs)
89  {
90  global $conf, $langs;
91 
92  // Load traductions files required by page
93  $langs->loadLangs(array("companies", "errors"));
94 
95  $form = new Form($this->db);
96 
97  $texte = $this->description.".<br>\n";
98  $texte .= '<!-- form for option of ODT templates -->';
99  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">';
100  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
101  $texte .= '<input type="hidden" name="page_y" value="">';
102  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
103  $texte .= '<input type="hidden" name="param1" value="COMPANY_ADDON_PDF_ODT_PATH">';
104  $texte .= '<table class="nobordernopadding centpercent">';
105 
106  // List of directories area
107  $texte .= '<tr><td>';
108  $texttitle = $langs->trans("ListOfDirectories");
109  $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->COMPANY_ADDON_PDF_ODT_PATH)));
110  $listoffiles = array();
111  foreach ($listofdir as $key => $tmpdir) {
112  $tmpdir = trim($tmpdir);
113  $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
114  if (!$tmpdir) {
115  unset($listofdir[$key]);
116  continue;
117  }
118  if (!is_dir($tmpdir)) {
119  $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)) {
123  $listoffiles = array_merge($listoffiles, $tmpfiles);
124  }
125  }
126  }
127  $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
128  // Add list of substitution keys
129  $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
130  $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
131 
132  $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
133  $texte .= '<table><tr><td>';
134  $texte .= '<textarea class="flat" cols="60" name="value1">';
135  $texte .= $conf->global->COMPANY_ADDON_PDF_ODT_PATH;
136  $texte .= '</textarea>';
137  $texte .= '</td>';
138  $texte .= '<td class="center">&nbsp; ';
139  $texte .= '<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans("Modify").'">';
140  $texte .= '</td>';
141  $texte .= '</tr>';
142  $texte .= '</table>';
143 
144  // Scan directories
145  $nbofiles = count($listoffiles);
146  if (!empty($conf->global->COMPANY_ADDON_PDF_ODT_PATH)) {
147  $texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
148  //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
149  $texte .= $nbofiles;
150  //$texte.=$nbofiles?'</a>':'';
151  $texte .= '</b>';
152  }
153 
154  if ($nbofiles) {
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'].' &nbsp; <a class="reposition" href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=thirdparties/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a>';
159  $texte .= ' &nbsp; <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?modulepart=doctemplates&keyforuploaddir=COMPANY_ADDON_PDF_ODT_PATH&action=deletefile&token='.newToken().'&file='.urlencode(basename($file['name'])).'">'.img_picto('', 'delete').'</a>';
160  $texte .= '<br>';
161  }
162  $texte .= '</div>';
163  }
164  // Add input to upload a new template file.
165  $texte .= '<div>'.$langs->trans("UploadNewTemplate");
166  $maxfilesizearray = getMaxFileSizeArray();
167  $maxmin = $maxfilesizearray['maxmin'];
168  if ($maxmin > 0) {
169  $texte .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
170  }
171  $texte .= ' <input type="file" name="uploadfile">';
172  $texte .= '<input type="hidden" value="COMPANY_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
173  $texte .= '<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
174  $texte .= '</div>';
175  $texte .= '</td>';
176 
177  $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
178  $texte .= '<span class="opacitymedium">';
179  $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
180  $texte .= '</span>';
181  $texte .= '</td>';
182  $texte .= '</tr>';
183 
184  $texte .= '</table>';
185  $texte .= '</form>';
186 
187  return $texte;
188  }
189 
190  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
202  public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
203  {
204  // phpcs:enable
205  global $user, $langs, $conf, $mysoc, $hookmanager;
206  global $action;
207 
208  if (empty($srctemplatepath)) {
209  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
210  return -1;
211  }
212 
213  // Add odtgeneration hook
214  if (!is_object($hookmanager)) {
215  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
216  $hookmanager = new HookManager($this->db);
217  }
218  $hookmanager->initHooks(array('odtgeneration'));
219 
220  if (!is_object($outputlangs)) {
221  $outputlangs = $langs;
222  }
223  $sav_charset_output = $outputlangs->charset_output;
224  $outputlangs->charset_output = 'UTF-8';
225 
226  // Load translation files required by the page
227  $outputlangs->loadLangs(array("main", "dict", "companies", "projects"));
228 
229  if ($conf->societe->multidir_output[$object->entity]) {
230  $dir = $conf->societe->multidir_output[$object->entity];
231  $objectref = dol_sanitizeFileName($object->id);
232  if (!preg_match('/specimen/i', $objectref)) {
233  $dir .= "/".$objectref;
234  }
235 
236  if (!file_exists($dir)) {
237  if (dol_mkdir($dir) < 0) {
238  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
239  return -1;
240  }
241  }
242 
243  if (file_exists($dir)) {
244  //print "srctemplatepath=".$srctemplatepath; // Src filename
245  $newfile = basename($srctemplatepath);
246  $newfiletmp = preg_replace('/\.od(s|t)/i', '', $newfile);
247  $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
248  $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
249  // Get extension (ods or odt)
250  $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
251  if (!empty($conf->global->MAIN_DOC_USE_OBJECT_THIRDPARTY_NAME)) {
252  $newfiletmp = dol_sanitizeFileName(dol_string_nospecial($object->name)) . '-' . $newfiletmp;
253  $newfiletmp = preg_replace('/__+/', '_', $newfiletmp); // Replace repeated _ into one _ (to avoid string with substitution syntax)
254  }
255  if (getDolGlobalInt('MAIN_DOC_USE_TIMING')) {
256  $format = getDolGlobalInt('MAIN_DOC_USE_TIMING');
257  if ($format == '1') {
258  $format = '%Y%m%d%H%M%S';
259  }
260  $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
261  } else {
262  $filename = $newfiletmp . '.' . $newfileformat;
263  }
264  $file = $dir . '/' . $filename;
265  $object->builddoc_filename = $filename; // For triggers
266  //print "newfileformat=".$newfileformat;
267  //print "newdir=".$dir;
268  //print "newfile=".$newfile;
269  //print "file=".$file;
270  //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
271  //exit;
272 
273  dol_mkdir($conf->societe->multidir_temp[$object->entity]);
274  if (!is_writable($conf->societe->multidir_temp[$object->entity])) {
275  $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->societe->multidir_temp[$object->entity]);
276  dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
277  return -1;
278  }
279 
280  // Open and load template
281  require_once ODTPHP_PATH.'odf.php';
282  try {
283  $odfHandler = new Odf(
284  $srctemplatepath,
285  array(
286  'PATH_TO_TMP' => $conf->societe->multidir_temp[$object->entity],
287  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
288  'DELIMITER_LEFT' => '{',
289  'DELIMITER_RIGHT' => '}'
290  )
291  );
292  } catch (Exception $e) {
293  $this->error = $e->getMessage();
294  dol_syslog($e->getMessage(), LOG_INFO);
295  return -1;
296  }
297  //print $odfHandler->__toString()."\n";
298 
299  // Replace tags of lines for contacts
300  $contact_arrray = array();
301 
302  $sql = "SELECT p.rowid";
303  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
304  $sql .= " WHERE p.fk_soc = ".((int) $object->id);
305 
306  $result = $this->db->query($sql);
307  $num = $this->db->num_rows($result);
308 
309  if ($num) {
310  require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
311 
312  $i = 0;
313  $contactstatic = new Contact($this->db);
314 
315  while ($i < $num) {
316  $obj = $this->db->fetch_object($result);
317 
318  $contact_arrray[$i] = $obj->rowid;
319  $i++;
320  }
321  }
322  if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
323  try {
324  $listlines = $odfHandler->setSegment('companycontacts');
325 
326  foreach ($contact_arrray as $array_key => $contact_id) {
327  $res_contact = $contactstatic->fetch($contact_id);
328  if ((int) $res_contact > 0) {
329  $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact');
330  foreach ($tmparray as $key => $val) {
331  try {
332  $listlines->setVars($key, $val, true, 'UTF-8');
333  } catch (OdfException $e) {
334  dol_syslog($e->getMessage(), LOG_INFO);
335  } catch (SegmentException $e) {
336  dol_syslog($e->getMessage(), LOG_INFO);
337  }
338  }
339  $listlines->merge();
340  } else {
341  $this->error = $contactstatic->error;
342  dol_syslog($this->error, LOG_WARNING);
343  }
344  }
345  $odfHandler->mergeSegment($listlines);
346  } catch (OdfException $e) {
347  $this->error = $e->getMessage();
348  dol_syslog($this->error, LOG_WARNING);
349  //return -1;
350  }
351  }
352 
353  // Make substitutions into odt
354  $array_user = $this->get_substitutionarray_user($user, $outputlangs);
355  $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
356  $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs);
357  $array_other = $this->get_substitutionarray_other($outputlangs);
358 
359  $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other);
360 
361  complete_substitutions_array($tmparray, $outputlangs, $object);
362 
363  // Call the ODTSubstitution hook
364  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
365  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
366 
367  // Replace variables into document
368  foreach ($tmparray as $key => $value) {
369  try {
370  if (preg_match('/logo$/', $key)) { // Image
371  if (file_exists($value)) {
372  $odfHandler->setImage($key, $value);
373  } else {
374  $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
375  }
376  } else // Text
377  {
378  $odfHandler->setVars($key, $value, true, 'UTF-8');
379  }
380  } catch (OdfException $e) {
381  // setVars failed, probably because key not found
382  dol_syslog($e->getMessage(), LOG_INFO);
383  }
384  }
385 
386  // Replace labels translated
387  $tmparray = $outputlangs->get_translations_for_substitutions();
388  foreach ($tmparray as $key => $value) {
389  try {
390  $odfHandler->setVars($key, $value, true, 'UTF-8');
391  } catch (OdfException $e) {
392  dol_syslog($e->getMessage(), LOG_INFO);
393  }
394  }
395 
396  // Call the beforeODTSave hook
397  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
398  $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
399 
400  // Write new file
401  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
402  try {
403  $odfHandler->exportAsAttachedPDF($file);
404  } catch (Exception $e) {
405  $this->error = $e->getMessage();
406  dol_syslog($e->getMessage(), LOG_INFO);
407  return -1;
408  }
409  } else {
410  try {
411  $odfHandler->creator = $user->getFullName($outputlangs);
412  $odfHandler->title = $object->builddoc_filename;
413  $odfHandler->subject = $object->builddoc_filename;
414 
415  if (!empty($conf->global->ODT_ADD_DOLIBARR_ID)) {
416  $odfHandler->userdefined['dol_id'] = $object->id;
417  $odfHandler->userdefined['dol_element'] = $object->element;
418  }
419 
420  $odfHandler->saveToDisk($file);
421  } catch (Exception $e) {
422  $this->error = $e->getMessage();
423  dol_syslog($e->getMessage(), LOG_INFO);
424  return -1;
425  }
426  }
427  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
428  $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
429 
430  dolChmod($file);
431 
432  $odfHandler = null; // Destroy object
433 
434  $this->result = array('fullpath'=>$file);
435 
436  return 1; // Success
437  } else {
438  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
439  return -1;
440  }
441  }
442 
443  $this->error = 'UnknownError';
444  return -1;
445  }
446 }
dol_string_nospecial
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
Definition: functions.lib.php:1496
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1323
doc_generic_odt\info
info($langs)
Return description of a module.
Definition: doc_generic_odt.modules.php:88
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
CommonDocGenerator\get_substitutionarray_user
get_substitutionarray_user($user, $outputlangs)
Define array with couple substitution key => substitution value.
Definition: commondocgenerator.class.php:140
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:62
img_warning
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
Definition: functions.lib.php:4784
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
doc_generic_odt
Class to build documents using ODF templates generator.
Definition: doc_generic_odt.modules.php:37
name
$conf db name
Definition: repair.php:123
doc_generic_odt\write_file
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
Definition: doc_generic_odt.modules.php:202
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2675
doc_generic_odt\__construct
__construct($db)
Constructor.
Definition: doc_generic_odt.modules.php:50
CommonDocGenerator\get_substitutionarray_contact
get_substitutionarray_contact($object, $outputlangs, $array_key='object')
Define array with couple substitution key => substitution value.
Definition: commondocgenerator.class.php:357
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4135
dolChmod
dolChmod($filepath, $newmask='')
Change mod of a file.
Definition: functions.lib.php:7007
getMaxFileSizeArray
getMaxFileSizeArray()
Return the max allowed for file upload.
Definition: security.lib.php:1228
Exception
CommonDocGenerator\get_substitutionarray_thirdparty
get_substitutionarray_thirdparty($object, $outputlangs, $array_key='company')
Define array with couple substitution key => substitution value For example {company_name},...
Definition: commondocgenerator.class.php:292
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
Contact
Class to manage contact/addresses.
Definition: contact.class.php:42
CommonDocGenerator\get_substitutionarray_mysoc
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple substitution key => substitution value.
Definition: commondocgenerator.class.php:233
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11654
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:53
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:3056
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6936
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:156
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120
HookManager
Class to manage hooks.
Definition: hookmanager.class.php:30
complete_substitutions_array
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 "subst...
Definition: functions.lib.php:8489
CommonDocGenerator\get_substitutionarray_other
get_substitutionarray_other($outputlangs)
Define array with couple substitution key => substitution value.
Definition: commondocgenerator.class.php:415
ModeleThirdPartyDoc
Parent class for third parties models of doc generators.
Definition: modules_societe.class.php:33