dolibarr  7.0.0-beta
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 *
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/user/modules_user.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
31 
32 
37 {
38  var $emetteur; // Objet societe qui emet
39 
40  var $phpmin = array(5,2,0); // Minimum version of PHP required by module
41  var $version = 'dolibarr';
42 
43 
49  function __construct($db)
50  {
51  global $conf,$langs,$mysoc;
52 
53  $langs->load("main");
54  $langs->load("companies");
55 
56  $this->db = $db;
57  $this->name = "ODT templates";
58  $this->description = $langs->trans("DocumentModelOdt");
59  $this->scandir = 'USER_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
60 
61  // Dimension page pour format A4
62  $this->type = 'odt';
63  $this->page_largeur = 0;
64  $this->page_hauteur = 0;
65  $this->format = array($this->page_largeur,$this->page_hauteur);
66  $this->marge_gauche=0;
67  $this->marge_droite=0;
68  $this->marge_haute=0;
69  $this->marge_basse=0;
70 
71  $this->option_logo = 1; // Affiche logo
72  $this->option_tva = 0; // Gere option tva USER_TVAOPTION
73  $this->option_modereg = 0; // Affiche mode reglement
74  $this->option_condreg = 0; // Affiche conditions reglement
75  $this->option_codeproduitservice = 0; // Affiche code produit-service
76  $this->option_multilang = 1; // Dispo en plusieurs langues
77  $this->option_escompte = 0; // Affiche si il y a eu escompte
78  $this->option_credit_note = 0; // Support credit notes
79  $this->option_freetext = 1; // Support add of a personalised text
80  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
81 
82  // Recupere emetteur
83  $this->emetteur=$mysoc;
84  if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // By default if not defined
85  }
86 
87 
94  function info($langs)
95  {
96  global $conf,$langs;
97 
98  $langs->load("companies");
99  $langs->load("errors");
100 
101  $form = new Form($this->db);
102 
103  $texte = $this->description.".<br>\n";
104  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
105  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
106  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
107  $texte.= '<input type="hidden" name="param1" value="USER_ADDON_PDF_ODT_PATH">';
108  if ($conf->global->MAIN_PROPAL_CHOOSE_ODT_DOCUMENT > 0)
109  {
110  $texte.= '<input type="hidden" name="param2" value="USER_ADDON_PDF_ODT_DEFAULT">';
111  $texte.= '<input type="hidden" name="param3" value="USER_ADDON_PDF_ODT_TOBILL">';
112  $texte.= '<input type="hidden" name="param4" value="USER_ADDON_PDF_ODT_CLOSED">';
113  }
114  $texte.= '<table class="nobordernopadding" width="100%">';
115 
116  // List of directories area
117  $texte.= '<tr><td>';
118  $texttitle=$langs->trans("ListOfDirectories");
119  $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->USER_ADDON_PDF_ODT_PATH)));
120  $listoffiles=array();
121  foreach($listofdir as $key=>$tmpdir)
122  {
123  $tmpdir=trim($tmpdir);
124  $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
125  if (! $tmpdir) {
126  unset($listofdir[$key]); continue;
127  }
128  if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
129  else
130  {
131  $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.(ods|odt)');
132  if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
133  }
134  }
135  $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
136  // Add list of substitution keys
137  $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
138  $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
139 
140  $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
141  $texte.= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
142  $texte.= '<textarea class="flat" cols="60" name="value1">';
143  $texte.=$conf->global->USER_ADDON_PDF_ODT_PATH;
144  $texte.= '</textarea>';
145  $texte.= '</div><div style="display: inline-block; vertical-align: middle;">';
146  $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
147  $texte.= '<br></div></div>';
148 
149  // Scan directories
150  if (count($listofdir))
151  {
152  $texte.=$langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
153 
154  if ($conf->global->MAIN_PROPAL_CHOOSE_ODT_DOCUMENT > 0)
155  {
156  // Model for creation
157  $liste=ModelePDFUser::liste_modeles($this->db);
158  $texte.= '<table width="50%;">';
159  $texte.= '<tr>';
160  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalCreate").'</td>';
161  $texte.= '<td colspan="">';
162  $texte.= $form->selectarray('value2',$liste,$conf->global->USER_ADDON_PDF_ODT_DEFAULT);
163  $texte.= "</td></tr>";
164 
165  $texte.= '<tr>';
166  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalToBill").'</td>';
167  $texte.= '<td colspan="">';
168  $texte.= $form->selectarray('value3',$liste,$conf->global->USER_ADDON_PDF_ODT_TOBILL);
169  $texte.= "</td></tr>";
170  $texte.= '<tr>';
171 
172  $texte.= '<td width="60%;">'.$langs->trans("DefaultModelPropalClosed").'</td>';
173  $texte.= '<td colspan="">';
174  $texte.= $form->selectarray('value4',$liste,$conf->global->USER_ADDON_PDF_ODT_CLOSED);
175  $texte.= "</td></tr>";
176  $texte.= '</table>';
177  }
178  }
179 
180  $texte.= '</td>';
181 
182  $texte.= '<td valign="top" rowspan="2" class="hideonsmartphone">';
183  $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
184  $texte.= '</td>';
185  $texte.= '</tr>';
186 
187  $texte.= '</table>';
188  $texte.= '</form>';
189 
190  return $texte;
191  }
192 
204  function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidedesc=0,$hideref=0)
205  {
206  global $user,$langs,$conf,$mysoc,$hookmanager;
207 
208  if (empty($srctemplatepath))
209  {
210  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
211  return -1;
212  }
213 
214  // Add odtgeneration hook
215  if (! is_object($hookmanager))
216  {
217  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
218  $hookmanager=new HookManager($this->db);
219  }
220  $hookmanager->initHooks(array('odtgeneration'));
221  global $action;
222 
223  if (! is_object($outputlangs)) $outputlangs=$langs;
224  $sav_charset_output=$outputlangs->charset_output;
225  $outputlangs->charset_output='UTF-8';
226 
227  $outputlangs->load("main");
228  $outputlangs->load("dict");
229  $outputlangs->load("companies");
230  $outputlangs->load("bills");
231 
232  if ($conf->user->dir_output)
233  {
234  // If $object is id instead of object
235  if (! is_object($object))
236  {
237  $id = $object;
238  $object = new User($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 
247  $dir = $conf->user->dir_output;
248  $objectref = dol_sanitizeFileName($object->ref);
249  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
250  $file = $dir . "/" . $objectref . ".odt";
251 
252  if (! file_exists($dir))
253  {
254  if (dol_mkdir($dir) < 0)
255  {
256  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
257  return -1;
258  }
259  }
260 
261  if (file_exists($dir))
262  {
263  //print "srctemplatepath=".$srctemplatepath; // Src filename
264  $newfile=basename($srctemplatepath);
265  $newfiletmp=preg_replace('/\.od(t|s)/i','',$newfile);
266  $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
267  $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
268 
269  $newfiletmp=$objectref.'_'.$newfiletmp;
270 
271  // Get extension (ods or odt)
272  $newfileformat=substr($newfile, strrpos($newfile, '.')+1);
273  if ( ! empty($conf->global->MAIN_DOC_USE_TIMING))
274  {
275  $format=$conf->global->MAIN_DOC_USE_TIMING;
276  if ($format == '1') $format='%Y%m%d%H%M%S';
277  $filename=$newfiletmp.'-'.dol_print_date(dol_now(),$format).'.'.$newfileformat;
278  }
279  else
280  {
281  $filename=$newfiletmp.'.'.$newfileformat;
282  }
283  $file=$dir.'/'.$filename;
284  //print "newdir=".$dir;
285  //print "newfile=".$newfile;
286  //print "file=".$file;
287  //print "conf->user->dir_temp=".$conf->user->dir_temp;
288 
289  dol_mkdir($conf->user->dir_temp);
290 
291 
292  // If CUSTOMER contact defined on user, we use it
293  $usecontact=false;
294  $arrayidcontact=$object->getIdContact('external','CUSTOMER');
295  if (count($arrayidcontact) > 0)
296  {
297  $usecontact=true;
298  $result=$object->fetch_contact($arrayidcontact[0]);
299  }
300 
301  // Recipient name
302  if (! empty($usecontact))
303  {
304  // On peut utiliser le nom de la societe du contact
305  if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)) $socobject = $object->contact;
306  else {
307  $socobject = $object->thirdparty;
308  // if we have a CUSTOMER contact and we dont use it as recipient we store the contact object for later use
309  $contactobject = $object->contact;
310  }
311  }
312  else
313  {
314  $socobject=$object->thirdparty;
315  }
316 
317  // Open and load template
318  require_once ODTPHP_PATH.'odf.php';
319  try {
320  $odfHandler = new odf(
321  $srctemplatepath,
322  array(
323  'PATH_TO_TMP' => $conf->user->dir_temp,
324  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
325  'DELIMITER_LEFT' => '{',
326  'DELIMITER_RIGHT' => '}'
327  )
328  );
329  }
330  catch(Exception $e)
331  {
332  $this->error=$e->getMessage();
333  return -1;
334  }
335 
336  // Make substitutions into odt
337  $array_user=$this->get_substitutionarray_user($object,$outputlangs);
338  $array_soc=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
339  $array_thirdparty=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
340  $array_other=$this->get_substitutionarray_other($outputlangs);
341  // retrieve contact information for use in user as contact_xxx tags
342  $array_thirdparty_contact = array();
343  if ($usecontact)
344  $array_thirdparty_contact=$this->get_substitutionarray_contact($contactobject,$outputlangs,'contact');
345 
346  $tmparray = array_merge($array_user,$array_soc,$array_thirdparty,$array_other,$array_thirdparty_contact);
347  complete_substitutions_array($tmparray, $outputlangs, $object);
348  $object->fetch_optionals();
349  // Call the ODTSubstitution hook
350  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
351  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
352  foreach($tmparray as $key=>$value)
353  {
354  try {
355  if (preg_match('/logo$/',$key)) // Image
356  {
357  if (file_exists($value)) $odfHandler->setImage($key, $value);
358  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
359  }
360  else // Text
361  {
362  $odfHandler->setVars($key, $value, true, 'UTF-8');
363  }
364  }
365  catch(OdfException $e)
366  {
367  }
368  }
369 
370  // Replace labels translated
371  $tmparray=$outputlangs->get_translations_for_substitutions();
372  foreach($tmparray as $key=>$value)
373  {
374  try {
375  $odfHandler->setVars($key, $value, true, 'UTF-8');
376  }
377  catch(OdfException $e)
378  {
379  }
380  }
381 
382  // Call the beforeODTSave hook
383  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
384  $reshook=$hookmanager->executeHooks('beforeODTSave',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
385 
386  // Write new file
387  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
388  try {
389  $odfHandler->exportAsAttachedPDF($file);
390  }catch (Exception $e){
391  $this->error=$e->getMessage();
392  return -1;
393  }
394  }
395  else {
396  try {
397  $odfHandler->saveToDisk($file);
398  }catch (Exception $e){
399  $this->error=$e->getMessage();
400  return -1;
401  }
402  }
403 
404  $reshook=$hookmanager->executeHooks('afterODTCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
405 
406  if (! empty($conf->global->MAIN_UMASK))
407  @chmod($file, octdec($conf->global->MAIN_UMASK));
408 
409  $odfHandler=null; // Destroy object
410 
411  $this->result = array('fullpath'=>$file);
412 
413  return 1; // Success
414  }
415  else
416  {
417  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
418  return -1;
419  }
420  }
421 
422  return -1;
423  }
424 
425  function get_substitutionarray_object($object,$outputlangs,$array_key='object') {
426  $array_other=array();
427  foreach($object as $key => $value) {
428  if (!is_array($value) && !is_object($value)) {
429  $array_other[$array_key.'_'.$key] = $value;
430  }
431  }
432  return $array_other;
433  }
434 
435 }
436 
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.
</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.
Class to manage Dolibarr users.
Definition: user.class.php:39
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...
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
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).
info($langs)
Return description of a module.
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)
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...