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->scandir =
'MRP_MO_ADDON_PDF_ODT_PATH';
69 $this->page_largeur = 0;
70 $this->page_hauteur = 0;
71 $this->format = array($this->page_largeur, $this->page_hauteur);
72 $this->marge_gauche = 0;
73 $this->marge_droite = 0;
74 $this->marge_haute = 0;
75 $this->marge_basse = 0;
77 $this->option_logo = 1;
78 $this->option_tva = 0;
79 $this->option_modereg = 0;
80 $this->option_condreg = 0;
81 $this->option_multilang = 1;
82 $this->option_escompte = 0;
83 $this->option_credit_note = 0;
84 $this->option_freetext = 1;
85 $this->option_draft_watermark = 0;
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"));
113 $form =
new Form($this->db);
116 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
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),
'');
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");
145 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
147 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
148 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
150 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
151 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
152 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
154 $texte .=
'</textarea>';
155 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
156 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
157 $texte .=
'<br></div></div>';
160 $nbofiles = count($listoffiles);
162 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
164 $texte .= count($listoffiles);
170 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
172 foreach ($listoffiles as $file) {
173 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=mrps/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
174 $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>';
180 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
182 $maxmin = $maxfilesizearray[
'maxmin'];
184 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
186 $texte .=
' <input type="file" name="uploadfile">';
187 $texte .=
'<input type="hidden" value="MRP_MO_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
188 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
194 $texte .=
'</table>';
212 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 global $user, $langs, $conf,
$mysoc, $hookmanager;
217 if (empty($srctemplatepath)) {
218 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
223 if (!is_object($hookmanager)) {
224 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
227 $hookmanager->initHooks(array(
'odtgeneration'));
230 if (!is_object($outputlangs)) {
231 $outputlangs = $langs;
233 $sav_charset_output = $outputlangs->charset_output;
234 $outputlangs->charset_output =
'UTF-8';
236 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
238 if ($conf->mrp->dir_output) {
252 $dir = $conf->mrp->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
254 if (!preg_match(
'/specimen/i', $objectref)) {
255 $dir .=
"/".$objectref;
257 $file = $dir.
"/".$objectref.
".odt";
259 if (!file_exists($dir)) {
261 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
266 if (file_exists($dir)) {
268 $newfile = basename($srctemplatepath);
269 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
270 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
271 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
272 $newfiletmp = $objectref .
'_' . $newfiletmp;
275 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
278 if ($format ==
'1') {
279 $format =
'%Y%m%d%H%M%S';
283 $filename = $newfiletmp .
'.' . $newfileformat;
285 $file = $dir .
'/' . $filename;
292 if (!is_writable($conf->mrp->dir_temp)) {
293 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mrp->dir_temp);
294 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
300 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
301 if (count($arrayidcontact) > 0) {
303 $result =
$object->fetch_contact($arrayidcontact[0]);
307 $contactobject =
null;
308 if (!empty($usecontact)) {
310 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
311 $object->contact->fetch_thirdparty();
312 $socobject =
$object->contact->thirdparty;
313 $contactobject =
$object->contact;
315 $socobject =
$object->thirdparty;
317 $contactobject =
$object->contact;
320 $socobject =
$object->thirdparty;
324 $substitutionarray = array(
325 '__QTY_TO_PRODUCE__' =>
$object->qty,
329 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
330 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
334 $paramfreetext =
'MRP_MO_FREE_TEXT';
340 require_once ODTPHP_PATH.
'odf.php';
342 $odfHandler =
new Odf(
345 'PATH_TO_TMP' => $conf->mrp->dir_temp,
347 'DELIMITER_LEFT' =>
'{',
348 'DELIMITER_RIGHT' =>
'}'
352 $this->error = $e->getMessage();
365 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
366 }
catch (OdfException $e) {
371 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
373 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
379 $array_thirdparty_contact = array();
380 if ($usecontact && is_object($contactobject)) {
384 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
388 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
389 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
398 foreach ($tmparray as $key => $value) {
400 if (preg_match(
'/logo$/', $key)) {
402 if (file_exists($value)) {
403 $odfHandler->setImage($key, $value, $ratio);
405 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
409 $odfHandler->setVars($key, $value,
true,
'UTF-8');
411 }
catch (OdfException $e) {
417 $foundtagforlines = 1;
419 $listlines = $odfHandler->setSegment(
'lines');
420 }
catch (OdfExceptionSegmentNotFound $e) {
422 $foundtagforlines = 0;
425 if ($foundtagforlines) {
427 foreach (
$object->lines as $line) {
433 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
434 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
435 foreach ($tmparray as $key => $val) {
437 $listlines->setVars($key, $val,
true,
'UTF-8');
438 }
catch (SegmentException $e) {
445 $odfHandler->mergeSegment($listlines);
446 }
catch (OdfException $e) {
447 $this->error = $e->getMessage();
454 $tmparray = $outputlangs->get_translations_for_substitutions();
455 foreach ($tmparray as $key => $value) {
457 $odfHandler->setVars($key, $value,
true,
'UTF-8');
458 }
catch (OdfException $e) {
465 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
466 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
471 $odfHandler->exportAsAttachedPDF($file);
473 $this->error = $e->getMessage();
474 dol_syslog(
'Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
479 $odfHandler->saveToDisk($file);
481 $this->error = $e->getMessage();
482 dol_syslog(
'Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
487 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
488 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
494 $this->result = array(
'fullpath' => $file);
498 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);