43 public $version =
'dolibarr';
53 global $conf, $langs,
$mysoc;
56 $langs->loadLangs(array(
"main",
"companies"));
59 $this->
name =
"ODT templates";
60 $this->
description = $langs->trans(
"DocumentModelOdt");
61 $this->scandir =
'RECEPTION_ADDON_PDF_ODT_PATH';
65 $this->page_largeur = 0;
66 $this->page_hauteur = 0;
67 $this->format = array($this->page_largeur, $this->page_hauteur);
68 $this->marge_gauche = 0;
69 $this->marge_droite = 0;
70 $this->marge_haute = 0;
71 $this->marge_basse = 0;
73 $this->option_logo = 1;
74 $this->option_tva = 0;
75 $this->option_modereg = 0;
76 $this->option_condreg = 0;
77 $this->option_multilang = 1;
78 $this->option_escompte = 0;
79 $this->option_credit_note = 0;
80 $this->option_freetext = 1;
81 $this->option_draft_watermark = 0;
90 if (!$this->emetteur->country_code) {
91 $this->emetteur->country_code = substr($langs->defaultlang, -2);
104 global $conf, $langs;
107 $langs->loadLangs(array(
"companies",
"errors"));
109 $form =
new Form($this->db);
114 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
115 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
116 $texte .=
'<input type="hidden" name="page_y" value="">';
117 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
118 $texte .=
'<input type="hidden" name="param1" value="RECEPTION_ADDON_PDF_ODT_PATH">';
119 $texte .=
'<table class="nobordernopadding centpercent">';
122 $texte .=
'<tr><td>';
123 $texttitle = $langs->trans(
"ListOfDirectories");
124 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
125 $listoffiles = array();
126 foreach ($listofdir as $key => $tmpdir) {
127 $tmpdir = trim($tmpdir);
128 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
130 unset($listofdir[$key]);
133 if (!is_dir($tmpdir)) {
134 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
136 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
137 if (count($tmpfiles)) {
138 $listoffiles = array_merge($listoffiles, $tmpfiles);
142 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
143 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
145 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
146 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
149 $nbofiles = count($listoffiles);
150 if (!empty($odtPath)) {
151 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
153 $texte .= count($listoffiles);
155 $texte .=
'</b><br>';
158 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
160 foreach ($listoffiles as $file) {
161 $texte .=
'- '.$file[
'name'];
162 $texte .=
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=receptions/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
163 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=RECEPTION_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
170 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
171 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
172 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
174 $texte .=
'</textarea>';
175 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
176 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
177 $texte .=
'<br></div></div>';
180 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
182 $maxmin = $maxfilesizearray[
'maxmin'];
184 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
186 $texte .=
' <input type="file" name="uploadfile">';
187 $texte .=
'<input type="hidden" value="RECEPTION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
188 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
195 $texte .=
'</table>';
213 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
216 global $user, $langs, $conf,
$mysoc, $hookmanager;
218 if (empty($srctemplatepath)) {
219 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
224 if (!is_object($hookmanager)) {
225 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
228 $hookmanager->initHooks(array(
'odtgeneration'));
231 if (!is_object($outputlangs)) {
232 $outputlangs = $langs;
234 $sav_charset_output = $outputlangs->charset_output;
235 $outputlangs->charset_output =
'UTF-8';
237 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
239 if ($conf->reception->dir_output) {
253 $dir = !empty($conf->reception->multidir_output[
$object->entity ?? $conf->entity]) ? $conf->reception->multidir_output[
$object->entity ?? $conf->entity] : $conf->reception->dir_output;
255 if (!preg_match(
'/specimen/i', $objectref)) {
256 $dir .=
"/".$objectref;
258 $file = $dir.
"/".$objectref.
".odt";
260 if (!file_exists($dir)) {
262 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
267 if (file_exists($dir)) {
269 $newfile = basename($srctemplatepath);
270 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
271 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
272 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
273 $newfiletmp = $objectref .
'_' . $newfiletmp;
276 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
279 if ($format ==
'1') {
280 $format =
'%Y%m%d%H%M%S';
284 $filename = $newfiletmp .
'.' . $newfileformat;
286 $file = $dir .
'/' . $filename;
293 if (!is_writable($conf->reception->dir_temp)) {
294 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->reception->dir_temp);
295 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
301 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
302 if (count($arrayidcontact) > 0) {
304 $result =
$object->fetch_contact($arrayidcontact[0]);
308 $contactobject =
null;
309 if (!empty($usecontact)) {
311 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
312 $object->contact->fetch_thirdparty();
313 $socobject =
$object->contact->thirdparty;
314 $contactobject =
$object->contact;
316 $socobject =
$object->thirdparty;
318 $contactobject =
$object->contact;
321 $socobject =
$object->thirdparty;
325 $substitutionarray = array(
326 '__FROM_NAME__' => $this->emetteur->name,
327 '__FROM_EMAIL__' => $this->emetteur->email,
328 '__TOTAL_TTC__' =>
$object->total_ttc,
329 '__TOTAL_HT__' =>
$object->total_ht,
330 '__TOTAL_VAT__' =>
$object->total_tva
335 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
336 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
340 $paramfreetext =
'RECEPTION_FREE_TEXT';
346 require_once ODTPHP_PATH.
'odf.php';
348 $odfHandler =
new Odf(
351 'PATH_TO_TMP' => $conf->reception->dir_temp,
353 'DELIMITER_LEFT' =>
'{',
354 'DELIMITER_RIGHT' =>
'}'
358 $this->error = $e->getMessage();
369 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
370 }
catch (OdfException $e) {
375 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
377 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
383 $array_thirdparty_contact = array();
384 if ($usecontact && is_object($contactobject)) {
388 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
392 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
393 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
402 foreach ($tmparray as $key => $value) {
404 if (preg_match(
'/logo$/', $key)) {
405 if (file_exists($value)) {
406 $odfHandler->setImage($key, $value, $ratio);
408 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
411 $odfHandler->setVars($key, $value,
true,
'UTF-8');
413 }
catch (OdfException $e) {
418 $foundtagforlines = 1;
420 $listlines = $odfHandler->setSegment(
'lines');
421 }
catch (OdfExceptionSegmentNotFound $e) {
423 $foundtagforlines = 0;
426 if ($foundtagforlines) {
428 foreach (
$object->lines as $line) {
434 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
435 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
436 foreach ($tmparray as $key => $val) {
438 $listlines->setVars($key, $val,
true,
'UTF-8');
439 }
catch (SegmentException $e) {
446 $odfHandler->mergeSegment($listlines);
447 }
catch (OdfException $e) {
448 $this->error = $e->getMessage();
455 $tmparray = $outputlangs->get_translations_for_substitutions();
456 foreach ($tmparray as $key => $value) {
458 $odfHandler->setVars($key, $value,
true,
'UTF-8');
459 }
catch (OdfException $e) {
465 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
466 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
471 $odfHandler->exportAsAttachedPDF($file);
473 $this->error = $e->getMessage();
479 $odfHandler->saveToDisk($file);
481 $this->error = $e->getMessage();
486 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
487 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
493 $this->result = array(
'fullpath' => $file);
497 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);