229 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
232 global $user, $langs,
$conf, $mysoc, $hookmanager;
235 if (empty($srctemplatepath)) {
236 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
241 if (!is_object($hookmanager)) {
242 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
245 $hookmanager->initHooks(array(
'odtgeneration'));
248 if (!is_object($outputlangs)) {
249 $outputlangs = $langs;
251 $sav_charset_output = $outputlangs->charset_output;
252 $outputlangs->charset_output =
'UTF-8';
254 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
256 if (
$conf->recruitment->dir_output) {
268 $dir =
$conf->recruitment->multidir_output[isset(
$object->entity) ?
$object->entity : 1].
'/recruitmentjobposition/';
270 if (!preg_match(
'/specimen/i', $objectref)) {
271 $dir .=
"/".$objectref;
273 $file = $dir.
"/".$objectref.
".odt";
275 if (!file_exists($dir)) {
277 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
282 if (file_exists($dir)) {
284 $newfile = basename($srctemplatepath);
285 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
286 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
287 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
288 $newfiletmp = $objectref.
'_'.$newfiletmp;
291 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
294 if ($format ==
'1') {
295 $format =
'%Y%m%d%H%M%S';
299 $filename = $newfiletmp.
'.'.$newfileformat;
301 $file = $dir.
'/'.$filename;
308 if (!is_writable(
$conf->recruitment->dir_temp)) {
309 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->recruitment->dir_temp);
310 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
316 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
317 if (count($arrayidcontact) > 0) {
319 $result =
$object->fetch_contact($arrayidcontact[0]);
323 $contactobject =
null;
324 if (!empty($usecontact)) {
327 $object->contact->fetch_thirdparty();
328 $socobject =
$object->contact->thirdparty;
329 $contactobject =
$object->contact;
331 $socobject =
$object->thirdparty;
333 $contactobject =
$object->contact;
336 $socobject =
$object->thirdparty;
340 $substitutionarray = array(
341 '__FROM_NAME__' => $this->emetteur->name,
342 '__FROM_EMAIL__' => $this->emetteur->email,
343 '__TOTAL_TTC__' =>
$object->total_ttc,
344 '__TOTAL_HT__' =>
$object->total_ht,
345 '__TOTAL_VAT__' =>
$object->total_tva
349 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
350 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
354 $paramfreetext =
'ORDER_FREE_TEXT';
360 require_once ODTPHP_PATH.
'odf.php';
362 $odfHandler =
new Odf(
365 'PATH_TO_TMP' =>
$conf->recruitment->dir_temp,
367 'DELIMITER_LEFT' =>
'{',
368 'DELIMITER_RIGHT' =>
'}'
372 $this->error = $e->getMessage();
385 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
386 }
catch (OdfException $e) {
393 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
399 $array_thirdparty_contact = array();
400 if ($usecontact && is_object($contactobject)) {
404 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
408 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
409 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
411 foreach ($tmparray as $key => $value) {
413 if (preg_match(
'/logo$/', $key)) {
415 if (file_exists($value)) {
416 $odfHandler->setImage($key, $value);
418 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
422 $odfHandler->setVars($key, $value,
true,
'UTF-8');
424 }
catch (OdfException $e) {
429 $foundtagforlines = 1;
431 $listlines = $odfHandler->setSegment(
'lines');
432 }
catch (OdfExceptionSegmentNotFound $e) {
434 $foundtagforlines = 0;
437 if ($foundtagforlines) {
439 foreach (
$object->lines as $line) {
444 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
445 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
446 foreach ($tmparray as $key => $val) {
448 $listlines->setVars($key, $val,
true,
'UTF-8');
449 }
catch (SegmentException $e) {
456 $odfHandler->mergeSegment($listlines);
457 }
catch (OdfException $e) {
458 $this->error = $e->getMessage();
465 $tmparray = $outputlangs->get_translations_for_substitutions();
466 foreach ($tmparray as $key => $value) {
468 $odfHandler->setVars($key, $value,
true,
'UTF-8');
469 }
catch (OdfException $e) {
476 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
477 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
482 $odfHandler->exportAsAttachedPDF($file);
484 $this->error = $e->getMessage();
490 $odfHandler->saveToDisk($file);
492 $this->error = $e->getMessage();
498 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
499 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
505 $this->result = array(
'fullpath' => $file);
509 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);