dolibarr  17.0.4
doc_generic_usergroup_odt.modules.php
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) 2018 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/usergroup/modules_usergroup.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
34 
35 
40 {
45  public $emetteur;
46 
51  public $phpmin = array(7, 0);
52 
57  public $version = 'dolibarr';
58 
59 
65  public function __construct($db)
66  {
67  global $conf, $langs, $mysoc;
68 
69  // Load translation files required by the page
70  $langs->loadLangs(array("main", "companies"));
71 
72  $this->db = $db;
73  $this->name = "ODT templates";
74  $this->description = $langs->trans("DocumentModelOdt");
75  $this->scandir = 'USERGROUP_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
76 
77  // Page size for A4 format
78  $this->type = 'odt';
79  $this->page_largeur = 0;
80  $this->page_hauteur = 0;
81  $this->format = array($this->page_largeur, $this->page_hauteur);
82  $this->marge_gauche = 0;
83  $this->marge_droite = 0;
84  $this->marge_haute = 0;
85  $this->marge_basse = 0;
86 
87  $this->option_logo = 1; // Display logo
88  $this->option_tva = 0; // Manage the vat option USERGROUP_TVAOPTION
89  $this->option_modereg = 0; // Display payment mode
90  $this->option_condreg = 0; // Display payment terms
91  $this->option_multilang = 1; // Available in several languages
92  $this->option_escompte = 0; // Displays if there has been a discount
93  $this->option_credit_note = 0; // Support credit notes
94  $this->option_freetext = 1; // Support add of a personalised text
95  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
96 
97  // Get source company
98  $this->emetteur = $mysoc;
99  if (!$this->emetteur->country_code) {
100  $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
101  }
102  }
103 
104 
111  public function info($langs)
112  {
113  global $conf, $langs;
114 
115  // Load translation files required by the page
116  $langs->loadLangs(array("errors", "companies"));
117 
118  $form = new Form($this->db);
119 
120  $odtChosen = getDolGlobalInt('MAIN_PROPAL_CHOOSE_ODT_DOCUMENT') > 0;
121  $odtPath = trim(getDolGlobalString('USERGROUP_ADDON_PDF_ODT_PATH'));
122 
123  $texte = $this->description.".<br>\n";
124  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
125  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
126  $texte .= '<input type="hidden" name="page_y" value="">';
127  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
128  $texte .= '<input type="hidden" name="param1" value="USERGROUP_ADDON_PDF_ODT_PATH">';
129  if ($odtChosen) {
130  $texte .= '<input type="hidden" name="param2" value="USERGROUP_ADDON_PDF_ODT_DEFAULT">';
131  $texte .= '<input type="hidden" name="param3" value="USERGROUP_ADDON_PDF_ODT_TOBILL">';
132  $texte .= '<input type="hidden" name="param4" value="USERGROUP_ADDON_PDF_ODT_CLOSED">';
133  }
134  $texte .= '<table class="nobordernopadding" width="100%">';
135 
136  // List of directories area
137  $texte .= '<tr><td>';
138  $texttitle = $langs->trans("ListOfDirectories");
139  $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', $odtPath));
140  $listoffiles = array();
141  foreach ($listofdir as $key => $tmpdir) {
142  $tmpdir = trim($tmpdir);
143  $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
144  if (!$tmpdir) {
145  unset($listofdir[$key]);
146  continue;
147  }
148  if (!is_dir($tmpdir)) {
149  $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
150  } else {
151  $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
152  if (count($tmpfiles)) {
153  $listoffiles = array_merge($listoffiles, $tmpfiles);
154  }
155  }
156  }
157  $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
158  // Add list of substitution keys
159  $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
160  $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
161 
162  $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
163  $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
164  $texte .= '<textarea class="flat" cols="60" name="value1">';
165  $texte .= $odtPath;
166  $texte .= '</textarea>';
167  $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
168  $texte .= '<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans("Modify").'">';
169  $texte .= '<br></div></div>';
170 
171  // Scan directories
172  if (count($listofdir)) {
173  $texte .= $langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
174 
175  if ($odtChosen) {
176  // Model for creation
177  $list = ModelePDFUserGroup::liste_modeles($this->db);
178  $texte .= '<table width="50%;">';
179  $texte .= '<tr>';
180  $texte .= '<td width="60%;">'.$langs->trans("DefaultModelPropalCreate").'</td>';
181  $texte .= '<td colspan="">';
182  $texte .= $form->selectarray('value2', $list, getDolGlobalString('USERGROUP_ADDON_PDF_ODT_DEFAULT'));
183  $texte .= "</td></tr>";
184 
185  $texte .= '<tr>';
186  $texte .= '<td width="60%;">'.$langs->trans("DefaultModelPropalToBill").'</td>';
187  $texte .= '<td colspan="">';
188  $texte .= $form->selectarray('value3', $list, getDolGlobalString('USERGROUP_ADDON_PDF_ODT_TOBILL'));
189  $texte .= "</td></tr>";
190  $texte .= '<tr>';
191 
192  $texte .= '<td width="60%;">'.$langs->trans("DefaultModelPropalClosed").'</td>';
193  $texte .= '<td colspan="">';
194  $texte .= $form->selectarray('value4', $list, getDolGlobalString('USERGROUP_ADDON_PDF_ODT_CLOSED'));
195  $texte .= "</td></tr>";
196  $texte .= '</table>';
197  }
198  }
199 
200  $texte .= '</td>';
201 
202  $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
203  $texte .= '<span class="opacitymedium">';
204  $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
205  $texte .= '</span>';
206  $texte .= '</td>';
207  $texte .= '</tr>';
208 
209  $texte .= '</table>';
210  $texte .= '</form>';
211 
212  return $texte;
213  }
214 
215  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
227  public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
228  {
229  // phpcs:enable
230  global $user, $langs, $conf, $mysoc, $hookmanager;
231 
232  if (empty($srctemplatepath)) {
233  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
234  return -1;
235  }
236 
237  // Add odtgeneration hook
238  if (!is_object($hookmanager)) {
239  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
240  $hookmanager = new HookManager($this->db);
241  }
242  $hookmanager->initHooks(array('odtgeneration'));
243  global $action;
244 
245  if (!is_object($outputlangs)) {
246  $outputlangs = $langs;
247  }
248  $sav_charset_output = $outputlangs->charset_output;
249  $outputlangs->charset_output = 'UTF-8';
250 
251  // Load translation files required by the page
252  $outputlangs->loadLangs(array("main", "companies", "bills", "dict"));
253 
254  if ($conf->user->dir_output) {
255  // If $object is id instead of object
256  if (!is_object($object)) {
257  $id = $object;
258  $object = new UserGroup($this->db);
259  $result = $object->fetch($id);
260  if ($result < 0) {
261  dol_print_error($this->db, $object->error);
262  return -1;
263  }
264  }
265 
266  $dir = $conf->user->dir_output.'/usergroups';
267  $objectref = dol_sanitizeFileName($object->ref);
268  if (!preg_match('/specimen/i', $objectref)) {
269  $dir .= "/".$objectref;
270  }
271  $file = $dir."/".$objectref.".odt";
272 
273  if (!file_exists($dir)) {
274  if (dol_mkdir($dir) < 0) {
275  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
276  return -1;
277  }
278  }
279 
280  if (file_exists($dir)) {
281  //print "srctemplatepath=".$srctemplatepath; // Src filename
282  $newfile = basename($srctemplatepath);
283  $newfiletmp = preg_replace('/\.od(t|s)/i', '', $newfile);
284  $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
285  $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
286 
287  $newfiletmp = $objectref . '_' . $newfiletmp;
288 
289  // Get extension (ods or odt)
290  $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
291  if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
292  $format = $conf->global->MAIN_DOC_USE_TIMING;
293  if ($format == '1') {
294  $format = '%Y%m%d%H%M%S';
295  }
296  $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
297  } else {
298  $filename = $newfiletmp . '.' . $newfileformat;
299  }
300  $file = $dir . '/' . $filename;
301  //print "newdir=".$dir;
302  //print "newfile=".$newfile;
303  //print "file=".$file;
304  //print "conf->user->dir_temp=".$conf->user->dir_temp;
305 
306  dol_mkdir($conf->user->dir_temp);
307  if (!is_writable($conf->user->dir_temp)) {
308  $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->user->dir_temp);
309  dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
310  return -1;
311  }
312 
313  // If CUSTOMER contact defined on user, we use it
314  $usecontact = false;
315  $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
316  if (count($arrayidcontact) > 0) {
317  $usecontact = true;
318  $result = $object->fetch_contact($arrayidcontact[0]);
319  }
320 
321  // Recipient name
322  if (!empty($usecontact)) {
323  // We can use the company of contact instead of thirdparty company
324  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))) {
325  $object->contact->fetch_thirdparty();
326  $socobject = $object->contact->thirdparty;
327  $contactobject = $object->contact;
328  } else {
329  $socobject = $object->thirdparty;
330  // if we have a CUSTOMER contact and we dont use it as thirdparty recipient we store the contact object for later use
331  $contactobject = $object->contact;
332  }
333  } else {
334  $socobject = $object->thirdparty;
335  }
336  // Make substitution
337  $substitutionarray = array(
338  '__FROM_NAME__' => $this->emetteur->name,
339  '__FROM_EMAIL__' => $this->emetteur->email,
340  '__TOTAL_TTC__' => $object->total_ttc,
341  '__TOTAL_HT__' => $object->total_ht,
342  '__TOTAL_VAT__' => $object->total_tva
343  );
344  complete_substitutions_array($substitutionarray, $langs, $object);
345  // Call the ODTSubstitution hook
346  $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray);
347  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
348 
349  // Line of free text
350  $newfreetext = '';
351  $paramfreetext = 'user_FREE_TEXT';
352  if (!empty($conf->global->$paramfreetext)) {
353  $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
354  }
355 
356  // Open and load template
357  require_once ODTPHP_PATH.'odf.php';
358  try {
359  $odfHandler = new odf(
360  $srctemplatepath,
361  array(
362  'PATH_TO_TMP' => $conf->user->dir_temp,
363  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
364  'DELIMITER_LEFT' => '{',
365  'DELIMITER_RIGHT' => '}'
366  )
367  );
368  } catch (Exception $e) {
369  $this->error = $e->getMessage();
370  dol_syslog($e->getMessage(), LOG_WARNING);
371  return -1;
372  }
373  // After construction $odfHandler->contentXml contains content and
374  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
375  // [!-- BEGIN lines --]*[!-- END lines --]
376  //print html_entity_decode($odfHandler->__toString());
377  //print exit;
378 
379 
380  // Make substitutions into odt of freetext
381  try {
382  $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
383  } catch (OdfException $e) {
384  dol_syslog($e->getMessage(), LOG_WARNING);
385  }
386 
387  // Make substitutions into odt
388  $array_user = $this->get_substitutionarray_user($user, $outputlangs);
389  $array_global = $this->get_substitutionarray_each_var_object($object, $outputlangs);
390  $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
391  $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
392  $array_objet = $this->get_substitutionarray_each_var_object($object, $outputlangs);
393  $array_other = $this->get_substitutionarray_other($outputlangs);
394  // retrieve contact information for use in object as contact_xxx tags
395  $array_thirdparty_contact = array();
396  if ($usecontact && is_object($contactobject)) {
397  $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
398  }
399 
400  $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
401  complete_substitutions_array($tmparray, $outputlangs, $object);
402  $object->fetch_optionals();
403  // Call the ODTSubstitution hook
404  $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
405  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
406  foreach ($tmparray as $key => $value) {
407  try {
408  if (preg_match('/logo$/', $key)) { // Image
409  if (file_exists($value)) {
410  $odfHandler->setImage($key, $value);
411  } else {
412  $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
413  }
414  } else // Text
415  {
416  $odfHandler->setVars($key, $value, true, 'UTF-8');
417  }
418  } catch (OdfException $e) {
419  dol_syslog($e->getMessage(), LOG_WARNING);
420  }
421  }
422  // Replace tags of lines
423  try {
424  $foundtagforlines = 1;
425  try {
426  $listlines = $odfHandler->setSegment('lines');
427  } catch (OdfException $e) {
428  // We may arrive here if tags for lines not present into template
429  $foundtagforlines = 0;
430  dol_syslog($e->getMessage(), LOG_INFO);
431  }
432  if ($foundtagforlines) {
433  foreach ($object->members as $u) {
434  $tmparray = $this->get_substitutionarray_each_var_object($u, $outputlangs);
435  unset($tmparray['object_pass']);
436  unset($tmparray['object_pass_indatabase']);
437  complete_substitutions_array($tmparray, $outputlangs, $object, $user, "completesubstitutionarray_users");
438  // Call the ODTSubstitutionLine hook
439  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$u);
440  $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
441  foreach ($tmparray as $key => $val) {
442  try {
443  if (!is_array($val)) {
444  $listlines->setVars($key, $val, true, 'UTF-8');
445  }
446  } catch (OdfException $e) {
447  dol_syslog($e->getMessage(), LOG_WARNING);
448  } catch (SegmentException $e) {
449  dol_syslog($e->getMessage(), LOG_WARNING);
450  }
451  }
452  $listlines->merge();
453  }
454  $odfHandler->mergeSegment($listlines);
455  }
456  } catch (OdfException $e) {
457  $this->error = $e->getMessage();
458  dol_syslog($this->error, LOG_WARNING);
459  return -1;
460  }
461 
462  // Replace labels translated
463  $tmparray = $outputlangs->get_translations_for_substitutions();
464  foreach ($tmparray as $key => $value) {
465  try {
466  $odfHandler->setVars($key, $value, true, 'UTF-8');
467  } catch (OdfException $e) {
468  dol_syslog($e->getMessage(), LOG_WARNING);
469  }
470  }
471 
472  // Call the beforeODTSave hook
473  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
474  $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
475 
476  // Write new file
477  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
478  try {
479  $odfHandler->exportAsAttachedPDF($file);
480  } catch (Exception $e) {
481  $this->error = $e->getMessage();
482  dol_syslog($e->getMessage(), LOG_WARNING);
483  return -1;
484  }
485  } else {
486  try {
487  $odfHandler->saveToDisk($file);
488  } catch (Exception $e) {
489  $this->error = $e->getMessage();
490  dol_syslog($e->getMessage(), LOG_WARNING);
491  return -1;
492  }
493  }
494 
495  $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
496 
497  if (!empty($conf->global->MAIN_UMASK)) {
498  @chmod($file, octdec($conf->global->MAIN_UMASK));
499  }
500 
501  $odfHandler = null; // Destroy object
502 
503  $this->result = array('fullpath'=>$file);
504 
505  return 1; // Success
506  } else {
507  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
508  return -1;
509  }
510  }
511 
512  return -1;
513  }
514 }
get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive=1)
Define array with couple substitution key => substitution value.
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple substitution key => substitution value.
get_substitutionarray_contact($object, $outputlangs, $array_key='object')
Define array with couple substitution key => substitution value.
get_substitutionarray_other($outputlangs)
Define array with couple substitution key => substitution value.
get_substitutionarray_thirdparty($object, $outputlangs, $array_key='company')
Define array with couple substitution key => substitution value For example {company_name},...
get_substitutionarray_user($user, $outputlangs)
Define array with couple substitution key => substitution value.
Class to manage generation of HTML components Only common components must be here.
Class to manage hooks.
Parent class to manage intervention document templates.
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
Class to manage user groups.
Class to build documents using ODF templates generator.
info($langs)
Return description of a module.
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
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:61
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
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...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:122
$conf db
API class for accounts.
Definition: inc.php:41