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 if (!is_object($hookmanager)) {
245 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
248 $hookmanager->initHooks(array(
'odtgeneration'));
251 if (!is_object($outputlangs)) {
252 $outputlangs = $langs;
254 $sav_charset_output = $outputlangs->charset_output;
255 $outputlangs->charset_output =
'UTF-8';
257 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
259 if (
$conf->mymodule->dir_output) {
275 if (!preg_match(
'/specimen/i', $objectref)) {
276 $dir .=
"/".$objectref;
278 $file = $dir.
"/".$objectref.
".odt";
280 if (!file_exists($dir)) {
282 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
287 if (file_exists($dir)) {
289 $newfile = basename($srctemplatepath);
290 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
291 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
292 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
294 $newfiletmp = $objectref .
'_' . $newfiletmp;
298 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
301 if ($format ==
'1') {
302 $format =
'%Y%m%d%H%M%S';
306 $filename = $newfiletmp .
'.' . $newfileformat;
308 $file = $dir .
'/' . $filename;
315 if (!is_writable(
$conf->mymodule->dir_temp)) {
316 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->mymodule->dir_temp);
317 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
323 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
324 if (count($arrayidcontact) > 0) {
326 $result =
$object->fetch_contact($arrayidcontact[0]);
330 $contactobject =
null;
331 if (!empty($usecontact)) {
333 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset(
$conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'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;
347 $substitutionarray = array(
348 '__FROM_NAME__' => $this->emetteur->name,
349 '__FROM_EMAIL__' => $this->emetteur->email,
350 '__TOTAL_TTC__' =>
$object->total_ttc,
351 '__TOTAL_HT__' =>
$object->total_ht,
352 '__TOTAL_VAT__' =>
$object->total_tva
356 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
357 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
361 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
367 require_once ODTPHP_PATH.
'odf.php';
369 $odfHandler =
new Odf(
372 'PATH_TO_TMP' =>
$conf->mymodule->dir_temp,
374 'DELIMITER_LEFT' =>
'{',
375 'DELIMITER_RIGHT' =>
'}'
379 $this->error = $e->getMessage();
392 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
393 }
catch (OdfException $e) {
400 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
406 $array_thirdparty_contact = array();
407 if ($usecontact && is_object($contactobject)) {
411 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
415 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
416 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
418 foreach ($tmparray as $key => $value) {
420 if (preg_match(
'/logo$/', $key)) {
422 if (file_exists($value)) {
423 $odfHandler->setImage($key, $value);
425 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
429 $odfHandler->setVars($key, $value,
true,
'UTF-8');
431 }
catch (OdfException $e) {
436 $foundtagforlines = 1;
438 $listlines = $odfHandler->setSegment(
'lines');
439 }
catch (OdfExceptionSegmentNotFound $e) {
441 $foundtagforlines = 0;
445 if ($foundtagforlines) {
447 foreach (
$object->lines as $line) {
452 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
453 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
454 foreach ($tmparray as $key => $val) {
456 $listlines->setVars($key, $val,
true,
'UTF-8');
457 }
catch (SegmentException $e) {
464 $odfHandler->mergeSegment($listlines);
465 }
catch (OdfException $e) {
466 $this->error = $e->getMessage();
473 $tmparray = $outputlangs->get_translations_for_substitutions();
474 foreach ($tmparray as $key => $value) {
476 $odfHandler->setVars($key, $value,
true,
'UTF-8');
477 }
catch (OdfException $e) {
484 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
485 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
490 $odfHandler->exportAsAttachedPDF($file);
492 $this->error = $e->getMessage();
498 $odfHandler->saveToDisk($file);
500 $this->error = $e->getMessage();
506 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
507 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
513 $this->result = array(
'fullpath' => $file);
517 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);