56 public $phpmin = array(7, 0);
61 public $version =
'dolibarr';
74 $langs->loadLangs(array(
"main",
"companies"));
77 $this->
name =
"ODT templates";
78 $this->
description = $langs->trans(
"DocumentModelOdt");
79 $this->scandir =
'MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH';
83 $this->page_largeur = 0;
84 $this->page_hauteur = 0;
85 $this->format = array($this->page_largeur, $this->page_hauteur);
86 $this->marge_gauche = 0;
87 $this->marge_droite = 0;
88 $this->marge_haute = 0;
89 $this->marge_basse = 0;
91 $this->option_logo = 1;
92 $this->option_tva = 0;
93 $this->option_modereg = 0;
94 $this->option_condreg = 0;
95 $this->option_multilang = 1;
96 $this->option_escompte = 0;
97 $this->option_credit_note = 0;
98 $this->option_freetext = 1;
99 $this->option_draft_watermark = 0;
108 if (!$this->emetteur->country_code) {
109 $this->emetteur->country_code =
dol_substr($langs->defaultlang, -2);
125 $langs->loadLangs(array(
"errors",
"companies"));
127 $form =
new Form($this->db);
132 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
133 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
134 $texte .=
'<input type="hidden" name="page_y" value="">';
135 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
136 $texte .=
'<input type="hidden" name="param1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
138 $texte .=
'<table class="nobordernopadding centpercent">';
141 $texte .=
'<tr><td>';
142 $texttitle = $langs->trans(
"ListOfDirectories");
143 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
144 $listoffiles = array();
145 foreach ($listofdir as $key => $tmpdir) {
146 $tmpdir = trim($tmpdir);
147 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
149 unset($listofdir[$key]);
153 $texttitle .= img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
155 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
156 if (count($tmpfiles)) {
157 $listoffiles = array_merge($listoffiles, $tmpfiles);
161 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
162 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
164 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
165 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
168 $nbofiles = count($listoffiles);
170 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
172 $texte .= count($listoffiles);
178 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
180 foreach ($listoffiles as $file) {
181 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=mymodule_myobject/'.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'listlight').
'</a>';
182 $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>';
190 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
191 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
192 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
194 $texte .=
'</textarea>';
195 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
196 $texte .=
'<input type="submit" class="button button-edit smallpaddingimp reposition" name="modify" value="'.dolPrintHTMLForAttribute($langs->trans(
"Modify")).
'">';
197 $texte .=
'<br></div></div>';
200 $texte .=
'<input type="hidden" name="value1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
204 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
206 $maxmin = $maxfilesizearray[
'maxmin'];
208 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
210 $texte .=
' <input type="file" name="uploadfile">';
211 $texte .=
'<input type="hidden" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
212 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
219 $texte .=
'</table>';
237 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
243 if (empty($srctemplatepath)) {
244 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
249 if (!is_object($hookmanager)) {
250 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
253 $hookmanager->initHooks(array(
'odtgeneration'));
256 if (!is_object($outputlangs)) {
257 $outputlangs = $langs;
259 $sav_charset_output = $outputlangs->charset_output;
260 $outputlangs->charset_output =
'UTF-8';
262 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
264 if (
$conf->mymodule->dir_output) {
271 dol_print_error($this->db,
$object->error);
280 if (!preg_match(
'/specimen/i', $objectref)) {
281 $dir .=
"/".$objectref;
283 $file = $dir.
"/".$objectref.
".odt";
285 if (!file_exists($dir)) {
287 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
292 if (file_exists($dir)) {
294 $newfile = basename($srctemplatepath);
295 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
296 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
297 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
299 $newfiletmp = $objectref .
'_' . $newfiletmp;
303 $newfileformat =
dol_substr($newfile, strrpos($newfile,
'.') + 1);
306 if ($format ==
'1') {
307 $format =
'%Y%m%d%H%M%S';
311 $filename = $newfiletmp .
'.' . $newfileformat;
313 $file = $dir .
'/' . $filename;
321 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->mymodule->dir_temp);
322 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
328 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
329 if (count($arrayidcontact) > 0) {
331 $result =
$object->fetch_contact($arrayidcontact[0]);
335 $contactobject =
null;
336 if (!empty($usecontact)) {
338 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset(
$conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
339 $object->contact->fetch_thirdparty();
340 $socobject =
$object->contact->thirdparty;
341 $contactobject =
$object->contact;
343 $socobject =
$object->thirdparty;
345 $contactobject =
$object->contact;
348 $socobject =
$object->thirdparty;
352 $substitutionarray = array(
353 '__FROM_NAME__' => $this->emetteur->name,
354 '__FROM_EMAIL__' => $this->emetteur->email,
355 '__TOTAL_TTC__' =>
$object->total_ttc,
356 '__TOTAL_HT__' =>
$object->total_ht,
357 '__TOTAL_VAT__' =>
$object->total_tva
361 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
362 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
366 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
372 require_once ODTPHP_PATH.
'odf.php';
374 $odfHandler =
new Odf(
377 'PATH_TO_TMP' =>
$conf->mymodule->dir_temp,
379 'DELIMITER_LEFT' =>
'{',
380 'DELIMITER_RIGHT' =>
'}'
384 $this->error = $e->getMessage();
398 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
399 }
catch (OdfException $e) {
404 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
406 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
412 $array_thirdparty_contact = array();
413 if ($usecontact && is_object($contactobject)) {
417 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
421 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
422 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
431 foreach ($tmparray as $key => $value) {
433 if (preg_match(
'/logo$/', $key)) {
435 if (file_exists($value)) {
436 $odfHandler->setImage($key, $value, $ratio);
438 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
442 $odfHandler->setVars($key, $value,
true,
'UTF-8');
444 }
catch (OdfException $e) {
449 $foundtagforlines = 1;
451 $listlines = $odfHandler->setSegment(
'lines');
452 }
catch (OdfExceptionSegmentNotFound $e) {
454 $foundtagforlines = 0;
458 if ($foundtagforlines) {
460 foreach (
$object->lines as $line) {
467 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
468 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
469 foreach ($tmparray as $key => $val) {
471 $listlines->setVars($key, $val,
true,
'UTF-8');
472 }
catch (SegmentException $e) {
479 $odfHandler->mergeSegment($listlines);
480 }
catch (OdfException $e) {
481 $this->error = $e->getMessage();
488 $tmparray = $outputlangs->get_translations_for_substitutions();
489 foreach ($tmparray as $key => $value) {
491 $odfHandler->setVars($key, $value,
true,
'UTF-8');
492 }
catch (OdfException $e) {
499 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
500 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
505 $odfHandler->exportAsAttachedPDF($file);
507 $this->error = $e->getMessage();
513 $odfHandler->saveToDisk($file);
515 $this->error = $e->getMessage();
521 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
522 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
528 $this->result = array(
'fullpath' => $file);
532 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);