56 public $phpmin = array(7, 0);
61 public $version =
'dolibarr';
66 public $position = 60;
79 $langs->loadLangs(array(
"main",
"companies"));
82 $this->
name =
"ODT templates";
83 $this->
description = $langs->trans(
"DocumentModelOdt");
84 $this->scandir =
'MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH';
88 $this->page_largeur = 0;
89 $this->page_hauteur = 0;
90 $this->format = array($this->page_largeur, $this->page_hauteur);
91 $this->marge_gauche = 0;
92 $this->marge_droite = 0;
93 $this->marge_haute = 0;
94 $this->marge_basse = 0;
96 $this->option_logo = 1;
97 $this->option_tva = 0;
98 $this->option_modereg = 0;
99 $this->option_condreg = 0;
100 $this->option_multilang = 1;
101 $this->option_escompte = 0;
102 $this->option_credit_note = 0;
103 $this->option_freetext = 1;
104 $this->option_draft_watermark = 0;
113 if (!$this->emetteur->country_code) {
114 $this->emetteur->country_code =
dol_substr($langs->defaultlang, -2);
130 $langs->loadLangs(array(
"errors",
"companies"));
132 $form =
new Form($this->db);
137 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
138 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
139 $texte .=
'<input type="hidden" name="page_y" value="">';
140 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
141 $texte .=
'<input type="hidden" name="param1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
143 $texte .=
'<table class="nobordernopadding centpercent">';
146 $texte .=
'<tr><td>';
147 $texttitle = $langs->trans(
"ListOfDirectories");
148 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
149 $listoffiles = array();
150 foreach ($listofdir as $key => $tmpdir) {
151 $tmpdir = trim($tmpdir);
152 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
154 unset($listofdir[$key]);
158 $texttitle .= img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
160 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
161 if (count($tmpfiles)) {
162 $listoffiles = array_merge($listoffiles, $tmpfiles);
166 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
167 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
169 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
170 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
173 $nbofiles = count($listoffiles);
175 $texte .=
'<span class="opacitylow">'.$langs->trans(
"NumberOfModelFilesFound").
': <b>';
177 $texte .= count($listoffiles);
179 $texte .=
'</b></span>';
183 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx opacitylow">';
185 foreach ($listoffiles as $file) {
186 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=mymodule_myobject/'.urlencode(basename($file[
'name'])).
'">'.img_picto(
'',
'listlight').
'</a>';
187 $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>';
195 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
196 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
197 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
199 $texte .=
'</textarea>';
200 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
201 $texte .=
'<input type="submit" class="button button-edit smallpaddingimp reposition" name="modify" value="'.dolPrintHTMLForAttribute($langs->trans(
"Modify")).
'">';
202 $texte .=
'<br></div></div>';
205 $texte .=
'<input type="hidden" name="value1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
209 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
211 $maxmin = $maxfilesizearray[
'maxmin'];
213 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
215 $texte .=
' <input type="file" name="uploadfile">';
216 $texte .=
'<input type="hidden" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
217 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
224 $texte .=
'</table>';
242 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
248 if (empty($srctemplatepath)) {
249 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
254 if (!is_object($hookmanager)) {
255 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
258 $hookmanager->initHooks(array(
'odtgeneration'));
261 if (!is_object($outputlangs)) {
262 $outputlangs = $langs;
264 $sav_charset_output = $outputlangs->charset_output;
265 $outputlangs->charset_output =
'UTF-8';
267 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
269 if (
$conf->mymodule->dir_output) {
276 dol_print_error($this->db,
$object->error);
285 if (!preg_match(
'/specimen/i', $objectref)) {
286 $dir .=
"/".$objectref;
288 $file = $dir.
"/".$objectref.
".odt";
290 if (!file_exists($dir)) {
292 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
297 if (file_exists($dir)) {
299 $newfile = basename($srctemplatepath);
300 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
301 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
302 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
304 $newfiletmp = $objectref .
'_' . $newfiletmp;
308 $newfileformat =
dol_substr($newfile, strrpos($newfile,
'.') + 1);
311 if ($format ==
'1') {
312 $format =
'%Y%m%d%H%M%S';
316 $filename = $newfiletmp .
'.' . $newfileformat;
318 $file = $dir .
'/' . $filename;
326 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->mymodule->dir_temp);
327 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
333 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
334 if (count($arrayidcontact) > 0) {
336 $result =
$object->fetch_contact($arrayidcontact[0]);
340 $contactobject =
null;
341 if (!empty($usecontact)) {
343 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset(
$conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
344 $object->contact->fetch_thirdparty();
345 $socobject =
$object->contact->thirdparty;
346 $contactobject =
$object->contact;
348 $socobject =
$object->thirdparty;
350 $contactobject =
$object->contact;
353 $socobject =
$object->thirdparty;
357 $substitutionarray = array(
358 '__FROM_NAME__' => $this->emetteur->name,
359 '__FROM_EMAIL__' => $this->emetteur->email,
360 '__TOTAL_TTC__' =>
$object->total_ttc,
361 '__TOTAL_HT__' =>
$object->total_ht,
362 '__TOTAL_VAT__' =>
$object->total_tva
366 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
367 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
371 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
377 require_once ODTPHP_PATH.
'odf.php';
379 $odfHandler =
new Odf(
382 'PATH_TO_TMP' =>
$conf->mymodule->dir_temp,
384 'DELIMITER_LEFT' =>
'{',
385 'DELIMITER_RIGHT' =>
'}'
389 $this->error = $e->getMessage();
403 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
404 }
catch (OdfException $e) {
409 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
411 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
417 $array_thirdparty_contact = array();
418 if ($usecontact && is_object($contactobject)) {
422 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
426 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
427 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
436 foreach ($tmparray as $key => $value) {
438 if (preg_match(
'/logo$/', $key)) {
440 if (file_exists($value)) {
441 $odfHandler->setImage($key, $value, $ratio);
443 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
447 $odfHandler->setVars($key, $value,
true,
'UTF-8');
449 }
catch (OdfException $e) {
454 $foundtagforlines = 1;
456 $listlines = $odfHandler->setSegment(
'lines');
457 }
catch (OdfExceptionSegmentNotFound $e) {
459 $foundtagforlines = 0;
463 if ($foundtagforlines) {
465 foreach (
$object->lines as $line) {
472 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
473 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
474 foreach ($tmparray as $key => $val) {
476 $listlines->setVars($key, $val,
true,
'UTF-8');
477 }
catch (SegmentException $e) {
484 $odfHandler->mergeSegment($listlines);
485 }
catch (OdfException $e) {
486 $this->error = $e->getMessage();
493 $tmparray = $outputlangs->get_translations_for_substitutions();
494 foreach ($tmparray as $key => $value) {
496 $odfHandler->setVars($key, $value,
true,
'UTF-8');
497 }
catch (OdfException $e) {
504 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
505 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
510 $odfHandler->exportAsAttachedPDF($file);
512 $this->error = $e->getMessage();
518 $odfHandler->saveToDisk($file);
520 $this->error = $e->getMessage();
526 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
527 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
533 $this->result = array(
'fullpath' => $file);
537 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined: