dolibarr 21.0.0-alpha
doc_generic_member_odt.class.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-2021 Frédéric France <frederic.france@netlogic.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/member/modules_member.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.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{
44 public $version = 'dolibarr';
45
46
52 public function __construct($db)
53 {
54 global $conf, $langs, $mysoc;
55
56 // Load translation files required by the page
57 $langs->loadLangs(array("main", "companies"));
58
59 $this->db = $db;
60 $this->name = "ODT templates";
61 $this->description = $langs->trans("DocumentModelOdt");
62 $this->scandir = 'MEMBER_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
63
64 // Page size for A4 format
65 $this->type = 'odt';
66 $this->page_largeur = 0;
67 $this->page_hauteur = 0;
68 $this->format = array($this->page_largeur, $this->page_hauteur);
69 $this->marge_gauche = 0;
70 $this->marge_droite = 0;
71 $this->marge_haute = 0;
72 $this->marge_basse = 0;
73
74 $this->option_logo = 1; // Display logo
75 $this->option_tva = 0; // Manage the vat option FACTURE_TVAOPTION
76 $this->option_modereg = 0; // Display payment mode
77 $this->option_condreg = 0; // Display payment terms
78 $this->option_multilang = 1; // Available in several languages
79 $this->option_escompte = 0; // Displays if there has been a discount
80 $this->option_credit_note = 0; // Support credit notes
81 $this->option_freetext = 1; // Support add of a personalised text
82 $this->option_draft_watermark = 0; // Support add of a watermark on drafts
83
84 // Recupere emetteur
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 $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="MEMBER_ADDON_PDF_ODT_PATH">';
113 $texte .= '<table class="nobordernopadding" width="100%">';
114
115 // List of directories area
116 $texte .= '<tr><td>';
117 $texttitle = $langs->trans("ListOfDirectories");
118 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString('MEMBER_ADDON_PDF_ODT_PATH'))));
119 $listoffiles = array();
120 foreach ($listofdir as $key => $tmpdir) {
121 $tmpdir = trim($tmpdir);
122 $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
123 if (!$tmpdir) {
124 unset($listofdir[$key]);
125 continue;
126 }
127 if (!is_dir($tmpdir)) {
128 $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
129 } else {
130 $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
131 if (count($tmpfiles)) {
132 $listoffiles = array_merge($listoffiles, $tmpfiles);
133 }
134 }
135 }
136 $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
137 $texthelp .= '<br><br><span class="opacitymedium">'.$langs->trans("ExampleOfDirectoriesForModelGen").'</span>';
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, 3, $this->name);
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('MEMBER_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 button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($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=members/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a>';
159 $texte .= ' &nbsp; <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?modulepart=doctemplates&keyforuploaddir=MEMBER_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 $maxfilesizearray = getMaxFileSizeArray();
167 $maxmin = $maxfilesizearray['maxmin'];
168 if ($maxmin > 0) {
169 $texte .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
170 }
171 $texte .= ' <input type="file" name="uploadfile">';
172 $texte .= '<input type="hidden" value="MEMBER_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
173 $texte .= '<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload">';
174 $texte .= '</div>';
175 $texte .= '</td>';
176
177 $texte .= '</tr>';
178
179 $texte .= '</table>';
180 $texte .= '</form>';
181
182 return $texte;
183 }
184
185 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
197 public function write_file($object, $outputlangs, $srctemplatepath, $mode = 'member', $nooutput = 0, $filename = 'tmp_cards')
198 {
199 // phpcs:enable
200 global $user, $langs, $conf, $mysoc, $hookmanager;
201
202 if (empty($srctemplatepath)) {
203 dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
204 return -1;
205 }
206
207 // Add odtgeneration hook
208 if (!is_object($hookmanager)) {
209 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
210 $hookmanager = new HookManager($this->db);
211 }
212 $hookmanager->initHooks(array('odtgeneration'));
213 global $action;
214
215 if (!is_object($outputlangs)) {
216 $outputlangs = $langs;
217 }
218 $sav_charset_output = $outputlangs->charset_output;
219 $outputlangs->charset_output = 'UTF-8';
220
221 // Load translation files required by the page
222 $outputlangs->loadLangs(array("main", "companies", "bills", "dict"));
223
224 if ($conf->adherent->dir_output) {
225 // If $object is id instead of object
226 if (!is_object($object)) {
227 $id = $object;
228 $object = new User($this->db);
229 $result = $object->fetch($id);
230 if ($result < 0) {
231 dol_print_error($this->db, $object->error);
232 return -1;
233 }
234 }
235
236 $object->fetch_thirdparty();
237
238 $dir = $conf->adherent->dir_output;
239 $objectref = dol_sanitizeFileName($object->ref);
240 if (!preg_match('/specimen/i', $objectref)) {
241 $dir .= "/".$objectref;
242 }
243 $file = $dir."/".$objectref.".odt";
244
245 if (!file_exists($dir)) {
246 if (dol_mkdir($dir) < 0) {
247 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
248 return -1;
249 }
250 }
251
252 if (file_exists($dir)) {
253 //print "srctemplatepath=".$srctemplatepath; // Src filename
254 $newfile = basename($srctemplatepath);
255 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
256 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
257 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
258
259 $newfiletmp = $objectref . '_' . $newfiletmp;
260
261 // Get extension (ods or odt)
262 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
263 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
264 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
265 if ($format == '1') {
266 $format = '%Y%m%d%H%M%S';
267 }
268 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
269 } else {
270 $filename = $newfiletmp . '.' . $newfileformat;
271 }
272 $file = $dir . '/' . $filename;
273 //print "newdir=".$dir;
274 //print "newfile=".$newfile;
275 //print "file=".$file;
276 //print "conf->adherent->dir_temp=".$conf->adherent->dir_temp;
277
278 dol_mkdir($conf->adherent->dir_temp);
279 if (!is_writable($conf->adherent->dir_temp)) {
280 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->adherent->dir_temp);
281 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
282 return -1;
283 }
284
285 // If CUSTOMER contact defined on member, we use it
286 $usecontact = false;
287 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
288 if (count($arrayidcontact) > 0) {
289 $usecontact = true;
290 $result = $object->fetch_contact($arrayidcontact[0]);
291 }
292
293 // Recipient name
294 if (!empty($usecontact)) {
295 // We can use the company of contact instead of thirdparty company
296 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || getDolGlobalString('MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
297 $object->contact->fetch_thirdparty();
298 $socobject = $object->contact->thirdparty;
299 $contactobject = $object->contact;
300 } else {
301 $socobject = $object->thirdparty;
302 // if we have a CUSTOMER contact and we don't use it as thirdparty recipient we store the contact object for later use
303 $contactobject = $object->contact;
304 }
305 } else {
306 $socobject = $object->thirdparty;
307 }
308
309 // Open and load template
310 require_once ODTPHP_PATH.'odf.php';
311 try {
312 $odfHandler = new Odf(
313 $srctemplatepath,
314 array(
315 'PATH_TO_TMP' => $conf->adherent->dir_temp,
316 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
317 'DELIMITER_LEFT' => '{',
318 'DELIMITER_RIGHT' => '}'
319 )
320 );
321 } catch (Exception $e) {
322 $this->error = $e->getMessage();
323 dol_syslog($e->getMessage(), LOG_WARNING);
324 return -1;
325 }
326
327 // Make substitutions into odt
328 $array_member = $this->getSubstitutionarrayMember($object, $outputlangs);
329 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
330 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
331 $array_other = $this->get_substitutionarray_other($outputlangs);
332 // retrieve contact information for use in object as contact_xxx tags
333 $array_thirdparty_contact = array();
334 if ($usecontact && is_object($contactobject)) {
335 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
336 }
337
338 $tmparray = array_merge($array_member, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
339 complete_substitutions_array($tmparray, $outputlangs, $object);
340 // Call the ODTSubstitution hook
341 $parameters = array(
342 'file'=>$file,
343 'object'=>$object,
344 'outputlangs'=>$outputlangs,
345 'substitutionarray'=>&$tmparray
346 );
347 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
348 foreach ($tmparray as $key => $value) {
349 try {
350 if (preg_match('/logo$/', $key)) {
351 // Image
352 if (file_exists($value)) {
353 $odfHandler->setImage($key, $value);
354 } else {
355 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
356 }
357 } else {
358 // Text
359 $odfHandler->setVars($key, $value, true, 'UTF-8');
360 }
361 } catch (OdfException $e) {
362 dol_syslog($e->getMessage(), LOG_WARNING);
363 }
364 }
365
366 // Replace labels translated
367 $tmparray = $outputlangs->get_translations_for_substitutions();
368 foreach ($tmparray as $key => $value) {
369 try {
370 $odfHandler->setVars($key, $value, true, 'UTF-8');
371 } catch (OdfException $e) {
372 dol_syslog($e->getMessage(), LOG_WARNING);
373 }
374 }
375
376 // Call the beforeODTSave hook
377 $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
378 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
379
380 // Write new file
381 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
382 try {
383 $odfHandler->exportAsAttachedPDF($file);
384 } catch (Exception $e) {
385 $this->error = $e->getMessage();
386 dol_syslog($e->getMessage(), LOG_WARNING);
387 return -1;
388 }
389 } else {
390 try {
391 $odfHandler->saveToDisk($file);
392 } catch (Exception $e) {
393 $this->error = $e->getMessage();
394 dol_syslog($e->getMessage(), LOG_WARNING);
395 return -1;
396 }
397 }
398
399 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
400
401 dolChmod($file);
402
403 $odfHandler = null; // Destroy object
404
405 $this->result = array('fullpath'=>$file);
406
407 return 1; // Success
408 } else {
409 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
410 return -1;
411 }
412 }
413
414 return -1;
415 }
416
417 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
426 public function get_substitutionarray_object($object, $outputlangs, $array_key = 'object')
427 {
428 // phpcs:enable
429 if (!$object instanceof Adherent) {
430 dol_syslog("Expected Adherent object, got ".gettype($object), LOG_ERR);
431 return array();
432 }
433
434 $array_other = array();
435 foreach ($object as $key => $value) {
436 if (!is_array($value) && !is_object($value)) {
437 $array_other[$array_key.'_'.$key] = $value;
438 }
439 }
440 return $array_other;
441 }
442}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage members of a foundation.
getSubstitutionarrayMember($member, $outputlangs)
Define array with couple substitution key => substitution value.
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}...
Class to manage generation of HTML components Only common components must be here.
Class to manage hooks.
Parent class to manage intervention document templates.
Class to manage Dolibarr users.
Class to build documents using ODF templates generator.
info($langs)
Return description of a module.
get_substitutionarray_object($object, $outputlangs, $array_key='object')
get substitution array for object
write_file($object, $outputlangs, $srctemplatepath, $mode='member', $nooutput=0, $filename='tmp_cards')
Function to build a document on disk using the generic odt 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)
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).
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.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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:139
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:142
getMaxFileSizeArray()
Return the max allowed for file upload.