dolibarr  7.0.0-beta
doc_generic_contract_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 Ferran Marcet <fmarcet@2byte.es>
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/contract/modules_contract.php';
28 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
33 
34 
39 {
40  var $emetteur; // Objet societe qui emet
41 
42  var $phpmin = array(5,2,0); // Minimum version of PHP required by module
43  var $version = 'dolibarr';
44 
45 
51  function __construct($db)
52  {
53  global $conf,$langs,$mysoc;
54 
55  $langs->load("main");
56  $langs->load("companies");
57 
58  $this->db = $db;
59  $this->name = "ODT templates";
60  $this->description = $langs->trans("DocumentModelOdt");
61  $this->scandir = 'CONTRACT_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
62 
63  // Dimension page pour format A4
64  $this->type = 'odt';
65  $this->page_largeur = 0;
66  $this->page_hauteur = 0;
67  $this->format = array($this->page_largeur,$this->page_hauteur);
68  $this->marge_gauche=0;
69  $this->marge_droite=0;
70  $this->marge_haute=0;
71  $this->marge_basse=0;
72 
73  $this->option_logo = 1; // Affiche logo
74  $this->option_tva = 0; // Gere option tva CONTRACT_TVAOPTION
75  $this->option_modereg = 0; // Affiche mode reglement
76  $this->option_condreg = 0; // Affiche conditions reglement
77  $this->option_codeproduitservice = 0; // Affiche code produit-service
78  $this->option_multilang = 1; // Dispo en plusieurs langues
79  $this->option_escompte = 0; // Affiche si il y a eu escompte
80  $this->option_credit_note = 0; // Support credit notes
81  $this->option_freetext = 1; // Support add of a personalised text
82  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
83 
84  // Recupere emetteur
85  $this->emetteur=$mysoc;
86  if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // By default if not defined
87  }
88 
89 
96  function info($langs)
97  {
98  global $conf,$langs;
99 
100  $langs->load("companies");
101  $langs->load("errors");
102 
103  $form = new Form($this->db);
104 
105  $texte = $this->description.".<br>\n";
106  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">';
107  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
108  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
109  $texte.= '<input type="hidden" name="param1" value="CONTRACT_ADDON_PDF_ODT_PATH">';
110  $texte.= '<table class="nobordernopadding" width="100%">';
111 
112  // List of directories area
113  $texte.= '<tr><td>';
114  $texttitle=$langs->trans("ListOfDirectories");
115  $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->CONTRACT_ADDON_PDF_ODT_PATH)));
116  $listoffiles=array();
117  foreach($listofdir as $key=>$tmpdir)
118  {
119  $tmpdir=trim($tmpdir);
120  $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
121  if (! $tmpdir) {
122  unset($listofdir[$key]); continue;
123  }
124  if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
125  else
126  {
127  $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.(ods|odt)');
128  if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
129  }
130  }
131  $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
132  // Add list of substitution keys
133  $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
134  $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
135 
136  $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
137  $texte.= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
138  $texte.= '<textarea class="flat" cols="60" name="value1">';
139  $texte.=$conf->global->CONTRACT_ADDON_PDF_ODT_PATH;
140  $texte.= '</textarea>';
141  $texte.= '</div><div style="display: inline-block; vertical-align: middle;">';
142  $texte.= '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Modify")).'" name="Button">';
143  $texte.= '<br></div></div>';
144 
145  // Scan directories
146  if (count($listofdir))
147  {
148  $texte.=$langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
149  }
150 
151  // Add select to upload a new template file. TODO Copy this feature on other admin pages.
152  $texte.= '<div>'.$langs->trans("UploadNewTemplate").' <input type="file" name="uploadfile">';
153  $texte.= '<input type="hidden" value="CONTRACT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
154  $texte.= '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
155  $texte.= '</div>';
156 
157  $texte.= '</td>';
158 
159  $texte.= '<td valign="top" rowspan="2" class="hideonsmartphone">';
160  $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
161  $texte.= '</td>';
162  $texte.= '</tr>';
163 
164  $texte.= '</table>';
165  $texte.= '</form>';
166 
167  return $texte;
168  }
169 
181  function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidedesc=0,$hideref=0)
182  {
183  global $user,$langs,$conf,$mysoc,$hookmanager;
184 
185  if (empty($srctemplatepath))
186  {
187  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
188  return -1;
189  }
190 
191  // Add odtgeneration hook
192  if (! is_object($hookmanager))
193  {
194  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
195  $hookmanager=new HookManager($this->db);
196  }
197  $hookmanager->initHooks(array('odtgeneration'));
198  global $action;
199 
200  if (! is_object($outputlangs)) $outputlangs=$langs;
201  $sav_charset_output=$outputlangs->charset_output;
202  $outputlangs->charset_output='UTF-8';
203 
204  $outputlangs->load("main");
205  $outputlangs->load("dict");
206  $outputlangs->load("companies");
207  $outputlangs->load("bills");
208 
209  if ($conf->contrat->dir_output)
210  {
211  // If $object is id instead of object
212  if (! is_object($object))
213  {
214  $id = $object;
215  $object = new Contrat($this->db);
216  $result=$object->fetch($id);
217  if ($result < 0)
218  {
219  dol_print_error($this->db,$object->error);
220  return -1;
221  }
222  }
223 
224  $dir = $conf->contrat->dir_output;
225  $objectref = dol_sanitizeFileName($object->ref);
226  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
227  $file = $dir . "/" . $objectref . ".odt";
228 
229  if (! file_exists($dir))
230  {
231  if (dol_mkdir($dir) < 0)
232  {
233  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
234  return -1;
235  }
236  }
237 
238  if (file_exists($dir))
239  {
240  //print "srctemplatepath=".$srctemplatepath; // Src filename
241  $newfile=basename($srctemplatepath);
242  $newfiletmp=preg_replace('/\.od(t|s)/i','',$newfile);
243  $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
244  $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
245 
246  $newfiletmp=$objectref.'_'.$newfiletmp;
247 
248  // Get extension (ods or odt)
249  $newfileformat=substr($newfile, strrpos($newfile, '.')+1);
250  if ( ! empty($conf->global->MAIN_DOC_USE_TIMING))
251  {
252  $format=$conf->global->MAIN_DOC_USE_TIMING;
253  if ($format == '1') $format='%Y%m%d%H%M%S';
254  $filename=$newfiletmp.'-'.dol_print_date(dol_now(),$format).'.'.$newfileformat;
255  }
256  else
257  {
258  $filename=$newfiletmp.'.'.$newfileformat;
259  }
260  $file=$dir.'/'.$filename;
261  //print "newdir=".$dir;
262  //print "newfile=".$newfile;
263  //print "file=".$file;
264  //print "conf->contrat->dir_temp=".$conf->contrat->dir_temp;
265 
266  dol_mkdir($conf->contrat->dir_temp);
267 
268 
269  // If CUSTOMER contact defined on contract, we use it
270  $usecontact=false;
271  $arrayidcontact=$object->getIdContact('external','CUSTOMER');
272  if (count($arrayidcontact) > 0)
273  {
274  $usecontact=true;
275  $result=$object->fetch_contact($arrayidcontact[0]);
276  }
277 
278  // Recipient name
279  if (! empty($usecontact))
280  {
281  // On peut utiliser le nom de la societe du contact
282  if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)) $socobject = $object->contact;
283  else {
284  $socobject = $object->thirdparty;
285  // if we have a CUSTOMER contact and we dont use it as recipient we store the contact object for later use
286  $contactobject = $object->contact;
287  }
288  }
289  else
290  {
291  $socobject=$object->thirdparty;
292  }
293  // Make substitution
294  $substitutionarray=array(
295  '__FROM_NAME__' => $this->emetteur->name,
296  '__FROM_EMAIL__' => $this->emetteur->email,
297  '__TOTAL_TTC__' => $object->total_ttc,
298  '__TOTAL_HT__' => $object->total_ht,
299  '__TOTAL_VAT__' => $object->total_vat
300  );
301  complete_substitutions_array($substitutionarray, $langs, $object);
302  // Call the ODTSubstitution hook
303  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$substitutionarray);
304  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
305 
306  // Line of free text
307  $newfreetext='';
308  $paramfreetext='contract_FREE_TEXT';
309  if (! empty($conf->global->$paramfreetext))
310  {
311  $newfreetext=make_substitutions($conf->global->$paramfreetext,$substitutionarray);
312  }
313 
314  // Open and load template
315  require_once ODTPHP_PATH.'odf.php';
316  try {
317  $odfHandler = new odf(
318  $srctemplatepath,
319  array(
320  'PATH_TO_TMP' => $conf->contrat->dir_temp,
321  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
322  'DELIMITER_LEFT' => '{',
323  'DELIMITER_RIGHT' => '}'
324  )
325  );
326  }
327  catch(Exception $e)
328  {
329  $this->error=$e->getMessage();
330  return -1;
331  }
332  // After construction $odfHandler->contentXml contains content and
333  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
334  // [!-- BEGIN lines --]*[!-- END lines --]
335  //print html_entity_decode($odfHandler->__toString());
336  //print exit;
337 
338 
339  // Make substitutions into odt of freetext
340  try {
341  $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
342  }
343  catch(OdfException $e)
344  {
345  }
346 
347  // Make substitutions into odt
348  $array_contract=$this->get_substitutionarray_each_var_object($object, $outputlangs);
349  $array_user=$this->get_substitutionarray_user($user,$outputlangs);
350  $array_soc=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
351  $array_thirdparty=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
352  $array_objet=$this->get_substitutionarray_object($object,$outputlangs);
353  $array_other=$this->get_substitutionarray_other($outputlangs);
354  // retrieve contact information for use in contract as contact_xxx tags
355  $array_thirdparty_contact = array();
356  if ($usecontact)
357  $array_thirdparty_contact=$this->get_substitutionarray_contact($contactobject,$outputlangs,'contact');
358 
359  $tmparray = array_merge($array_contract,$array_user,$array_soc,$array_thirdparty,$array_objet,$array_other,$array_thirdparty_contact);
360  complete_substitutions_array($tmparray, $outputlangs, $object);
361  $object->fetch_optionals();
362  // Call the ODTSubstitution hook
363  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
364  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
365  foreach($tmparray as $key=>$value)
366  {
367  try {
368  if (preg_match('/logo$/',$key)) // Image
369  {
370  if (file_exists($value)) $odfHandler->setImage($key, $value);
371  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
372  }
373  else // Text
374  {
375  $odfHandler->setVars($key, $value, true, 'UTF-8');
376  }
377  }
378  catch(OdfException $e)
379  {
380  }
381  }
382  // Replace tags of lines
383  try
384  {
385  $listlines = $odfHandler->setSegment('lines');
386  foreach ($object->lines as $line)
387  {
388  $tmparray=$this->get_substitutionarray_lines($line,$outputlangs);
389  complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
390  // Call the ODTSubstitutionLine hook
391  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray,'line'=>$line);
392  $reshook=$hookmanager->executeHooks('ODTSubstitutionLine',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
393  foreach($tmparray as $key => $val)
394  {
395  try
396  {
397  $listlines->setVars($key, $val, true, 'UTF-8');
398  }
399  catch(OdfException $e)
400  {
401  }
402  catch(SegmentException $e)
403  {
404  }
405  }
406  $listlines->merge();
407  }
408  $odfHandler->mergeSegment($listlines);
409  }
410  catch(OdfException $e)
411  {
412  $this->error=$e->getMessage();
413  dol_syslog($this->error, LOG_WARNING);
414  return -1;
415  }
416 
417  // Replace labels translated
418  $tmparray=$outputlangs->get_translations_for_substitutions();
419  foreach($tmparray as $key=>$value)
420  {
421  try {
422  $odfHandler->setVars($key, $value, true, 'UTF-8');
423  }
424  catch(OdfException $e)
425  {
426  }
427  }
428 
429  // Call the beforeODTSave hook
430  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
431  $reshook=$hookmanager->executeHooks('beforeODTSave',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
432 
433  // Write new file
434  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
435  try {
436  $odfHandler->exportAsAttachedPDF($file);
437  }catch (Exception $e){
438  $this->error=$e->getMessage();
439  return -1;
440  }
441  }
442  else {
443  try {
444  $odfHandler->saveToDisk($file);
445  }catch (Exception $e){
446  $this->error=$e->getMessage();
447  return -1;
448  }
449  }
450 
451  $reshook=$hookmanager->executeHooks('afterODTCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
452 
453  if (! empty($conf->global->MAIN_UMASK))
454  @chmod($file, octdec($conf->global->MAIN_UMASK));
455 
456  $odfHandler=null; // Destroy object
457 
458  $this->result = array('fullpath'=>$file);
459 
460  return 1; // Success
461  }
462  else
463  {
464  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
465  return -1;
466  }
467  }
468 
469  return -1;
470  }
471 
472 }
473 
info($langs)
Return description of a module.
Parent class to manage intervention document templates.
get_substitutionarray_lines($line, $outputlangs)
Define array with couple substitution key => substitution value.
</td >< tdclass="liste_titre"align="right"></td ></tr >< trclass="liste_titre">< inputtype="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< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="'.$i.'"'.$disabled.'></td >< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"></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:554
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive=true)
Define array with couple subtitution key => subtitution value.
get_substitutionarray_object($object, $outputlangs, $array_key='object')
Define array with couple substitution key => substitution value.
get_substitutionarray_user($user, $outputlangs)
Define array with couple subtitution key => subtitution value.
Class to manage contracts.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="")
Scan a directory and return a list of files/directories.
Definition: files.lib.php:58
Class to manage hooks.
get_substitutionarray_contact($object, $outputlangs, $array_key= 'object')
Define array with couple subtitution key => subtitution value.
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.
img_warning($titlealt= 'default', $moreatt= '')
Show warning logo.
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple subtitution key => subtitution value.
Class to build documents using ODF templates generator.
dol_now($mode='gmt')
Return date for now.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:104
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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)
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substition into a text string, replacing keys with vals from $substitutionarray (oldval=>newval)...
get_substitutionarray_other($outputlangs)
Define array with couple subtitution key => subtitution value.
type
Definition: viewcat.php:283
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...