dolibarr  7.0.0-beta
doc_generic_shipment_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) 2014 Marcos GarcĂ­a <marcosgdf@gmail.com>
5  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
6 
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * or see http://www.gnu.org/
21 */
22 
29 require_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
30 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
35 
36 
41 {
42  var $emetteur; // Objet societe qui emet
43 
44  var $phpmin = array(5,2,0); // Minimum version of PHP required by module
45  var $version = 'dolibarr';
46 
47 
53  function __construct($db)
54  {
55  global $conf,$langs,$mysoc;
56 
57  $langs->load("main");
58  $langs->load("companies");
59 
60  $this->db = $db;
61  $this->name = "ODT templates";
62  $this->description = $langs->trans("DocumentModelOdt");
63  $this->scandir = 'EXPEDITION_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
64 
65  // Dimension page pour format A4
66  $this->type = 'odt';
67  $this->page_largeur = 0;
68  $this->page_hauteur = 0;
69  $this->format = array($this->page_largeur,$this->page_hauteur);
70  $this->marge_gauche=0;
71  $this->marge_droite=0;
72  $this->marge_haute=0;
73  $this->marge_basse=0;
74 
75  $this->option_logo = 1; // Affiche logo
76  $this->option_tva = 0; // Gere option tva EXPEDITION_TVAOPTION
77  $this->option_modereg = 0; // Affiche mode reglement
78  $this->option_condreg = 0; // Affiche conditions reglement
79  $this->option_codeproduitservice = 0; // Affiche code produit-service
80  $this->option_multilang = 1; // Dispo en plusieurs langues
81  $this->option_escompte = 0; // Affiche si il y a eu escompte
82  $this->option_credit_note = 0; // Support credit notes
83  $this->option_freetext = 1; // Support add of a personalised text
84  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
85 
86  // Recupere emetteur
87  $this->emetteur=$mysoc;
88  if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // By default if not defined
89  }
90 
91 
98  function info($langs)
99  {
100  global $conf,$langs;
101 
102  $langs->load("companies");
103  $langs->load("errors");
104 
105  $form = new Form($this->db);
106 
107  $texte = $this->description.".<br>\n";
108  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
109  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
110  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
111  $texte.= '<input type="hidden" name="param1" value="EXPEDITION_ADDON_PDF_ODT_PATH">';
112  $texte.= '<table class="nobordernopadding" width="100%">';
113 
114  // List of directories area
115  $texte.= '<tr><td>';
116  $texttitle=$langs->trans("ListOfDirectories");
117  $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->EXPEDITION_ADDON_PDF_ODT_PATH)));
118  $listoffiles=array();
119  foreach($listofdir as $key=>$tmpdir)
120  {
121  $tmpdir=trim($tmpdir);
122  $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
123  if (! $tmpdir) {
124  unset($listofdir[$key]); continue;
125  }
126  if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
127  else
128  {
129  $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.(ods|odt)');
130  if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
131  }
132  }
133  $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
134  // Add list of substitution keys
135  $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
136  $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
137 
138  $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
139  $texte.= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
140  $texte.= '<textarea class="flat" cols="60" name="value1">';
141  $texte.=$conf->global->EXPEDITION_ADDON_PDF_ODT_PATH;
142  $texte.= '</textarea>';
143  $texte.= '</div><div style="display: inline-block; vertical-align: middle;">';
144  $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
145  $texte.= '<br></div></div>';
146 
147  // Scan directories
148  $nbofiles=count($listoffiles);
149  if (! empty($conf->global->EXPEDITION_ADDON_PDF_ODT_PATH))
150  {
151  $texte.=$langs->trans("NumberOfModelFilesFound").': <b>';
152  //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
153  $texte.=count($listoffiles);
154  //$texte.=$nbofiles?'</a>':'';
155  $texte.='</b>';
156  }
157  if ($nbofiles)
158  {
159  $texte.='<div id="div_'.get_class($this).'" class="hidden">';
160  foreach($listoffiles as $file)
161  {
162  $texte.=$file['name'].'<br>';
163  }
164  $texte.='<div id="div_'.get_class($this).'">';
165  }
166 
167  $texte.= '</td>';
168 
169  $texte.= '<td valign="top" rowspan="2" class="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 
191  function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidedesc=0,$hideref=0)
192  {
193  global $user,$langs,$conf,$mysoc,$hookmanager;
194 
195  if (empty($srctemplatepath))
196  {
197  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
198  return -1;
199  }
200 
201  // Add odtgeneration hook
202  if (! is_object($hookmanager))
203  {
204  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
205  $hookmanager=new HookManager($this->db);
206  }
207  $hookmanager->initHooks(array('odtgeneration'));
208  global $action;
209 
210  if (! is_object($outputlangs)) $outputlangs=$langs;
211  $sav_charset_output=$outputlangs->charset_output;
212  $outputlangs->charset_output='UTF-8';
213 
214  $outputlangs->load("main");
215  $outputlangs->load("dict");
216  $outputlangs->load("companies");
217  $outputlangs->load("bills");
218 
219  if ($conf->expedition->dir_output."/sending")
220  {
221  // If $object is id instead of object
222  if (! is_object($object))
223  {
224  $id = $object;
225  $object = new Expedition($this->db);
226  $result=$object->fetch($id);
227  if ($result < 0)
228  {
229  dol_print_error($this->db,$object->error);
230  return -1;
231  }
232  }
233 
234  $dir = $conf->expedition->dir_output."/sending";
235  $objectref = dol_sanitizeFileName($object->ref);
236  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
237  $file = $dir . "/" . $objectref . ".odt";
238 
239  if (! file_exists($dir))
240  {
241  if (dol_mkdir($dir) < 0)
242  {
243  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
244  return -1;
245  }
246  }
247 
248  if (file_exists($dir))
249  {
250  //print "srctemplatepath=".$srctemplatepath; // Src filename
251  $newfile=basename($srctemplatepath);
252  $newfiletmp=preg_replace('/\.od(t|s)/i','',$newfile);
253  $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
254  $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
255  $newfiletmp=$objectref.'_'.$newfiletmp;
256  //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
257  // Get extension (ods or odt)
258  $newfileformat=substr($newfile, strrpos($newfile, '.')+1);
259  if ( ! empty($conf->global->MAIN_DOC_USE_TIMING))
260  {
261  $format=$conf->global->MAIN_DOC_USE_TIMING;
262  if ($format == '1') $format='%Y%m%d%H%M%S';
263  $filename=$newfiletmp.'-'.dol_print_date(dol_now(),$format).'.'.$newfileformat;
264  }
265  else
266  {
267  $filename=$newfiletmp.'.'.$newfileformat;
268  }
269  $file=$dir.'/'.$filename;
270  //print "newdir=".$dir;
271  //print "newfile=".$newfile;
272  //print "file=".$file;
273  //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
274 
275  dol_mkdir($conf->expedition->dir_temp);
276 
277 
278  // If BILLING contact defined on invoice, we use it
279  $usecontact=false;
280  $arrayidcontact=$object->getIdContact('external','BILLING');
281  if (count($arrayidcontact) > 0)
282  {
283  $usecontact=true;
284  $result=$object->fetch_contact($arrayidcontact[0]);
285  }
286 
287  // Recipient name
288  if (! empty($usecontact))
289  {
290  // On peut utiliser le nom de la societe du contact
291  if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)) $socobject = $object->contact;
292  else $socobject = $object->thirdparty;
293  }
294  else
295  {
296  $socobject=$object->thirdparty;
297  }
298 
299  // Make substitution
300  $substitutionarray=array(
301  '__FROM_NAME__' => $this->emetteur->name,
302  '__FROM_EMAIL__' => $this->emetteur->email,
303  '__TOTAL_TTC__' => $object->total_ttc,
304  '__TOTAL_HT__' => $object->total_ht,
305  '__TOTAL_VAT__' => $object->total_vat
306  );
307  complete_substitutions_array($substitutionarray, $langs, $object);
308  // Call the ODTSubstitution hook
309  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$substitutionarray);
310  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
311 
312  // Line of free text
313  $newfreetext='';
314  $paramfreetext='EXPEDITION_FREE_TEXT';
315  if (! empty($conf->global->$paramfreetext))
316  {
317  $newfreetext=make_substitutions($conf->global->$paramfreetext,$substitutionarray);
318  }
319 
320  // Open and load template
321  require_once ODTPHP_PATH.'odf.php';
322  try {
323  $odfHandler = new odf(
324  $srctemplatepath,
325  array(
326  'PATH_TO_TMP' => $conf->expedition->dir_temp,
327  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
328  'DELIMITER_LEFT' => '{',
329  'DELIMITER_RIGHT' => '}'
330  )
331  );
332  }
333  catch(Exception $e)
334  {
335  $this->error=$e->getMessage();
336  return -1;
337  }
338  // After construction $odfHandler->contentXml contains content and
339  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
340  // [!-- BEGIN lines --]*[!-- END lines --]
341  //print html_entity_decode($odfHandler->__toString());
342  //print exit;
343 
344 
345  // Make substitutions into odt of freetext
346  try {
347  $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
348  }
349  catch(OdfException $e)
350  {
351  }
352 
353  // Make substitutions into odt of user info
354  $tmparray=$this->get_substitutionarray_user($user,$outputlangs);
355  //var_dump($tmparray); exit;
356  foreach($tmparray as $key=>$value)
357  {
358  try {
359  if (preg_match('/logo$/',$key)) // Image
360  {
361  //var_dump($value);exit;
362  if (file_exists($value)) $odfHandler->setImage($key, $value);
363  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
364  }
365  else // Text
366  {
367  $odfHandler->setVars($key, $value, true, 'UTF-8');
368  }
369  }
370  catch(OdfException $e)
371  {
372  }
373  }
374  // Make substitutions into odt of mysoc
375  $tmparray=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
376  //var_dump($tmparray); exit;
377  foreach($tmparray as $key=>$value)
378  {
379  try {
380  if (preg_match('/logo$/',$key)) // Image
381  {
382  //var_dump($value);exit;
383  if (file_exists($value)) $odfHandler->setImage($key, $value);
384  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
385  }
386  else // Text
387  {
388  $odfHandler->setVars($key, $value, true, 'UTF-8');
389  }
390  }
391  catch(OdfException $e)
392  {
393  }
394  }
395  // Make substitutions into odt of thirdparty
396  $tmparray=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
397  foreach($tmparray as $key=>$value)
398  {
399  try {
400  if (preg_match('/logo$/',$key)) // Image
401  {
402  if (file_exists($value)) $odfHandler->setImage($key, $value);
403  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
404  }
405  else // Text
406  {
407  $odfHandler->setVars($key, $value, true, 'UTF-8');
408  }
409  }
410  catch(OdfException $e)
411  {
412  }
413  }
414  // Replace tags of object + external modules
415  $tmparray=$this->get_substitutionarray_shipment($object,$outputlangs);
416  complete_substitutions_array($tmparray, $outputlangs, $object);
417  // Call the ODTSubstitution hook
418  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
419  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
420  foreach($tmparray as $key=>$value)
421  {
422  try {
423  if (preg_match('/logo$/',$key)) // Image
424  {
425  if (file_exists($value)) $odfHandler->setImage($key, $value);
426  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
427  }
428  else // Text
429  {
430  $odfHandler->setVars($key, $value, true, 'UTF-8');
431  }
432  }
433  catch(OdfException $e)
434  {
435  }
436  }
437  // Replace tags of lines
438  try
439  {
440  $listlines = $odfHandler->setSegment('lines');
441  foreach ($object->lines as $line)
442  {
443  $tmparray=$this->get_substitutionarray_shipment_lines($line,$outputlangs);
444  complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
445  // Call the ODTSubstitutionLine hook
446  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray,'line'=>$line);
447  $reshook=$hookmanager->executeHooks('ODTSubstitutionLine',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
448  foreach($tmparray as $key => $val)
449  {
450  try
451  {
452  $listlines->setVars($key, $val, true, 'UTF-8');
453  }
454  catch(OdfException $e)
455  {
456  }
457  catch(SegmentException $e)
458  {
459  }
460  }
461  $listlines->merge();
462  }
463  $odfHandler->mergeSegment($listlines);
464  }
465  catch(OdfException $e)
466  {
467  $this->error=$e->getMessage();
468  dol_syslog($this->error, LOG_WARNING);
469  return -1;
470  }
471 
472  // Replace labels translated
473  $tmparray=$outputlangs->get_translations_for_substitutions();
474  foreach($tmparray as $key=>$value)
475  {
476  try {
477  $odfHandler->setVars($key, $value, true, 'UTF-8');
478  }
479  catch(OdfException $e)
480  {
481  }
482  }
483 
484  // Call the beforeODTSave hook
485  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
486  $reshook=$hookmanager->executeHooks('beforeODTSave',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
487 
488  // Write new file
489  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
490  try {
491  $odfHandler->exportAsAttachedPDF($file);
492  }catch (Exception $e){
493  $this->error=$e->getMessage();
494  return -1;
495  }
496  }
497  else {
498  try {
499  $odfHandler->saveToDisk($file);
500  }catch (Exception $e){
501  $this->error=$e->getMessage();
502  return -1;
503  }
504  }
505  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
506  $reshook=$hookmanager->executeHooks('afterODTCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
507 
508  if (! empty($conf->global->MAIN_UMASK))
509  @chmod($file, octdec($conf->global->MAIN_UMASK));
510 
511  $odfHandler=null; // Destroy object
512 
513  $this->result = array('fullpath'=>$file);
514 
515  return 1; // Success
516  }
517  else
518  {
519  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
520  return -1;
521  }
522  }
523 
524  return -1;
525  }
526 
527 }
528 
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_shipment($object, $outputlangs, $array_key='object')
Define array with couple substitution key => substitution value.
Parent class of sending receipts models.
</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
info($langs)
Return description of a module.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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...
if(empty($reshook)) $form
View.
Definition: perms.php:103
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
get_substitutionarray_shipment_lines($line, $outputlangs)
Define array with couple substitution key => substitution value.
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.
img_warning($titlealt= 'default', $moreatt= '')
Show warning logo.
Class to manage shipments.
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple subtitution key => subtitution value.
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
Class to build documents using ODF templates generator.
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)...
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...