47 public $version =
'dolibarr';
57 global
$conf, $langs, $mysoc;
60 $langs->loadLangs(array(
"main",
"companies"));
63 $this->
name =
"ODT templates";
64 $this->
description = $langs->trans(
"DocumentModelOdt");
65 $this->update_main_doc_field = 1;
66 $this->scandir =
'MRP_MO_ADDON_PDF_ODT_PATH';
70 $this->page_largeur = 0;
71 $this->page_hauteur = 0;
72 $this->format = array($this->page_largeur, $this->page_hauteur);
73 $this->marge_gauche = 0;
74 $this->marge_droite = 0;
75 $this->marge_haute = 0;
76 $this->marge_basse = 0;
78 $this->option_logo = 1;
79 $this->option_tva = 0;
80 $this->option_modereg = 0;
81 $this->option_condreg = 0;
82 $this->option_multilang = 1;
83 $this->option_escompte = 0;
84 $this->option_credit_note = 0;
85 $this->option_freetext = 1;
86 $this->option_draft_watermark = 0;
88 if ($mysoc ===
null) {
94 $this->emetteur = $mysoc;
95 if (!$this->emetteur->country_code) {
96 $this->emetteur->country_code = substr($langs->defaultlang, -2);
109 global
$conf, $langs;
112 $langs->loadLangs(array(
"errors",
"companies"));
114 $form =
new Form($this->db);
117 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
118 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
119 $texte .=
'<input type="hidden" name="page_y" value="">';
120 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
121 $texte .=
'<input type="hidden" name="param1" value="MRP_MO_ADDON_PDF_ODT_PATH">';
122 $texte .=
'<table class="nobordernopadding" width="100%">';
125 $texte .=
'<tr><td>';
126 $texttitle = $langs->trans(
"ListOfDirectories");
127 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
$conf->global->MRP_MO_ADDON_PDF_ODT_PATH)));
128 $listoffiles = array();
129 foreach ($listofdir as $key => $tmpdir) {
130 $tmpdir = trim($tmpdir);
131 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
133 unset($listofdir[$key]);
136 if (!is_dir($tmpdir)) {
137 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
139 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
140 if (count($tmpfiles)) {
141 $listoffiles = array_merge($listoffiles, $tmpfiles);
145 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
146 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
148 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
149 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
151 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
152 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
153 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
155 $texte .=
'</textarea>';
156 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
157 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
158 $texte .=
'<br></div></div>';
161 $nbofiles = count($listoffiles);
163 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
165 $texte .= count($listoffiles);
171 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
173 foreach ($listoffiles as $file) {
174 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=mrps/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
175 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=MRP_MO_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
185 $texte .=
'</table>';
203 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
206 global $user, $langs,
$conf, $mysoc, $hookmanager;
208 if (empty($srctemplatepath)) {
209 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
214 if (!is_object($hookmanager)) {
215 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
218 $hookmanager->initHooks(array(
'odtgeneration'));
221 if (!is_object($outputlangs)) {
222 $outputlangs = $langs;
224 $sav_charset_output = $outputlangs->charset_output;
225 $outputlangs->charset_output =
'UTF-8';
227 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
229 if (
$conf->mrp->dir_output) {
245 if (!preg_match(
'/specimen/i', $objectref)) {
246 $dir .=
"/".$objectref;
248 $file = $dir.
"/".$objectref.
".odt";
250 if (!file_exists($dir)) {
252 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
257 if (file_exists($dir)) {
259 $newfile = basename($srctemplatepath);
260 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
261 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
262 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
263 $newfiletmp = $objectref .
'_' . $newfiletmp;
266 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
269 if ($format ==
'1') {
270 $format =
'%Y%m%d%H%M%S';
274 $filename = $newfiletmp .
'.' . $newfileformat;
276 $file = $dir .
'/' . $filename;
283 if (!is_writable(
$conf->mrp->dir_temp)) {
284 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->mrp->dir_temp);
285 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
291 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
292 if (count($arrayidcontact) > 0) {
294 $result =
$object->fetch_contact($arrayidcontact[0]);
298 $contactobject =
null;
299 if (!empty($usecontact)) {
302 $object->contact->fetch_thirdparty();
303 $socobject =
$object->contact->thirdparty;
304 $contactobject =
$object->contact;
306 $socobject =
$object->thirdparty;
308 $contactobject =
$object->contact;
311 $socobject =
$object->thirdparty;
315 $substitutionarray = array(
316 '__QTY_TO_PRODUCE__' =>
$object->qty,
320 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
321 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
325 $paramfreetext =
'MRP_MO_FREE_TEXT';
331 require_once ODTPHP_PATH.
'odf.php';
333 $odfHandler =
new Odf(
336 'PATH_TO_TMP' =>
$conf->mrp->dir_temp,
338 'DELIMITER_LEFT' =>
'{',
339 'DELIMITER_RIGHT' =>
'}'
343 $this->error = $e->getMessage();
356 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
357 }
catch (OdfException $e) {
362 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
364 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
370 $array_thirdparty_contact = array();
371 if ($usecontact && is_object($contactobject)) {
375 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
379 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
380 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
389 foreach ($tmparray as $key => $value) {
391 if (preg_match(
'/logo$/', $key)) {
393 if (file_exists($value)) {
394 $odfHandler->setImage($key, $value, $ratio);
396 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
400 $odfHandler->setVars($key, $value,
true,
'UTF-8');
402 }
catch (OdfException $e) {
408 $foundtagforlines = 1;
410 $listlines = $odfHandler->setSegment(
'lines');
411 }
catch (OdfExceptionSegmentNotFound $e) {
413 $foundtagforlines = 0;
416 if ($foundtagforlines) {
418 foreach (
$object->lines as $line) {
424 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
425 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
426 foreach ($tmparray as $key => $val) {
428 $listlines->setVars($key, $val,
true,
'UTF-8');
429 }
catch (SegmentException $e) {
436 $odfHandler->mergeSegment($listlines);
437 }
catch (OdfException $e) {
438 $this->error = $e->getMessage();
445 $tmparray = $outputlangs->get_translations_for_substitutions();
446 foreach ($tmparray as $key => $value) {
448 $odfHandler->setVars($key, $value,
true,
'UTF-8');
449 }
catch (OdfException $e) {
456 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
457 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
462 $odfHandler->exportAsAttachedPDF($file);
464 $this->error = $e->getMessage();
465 dol_syslog(
'Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
470 $odfHandler->saveToDisk($file);
472 $this->error = $e->getMessage();
473 dol_syslog(
'Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
478 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
479 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
485 $this->result = array(
'fullpath' => $file);
489 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);