29require_once DOL_DOCUMENT_ROOT.
'/core/modules/ticket/modules_ticket.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
31require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
32require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
33require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
45 public $version =
'dolibarr';
58 $langs->loadLangs(array(
"main",
"companies"));
61 $this->
name =
"ODT templates";
62 $this->
description = $langs->trans(
"DocumentModelOdt");
63 $this->scandir =
'TICKET_ADDON_PDF_ODT_PATH';
67 $this->page_largeur = 0;
68 $this->page_hauteur = 0;
69 $this->format = array($this->page_largeur, $this->page_hauteur);
70 $this->marge_gauche = 0;
71 $this->marge_droite = 0;
72 $this->marge_haute = 0;
73 $this->marge_basse = 0;
75 $this->option_logo = 1;
76 $this->option_tva = 0;
77 $this->option_multilang = 1;
78 $this->option_freetext = 0;
79 $this->option_draft_watermark = 0;
88 if (!$this->emetteur->country_code) {
89 $this->emetteur->country_code = substr($langs->defaultlang, -2);
105 $langs->loadLangs(array(
'companies',
'errors'));
107 $form =
new Form($this->db);
110 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
111 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
112 $texte .=
'<input type="hidden" name="page_y" value="">';
113 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
114 $texte .=
'<input type="hidden" name="param1" value="TICKET_ADDON_PDF_ODT_PATH">';
116 $texte .=
'<table class="nobordernopadding" width="100%">';
119 $texte .=
'<tr><td>';
120 $texttitle = $langs->trans(
"ListOfDirectories");
121 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
getDolGlobalString(
'TICKET_ADDON_PDF_ODT_PATH'))));
122 $listoffiles = array();
123 foreach ($listofdir as $key => $tmpdir) {
124 $tmpdir = trim($tmpdir);
125 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
127 unset($listofdir[$key]);
130 if (!is_dir($tmpdir)) {
131 $texttitle .= img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
133 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
134 if (count($tmpfiles)) {
135 $listoffiles = array_merge($listoffiles, $tmpfiles);
139 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
140 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
142 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
143 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
146 if (count($listofdir)) {
147 $texte .=
'<span class="opacitylow">'.$langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b></span><br>';
149 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx opacitylow">';
151 foreach ($listoffiles as $file) {
152 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=tickets/'.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'listlight').
'</a>';
153 $texte .=
' <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 .=
'<span class="classlink cursorpointer" onclick="javascript: jQuery(\'.div_odtmodelsof'.get_class($this).
'\').toggle();
">'.img_picto('', 'add').' '.$langs->trans("UploadNewTemplate
").'...</span>';
161 $texte .= '<div class="div_odtmodelsof
'.get_class($this).'" style="display: none; margin-top: 6px;
">';
162 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name);
163 $texte .= '<div><div style="display:
inline-block; min-width: 100px; vertical-align: middle;
">';
164 $texte .= '<textarea class="flat textareafordir
" spellcheck="false" cols="60
" name="value1
">';
165 $texte .= getDolGlobalString('TICKET_ADDON_PDF_ODT_PATH');
166 $texte .= '</textarea>';
167 $texte .= '</div><div style="display:
inline-block; vertical-align: middle;
">';
168 $texte .= '<input type="submit
" class="button button-edit reposition smallpaddingimp
" name="modify
" value="'.dol_escape_htmltag($langs->trans("Modify")).'">';
169 $texte .= '<br></div></div>';
172 // Add input to upload a new template file.
173 $texte .= '<div class="div_odtmodelsof
'.get_class($this).'" style="display: none;
">';
174 $maxfilesizearray = getMaxFileSizeArray();
175 $maxmin = $maxfilesizearray['maxmin'];
177 $texte .= '<input type="hidden
" name="MAX_FILE_SIZE
" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
179 $texte .= ' <input type="file
" name="uploadfile
">';
180 $texte .= '<input type="hidden
" value="TICKET_ADDON_PDF_ODT_PATH
" name="keyforuploaddir
">';
181 $texte .= '<input type="submit
" class="button smallpaddingimp reposition
" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload
">';
188 $texte .= '</table>';
194 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
206 public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
209 global $user, $langs, $conf, $mysoc, $hookmanager;
211 if (empty($srctemplatepath)) {
216 // Add odtgeneration hook
217 if (!is_object($hookmanager)) {
218 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
219 $hookmanager = new HookManager($this->db);
221 $hookmanager->initHooks(array('odtgeneration'));
224 if (!is_object($outputlangs)) {
225 $outputlangs = $langs;
227 $sav_charset_output = $outputlangs->charset_output;
228 $outputlangs->charset_output = 'UTF-8';
230 // Load translation files required by the page
231 $outputlangs->loadLangs(array("main
", "companies
", "bills
", "dict
"));
233 if ($conf->ticket->dir_output) {
234 // If $object is id instead of object
235 if (!is_object($object)) {
237 $object = new User($this->db);
238 $result = $object->fetch($id);
240 dol_print_error($this->db, $object->error);
245 $object->fetch_thirdparty();
247 $dir = $conf->ticket->dir_output;
248 $objectref = dol_sanitizeFileName($object->ref);
249 if (!preg_match('/specimen/i', $objectref)) {
250 $dir .= "/
".$objectref;
252 $file = $dir."/
".$objectref.".odt
";
254 if (!file_exists($dir)) {
255 if (dol_mkdir($dir) < 0) {
256 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
261 if (file_exists($dir)) {
262 //print "srctemplatepath=
".$srctemplatepath; // Src filename
263 $newfile = basename($srctemplatepath);
264 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
265 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
266 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
268 $newfiletmp = $objectref . '_' . $newfiletmp;
270 // Get extension (ods or odt)
271 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
272 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
273 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
274 if ($format == '1') {
275 $format = '%Y%m%d%H%M%S';
277 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
279 $filename = $newfiletmp . '.' . $newfileformat;
281 $file = $dir . '/' . $filename;
282 //print "newdir=
".$dir;
283 //print "newfile=
".$newfile;
284 //print "file=
".$file;
285 //print "conf->ticket->dir_temp=
".$conf->ticket->dir_temp;
287 dol_mkdir($conf->ticket->dir_temp);
288 if (!is_writable($conf->ticket->dir_temp)) {
289 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory
", $conf->ticket->dir_temp);
290 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
294 // If CUSTOMER contact defined on user, we use it
296 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
297 if (count($arrayidcontact) > 0) {
299 $result = $object->fetch_contact($arrayidcontact[0]);
302 $contactobject = null;
304 if (!empty($usecontact)) {
305 // We can use the company of contact instead of thirdparty company
306 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || getDolGlobalString('MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
307 $object->contact->fetch_thirdparty();
308 $socobject = $object->contact->thirdparty;
309 $contactobject = $object->contact;
311 $socobject = $object->thirdparty;
312 // if we have a CUSTOMER contact and we don't use it as thirdparty recipient we store the contact object for later use
313 $contactobject = $object->contact;
316 $socobject = $object->thirdparty;
319 // Open and load template
320 require_once ODTPHP_PATH.'odf.php';
322 $odfHandler = new Odf(
325 'PATH_TO_TMP' => $conf->ticket->dir_temp,
326 'ZIP_PROXY' => getDolGlobalString('MAIN_ODF_ZIP_PROXY', 'PclZipProxy'), // PhpZipProxy or PclZipProxy. Got "bad compression method
" error when using PhpZipProxy.
327 'DELIMITER_LEFT' => '{',
328 'DELIMITER_RIGHT' => '}'
331 } catch (Exception $e) {
332 $this->error = $e->getMessage();
333 dol_syslog($e->getMessage(), LOG_WARNING);
337 // Make substitutions into odt
338 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
339 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
340 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
341 $array_other = $this->get_substitutionarray_other($outputlangs);
342 // retrieve contact information for use in object as contact_xxx tags
343 $array_thirdparty_contact = array();
344 if ($usecontact && is_object($contactobject)) {
345 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
348 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
349 complete_substitutions_array($tmparray, $outputlangs, $object);
350 $object->fetch_optionals();
351 // Call the ODTSubstitution hook
352 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
353 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
355 // retrieve the constant to apply a ratio for image size or set the ratio to 1
356 if (getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO')) {
357 $ratio = (float) getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO');
362 foreach ($tmparray as $key => $value) {
364 if (preg_match('/logo$/', $key)) { // Image
365 if (file_exists($value)) {
366 $odfHandler->setImage($key, $value, $ratio);
368 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
371 $odfHandler->setVars($key, $value, true, 'UTF-8');
373 } catch (OdfException $e) {
374 dol_syslog($e->getMessage(), LOG_WARNING);
378 // Replace labels translated
379 $tmparray = $outputlangs->get_translations_for_substitutions();
380 foreach ($tmparray as $key => $value) {
382 $odfHandler->setVars($key, $value, true, 'UTF-8');
383 } catch (OdfException $e) {
384 dol_syslog($e->getMessage(), LOG_WARNING);
388 // Call the beforeODTSave hook
389 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs);
390 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
393 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
395 $odfHandler->exportAsAttachedPDF($file);
396 } catch (Exception $e) {
397 $this->error = $e->getMessage();
398 dol_syslog($e->getMessage(), LOG_WARNING);
403 $odfHandler->saveToDisk($file);
404 } catch (Exception $e) {
405 $this->error = $e->getMessage();
406 dol_syslog($e->getMessage(), LOG_WARNING);
411 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
415 $odfHandler = null; // Destroy object
417 $this->result = array('fullpath' => $file);
421 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
429 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
438 public function get_substitutionarray_object($object, $outputlangs, $array_key = 'object')
441 if (!$object instanceof User) {
442 dol_syslog("Expected
Ticket object, got
".gettype($object), LOG_ERR);
446 $array_other = array();
447 foreach ($object as $key => $value) {
448 if (!is_array($value) && !is_object($value)) {
449 $array_other[$array_key.'_'.$key] = $value;
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Parent class for documents models.
write_file($object, $outputlangs, $srctemplatepath='', $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
Class to build documents using ODF templates generator.
__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.
getCallerInfoString()
Get caller info as a string that can be appended to a log message.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
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.
conf($dolibarr_main_document_root, $realpathconf=null)
Load conf file (file must exists)
Class to generate the form for creating a new ticket.
$conf db name
Only used if Module[ID]Name translation string is not found.
multi select button
0 = Do not include form tag and submit button -1 = Do not include form tag but include submit button