216 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
219 global $user, $langs, $conf, $mysoc, $hookmanager;
221 if (empty($srctemplatepath)) {
222 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
227 if (!is_object($hookmanager)) {
228 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
231 $hookmanager->initHooks(array(
'odtgeneration'));
234 if (!is_object($outputlangs)) {
235 $outputlangs = $langs;
237 $sav_charset_output = $outputlangs->charset_output;
238 $outputlangs->charset_output =
'UTF-8';
241 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
243 if ($conf->expedition->dir_output.
"/sending") {
245 if (!is_object($object)) {
248 $result = $object->fetch($id);
255 $object->fetch_thirdparty();
257 $dir = $conf->expedition->dir_output.
"/sending";
259 if (!preg_match(
'/specimen/i', $objectref)) {
260 $dir .=
"/".$objectref;
262 $file = $dir.
"/".$objectref.
".odt";
264 if (!file_exists($dir)) {
266 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
271 if (file_exists($dir)) {
273 $newfile = basename($srctemplatepath);
274 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
275 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
276 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
277 $newfiletmp = $objectref .
'_' . $newfiletmp;
280 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
283 if ($format ==
'1') {
284 $format =
'%Y%m%d%H%M%S';
288 $filename = $newfiletmp .
'.' . $newfileformat;
290 $file = $dir .
'/' . $filename;
297 if (!is_writable($conf->expedition->dir_temp)) {
298 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->expedition->dir_temp);
299 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
305 $arrayidcontact = $object->getIdContact(
'external',
'SHIPPING');
306 if (count($arrayidcontact) > 0) {
308 $result = $object->fetch_contact($arrayidcontact[0]);
312 $contactobject =
null;
313 if (!empty($usecontact)) {
315 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))) {
316 $object->contact->fetch_thirdparty();
317 $socobject = $object->contact->thirdparty;
318 $contactobject = $object->contact;
320 $socobject = $object->thirdparty;
322 $contactobject = $object->contact;
325 $socobject = $object->thirdparty;
329 $substitutionarray = array(
330 '__FROM_NAME__' => $this->emetteur->name,
331 '__FROM_EMAIL__' => $this->emetteur->email,
332 '__TOTAL_TTC__' => $object->total_ttc,
333 '__TOTAL_HT__' => $object->total_ht,
334 '__TOTAL_VAT__' => $object->total_tva
338 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
339 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
343 $paramfreetext =
'EXPEDITION_FREE_TEXT';
344 if (!empty($conf->global->$paramfreetext)) {
349 require_once ODTPHP_PATH.
'odf.php';
351 $odfHandler =
new Odf(
354 'PATH_TO_TMP' => $conf->expedition->dir_temp,
355 'ZIP_PROXY' =>
'PclZipProxy',
356 'DELIMITER_LEFT' =>
'{',
357 'DELIMITER_RIGHT' =>
'}'
361 $this->error = $e->getMessage();
374 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
375 }
catch (OdfException $e) {
382 foreach ($tmparray as $key => $value) {
384 if (preg_match(
'/logo$/', $key)) {
386 if (file_exists($value)) {
387 $odfHandler->setImage($key, $value);
389 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
393 $odfHandler->setVars($key, $value,
true,
'UTF-8');
395 }
catch (OdfException $e) {
402 foreach ($tmparray as $key => $value) {
404 if (preg_match(
'/logo$/', $key)) {
406 if (file_exists($value)) {
407 $odfHandler->setImage($key, $value);
409 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
413 $odfHandler->setVars($key, $value,
true,
'UTF-8');
415 }
catch (OdfException $e) {
420 if ($socobject->element ==
'contact') {
425 foreach ($tmparray as $key => $value) {
427 if (preg_match(
'/logo$/', $key)) {
428 if (file_exists($value)) {
429 $odfHandler->setImage($key, $value);
431 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
435 $odfHandler->setVars($key, $value,
true,
'UTF-8');
437 }
catch (OdfException $e) {
442 if ($usecontact && is_object($contactobject)) {
444 foreach ($tmparray as $key => $value) {
446 if (preg_match(
'/logo$/', $key)) {
447 if (file_exists($value)) {
448 $odfHandler->setImage($key, $value);
450 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
454 $odfHandler->setVars($key, $value,
true,
'UTF-8');
456 }
catch (OdfException $e) {
467 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
468 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
469 foreach ($tmparray as $key => $value) {
471 if (preg_match(
'/logo$/', $key)) {
472 if (file_exists($value)) {
473 $odfHandler->setImage($key, $value);
475 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
479 $odfHandler->setVars($key, $value,
true,
'UTF-8');
481 }
catch (OdfException $e) {
487 $foundtagforlines = 1;
489 $listlines = $odfHandler->setSegment(
'lines');
490 }
catch (OdfException $e) {
492 $foundtagforlines = 0;
495 if ($foundtagforlines) {
496 foreach ($object->lines as $line) {
500 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
501 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
502 foreach ($tmparray as $key => $val) {
504 $listlines->setVars($key, $val,
true,
'UTF-8');
505 }
catch (OdfException $e) {
507 }
catch (SegmentException $e) {
513 $odfHandler->mergeSegment($listlines);
515 }
catch (OdfException $e) {
516 $this->error = $e->getMessage();
522 $tmparray = $outputlangs->get_translations_for_substitutions();
523 foreach ($tmparray as $key => $value) {
525 $odfHandler->setVars($key, $value,
true,
'UTF-8');
526 }
catch (OdfException $e) {
532 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
533 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
536 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
538 $odfHandler->exportAsAttachedPDF($file);
540 $this->error = $e->getMessage();
546 $odfHandler->saveToDisk($file);
548 $this->error = $e->getMessage();
553 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
554 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
560 $this->result = array(
'fullpath'=>$file);
564 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);