28 require_once DOL_DOCUMENT_ROOT.
'/core/modules/contract/modules_contract.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';
51 public $phpmin = array(5, 6);
57 public $version =
'dolibarr';
67 global $conf, $langs, $mysoc;
70 $langs->loadLangs(array(
"main",
"companies"));
73 $this->
name =
"ODT templates";
74 $this->
description = $langs->trans(
"DocumentModelOdt");
75 $this->scandir =
'CONTRACT_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(
'companies',
'errors'));
121 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
122 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
123 $texte .=
'<input type="hidden" name="page_y" value="">';
124 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
125 $texte .=
'<input type="hidden" name="param1" value="CONTRACT_ADDON_PDF_ODT_PATH">';
126 $texte .=
'<table class="nobordernopadding" width="100%">';
129 $texte .=
'<tr><td>';
130 $texttitle = $langs->trans(
"ListOfDirectories");
131 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->CONTRACT_ADDON_PDF_ODT_PATH)));
132 $listoffiles = array();
133 foreach ($listofdir as $key => $tmpdir) {
134 $tmpdir = trim($tmpdir);
135 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
137 unset($listofdir[$key]);
140 if (!is_dir($tmpdir)) {
141 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
143 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
144 if (count($tmpfiles)) {
145 $listoffiles = array_merge($listoffiles, $tmpfiles);
149 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
151 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
152 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
154 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
155 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
156 $texte .=
'<textarea class="flat" cols="60" name="value1">';
157 $texte .= $conf->global->CONTRACT_ADDON_PDF_ODT_PATH;
158 $texte .=
'</textarea>';
159 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
160 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'" name="Button">';
161 $texte .=
'<br></div></div>';
164 if (count($listofdir)) {
165 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
169 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
171 $maxmin = $maxfilesizearray[
'maxmin'];
173 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
175 $texte .=
' <input type="file" name="uploadfile">';
176 $texte .=
'<input type="hidden" value="CONTRACT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
177 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
182 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
183 $texte .=
'<span class="opacitymedium">';
184 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
189 $texte .=
'</table>';
207 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
210 global $user, $langs, $conf, $mysoc, $hookmanager;
212 if (empty($srctemplatepath)) {
213 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
218 if (!is_object($hookmanager)) {
219 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
222 $hookmanager->initHooks(array(
'odtgeneration'));
225 if (!is_object($outputlangs)) {
226 $outputlangs = $langs;
228 $sav_charset_output = $outputlangs->charset_output;
229 $outputlangs->charset_output =
'UTF-8';
232 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
234 if ($conf->contrat->multidir_output[$object->entity]) {
236 if (!is_object($object)) {
239 $result = $object->fetch($id);
246 $object->fetch_thirdparty();
248 $dir = $conf->contrat->multidir_output[$object->entity];
250 if (!preg_match(
'/specimen/i', $objectref)) {
251 $dir .=
"/".$objectref;
253 $file = $dir.
"/".$objectref.
".odt";
255 if (!file_exists($dir)) {
257 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
262 if (file_exists($dir)) {
264 $newfile = basename($srctemplatepath);
265 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
266 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
267 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
269 $newfiletmp = $objectref.
'_'.$newfiletmp;
272 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
273 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
274 $format = $conf->global->MAIN_DOC_USE_TIMING;
275 if ($format ==
'1') {
276 $format =
'%Y%m%d%H%M%S';
280 $filename = $newfiletmp.
'.'.$newfileformat;
282 $file = $dir.
'/'.$filename;
289 if (!is_writable($conf->contrat->dir_temp)) {
290 $this->error =
"Failed to write in temp directory ".$conf->contrat->dir_temp;
291 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
297 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
298 if (count($arrayidcontact) > 0) {
300 $result = $object->fetch_contact($arrayidcontact[0]);
304 $contactobject =
null;
305 if (!empty($usecontact)) {
307 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))) {
308 $object->contact->fetch_thirdparty();
309 $socobject = $object->contact->thirdparty;
310 $contactobject = $object->contact;
312 $socobject = $object->thirdparty;
314 $contactobject = $object->contact;
317 $socobject = $object->thirdparty;
320 $object->fetch_optionals();
332 $array_thirdparty_contact = array();
333 if ($usecontact && is_object($contactobject)) {
337 $substitutionarray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
340 $tmparray = $substitutionarray;
343 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
344 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
348 $paramfreetext =
'CONTRACT_FREE_TEXT';
349 if (!empty($conf->global->$paramfreetext)) {
355 require_once ODTPHP_PATH.
'odf.php';
357 $odfHandler =
new odf(
360 'PATH_TO_TMP' => $conf->contrat->dir_temp,
361 'ZIP_PROXY' =>
'PclZipProxy',
362 'DELIMITER_LEFT' =>
'{',
363 'DELIMITER_RIGHT' =>
'}'
367 $this->error = $e->getMessage();
380 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
381 }
catch (OdfException $e) {
385 foreach ($tmparray as $key => $value) {
387 if (preg_match(
'/logo$/', $key)) {
388 if (file_exists($value)) {
389 $odfHandler->setImage($key, $value);
391 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
395 $odfHandler->setVars($key, $value,
true,
'UTF-8');
397 }
catch (OdfException $e) {
404 $foundtagforlines = 1;
406 $listlines = $odfHandler->setSegment(
'lines');
407 }
catch (OdfException $e) {
409 $foundtagforlines = 0;
412 if ($foundtagforlines) {
414 foreach ($object->lines as $line) {
419 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
420 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
421 foreach ($tmparray as $key => $val) {
423 $listlines->setVars($key, $val,
true,
'UTF-8');
424 }
catch (OdfException $e) {
426 }
catch (SegmentException $e) {
432 $odfHandler->mergeSegment($listlines);
434 }
catch (OdfException $e) {
435 $this->error = $e->getMessage();
441 $tmparray = $outputlangs->get_translations_for_substitutions();
442 foreach ($tmparray as $key => $value) {
444 $odfHandler->setVars($key, $value,
true,
'UTF-8');
445 }
catch (OdfException $e) {
451 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
452 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
455 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
457 $odfHandler->exportAsAttachedPDF($file);
459 $this->error = $e->getMessage();
464 $odfHandler->saveToDisk($file);
466 $this->error = $e->getMessage();
471 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
473 if (!empty($conf->global->MAIN_UMASK)) {
474 @chmod($file, octdec($conf->global->MAIN_UMASK));
479 $this->result = array(
'fullpath'=>$file);
483 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);