dolibarr 20.0.4
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 *
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/societe/modules_societe.class.php';
28require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
32
33
38{
42 public $version = 'dolibarr';
43
44
50 public function __construct($db)
51 {
52 global $langs, $mysoc;
53
54 // Load translation files required by the page
55 $langs->loadLangs(array("main", "companies"));
56
57 $this->db = $db;
58 $this->name = "ODT templates";
59 $this->description = $langs->trans("DocumentModelOdt");
60 $this->scandir = 'COMPANY_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
61
62 // Page size for A4 format
63 $this->type = 'odt';
64 $this->page_largeur = 0;
65 $this->page_hauteur = 0;
66 $this->format = array($this->page_largeur, $this->page_hauteur);
67 $this->marge_gauche = 0;
68 $this->marge_droite = 0;
69 $this->marge_haute = 0;
70 $this->marge_basse = 0;
71
72 $this->option_logo = 1; // Display logo
73 $this->option_freetext = 0; // Support add of a personalised text
74 $this->option_draft_watermark = 0; // Support add of a watermark on drafts
75
76 // Retrieves transmitter
77 $this->emetteur = $mysoc;
78 if (!$this->emetteur->country_code) {
79 $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default, if was not defined
80 }
81 }
82
83
90 public function info($langs)
91 {
92 global $conf, $langs;
93
94 // Load traductions files required by page
95 $langs->loadLangs(array("companies", "errors"));
96
97 $form = new Form($this->db);
98
99 $texte = $this->description.".<br>\n";
100 $texte .= '<!-- form for option of ODT templates -->';
101 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">';
102 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
103 $texte .= '<input type="hidden" name="page_y" value="">';
104 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
105 $texte .= '<input type="hidden" name="param1" value="COMPANY_ADDON_PDF_ODT_PATH">';
106 $texte .= '<table class="nobordernopadding centpercent">';
107
108 // List of directories area
109 $texte .= '<tr><td>';
110 $texttitle = $langs->trans("ListOfDirectories");
111 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString('COMPANY_ADDON_PDF_ODT_PATH'))));
112 $listoffiles = array();
113 foreach ($listofdir as $key => $tmpdir) {
114 $tmpdir = trim($tmpdir);
115 $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
116 if (!$tmpdir) {
117 unset($listofdir[$key]);
118 continue;
119 }
120 if (!is_dir($tmpdir)) {
121 $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
122 } else {
123 $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.od(s|t)$', '', 'name', SORT_ASC, 0, true); // Disable hook for the moment
124 if (count($tmpfiles)) {
125 $listoffiles = array_merge($listoffiles, $tmpfiles);
126 }
127 }
128 }
129 $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
130 $texthelp .= '<br><br><span class="opacitymedium">'.$langs->trans("ExampleOfDirectoriesForModelGen").'</span>';
131 // Add list of substitution keys
132 $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
133 $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
134
135 if (!getDolGlobalString('MAIN_NO_MULTIDIR_FOR_ODT')) {
136 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name);
137 $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
138 //$texte .= '<table><tr><td>';
139 $texte .= '<textarea class="flat" cols="60" name="value1">';
140 $texte .= getDolGlobalString('COMPANY_ADDON_PDF_ODT_PATH');
141 $texte .= '</textarea>';
142 //$texte .= '</td>';
143 //$texte .= '<td class="center">&nbsp; ';
144 $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
145 $texte .= '<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans("Modify")).'">';
146 //$texte .= '</td>';
147 //$texte .= '</tr>';
148 //$texte .= '</table>';
149 $texte .= '</div></div>';
150 } else {
151 $texte .= '<br>';
152 $texte .= '<input type="hidden" name="value1" value="COMPANY_ADDON_PDF_ODT_PATH">';
153 }
154
155 // Scan directories
156 $nbofiles = count($listoffiles);
157 if (getDolGlobalString('COMPANY_ADDON_PDF_ODT_PATH')) {
158 $texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
159 //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
160 $texte .= $nbofiles;
161 //$texte.=$nbofiles?'</a>':'';
162 $texte .= '</b>';
163 }
164
165 if ($nbofiles) {
166 $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
167 // Show list of found files
168 foreach ($listoffiles as $file) {
169 $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>';
170 $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>';
171 $texte .= '<br>';
172 }
173 $texte .= '</div>';
174 }
175 // Add input to upload a new template file.
176 $texte .= '<div>'.$langs->trans("UploadNewTemplate");
177 $maxfilesizearray = getMaxFileSizeArray();
178 $maxmin = $maxfilesizearray['maxmin'];
179 if ($maxmin > 0) {
180 $texte .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
181 }
182 $texte .= ' <input type="file" name="uploadfile">';
183 $texte .= '<input type="hidden" value="COMPANY_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
184 $texte .= '<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
185 $texte .= '</div>';
186 $texte .= '</td>';
187
188 $texte .= '</tr>';
189
190 $texte .= '</table>';
191 $texte .= '</form>';
192
193 return $texte;
194 }
195
196 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
208 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
209 {
210 // phpcs:enable
211 global $user, $langs, $conf, $mysoc, $hookmanager;
212 global $action;
213
214 if (empty($srctemplatepath)) {
215 dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
216 return -1;
217 }
218
219 // Add odtgeneration hook
220 if (!is_object($hookmanager)) {
221 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
222 $hookmanager = new HookManager($this->db);
223 }
224 $hookmanager->initHooks(array('odtgeneration'));
225 global $action;
226
227 if (!is_object($outputlangs)) {
228 $outputlangs = $langs;
229 }
230 $sav_charset_output = $outputlangs->charset_output;
231 $outputlangs->charset_output = 'UTF-8';
232
233 // Load translation files required by the page
234 $outputlangs->loadLangs(array("main", "dict", "companies", "projects"));
235
236 if ($conf->societe->multidir_output[$object->entity]) {
237 $dir = $conf->societe->multidir_output[$object->entity];
238 $objectref = dol_sanitizeFileName($object->id);
239 if (!preg_match('/specimen/i', $objectref)) {
240 $dir .= "/".$objectref;
241 }
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(s|t)/i', '', $newfile);
254 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
255 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
256 // Get extension (ods or odt)
257 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
258 if (getDolGlobalString('MAIN_DOC_USE_OBJECT_THIRDPARTY_NAME')) {
259 $newfiletmp = dol_sanitizeFileName(dol_string_nospecial($object->name)) . '-' . $newfiletmp;
260 $newfiletmp = preg_replace('/__+/', '_', $newfiletmp); // Replace repeated _ into one _ (to avoid string with substitution syntax)
261 }
262 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
263 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
264 if ($format == '1') {
265 $format = '%Y%m%d%H%M%S';
266 }
267 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
268 } else {
269 $filename = $newfiletmp . '.' . $newfileformat;
270 }
271 $file = $dir . '/' . $filename;
272 $object->builddoc_filename = $filename; // For triggers
273 $object->context['builddoc_filename'] = $filename; // For triggers
274 //print "newfileformat=".$newfileformat;
275 //print "newdir=".$dir;
276 //print "newfile=".$newfile;
277 //print "file=".$file;
278 //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
279 //exit;
280
281 dol_mkdir($conf->societe->multidir_temp[$object->entity]);
282 if (!is_writable($conf->societe->multidir_temp[$object->entity])) {
283 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->societe->multidir_temp[$object->entity]);
284 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
285 return -1;
286 }
287
288 // Open and load template
289 require_once ODTPHP_PATH.'odf.php';
290 try {
291 $odfHandler = new Odf(
292 $srctemplatepath,
293 array(
294 'PATH_TO_TMP' => $conf->societe->multidir_temp[$object->entity],
295 'ZIP_PROXY' => getDolGlobalString('MAIN_ODF_ZIP_PROXY', 'PclZipProxy'), // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
296 'DELIMITER_LEFT' => '{',
297 'DELIMITER_RIGHT' => '}'
298 )
299 );
300 } catch (Exception $e) {
301 $this->error = $e->getMessage();
302 dol_syslog($e->getMessage(), LOG_INFO);
303 return -1;
304 }
305 //print $odfHandler->__toString()."\n";
306
307 // Replace tags of lines for contacts
308 $contact_arrray = array();
309
310 $sql = "SELECT p.rowid";
311 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
312 $sql .= " WHERE p.fk_soc = ".((int) $object->id);
313
314 $result = $this->db->query($sql);
315 $num = $this->db->num_rows($result);
316
317 if ($num) {
318 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
319
320 $i = 0;
321 $contactstatic = new Contact($this->db);
322
323 while ($i < $num) {
324 $obj = $this->db->fetch_object($result);
325
326 $contact_arrray[$i] = $obj->rowid;
327 $i++;
328 }
329 }
330 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
331 $foundtagforlines = 1;
332 try {
333 $listlines = $odfHandler->setSegment('companycontacts');
334 } catch (OdfExceptionSegmentNotFound $e) {
335 // We may arrive here if tags for lines not present into template
336 $foundtagforlines = 0;
337 dol_syslog($e->getMessage(), LOG_INFO);
338 }
339 if ($foundtagforlines) {
340 foreach ($contact_arrray as $array_key => $contact_id) {
341 $res_contact = $contactstatic->fetch($contact_id);
342 if ((int) $res_contact > 0) {
343 $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact');
344 foreach ($tmparray as $key => $val) {
345 try {
346 $listlines->setVars($key, $val, true, 'UTF-8');
347 } catch (SegmentException $e) {
348 dol_syslog($e->getMessage(), LOG_INFO);
349 }
350 }
351 $listlines->merge();
352 } else {
353 $this->error = $contactstatic->error;
354 dol_syslog($this->error, LOG_WARNING);
355 }
356 }
357 try {
358 $odfHandler->mergeSegment($listlines);
359 } catch (OdfException $e) {
360 $this->error = $e->getMessage();
361 dol_syslog($this->error, LOG_WARNING);
362 //return -1;
363 }
364 }
365 }
366
367 // Make substitutions into odt
368 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
369 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
370 $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs);
371 $array_other = $this->get_substitutionarray_other($outputlangs);
372
373 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other);
374
375 complete_substitutions_array($tmparray, $outputlangs, $object);
376
377 // Call the ODTSubstitution hook
378 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
379 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
380
381 // Replace variables into document
382 foreach ($tmparray as $key => $value) {
383 try {
384 if (preg_match('/logo$/', $key)) { // Image
385 if (file_exists($value)) {
386 $odfHandler->setImage($key, $value);
387 } else {
388 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
389 }
390 } else { // Text
391 $odfHandler->setVars($key, $value, true, 'UTF-8');
392 }
393 } catch (OdfException $e) {
394 // setVars failed, probably because key not found
395 dol_syslog($e->getMessage(), LOG_INFO);
396 }
397 }
398
399 // Replace labels translated
400 $tmparray = $outputlangs->get_translations_for_substitutions();
401 foreach ($tmparray as $key => $value) {
402 try {
403 $odfHandler->setVars($key, $value, true, 'UTF-8');
404 } catch (OdfException $e) {
405 dol_syslog($e->getMessage(), LOG_INFO);
406 }
407 }
408
409 // Call the beforeODTSave hook
410 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
411 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
412
413 // Write new file
414 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
415 try {
416 $odfHandler->exportAsAttachedPDF($file);
417 } catch (Exception $e) {
418 $this->error = $e->getMessage();
419 dol_syslog($e->getMessage(), LOG_INFO);
420 return -1;
421 }
422 } else {
423 try {
424 $odfHandler->creator = $user->getFullName($outputlangs);
425 $odfHandler->title = $object->builddoc_filename;
426 $odfHandler->subject = $object->builddoc_filename;
427
428 if (getDolGlobalString('ODT_ADD_DOLIBARR_ID')) {
429 $odfHandler->userdefined['dol_id'] = $object->id;
430 $odfHandler->userdefined['dol_element'] = $object->element;
431 }
432
433 $odfHandler->saveToDisk($file);
434 } catch (Exception $e) {
435 $this->error = $e->getMessage();
436 dol_syslog($e->getMessage(), LOG_INFO);
437 return -1;
438 }
439 }
440 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
441 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
442
443 dolChmod($file);
444
445 $odfHandler = null; // Destroy object
446
447 $this->result = array('fullpath'=>$file);
448
449 return 1; // Success
450 } else {
451 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
452 return -1;
453 }
454 }
455
456 $this->error = 'UnknownError';
457 return -1;
458 }
459}
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.
__construct($db)
Constructor.
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)
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 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.