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 $hookmanager->initHooks(array(
'odtgeneration'));
243 if (!is_object($outputlangs)) {
244 $outputlangs = $langs;
246 $sav_charset_output = $outputlangs->charset_output;
247 $outputlangs->charset_output =
'UTF-8';
249 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
251 if ($conf->recruitment->dir_output) {
263 $dir = $conf->recruitment->multidir_output[isset(
$object->entity) ?
$object->entity : 1].
'/recruitmentjobposition/';
265 if (!preg_match(
'/specimen/i', $objectref)) {
266 $dir .=
"/".$objectref;
268 $file = $dir.
"/".$objectref.
".odt";
270 if (!file_exists($dir)) {
272 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
277 if (file_exists($dir)) {
279 $newfile = basename($srctemplatepath);
280 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
281 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
282 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
283 $newfiletmp = $objectref.
'_'.$newfiletmp;
286 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
289 if ($format ==
'1') {
290 $format =
'%Y%m%d%H%M%S';
294 $filename = $newfiletmp.
'.'.$newfileformat;
296 $file = $dir.
'/'.$filename;
303 if (!is_writable($conf->recruitment->dir_temp)) {
304 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->recruitment->dir_temp);
305 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
311 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
312 if (count($arrayidcontact) > 0) {
314 $result =
$object->fetch_contact($arrayidcontact[0]);
318 $contactobject =
null;
319 if (!empty($usecontact)) {
321 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
322 $object->contact->fetch_thirdparty();
323 $socobject =
$object->contact->thirdparty;
324 $contactobject =
$object->contact;
326 $socobject =
$object->thirdparty;
328 $contactobject =
$object->contact;
331 $socobject =
$object->thirdparty;
335 $substitutionarray = array(
336 '__FROM_NAME__' => $this->emetteur->name,
337 '__FROM_EMAIL__' => $this->emetteur->email,
338 '__TOTAL_TTC__' =>
$object->total_ttc,
339 '__TOTAL_HT__' =>
$object->total_ht,
340 '__TOTAL_VAT__' =>
$object->total_tva
344 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
345 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
349 $paramfreetext =
'ORDER_FREE_TEXT';
355 require_once ODTPHP_PATH.
'odf.php';
357 $odfHandler =
new Odf(
360 'PATH_TO_TMP' => $conf->recruitment->dir_temp,
362 'DELIMITER_LEFT' =>
'{',
363 'DELIMITER_RIGHT' =>
'}'
367 $this->error = $e->getMessage();
380 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
381 }
catch (OdfException $e) {
388 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
394 $array_thirdparty_contact = array();
395 if ($usecontact && is_object($contactobject)) {
399 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
403 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
404 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
406 foreach ($tmparray as $key => $value) {
408 if (preg_match(
'/logo$/', $key)) {
410 if (file_exists($value)) {
411 $odfHandler->setImage($key, $value);
413 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
417 $odfHandler->setVars($key, $value,
true,
'UTF-8');
419 }
catch (OdfException $e) {
424 $foundtagforlines = 1;
426 $listlines = $odfHandler->setSegment(
'lines');
427 }
catch (OdfExceptionSegmentNotFound $e) {
429 $foundtagforlines = 0;
432 if ($foundtagforlines) {
434 foreach (
$object->lines as $line) {
439 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
440 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
441 foreach ($tmparray as $key => $val) {
443 $listlines->setVars($key, $val,
true,
'UTF-8');
444 }
catch (SegmentException $e) {
451 $odfHandler->mergeSegment($listlines);
452 }
catch (OdfException $e) {
453 $this->error = $e->getMessage();
460 $tmparray = $outputlangs->get_translations_for_substitutions();
461 foreach ($tmparray as $key => $value) {
463 $odfHandler->setVars($key, $value,
true,
'UTF-8');
464 }
catch (OdfException $e) {
471 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
472 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
477 $odfHandler->exportAsAttachedPDF($file);
479 $this->error = $e->getMessage();
485 $odfHandler->saveToDisk($file);
487 $this->error = $e->getMessage();
493 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
494 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
500 $this->result = array(
'fullpath' => $file);
504 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);