107 global $conf, $langs;
110 $langs->loadLangs(array(
"errors",
"companies"));
112 $form =
new Form($this->db);
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="USERGROUP_ADDON_PDF_ODT_PATH">';
124 $texte .=
'<input type="hidden" name="param2" value="USERGROUP_ADDON_PDF_ODT_DEFAULT">';
125 $texte .=
'<input type="hidden" name="param3" value="USERGROUP_ADDON_PDF_ODT_TOBILL">';
126 $texte .=
'<input type="hidden" name="param4" value="USERGROUP_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]+/',
',', $odtPath));
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),
'');
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");
152 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
154 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
155 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
157 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
158 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
159 $texte .=
'<textarea class="flat" cols="60" name="value1">';
161 $texte .=
'</textarea>';
162 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
163 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
164 $texte .=
'<br></div></div>';
167 if (count($listofdir)) {
168 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>'.count($listoffiles).
'</b>';
173 $texte .=
'<table width="50%;">';
175 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalCreate").
'</td>';
176 $texte .=
'<td colspan="">';
177 $texte .= $form->selectarray(
'value2', $list,
getDolGlobalString(
'USERGROUP_ADDON_PDF_ODT_DEFAULT'));
178 $texte .=
"</td></tr>";
181 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalToBill").
'</td>';
182 $texte .=
'<td colspan="">';
183 $texte .= $form->selectarray(
'value3', $list,
getDolGlobalString(
'USERGROUP_ADDON_PDF_ODT_TOBILL'));
184 $texte .=
"</td></tr>";
187 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelPropalClosed").
'</td>';
188 $texte .=
'<td colspan="">';
189 $texte .= $form->selectarray(
'value4', $list,
getDolGlobalString(
'USERGROUP_ADDON_PDF_ODT_CLOSED'));
190 $texte .=
"</td></tr>";
191 $texte .=
'</table>';
193 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
195 foreach ($listoffiles as $file) {
196 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=usergroups/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
197 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=USERGROUP_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
203 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
205 $maxmin = $maxfilesizearray[
'maxmin'];
207 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
209 $texte .=
' <input type="file" name="uploadfile">';
210 $texte .=
'<input type="hidden" value="USERGROUP_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
211 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
218 $texte .=
'</table>';
236 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
239 global $user, $langs, $conf, $mysoc, $hookmanager;
241 if (empty($srctemplatepath)) {
242 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
247 if (!is_object($hookmanager)) {
248 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
251 $hookmanager->initHooks(array(
'odtgeneration'));
254 if (!is_object($outputlangs)) {
255 $outputlangs = $langs;
257 $sav_charset_output = $outputlangs->charset_output;
258 $outputlangs->charset_output =
'UTF-8';
261 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
263 if ($conf->user->dir_output) {
275 $dir = $conf->user->dir_output.
'/usergroups';
277 if (!preg_match(
'/specimen/i', $objectref)) {
278 $dir .=
"/".$objectref;
280 $file = $dir.
"/".$objectref.
".odt";
282 if (!file_exists($dir)) {
284 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
289 if (file_exists($dir)) {
291 $newfile = basename($srctemplatepath);
292 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
293 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
294 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
296 $newfiletmp = $objectref .
'_' . $newfiletmp;
299 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
302 if ($format ==
'1') {
303 $format =
'%Y%m%d%H%M%S';
307 $filename = $newfiletmp .
'.' . $newfileformat;
309 $file = $dir .
'/' . $filename;
316 if (!is_writable($conf->user->dir_temp)) {
317 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->user->dir_temp);
318 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
324 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
325 if (count($arrayidcontact) > 0) {
327 $result =
$object->fetch_contact($arrayidcontact[0]);
331 if (!empty($usecontact)) {
333 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
334 $object->contact->fetch_thirdparty();
335 $socobject =
$object->contact->thirdparty;
336 $contactobject =
$object->contact;
338 $socobject =
$object->thirdparty;
340 $contactobject =
$object->contact;
343 $socobject =
$object->thirdparty;
346 $substitutionarray = array(
347 '__FROM_NAME__' => $this->emetteur->name,
348 '__FROM_EMAIL__' => $this->emetteur->email,
349 '__TOTAL_TTC__' =>
$object->total_ttc,
350 '__TOTAL_HT__' =>
$object->total_ht,
351 '__TOTAL_VAT__' =>
$object->total_tva
355 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
356 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
360 $paramfreetext =
'user_FREE_TEXT';
366 require_once ODTPHP_PATH.
'odf.php';
368 $odfHandler =
new Odf(
371 'PATH_TO_TMP' => $conf->user->dir_temp,
373 'DELIMITER_LEFT' =>
'{',
374 'DELIMITER_RIGHT' =>
'}'
378 $this->error = $e->getMessage();
391 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
392 }
catch (OdfException $e) {
404 $array_thirdparty_contact = array();
405 if ($usecontact && is_object($contactobject)) {
409 $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
413 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
414 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
415 foreach ($tmparray as $key => $value) {
417 if (preg_match(
'/logo$/', $key)) {
418 if (file_exists($value)) {
419 $odfHandler->setImage($key, $value);
421 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
424 $odfHandler->setVars($key, $value,
true,
'UTF-8');
426 }
catch (OdfException $e) {
431 $foundtagforlines = 1;
433 $listlines = $odfHandler->setSegment(
'lines');
434 }
catch (OdfExceptionSegmentNotFound $e) {
436 $foundtagforlines = 0;
439 if ($foundtagforlines) {
440 foreach (
$object->members as $u) {
442 unset($tmparray[
'object_pass']);
443 unset($tmparray[
'object_pass_indatabase']);
446 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $u);
447 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
448 foreach ($tmparray as $key => $val) {
450 if (!is_array($val)) {
451 $listlines->setVars($key, $val,
true,
'UTF-8');
453 }
catch (SegmentException $e) {
460 $odfHandler->mergeSegment($listlines);
461 }
catch (OdfException $e) {
462 $this->error = $e->getMessage();
469 $tmparray = $outputlangs->get_translations_for_substitutions();
470 foreach ($tmparray as $key => $value) {
472 $odfHandler->setVars($key, $value,
true,
'UTF-8');
473 }
catch (OdfException $e) {
479 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
480 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
485 $odfHandler->exportAsAttachedPDF($file);
487 $this->error = $e->getMessage();
493 $odfHandler->saveToDisk($file);
495 $this->error = $e->getMessage();
501 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
507 $this->result = array(
'fullpath' => $file);
511 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);