43 public $version =
'dolibarr';
53 global
$conf, $langs, $mysoc;
55 $langs->loadLangs(array(
"main",
"companies"));
58 $this->
name =
"ODT templates";
59 $this->
description = $langs->trans(
"DocumentModelOdt");
60 $this->scandir =
'RECEPTION_ADDON_PDF_ODT_PATH';
64 $this->page_largeur = 0;
65 $this->page_hauteur = 0;
66 $this->format = array($this->page_largeur, $this->page_hauteur);
67 $this->marge_gauche = 0;
68 $this->marge_droite = 0;
69 $this->marge_haute = 0;
70 $this->marge_basse = 0;
72 $this->option_logo = 1;
73 $this->option_tva = 0;
74 $this->option_modereg = 0;
75 $this->option_condreg = 0;
76 $this->option_multilang = 1;
77 $this->option_escompte = 0;
78 $this->option_credit_note = 0;
79 $this->option_freetext = 1;
80 $this->option_draft_watermark = 0;
82 if ($mysoc ===
null) {
88 $this->emetteur = $mysoc;
89 if (!$this->emetteur->country_code) {
90 $this->emetteur->country_code = substr($langs->defaultlang, -2);
103 global
$conf, $langs;
105 $langs->load(
"companies");
106 $langs->load(
"errors");
108 $form =
new Form($this->db);
111 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
112 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
113 $texte .=
'<input type="hidden" name="page_y" value="">';
114 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
115 $texte .=
'<input type="hidden" name="param1" value="RECEPTION_ADDON_PDF_ODT_PATH">';
116 $texte .=
'<table class="nobordernopadding centpercent">';
119 $texte .=
'<tr><td>';
120 $texttitle = $langs->trans(
"ListOfDirectories");
121 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
$conf->global->RECEPTION_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");
145 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
146 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
147 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
149 $texte .=
'</textarea>';
150 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
151 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans(
"Modify").
'">';
152 $texte .=
'<br></div></div>';
155 $nbofiles = count($listoffiles);
157 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
159 $texte .= count($listoffiles);
164 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
166 foreach ($listoffiles as $file) {
167 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=receptions/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
168 $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>';
174 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
176 $maxmin = $maxfilesizearray[
'maxmin'];
178 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
180 $texte .=
' <input type="file" name="uploadfile">';
181 $texte .=
'<input type="hidden" value="RECEPTION_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
182 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
189 $texte .=
'</table>';
207 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
210 global $user, $langs,
$conf, $mysoc, $hookmanager;
212 if (empty($srctemplatepath)) {
213 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
218 if (!is_object($hookmanager)) {
219 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
222 $hookmanager->initHooks(array(
'odtgeneration'));
225 if (!is_object($outputlangs)) {
226 $outputlangs = $langs;
228 $sav_charset_output = $outputlangs->charset_output;
229 $outputlangs->charset_output =
'UTF-8';
231 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
233 if (
$conf->reception->dir_output) {
247 $dir = !empty(
$conf->reception->multidir_output[
$object->entity]) ?
$conf->reception->multidir_output[
$object->entity] :
$conf->reception->dir_output;
249 if (!preg_match(
'/specimen/i', $objectref)) {
250 $dir .=
"/".$objectref;
252 $file = $dir.
"/".$objectref.
".odt";
254 if (!file_exists($dir)) {
256 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
261 if (file_exists($dir)) {
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);
267 $newfiletmp = $objectref .
'_' . $newfiletmp;
270 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
273 if ($format ==
'1') {
274 $format =
'%Y%m%d%H%M%S';
278 $filename = $newfiletmp .
'.' . $newfileformat;
280 $file = $dir .
'/' . $filename;
287 if (!is_writable(
$conf->reception->dir_temp)) {
288 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->reception->dir_temp);
289 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
295 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
296 if (count($arrayidcontact) > 0) {
298 $result =
$object->fetch_contact($arrayidcontact[0]);
302 $contactobject =
null;
303 if (!empty($usecontact)) {
306 $object->contact->fetch_thirdparty();
307 $socobject =
$object->contact->thirdparty;
308 $contactobject =
$object->contact;
310 $socobject =
$object->thirdparty;
312 $contactobject =
$object->contact;
315 $socobject =
$object->thirdparty;
319 $substitutionarray = array(
320 '__FROM_NAME__' => $this->emetteur->name,
321 '__FROM_EMAIL__' => $this->emetteur->email,
322 '__TOTAL_TTC__' =>
$object->total_ttc,
323 '__TOTAL_HT__' =>
$object->total_ht,
324 '__TOTAL_VAT__' =>
$object->total_tva
329 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
330 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
334 $paramfreetext =
'RECEPTION_FREE_TEXT';
340 require_once ODTPHP_PATH.
'odf.php';
342 $odfHandler =
new Odf(
345 'PATH_TO_TMP' =>
$conf->reception->dir_temp,
347 'DELIMITER_LEFT' =>
'{',
348 'DELIMITER_RIGHT' =>
'}'
352 $this->error = $e->getMessage();
363 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
364 }
catch (OdfException $e) {
369 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
371 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
377 $array_thirdparty_contact = array();
378 if ($usecontact && is_object($contactobject)) {
382 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
386 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
387 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
396 foreach ($tmparray as $key => $value) {
398 if (preg_match(
'/logo$/', $key)) {
399 if (file_exists($value)) {
400 $odfHandler->setImage($key, $value, $ratio);
402 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
405 $odfHandler->setVars($key, $value,
true,
'UTF-8');
407 }
catch (OdfException $e) {
412 $foundtagforlines = 1;
414 $listlines = $odfHandler->setSegment(
'lines');
415 }
catch (OdfExceptionSegmentNotFound $e) {
417 $foundtagforlines = 0;
420 if ($foundtagforlines) {
422 foreach (
$object->lines as $line) {
428 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
429 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
430 foreach ($tmparray as $key => $val) {
432 $listlines->setVars($key, $val,
true,
'UTF-8');
433 }
catch (SegmentException $e) {
440 $odfHandler->mergeSegment($listlines);
441 }
catch (OdfException $e) {
442 $this->error = $e->getMessage();
449 $tmparray = $outputlangs->get_translations_for_substitutions();
450 foreach ($tmparray as $key => $value) {
452 $odfHandler->setVars($key, $value,
true,
'UTF-8');
453 }
catch (OdfException $e) {
459 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
460 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
465 $odfHandler->exportAsAttachedPDF($file);
467 $this->error = $e->getMessage();
473 $odfHandler->saveToDisk($file);
475 $this->error = $e->getMessage();
480 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
481 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
487 $this->result = array(
'fullpath' => $file);
491 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);