dolibarr 24.0.0-beta
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-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2026 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 ?? $conf->entity]) {
243 $dir = $conf->societe->multidir_output[$object->entity ?? $conf->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 $contactstatic = null;
323
324 if ($num) {
325 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
326
327 $i = 0;
328 $contactstatic = new Contact($this->db);
329
330 while ($i < $num) {
331 $obj = $this->db->fetch_object($result);
332
333 $contact_arrray[$i] = $obj->rowid;
334 $i++;
335 }
336 }
337 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
338 $listlines = null;
339 try {
340 $listlines = $odfHandler->setSegment('companycontacts');
341 } catch (OdfExceptionSegmentNotFound $e) {
342 // We may arrive here if tags for lines not present into template
343 $listlines = null;
344 dol_syslog($e->getMessage(), LOG_INFO);
345 }
346 if ($listlines !== null && $contactstatic !== null) {
347 foreach ($contact_arrray as $array_key => $contact_id) {
348 $res_contact = $contactstatic->fetch($contact_id);
349 if ((int) $res_contact > 0) {
350 $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact');
351 foreach ($tmparray as $key => $val) {
352 try {
353 $listlines->setVars($key, $val, true, 'UTF-8');
354 } catch (SegmentException $e) {
355 dol_syslog($e->getMessage(), LOG_INFO);
356 }
357 }
358 $listlines->merge();
359 } else {
360 $this->error = $contactstatic->error;
361 dol_syslog($this->error, LOG_WARNING);
362 }
363 }
364 try {
365 $odfHandler->mergeSegment($listlines);
366 } catch (OdfException $e) {
367 $this->error = $e->getMessage();
368 dol_syslog($this->error, LOG_WARNING);
369 //return -1;
370 }
371 }
372 }
373
374 // Make substitutions into odt
375 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
376 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
377 $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs);
378 $array_other = $this->get_substitutionarray_other($outputlangs);
379
380 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other);
381
382 complete_substitutions_array($tmparray, $outputlangs, $object);
383
384 // Call the ODTSubstitution hook
385 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
386 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
387
388 // retrieve the constant to apply a ratio for image size or set the ratio to 1
389 if (getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO')) {
390 $ratio = (float) getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO');
391 } else {
392 $ratio = 1;
393 }
394
395 // Replace variables into document
396 foreach ($tmparray as $key => $value) {
397 try {
398 if (preg_match('/logo$/', $key)) { // Image
399 if (file_exists($value)) {
400 $odfHandler->setImage($key, $value, $ratio);
401 } else {
402 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
403 }
404 } else { // Text
405 $odfHandler->setVars($key, $value, true, 'UTF-8');
406 }
407 } catch (OdfException $e) {
408 // setVars failed, probably because key not found
409 dol_syslog($e->getMessage(), LOG_INFO);
410 }
411 }
412
413 // Replace labels translated
414 $tmparray = $outputlangs->get_translations_for_substitutions();
415 foreach ($tmparray as $key => $value) {
416 try {
417 $odfHandler->setVars($key, $value, true, 'UTF-8');
418 } catch (OdfException $e) {
419 dol_syslog($e->getMessage(), LOG_INFO);
420 }
421 }
422
423 // Call the beforeODTSave hook
424 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
425 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
426
427 // Write new file
428 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
429 try {
430 $odfHandler->exportAsAttachedPDF($file);
431 } catch (Exception $e) {
432 $this->error = $e->getMessage();
433 dol_syslog($e->getMessage(), LOG_INFO);
434 return -1;
435 }
436 } else {
437 try {
438 $odfHandler->creator = $user->getFullName($outputlangs);
439 $odfHandler->title = $object->builddoc_filename;
440 $odfHandler->subject = $object->builddoc_filename;
441
442 if (getDolGlobalString('ODT_ADD_DOLIBARR_ID')) {
443 $odfHandler->userdefined['dol_id'] = $object->id;
444 $odfHandler->userdefined['dol_element'] = $object->element;
445 }
446
447 $odfHandler->saveToDisk($file);
448 } catch (Exception $e) {
449 $this->error = $e->getMessage();
450 dol_syslog($e->getMessage(), LOG_INFO);
451 return -1;
452 }
453 }
454 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
455 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
456
457 dolChmod($file);
458
459 $odfHandler = null; // Destroy object
460
461 $this->result = array('fullpath' => $file);
462
463 return 1; // Success
464 } else {
465 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
466 return -1;
467 }
468 }
469
470 $this->error = 'UnknownError';
471 return -1;
472 }
473}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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.
global $mysoc
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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:64
dol_now($mode='gmt')
Return date for now.
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolChmod($filepath, $newmask='')
Change mod of a file.
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_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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)
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:130
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133
getMaxFileSizeArray()
Return the max allowed for file upload.