dolibarr 20.0.0
doc_generic_mo_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 * Copyright (C) 2018-2021 Philippe Grand <philippe.grand@atoo-net.com>
7 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/modules/mrp/modules_mo.php';
31require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
36
37
42{
46 public $version = 'dolibarr';
47
48
54 public function __construct($db)
55 {
56 global $conf, $langs, $mysoc;
57
58 // Load translation files required by the page
59 $langs->loadLangs(array("main", "companies"));
60
61 $this->db = $db;
62 $this->name = "ODT templates";
63 $this->description = $langs->trans("DocumentModelOdt");
64 $this->scandir = 'MRP_MO_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
65
66 // Page size for A4 format
67 $this->type = 'odt';
68 $this->page_largeur = 0;
69 $this->page_hauteur = 0;
70 $this->format = array($this->page_largeur, $this->page_hauteur);
71 $this->marge_gauche = 0;
72 $this->marge_droite = 0;
73 $this->marge_haute = 0;
74 $this->marge_basse = 0;
75
76 $this->option_logo = 1; // Display logo
77 $this->option_tva = 0; // Manage the vat option
78 $this->option_modereg = 0; // Display payment mode
79 $this->option_condreg = 0; // Display payment terms
80 $this->option_multilang = 1; // Available in several languages
81 $this->option_escompte = 0; // Displays if there has been a discount
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 // Get source company
87 $this->emetteur = $mysoc;
88 if (!$this->emetteur->country_code) {
89 $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
90 }
91 }
92
93
100 public function info($langs)
101 {
102 global $conf, $langs;
103
104 // Load translation files required by the page
105 $langs->loadLangs(array("errors", "companies"));
106
107 $form = new Form($this->db);
108
109 $texte = $this->description.".<br>\n";
110 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
111 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
112 $texte .= '<input type="hidden" name="page_y" value="">';
113 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
114 $texte .= '<input type="hidden" name="param1" value="MRP_MO_ADDON_PDF_ODT_PATH">';
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->MRP_MO_ADDON_PDF_ODT_PATH)));
121 $listoffiles = array();
122 foreach ($listofdir as $key => $tmpdir) {
123 $tmpdir = trim($tmpdir);
124 $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
125 if (!$tmpdir) {
126 unset($listofdir[$key]);
127 continue;
128 }
129 if (!is_dir($tmpdir)) {
130 $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
131 } else {
132 $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
133 if (count($tmpfiles)) {
134 $listoffiles = array_merge($listoffiles, $tmpfiles);
135 }
136 }
137 }
138 $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
139 $texthelp .= '<br><br><span class="opacitymedium">'.$langs->trans("ExampleOfDirectoriesForModelGen").'</span>';
140 // Add list of substitution keys
141 $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
142 $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
143
144 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name);
145 $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
146 $texte .= '<textarea class="flat" cols="60" name="value1">';
147 $texte .= getDolGlobalString('MRP_MO_ADDON_PDF_ODT_PATH');
148 $texte .= '</textarea>';
149 $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
150 $texte .= '<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans("Modify")).'">';
151 $texte .= '<br></div></div>';
152
153 // Scan directories
154 $nbofiles = count($listoffiles);
155 if (getDolGlobalString('MRP_MO_ADDON_PDF_ODT_PATH')) {
156 $texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
157 //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
158 $texte .= count($listoffiles);
159 //$texte.=$nbofiles?'</a>':'';
160 $texte .= '</b>';
161 }
162
163 if ($nbofiles) {
164 $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
165 // Show list of found files
166 foreach ($listoffiles as $file) {
167 $texte .= '- '.$file['name'].' <a href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=mrps/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a>';
168 $texte .= ' &nbsp; <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?modulepart=doctemplates&keyforuploaddir=MRP_MO_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
174 $texte .= '</td>';
175
176 $texte .= '</tr>';
177
178 $texte .= '</table>';
179 $texte .= '</form>';
180
181 return $texte;
182 }
183
184 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
196 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
197 {
198 // phpcs:enable
199 global $user, $langs, $conf, $mysoc, $hookmanager;
200
201 if (empty($srctemplatepath)) {
202 dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
203 return -1;
204 }
205
206 // Add odtgeneration hook
207 if (!is_object($hookmanager)) {
208 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
209 $hookmanager = new HookManager($this->db);
210 }
211 $hookmanager->initHooks(array('odtgeneration'));
212 global $action;
213
214 if (!is_object($outputlangs)) {
215 $outputlangs = $langs;
216 }
217 $sav_charset_output = $outputlangs->charset_output;
218 $outputlangs->charset_output = 'UTF-8';
219
220 $outputlangs->loadLangs(array("main", "dict", "companies", "bills"));
221
222 if ($conf->mrp->dir_output) {
223 // If $object is id instead of object
224 if (!is_object($object)) {
225 $id = $object;
226 $object = new Mo($this->db);
227 $result = $object->fetch($id);
228 if ($result < 0) {
229 dol_print_error($this->db, $object->error);
230 return -1;
231 }
232 }
233
234 $object->fetch_thirdparty();
235
236 $dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1];
237 $objectref = dol_sanitizeFileName($object->ref);
238 if (!preg_match('/specimen/i', $objectref)) {
239 $dir .= "/".$objectref;
240 }
241 $file = $dir."/".$objectref.".odt";
242
243 if (!file_exists($dir)) {
244 if (dol_mkdir($dir) < 0) {
245 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
246 return -1;
247 }
248 }
249
250 if (file_exists($dir)) {
251 //print "srctemplatepath=".$srctemplatepath; // Src filename
252 $newfile = basename($srctemplatepath);
253 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
254 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
255 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
256 $newfiletmp = $objectref . '_' . $newfiletmp;
257 //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
258 // Get extension (ods or odt)
259 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
260 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
261 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
262 if ($format == '1') {
263 $format = '%Y%m%d%H%M%S';
264 }
265 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
266 } else {
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->mrp->dir_temp);
276 if (!is_writable($conf->mrp->dir_temp)) {
277 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->mrp->dir_temp);
278 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
279 return -1;
280 }
281
282 // If CUSTOMER contact defined on order, we use it
283 $usecontact = false;
284 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
285 if (count($arrayidcontact) > 0) {
286 $usecontact = true;
287 $result = $object->fetch_contact($arrayidcontact[0]);
288 }
289
290 // Recipient name
291 $contactobject = null;
292 if (!empty($usecontact)) {
293 // We can use the company of contact instead of thirdparty company
294 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || getDolGlobalString('MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
295 $object->contact->fetch_thirdparty();
296 $socobject = $object->contact->thirdparty;
297 $contactobject = $object->contact;
298 } else {
299 $socobject = $object->thirdparty;
300 // if we have a CUSTOMER contact and we don't use it as thirdparty recipient we store the contact object for later use
301 $contactobject = $object->contact;
302 }
303 } else {
304 $socobject = $object->thirdparty;
305 }
306
307 // Make substitution
308 $substitutionarray = array(
309 '__QTY_TO_PRODUCE__' => $object->qty,
310 );
311 complete_substitutions_array($substitutionarray, $langs, $object);
312 // Call the ODTSubstitution hook
313 $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray);
314 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
315
316 // Line of free text
317 $newfreetext = '';
318 $paramfreetext = 'MRP_MO_FREE_TEXT';
319 if (!empty($conf->global->$paramfreetext)) {
320 $newfreetext = make_substitutions(getDolGlobalString($paramfreetext), $substitutionarray);
321 }
322
323 // Open and load template
324 require_once ODTPHP_PATH.'odf.php';
325 try {
326 $odfHandler = new Odf(
327 $srctemplatepath,
328 array(
329 'PATH_TO_TMP' => $conf->mrp->dir_temp,
330 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
331 'DELIMITER_LEFT' => '{',
332 'DELIMITER_RIGHT' => '}'
333 )
334 );
335 } catch (Exception $e) {
336 $this->error = $e->getMessage();
337 dol_syslog($e->getMessage(), LOG_INFO);
338 return -1;
339 }
340
341 // After construction $odfHandler->contentXml contains content and
342 // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
343 // [!-- BEGIN lines --]*[!-- END lines --]
344 //print html_entity_decode($odfHandler->__toString());
345 //print exit;
346
347 // Make substitutions into odt of freetext
348 try {
349 $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
350 } catch (OdfException $e) {
351 dol_syslog($e->getMessage(), LOG_INFO);
352 }
353
354 // Define substitution array
355 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
356 $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs);
357 $array_objet = $this->get_substitutionarray_object($object, $outputlangs);
358 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
359 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
360 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
361 $array_other = $this->get_substitutionarray_other($outputlangs);
362 // retrieve contact information for use in object as contact_xxx tags
363 $array_thirdparty_contact = array();
364 if ($usecontact && is_object($contactobject)) {
365 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
366 }
367
368 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
369 complete_substitutions_array($tmparray, $outputlangs, $object);
370
371 // Call the ODTSubstitution hook
372 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
373 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
374
375 foreach ($tmparray as $key => $value) {
376 try {
377 if (preg_match('/logo$/', $key)) {
378 // Image
379 if (file_exists($value)) {
380 $odfHandler->setImage($key, $value);
381 } else {
382 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
383 }
384 } else {
385 // Text
386 $odfHandler->setVars($key, $value, true, 'UTF-8');
387 }
388 } catch (OdfException $e) {
389 dol_syslog($e->getMessage(), LOG_INFO);
390 }
391 }
392
393 // Replace tags of lines
394 $foundtagforlines = 1;
395 try {
396 $listlines = $odfHandler->setSegment('lines');
397 } catch (OdfExceptionSegmentNotFound $e) {
398 // We may arrive here if tags for lines not present into template
399 $foundtagforlines = 0;
400 dol_syslog($e->getMessage(), LOG_INFO);
401 }
402 if ($foundtagforlines) {
403 $linenumber = 0;
404 foreach ($object->lines as $line) {
405 $linenumber++;
406 $tmparray = $this->get_substitutionarray_lines($line, $outputlangs, $linenumber);
407 complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
408 // Call the ODTSubstitutionLine hook
409 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$line);
410 $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
411 foreach ($tmparray as $key => $val) {
412 try {
413 $listlines->setVars($key, $val, true, 'UTF-8');
414 } catch (SegmentException $e) {
415 dol_syslog($e->getMessage(), LOG_INFO);
416 }
417 }
418 $listlines->merge();
419 }
420 try {
421 $odfHandler->mergeSegment($listlines);
422 } catch (OdfException $e) {
423 $this->error = $e->getMessage();
424 dol_syslog($this->error, LOG_WARNING);
425 return -1;
426 }
427 }
428
429 // Replace labels translated
430 $tmparray = $outputlangs->get_translations_for_substitutions();
431 foreach ($tmparray as $key => $value) {
432 try {
433 $odfHandler->setVars($key, $value, true, 'UTF-8');
434 } catch (OdfException $e) {
435 dol_syslog($e->getMessage(), LOG_INFO);
436 }
437 }
438
439 // Call the beforeODTSave hook
440
441 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
442 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
443
444 // Write new file
445 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
446 try {
447 $odfHandler->exportAsAttachedPDF($file);
448 } catch (Exception $e) {
449 $this->error = $e->getMessage();
450 dol_syslog('Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
451 return -1;
452 }
453 } else {
454 try {
455 $odfHandler->saveToDisk($file);
456 } catch (Exception $e) {
457 $this->error = $e->getMessage();
458 dol_syslog('Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
459 return -1;
460 }
461 }
462
463 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
464 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
465
466 dolChmod($file);
467
468 $odfHandler = null; // Destroy object
469
470 $this->result = array('fullpath'=>$file);
471
472 return 1; // Success
473 } else {
474 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
475 return -1;
476 }
477 }
478
479 return -1;
480 }
481}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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_lines($line, $outputlangs, $linenumber=0)
Define array with couple substitution key => substitution value Note that vars into substitutions arr...
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.
Class for Mo.
Definition mo.class.php:36
Parent class for mos models.
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($utf8_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:63
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
newToken()
Return the value of token currently saved into session with name 'newtoken'.
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.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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:137
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:140