30 require_once DOL_DOCUMENT_ROOT.
'/core/modules/mrp/modules_mo.php';
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';
62 global $conf, $langs, $mysoc;
65 $langs->loadLangs(array(
"main",
"companies"));
68 $this->
name =
"ODT templates";
69 $this->
description = $langs->trans(
"DocumentModelOdt");
70 $this->scandir =
'MRP_MO_ADDON_PDF_ODT_PATH';
74 $this->page_largeur = 0;
75 $this->page_hauteur = 0;
76 $this->format = array($this->page_largeur, $this->page_hauteur);
77 $this->marge_gauche = 0;
78 $this->marge_droite = 0;
79 $this->marge_haute = 0;
80 $this->marge_basse = 0;
82 $this->option_logo = 1;
83 $this->option_tva = 0;
84 $this->option_modereg = 0;
85 $this->option_condreg = 0;
86 $this->option_multilang = 1;
87 $this->option_escompte = 0;
88 $this->option_credit_note = 0;
89 $this->option_freetext = 1;
90 $this->option_draft_watermark = 0;
93 $this->emetteur = $mysoc;
94 if (!$this->emetteur->country_code) {
95 $this->emetteur->country_code = substr($langs->defaultlang, -2);
108 global $conf, $langs;
111 $langs->loadLangs(array(
"errors",
"companies"));
116 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
117 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
118 $texte .=
'<input type="hidden" name="page_y" value="">';
119 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
120 $texte .=
'<input type="hidden" name="param1" value="MRP_MO_ADDON_PDF_ODT_PATH">';
121 $texte .=
'<table class="nobordernopadding" width="100%">';
124 $texte .=
'<tr><td>';
125 $texttitle = $langs->trans(
"ListOfDirectories");
126 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->MRP_MO_ADDON_PDF_ODT_PATH)));
127 $listoffiles = array();
128 foreach ($listofdir as $key => $tmpdir) {
129 $tmpdir = trim($tmpdir);
130 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
132 unset($listofdir[$key]);
135 if (!is_dir($tmpdir)) {
136 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
138 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
139 if (count($tmpfiles)) {
140 $listoffiles = array_merge($listoffiles, $tmpfiles);
144 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
146 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
147 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
149 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
150 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
151 $texte .=
'<textarea class="flat" cols="60" name="value1">';
152 $texte .= $conf->global->MRP_MO_ADDON_PDF_ODT_PATH;
153 $texte .=
'</textarea>';
154 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
155 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
156 $texte .=
'<br></div></div>';
159 $nbofiles = count($listoffiles);
160 if (!empty($conf->global->MRP_MO_ADDON_PDF_ODT_PATH)) {
161 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
163 $texte .= count($listoffiles);
169 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
171 foreach ($listoffiles as $file) {
172 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=mrps/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
173 $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>';
181 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
182 $texte .=
'<span class="opacitymedium">';
183 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
188 $texte .=
'</table>';
206 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
209 global $user, $langs, $conf, $mysoc, $hookmanager;
211 if (empty($srctemplatepath)) {
212 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
217 if (!is_object($hookmanager)) {
218 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
221 $hookmanager->initHooks(array(
'odtgeneration'));
224 if (!is_object($outputlangs)) {
225 $outputlangs = $langs;
227 $sav_charset_output = $outputlangs->charset_output;
228 $outputlangs->charset_output =
'UTF-8';
230 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
232 if ($conf->mrp->dir_output) {
234 if (!is_object($object)) {
236 $object =
new Mo($this->db);
237 $result = $object->fetch($id);
244 $object->fetch_thirdparty();
246 $dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1];
248 if (!preg_match(
'/specimen/i', $objectref)) {
249 $dir .=
"/".$objectref;
251 $file = $dir.
"/".$objectref.
".odt";
253 if (!file_exists($dir)) {
255 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
260 if (file_exists($dir)) {
262 $newfile = basename($srctemplatepath);
263 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
264 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
265 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
266 $newfiletmp = $objectref .
'_' . $newfiletmp;
269 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
272 if ($format ==
'1') {
273 $format =
'%Y%m%d%H%M%S';
277 $filename = $newfiletmp .
'.' . $newfileformat;
279 $file = $dir .
'/' . $filename;
286 if (!is_writable($conf->mrp->dir_temp)) {
287 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mrp->dir_temp);
288 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
294 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
295 if (count($arrayidcontact) > 0) {
297 $result = $object->fetch_contact($arrayidcontact[0]);
301 $contactobject =
null;
302 if (!empty($usecontact)) {
304 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))) {
305 $object->contact->fetch_thirdparty();
306 $socobject = $object->contact->thirdparty;
307 $contactobject = $object->contact;
309 $socobject = $object->thirdparty;
311 $contactobject = $object->contact;
314 $socobject = $object->thirdparty;
318 $substitutionarray = array(
319 '__QTY_TO_PRODUCE__' => $object->qty,
323 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
324 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
328 $paramfreetext =
'MRP_MO_FREE_TEXT';
329 if (!empty($conf->global->$paramfreetext)) {
334 require_once ODTPHP_PATH.
'odf.php';
336 $odfHandler =
new Odf(
339 'PATH_TO_TMP' => $conf->mrp->dir_temp,
340 'ZIP_PROXY' =>
'PclZipProxy',
341 'DELIMITER_LEFT' =>
'{',
342 'DELIMITER_RIGHT' =>
'}'
346 $this->error = $e->getMessage();
359 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
360 }
catch (OdfException $e) {
373 $array_thirdparty_contact = array();
374 if ($usecontact && is_object($contactobject)) {
378 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
382 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
383 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
385 foreach ($tmparray as $key => $value) {
387 if (preg_match(
'/logo$/', $key)) {
389 if (file_exists($value)) {
390 $odfHandler->setImage($key, $value);
392 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
396 $odfHandler->setVars($key, $value,
true,
'UTF-8');
398 }
catch (OdfException $e) {
405 $foundtagforlines = 1;
407 $listlines = $odfHandler->setSegment(
'lines');
408 }
catch (OdfException $e) {
410 $foundtagforlines = 0;
414 if ($foundtagforlines) {
416 foreach ($object->lines as $line) {
421 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
422 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
423 foreach ($tmparray as $key => $val) {
425 $listlines->setVars($key, $val,
true,
'UTF-8');
426 }
catch (OdfException $e) {
428 }
catch (SegmentException $e) {
434 $odfHandler->mergeSegment($listlines);
436 }
catch (OdfException $e) {
437 $this->error = $e->getMessage();
443 $tmparray = $outputlangs->get_translations_for_substitutions();
444 foreach ($tmparray as $key => $value) {
446 $odfHandler->setVars($key, $value,
true,
'UTF-8');
447 }
catch (OdfException $e) {
454 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
455 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
458 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
460 $odfHandler->exportAsAttachedPDF($file);
462 $this->error = $e->getMessage();
463 dol_syslog(
'Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
468 $odfHandler->saveToDisk($file);
470 $this->error = $e->getMessage();
471 dol_syslog(
'Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
476 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
477 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
483 $this->result = array(
'fullpath'=>$file);
487 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);