28 require_once DOL_DOCUMENT_ROOT.
'/core/modules/propale/modules_propale.php';
29 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/lib/signature.lib.php';
51 public $phpmin = array(7, 0);
56 public $version =
'dolibarr';
66 global $conf, $langs, $mysoc;
69 $langs->loadLangs(array(
"main",
"companies"));
72 $this->
name =
"ODT templates";
73 $this->
description = $langs->trans(
"DocumentModelOdt");
74 $this->update_main_doc_field = 1;
75 $this->scandir =
'PROPALE_ADDON_PDF_ODT_PATH';
79 $this->page_largeur = 0;
80 $this->page_hauteur = 0;
81 $this->format = array($this->page_largeur, $this->page_hauteur);
82 $this->marge_gauche = 0;
83 $this->marge_droite = 0;
84 $this->marge_haute = 0;
85 $this->marge_basse = 0;
87 $this->option_logo = 1;
88 $this->option_tva = 0;
89 $this->option_modereg = 0;
90 $this->option_condreg = 0;
91 $this->option_multilang = 1;
92 $this->option_escompte = 0;
93 $this->option_credit_note = 0;
94 $this->option_freetext = 1;
95 $this->option_draft_watermark = 0;
98 $this->emetteur = $mysoc;
99 if (!$this->emetteur->country_code) {
100 $this->emetteur->country_code = substr($langs->defaultlang, -2);
113 global $conf, $langs;
116 $langs->loadLangs(array(
"errors",
"companies"));
124 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
125 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
126 $texte .=
'<input type="hidden" name="page_y" value="">';
127 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
128 $texte .=
'<input type="hidden" name="param1" value="PROPALE_ADDON_PDF_ODT_PATH">';
130 $texte .=
'<input type="hidden" name="param2" value="PROPALE_ADDON_PDF_ODT_DEFAULT">';
131 $texte .=
'<input type="hidden" name="param3" value="PROPALE_ADDON_PDF_ODT_TOBILL">';
132 $texte .=
'<input type="hidden" name="param4" value="PROPALE_ADDON_PDF_ODT_CLOSED">';
134 $texte .=
'<table class="nobordernopadding centpercent">';
137 $texte .=
'<tr><td>';
138 $texttitle = $langs->trans(
"ListOfDirectories");
139 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
140 $listoffiles = array();
141 foreach ($listofdir as $key => $tmpdir) {
142 $tmpdir = trim($tmpdir);
143 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
145 unset($listofdir[$key]);
148 if (!is_dir($tmpdir)) {
149 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
151 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
152 if (count($tmpfiles)) {
153 $listoffiles = array_merge($listoffiles, $tmpfiles);
157 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
159 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
160 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
162 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
163 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
164 $texte .=
'<textarea class="flat" cols="60" name="value1">';
166 $texte .=
'</textarea>';
167 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
168 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
169 $texte .=
'<br></div></div>';
172 $nbofiles = count($listoffiles);
173 if (!empty($odtPath)) {
174 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
176 $texte .= count($listoffiles);
182 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
184 foreach ($listoffiles as $file) {
185 $texte .=
'- '.$file[
'name'];
186 $texte .=
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=proposals/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
187 $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>';
196 $texte .=
'<table width="50%">';
198 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
199 $texte .=
'<td colspan="">';
201 $texte .=
"</td></tr>";
204 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
205 $texte .=
'<td colspan="">';
207 $texte .=
"</td></tr>";
210 $texte .=
'<td width="60%">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
211 $texte .=
'<td colspan="">';
213 $texte .=
"</td></tr>";
214 $texte .=
'</table>';
218 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
220 $maxmin = $maxfilesizearray[
'maxmin'];
222 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
224 $texte .=
' <input type="file" name="uploadfile">';
225 $texte .=
'<input type="hidden" value="PROPALE_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
226 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
230 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
231 $texte .=
'<span class="opacitymedium">';
232 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
237 $texte .=
'</table>';
255 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
258 global $user, $langs, $conf, $mysoc, $hookmanager;
260 if (empty($srctemplatepath)) {
261 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
266 if (!is_object($hookmanager)) {
267 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
270 $hookmanager->initHooks(array(
'odtgeneration'));
273 if (!is_object($outputlangs)) {
274 $outputlangs = $langs;
276 $sav_charset_output = $outputlangs->charset_output;
277 $outputlangs->charset_output =
'UTF-8';
280 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
282 if ($conf->propal->multidir_output[$conf->entity]) {
284 if (!is_object($object)) {
287 $result = $object->fetch($id);
294 $object->fetch_thirdparty();
296 $dir = $conf->propal->multidir_output[$object->entity];
298 if (!preg_match(
'/specimen/i', $objectref)) {
299 $dir .=
"/".$objectref;
301 $file = $dir.
"/".$objectref.
".odt";
303 if (!file_exists($dir)) {
305 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
310 if (file_exists($dir)) {
312 $newfile = basename($srctemplatepath);
313 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
314 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
315 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
317 $newfiletmp = $objectref .
'_' . $newfiletmp;
320 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
321 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
322 $format = $conf->global->MAIN_DOC_USE_TIMING;
323 if ($format ==
'1') {
324 $format =
'%Y%m%d%H%M%S';
328 $filename = $newfiletmp .
'.' . $newfileformat;
330 $file = $dir .
'/' . $filename;
336 dol_mkdir($conf->propal->multidir_temp[$object->entity]);
337 if (!is_writable($conf->propal->dir_temp)) {
338 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->propal->dir_temp);
339 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
345 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
346 if (count($arrayidcontact) > 0) {
348 $result = $object->fetch_contact($arrayidcontact[0]);
352 $contactobject =
null;
353 if (!empty($usecontact)) {
355 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
356 $object->contact->fetch_thirdparty();
357 $socobject = $object->contact->thirdparty;
358 $contactobject = $object->contact;
360 $socobject = $object->thirdparty;
362 $contactobject = $object->contact;
365 $socobject = $object->thirdparty;
368 $substitutionarray = array(
369 '__FROM_NAME__' => $this->emetteur->name,
370 '__FROM_EMAIL__' => $this->emetteur->email,
371 '__TOTAL_TTC__' => $object->total_ttc,
372 '__TOTAL_HT__' => $object->total_ht,
373 '__TOTAL_VAT__' => $object->total_tva
377 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
378 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
382 $paramfreetext =
'PROPOSAL_FREE_TEXT';
383 if (!empty($conf->global->$paramfreetext)) {
388 require_once ODTPHP_PATH.
'odf.php';
390 $odfHandler =
new odf(
393 'PATH_TO_TMP' => $conf->propal->multidir_temp[$object->entity],
394 'ZIP_PROXY' =>
'PclZipProxy',
395 'DELIMITER_LEFT' =>
'{',
396 'DELIMITER_RIGHT' =>
'}'
400 $this->error = $e->getMessage();
410 $object->fetch_optionals();
414 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
415 }
catch (OdfException $e) {
428 $array_thirdparty_contact = array();
429 if ($usecontact && is_object($contactobject)) {
433 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
437 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
438 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
440 foreach ($tmparray as $key => $value) {
442 if (preg_match(
'/logo$/', $key)) {
443 if (file_exists($value)) {
444 $odfHandler->setImage($key, $value);
446 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
450 $odfHandler->setVars($key, $value,
true,
'UTF-8');
452 }
catch (OdfException $e) {
458 $foundtagforlines = 1;
460 $listlines = $odfHandler->setSegment(
'lines');
461 }
catch (OdfException $e) {
463 $foundtagforlines = 0;
466 if ($foundtagforlines) {
468 foreach ($object->lines as $line) {
473 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
474 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
475 foreach ($tmparray as $key => $val) {
477 $listlines->setVars($key, $val,
true,
'UTF-8');
478 }
catch (OdfException $e) {
480 }
catch (SegmentException $e) {
486 $odfHandler->mergeSegment($listlines);
488 }
catch (OdfException $e) {
489 $this->error = $e->getMessage();
495 $tmparray = $outputlangs->get_translations_for_substitutions();
496 foreach ($tmparray as $key => $value) {
498 $odfHandler->setVars($key, $value,
true,
'UTF-8');
499 }
catch (OdfException $e) {
505 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
506 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
509 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
511 $odfHandler->exportAsAttachedPDF($file);
513 $this->error = $e->getMessage();
519 $odfHandler->saveToDisk($file);
521 $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);