27 require_once DOL_DOCUMENT_ROOT.
'/core/modules/user/modules_user.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
48 public $phpmin = array(7, 0);
54 public $version =
'dolibarr';
64 global $conf, $langs, $mysoc;
67 $langs->loadLangs(array(
"main",
"companies"));
70 $this->
name =
"ODT templates";
71 $this->
description = $langs->trans(
"DocumentModelOdt");
72 $this->scandir =
'USER_ADDON_PDF_ODT_PATH';
76 $this->page_largeur = 0;
77 $this->page_hauteur = 0;
78 $this->format = array($this->page_largeur, $this->page_hauteur);
79 $this->marge_gauche = 0;
80 $this->marge_droite = 0;
81 $this->marge_haute = 0;
82 $this->marge_basse = 0;
84 $this->option_logo = 1;
85 $this->option_tva = 0;
86 $this->option_modereg = 0;
87 $this->option_condreg = 0;
88 $this->option_multilang = 1;
89 $this->option_escompte = 0;
90 $this->option_credit_note = 0;
91 $this->option_freetext = 1;
92 $this->option_draft_watermark = 0;
95 $this->emetteur = $mysoc;
96 if (!$this->emetteur->country_code) {
97 $this->emetteur->country_code = substr($langs->defaultlang, -2);
110 global $conf, $langs;
113 $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="USER_ADDON_PDF_ODT_PATH">';
127 $texte .=
'<input type="hidden" name="param2" value="USER_ADDON_PDF_ODT_DEFAULT">';
128 $texte .=
'<input type="hidden" name="param3" value="USER_ADDON_PDF_ODT_TOBILL">';
129 $texte .=
'<input type="hidden" name="param4" value="USER_ADDON_PDF_ODT_CLOSED">';
131 $texte .=
'<table class="nobordernopadding" width="100%">';
134 $texte .=
'<tr><td>';
135 $texttitle = $langs->trans(
"ListOfDirectories");
136 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
137 $listoffiles = array();
138 foreach ($listofdir as $key => $tmpdir) {
139 $tmpdir = trim($tmpdir);
140 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
142 unset($listofdir[$key]);
145 if (!is_dir($tmpdir)) {
146 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
148 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
149 if (count($tmpfiles)) {
150 $listoffiles = array_merge($listoffiles, $tmpfiles);
154 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
156 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
157 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
159 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
160 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
161 $texte .=
'<textarea class="flat" cols="60" name="value1">';
163 $texte .=
'</textarea>';
164 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
165 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
166 $texte .=
'<br></div></div>';
169 if (count($listofdir)) {
170 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
175 $texte .=
'<table width="50%;">';
177 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
178 $texte .=
'<td colspan="">';
180 $texte .=
"</td></tr>";
183 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
184 $texte .=
'<td colspan="">';
186 $texte .=
"</td></tr>";
189 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
190 $texte .=
'<td colspan="">';
192 $texte .=
"</td></tr>";
193 $texte .=
'</table>';
195 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
197 foreach ($listoffiles as $file) {
198 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=users/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
199 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=USER_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
205 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
207 $maxmin = $maxfilesizearray[
'maxmin'];
209 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
211 $texte .=
' <input type="file" name="uploadfile">';
212 $texte .=
'<input type="hidden" value="USER_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
213 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
218 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
219 $texte .=
'<span class="opacitymedium">';
220 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
225 $texte .=
'</table>';
243 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
246 global $user, $langs, $conf, $mysoc, $hookmanager;
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';
268 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
270 if ($conf->user->dir_output) {
272 if (!is_object($object)) {
274 $object =
new User($this->
db);
275 $result = $object->fetch($id);
282 $object->fetch_thirdparty();
284 $dir = $conf->user->dir_output;
286 if (!preg_match(
'/specimen/i', $objectref)) {
287 $dir .=
"/".$objectref;
289 $file = $dir.
"/".$objectref.
".odt";
291 if (!file_exists($dir)) {
293 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
298 if (file_exists($dir)) {
300 $newfile = basename($srctemplatepath);
301 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
302 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
303 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
305 $newfiletmp = $objectref .
'_' . $newfiletmp;
308 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
309 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
310 $format = $conf->global->MAIN_DOC_USE_TIMING;
311 if ($format ==
'1') {
312 $format =
'%Y%m%d%H%M%S';
316 $filename = $newfiletmp .
'.' . $newfileformat;
318 $file = $dir .
'/' . $filename;
325 if (!is_writable($conf->user->dir_temp)) {
326 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->user->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 if (!empty($usecontact)) {
341 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))) {
342 $socobject = $object->contact;
344 $socobject = $object->thirdparty;
346 $contactobject = $object->contact;
349 $socobject = $object->thirdparty;
353 require_once ODTPHP_PATH.
'odf.php';
355 $odfHandler =
new odf(
358 'PATH_TO_TMP' => $conf->user->dir_temp,
359 'ZIP_PROXY' =>
'PclZipProxy',
360 'DELIMITER_LEFT' =>
'{',
361 'DELIMITER_RIGHT' =>
'}'
365 $this->error = $e->getMessage();
376 $array_thirdparty_contact = array();
377 if ($usecontact && is_object($contactobject)) {
381 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
383 $object->fetch_optionals();
385 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
386 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
387 foreach ($tmparray as $key => $value) {
389 if (preg_match(
'/logo$/', $key)) {
390 if (file_exists($value)) {
391 $odfHandler->setImage($key, $value);
393 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
397 $odfHandler->setVars($key, $value,
true,
'UTF-8');
399 }
catch (OdfException $e) {
405 $tmparray = $outputlangs->get_translations_for_substitutions();
406 foreach ($tmparray as $key => $value) {
408 $odfHandler->setVars($key, $value,
true,
'UTF-8');
409 }
catch (OdfException $e) {
415 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
416 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
419 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
421 $odfHandler->exportAsAttachedPDF($file);
423 $this->error = $e->getMessage();
429 $odfHandler->saveToDisk($file);
431 $this->error = $e->getMessage();
437 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
443 $this->result = array(
'fullpath'=>$file);
447 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
467 $array_other = array();
468 foreach ($object as $key => $value) {
469 if (!is_array($value) && !is_object($value)) {
470 $array_other[$array_key.
'_'.$key] = $value;