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 $phpmin = array(7, 0);
57 public $version =
'dolibarr';
67 global $conf, $langs, $mysoc;
70 $langs->loadLangs(array(
"main",
"companies"));
73 $this->
name =
"ODT templates";
74 $this->
description = $langs->trans(
"DocumentModelOdt");
75 $this->scandir =
'USERGROUP_ADDON_PDF_ODT_PATH';
79 $this->page_largeur = 0;
80 $this->page_hauteur = 0;
81 $this->format = array($this->page_largeur, $this->page_hauteur);
82 $this->marge_gauche = 0;
83 $this->marge_droite = 0;
84 $this->marge_haute = 0;
85 $this->marge_basse = 0;
87 $this->option_logo = 1;
88 $this->option_tva = 0;
89 $this->option_modereg = 0;
90 $this->option_condreg = 0;
91 $this->option_multilang = 1;
92 $this->option_escompte = 0;
93 $this->option_credit_note = 0;
94 $this->option_freetext = 1;
95 $this->option_draft_watermark = 0;
98 $this->emetteur = $mysoc;
99 if (!$this->emetteur->country_code) {
100 $this->emetteur->country_code = substr($langs->defaultlang, -2);
113 global $conf, $langs;
116 $langs->loadLangs(array(
"errors",
"companies"));
124 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
125 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
126 $texte .=
'<input type="hidden" name="page_y" value="">';
127 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
128 $texte .=
'<input type="hidden" name="param1" value="USERGROUP_ADDON_PDF_ODT_PATH">';
130 $texte .=
'<input type="hidden" name="param2" value="USERGROUP_ADDON_PDF_ODT_DEFAULT">';
131 $texte .=
'<input type="hidden" name="param3" value="USERGROUP_ADDON_PDF_ODT_TOBILL">';
132 $texte .=
'<input type="hidden" name="param4" value="USERGROUP_ADDON_PDF_ODT_CLOSED">';
134 $texte .=
'<table class="nobordernopadding" width="100%">';
137 $texte .=
'<tr><td>';
138 $texttitle = $langs->trans(
"ListOfDirectories");
139 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
140 $listoffiles = array();
141 foreach ($listofdir as $key => $tmpdir) {
142 $tmpdir = trim($tmpdir);
143 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
145 unset($listofdir[$key]);
148 if (!is_dir($tmpdir)) {
149 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
151 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
152 if (count($tmpfiles)) {
153 $listoffiles = array_merge($listoffiles, $tmpfiles);
157 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
159 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
160 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
162 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
163 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
164 $texte .=
'<textarea class="flat" cols="60" name="value1">';
166 $texte .=
'</textarea>';
167 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
168 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
169 $texte .=
'<br></div></div>';
172 if (count($listofdir)) {
173 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
178 $texte .=
'<table width="50%;">';
180 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
181 $texte .=
'<td colspan="">';
183 $texte .=
"</td></tr>";
186 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
187 $texte .=
'<td colspan="">';
189 $texte .=
"</td></tr>";
192 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
193 $texte .=
'<td colspan="">';
195 $texte .=
"</td></tr>";
196 $texte .=
'</table>';
202 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
203 $texte .=
'<span class="opacitymedium">';
204 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
209 $texte .=
'</table>';
227 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
230 global $user, $langs, $conf, $mysoc, $hookmanager;
232 if (empty($srctemplatepath)) {
233 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
238 if (!is_object($hookmanager)) {
239 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
242 $hookmanager->initHooks(array(
'odtgeneration'));
245 if (!is_object($outputlangs)) {
246 $outputlangs = $langs;
248 $sav_charset_output = $outputlangs->charset_output;
249 $outputlangs->charset_output =
'UTF-8';
252 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
254 if ($conf->user->dir_output) {
256 if (!is_object($object)) {
259 $result = $object->fetch($id);
266 $dir = $conf->user->dir_output.
'/usergroups';
268 if (!preg_match(
'/specimen/i', $objectref)) {
269 $dir .=
"/".$objectref;
271 $file = $dir.
"/".$objectref.
".odt";
273 if (!file_exists($dir)) {
275 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
280 if (file_exists($dir)) {
282 $newfile = basename($srctemplatepath);
283 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
284 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
285 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
287 $newfiletmp = $objectref .
'_' . $newfiletmp;
290 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
291 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
292 $format = $conf->global->MAIN_DOC_USE_TIMING;
293 if ($format ==
'1') {
294 $format =
'%Y%m%d%H%M%S';
298 $filename = $newfiletmp .
'.' . $newfileformat;
300 $file = $dir .
'/' . $filename;
307 if (!is_writable($conf->user->dir_temp)) {
308 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->user->dir_temp);
309 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
315 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
316 if (count($arrayidcontact) > 0) {
318 $result = $object->fetch_contact($arrayidcontact[0]);
322 if (!empty($usecontact)) {
324 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))) {
325 $object->contact->fetch_thirdparty();
326 $socobject = $object->contact->thirdparty;
327 $contactobject = $object->contact;
329 $socobject = $object->thirdparty;
331 $contactobject = $object->contact;
334 $socobject = $object->thirdparty;
337 $substitutionarray = array(
338 '__FROM_NAME__' => $this->emetteur->name,
339 '__FROM_EMAIL__' => $this->emetteur->email,
340 '__TOTAL_TTC__' => $object->total_ttc,
341 '__TOTAL_HT__' => $object->total_ht,
342 '__TOTAL_VAT__' => $object->total_tva
346 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
347 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
351 $paramfreetext =
'user_FREE_TEXT';
352 if (!empty($conf->global->$paramfreetext)) {
357 require_once ODTPHP_PATH.
'odf.php';
359 $odfHandler =
new odf(
362 'PATH_TO_TMP' => $conf->user->dir_temp,
363 'ZIP_PROXY' =>
'PclZipProxy',
364 'DELIMITER_LEFT' =>
'{',
365 'DELIMITER_RIGHT' =>
'}'
369 $this->error = $e->getMessage();
382 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
383 }
catch (OdfException $e) {
395 $array_thirdparty_contact = array();
396 if ($usecontact && is_object($contactobject)) {
400 $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
402 $object->fetch_optionals();
404 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
405 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
406 foreach ($tmparray as $key => $value) {
408 if (preg_match(
'/logo$/', $key)) {
409 if (file_exists($value)) {
410 $odfHandler->setImage($key, $value);
412 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
416 $odfHandler->setVars($key, $value,
true,
'UTF-8');
418 }
catch (OdfException $e) {
424 $foundtagforlines = 1;
426 $listlines = $odfHandler->setSegment(
'lines');
427 }
catch (OdfException $e) {
429 $foundtagforlines = 0;
432 if ($foundtagforlines) {
433 foreach ($object->members as $u) {
435 unset($tmparray[
'object_pass']);
436 unset($tmparray[
'object_pass_indatabase']);
439 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$u);
440 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
441 foreach ($tmparray as $key => $val) {
443 if (!is_array($val)) {
444 $listlines->setVars($key, $val,
true,
'UTF-8');
446 }
catch (OdfException $e) {
448 }
catch (SegmentException $e) {
454 $odfHandler->mergeSegment($listlines);
456 }
catch (OdfException $e) {
457 $this->error = $e->getMessage();
463 $tmparray = $outputlangs->get_translations_for_substitutions();
464 foreach ($tmparray as $key => $value) {
466 $odfHandler->setVars($key, $value,
true,
'UTF-8');
467 }
catch (OdfException $e) {
473 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
474 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
477 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
479 $odfHandler->exportAsAttachedPDF($file);
481 $this->error = $e->getMessage();
487 $odfHandler->saveToDisk($file);
489 $this->error = $e->getMessage();
495 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
497 if (!empty($conf->global->MAIN_UMASK)) {
498 @chmod($file, octdec($conf->global->MAIN_UMASK));
503 $this->result = array(
'fullpath'=>$file);
507 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);