223 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
226 global $user, $langs, $conf, $mysoc, $hookmanager;
229 if (empty($srctemplatepath)) {
230 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
235 $hookmanager->initHooks(array(
'odtgeneration'));
237 if (!is_object($outputlangs)) {
238 $outputlangs = $langs;
240 $sav_charset_output = $outputlangs->charset_output;
241 $outputlangs->charset_output =
'UTF-8';
243 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
245 if ($conf->recruitment->dir_output) {
247 if (!is_object($object)) {
250 $result = $object->fetch($id);
257 $dir = $conf->recruitment->multidir_output[isset($object->entity) ? $object->entity : 1].
'/recruitmentjobposition/';
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->recruitment->dir_temp)) {
298 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->recruitment->dir_temp);
299 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
305 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
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) ||
getDolGlobalString(
'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 =
'ORDER_FREE_TEXT';
349 require_once ODTPHP_PATH.
'odf.php';
351 $odfHandler =
new Odf(
354 'PATH_TO_TMP' => $conf->recruitment->dir_temp,
356 'DELIMITER_LEFT' =>
'{',
357 'DELIMITER_RIGHT' =>
'}'
361 $this->error = $e->getMessage();
374 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
375 }
catch (OdfException $e) {
388 $array_thirdparty_contact = array();
389 if ($usecontact && is_object($contactobject)) {
393 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
397 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
398 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
400 foreach ($tmparray as $key => $value) {
402 if (preg_match(
'/logo$/', $key)) {
404 if (file_exists($value)) {
405 $odfHandler->setImage($key, $value);
407 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
411 $odfHandler->setVars($key, $value,
true,
'UTF-8');
413 }
catch (OdfException $e) {
419 $foundtagforlines = 1;
421 $listlines = $odfHandler->setSegment(
'lines');
422 }
catch (OdfExceptionSegmentNotFound $e) {
424 $foundtagforlines = 0;
426 }
catch (OdfException $e) {
427 $foundtagforlines = 0;
430 if ($foundtagforlines) {
432 foreach ($object->lines as $line) {
437 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
438 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
439 foreach ($tmparray as $key => $val) {
441 $listlines->setVars($key, $val,
true,
'UTF-8');
442 }
catch (OdfException $e) {
444 }
catch (SegmentException $e) {
450 $odfHandler->mergeSegment($listlines);
452 }
catch (OdfException $e) {
453 $this->error = $e->getMessage();
459 $tmparray = $outputlangs->get_translations_for_substitutions();
460 foreach ($tmparray as $key => $value) {
462 $odfHandler->setVars($key, $value,
true,
'UTF-8');
463 }
catch (OdfException $e) {
470 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
471 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
476 $odfHandler->exportAsAttachedPDF($file);
478 $this->error = $e->getMessage();
484 $odfHandler->saveToDisk($file);
486 $this->error = $e->getMessage();
492 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
493 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
499 $this->result = array(
'fullpath'=>$file);
503 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);