224 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
227 global $user, $langs, $conf, $mysoc, $hookmanager;
230 if (empty($srctemplatepath)) {
231 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
236 $hookmanager->initHooks(array(
'odtgeneration'));
238 if (!is_object($outputlangs)) {
239 $outputlangs = $langs;
241 $sav_charset_output = $outputlangs->charset_output;
242 $outputlangs->charset_output =
'UTF-8';
244 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
246 if ($conf->recruitment->dir_output) {
258 $dir = $conf->recruitment->multidir_output[isset(
$object->entity) ?
$object->entity : 1].
'/recruitmentjobposition/';
260 if (!preg_match(
'/specimen/i', $objectref)) {
261 $dir .=
"/".$objectref;
263 $file = $dir.
"/".$objectref.
".odt";
265 if (!file_exists($dir)) {
267 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
272 if (file_exists($dir)) {
274 $newfile = basename($srctemplatepath);
275 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
276 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
277 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
278 $newfiletmp = $objectref.
'_'.$newfiletmp;
281 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
284 if ($format ==
'1') {
285 $format =
'%Y%m%d%H%M%S';
289 $filename = $newfiletmp.
'.'.$newfileformat;
291 $file = $dir.
'/'.$filename;
298 if (!is_writable($conf->recruitment->dir_temp)) {
299 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->recruitment->dir_temp);
300 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
306 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
307 if (count($arrayidcontact) > 0) {
309 $result =
$object->fetch_contact($arrayidcontact[0]);
313 $contactobject =
null;
314 if (!empty($usecontact)) {
316 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
317 $object->contact->fetch_thirdparty();
318 $socobject =
$object->contact->thirdparty;
319 $contactobject =
$object->contact;
321 $socobject =
$object->thirdparty;
323 $contactobject =
$object->contact;
326 $socobject =
$object->thirdparty;
330 $substitutionarray = array(
331 '__FROM_NAME__' => $this->emetteur->name,
332 '__FROM_EMAIL__' => $this->emetteur->email,
333 '__TOTAL_TTC__' =>
$object->total_ttc,
334 '__TOTAL_HT__' =>
$object->total_ht,
335 '__TOTAL_VAT__' =>
$object->total_tva
339 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
340 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
344 $paramfreetext =
'ORDER_FREE_TEXT';
350 require_once ODTPHP_PATH.
'odf.php';
352 $odfHandler =
new Odf(
355 'PATH_TO_TMP' => $conf->recruitment->dir_temp,
356 'ZIP_PROXY' =>
'PclZipProxy',
357 'DELIMITER_LEFT' =>
'{',
358 'DELIMITER_RIGHT' =>
'}'
362 $this->error = $e->getMessage();
375 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
376 }
catch (OdfException $e) {
383 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
389 $array_thirdparty_contact = array();
390 if ($usecontact && is_object($contactobject)) {
394 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
398 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
399 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
401 foreach ($tmparray as $key => $value) {
403 if (preg_match(
'/logo$/', $key)) {
405 if (file_exists($value)) {
406 $odfHandler->setImage($key, $value);
408 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
412 $odfHandler->setVars($key, $value,
true,
'UTF-8');
414 }
catch (OdfException $e) {
419 $foundtagforlines = 1;
421 $listlines = $odfHandler->setSegment(
'lines');
422 }
catch (OdfExceptionSegmentNotFound $e) {
424 $foundtagforlines = 0;
427 if ($foundtagforlines) {
429 foreach (
$object->lines as $line) {
434 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
435 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
436 foreach ($tmparray as $key => $val) {
438 $listlines->setVars($key, $val,
true,
'UTF-8');
439 }
catch (SegmentException $e) {
446 $odfHandler->mergeSegment($listlines);
447 }
catch (OdfException $e) {
448 $this->error = $e->getMessage();
455 $tmparray = $outputlangs->get_translations_for_substitutions();
456 foreach ($tmparray as $key => $value) {
458 $odfHandler->setVars($key, $value,
true,
'UTF-8');
459 }
catch (OdfException $e) {
466 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
467 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
472 $odfHandler->exportAsAttachedPDF($file);
474 $this->error = $e->getMessage();
480 $odfHandler->saveToDisk($file);
482 $this->error = $e->getMessage();
488 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
489 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
495 $this->result = array(
'fullpath' => $file);
499 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);