31 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
35 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
52 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 =
'ASSET_ASSET_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"));
115 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
116 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
117 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
118 $texte .=
'<input type="hidden" name="param1" value="ASSET_ASSET_ADDON_PDF_ODT_PATH">';
119 $texte .=
'<table class="nobordernopadding" width="100%">';
122 $texte .=
'<tr><td>';
123 $texttitle = $langs->trans(
"ListOfDirectories");
124 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->ASSET_ASSET_ADDON_PDF_ODT_PATH)));
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), 0);
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");
144 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
145 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
147 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
148 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
149 $texte .=
'<textarea class="flat" cols="60" name="value1">';
150 $texte .= $conf->global->ASSET_ASSET_ADDON_PDF_ODT_PATH;
151 $texte .=
'</textarea>';
152 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
153 $texte .=
'<input type="submit" class="button small" name="Button"value="'.$langs->trans(
"Modify").
'">';
154 $texte .=
'<br></div></div>';
157 $nbofiles = count($listoffiles);
158 if (!empty($conf->global->ASSET_ASSET_ADDON_PDF_ODT_PATH)) {
159 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
161 $texte .= count($listoffiles);
167 $texte .=
'<div id="div_'.get_class($this).
'" class="hidden">';
168 foreach ($listoffiles as $file) {
169 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=asset_asset/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
170 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=ASSET_ASSET_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
178 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
179 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
183 $texte .=
'</table>';
201 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
204 global $user, $langs, $conf, $mysoc, $hookmanager;
206 if (empty($srctemplatepath)) {
207 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
212 if (!is_object($hookmanager)) {
213 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
216 $hookmanager->initHooks(array(
'odtgeneration'));
219 if (!is_object($outputlangs)) {
220 $outputlangs = $langs;
222 $sav_charset_output = $outputlangs->charset_output;
223 $outputlangs->charset_output =
'UTF-8';
225 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
227 if ($conf->asset->dir_output) {
229 if (!is_object($object)) {
231 $object =
new Asset($this->db);
232 $result = $object->fetch($id);
239 $object->fetch_thirdparty();
241 $dir = $conf->asset->multidir_output[isset($object->entity) ? $object->entity : 1];
243 if (!preg_match(
'/specimen/i', $objectref)) {
244 $dir .=
"/".$objectref;
246 $file = $dir.
"/".$objectref.
".odt";
248 if (!file_exists($dir)) {
250 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
255 if (file_exists($dir)) {
257 $newfile = basename($srctemplatepath);
258 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
259 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
260 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
261 $newfiletmp = $objectref.
'_'.$newfiletmp;
264 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
267 if ($format ==
'1') {
268 $format =
'%Y%m%d%H%M%S';
272 $filename = $newfiletmp.
'.'.$newfileformat;
274 $file = $dir.
'/'.$filename;
281 if (!is_writable($conf->asset->dir_temp)) {
282 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->asset->dir_temp);
283 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
289 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
290 if (count($arrayidcontact) > 0) {
292 $result = $object->fetch_contact($arrayidcontact[0]);
296 $contactobject =
null;
297 if (!empty($usecontact)) {
299 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))) {
300 $object->contact->fetch_thirdparty();
301 $socobject = $object->contact->thirdparty;
302 $contactobject = $object->contact;
304 $socobject = $object->thirdparty;
306 $contactobject = $object->contact;
309 $socobject = $object->thirdparty;
313 $substitutionarray = array(
314 '__FROM_NAME__' => $this->emetteur->name,
315 '__FROM_EMAIL__' => $this->emetteur->email,
316 '__TOTAL_TTC__' => $object->total_ttc,
317 '__TOTAL_HT__' => $object->total_ht,
318 '__TOTAL_VAT__' => $object->total_tva
322 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
323 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
327 $paramfreetext =
'ORDER_FREE_TEXT';
328 if (!empty($conf->global->$paramfreetext)) {
333 require_once ODTPHP_PATH.
'odf.php';
335 $odfHandler =
new Odf(
338 'PATH_TO_TMP' => $conf->asset->dir_temp,
339 'ZIP_PROXY' =>
'PclZipProxy',
340 'DELIMITER_LEFT' =>
'{',
341 'DELIMITER_RIGHT' =>
'}'
345 $this->error = $e->getMessage();
358 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
359 }
catch (OdfException $e) {
372 $array_thirdparty_contact = array();
373 if ($usecontact && is_object($contactobject)) {
377 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
381 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
382 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
384 foreach ($tmparray as $key => $value) {
386 if (preg_match(
'/logo$/', $key)) {
388 if (file_exists($value)) {
389 $odfHandler->setImage($key, $value);
391 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
395 $odfHandler->setVars($key, $value,
true,
'UTF-8');
397 }
catch (OdfException $e) {
403 $foundtagforlines = 1;
405 $listlines = $odfHandler->setSegment(
'lines');
406 }
catch (OdfException $e) {
408 $foundtagforlines = 0;
411 if ($foundtagforlines) {
413 foreach ($object->lines as $line) {
418 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
419 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
420 foreach ($tmparray as $key => $val) {
422 $listlines->setVars($key, $val,
true,
'UTF-8');
423 }
catch (OdfException $e) {
425 }
catch (SegmentException $e) {
431 $odfHandler->mergeSegment($listlines);
433 }
catch (OdfException $e) {
434 $this->error = $e->getMessage();
440 $tmparray = $outputlangs->get_translations_for_substitutions();
441 foreach ($tmparray as $key => $value) {
443 $odfHandler->setVars($key, $value,
true,
'UTF-8');
444 }
catch (OdfException $e) {
451 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
452 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
455 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
457 $odfHandler->exportAsAttachedPDF($file);
459 $this->error = $e->getMessage();
465 $odfHandler->saveToDisk($file);
467 $this->error = $e->getMessage();
473 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
474 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
480 $this->result = array(
'fullpath'=>$file);
484 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);