dolibarr 18.0.6
doc_generic_ticket_odt.modules.php
Go to the documentation of this file.
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-2020 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/ticket/modules_ticket.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.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 $emetteur;
43
48 public $version = 'dolibarr';
49
50
56 public function __construct($db)
57 {
58 global $conf, $langs, $mysoc;
59
60 // Load translation files required by the page
61 $langs->loadLangs(array("main", "companies"));
62
63 $this->db = $db;
64 $this->name = "ODT templates";
65 $this->description = $langs->trans("DocumentModelOdt");
66 $this->scandir = 'TICKET_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
67
68 // Page size for A4 format
69 $this->type = 'odt';
70 $this->page_largeur = 0;
71 $this->page_hauteur = 0;
72 $this->format = array($this->page_largeur, $this->page_hauteur);
73 $this->marge_gauche = 0;
74 $this->marge_droite = 0;
75 $this->marge_haute = 0;
76 $this->marge_basse = 0;
77
78 $this->option_logo = 1; // Display logo
79 $this->option_tva = 0; // Manage the vat option USER_TVAOPTION
80 $this->option_multilang = 1; // Available in several languages
81 $this->option_freetext = 0; // Support add of a personalised text
82 $this->option_draft_watermark = 0; // Support add of a watermark on drafts
83
84 // Get source company
85 $this->emetteur = $mysoc;
86 if (!$this->emetteur->country_code) {
87 $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
88 }
89 }
90
91
98 public function info($langs)
99 {
100 global $conf, $langs;
101
102 // Load translation files required by the page
103 $langs->loadLangs(array('companies', 'errors'));
104
105 $form = new Form($this->db);
106
107 $texte = $this->description.".<br>\n";
108 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">';
109 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
110 $texte .= '<input type="hidden" name="page_y" value="">';
111 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
112 $texte .= '<input type="hidden" name="param1" value="TICKET_ADDON_PDF_ODT_PATH">';
113
114 $texte .= '<table class="nobordernopadding" width="100%">';
115
116 // List of directories area
117 $texte .= '<tr><td>';
118 $texttitle = $langs->trans("ListOfDirectories");
119 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString('TICKET_ADDON_PDF_ODT_PATH'))));
120 $listoffiles = array();
121 foreach ($listofdir as $key => $tmpdir) {
122 $tmpdir = trim($tmpdir);
123 $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
124 if (!$tmpdir) {
125 unset($listofdir[$key]);
126 continue;
127 }
128 if (!is_dir($tmpdir)) {
129 $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
130 } else {
131 $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
132 if (count($tmpfiles)) {
133 $listoffiles = array_merge($listoffiles, $tmpfiles);
134 }
135 }
136 }
137 $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
138 // Add list of substitution keys
139 $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
140 $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
141
142 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
143 $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
144 $texte .= '<textarea class="flat" cols="60" name="value1">';
145 $texte .= getDolGlobalString('TICKET_ADDON_PDF_ODT_PATH');
146 $texte .= '</textarea>';
147 $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
148 $texte .= '<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans("Modify").'">';
149 $texte .= '<br></div></div>';
150
151 // Scan directories
152 if (count($listofdir)) {
153 $texte .= $langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
154
155 $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
156 // Show list of found files
157 foreach ($listoffiles as $file) {
158 $texte .= '- '.$file['name'].' <a href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=tickets/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a>';
159 $texte .= ' &nbsp; <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?modulepart=doctemplates&keyforuploaddir=TICKET_ADDON_PDF_ODT_PATH&action=deletefile&token='.newToken().'&file='.urlencode(basename($file['name'])).'">'.img_picto('', 'delete').'</a>';
160 $texte .= '<br>';
161 }
162 $texte .= '</div>';
163 }
164 // Add input to upload a new template file.
165 $texte .= '<div>'.$langs->trans("UploadNewTemplate");
166 $texte .= ' <input type="file" name="uploadfile">';
167 $maxfilesizearray = getMaxFileSizeArray();
168 $maxmin = $maxfilesizearray['maxmin'];
169 if ($maxmin > 0) {
170 $texte .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
171 }
172 $texte .= '<input type="hidden" value="TICKET_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
173 $texte .= '<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
174 $texte .= '</div>';
175
176 $texte .= '</td>';
177
178 $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
179 $texte .= '<span class="opacitymedium">';
180 $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
181 $texte .= '</span>';
182 $texte .= '</td>';
183 $texte .= '</tr>';
184
185 $texte .= '</table>';
186 $texte .= '</form>';
187
188 return $texte;
189 }
190
191 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
203 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
204 {
205 // phpcs:enable
206 global $user, $langs, $conf, $mysoc, $hookmanager;
207
208 if (empty($srctemplatepath)) {
209 dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
210 return -1;
211 }
212
213 // Add odtgeneration hook
214 if (!is_object($hookmanager)) {
215 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
216 $hookmanager = new HookManager($this->db);
217 }
218 $hookmanager->initHooks(array('odtgeneration'));
219 global $action;
220
221 if (!is_object($outputlangs)) {
222 $outputlangs = $langs;
223 }
224 $sav_charset_output = $outputlangs->charset_output;
225 $outputlangs->charset_output = 'UTF-8';
226
227 // Load translation files required by the page
228 $outputlangs->loadLangs(array("main", "companies", "bills", "dict"));
229
230 if ($conf->ticket->dir_output) {
231 // If $object is id instead of object
232 if (!is_object($object)) {
233 $id = $object;
234 $object = new User($this->db);
235 $result = $object->fetch($id);
236 if ($result < 0) {
237 dol_print_error($this->db, $object->error);
238 return -1;
239 }
240 }
241
242 $object->fetch_thirdparty();
243
244 $dir = $conf->ticket->dir_output;
245 $objectref = dol_sanitizeFileName($object->ref);
246 if (!preg_match('/specimen/i', $objectref)) {
247 $dir .= "/".$objectref;
248 }
249 $file = $dir."/".$objectref.".odt";
250
251 if (!file_exists($dir)) {
252 if (dol_mkdir($dir) < 0) {
253 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
254 return -1;
255 }
256 }
257
258 if (file_exists($dir)) {
259 //print "srctemplatepath=".$srctemplatepath; // Src filename
260 $newfile = basename($srctemplatepath);
261 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
262 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
263 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
264
265 $newfiletmp = $objectref . '_' . $newfiletmp;
266
267 // Get extension (ods or odt)
268 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
269 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
270 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
271 if ($format == '1') {
272 $format = '%Y%m%d%H%M%S';
273 }
274 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
275 } else {
276 $filename = $newfiletmp . '.' . $newfileformat;
277 }
278 $file = $dir . '/' . $filename;
279 //print "newdir=".$dir;
280 //print "newfile=".$newfile;
281 //print "file=".$file;
282 //print "conf->ticket->dir_temp=".$conf->ticket->dir_temp;
283
284 dol_mkdir($conf->ticket->dir_temp);
285 if (!is_writable($conf->ticket->dir_temp)) {
286 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->ticket->dir_temp);
287 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
288 return -1;
289 }
290
291 // If CUSTOMER contact defined on user, we use it
292 $usecontact = false;
293 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
294 if (count($arrayidcontact) > 0) {
295 $usecontact = true;
296 $result = $object->fetch_contact($arrayidcontact[0]);
297 }
298
299 // Recipient name
300 if (!empty($usecontact)) {
301 // We can use the company of contact instead of thirdparty company
302 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))) {
303 $object->contact->fetch_thirdparty();
304 $socobject = $object->contact->thirdparty;
305 $contactobject = $object->contact;
306 } else {
307 $socobject = $object->thirdparty;
308 // if we have a CUSTOMER contact and we dont use it as thirdparty recipient we store the contact object for later use
309 $contactobject = $object->contact;
310 }
311 } else {
312 $socobject = $object->thirdparty;
313 }
314
315 // Open and load template
316 require_once ODTPHP_PATH.'odf.php';
317 try {
318 $odfHandler = new Odf(
319 $srctemplatepath,
320 array(
321 'PATH_TO_TMP' => $conf->ticket->dir_temp,
322 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
323 'DELIMITER_LEFT' => '{',
324 'DELIMITER_RIGHT' => '}'
325 )
326 );
327 } catch (Exception $e) {
328 $this->error = $e->getMessage();
329 dol_syslog($e->getMessage(), LOG_WARNING);
330 return -1;
331 }
332
333 // Make substitutions into odt
334 $array_user = $this->get_substitutionarray_user($object, $outputlangs);
335 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
336 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
337 $array_other = $this->get_substitutionarray_other($outputlangs);
338 // retrieve contact information for use in object as contact_xxx tags
339 $array_thirdparty_contact = array();
340 if ($usecontact && is_object($contactobject)) {
341 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
342 }
343
344 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
345 complete_substitutions_array($tmparray, $outputlangs, $object);
346 $object->fetch_optionals();
347 // Call the ODTSubstitution hook
348 $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
349 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
350 foreach ($tmparray as $key => $value) {
351 try {
352 if (preg_match('/logo$/', $key)) { // Image
353 if (file_exists($value)) {
354 $odfHandler->setImage($key, $value);
355 } else {
356 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
357 }
358 } else // Text
359 {
360 $odfHandler->setVars($key, $value, true, 'UTF-8');
361 }
362 } catch (OdfException $e) {
363 dol_syslog($e->getMessage(), LOG_WARNING);
364 }
365 }
366
367 // Replace labels translated
368 $tmparray = $outputlangs->get_translations_for_substitutions();
369 foreach ($tmparray as $key => $value) {
370 try {
371 $odfHandler->setVars($key, $value, true, 'UTF-8');
372 } catch (OdfException $e) {
373 dol_syslog($e->getMessage(), LOG_WARNING);
374 }
375 }
376
377 // Call the beforeODTSave hook
378 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
379 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
380
381 // Write new file
382 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
383 try {
384 $odfHandler->exportAsAttachedPDF($file);
385 } catch (Exception $e) {
386 $this->error = $e->getMessage();
387 dol_syslog($e->getMessage(), LOG_WARNING);
388 return -1;
389 }
390 } else {
391 try {
392 $odfHandler->saveToDisk($file);
393 } catch (Exception $e) {
394 $this->error = $e->getMessage();
395 dol_syslog($e->getMessage(), LOG_WARNING);
396 return -1;
397 }
398 }
399
400 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
401
402 dolChmod($file);
403
404 $odfHandler = null; // Destroy object
405
406 $this->result = array('fullpath'=>$file);
407
408 return 1; // Success
409 } else {
410 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
411 return -1;
412 }
413 }
414
415 return -1;
416 }
417
418 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
427 public function get_substitutionarray_object($object, $outputlangs, $array_key = 'object')
428 {
429 // phpcs:enable
430 $array_other = array();
431 foreach ($object as $key => $value) {
432 if (!is_array($value) && !is_object($value)) {
433 $array_other[$array_key.'_'.$key] = $value;
434 }
435 }
436 return $array_other;
437 }
438}
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 for documents models.
Class to manage Dolibarr users.
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.
info($langs)
Return description of a module.
get_substitutionarray_object($object, $outputlangs, $array_key='object')
get substitution array for object
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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:120
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:123
getMaxFileSizeArray()
Return the max allowed for file upload.