dolibarr  9.0.0
doc_generic_user_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) 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 <http://www.gnu.org/licenses/>.
18 * or see http://www.gnu.org/
19 */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/modules/user/modules_user.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.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, 4);
50 
55  public $version = 'dolibarr';
56 
57 
63  function __construct($db)
64  {
65  global $conf, $langs, $mysoc;
66 
67  // Load translation files required by the page
68  $langs->loadLangs(array("main","companies"));
69 
70  $this->db = $db;
71  $this->name = "ODT templates";
72  $this->description = $langs->trans("DocumentModelOdt");
73  $this->scandir = 'USER_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
74 
75  // Dimension page pour format A4
76  $this->type = 'odt';
77  $this->page_largeur = 0;
78  $this->page_hauteur = 0;
79  $this->format = array($this->page_largeur,$this->page_hauteur);
80  $this->marge_gauche=0;
81  $this->marge_droite=0;
82  $this->marge_haute=0;
83  $this->marge_basse=0;
84 
85  $this->option_logo = 1; // Affiche logo
86  $this->option_tva = 0; // Gere option tva USER_TVAOPTION
87  $this->option_modereg = 0; // Affiche mode reglement
88  $this->option_condreg = 0; // Affiche conditions reglement
89  $this->option_codeproduitservice = 0; // Affiche code produit-service
90  $this->option_multilang = 1; // Dispo en plusieurs langues
91  $this->option_escompte = 0; // Affiche si il y a eu escompte
92  $this->option_credit_note = 0; // Support credit notes
93  $this->option_freetext = 1; // Support add of a personalised text
94  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
95 
96  // Recupere emetteur
97  $this->emetteur=$mysoc;
98  if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // By default if not defined
99  }
100 
101 
108  function info($langs)
109  {
110  global $conf, $langs;
111 
112  // Load translation files required by the page
113  $langs->loadLangs(array('companies', 'errors'));
114 
115  $form = new Form($this->db);
116 
117  $texte = $this->description.".<br>\n";
118  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
119  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
120  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
121  $texte.= '<input type="hidden" name="param1" value="USER_ADDON_PDF_ODT_PATH">';
122  if ($conf->global->MAIN_PROPAL_CHOOSE_ODT_DOCUMENT > 0)
123  {
124  $texte.= '<input type="hidden" name="param2" value="USER_ADDON_PDF_ODT_DEFAULT">';
125  $texte.= '<input type="hidden" name="param3" value="USER_ADDON_PDF_ODT_TOBILL">';
126  $texte.= '<input type="hidden" name="param4" value="USER_ADDON_PDF_ODT_CLOSED">';
127  }
128  $texte.= '<table class="nobordernopadding" width="100%">';
129 
130  // List of directories area
131  $texte.= '<tr><td>';
132  $texttitle=$langs->trans("ListOfDirectories");
133  $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->USER_ADDON_PDF_ODT_PATH)));
134  $listoffiles=array();
135  foreach($listofdir as $key=>$tmpdir)
136  {
137  $tmpdir=trim($tmpdir);
138  $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
139  if (! $tmpdir) {
140  unset($listofdir[$key]); continue;
141  }
142  if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
143  else
144  {
145  $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.(ods|odt)');
146  if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
147  }
148  }
149  $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
150  // Add list of substitution keys
151  $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
152  $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
153 
154  $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
155  $texte.= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
156  $texte.= '<textarea class="flat" cols="60" name="value1">';
157  $texte.=$conf->global->USER_ADDON_PDF_ODT_PATH;
158  $texte.= '</textarea>';
159  $texte.= '</div><div style="display: inline-block; vertical-align: middle;">';
160  $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
161  $texte.= '<br></div></div>';
162 
163  // Scan directories
164  if (count($listofdir))
165  {
166  $texte.=$langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
167 
168  if ($conf->global->MAIN_PROPAL_CHOOSE_ODT_DOCUMENT > 0)
169  {
170  // Model for creation
171  $liste=ModelePDFUser::liste_modeles($this->db);
172  $texte.= '<table width="50%;">';
173  $texte.= '<tr>';
174  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalCreate").'</td>';
175  $texte.= '<td colspan="">';
176  $texte.= $form->selectarray('value2',$liste,$conf->global->USER_ADDON_PDF_ODT_DEFAULT);
177  $texte.= "</td></tr>";
178 
179  $texte.= '<tr>';
180  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalToBill").'</td>';
181  $texte.= '<td colspan="">';
182  $texte.= $form->selectarray('value3',$liste,$conf->global->USER_ADDON_PDF_ODT_TOBILL);
183  $texte.= "</td></tr>";
184  $texte.= '<tr>';
185 
186  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalClosed").'</td>';
187  $texte.= '<td colspan="">';
188  $texte.= $form->selectarray('value4',$liste,$conf->global->USER_ADDON_PDF_ODT_CLOSED);
189  $texte.= "</td></tr>";
190  $texte.= '</table>';
191  }
192  }
193 
194  $texte.= '</td>';
195 
196  $texte.= '<td valign="top" rowspan="2" class="hideonsmartphone">';
197  $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
198  $texte.= '</td>';
199  $texte.= '</tr>';
200 
201  $texte.= '</table>';
202  $texte.= '</form>';
203 
204  return $texte;
205  }
206 
207  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
219  function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidedesc=0,$hideref=0)
220  {
221  // phpcs:enable
222  global $user, $langs, $conf, $mysoc, $hookmanager;
223 
224  if (empty($srctemplatepath))
225  {
226  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
227  return -1;
228  }
229 
230  // Add odtgeneration hook
231  if (! is_object($hookmanager))
232  {
233  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
234  $hookmanager=new HookManager($this->db);
235  }
236  $hookmanager->initHooks(array('odtgeneration'));
237  global $action;
238 
239  if (! is_object($outputlangs)) $outputlangs=$langs;
240  $sav_charset_output=$outputlangs->charset_output;
241  $outputlangs->charset_output='UTF-8';
242 
243  // Load translation files required by the page
244  $outputlangs->loadLangs(array("main", "companies", "bills", "dict"));
245 
246  if ($conf->user->dir_output)
247  {
248  // If $object is id instead of object
249  if (! is_object($object))
250  {
251  $id = $object;
252  $object = new User($this->db);
253  $result=$object->fetch($id);
254  if ($result < 0)
255  {
256  dol_print_error($this->db,$object->error);
257  return -1;
258  }
259  }
260 
261  $dir = $conf->user->dir_output;
262  $objectref = dol_sanitizeFileName($object->ref);
263  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
264  $file = $dir . "/" . $objectref . ".odt";
265 
266  if (! file_exists($dir))
267  {
268  if (dol_mkdir($dir) < 0)
269  {
270  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
271  return -1;
272  }
273  }
274 
275  if (file_exists($dir))
276  {
277  //print "srctemplatepath=".$srctemplatepath; // Src filename
278  $newfile=basename($srctemplatepath);
279  $newfiletmp=preg_replace('/\.od(t|s)/i','',$newfile);
280  $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
281  $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
282 
283  $newfiletmp=$objectref.'_'.$newfiletmp;
284 
285  // Get extension (ods or odt)
286  $newfileformat=substr($newfile, strrpos($newfile, '.')+1);
287  if ( ! empty($conf->global->MAIN_DOC_USE_TIMING))
288  {
289  $format=$conf->global->MAIN_DOC_USE_TIMING;
290  if ($format == '1') $format='%Y%m%d%H%M%S';
291  $filename=$newfiletmp.'-'.dol_print_date(dol_now(),$format).'.'.$newfileformat;
292  }
293  else
294  {
295  $filename=$newfiletmp.'.'.$newfileformat;
296  }
297  $file=$dir.'/'.$filename;
298  //print "newdir=".$dir;
299  //print "newfile=".$newfile;
300  //print "file=".$file;
301  //print "conf->user->dir_temp=".$conf->user->dir_temp;
302 
303  dol_mkdir($conf->user->dir_temp);
304 
305 
306  // If CUSTOMER contact defined on user, we use it
307  $usecontact=false;
308  $arrayidcontact=$object->getIdContact('external','CUSTOMER');
309  if (count($arrayidcontact) > 0)
310  {
311  $usecontact=true;
312  $result=$object->fetch_contact($arrayidcontact[0]);
313  }
314 
315  // Recipient name
316  if (! empty($usecontact))
317  {
318  // On peut utiliser le nom de la societe du contact
319  if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)) $socobject = $object->contact;
320  else {
321  $socobject = $object->thirdparty;
322  // if we have a CUSTOMER contact and we dont use it as recipient we store the contact object for later use
323  $contactobject = $object->contact;
324  }
325  }
326  else
327  {
328  $socobject=$object->thirdparty;
329  }
330 
331  // Open and load template
332  require_once ODTPHP_PATH.'odf.php';
333  try {
334  $odfHandler = new odf(
335  $srctemplatepath,
336  array(
337  'PATH_TO_TMP' => $conf->user->dir_temp,
338  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
339  'DELIMITER_LEFT' => '{',
340  'DELIMITER_RIGHT' => '}'
341  )
342  );
343  }
344  catch(Exception $e)
345  {
346  $this->error=$e->getMessage();
347  dol_syslog($e->getMessage(), LOG_WARNING);
348  return -1;
349  }
350 
351  // Make substitutions into odt
352  $array_user=$this->get_substitutionarray_user($object,$outputlangs);
353  $array_soc=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
354  $array_thirdparty=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
355  $array_other=$this->get_substitutionarray_other($outputlangs);
356  // retrieve contact information for use in object as contact_xxx tags
357  $array_thirdparty_contact = array();
358  if ($usecontact && is_object($contactobject)) $array_thirdparty_contact=$this->get_substitutionarray_contact($contactobject,$outputlangs,'contact');
359 
360  $tmparray = array_merge($array_user,$array_soc,$array_thirdparty,$array_other,$array_thirdparty_contact);
361  complete_substitutions_array($tmparray, $outputlangs, $object);
362  $object->fetch_optionals();
363  // Call the ODTSubstitution hook
364  $parameters=array('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  foreach($tmparray as $key=>$value)
367  {
368  try {
369  if (preg_match('/logo$/',$key)) // Image
370  {
371  if (file_exists($value)) $odfHandler->setImage($key, $value);
372  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
373  }
374  else // Text
375  {
376  $odfHandler->setVars($key, $value, true, 'UTF-8');
377  }
378  }
379  catch(OdfException $e)
380  {
381  dol_syslog($e->getMessage(), LOG_WARNING);
382  }
383  }
384 
385  // Replace labels translated
386  $tmparray=$outputlangs->get_translations_for_substitutions();
387  foreach($tmparray as $key=>$value)
388  {
389  try {
390  $odfHandler->setVars($key, $value, true, 'UTF-8');
391  }
392  catch (OdfException $e)
393  {
394  dol_syslog($e->getMessage(), LOG_WARNING);
395  }
396  }
397 
398  // Call the beforeODTSave hook
399  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
400  $reshook=$hookmanager->executeHooks('beforeODTSave',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
401 
402  // Write new file
403  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
404  try {
405  $odfHandler->exportAsAttachedPDF($file);
406  } catch (Exception $e) {
407  $this->error=$e->getMessage();
408  dol_syslog($e->getMessage(), LOG_WARNING);
409  return -1;
410  }
411  }
412  else {
413  try {
414  $odfHandler->saveToDisk($file);
415  } catch (Exception $e) {
416  $this->error=$e->getMessage();
417  dol_syslog($e->getMessage(), LOG_WARNING);
418  return -1;
419  }
420  }
421 
422  $reshook=$hookmanager->executeHooks('afterODTCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
423 
424  if (! empty($conf->global->MAIN_UMASK))
425  @chmod($file, octdec($conf->global->MAIN_UMASK));
426 
427  $odfHandler=null; // Destroy object
428 
429  $this->result = array('fullpath'=>$file);
430 
431  return 1; // Success
432  }
433  else
434  {
435  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
436  return -1;
437  }
438  }
439 
440  return -1;
441  }
442 
443  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
452  function get_substitutionarray_object($object,$outputlangs,$array_key='object')
453  {
454  // phpcs:enable
455  $array_other = array();
456  foreach($object as $key => $value) {
457  if (!is_array($value) && !is_object($value)) {
458  $array_other[$array_key.'_'.$key] = $value;
459  }
460  }
461  return $array_other;
462  }
463 }
img_warning($titlealt='default', $moreatt='')
Show warning logo.
Parent class to manage intervention document templates.
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
Class to build documents using ODF templates generator.
get_substitutionarray_object($object, $outputlangs, $array_key='object')
get substitution array for object
</td >< td class="liste_titre" align="right"></td ></tr >< tr class="liste_titre">< input type="checkbox" onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< tr class="oddeven">< td >< input type="checkbox" class="check" name="' . $i . '"' . $disabled . '></td >< td >< input type="checkbox" class="check" name="choose'.$i.'"></td >< td class="nowrap"></td >< td >< input type="hidden" name="desc' . $i . '" value="' . dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:573
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Class to manage Dolibarr users.
Definition: user.class.php:41
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
get_substitutionarray_user($user, $outputlangs)
Define array with couple subtitution key => subtitution value.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
Class to manage hooks.
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple subtitution key => subtitution value.
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:59
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
info($langs)
Return description of a module.
get_substitutionarray_contact($object, $outputlangs, $array_key='object')
Define array with couple subtitution key => subtitution value.
get_substitutionarray_thirdparty($object, $outputlangs)
Define array with couple subtitution key => subtitution value.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
$version
Dolibarr version of the loaded document string.
get_substitutionarray_other($outputlangs)
Define array with couple subtitution key => subtitution 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...