dolibarr 21.0.0-alpha
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 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 $conf, $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" 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
232 if (!is_object($outputlangs)) {
233 $outputlangs = $langs;
234 }
235 $sav_charset_output = $outputlangs->charset_output;
236 $outputlangs->charset_output = 'UTF-8';
237
238 // Load translation files required by the page
239 $outputlangs->loadLangs(array("main", "dict", "companies", "projects"));
240
241 if ($conf->societe->multidir_output[$object->entity]) {
242 $dir = $conf->societe->multidir_output[$object->entity];
243 $objectref = dol_sanitizeFileName((string) $object->id);
244 if (!preg_match('/specimen/i', $objectref)) {
245 $dir .= "/".$objectref;
246 }
247
248 if (!file_exists($dir)) {
249 if (dol_mkdir($dir) < 0) {
250 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
251 return -1;
252 }
253 }
254
255 if (file_exists($dir)) {
256 //print "srctemplatepath=".$srctemplatepath; // Src filename
257 $newfile = basename($srctemplatepath);
258 $newfiletmp = preg_replace('/\.od(s|t)/i', '', $newfile);
259 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
260 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
261 // Get extension (ods or odt)
262 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
263 if (getDolGlobalString('MAIN_DOC_USE_OBJECT_THIRDPARTY_NAME')) {
264 $newfiletmp = dol_sanitizeFileName(dol_string_nospecial($object->name)) . '-' . $newfiletmp;
265 $newfiletmp = preg_replace('/__+/', '_', $newfiletmp); // Replace repeated _ into one _ (to avoid string with substitution syntax)
266 }
267 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
268 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
269 if ($format == '1') {
270 $format = '%Y%m%d%H%M%S';
271 }
272 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
273 } else {
274 $filename = $newfiletmp . '.' . $newfileformat;
275 }
276 $file = $dir . '/' . $filename;
277 $object->builddoc_filename = $filename; // For triggers
278 $object->context['builddoc_filename'] = $filename; // For triggers
279 //print "newfileformat=".$newfileformat;
280 //print "newdir=".$dir;
281 //print "newfile=".$newfile;
282 //print "file=".$file;
283 //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
284 //exit;
285
286 dol_mkdir($conf->societe->multidir_temp[$object->entity]);
287 if (!is_writable($conf->societe->multidir_temp[$object->entity])) {
288 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->societe->multidir_temp[$object->entity]);
289 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
290 return -1;
291 }
292
293 // Open and load template
294 require_once ODTPHP_PATH.'odf.php';
295 try {
296 $odfHandler = new Odf(
297 $srctemplatepath,
298 array(
299 'PATH_TO_TMP' => $conf->societe->multidir_temp[$object->entity],
300 'ZIP_PROXY' => getDolGlobalString('MAIN_ODF_ZIP_PROXY', 'PclZipProxy'), // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
301 'DELIMITER_LEFT' => '{',
302 'DELIMITER_RIGHT' => '}'
303 )
304 );
305 } catch (Exception $e) {
306 $this->error = $e->getMessage();
307 dol_syslog($e->getMessage(), LOG_INFO);
308 return -1;
309 }
310 //print $odfHandler->__toString()."\n";
311
312 // Replace tags of lines for contacts
313 $contact_arrray = array();
314
315 $sql = "SELECT p.rowid";
316 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
317 $sql .= " WHERE p.fk_soc = ".((int) $object->id);
318
319 $result = $this->db->query($sql);
320 $num = $this->db->num_rows($result);
321
322 if ($num) {
323 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
324
325 $i = 0;
326 $contactstatic = new Contact($this->db);
327
328 while ($i < $num) {
329 $obj = $this->db->fetch_object($result);
330
331 $contact_arrray[$i] = $obj->rowid;
332 $i++;
333 }
334 }
335 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
336 $foundtagforlines = 1;
337 try {
338 $listlines = $odfHandler->setSegment('companycontacts');
339 } catch (OdfExceptionSegmentNotFound $e) {
340 // We may arrive here if tags for lines not present into template
341 $foundtagforlines = 0;
342 dol_syslog($e->getMessage(), LOG_INFO);
343 }
344 if ($foundtagforlines) {
345 foreach ($contact_arrray as $array_key => $contact_id) {
346 $res_contact = $contactstatic->fetch($contact_id);
347 if ((int) $res_contact > 0) {
348 $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact');
349 foreach ($tmparray as $key => $val) {
350 try {
351 $listlines->setVars($key, $val, true, 'UTF-8');
352 } catch (SegmentException $e) {
353 dol_syslog($e->getMessage(), LOG_INFO);
354 }
355 }
356 $listlines->merge();
357 } else {
358 $this->error = $contactstatic->error;
359 dol_syslog($this->error, LOG_WARNING);
360 }
361 }
362 try {
363 $odfHandler->mergeSegment($listlines);
364 } catch (OdfException $e) {
365 $this->error = $e->getMessage();
366 dol_syslog($this->error, LOG_WARNING);
367 //return -1;
368 }
369 }
370 }
371
372 // Make substitutions into odt
373 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
374 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
375 $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs);
376 $array_other = $this->get_substitutionarray_other($outputlangs);
377
378 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other);
379
380 complete_substitutions_array($tmparray, $outputlangs, $object);
381
382 // Call the ODTSubstitution hook
383 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
384 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
385
386 // Replace variables into document
387 foreach ($tmparray as $key => $value) {
388 try {
389 if (preg_match('/logo$/', $key)) { // Image
390 if (file_exists($value)) {
391 $odfHandler->setImage($key, $value);
392 } else {
393 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
394 }
395 } else { // Text
396 $odfHandler->setVars($key, $value, true, 'UTF-8');
397 }
398 } catch (OdfException $e) {
399 // setVars failed, probably because key not found
400 dol_syslog($e->getMessage(), LOG_INFO);
401 }
402 }
403
404 // Replace labels translated
405 $tmparray = $outputlangs->get_translations_for_substitutions();
406 foreach ($tmparray as $key => $value) {
407 try {
408 $odfHandler->setVars($key, $value, true, 'UTF-8');
409 } catch (OdfException $e) {
410 dol_syslog($e->getMessage(), LOG_INFO);
411 }
412 }
413
414 // Call the beforeODTSave hook
415 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
416 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
417
418 // Write new file
419 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
420 try {
421 $odfHandler->exportAsAttachedPDF($file);
422 } catch (Exception $e) {
423 $this->error = $e->getMessage();
424 dol_syslog($e->getMessage(), LOG_INFO);
425 return -1;
426 }
427 } else {
428 try {
429 $odfHandler->creator = $user->getFullName($outputlangs);
430 $odfHandler->title = $object->builddoc_filename;
431 $odfHandler->subject = $object->builddoc_filename;
432
433 if (getDolGlobalString('ODT_ADD_DOLIBARR_ID')) {
434 $odfHandler->userdefined['dol_id'] = $object->id;
435 $odfHandler->userdefined['dol_element'] = $object->element;
436 }
437
438 $odfHandler->saveToDisk($file);
439 } catch (Exception $e) {
440 $this->error = $e->getMessage();
441 dol_syslog($e->getMessage(), LOG_INFO);
442 return -1;
443 }
444 }
445 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
446 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
447
448 dolChmod($file);
449
450 $odfHandler = null; // Destroy object
451
452 $this->result = array('fullpath' => $file);
453
454 return 1; // Success
455 } else {
456 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
457 return -1;
458 }
459 }
460
461 $this->error = 'UnknownError';
462 return -1;
463 }
464}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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_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)
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)
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)
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
getMaxFileSizeArray()
Return the max allowed for file upload.