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->multidir_output[
$object->entity].
"/sending") {
247 $dir = $conf->expedition->multidir_output[
$object->entity].
"/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,
345 'ZIP_PROXY' =>
'PclZipProxy',
346 'DELIMITER_LEFT' =>
'{',
347 'DELIMITER_RIGHT' =>
'}'
351 $this->error = $e->getMessage();
364 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
365 }
catch (OdfException $e) {
371 foreach ($tmparray as $key => $value) {
373 if (preg_match(
'/logo$/', $key)) {
375 if (file_exists($value)) {
376 $odfHandler->setImage($key, $value);
378 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
381 $odfHandler->setVars($key, $value,
true,
'UTF-8');
383 }
catch (OdfException $e) {
389 foreach ($tmparray as $key => $value) {
391 if (preg_match(
'/logo$/', $key)) {
393 if (file_exists($value)) {
394 $odfHandler->setImage($key, $value);
396 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
399 $odfHandler->setVars($key, $value,
true,
'UTF-8');
401 }
catch (OdfException $e) {
406 if ($socobject->element ==
'contact') {
411 foreach ($tmparray as $key => $value) {
413 if (preg_match(
'/logo$/', $key)) {
414 if (file_exists($value)) {
415 $odfHandler->setImage($key, $value);
417 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
420 $odfHandler->setVars($key, $value,
true,
'UTF-8');
422 }
catch (OdfException $e) {
427 if ($usecontact && is_object($contactobject)) {
429 foreach ($tmparray as $key => $value) {
431 if (preg_match(
'/logo$/', $key)) {
432 if (file_exists($value)) {
433 $odfHandler->setImage($key, $value);
435 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
438 $odfHandler->setVars($key, $value,
true,
'UTF-8');
440 }
catch (OdfException $e) {
452 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
453 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
455 foreach ($tmparray as $key => $value) {
457 if (preg_match(
'/logo$/', $key)) {
459 if (file_exists($value)) {
460 $odfHandler->setImage($key, $value);
462 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
466 $odfHandler->setVars($key, $value,
true,
'UTF-8');
468 }
catch (OdfException $e) {
474 $foundtagforlines = 1;
476 $listlines = $odfHandler->setSegment(
'lines');
477 }
catch (OdfExceptionSegmentNotFound $e) {
479 $foundtagforlines = 0;
482 if ($foundtagforlines) {
484 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 (SegmentException $e) {
501 $odfHandler->mergeSegment($listlines);
502 }
catch (OdfException $e) {
503 $this->error = $e->getMessage();
510 $tmparray = $outputlangs->get_translations_for_substitutions();
511 foreach ($tmparray as $key => $value) {
513 $odfHandler->setVars($key, $value,
true,
'UTF-8');
514 }
catch (OdfException $e) {
520 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
521 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
526 $odfHandler->exportAsAttachedPDF($file);
528 $this->error = $e->getMessage();
534 $odfHandler->saveToDisk($file);
536 $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);