121 global
$conf, $langs;
124 $langs->loadLangs(array(
"errors",
"companies"));
126 $form =
new Form($this->db);
129 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
130 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
131 $texte .=
'<input type="hidden" name="page_y" value="">';
132 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
133 $texte .=
'<input type="hidden" name="param1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
134 $texte .=
'<table class="nobordernopadding centpercent">';
137 $texte .=
'<tr><td>';
138 $texttitle = $langs->trans(
"ListOfDirectories");
139 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim(
getDolGlobalString(
'MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH'))));
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),
'');
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");
158 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
160 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
161 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
164 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
165 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
166 $texte .=
'<textarea class="flat" cols="60" name="value1">';
168 $texte .=
'</textarea>';
169 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
170 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
171 $texte .=
'<br></div></div>';
174 $texte .=
'<input type="hidden" name="value1" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH">';
178 $nbofiles = count($listoffiles);
180 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
182 $texte .= count($listoffiles);
188 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
190 foreach ($listoffiles as $file) {
191 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=mymodule_myobject/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
192 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
199 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
201 $maxmin = $maxfilesizearray[
'maxmin'];
203 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
205 $texte .=
' <input type="file" name="uploadfile">';
206 $texte .=
'<input type="hidden" value="MYMODULE_MYOBJECT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
207 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
214 $texte .=
'</table>';
232 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
235 global $user, $langs,
$conf, $mysoc, $hookmanager;
238 if (empty($srctemplatepath)) {
239 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
244 $hookmanager->initHooks(array(
'odtgeneration'));
246 if (!is_object($outputlangs)) {
247 $outputlangs = $langs;
249 $sav_charset_output = $outputlangs->charset_output;
250 $outputlangs->charset_output =
'UTF-8';
252 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
254 if (
$conf->mymodule->dir_output) {
270 if (!preg_match(
'/specimen/i', $objectref)) {
271 $dir .=
"/".$objectref;
273 $file = $dir.
"/".$objectref.
".odt";
275 if (!file_exists($dir)) {
277 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
282 if (file_exists($dir)) {
284 $newfile = basename($srctemplatepath);
285 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
286 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
287 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
289 $newfiletmp = $objectref .
'_' . $newfiletmp;
293 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
296 if ($format ==
'1') {
297 $format =
'%Y%m%d%H%M%S';
301 $filename = $newfiletmp .
'.' . $newfileformat;
303 $file = $dir .
'/' . $filename;
310 if (!is_writable(
$conf->mymodule->dir_temp)) {
311 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->mymodule->dir_temp);
312 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
318 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
319 if (count($arrayidcontact) > 0) {
321 $result =
$object->fetch_contact($arrayidcontact[0]);
325 $contactobject =
null;
326 if (!empty($usecontact)) {
328 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset(
$conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
329 $object->contact->fetch_thirdparty();
330 $socobject =
$object->contact->thirdparty;
331 $contactobject =
$object->contact;
333 $socobject =
$object->thirdparty;
335 $contactobject =
$object->contact;
338 $socobject =
$object->thirdparty;
342 $substitutionarray = array(
343 '__FROM_NAME__' => $this->emetteur->name,
344 '__FROM_EMAIL__' => $this->emetteur->email,
345 '__TOTAL_TTC__' =>
$object->total_ttc,
346 '__TOTAL_HT__' =>
$object->total_ht,
347 '__TOTAL_VAT__' =>
$object->total_tva
351 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
352 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
356 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
362 require_once ODTPHP_PATH.
'odf.php';
364 $odfHandler =
new Odf(
367 'PATH_TO_TMP' =>
$conf->mymodule->dir_temp,
369 'DELIMITER_LEFT' =>
'{',
370 'DELIMITER_RIGHT' =>
'}'
374 $this->error = $e->getMessage();
387 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
388 }
catch (OdfException $e) {
395 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
401 $array_thirdparty_contact = array();
402 if ($usecontact && is_object($contactobject)) {
406 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
410 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
411 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
413 foreach ($tmparray as $key => $value) {
415 if (preg_match(
'/logo$/', $key)) {
417 if (file_exists($value)) {
418 $odfHandler->setImage($key, $value);
420 $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;
440 if ($foundtagforlines) {
442 foreach (
$object->lines as $line) {
447 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
448 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
449 foreach ($tmparray as $key => $val) {
451 $listlines->setVars($key, $val,
true,
'UTF-8');
452 }
catch (SegmentException $e) {
459 $odfHandler->mergeSegment($listlines);
460 }
catch (OdfException $e) {
461 $this->error = $e->getMessage();
468 $tmparray = $outputlangs->get_translations_for_substitutions();
469 foreach ($tmparray as $key => $value) {
471 $odfHandler->setVars($key, $value,
true,
'UTF-8');
472 }
catch (OdfException $e) {
479 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
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 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
502 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
508 $this->result = array(
'fullpath' => $file);
512 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);