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(5, 6);
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'));
118 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
119 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
120 $texte .=
'<input type="hidden" name="page_y" value="">';
121 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
122 $texte .=
'<input type="hidden" name="param1" value="USER_ADDON_PDF_ODT_PATH">';
123 if (!empty($conf->global->MAIN_PROPAL_CHOOSE_ODT_DOCUMENT)) {
124 $texte .=
'<input type="hidden" name="param2" value="USER_ADDON_PDF_ODT_DEFAULT">';
125 $texte .=
'<input type="hidden" name="param3" value="USER_ADDON_PDF_ODT_TOBILL">';
126 $texte .=
'<input type="hidden" name="param4" value="USER_ADDON_PDF_ODT_CLOSED">';
128 $texte .=
'<table class="nobordernopadding" width="100%">';
131 $texte .=
'<tr><td>';
132 $texttitle = $langs->trans(
"ListOfDirectories");
133 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->USER_ADDON_PDF_ODT_PATH)));
134 $listoffiles = array();
135 foreach ($listofdir as $key => $tmpdir) {
136 $tmpdir = trim($tmpdir);
137 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
139 unset($listofdir[$key]);
142 if (!is_dir($tmpdir)) {
143 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
145 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
146 if (count($tmpfiles)) {
147 $listoffiles = array_merge($listoffiles, $tmpfiles);
151 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
153 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
154 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
156 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
157 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
158 $texte .=
'<textarea class="flat" cols="60" name="value1">';
159 $texte .= $conf->global->USER_ADDON_PDF_ODT_PATH;
160 $texte .=
'</textarea>';
161 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
162 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
163 $texte .=
'<br></div></div>';
166 if (count($listofdir)) {
167 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
169 if (!empty($conf->global->MAIN_PROPAL_CHOOSE_ODT_DOCUMENT)) {
172 $texte .=
'<table width="50%;">';
174 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
175 $texte .=
'<td colspan="">';
176 $texte .=
$form->selectarray(
'value2', $list, $conf->global->USER_ADDON_PDF_ODT_DEFAULT);
177 $texte .=
"</td></tr>";
180 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
181 $texte .=
'<td colspan="">';
182 $texte .=
$form->selectarray(
'value3', $list, $conf->global->USER_ADDON_PDF_ODT_TOBILL);
183 $texte .=
"</td></tr>";
186 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
187 $texte .=
'<td colspan="">';
188 $texte .=
$form->selectarray(
'value4', $list, $conf->global->USER_ADDON_PDF_ODT_CLOSED);
189 $texte .=
"</td></tr>";
190 $texte .=
'</table>';
192 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
194 foreach ($listoffiles as $file) {
195 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=users/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a><br>';
200 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
202 $maxmin = $maxfilesizearray[
'maxmin'];
204 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
206 $texte .=
' <input type="file" name="uploadfile">';
207 $texte .=
'<input type="hidden" value="USER_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
208 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
213 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
214 $texte .=
'<span class="opacitymedium">';
215 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
220 $texte .=
'</table>';
238 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
241 global $user, $langs, $conf, $mysoc, $hookmanager;
243 if (empty($srctemplatepath)) {
244 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
249 if (!is_object($hookmanager)) {
250 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
253 $hookmanager->initHooks(array(
'odtgeneration'));
256 if (!is_object($outputlangs)) {
257 $outputlangs = $langs;
259 $sav_charset_output = $outputlangs->charset_output;
260 $outputlangs->charset_output =
'UTF-8';
263 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
265 if ($conf->user->dir_output) {
267 if (!is_object($object)) {
269 $object =
new User($this->
db);
270 $result = $object->fetch($id);
277 $object->fetch_thirdparty();
279 $dir = $conf->user->dir_output;
281 if (!preg_match(
'/specimen/i', $objectref)) {
282 $dir .=
"/".$objectref;
284 $file = $dir.
"/".$objectref.
".odt";
286 if (!file_exists($dir)) {
288 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
293 if (file_exists($dir)) {
295 $newfile = basename($srctemplatepath);
296 $newfiletmp = preg_replace(
'/\.od(t|s)/i',
'', $newfile);
297 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
298 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
300 $newfiletmp = $objectref.
'_'.$newfiletmp;
303 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
304 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
305 $format = $conf->global->MAIN_DOC_USE_TIMING;
306 if ($format ==
'1') {
307 $format =
'%Y%m%d%H%M%S';
311 $filename = $newfiletmp.
'.'.$newfileformat;
313 $file = $dir.
'/'.$filename;
320 if (!is_writable($conf->user->dir_temp)) {
321 $this->error =
"Failed to write in temp directory ".$conf->user->dir_temp;
322 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
328 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
329 if (count($arrayidcontact) > 0) {
331 $result = $object->fetch_contact($arrayidcontact[0]);
335 if (!empty($usecontact)) {
336 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))) {
337 $socobject = $object->contact;
339 $socobject = $object->thirdparty;
341 $contactobject = $object->contact;
344 $socobject = $object->thirdparty;
348 require_once ODTPHP_PATH.
'odf.php';
350 $odfHandler =
new odf(
353 'PATH_TO_TMP' => $conf->user->dir_temp,
354 'ZIP_PROXY' =>
'PclZipProxy',
355 'DELIMITER_LEFT' =>
'{',
356 'DELIMITER_RIGHT' =>
'}'
360 $this->error = $e->getMessage();
371 $array_thirdparty_contact = array();
372 if ($usecontact && is_object($contactobject)) {
376 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
378 $object->fetch_optionals();
380 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
381 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
382 foreach ($tmparray as $key => $value) {
384 if (preg_match(
'/logo$/', $key)) {
385 if (file_exists($value)) {
386 $odfHandler->setImage($key, $value);
388 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
392 $odfHandler->setVars($key, $value,
true,
'UTF-8');
394 }
catch (OdfException $e) {
400 $tmparray = $outputlangs->get_translations_for_substitutions();
401 foreach ($tmparray as $key => $value) {
403 $odfHandler->setVars($key, $value,
true,
'UTF-8');
404 }
catch (OdfException $e) {
410 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
411 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
414 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
416 $odfHandler->exportAsAttachedPDF($file);
418 $this->error = $e->getMessage();
424 $odfHandler->saveToDisk($file);
426 $this->error = $e->getMessage();
432 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
434 if (!empty($conf->global->MAIN_UMASK)) {
435 @chmod($file, octdec($conf->global->MAIN_UMASK));
440 $this->result = array(
'fullpath'=>$file);
444 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
464 $array_other = array();
465 foreach ($object as $key => $value) {
466 if (!is_array($value) && !is_object($value)) {
467 $array_other[$array_key.
'_'.$key] = $value;