dolibarr 18.0.6
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 * 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 <https://www.gnu.org/licenses/>.
18* or see https://www.gnu.org/
19*/
20
27require_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
28require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
33
34
39{
44 public $emetteur;
45
50 public $version = 'dolibarr';
51
52
58 public function __construct($db)
59 {
60 global $conf, $langs, $mysoc;
61
62 // Load translation files required by the page
63 $langs->loadLangs(array("main", "companies"));
64
65 $this->db = $db;
66 $this->name = "ODT templates";
67 $this->description = $langs->trans("DocumentModelOdt");
68 $this->scandir = 'PRODUCT_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
69
70 // Page size for A4 format
71 $this->type = 'odt';
72 $this->page_largeur = 0;
73 $this->page_hauteur = 0;
74 $this->format = array($this->page_largeur, $this->page_hauteur);
75 $this->marge_gauche = 0;
76 $this->marge_droite = 0;
77 $this->marge_haute = 0;
78 $this->marge_basse = 0;
79
80 $this->option_logo = 1; // Display logo
81 $this->option_tva = 0; // Manage the vat option PRODUCT_TVAOPTION
82 $this->option_modereg = 0; // Display payment mode
83 $this->option_condreg = 0; // Display payment terms
84 $this->option_multilang = 1; // Available in several languages
85 $this->option_escompte = 0; // Displays if there has been a discount
86 $this->option_credit_note = 0; // Support credit notes
87 $this->option_freetext = 1; // Support add of a personalised text
88 $this->option_draft_watermark = 0; // Support add of a watermark on drafts
89
90 // Get source company
91 $this->emetteur = $mysoc;
92 if (!$this->emetteur->country_code) {
93 $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
94 }
95 }
96
97
104 public function info($langs)
105 {
106 global $conf, $langs;
107
108 // Load translation files required by the page
109 $langs->loadLangs(array("errors", "companies"));
110
111 $form = new Form($this->db);
112
113 $texte = $this->description.".<br>\n";
114 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">';
115 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
116 $texte .= '<input type="hidden" name="page_y" value="">';
117 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
118 $texte .= '<input type="hidden" name="param1" value="PRODUCT_ADDON_PDF_ODT_PATH">';
119 $texte .= '<table class="nobordernopadding" width="100%">';
120
121 // List of directories area
122 $texte .= '<tr><td>';
123 $texttitle = $langs->trans("ListOfDirectories");
124 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->PRODUCT_ADDON_PDF_ODT_PATH)));
125 $listoffiles = array();
126 foreach ($listofdir as $key => $tmpdir) {
127 $tmpdir = trim($tmpdir);
128 $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
129 if (!$tmpdir) {
130 unset($listofdir[$key]);
131 continue;
132 }
133 if (!is_dir($tmpdir)) {
134 $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
135 } else {
136 $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
137 if (count($tmpfiles)) {
138 $listoffiles = array_merge($listoffiles, $tmpfiles);
139 }
140 }
141 }
142 $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
143 // Add list of substitution keys
144 $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
145 $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
146
147 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
148 $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
149 $texte .= '<textarea class="flat" cols="60" name="value1">';
150 $texte .= getDolGlobalString('PRODUCT_ADDON_PDF_ODT_PATH');
151 $texte .= '</textarea>';
152 $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
153 $texte .= '<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans("Modify").'">';
154 $texte .= '<br></div></div>';
155
156 // Scan directories
157 $nbofiles = count($listoffiles);
158 if (!empty($conf->global->PRODUCT_ADDON_PDF_ODT_PATH)) {
159 $texte .= $langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
160 }
161
162 if ($nbofiles) {
163 $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
164 // Show list of found files
165 foreach ($listoffiles as $file) {
166 $texte .= '- '.$file['name'];
167 $texte .= ' <a href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=products/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a>';
168 $texte .= ' &nbsp; <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?modulepart=doctemplates&keyforuploaddir=PRODUCT_ADDON_PDF_ODT_PATH&action=deletefile&token='.newToken().'&file='.urlencode(basename($file['name'])).'">'.img_picto('', 'delete').'</a>';
169 $texte .= '<br>';
170 }
171 $texte .= '</div>';
172 }
173 // Add input to upload a new template file.
174 $texte .= '<div>'.$langs->trans("UploadNewTemplate");
175 $maxfilesizearray = getMaxFileSizeArray();
176 $maxmin = $maxfilesizearray['maxmin'];
177 if ($maxmin > 0) {
178 $texte .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
179 }
180 $texte .= ' <input type="file" name="uploadfile">';
181 $texte .= '<input type="hidden" value="PRODUCT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
182 $texte .= '<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
183 $texte .= '</div>';
184
185 $texte .= '</td>';
186
187 $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
188 $texte .= '<span class="opacitymedium">';
189 $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
190 $texte .= '</span>';
191 $texte .= '</td>';
192 $texte .= '</tr>';
193
194 $texte .= '</table>';
195 $texte .= '</form>';
196
197 return $texte;
198 }
199
200 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
212 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
213 {
214 // phpcs:enable
215 global $product, $langs, $conf, $mysoc, $hookmanager, $user;
216
217 if (empty($srctemplatepath)) {
218 dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
219 return -1;
220 }
221
222 // Add odtgeneration hook
223 if (!is_object($hookmanager)) {
224 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
225 $hookmanager = new HookManager($this->db);
226 }
227 $hookmanager->initHooks(array('odtgeneration'));
228 global $action;
229
230 if (!is_object($outputlangs)) {
231 $outputlangs = $langs;
232 }
233 $sav_charset_output = $outputlangs->charset_output;
234 $outputlangs->charset_output = 'UTF-8';
235
236 // Load translation files required by the page
237 $outputlangs->loadLangs(array("main", "dict", "companies", "bills"));
238
239 if ($conf->product->dir_output) {
240 // If $object is id instead of object
241 if (!is_object($object)) {
242 $id = $object;
243 $object = new Product($this->db);
244 $result = $object->fetch($id);
245 if ($result < 0) {
246 dol_print_error($this->db, $object->error);
247 return -1;
248 }
249 }
250 $productFournisseur = new ProductFournisseur($this->db);
251 $supplierprices = $productFournisseur->list_product_fournisseur_price($object->id);
252 $object->supplierprices = $supplierprices;
253
254 $dir = $conf->product->dir_output;
255 $objectref = dol_sanitizeFileName($object->ref);
256 if (!preg_match('/specimen/i', $objectref)) {
257 $dir .= "/".$objectref;
258 }
259 $file = $dir."/".$objectref.".odt";
260
261 if (!file_exists($dir)) {
262 if (dol_mkdir($dir) < 0) {
263 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
264 return -1;
265 }
266 }
267
268 if (file_exists($dir)) {
269 //print "srctemplatepath=".$srctemplatepath; // Src filename
270 $newfile = basename($srctemplatepath);
271 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
272 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
273 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
274
275 $newfiletmp = $objectref . '_' . $newfiletmp;
276
277 // Get extension (ods or odt)
278 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
279 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
280 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
281 if ($format == '1') {
282 $format = '%Y%m%d%H%M%S';
283 }
284 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
285 } else {
286 $filename = $newfiletmp . '.' . $newfileformat;
287 }
288 $file = $dir . '/' . $filename;
289 //print "newdir=".$dir;
290 //print "newfile=".$newfile;
291 //print "file=".$file;
292 //print "conf->product->dir_temp=".$conf->product->dir_temp;
293
294 dol_mkdir($conf->product->dir_temp);
295 if (!is_writable($conf->product->dir_temp)) {
296 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
297 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
298 return -1;
299 }
300
301 // If CUSTOMER contact defined on product, we use it
302 $usecontact = false;
303 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
304 if (count($arrayidcontact) > 0) {
305 $usecontact = true;
306 $result = $object->fetch_contact($arrayidcontact[0]);
307 }
308
309 // Recipient name
310 $contactobject = null;
311 if (!empty($usecontact)) {
312 // We can use the company of contact instead of thirdparty company
313 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
314 $object->contact->fetch_thirdparty();
315 $socobject = $object->contact->thirdparty;
316 $contactobject = $object->contact;
317 } else {
318 $socobject = $object->thirdparty;
319 // if we have a CUSTOMER contact and we dont use it as thirdparty recipient we store the contact object for later use
320 $contactobject = $object->contact;
321 }
322 } else {
323 $socobject = $object->thirdparty;
324 }
325 // Make substitution
326 $substitutionarray = array(
327 '__FROM_NAME__' => $this->emetteur->name,
328 '__FROM_EMAIL__' => $this->emetteur->email,
329 '__TOTAL_TTC__' => $object->total_ttc,
330 '__TOTAL_HT__' => $object->total_ht,
331 '__TOTAL_VAT__' => $object->total_tva
332 );
333 complete_substitutions_array($substitutionarray, $langs, $object);
334 // Call the ODTSubstitution hook
335 $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray);
336 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
337
338 // Line of free text
339 $newfreetext = '';
340 $paramfreetext = 'PRODUCT_FREE_TEXT';
341 if (!empty($conf->global->$paramfreetext)) {
342 $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
343 }
344
345 // Open and load template
346 require_once ODTPHP_PATH.'odf.php';
347 try {
348 $odfHandler = new Odf(
349 $srctemplatepath,
350 array(
351 'PATH_TO_TMP' => $conf->product->dir_temp,
352 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
353 'DELIMITER_LEFT' => '{',
354 'DELIMITER_RIGHT' => '}'
355 )
356 );
357 } catch (Exception $e) {
358 $this->error = $e->getMessage();
359 dol_syslog($e->getMessage(), LOG_INFO);
360 return -1;
361 }
362 // After construction $odfHandler->contentXml contains content and
363 // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
364 // [!-- BEGIN lines --]*[!-- END lines --]
365 //print html_entity_decode($odfHandler->__toString());
366 //print exit;
367
368
369 // Make substitutions into odt of freetext
370 try {
371 $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
372 } catch (OdfException $e) {
373 dol_syslog($e->getMessage(), LOG_INFO);
374 }
375
376 // Define substitution array
377 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
378 $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs);
379 //$array_objet=$this->get_substitutionarray_object($object,$outputlangs);
380 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
381 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
382 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
383 $array_other = $this->get_substitutionarray_other($outputlangs);
384 // retrieve contact information for use in object as contact_xxx tags
385 $array_thirdparty_contact = array();
386 if ($usecontact && is_object($contactobject)) {
387 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
388 }
389
390 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
391 complete_substitutions_array($tmparray, $outputlangs, $object);
392
393 // Call the ODTSubstitution hook
394 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
395 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
396
397 foreach ($tmparray as $key => $value) {
398 try {
399 if (preg_match('/logo$/', $key)) { // Image
400 if (file_exists($value)) {
401 $odfHandler->setImage($key, $value);
402 } else {
403 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
404 }
405 } else // Text
406 {
407 $odfHandler->setVars($key, $value, true, 'UTF-8');
408 }
409 } catch (OdfException $e) {
410 dol_syslog($e->getMessage(), LOG_INFO);
411 }
412 }
413 // Replace tags of lines
414 try {
415 $listlines = $odfHandler->setSegment('supplierprices');
416 if (!empty($object->supplierprices)) {
417 foreach ($object->supplierprices as $supplierprice) {
418 $array_lines = $this->get_substitutionarray_each_var_object($supplierprice, $outputlangs);
419 complete_substitutions_array($array_lines, $outputlangs, $object, $supplierprice, "completesubstitutionarray_lines");
420 // Call the ODTSubstitutionLine hook
421 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$array_lines, 'line'=>$supplierprice);
422 $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
423 foreach ($array_lines as $key => $val) {
424 try {
425 $listlines->setVars($key, $val, true, 'UTF-8');
426 } catch (OdfException $e) {
427 dol_syslog($e->getMessage(), LOG_INFO);
428 } catch (SegmentException $e) {
429 dol_syslog($e->getMessage(), LOG_INFO);
430 }
431 }
432 $listlines->merge();
433 }
434 }
435 $odfHandler->mergeSegment($listlines);
436 } catch (OdfException $e) {
437 $this->error = $e->getMessage();
438 dol_syslog($this->error, LOG_WARNING);
439 return -1;
440 }
441
442 // Replace labels translated
443 $tmparray = $outputlangs->get_translations_for_substitutions();
444 foreach ($tmparray as $key => $value) {
445 try {
446 $odfHandler->setVars($key, $value, true, 'UTF-8');
447 } catch (OdfException $e) {
448 dol_syslog($e->getMessage(), LOG_INFO);
449 }
450 }
451
452 // Call the beforeODTSave hook
453 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
454 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
455
456 // Write new file
457 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
458 try {
459 $odfHandler->exportAsAttachedPDF($file);
460 } catch (Exception $e) {
461 $this->error = $e->getMessage();
462 dol_syslog($e->getMessage(), LOG_INFO);
463 return -1;
464 }
465 } else {
466 try {
467 $odfHandler->saveToDisk($file);
468 } catch (Exception $e) {
469 $this->error = $e->getMessage();
470 dol_syslog($e->getMessage(), LOG_INFO);
471 return -1;
472 }
473 }
474
475 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
476 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
477
478 dolChmod($file);
479
480 $odfHandler = null; // Destroy object
481
482 $this->result = array('fullpath'=>$file);
483
484 return 1; // Success
485 } else {
486 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
487 return -1;
488 }
489 }
490
491 return -1;
492 }
493}
get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive=1)
Define array with couple substitution key => substitution value.
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple substitution key => substitution value.
get_substitutionarray_contact($object, $outputlangs, $array_key='object')
Define array with couple substitution key => substitution value.
get_substitutionarray_other($outputlangs)
Define array with couple substitution key => substitution value.
get_substitutionarray_thirdparty($object, $outputlangs, $array_key='company')
Define array with couple substitution key => substitution value For example {company_name}...
get_substitutionarray_user($user, $outputlangs)
Define array with couple substitution key => substitution value.
Class to manage generation of HTML components Only common components must be here.
Class to manage hooks.
Parent class to manage intervention document templates.
Class to manage predefined suppliers products.
Class to manage products or services.
Class to build documents using ODF templates generator.
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
info($langs)
Return description of a module.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:62
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:120
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:123
getMaxFileSizeArray()
Return the max allowed for file upload.