206 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
209 global $user, $langs, $conf, $mysoc, $hookmanager;
211 if (empty($srctemplatepath)) {
212 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
217 if (!is_object($hookmanager)) {
218 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
221 $hookmanager->initHooks(array(
'odtgeneration'));
224 if (!is_object($outputlangs)) {
225 $outputlangs = $langs;
227 $sav_charset_output = $outputlangs->charset_output;
228 $outputlangs->charset_output =
'UTF-8';
231 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
233 if ($conf->expedition->dir_output.
"/sending") {
235 if (!is_object($object)) {
238 $result = $object->fetch($id);
245 $object->fetch_thirdparty();
247 $dir = $conf->expedition->dir_output.
"/sending";
249 if (!preg_match(
'/specimen/i', $objectref)) {
250 $dir .=
"/".$objectref;
252 $file = $dir.
"/".$objectref.
".odt";
254 if (!file_exists($dir)) {
256 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
261 if (file_exists($dir)) {
263 $newfile = basename($srctemplatepath);
264 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
265 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
266 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
267 $newfiletmp = $objectref .
'_' . $newfiletmp;
270 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
273 if ($format ==
'1') {
274 $format =
'%Y%m%d%H%M%S';
278 $filename = $newfiletmp .
'.' . $newfileformat;
280 $file = $dir .
'/' . $filename;
287 if (!is_writable($conf->expedition->dir_temp)) {
288 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->expedition->dir_temp);
289 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
295 $arrayidcontact = $object->getIdContact(
'external',
'SHIPPING');
296 if (count($arrayidcontact) > 0) {
298 $result = $object->fetch_contact($arrayidcontact[0]);
302 $contactobject =
null;
303 if (!empty($usecontact)) {
305 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
306 $object->contact->fetch_thirdparty();
307 $socobject = $object->contact->thirdparty;
308 $contactobject = $object->contact;
310 $socobject = $object->thirdparty;
312 $contactobject = $object->contact;
315 $socobject = $object->thirdparty;
319 $substitutionarray = array(
320 '__FROM_NAME__' => $this->emetteur->name,
321 '__FROM_EMAIL__' => $this->emetteur->email,
322 '__TOTAL_TTC__' => $object->total_ttc,
323 '__TOTAL_HT__' => $object->total_ht,
324 '__TOTAL_VAT__' => $object->total_tva
328 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
329 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
333 $paramfreetext =
'EXPEDITION_FREE_TEXT';
334 if (!empty($conf->global->$paramfreetext)) {
339 require_once ODTPHP_PATH.
'odf.php';
341 $odfHandler =
new Odf(
344 'PATH_TO_TMP' => $conf->expedition->dir_temp,
346 'DELIMITER_LEFT' =>
'{',
347 'DELIMITER_RIGHT' =>
'}'
351 $this->error = $e->getMessage();
364 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
365 }
catch (OdfException $e) {
372 foreach ($tmparray as $key => $value) {
374 if (preg_match(
'/logo$/', $key)) {
376 if (file_exists($value)) {
377 $odfHandler->setImage($key, $value);
379 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
382 $odfHandler->setVars($key, $value,
true,
'UTF-8');
384 }
catch (OdfException $e) {
391 foreach ($tmparray as $key => $value) {
393 if (preg_match(
'/logo$/', $key)) {
395 if (file_exists($value)) {
396 $odfHandler->setImage($key, $value);
398 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
401 $odfHandler->setVars($key, $value,
true,
'UTF-8');
403 }
catch (OdfException $e) {
408 if ($socobject->element ==
'contact') {
413 foreach ($tmparray as $key => $value) {
415 if (preg_match(
'/logo$/', $key)) {
416 if (file_exists($value)) {
417 $odfHandler->setImage($key, $value);
419 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
422 $odfHandler->setVars($key, $value,
true,
'UTF-8');
424 }
catch (OdfException $e) {
429 if ($usecontact && is_object($contactobject)) {
431 foreach ($tmparray as $key => $value) {
433 if (preg_match(
'/logo$/', $key)) {
434 if (file_exists($value)) {
435 $odfHandler->setImage($key, $value);
437 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
440 $odfHandler->setVars($key, $value,
true,
'UTF-8');
442 }
catch (OdfException $e) {
454 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
455 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
456 foreach ($tmparray as $key => $value) {
458 if (preg_match(
'/logo$/', $key)) {
459 if (file_exists($value)) {
460 $odfHandler->setImage($key, $value);
462 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
465 $odfHandler->setVars($key, $value,
true,
'UTF-8');
467 }
catch (OdfException $e) {
473 $foundtagforlines = 1;
475 $listlines = $odfHandler->setSegment(
'lines');
476 }
catch (OdfExceptionSegmentNotFound $e) {
478 $foundtagforlines = 0;
480 }
catch (OdfException $e) {
481 $foundtagforlines = 0;
484 if ($foundtagforlines) {
485 foreach ($object->lines as $line) {
489 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
490 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
491 foreach ($tmparray as $key => $val) {
493 $listlines->setVars($key, $val,
true,
'UTF-8');
494 }
catch (OdfException $e) {
496 }
catch (SegmentException $e) {
502 $odfHandler->mergeSegment($listlines);
504 }
catch (OdfException $e) {
505 $this->error = $e->getMessage();
511 $tmparray = $outputlangs->get_translations_for_substitutions();
512 foreach ($tmparray as $key => $value) {
514 $odfHandler->setVars($key, $value,
true,
'UTF-8');
515 }
catch (OdfException $e) {
521 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
522 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
527 $odfHandler->exportAsAttachedPDF($file);
529 $this->error = $e->getMessage();
535 $odfHandler->saveToDisk($file);
537 $this->error = $e->getMessage();
542 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
543 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
549 $this->result = array(
'fullpath'=>$file);
553 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);