106 global $conf, $langs;
109 $langs->loadLangs(array(
'companies',
'errors'));
111 $form =
new Form($this->db);
114 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
115 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
116 $texte .=
'<input type="hidden" name="page_y" value="">';
117 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
118 $texte .=
'<input type="hidden" name="param1" value="CONTRACT_ADDON_PDF_ODT_PATH">';
119 $texte .=
'<table class="nobordernopadding centpercent">';
122 $texte .=
'<tr><td>';
123 $texttitle = $langs->trans(
"ListOfDirectories");
124 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->CONTRACT_ADDON_PDF_ODT_PATH)));
125 $listoffiles = array();
126 foreach ($listofdir as $key => $tmpdir) {
127 $tmpdir = trim($tmpdir);
128 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
130 unset($listofdir[$key]);
133 if (!is_dir($tmpdir)) {
134 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
136 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
137 if (count($tmpfiles)) {
138 $listoffiles = array_merge($listoffiles, $tmpfiles);
142 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
143 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
145 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
146 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
148 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
149 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
150 $texte .=
'<textarea class="flat" cols="60" name="value1">';
152 $texte .=
'</textarea>';
153 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
154 $texte .=
'<input type="submit" class="button reposition smallpaddingimp" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
155 $texte .=
'<br></div></div>';
158 $nbofiles = count($listoffiles);
160 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
162 $texte .= count($listoffiles);
168 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
170 foreach ($listoffiles as $file) {
171 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=contracts/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
172 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=CONTRACT_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
178 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
180 $maxmin = $maxfilesizearray[
'maxmin'];
182 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
184 $texte .=
' <input type="file" name="uploadfile">';
185 $texte .=
'<input type="hidden" value="CONTRACT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
186 $texte .=
'<input type="submit" class="button reposition smallpaddingimp" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
193 $texte .=
'</table>';
211 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
214 global $user, $langs, $conf, $mysoc, $hookmanager;
216 if (empty($srctemplatepath)) {
217 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
222 if (!is_object($hookmanager)) {
223 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
226 $hookmanager->initHooks(array(
'odtgeneration'));
229 if (!is_object($outputlangs)) {
230 $outputlangs = $langs;
232 $sav_charset_output = $outputlangs->charset_output;
233 $outputlangs->charset_output =
'UTF-8';
236 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
238 if ($conf->contract->multidir_output[
$object->entity]) {
252 $dir = $conf->contract->multidir_output[
$object->entity];
254 if (!preg_match(
'/specimen/i', $objectref)) {
255 $dir .=
"/".$objectref;
257 $file = $dir.
"/".$objectref.
".odt";
259 if (!file_exists($dir)) {
261 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
266 if (file_exists($dir)) {
268 $newfile = basename($srctemplatepath);
269 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
270 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
271 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
273 $newfiletmp = $objectref .
'_' . $newfiletmp;
276 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
279 if ($format ==
'1') {
280 $format =
'%Y%m%d%H%M%S';
284 $filename = $newfiletmp .
'.' . $newfileformat;
286 $file = $dir .
'/' . $filename;
293 if (!is_writable($conf->contract->dir_temp)) {
294 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->contrat->dir_temp);
295 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
301 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
302 if (count($arrayidcontact) > 0) {
304 $result =
$object->fetch_contact($arrayidcontact[0]);
308 $contactobject =
null;
309 if (!empty($usecontact)) {
311 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
312 $object->contact->fetch_thirdparty();
313 $socobject =
$object->contact->thirdparty;
314 $contactobject =
$object->contact;
316 $socobject =
$object->thirdparty;
318 $contactobject =
$object->contact;
321 $socobject =
$object->thirdparty;
330 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
336 $array_thirdparty_contact = array();
337 if ($usecontact && is_object($contactobject)) {
341 $substitutionarray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
344 $tmparray = $substitutionarray;
347 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
348 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
352 $paramfreetext =
'CONTRACT_FREE_TEXT';
359 require_once ODTPHP_PATH.
'odf.php';
361 $odfHandler =
new Odf(
364 'PATH_TO_TMP' => $conf->contrat->dir_temp,
366 'DELIMITER_LEFT' =>
'{',
367 'DELIMITER_RIGHT' =>
'}'
371 $this->error = $e->getMessage();
384 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
385 }
catch (OdfException $e) {
390 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
391 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
393 foreach ($tmparray as $key => $value) {
395 if (preg_match(
'/logo$/', $key)) {
397 if (file_exists($value)) {
398 $odfHandler->setImage($key, $value);
400 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
404 $odfHandler->setVars($key, $value,
true,
'UTF-8');
406 }
catch (OdfException $e) {
412 $foundtagforlines = 1;
414 $listlines = $odfHandler->setSegment(
'lines');
415 }
catch (OdfExceptionSegmentNotFound $e) {
417 $foundtagforlines = 0;
420 if ($foundtagforlines) {
422 foreach (
$object->lines as $line) {
427 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
428 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
429 foreach ($tmparray as $key => $val) {
431 $listlines->setVars($key, $val,
true,
'UTF-8');
432 }
catch (SegmentException $e) {
439 $odfHandler->mergeSegment($listlines);
440 }
catch (OdfException $e) {
441 $this->error = $e->getMessage();
448 $tmparray = $outputlangs->get_translations_for_substitutions();
449 foreach ($tmparray as $key => $value) {
451 $odfHandler->setVars($key, $value,
true,
'UTF-8');
452 }
catch (OdfException $e) {
458 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
459 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
464 $odfHandler->exportAsAttachedPDF($file);
466 $this->error = $e->getMessage();
472 $odfHandler->saveToDisk($file);
474 $this->error = $e->getMessage();
480 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
481 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
487 $this->result = array(
'fullpath' => $file);
491 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);