dolibarr  7.0.0-beta
doc_generic_product_odt.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@products.sourceforge.net>
3  * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * or see http://www.gnu.org/
18 */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/product/class/product.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 {
39  var $emetteur; // Objet societe qui emet
40 
41  var $phpmin = array(5,2,0); // Minimum version of PHP required by module
42  var $version = 'dolibarr';
43 
44 
50  function __construct($db)
51  {
52  global $conf,$langs,$mysoc;
53 
54  $langs->load("main");
55  $langs->load("companies");
56 
57  $this->db = $db;
58  $this->name = "ODT templates";
59  $this->description = $langs->trans("DocumentModelOdt");
60  $this->scandir = 'PRODUCT_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
61 
62  // Dimension page pour format A4
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; // Affiche logo
73  $this->option_tva = 0; // Gere option tva PRODUCT_TVAOPTION
74  $this->option_modereg = 0; // Affiche mode reglement
75  $this->option_condreg = 0; // Affiche conditions reglement
76  $this->option_codeproduitservice = 0; // Affiche code produit-service
77  $this->option_multilang = 1; // Dispo en plusieurs langues
78  $this->option_escompte = 0; // Affiche si il y a eu escompte
79  $this->option_credit_note = 0; // Support credit notes
80  $this->option_freetext = 1; // Support add of a personalised text
81  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
82 
83  // Recupere emetteur
84  $this->emetteur=$mysoc;
85  if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // By default if not defined
86  }
87 
88 
95  function info($langs)
96  {
97  global $conf,$langs;
98 
99  $langs->load("companies");
100  $langs->load("errors");
101 
102  $form = new Form($this->db);
103 
104  $texte = $this->description.".<br>\n";
105  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
106  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
107  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
108  $texte.= '<input type="hidden" name="param1" value="PRODUCT_ADDON_PDF_ODT_PATH">';
109  if ($conf->global->MAIN_PROPAL_CHOOSE_ODT_DOCUMENT > 0)
110  {
111  $texte.= '<input type="hidden" name="param2" value="PRODUCT_ADDON_PDF_ODT_DEFAULT">';
112  $texte.= '<input type="hidden" name="param3" value="PRODUCT_ADDON_PDF_ODT_TOBILL">';
113  $texte.= '<input type="hidden" name="param4" value="PRODUCT_ADDON_PDF_ODT_CLOSED">';
114  }
115  $texte.= '<table class="nobordernopadding" width="100%">';
116 
117  // List of directories area
118  $texte.= '<tr><td>';
119  $texttitle=$langs->trans("ListOfDirectories");
120  $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->PRODUCT_ADDON_PDF_ODT_PATH)));
121  $listoffiles=array();
122  foreach($listofdir as $key=>$tmpdir)
123  {
124  $tmpdir=trim($tmpdir);
125  $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
126  if (! $tmpdir) {
127  unset($listofdir[$key]); continue;
128  }
129  if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
130  else
131  {
132  $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.(ods|odt)');
133  if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
134  }
135  }
136  $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
137  // Add list of substitution keys
138  $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
139  $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
140 
141  $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
142  $texte.= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
143  $texte.= '<textarea class="flat" cols="60" name="value1">';
144  $texte.=$conf->global->PRODUCT_ADDON_PDF_ODT_PATH;
145  $texte.= '</textarea>';
146  $texte.= '</div><div style="display: inline-block; vertical-align: middle;">';
147  $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
148  $texte.= '<br></div></div>';
149 
150  // Scan directories
151  if (count($listofdir))
152  {
153  $texte.=$langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
154 
155  /*if ($conf->global->MAIN_PRODUCT_CHOOSE_ODT_DOCUMENT > 0)
156  {
157  // Model for creation
158  $liste=ModelePDFProduct::liste_modeles($this->db);
159  $texte.= '<table width="50%;">';
160  $texte.= '<tr>';
161  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalCreate").'</td>';
162  $texte.= '<td colspan="">';
163  $texte.= $form->selectarray('value2',$liste,$conf->global->PRODUCT_ADDON_PDF_ODT_DEFAULT);
164  $texte.= "</td></tr>";
165 
166  $texte.= '<tr>';
167  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalToBill").'</td>';
168  $texte.= '<td colspan="">';
169  $texte.= $form->selectarray('value3',$liste,$conf->global->PRODUCT_ADDON_PDF_ODT_TOBILL);
170  $texte.= "</td></tr>";
171  $texte.= '<tr>';
172 
173  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalClosed").'</td>';
174  $texte.= '<td colspan="">';
175  $texte.= $form->selectarray('value4',$liste,$conf->global->PRODUCT_ADDON_PDF_ODT_CLOSED);
176  $texte.= "</td></tr>";
177  $texte.= '</table>';
178  }*/
179  }
180 
181  $texte.= '</td>';
182 
183  $texte.= '<td valign="top" rowspan="2" class="hideonsmartphone">';
184  $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
185  $texte.= '</td>';
186  $texte.= '</tr>';
187 
188  $texte.= '</table>';
189  $texte.= '</form>';
190 
191  return $texte;
192  }
193 
205  function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidedesc=0,$hideref=0)
206  {
207  global $product,$langs,$conf,$mysoc,$hookmanager,$user;
208 
209  if (empty($srctemplatepath))
210  {
211  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
212  return -1;
213  }
214 
215  // Add odtgeneration hook
216  if (! is_object($hookmanager))
217  {
218  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
219  $hookmanager=new HookManager($this->db);
220  }
221  $hookmanager->initHooks(array('odtgeneration'));
222  global $action;
223 
224  if (! is_object($outputlangs)) $outputlangs=$langs;
225  $sav_charset_output=$outputlangs->charset_output;
226  $outputlangs->charset_output='UTF-8';
227 
228  $outputlangs->load("main");
229  $outputlangs->load("dict");
230  $outputlangs->load("companies");
231  $outputlangs->load("bills");
232  if ($conf->produit->dir_output)
233  {
234  // If $object is id instead of object
235  if (! is_object($object))
236  {
237  $id = $object;
238  $object = new Product($this->db);
239  $result=$object->fetch($id);
240  if ($result < 0)
241  {
242  dol_print_error($this->db,$object->error);
243  return -1;
244  }
245  }
246  $productFournisseur = new ProductFournisseur($this->db);
247  $supplierprices = $productFournisseur->list_product_fournisseur_price($object->id);
248  $object->supplierprices = $supplierprices;
249 
250  $dir = $conf->produit->dir_output;
251  $objectref = dol_sanitizeFileName($object->ref);
252  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
253  $file = $dir . "/" . $objectref . ".odt";
254 
255  if (! file_exists($dir))
256  {
257  if (dol_mkdir($dir) < 0)
258  {
259  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
260  return -1;
261  }
262  }
263 
264  if (file_exists($dir))
265  {
266  //print "srctemplatepath=".$srctemplatepath; // Src filename
267  $newfile=basename($srctemplatepath);
268  $newfiletmp=preg_replace('/\.od(t|s)/i','',$newfile);
269  $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
270  $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
271 
272  $newfiletmp=$objectref.'_'.$newfiletmp;
273 
274  // Get extension (ods or odt)
275  $newfileformat=substr($newfile, strrpos($newfile, '.')+1);
276  if ( ! empty($conf->global->MAIN_DOC_USE_TIMING))
277  {
278  $format=$conf->global->MAIN_DOC_USE_TIMING;
279  if ($format == '1') $format='%Y%m%d%H%M%S';
280  $filename=$newfiletmp.'-'.dol_print_date(dol_now(),$format).'.'.$newfileformat;
281  }
282  else
283  {
284  $filename=$newfiletmp.'.'.$newfileformat;
285  }
286  $file=$dir.'/'.$filename;
287  //print "newdir=".$dir;
288  //print "newfile=".$newfile;
289  //print "file=".$file;
290  //print "conf->produit->dir_temp=".$conf->produit->dir_temp;
291 
292  dol_mkdir($conf->produit->dir_temp);
293 
294 
295  // If CUSTOMER contact defined on product, we use it
296  $usecontact=false;
297  $arrayidcontact=$object->getIdContact('external','CUSTOMER');
298  if (count($arrayidcontact) > 0)
299  {
300  $usecontact=true;
301  $result=$object->fetch_contact($arrayidcontact[0]);
302  }
303 
304  // Recipient name
305  if (! empty($usecontact))
306  {
307  // On peut utiliser le nom de la societe du contact
308  if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)) $socobject = $object->contact;
309  else {
310  $socobject = $object->thirdparty;
311  // if we have a CUSTOMER contact and we dont use it as recipient we store the contact object for later use
312  $contactobject = $object->contact;
313  }
314  }
315  else
316  {
317  $socobject=$object->thirdparty;
318  }
319  // Make substitution
320  $substitutionarray=array(
321  '__FROM_NAME__' => $this->emetteur->name,
322  '__FROM_EMAIL__' => $this->emetteur->email,
323  '__TOTAL_TTC__' => $object->total_ttc,
324  '__TOTAL_HT__' => $object->total_ht,
325  '__TOTAL_VAT__' => $object->total_vat
326  );
327  complete_substitutions_array($substitutionarray, $langs, $object);
328  // Call the ODTSubstitution hook
329  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$substitutionarray);
330  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
331 
332  // Line of free text
333  $newfreetext='';
334  $paramfreetext='product_FREE_TEXT';
335  if (! empty($conf->global->$paramfreetext))
336  {
337  $newfreetext=make_substitutions($conf->global->$paramfreetext,$substitutionarray);
338  }
339 
340  // Open and load template
341  require_once ODTPHP_PATH.'odf.php';
342  try {
343  $odfHandler = new odf(
344  $srctemplatepath,
345  array(
346  'PATH_TO_TMP' => $conf->produit->dir_temp,
347  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
348  'DELIMITER_LEFT' => '{',
349  'DELIMITER_RIGHT' => '}'
350  )
351  );
352  }
353  catch(Exception $e)
354  {
355  $this->error=$e->getMessage();
356  return -1;
357  }
358  // After construction $odfHandler->contentXml contains content and
359  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
360  // [!-- BEGIN lines --]*[!-- END lines --]
361  //print html_entity_decode($odfHandler->__toString());
362  //print exit;
363 
364 
365  // Make substitutions into odt of freetext
366  try {
367  $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
368  }
369  catch(OdfException $e)
370  {
371  }
372 
373  // Make substitutions into odt
374  $array_global = $this->get_substitutionarray_each_var_object($object, $outputlangs);
375  $array_user=$this->get_substitutionarray_user($user,$outputlangs);
376  $array_soc=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
377  $array_thirdparty=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
378  //$array_objet=$this->get_substitutionarray_object($object,$outputlangs);
379  $array_other=$this->get_substitutionarray_other($outputlangs);
380  // retrieve contact information for use in product as contact_xxx tags
381  $array_thirdparty_contact = array();
382  if ($usecontact)
383  $array_thirdparty_contact=$this->get_substitutionarray_contact($contactobject,$outputlangs,'contact');
384 
385  $tmparray = array_merge($array_global,$array_user,$array_soc,$array_thirdparty,$array_other,$array_thirdparty_contact);
386  complete_substitutions_array($tmparray, $outputlangs, $object);
387  $object->fetch_optionals();
388  // Call the ODTSubstitution hook
389  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
390  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
391  foreach($tmparray as $key=>$value)
392  {
393  try {
394  if (preg_match('/logo$/',$key)) // Image
395  {
396  if (file_exists($value)) $odfHandler->setImage($key, $value);
397  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
398  }
399  else // Text
400  {
401  $odfHandler->setVars($key, $value, true, 'UTF-8');
402  }
403  }
404  catch(OdfException $e)
405  {
406  }
407  }
408  // Replace tags of lines
409  try
410  {
411  $listlines = $odfHandler->setSegment('supplierprices');
412  if(!empty($object->supplierprices)){
413  foreach ($object->supplierprices as $supplierprice)
414  {
415  $array_lines = $this->get_substitutionarray_each_var_object($supplierprice, $outputlangs);
416  complete_substitutions_array($array_lines, $outputlangs, $object, $supplierprice, "completesubstitutionarray_lines");
417  // Call the ODTSubstitutionLine hook
418  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$array_lines,'line'=>$supplierprice);
419  $reshook=$hookmanager->executeHooks('ODTSubstitutionLine',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
420  foreach($array_lines as $key => $val)
421  {
422  try
423  {
424  $listlines->setVars($key, $val, true, 'UTF-8');
425  }
426  catch(OdfException $e)
427  {
428  }
429  catch(SegmentException $e)
430  {
431  }
432  }
433  $listlines->merge();
434  }
435  }
436  $odfHandler->mergeSegment($listlines);
437  }
438  catch(OdfException $e)
439  {
440  $this->error=$e->getMessage();
441  dol_syslog($this->error, LOG_WARNING);
442  return -1;
443  }
444 
445  // Replace labels translated
446  $tmparray=$outputlangs->get_translations_for_substitutions();
447  foreach($tmparray as $key=>$value)
448  {
449  try {
450  $odfHandler->setVars($key, $value, true, 'UTF-8');
451  }
452  catch(OdfException $e)
453  {
454  }
455  }
456 
457  // Call the beforeODTSave hook
458  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
459  $reshook=$hookmanager->executeHooks('beforeODTSave',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
460 
461  // Write new file
462  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
463  try {
464  $odfHandler->exportAsAttachedPDF($file);
465  }catch (Exception $e){
466  $this->error=$e->getMessage();
467  return -1;
468  }
469  }
470  else {
471  try {
472  $odfHandler->saveToDisk($file);
473  }catch (Exception $e){
474  $this->error=$e->getMessage();
475  return -1;
476  }
477  }
478 
479  $reshook=$hookmanager->executeHooks('afterODTCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
480 
481  if (! empty($conf->global->MAIN_UMASK))
482  @chmod($file, octdec($conf->global->MAIN_UMASK));
483 
484  $odfHandler=null; // Destroy object
485 
486  $this->result = array('fullpath'=>$file);
487 
488  return 1; // Success
489  }
490  else
491  {
492  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
493  return -1;
494  }
495  }
496 
497  return -1;
498  }
499 
500 }
501 
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
Class to build documents using ODF templates generator.
</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.
Class to manage products or services.
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
info($langs)
Return description of a module.
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.
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)
Parent class to manage intervention document templates.
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
Class to manage predefined suppliers products.
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...