221 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
224 global $user, $langs, $conf, $mysoc, $hookmanager;
226 if (empty($srctemplatepath)) {
227 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
232 if (!is_object($hookmanager)) {
233 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
236 $hookmanager->initHooks(array(
'odtgeneration'));
239 if (!is_object($outputlangs)) {
240 $outputlangs = $langs;
242 $sav_charset_output = $outputlangs->charset_output;
243 $outputlangs->charset_output =
'UTF-8';
246 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
248 if ($conf->user->dir_output) {
250 if (!is_object($object)) {
253 $result = $object->fetch($id);
260 $dir = $conf->user->dir_output.
'/usergroups';
262 if (!preg_match(
'/specimen/i', $objectref)) {
263 $dir .=
"/".$objectref;
265 $file = $dir.
"/".$objectref.
".odt";
267 if (!file_exists($dir)) {
269 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
274 if (file_exists($dir)) {
276 $newfile = basename($srctemplatepath);
277 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
278 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
279 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
281 $newfiletmp = $objectref .
'_' . $newfiletmp;
284 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
287 if ($format ==
'1') {
288 $format =
'%Y%m%d%H%M%S';
292 $filename = $newfiletmp .
'.' . $newfileformat;
294 $file = $dir .
'/' . $filename;
301 if (!is_writable($conf->user->dir_temp)) {
302 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->user->dir_temp);
303 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
309 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
310 if (count($arrayidcontact) > 0) {
312 $result = $object->fetch_contact($arrayidcontact[0]);
316 if (!empty($usecontact)) {
318 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))) {
319 $object->contact->fetch_thirdparty();
320 $socobject = $object->contact->thirdparty;
321 $contactobject = $object->contact;
323 $socobject = $object->thirdparty;
325 $contactobject = $object->contact;
328 $socobject = $object->thirdparty;
331 $substitutionarray = array(
332 '__FROM_NAME__' => $this->emetteur->name,
333 '__FROM_EMAIL__' => $this->emetteur->email,
334 '__TOTAL_TTC__' => $object->total_ttc,
335 '__TOTAL_HT__' => $object->total_ht,
336 '__TOTAL_VAT__' => $object->total_tva
340 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
341 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
345 $paramfreetext =
'user_FREE_TEXT';
346 if (!empty($conf->global->$paramfreetext)) {
351 require_once ODTPHP_PATH.
'odf.php';
353 $odfHandler =
new Odf(
356 'PATH_TO_TMP' => $conf->user->dir_temp,
357 'ZIP_PROXY' =>
'PclZipProxy',
358 'DELIMITER_LEFT' =>
'{',
359 'DELIMITER_RIGHT' =>
'}'
363 $this->error = $e->getMessage();
376 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
377 }
catch (OdfException $e) {
389 $array_thirdparty_contact = array();
390 if ($usecontact && is_object($contactobject)) {
394 $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
396 $object->fetch_optionals();
398 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
399 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
400 foreach ($tmparray as $key => $value) {
402 if (preg_match(
'/logo$/', $key)) {
403 if (file_exists($value)) {
404 $odfHandler->setImage($key, $value);
406 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
410 $odfHandler->setVars($key, $value,
true,
'UTF-8');
412 }
catch (OdfException $e) {
418 $foundtagforlines = 1;
420 $listlines = $odfHandler->setSegment(
'lines');
421 }
catch (OdfException $e) {
423 $foundtagforlines = 0;
426 if ($foundtagforlines) {
427 foreach ($object->members as $u) {
429 unset($tmparray[
'object_pass']);
430 unset($tmparray[
'object_pass_indatabase']);
433 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$u);
434 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
435 foreach ($tmparray as $key => $val) {
437 if (!is_array($val)) {
438 $listlines->setVars($key, $val,
true,
'UTF-8');
440 }
catch (OdfException $e) {
442 }
catch (SegmentException $e) {
448 $odfHandler->mergeSegment($listlines);
450 }
catch (OdfException $e) {
451 $this->error = $e->getMessage();
457 $tmparray = $outputlangs->get_translations_for_substitutions();
458 foreach ($tmparray as $key => $value) {
460 $odfHandler->setVars($key, $value,
true,
'UTF-8');
461 }
catch (OdfException $e) {
467 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
468 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
471 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
473 $odfHandler->exportAsAttachedPDF($file);
475 $this->error = $e->getMessage();
481 $odfHandler->saveToDisk($file);
483 $this->error = $e->getMessage();
489 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
495 $this->result = array(
'fullpath'=>$file);
499 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);