55 public $phpmin = array(7, 0);
60 public $version =
'dolibarr';
73 $langs->loadLangs(array(
"main",
"companies"));
76 $this->
name =
"ODT templates";
77 $this->
description = $langs->trans(
"DocumentModelOdt");
78 $this->scandir =
'MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH';
82 $this->page_largeur = 0;
83 $this->page_hauteur = 0;
84 $this->format = array($this->page_largeur, $this->page_hauteur);
85 $this->marge_gauche = 0;
86 $this->marge_droite = 0;
87 $this->marge_haute = 0;
88 $this->marge_basse = 0;
90 $this->option_logo = 1;
91 $this->option_tva = 0;
92 $this->option_modereg = 0;
93 $this->option_condreg = 0;
94 $this->option_multilang = 1;
95 $this->option_escompte = 0;
96 $this->option_credit_note = 0;
97 $this->option_freetext = 1;
98 $this->option_draft_watermark = 0;
107 if (!$this->emetteur->country_code) {
108 $this->emetteur->country_code = substr($langs->defaultlang, -2);
124 $langs->loadLangs(array(
"errors",
"companies"));
126 $form =
new Form($this->db);
131 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
132 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
133 $texte .=
'<input type="hidden" name="page_y" value="">';
134 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
135 $texte .=
'<input type="hidden" name="param1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
137 $texte .=
'<table class="nobordernopadding centpercent">';
140 $texte .=
'<tr><td>';
141 $texttitle = $langs->trans(
"ListOfDirectories");
142 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
143 $listoffiles = array();
144 foreach ($listofdir as $key => $tmpdir) {
145 $tmpdir = trim($tmpdir);
146 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
148 unset($listofdir[$key]);
151 if (!is_dir($tmpdir)) {
152 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
154 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
155 if (count($tmpfiles)) {
156 $listoffiles = array_merge($listoffiles, $tmpfiles);
160 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
161 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
163 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
164 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
167 $nbofiles = count($listoffiles);
169 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
171 $texte .= count($listoffiles);
177 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
179 foreach ($listoffiles as $file) {
180 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=mymodule_myobject/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
181 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
189 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
190 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
191 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
193 $texte .=
'</textarea>';
194 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
195 $texte .=
'<input type="submit" class="button button-edit smallpaddingimp reposition" name="modify" value="'.dolPrintHTMLForAttribute($langs->trans(
"Modify")).
'">';
196 $texte .=
'<br></div></div>';
199 $texte .=
'<input type="hidden" name="value1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
203 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
205 $maxmin = $maxfilesizearray[
'maxmin'];
207 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
209 $texte .=
' <input type="file" name="uploadfile">';
210 $texte .=
'<input type="hidden" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
211 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
218 $texte .=
'</table>';
236 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
239 global $user, $langs, $conf,
$mysoc, $hookmanager;
242 if (empty($srctemplatepath)) {
243 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
248 if (!is_object($hookmanager)) {
249 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
252 $hookmanager->initHooks(array(
'odtgeneration'));
255 if (!is_object($outputlangs)) {
256 $outputlangs = $langs;
258 $sav_charset_output = $outputlangs->charset_output;
259 $outputlangs->charset_output =
'UTF-8';
261 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
263 if ($conf->mymodule->dir_output) {
277 $dir = $conf->mymodule->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
279 if (!preg_match(
'/specimen/i', $objectref)) {
280 $dir .=
"/".$objectref;
282 $file = $dir.
"/".$objectref.
".odt";
284 if (!file_exists($dir)) {
286 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
291 if (file_exists($dir)) {
293 $newfile = basename($srctemplatepath);
294 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
295 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
296 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
298 $newfiletmp = $objectref .
'_' . $newfiletmp;
302 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
305 if ($format ==
'1') {
306 $format =
'%Y%m%d%H%M%S';
310 $filename = $newfiletmp .
'.' . $newfileformat;
312 $file = $dir .
'/' . $filename;
319 if (!is_writable($conf->mymodule->dir_temp)) {
320 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
321 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
327 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
328 if (count($arrayidcontact) > 0) {
330 $result =
$object->fetch_contact($arrayidcontact[0]);
334 $contactobject =
null;
335 if (!empty($usecontact)) {
337 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
338 $object->contact->fetch_thirdparty();
339 $socobject =
$object->contact->thirdparty;
340 $contactobject =
$object->contact;
342 $socobject =
$object->thirdparty;
344 $contactobject =
$object->contact;
347 $socobject =
$object->thirdparty;
351 $substitutionarray = array(
352 '__FROM_NAME__' => $this->emetteur->name,
353 '__FROM_EMAIL__' => $this->emetteur->email,
354 '__TOTAL_TTC__' =>
$object->total_ttc,
355 '__TOTAL_HT__' =>
$object->total_ht,
356 '__TOTAL_VAT__' =>
$object->total_tva
360 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
361 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
365 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
371 require_once ODTPHP_PATH.
'odf.php';
373 $odfHandler =
new Odf(
376 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
378 'DELIMITER_LEFT' =>
'{',
379 'DELIMITER_RIGHT' =>
'}'
383 $this->error = $e->getMessage();
397 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
398 }
catch (OdfException $e) {
403 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
405 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
411 $array_thirdparty_contact = array();
412 if ($usecontact && is_object($contactobject)) {
416 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
420 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
421 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
430 foreach ($tmparray as $key => $value) {
432 if (preg_match(
'/logo$/', $key)) {
434 if (file_exists($value)) {
435 $odfHandler->setImage($key, $value, $ratio);
437 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
441 $odfHandler->setVars($key, $value,
true,
'UTF-8');
443 }
catch (OdfException $e) {
448 $foundtagforlines = 1;
450 $listlines = $odfHandler->setSegment(
'lines');
451 }
catch (OdfExceptionSegmentNotFound $e) {
453 $foundtagforlines = 0;
457 if ($foundtagforlines) {
459 foreach (
$object->lines as $line) {
466 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
467 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
468 foreach ($tmparray as $key => $val) {
470 $listlines->setVars($key, $val,
true,
'UTF-8');
471 }
catch (SegmentException $e) {
478 $odfHandler->mergeSegment($listlines);
479 }
catch (OdfException $e) {
480 $this->error = $e->getMessage();
487 $tmparray = $outputlangs->get_translations_for_substitutions();
488 foreach ($tmparray as $key => $value) {
490 $odfHandler->setVars($key, $value,
true,
'UTF-8');
491 }
catch (OdfException $e) {
498 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
499 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
504 $odfHandler->exportAsAttachedPDF($file);
506 $this->error = $e->getMessage();
512 $odfHandler->saveToDisk($file);
514 $this->error = $e->getMessage();
520 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
521 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
527 $this->result = array(
'fullpath' => $file);
531 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);