27 require_once DOL_DOCUMENT_ROOT.
'/core/modules/usergroup/modules_usergroup.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/user/class/usergroup.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
51 public $version =
'dolibarr';
61 global $conf, $langs, $mysoc;
64 $langs->loadLangs(array(
"main",
"companies"));
67 $this->
name =
"ODT templates";
68 $this->
description = $langs->trans(
"DocumentModelOdt");
69 $this->scandir =
'USERGROUP_ADDON_PDF_ODT_PATH';
73 $this->page_largeur = 0;
74 $this->page_hauteur = 0;
75 $this->format = array($this->page_largeur, $this->page_hauteur);
76 $this->marge_gauche = 0;
77 $this->marge_droite = 0;
78 $this->marge_haute = 0;
79 $this->marge_basse = 0;
81 $this->option_logo = 1;
82 $this->option_tva = 0;
83 $this->option_modereg = 0;
84 $this->option_condreg = 0;
85 $this->option_multilang = 1;
86 $this->option_escompte = 0;
87 $this->option_credit_note = 0;
88 $this->option_freetext = 1;
89 $this->option_draft_watermark = 0;
92 $this->emetteur = $mysoc;
93 if (!$this->emetteur->country_code) {
94 $this->emetteur->country_code = substr($langs->defaultlang, -2);
107 global $conf, $langs;
110 $langs->loadLangs(array(
"errors",
"companies"));
118 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
119 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
120 $texte .=
'<input type="hidden" name="page_y" value="">';
121 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
122 $texte .=
'<input type="hidden" name="param1" value="USERGROUP_ADDON_PDF_ODT_PATH">';
124 $texte .=
'<input type="hidden" name="param2" value="USERGROUP_ADDON_PDF_ODT_DEFAULT">';
125 $texte .=
'<input type="hidden" name="param3" value="USERGROUP_ADDON_PDF_ODT_TOBILL">';
126 $texte .=
'<input type="hidden" name="param4" value="USERGROUP_ADDON_PDF_ODT_CLOSED">';
128 $texte .=
'<table class="nobordernopadding" width="100%">';
131 $texte .=
'<tr><td>';
132 $texttitle = $langs->trans(
"ListOfDirectories");
133 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
134 $listoffiles = array();
135 foreach ($listofdir as $key => $tmpdir) {
136 $tmpdir = trim($tmpdir);
137 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
139 unset($listofdir[$key]);
142 if (!is_dir($tmpdir)) {
143 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
145 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
146 if (count($tmpfiles)) {
147 $listoffiles = array_merge($listoffiles, $tmpfiles);
151 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
153 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
154 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
156 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
157 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
158 $texte .=
'<textarea class="flat" cols="60" name="value1">';
160 $texte .=
'</textarea>';
161 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
162 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
163 $texte .=
'<br></div></div>';
166 if (count($listofdir)) {
167 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
172 $texte .=
'<table width="50%;">';
174 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
175 $texte .=
'<td colspan="">';
177 $texte .=
"</td></tr>";
180 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
181 $texte .=
'<td colspan="">';
183 $texte .=
"</td></tr>";
186 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
187 $texte .=
'<td colspan="">';
189 $texte .=
"</td></tr>";
190 $texte .=
'</table>';
196 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
197 $texte .=
'<span class="opacitymedium">';
198 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
203 $texte .=
'</table>';
221 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
224 global $user, $langs, $conf, $mysoc, $hookmanager;
226 if (empty($srctemplatepath)) {
227 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
232 if (!is_object($hookmanager)) {
233 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
236 $hookmanager->initHooks(array(
'odtgeneration'));
239 if (!is_object($outputlangs)) {
240 $outputlangs = $langs;
242 $sav_charset_output = $outputlangs->charset_output;
243 $outputlangs->charset_output =
'UTF-8';
246 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
248 if ($conf->user->dir_output) {
250 if (!is_object($object)) {
253 $result = $object->fetch($id);
260 $dir = $conf->user->dir_output.
'/usergroups';
262 if (!preg_match(
'/specimen/i', $objectref)) {
263 $dir .=
"/".$objectref;
265 $file = $dir.
"/".$objectref.
".odt";
267 if (!file_exists($dir)) {
269 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
274 if (file_exists($dir)) {
276 $newfile = basename($srctemplatepath);
277 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
278 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
279 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
281 $newfiletmp = $objectref .
'_' . $newfiletmp;
284 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
287 if ($format ==
'1') {
288 $format =
'%Y%m%d%H%M%S';
292 $filename = $newfiletmp .
'.' . $newfileformat;
294 $file = $dir .
'/' . $filename;
301 if (!is_writable($conf->user->dir_temp)) {
302 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->user->dir_temp);
303 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
309 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
310 if (count($arrayidcontact) > 0) {
312 $result = $object->fetch_contact($arrayidcontact[0]);
316 if (!empty($usecontact)) {
318 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
319 $object->contact->fetch_thirdparty();
320 $socobject = $object->contact->thirdparty;
321 $contactobject = $object->contact;
323 $socobject = $object->thirdparty;
325 $contactobject = $object->contact;
328 $socobject = $object->thirdparty;
331 $substitutionarray = array(
332 '__FROM_NAME__' => $this->emetteur->name,
333 '__FROM_EMAIL__' => $this->emetteur->email,
334 '__TOTAL_TTC__' => $object->total_ttc,
335 '__TOTAL_HT__' => $object->total_ht,
336 '__TOTAL_VAT__' => $object->total_tva
340 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
341 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
345 $paramfreetext =
'user_FREE_TEXT';
346 if (!empty($conf->global->$paramfreetext)) {
351 require_once ODTPHP_PATH.
'odf.php';
353 $odfHandler =
new Odf(
356 'PATH_TO_TMP' => $conf->user->dir_temp,
357 'ZIP_PROXY' =>
'PclZipProxy',
358 'DELIMITER_LEFT' =>
'{',
359 'DELIMITER_RIGHT' =>
'}'
363 $this->error = $e->getMessage();
376 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
377 }
catch (OdfException $e) {
389 $array_thirdparty_contact = array();
390 if ($usecontact && is_object($contactobject)) {
394 $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
396 $object->fetch_optionals();
398 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
399 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
400 foreach ($tmparray as $key => $value) {
402 if (preg_match(
'/logo$/', $key)) {
403 if (file_exists($value)) {
404 $odfHandler->setImage($key, $value);
406 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
410 $odfHandler->setVars($key, $value,
true,
'UTF-8');
412 }
catch (OdfException $e) {
418 $foundtagforlines = 1;
420 $listlines = $odfHandler->setSegment(
'lines');
421 }
catch (OdfException $e) {
423 $foundtagforlines = 0;
426 if ($foundtagforlines) {
427 foreach ($object->members as $u) {
429 unset($tmparray[
'object_pass']);
430 unset($tmparray[
'object_pass_indatabase']);
433 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$u);
434 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
435 foreach ($tmparray as $key => $val) {
437 if (!is_array($val)) {
438 $listlines->setVars($key, $val,
true,
'UTF-8');
440 }
catch (OdfException $e) {
442 }
catch (SegmentException $e) {
448 $odfHandler->mergeSegment($listlines);
450 }
catch (OdfException $e) {
451 $this->error = $e->getMessage();
457 $tmparray = $outputlangs->get_translations_for_substitutions();
458 foreach ($tmparray as $key => $value) {
460 $odfHandler->setVars($key, $value,
true,
'UTF-8');
461 }
catch (OdfException $e) {
467 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
468 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
471 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
473 $odfHandler->exportAsAttachedPDF($file);
475 $this->error = $e->getMessage();
481 $odfHandler->saveToDisk($file);
483 $this->error = $e->getMessage();
489 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
495 $this->result = array(
'fullpath'=>$file);
499 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);