dolibarr 22.0.5
doc_generic_odt.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2010-2011 Laurent Destailleur <ely@users.sourceforge.net>
3 * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
4 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
28require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
29require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/company.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{
43 public $version = 'dolibarr';
44
45
51 public function __construct($db)
52 {
53 global $langs, $mysoc;
54
55 // Load translation files required by the page
56 $langs->loadLangs(array("main", "companies"));
57
58 $this->db = $db;
59 $this->name = "ODT templates";
60 $this->description = $langs->trans("DocumentModelOdt");
61 $this->scandir = 'COMPANY_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
62
63 // Page size for A4 format
64 $this->type = 'odt';
65 $this->page_largeur = 0;
66 $this->page_hauteur = 0;
67 $this->format = array($this->page_largeur, $this->page_hauteur);
68 $this->marge_gauche = 0;
69 $this->marge_droite = 0;
70 $this->marge_haute = 0;
71 $this->marge_basse = 0;
72
73 $this->option_logo = 1; // Display logo
74 $this->option_freetext = 0; // Support add of a personalised text
75 $this->option_draft_watermark = 0; // Support add of a watermark on drafts
76
77 if ($mysoc === null) {
78 dol_syslog(get_class($this).'::__construct() Global $mysoc should not be null.'. getCallerInfoString(), LOG_ERR);
79 return;
80 }
81
82 // Retrieves issuer
83 $this->emetteur = $mysoc;
84 if (!$this->emetteur->country_code) {
85 $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default, if was not defined
86 }
87 }
88
89
96 public function info($langs)
97 {
98 global $langs;
99
100 // Load traductions files required by page
101 $langs->loadLangs(array("companies", "errors"));
102
103 $form = new Form($this->db);
104
105 $texte = $this->description.".<br>\n";
106 $texte .= '<!-- form for option of ODT templates -->';
107 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">';
108 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
109 $texte .= '<input type="hidden" name="page_y" value="">';
110 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
111 $texte .= '<input type="hidden" name="param1" value="COMPANY_ADDON_PDF_ODT_PATH">';
112 $texte .= '<table class="nobordernopadding centpercent">';
113
114 // List of directories area
115 $texte .= '<tr><td>';
116 $texttitle = $langs->trans("ListOfDirectories");
117 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString('COMPANY_ADDON_PDF_ODT_PATH'))));
118 $listoffiles = array();
119 foreach ($listofdir as $key => $tmpdir) {
120 $tmpdir = trim($tmpdir);
121 $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
122 if (!$tmpdir) {
123 unset($listofdir[$key]);
124 continue;
125 }
126 if (!is_dir($tmpdir)) {
127 $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), '');
128 } else {
129 $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.od(s|t)$', '', 'name', SORT_ASC, 0, 1); // Disable hook for the moment
130 if (count($tmpfiles)) {
131 $listoffiles = array_merge($listoffiles, $tmpfiles);
132 }
133 }
134 }
135 $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
136 $texthelp .= '<br><br><span class="opacitymedium">'.$langs->trans("ExampleOfDirectoriesForModelGen").'</span>';
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 if (!getDolGlobalString('MAIN_NO_MULTIDIR_FOR_ODT')) {
142 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name);
143 $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
144 //$texte .= '<table><tr><td>';
145 $texte .= '<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
146 $texte .= getDolGlobalString('COMPANY_ADDON_PDF_ODT_PATH');
147 $texte .= '</textarea>';
148 //$texte .= '</td>';
149 //$texte .= '<td class="center">&nbsp; ';
150 $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
151 $texte .= '<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans("Modify")).'">';
152 //$texte .= '</td>';
153 //$texte .= '</tr>';
154 //$texte .= '</table>';
155 $texte .= '</div></div>';
156 } else {
157 $texte .= '<br>';
158 $texte .= '<input type="hidden" name="value1" value="COMPANY_ADDON_PDF_ODT_PATH">';
159 }
160
161 // Scan directories
162 $nbofiles = count($listoffiles);
163 if (getDolGlobalString('COMPANY_ADDON_PDF_ODT_PATH')) {
164 $texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
165 //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
166 $texte .= $nbofiles;
167 //$texte.=$nbofiles?'</a>':'';
168 $texte .= '</b>';
169 }
170
171 if ($nbofiles) {
172 $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
173 // Show list of found files
174 foreach ($listoffiles as $file) {
175 $texte .= '- '.$file['name'].' &nbsp; <a class="reposition" href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=thirdparties/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a>';
176 $texte .= ' &nbsp; <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?modulepart=doctemplates&keyforuploaddir=COMPANY_ADDON_PDF_ODT_PATH&action=deletefile&token='.newToken().'&file='.urlencode(basename($file['name'])).'">'.img_picto('', 'delete').'</a>';
177 $texte .= '<br>';
178 }
179 $texte .= '</div>';
180 }
181 // Add input to upload a new template file.
182 $texte .= '<div>'.$langs->trans("UploadNewTemplate");
183 $maxfilesizearray = getMaxFileSizeArray();
184 $maxmin = $maxfilesizearray['maxmin'];
185 if ($maxmin > 0) {
186 $texte .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
187 }
188 $texte .= ' <input type="file" name="uploadfile">';
189 $texte .= '<input type="hidden" value="COMPANY_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
190 $texte .= '<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
191 $texte .= '</div>';
192 $texte .= '</td>';
193
194 $texte .= '</tr>';
195
196 $texte .= '</table>';
197 $texte .= '</form>';
198
199 return $texte;
200 }
201
202 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
214 public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 {
216 // phpcs:enable
217 global $user, $langs, $conf, $mysoc, $hookmanager;
218 global $action;
219
220 if (empty($srctemplatepath)) {
221 dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
222 return -1;
223 }
224
225 // Add odtgeneration hook
226 if (!is_object($hookmanager)) {
227 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
228 $hookmanager = new HookManager($this->db);
229 }
230 $hookmanager->initHooks(array('odtgeneration'));
231 global $action;
232
233 if (!is_object($outputlangs)) {
234 $outputlangs = $langs;
235 }
236 $sav_charset_output = $outputlangs->charset_output;
237 $outputlangs->charset_output = 'UTF-8';
238
239 // Load translation files required by the page
240 $outputlangs->loadLangs(array("main", "dict", "companies", "projects"));
241
242 if ($conf->societe->multidir_output[$object->entity]) {
243 $dir = $conf->societe->multidir_output[$object->entity];
244 $objectref = dol_sanitizeFileName((string) $object->id);
245 if (!preg_match('/specimen/i', $objectref)) {
246 $dir .= "/".$objectref;
247 }
248
249 if (!file_exists($dir)) {
250 if (dol_mkdir($dir) < 0) {
251 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
252 return -1;
253 }
254 }
255
256 if (file_exists($dir)) {
257 //print "srctemplatepath=".$srctemplatepath; // Src filename
258 $newfile = basename($srctemplatepath);
259 $newfiletmp = preg_replace('/\.od(s|t)/i', '', $newfile);
260 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
261 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
262 // Get extension (ods or odt)
263 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
264 if (getDolGlobalString('MAIN_DOC_USE_OBJECT_THIRDPARTY_NAME')) {
265 $newfiletmp = dol_sanitizeFileName(dol_string_nospecial($object->name)) . '-' . $newfiletmp;
266 $newfiletmp = preg_replace('/__+/', '_', $newfiletmp); // Replace repeated _ into one _ (to avoid string with substitution syntax)
267 }
268 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
269 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
270 if ($format == '1') {
271 $format = '%Y%m%d%H%M%S';
272 }
273 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
274 } else {
275 $filename = $newfiletmp . '.' . $newfileformat;
276 }
277 $file = $dir . '/' . $filename;
278 $object->builddoc_filename = $filename; // For triggers
279 $object->context['builddoc_filename'] = $filename; // For triggers
280 //print "newfileformat=".$newfileformat;
281 //print "newdir=".$dir;
282 //print "newfile=".$newfile;
283 //print "file=".$file;
284 //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
285 //exit;
286
287 dol_mkdir($conf->societe->multidir_temp[$object->entity]);
288 if (!is_writable($conf->societe->multidir_temp[$object->entity])) {
289 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->societe->multidir_temp[$object->entity]);
290 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
291 return -1;
292 }
293
294 // Open and load template
295 require_once ODTPHP_PATH.'odf.php';
296 try {
297 $odfHandler = new Odf(
298 $srctemplatepath,
299 array(
300 'PATH_TO_TMP' => $conf->societe->multidir_temp[$object->entity],
301 'ZIP_PROXY' => getDolGlobalString('MAIN_ODF_ZIP_PROXY', 'PclZipProxy'), // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
302 'DELIMITER_LEFT' => '{',
303 'DELIMITER_RIGHT' => '}'
304 )
305 );
306 } catch (Exception $e) {
307 $this->error = $e->getMessage();
308 dol_syslog($e->getMessage(), LOG_INFO);
309 return -1;
310 }
311 //print $odfHandler->__toString()."\n";
312
313 // Replace tags of lines for contacts
314 $contact_arrray = array();
315
316 $sql = "SELECT p.rowid";
317 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
318 $sql .= " WHERE p.fk_soc = ".((int) $object->id);
319
320 $result = $this->db->query($sql);
321 $num = $this->db->num_rows($result);
322
323 if ($num) {
324 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
325
326 $i = 0;
327 $contactstatic = new Contact($this->db);
328
329 while ($i < $num) {
330 $obj = $this->db->fetch_object($result);
331
332 $contact_arrray[$i] = $obj->rowid;
333 $i++;
334 }
335 }
336 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
337 $foundtagforlines = 1;
338 try {
339 $listlines = $odfHandler->setSegment('companycontacts');
340 } catch (OdfExceptionSegmentNotFound $e) {
341 // We may arrive here if tags for lines not present into template
342 $foundtagforlines = 0;
343 dol_syslog($e->getMessage(), LOG_INFO);
344 }
345 if ($foundtagforlines && $contactstatic !== null) {
346 foreach ($contact_arrray as $array_key => $contact_id) {
347 $res_contact = $contactstatic->fetch($contact_id);
348 if ((int) $res_contact > 0) {
349 $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact');
350 foreach ($tmparray as $key => $val) {
351 try {
352 $listlines->setVars($key, $val, true, 'UTF-8');
353 } catch (SegmentException $e) {
354 dol_syslog($e->getMessage(), LOG_INFO);
355 }
356 }
357 $listlines->merge();
358 } else {
359 $this->error = $contactstatic->error;
360 dol_syslog($this->error, LOG_WARNING);
361 }
362 }
363 try {
364 $odfHandler->mergeSegment($listlines);
365 } catch (OdfException $e) {
366 $this->error = $e->getMessage();
367 dol_syslog($this->error, LOG_WARNING);
368 //return -1;
369 }
370 }
371 }
372
373 // Make substitutions into odt
374 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
375 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
376 $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs);
377 $array_other = $this->get_substitutionarray_other($outputlangs);
378
379 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other);
380
381 complete_substitutions_array($tmparray, $outputlangs, $object);
382
383 // Call the ODTSubstitution hook
384 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
385 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
386
387 // retrieve the constant to apply a ratio for image size or set the ratio to 1
388 if (getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO')) {
389 $ratio = floatval(getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO'));
390 } else {
391 $ratio = 1;
392 }
393
394 // Replace variables into document
395 foreach ($tmparray as $key => $value) {
396 try {
397 if (preg_match('/logo$/', $key)) { // Image
398 if (file_exists($value)) {
399 $odfHandler->setImage($key, $value, $ratio);
400 } else {
401 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
402 }
403 } else { // Text
404 $odfHandler->setVars($key, $value, true, 'UTF-8');
405 }
406 } catch (OdfException $e) {
407 // setVars failed, probably because key not found
408 dol_syslog($e->getMessage(), LOG_INFO);
409 }
410 }
411
412 // Replace labels translated
413 $tmparray = $outputlangs->get_translations_for_substitutions();
414 foreach ($tmparray as $key => $value) {
415 try {
416 $odfHandler->setVars($key, $value, true, 'UTF-8');
417 } catch (OdfException $e) {
418 dol_syslog($e->getMessage(), LOG_INFO);
419 }
420 }
421
422 // Call the beforeODTSave hook
423 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
424 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
425
426 // Write new file
427 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
428 try {
429 $odfHandler->exportAsAttachedPDF($file);
430 } catch (Exception $e) {
431 $this->error = $e->getMessage();
432 dol_syslog($e->getMessage(), LOG_INFO);
433 return -1;
434 }
435 } else {
436 try {
437 $odfHandler->creator = $user->getFullName($outputlangs);
438 $odfHandler->title = $object->builddoc_filename;
439 $odfHandler->subject = $object->builddoc_filename;
440
441 if (getDolGlobalString('ODT_ADD_DOLIBARR_ID')) {
442 $odfHandler->userdefined['dol_id'] = $object->id;
443 $odfHandler->userdefined['dol_element'] = $object->element;
444 }
445
446 $odfHandler->saveToDisk($file);
447 } catch (Exception $e) {
448 $this->error = $e->getMessage();
449 dol_syslog($e->getMessage(), LOG_INFO);
450 return -1;
451 }
452 }
453 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
454 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
455
456 dolChmod($file);
457
458 $odfHandler = null; // Destroy object
459
460 $this->result = array('fullpath' => $file);
461
462 return 1; // Success
463 } else {
464 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
465 return -1;
466 }
467 }
468
469 $this->error = 'UnknownError';
470 return -1;
471 }
472}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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 contact/addresses.
Class to manage generation of HTML components Only common components must be here.
Class to manage hooks.
Parent class for third parties models of doc generators.
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.
__construct($db)
Constructor.
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_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
getCallerInfoString()
Get caller info as a string that can be appended to a log message.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
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...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a 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)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:158
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:161
getMaxFileSizeArray()
Return the max allowed for file upload.