53 public $phpmin = array(7, 0);
58 public $version =
'dolibarr';
71 $langs->loadLangs(array(
"main",
"companies"));
74 $this->
name =
"ODT templates";
75 $this->
description = $langs->trans(
"DocumentModelOdt");
76 $this->update_main_doc_field = 1;
77 $this->scandir =
'PROPALE_ADDON_PDF_ODT_PATH';
81 $this->page_largeur = 0;
82 $this->page_hauteur = 0;
83 $this->format = array($this->page_largeur, $this->page_hauteur);
84 $this->marge_gauche = 0;
85 $this->marge_droite = 0;
86 $this->marge_haute = 0;
87 $this->marge_basse = 0;
89 $this->option_logo = 1;
90 $this->option_tva = 0;
91 $this->option_modereg = 0;
92 $this->option_condreg = 0;
93 $this->option_multilang = 1;
94 $this->option_escompte = 0;
95 $this->option_credit_note = 0;
96 $this->option_freetext = 1;
97 $this->option_draft_watermark = 0;
106 if (!$this->emetteur->country_code) {
107 $this->emetteur->country_code = substr($langs->defaultlang, -2);
123 $langs->loadLangs(array(
"errors",
"companies"));
125 $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="PROPALE_ADDON_PDF_ODT_PATH">';
137 $texte .=
'<input type="hidden" name="param2" value="PROPALE_ADDON_PDF_ODT_DEFAULT">';
138 $texte .=
'<input type="hidden" name="param3" value="PROPALE_ADDON_PDF_ODT_TOBILL">';
139 $texte .=
'<input type="hidden" name="param4" value="PROPALE_ADDON_PDF_ODT_CLOSED">';
141 $texte .=
'<table class="nobordernopadding centpercent">';
144 $texte .=
'<tr><td>';
145 $texttitle = $langs->trans(
"ListOfDirectories");
146 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
147 $listoffiles = array();
148 foreach ($listofdir as $key => $tmpdir) {
149 $tmpdir = trim($tmpdir);
150 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
152 unset($listofdir[$key]);
155 if (!is_dir($tmpdir)) {
156 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
158 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
159 if (count($tmpfiles)) {
160 $listoffiles = array_merge($listoffiles, $tmpfiles);
164 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
165 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
167 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
168 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
171 $nbofiles = count($listoffiles);
172 if (!empty($odtPath)) {
173 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
175 $texte .= count($listoffiles);
177 $texte .=
'</b><br>';
181 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
183 foreach ($listoffiles as $file) {
184 $texte .=
'- '.$file[
'name'];
185 $texte .=
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=proposals/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
186 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=PROPALE_ADDON_PDF_ODT_PATH&action=deletefile&token='.newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
195 $texte .=
'<table width="50%">';
197 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
198 $texte .=
'<td colspan="">';
199 $texte .= $form->selectarray(
'value2', $list,
getDolGlobalString(
'PROPALE_ADDON_PDF_ODT_DEFAULT'));
200 $texte .=
"</td></tr>";
203 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
204 $texte .=
'<td colspan="">';
205 $texte .= $form->selectarray(
'value3', $list,
getDolGlobalString(
'PROPALE_ADDON_PDF_ODT_TOBILL'));
206 $texte .=
"</td></tr>";
209 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
210 $texte .=
'<td colspan="">';
211 $texte .= $form->selectarray(
'value4', $list,
getDolGlobalString(
'PROPALE_ADDON_PDF_ODT_CLOSED'));
212 $texte .=
"</td></tr>";
213 $texte .=
'</table>';
218 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
219 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
220 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
222 $texte .=
'</textarea>';
223 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
224 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dolPrintHTMLForAttribute($langs->trans(
"Modify")).
'">';
225 $texte .=
'<br></div></div>';
228 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
230 $maxmin = $maxfilesizearray[
'maxmin'];
232 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
234 $texte .=
' <input type="file" name="uploadfile">';
235 $texte .=
'<input type="hidden" value="PROPALE_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
236 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
242 $texte .=
'</table>';
260 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
265 if (empty($srctemplatepath)) {
266 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
271 if (!is_object($hookmanager)) {
272 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
275 $hookmanager->initHooks(array(
'odtgeneration'));
278 if (!is_object($outputlangs)) {
279 $outputlangs = $langs;
281 $sav_charset_output = $outputlangs->charset_output;
282 $outputlangs->charset_output =
'UTF-8';
285 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
287 if (
$conf->propal->multidir_output[
$conf->entity]) {
303 if (!preg_match(
'/specimen/i', $objectref)) {
304 $dir .=
"/".$objectref;
306 $file = $dir.
"/".$objectref.
".odt";
308 if (!file_exists($dir)) {
310 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
315 if (file_exists($dir)) {
317 $newfile = basename($srctemplatepath);
318 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
319 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
320 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
322 $newfiletmp = $objectref .
'_' . $newfiletmp;
325 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
328 if ($format ==
'1') {
329 $format =
'%Y%m%d%H%M%S';
333 $filename = $newfiletmp .
'.' . $newfileformat;
335 $file = $dir .
'/' . $filename;
342 if (!is_writable(
$conf->propal->dir_temp)) {
343 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->propal->dir_temp);
344 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
350 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
351 if (count($arrayidcontact) > 0) {
353 $result =
$object->fetch_contact($arrayidcontact[0]);
357 $contactobject =
null;
358 if (!empty($usecontact)) {
361 $object->contact->fetch_thirdparty();
362 $socobject =
$object->contact->thirdparty;
363 $contactobject =
$object->contact;
365 $socobject =
$object->thirdparty;
367 $contactobject =
$object->contact;
370 $socobject =
$object->thirdparty;
373 $substitutionarray = array(
374 '__FROM_NAME__' => $this->emetteur->name,
375 '__FROM_EMAIL__' => $this->emetteur->email,
376 '__TOTAL_TTC__' =>
$object->total_ttc,
377 '__TOTAL_HT__' =>
$object->total_ht,
378 '__TOTAL_VAT__' =>
$object->total_tva
382 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
383 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
387 $paramfreetext =
'PROPOSAL_FREE_TEXT';
393 require_once ODTPHP_PATH.
'odf.php';
395 $odfHandler =
new Odf(
398 'PATH_TO_TMP' =>
$conf->propal->multidir_temp[
$object->entity],
400 'DELIMITER_LEFT' =>
'{',
401 'DELIMITER_RIGHT' =>
'}'
405 $this->error = $e->getMessage();
419 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
420 }
catch (OdfException $e) {
425 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
427 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
433 include_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
436 $companybankaccount->fetch(0,
'',
$object->thirdparty->id);
437 $array_objet[
'company_default_bank_iban'] = $companybankaccount->iban;
438 $array_objet[
'company_default_bank_bic'] = $companybankaccount->bic;
441 $array_thirdparty_contact = array();
442 if ($usecontact && is_object($contactobject)) {
446 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
450 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
451 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
460 foreach ($tmparray as $key => $value) {
462 if (preg_match(
'/logo$/', $key)) {
464 if (file_exists($value)) {
465 $odfHandler->setImage($key, $value, $ratio);
467 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
471 $odfHandler->setVars($key, $value,
true,
'UTF-8');
473 }
catch (OdfException $e) {
478 $foundtagforlines = 1;
480 $listlines = $odfHandler->setSegment(
'lines');
481 }
catch (OdfExceptionSegmentNotFound $e) {
483 $foundtagforlines = 0;
486 if ($foundtagforlines) {
488 foreach (
$object->lines as $line) {
494 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
495 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
496 foreach ($tmparray as $key => $val) {
498 $listlines->setVars($key, $val,
true,
'UTF-8');
499 }
catch (SegmentException $e) {
506 $odfHandler->mergeSegment($listlines);
507 }
catch (OdfException $e) {
508 $this->error = $e->getMessage();
515 $tmparray = $outputlangs->get_translations_for_substitutions();
516 foreach ($tmparray as $key => $value) {
518 $odfHandler->setVars($key, $value,
true,
'UTF-8');
519 }
catch (OdfException $e) {
525 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
526 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
531 $odfHandler->exportAsAttachedPDF($file);
533 $this->error = $e->getMessage();
539 $odfHandler->saveToDisk($file);
541 $this->error = $e->getMessage();
546 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
547 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
553 $this->result = array(
'fullpath' => $file);
557 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);