29require_once DOL_DOCUMENT_ROOT.
'/core/modules/propale/modules_propale.php';
30require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
31require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
32require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
33require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
34require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
35require_once DOL_DOCUMENT_ROOT.
'/core/lib/signature.lib.php';
53 public $phpmin = array(7, 0);
58 public $version =
'dolibarr';
71 $langs->loadLangs(array(
"main",
"companies"));
74 $this->
name =
"ODT templates";
75 $this->
description = $langs->trans(
"DocumentModelOdt");
76 $this->update_main_doc_field = 1;
77 $this->scandir =
'PROPALE_ADDON_PDF_ODT_PATH';
81 $this->page_largeur = 0;
82 $this->page_hauteur = 0;
83 $this->format = array($this->page_largeur, $this->page_hauteur);
84 $this->marge_gauche = 0;
85 $this->marge_droite = 0;
86 $this->marge_haute = 0;
87 $this->marge_basse = 0;
89 $this->option_logo = 1;
90 $this->option_tva = 0;
91 $this->option_modereg = 0;
92 $this->option_condreg = 0;
93 $this->option_multilang = 1;
94 $this->option_escompte = 0;
95 $this->option_credit_note = 0;
96 $this->option_freetext = 1;
97 $this->option_draft_watermark = 0;
106 if (!$this->emetteur->country_code) {
107 $this->emetteur->country_code = substr($langs->defaultlang, -2);
123 $langs->loadLangs(array(
"errors",
"companies"));
125 $form =
new Form($this->db);
131 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
132 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
133 $texte .=
'<input type="hidden" name="page_y" value="">';
134 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
135 $texte .=
'<input type="hidden" name="param1" value="PROPALE_ADDON_PDF_ODT_PATH">';
137 $texte .=
'<input type="hidden" name="param2" value="PROPALE_ADDON_PDF_ODT_DEFAULT">';
138 $texte .=
'<input type="hidden" name="param3" value="PROPALE_ADDON_PDF_ODT_TOBILL">';
139 $texte .=
'<input type="hidden" name="param4" value="PROPALE_ADDON_PDF_ODT_CLOSED">';
141 $texte .=
'<table class="nobordernopadding centpercent">';
144 $texte .=
'<tr><td>';
145 $texttitle = $langs->trans(
"ListOfDirectories");
146 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
147 $listoffiles = array();
148 foreach ($listofdir as $key => $tmpdir) {
149 $tmpdir = trim($tmpdir);
150 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
152 unset($listofdir[$key]);
155 if (!is_dir($tmpdir)) {
156 $texttitle .= img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
158 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
159 if (count($tmpfiles)) {
160 $listoffiles = array_merge($listoffiles, $tmpfiles);
164 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
165 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
167 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
168 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
171 $nbofiles = count($listoffiles);
172 if (!empty($odtPath)) {
173 $texte .=
'<span class="opacitylow">'.$langs->trans(
"NumberOfModelFilesFound").
': <b>';
175 $texte .= count($listoffiles);
177 $texte .=
'</b></span><br>';
181 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx opacitylow">';
183 foreach ($listoffiles as $file) {
184 $texte .=
'- '.$file[
'name'];
185 $texte .=
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=proposals/'.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'listlight').
'</a>';
186 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=PROPALE_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'delete').
'</a>';
195 $texte .=
'<table width="50%">';
197 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
198 $texte .=
'<td colspan="">';
199 $texte .= $form->selectarray(
'value2', $list,
getDolGlobalString(
'PROPALE_ADDON_PDF_ODT_DEFAULT'));
200 $texte .=
"</td></tr>";
203 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
204 $texte .=
'<td colspan="">';
205 $texte .= $form->selectarray(
'value3', $list,
getDolGlobalString(
'PROPALE_ADDON_PDF_ODT_TOBILL'));
206 $texte .=
"</td></tr>";
209 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
210 $texte .=
'<td colspan="">';
211 $texte .= $form->selectarray(
'value4', $list,
getDolGlobalString(
'PROPALE_ADDON_PDF_ODT_CLOSED'));
212 $texte .=
"</td></tr>";
213 $texte .=
'</table>';
219 $texte .=
'<span class="classlink cursorpointer" onclick="javascript: jQuery(\'.div_odtmodelsof'.get_class($this).
'\').toggle();
">'.img_picto('', 'add').' '.$langs->trans("UploadNewTemplate
").'...</span>';
220 $texte .= '<div class="div_odtmodelsof
'.get_class($this).'" style="display: none; margin-top: 6px;
">';
221 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name);
222 $texte .= '<div><div style="display:
inline-block; min-width: 100px; vertical-align: middle;
">';
223 $texte .= '<textarea class="flat textareafordir
" spellcheck="false" cols="60
" name="value1
">';
225 $texte .= '</textarea>';
226 $texte .= '</div><div style="display:
inline-block; vertical-align: middle;
">';
227 $texte .= '<input type="submit
" class="button button-edit reposition smallpaddingimp
" name="modify
" value="'.dolPrintHTMLForAttribute($langs->trans("Modify")).'">';
228 $texte .= '<br></div></div>';
230 // Add input to upload a new template file.
232 $maxfilesizearray = getMaxFileSizeArray();
233 $maxmin = $maxfilesizearray['maxmin'];
235 $texte .= '<input type="hidden
" name="MAX_FILE_SIZE
" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
237 $texte .= ' <input type="file
" name="uploadfile
">';
238 $texte .= '<input type="hidden
" value="PROPALE_ADDON_PDF_ODT_PATH
" name="keyforuploaddir
">';
239 $texte .= '<input type="submit
" class="button smallpaddingimp reposition
" value="'.dol_escape_htmltag($langs->trans("Upload")).'" name="upload
">';
246 $texte .= '</table>';
252 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
264 public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
267 global $user, $langs, $conf, $mysoc, $hookmanager;
269 if (empty($srctemplatepath)) {
274 // Add odtgeneration hook
275 if (!is_object($hookmanager)) {
276 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
277 $hookmanager = new HookManager($this->db);
279 $hookmanager->initHooks(array('odtgeneration'));
282 if (!is_object($outputlangs)) {
283 $outputlangs = $langs;
285 $sav_charset_output = $outputlangs->charset_output;
286 $outputlangs->charset_output = 'UTF-8';
288 // Load translation files required by the page
289 $outputlangs->loadLangs(array("main
", "dict
", "companies
", "bills
"));
291 if ($conf->propal->multidir_output[$conf->entity]) {
292 // If $object is id instead of object
293 if (!is_object($object)) {
295 $object = new Propal($this->db);
296 $result = $object->fetch($id);
298 dol_print_error($this->db, $object->error);
303 $object->fetch_thirdparty();
305 $dir = $conf->propal->multidir_output[$object->entity ?? $conf->entity];
306 $objectref = dol_sanitizeFileName($object->ref);
307 if (!preg_match('/specimen/i', $objectref)) {
308 $dir .= "/
".$objectref;
310 $file = $dir."/
".$objectref.".odt
";
312 if (!file_exists($dir)) {
313 if (dol_mkdir($dir) < 0) {
314 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
319 if (file_exists($dir)) {
320 //print "srctemplatepath=
".$srctemplatepath; // Src filename
321 $newfile = basename($srctemplatepath);
322 $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile);
323 $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
324 $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
326 $newfiletmp = $objectref . '_' . $newfiletmp;
328 // Get extension (ods or odt)
329 $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
330 if (getDolGlobalString('MAIN_DOC_USE_TIMING')) {
331 $format = getDolGlobalString('MAIN_DOC_USE_TIMING');
332 if ($format == '1') {
333 $format = '%Y%m%d%H%M%S';
335 $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat;
337 $filename = $newfiletmp . '.' . $newfileformat;
339 $file = $dir . '/' . $filename;
340 //print "newdir=
".$dir;
341 //print "newfile=
".$newfile;
342 //print "file=
".$file;
343 //print "conf->propal->dir_temp=
".$conf->propal->dir_temp;
345 dol_mkdir($conf->propal->multidir_temp[$object->entity]);
346 if (!is_writable($conf->propal->dir_temp)) {
347 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory
", $conf->propal->dir_temp);
348 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
352 // If CUSTOMER contact defined on proposal, we use it
354 $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
355 if (count($arrayidcontact) > 0) {
357 $result = $object->fetch_contact($arrayidcontact[0]);
361 $contactobject = null;
362 if (!empty($usecontact)) {
363 // We can use the company of contact instead of thirdparty company
364 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || getDolGlobalString('MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
365 $object->contact->fetch_thirdparty();
366 $socobject = $object->contact->thirdparty;
367 $contactobject = $object->contact;
369 $socobject = $object->thirdparty;
370 // if we have a CUSTOMER contact and we don't use it as thirdparty recipient we store the contact object for later use
371 $contactobject = $object->contact;
374 $socobject = $object->thirdparty;
377 $substitutionarray = array(
378 '__FROM_NAME__' => $this->emetteur->name,
379 '__FROM_EMAIL__' => $this->emetteur->email,
380 '__TOTAL_TTC__' => $object->total_ttc,
381 '__TOTAL_HT__' => $object->total_ht,
382 '__TOTAL_VAT__' => $object->total_tva
384 complete_substitutions_array($substitutionarray, $langs, $object);
385 // Call the ODTSubstitution hook
386 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$substitutionarray);
387 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
391 $paramfreetext = 'PROPOSAL_FREE_TEXT';
392 if (getDolGlobalString($paramfreetext)) {
393 $newfreetext = make_substitutions(getDolGlobalString($paramfreetext), $substitutionarray);
396 // Open and load template
397 require_once ODTPHP_PATH.'odf.php';
399 $odfHandler = new Odf(
402 'PATH_TO_TMP' => $conf->propal->multidir_temp[$object->entity],
403 'ZIP_PROXY' => getDolGlobalString('MAIN_ODF_ZIP_PROXY', 'PclZipProxy'), // PhpZipProxy or PclZipProxy. Got "bad compression method
" error when using PhpZipProxy.
404 'DELIMITER_LEFT' => '{',
405 'DELIMITER_RIGHT' => '}'
408 } catch (Exception $e) {
409 $this->error = $e->getMessage();
410 dol_syslog($e->getMessage(), LOG_INFO);
413 // After construction $odfHandler->contentXml contains content and
414 // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
415 // [!-- BEGIN lines --]*[!-- END lines --]
416 //print html_entity_decode($odfHandler->__toString());
419 $object->fetch_optionals();
421 // Make substitutions into odt of freetext
423 $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
424 } catch (OdfException $e) {
425 dol_syslog($e->getMessage(), LOG_INFO);
428 // Define substitution array
429 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
430 $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs);
431 $array_objet = $this->get_substitutionarray_object($object, $outputlangs);
432 $array_user = $this->get_substitutionarray_user($user, $outputlangs);
433 $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
434 $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
435 $array_other = $this->get_substitutionarray_other($outputlangs);
437 include_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
438 $companybankaccount = new CompanyBankAccount($this->db);
439 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
440 $companybankaccount->fetch(0, '', $object->thirdparty->id);
441 $array_objet['company_default_bank_iban'] = $companybankaccount->iban;
442 $array_objet['company_default_bank_bic'] = $companybankaccount->bic;
444 // retrieve contact information for use in object as contact_xxx tags
445 $array_thirdparty_contact = array();
446 if ($usecontact && is_object($contactobject)) {
447 $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact');
450 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
451 complete_substitutions_array($tmparray, $outputlangs, $object);
453 // Call the ODTSubstitution hook
454 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
455 $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
457 // retrieve the constant to apply a ratio for image size or set the ratio to 1
458 if (getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO')) {
459 $ratio = (float) getDolGlobalString('MAIN_DOC_ODT_IMAGE_RATIO');
464 foreach ($tmparray as $key => $value) {
466 if (preg_match('/logo$/', $key)) {
468 if (file_exists($value)) {
469 $odfHandler->setImage($key, $value, $ratio);
471 $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
475 $odfHandler->setVars($key, $value, true, 'UTF-8');
477 } catch (OdfException $e) {
478 dol_syslog($e->getMessage(), LOG_INFO);
481 // Replace tags of lines
482 $foundtagforlines = 1;
484 $listlines = $odfHandler->setSegment('lines');
485 } catch (OdfExceptionSegmentNotFound $e) {
486 // We may arrive here if tags for lines not present into template
487 $foundtagforlines = 0;
488 dol_syslog($e->getMessage(), LOG_INFO);
490 if ($foundtagforlines) {
492 foreach ($object->lines as $line) {
495 $tmparray = $this->get_substitutionarray_lines($line, $outputlangs, $linenumber);
496 complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines
");
497 // Call the ODTSubstitutionLine hook
498 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray, 'line' => $line);
499 $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
500 foreach ($tmparray as $key => $val) {
502 $listlines->setVars($key, $val, true, 'UTF-8');
503 } catch (SegmentException $e) {
504 dol_syslog($e->getMessage(), LOG_INFO);
510 $odfHandler->mergeSegment($listlines);
511 } catch (OdfException $e) {
512 $this->error = $e->getMessage();
513 dol_syslog($this->error, LOG_WARNING);
518 // Replace labels translated
519 $tmparray = $outputlangs->get_translations_for_substitutions();
520 foreach ($tmparray as $key => $value) {
522 $odfHandler->setVars($key, $value, true, 'UTF-8');
523 } catch (OdfException $e) {
524 dol_syslog($e->getMessage(), LOG_INFO);
528 // Call the beforeODTSave hook
529 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
530 $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
533 if (getDolGlobalString('MAIN_ODT_AS_PDF')) {
535 $odfHandler->exportAsAttachedPDF($file);
536 } catch (Exception $e) {
537 $this->error = $e->getMessage();
538 dol_syslog($e->getMessage(), LOG_INFO);
543 $odfHandler->saveToDisk($file);
544 } catch (Exception $e) {
545 $this->error = $e->getMessage();
546 dol_syslog($e->getMessage(), LOG_INFO);
550 $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray);
551 $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
555 $odfHandler = null; // Destroy object
557 $this->result = array('fullpath' => $file);
561 $this->error = $langs->transnoentities("ErrorCanNotCreateDir
", $dir);
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Class mere des modeles de propale.
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
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.
info($langs)
Return description of a module.
__construct($db)
Constructor.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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)
$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