212 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 global $user, $langs, $conf, $mysoc, $hookmanager;
218 if (empty($srctemplatepath)) {
219 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
224 if (!is_object($hookmanager)) {
225 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
228 $hookmanager->initHooks(array(
'odtgeneration'));
231 if (!is_object($outputlangs)) {
232 $outputlangs = $langs;
234 $sav_charset_output = $outputlangs->charset_output;
235 $outputlangs->charset_output =
'UTF-8';
237 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
239 if ($conf->recruitment->dir_output) {
241 if (!is_object($object)) {
244 $result = $object->fetch($id);
251 $dir = $conf->recruitment->multidir_output[isset($object->entity) ? $object->entity : 1].
'/recruitmentjobposition/';
253 if (!preg_match(
'/specimen/i', $objectref)) {
254 $dir .=
"/".$objectref;
256 $file = $dir.
"/".$objectref.
".odt";
258 if (!file_exists($dir)) {
260 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
265 if (file_exists($dir)) {
267 $newfile = basename($srctemplatepath);
268 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
269 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
270 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
271 $newfiletmp = $objectref.
'_'.$newfiletmp;
274 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
277 if ($format ==
'1') {
278 $format =
'%Y%m%d%H%M%S';
282 $filename = $newfiletmp.
'.'.$newfileformat;
284 $file = $dir.
'/'.$filename;
291 if (!is_writable($conf->recruitment->dir_temp)) {
292 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->recruitment->dir_temp);
293 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
299 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
300 if (count($arrayidcontact) > 0) {
302 $result = $object->fetch_contact($arrayidcontact[0]);
306 $contactobject =
null;
307 if (!empty($usecontact)) {
309 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
310 $object->contact->fetch_thirdparty();
311 $socobject = $object->contact->thirdparty;
312 $contactobject = $object->contact;
314 $socobject = $object->thirdparty;
316 $contactobject = $object->contact;
319 $socobject = $object->thirdparty;
323 $substitutionarray = array(
324 '__FROM_NAME__' => $this->emetteur->name,
325 '__FROM_EMAIL__' => $this->emetteur->email,
326 '__TOTAL_TTC__' => $object->total_ttc,
327 '__TOTAL_HT__' => $object->total_ht,
328 '__TOTAL_VAT__' => $object->total_tva
332 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
333 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
337 $paramfreetext =
'ORDER_FREE_TEXT';
338 if (!empty($conf->global->$paramfreetext)) {
343 require_once ODTPHP_PATH.
'odf.php';
345 $odfHandler =
new Odf(
348 'PATH_TO_TMP' => $conf->recruitment->dir_temp,
349 'ZIP_PROXY' =>
'PclZipProxy',
350 'DELIMITER_LEFT' =>
'{',
351 'DELIMITER_RIGHT' =>
'}'
355 $this->error = $e->getMessage();
368 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
369 }
catch (OdfException $e) {
382 $array_thirdparty_contact = array();
383 if ($usecontact && is_object($contactobject)) {
387 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
391 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
392 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
394 foreach ($tmparray as $key => $value) {
396 if (preg_match(
'/logo$/', $key)) {
398 if (file_exists($value)) {
399 $odfHandler->setImage($key, $value);
401 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
405 $odfHandler->setVars($key, $value,
true,
'UTF-8');
407 }
catch (OdfException $e) {
413 $foundtagforlines = 1;
415 $listlines = $odfHandler->setSegment(
'lines');
416 }
catch (OdfException $e) {
418 $foundtagforlines = 0;
421 if ($foundtagforlines) {
423 foreach ($object->lines as $line) {
428 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
429 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
430 foreach ($tmparray as $key => $val) {
432 $listlines->setVars($key, $val,
true,
'UTF-8');
433 }
catch (OdfException $e) {
435 }
catch (SegmentException $e) {
441 $odfHandler->mergeSegment($listlines);
443 }
catch (OdfException $e) {
444 $this->error = $e->getMessage();
450 $tmparray = $outputlangs->get_translations_for_substitutions();
451 foreach ($tmparray as $key => $value) {
453 $odfHandler->setVars($key, $value,
true,
'UTF-8');
454 }
catch (OdfException $e) {
461 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
462 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
465 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
467 $odfHandler->exportAsAttachedPDF($file);
469 $this->error = $e->getMessage();
475 $odfHandler->saveToDisk($file);
477 $this->error = $e->getMessage();
483 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
484 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
490 $this->result = array(
'fullpath'=>$file);
494 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);