dolibarr 18.0.6
doc_generic_usergroup_odt.modules.php
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) 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/usergroup/modules_usergroup.class.php';
28require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
29require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
34
35
40{
45 public $emetteur;
46
51 public $version = 'dolibarr';
52
53
59 public function __construct($db)
60 {
61 global $conf, $langs, $mysoc;
62
63 // Load translation files required by the page
64 $langs->loadLangs(array("main", "companies"));
65
66 $this->db = $db;
67 $this->name = "ODT templates";
68 $this->description = $langs->trans("DocumentModelOdt");
69 $this->scandir = 'USERGROUP_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
70
71 // Page size for A4 format
72 $this->type = 'odt';
73 $this->page_largeur = 0;
74 $this->page_hauteur = 0;
75 $this->format = array($this->page_largeur, $this->page_hauteur);
76 $this->marge_gauche = 0;
77 $this->marge_droite = 0;
78 $this->marge_haute = 0;
79 $this->marge_basse = 0;
80
81 $this->option_logo = 1; // Display logo
82 $this->option_tva = 0; // Manage the vat option USERGROUP_TVAOPTION
83 $this->option_modereg = 0; // Display payment mode
84 $this->option_condreg = 0; // Display payment terms
85 $this->option_multilang = 1; // Available in several languages
86 $this->option_escompte = 0; // Displays if there has been a discount
87 $this->option_credit_note = 0; // Support credit notes
88 $this->option_freetext = 1; // Support add of a personalised text
89 $this->option_draft_watermark = 0; // Support add of a watermark on drafts
90
91 // Get source company
92 $this->emetteur = $mysoc;
93 if (!$this->emetteur->country_code) {
94 $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
95 }
96 }
97
98
105 public function info($langs)
106 {
107 global $conf, $langs;
108
109 // Load translation files required by the page
110 $langs->loadLangs(array("errors", "companies"));
111
112 $form = new Form($this->db);
113
114 $odtChosen = getDolGlobalInt('MAIN_PROPAL_CHOOSE_ODT_DOCUMENT') > 0;
115 $odtPath = trim(getDolGlobalString('USERGROUP_ADDON_PDF_ODT_PATH'));
116
117 $texte = $this->description.".<br>\n";
118 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
119 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
120 $texte .= '<input type="hidden" name="page_y" value="">';
121 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
122 $texte .= '<input type="hidden" name="param1" value="USERGROUP_ADDON_PDF_ODT_PATH">';
123 if ($odtChosen) {
124 $texte .= '<input type="hidden" name="param2" value="USERGROUP_ADDON_PDF_ODT_DEFAULT">';
125 $texte .= '<input type="hidden" name="param3" value="USERGROUP_ADDON_PDF_ODT_TOBILL">';
126 $texte .= '<input type="hidden" name="param4" value="USERGROUP_ADDON_PDF_ODT_CLOSED">';
127 }
128 $texte .= '<table class="nobordernopadding" width="100%">';
129
130 // List of directories area
131 $texte .= '<tr><td>';
132 $texttitle = $langs->trans("ListOfDirectories");
133 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', $odtPath));
134 $listoffiles = array();
135 foreach ($listofdir as $key => $tmpdir) {
136 $tmpdir = trim($tmpdir);
137 $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
138 if (!$tmpdir) {
139 unset($listofdir[$key]);
140 continue;
141 }
142 if (!is_dir($tmpdir)) {
143 $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
144 } else {
145 $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
146 if (count($tmpfiles)) {
147 $listoffiles = array_merge($listoffiles, $tmpfiles);
148 }
149 }
150 }
151 $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
152 // Add list of substitution keys
153 $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
154 $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
155
156 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
157 $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
158 $texte .= '<textarea class="flat" cols="60" name="value1">';
159 $texte .= $odtPath;
160 $texte .= '</textarea>';
161 $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
162 $texte .= '<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans("Modify").'">';
163 $texte .= '<br></div></div>';
164
165 // Scan directories
166 if (count($listofdir)) {
167 $texte .= $langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
168
169 if ($odtChosen) {
170 // Model for creation
171 $list = ModelePDFUserGroup::liste_modeles($this->db);
172 $texte .= '<table width="50%;">';
173 $texte .= '<tr>';
174 $texte .= '<td width="60%;">'.$langs->trans("DefaultModelPropalCreate").'</td>';
175 $texte .= '<td colspan="">';
176 $texte .= $form->selectarray('value2', $list, getDolGlobalString('USERGROUP_ADDON_PDF_ODT_DEFAULT'));
177 $texte .= "</td></tr>";
178
179 $texte .= '<tr>';
180 $texte .= '<td width="60%;">'.$langs->trans("DefaultModelPropalToBill").'</td>';
181 $texte .= '<td colspan="">';
182 $texte .= $form->selectarray('value3', $list, getDolGlobalString('USERGROUP_ADDON_PDF_ODT_TOBILL'));
183 $texte .= "</td></tr>";
184 $texte .= '<tr>';
185
186 $texte .= '<td width="60%;">'.$langs->trans("DefaultModelPropalClosed").'</td>';
187 $texte .= '<td colspan="">';
188 $texte .= $form->selectarray('value4', $list, getDolGlobalString('USERGROUP_ADDON_PDF_ODT_CLOSED'));
189 $texte .= "</td></tr>";
190 $texte .= '</table>';
191 }
192 }
193
194 $texte .= '</td>';
195
196 $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
197 $texte .= '<span class="opacitymedium">';
198 $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
199 $texte .= '</span>';
200 $texte .= '</td>';
201 $texte .= '</tr>';
202
203 $texte .= '</table>';
204 $texte .= '</form>';
205
206 return $texte;
207 }
208
209 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
221 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
222 {
223 // phpcs:enable
224 global $user, $langs, $conf, $mysoc, $hookmanager;
225
226 if (empty($srctemplatepath)) {
227 dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
228 return -1;
229 }
230
231 // Add odtgeneration hook
232 if (!is_object($hookmanager)) {
233 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
234 $hookmanager = new HookManager($this->db);
235 }
236 $hookmanager->initHooks(array('odtgeneration'));
237 global $action;
238
239 if (!is_object($outputlangs)) {
240 $outputlangs = $langs;
241 }
242 $sav_charset_output = $outputlangs->charset_output;
243 $outputlangs->charset_output = 'UTF-8';
244
245 // Load translation files required by the page
246 $outputlangs->loadLangs(array("main", "companies", "bills", "dict"));
247
248 if ($conf->user->dir_output) {
249 // If $object is id instead of object
250 if (!is_object($object)) {
251 $id = $object;
252 $object = new UserGroup($this->db);
253 $result = $object->fetch($id);
254 if ($result < 0) {
255 dol_print_error($this->db, $object->error);
256 return -1;
257 }
258 }
259
260 $dir = $conf->user->dir_output.'/usergroups';
261 $objectref = dol_sanitizeFileName($object->ref);
262 if (!preg_match('/specimen/i', $objectref)) {
263 $dir .= "/".$objectref;
264 }
265 $file = $dir."/".$objectref.".odt";
266
267 if (!file_exists($dir)) {
268 if (dol_mkdir($dir) < 0) {
269 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
270 return -1;
271 }
272 }
273
274 if (file_exists($dir)) {
275 //print "srctemplatepath=".$srctemplatepath; // Src filename
276 $newfile = basename($srctemplatepath);
277 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
278 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
279 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
280
281 $newfiletmp = $objectref . '_' . $newfiletmp;
282
283 // Get extension (ods or odt)
284 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
285 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
286 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
287 if ($format == '1') {
288 $format = '%Y%m%d%H%M%S';
289 }
290 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
291 } else {
292 $filename = $newfiletmp . '.' . $newfileformat;
293 }
294 $file = $dir . '/' . $filename;
295 //print "newdir=".$dir;
296 //print "newfile=".$newfile;
297 //print "file=".$file;
298 //print "conf->user->dir_temp=".$conf->user->dir_temp;
299
300 dol_mkdir($conf->user->dir_temp);
301 if (!is_writable($conf->user->dir_temp)) {
302 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->user->dir_temp);
303 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
304 return -1;
305 }
306
307 // If CUSTOMER contact defined on user, we use it
308 $usecontact = false;
309 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
310 if (count($arrayidcontact) > 0) {
311 $usecontact = true;
312 $result = $object->fetch_contact($arrayidcontact[0]);
313 }
314
315 // Recipient name
316 if (!empty($usecontact)) {
317 // We can use the company of contact instead of thirdparty company
318 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))) {
319 $object->contact->fetch_thirdparty();
320 $socobject = $object->contact->thirdparty;
321 $contactobject = $object->contact;
322 } else {
323 $socobject = $object->thirdparty;
324 // if we have a CUSTOMER contact and we dont use it as thirdparty recipient we store the contact object for later use
325 $contactobject = $object->contact;
326 }
327 } else {
328 $socobject = $object->thirdparty;
329 }
330 // Make substitution
331 $substitutionarray = array(
332 '__FROM_NAME__' => $this->emetteur->name,
333 '__FROM_EMAIL__' => $this->emetteur->email,
334 '__TOTAL_TTC__' => $object->total_ttc,
335 '__TOTAL_HT__' => $object->total_ht,
336 '__TOTAL_VAT__' => $object->total_tva
337 );
338 complete_substitutions_array($substitutionarray, $langs, $object);
339 // Call the ODTSubstitution hook
340 $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray);
341 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
342
343 // Line of free text
344 $newfreetext = '';
345 $paramfreetext = 'user_FREE_TEXT';
346 if (!empty($conf->global->$paramfreetext)) {
347 $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
348 }
349
350 // Open and load template
351 require_once ODTPHP_PATH.'odf.php';
352 try {
353 $odfHandler = new Odf(
354 $srctemplatepath,
355 array(
356 'PATH_TO_TMP' => $conf->user->dir_temp,
357 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
358 'DELIMITER_LEFT' => '{',
359 'DELIMITER_RIGHT' => '}'
360 )
361 );
362 } catch (Exception $e) {
363 $this->error = $e->getMessage();
364 dol_syslog($e->getMessage(), LOG_WARNING);
365 return -1;
366 }
367 // After construction $odfHandler->contentXml contains content and
368 // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
369 // [!-- BEGIN lines --]*[!-- END lines --]
370 //print html_entity_decode($odfHandler->__toString());
371 //print exit;
372
373
374 // Make substitutions into odt of freetext
375 try {
376 $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
377 } catch (OdfException $e) {
378 dol_syslog($e->getMessage(), LOG_WARNING);
379 }
380
381 // Make substitutions into odt
382 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
383 $array_global = $this->get_substitutionarray_each_var_object($object, $outputlangs);
384 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
385 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
386 $array_objet = $this->get_substitutionarray_each_var_object($object, $outputlangs);
387 $array_other = $this->get_substitutionarray_other($outputlangs);
388 // retrieve contact information for use in object as contact_xxx tags
389 $array_thirdparty_contact = array();
390 if ($usecontact && is_object($contactobject)) {
391 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
392 }
393
394 $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
395 complete_substitutions_array($tmparray, $outputlangs, $object);
396 $object->fetch_optionals();
397 // Call the ODTSubstitution hook
398 $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
399 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
400 foreach ($tmparray as $key => $value) {
401 try {
402 if (preg_match('/logo$/', $key)) { // Image
403 if (file_exists($value)) {
404 $odfHandler->setImage($key, $value);
405 } else {
406 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
407 }
408 } else // Text
409 {
410 $odfHandler->setVars($key, $value, true, 'UTF-8');
411 }
412 } catch (OdfException $e) {
413 dol_syslog($e->getMessage(), LOG_WARNING);
414 }
415 }
416 // Replace tags of lines
417 try {
418 $foundtagforlines = 1;
419 try {
420 $listlines = $odfHandler->setSegment('lines');
421 } catch (OdfException $e) {
422 // We may arrive here if tags for lines not present into template
423 $foundtagforlines = 0;
424 dol_syslog($e->getMessage(), LOG_INFO);
425 }
426 if ($foundtagforlines) {
427 foreach ($object->members as $u) {
428 $tmparray = $this->get_substitutionarray_each_var_object($u, $outputlangs);
429 unset($tmparray['object_pass']);
430 unset($tmparray['object_pass_indatabase']);
431 complete_substitutions_array($tmparray, $outputlangs, $object, $user, "completesubstitutionarray_users");
432 // Call the ODTSubstitutionLine hook
433 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$u);
434 $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
435 foreach ($tmparray as $key => $val) {
436 try {
437 if (!is_array($val)) {
438 $listlines->setVars($key, $val, true, 'UTF-8');
439 }
440 } catch (OdfException $e) {
441 dol_syslog($e->getMessage(), LOG_WARNING);
442 } catch (SegmentException $e) {
443 dol_syslog($e->getMessage(), LOG_WARNING);
444 }
445 }
446 $listlines->merge();
447 }
448 $odfHandler->mergeSegment($listlines);
449 }
450 } catch (OdfException $e) {
451 $this->error = $e->getMessage();
452 dol_syslog($this->error, LOG_WARNING);
453 return -1;
454 }
455
456 // Replace labels translated
457 $tmparray = $outputlangs->get_translations_for_substitutions();
458 foreach ($tmparray as $key => $value) {
459 try {
460 $odfHandler->setVars($key, $value, true, 'UTF-8');
461 } catch (OdfException $e) {
462 dol_syslog($e->getMessage(), LOG_WARNING);
463 }
464 }
465
466 // Call the beforeODTSave hook
467 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
468 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
469
470 // Write new file
471 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
472 try {
473 $odfHandler->exportAsAttachedPDF($file);
474 } catch (Exception $e) {
475 $this->error = $e->getMessage();
476 dol_syslog($e->getMessage(), LOG_WARNING);
477 return -1;
478 }
479 } else {
480 try {
481 $odfHandler->saveToDisk($file);
482 } catch (Exception $e) {
483 $this->error = $e->getMessage();
484 dol_syslog($e->getMessage(), LOG_WARNING);
485 return -1;
486 }
487 }
488
489 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
490
491 dolChmod($file);
492
493 $odfHandler = null; // Destroy object
494
495 $this->result = array('fullpath'=>$file);
496
497 return 1; // Success
498 } else {
499 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
500 return -1;
501 }
502 }
503
504 return -1;
505 }
506}
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.
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
Class to manage user groups.
Class to build documents using ODF templates generator.
info($langs)
Return description of a module.
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt 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.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
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.
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