230 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
233 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';
255 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
257 if ($conf->user->dir_output) {
259 if (!is_object($object)) {
262 $result = $object->fetch($id);
269 $dir = $conf->user->dir_output.
'/usergroups';
271 if (!preg_match(
'/specimen/i', $objectref)) {
272 $dir .=
"/".$objectref;
274 $file = $dir.
"/".$objectref.
".odt";
276 if (!file_exists($dir)) {
278 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
283 if (file_exists($dir)) {
285 $newfile = basename($srctemplatepath);
286 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
287 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
288 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
290 $newfiletmp = $objectref .
'_' . $newfiletmp;
293 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
296 if ($format ==
'1') {
297 $format =
'%Y%m%d%H%M%S';
301 $filename = $newfiletmp .
'.' . $newfileformat;
303 $file = $dir .
'/' . $filename;
310 if (!is_writable($conf->user->dir_temp)) {
311 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->user->dir_temp);
312 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
318 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
319 if (count($arrayidcontact) > 0) {
321 $result = $object->fetch_contact($arrayidcontact[0]);
325 if (!empty($usecontact)) {
327 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
328 $object->contact->fetch_thirdparty();
329 $socobject = $object->contact->thirdparty;
330 $contactobject = $object->contact;
332 $socobject = $object->thirdparty;
334 $contactobject = $object->contact;
337 $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 =
'user_FREE_TEXT';
355 if (!empty($conf->global->$paramfreetext)) {
360 require_once ODTPHP_PATH.
'odf.php';
362 $odfHandler =
new Odf(
365 'PATH_TO_TMP' => $conf->user->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) {
398 $array_thirdparty_contact = array();
399 if ($usecontact && is_object($contactobject)) {
403 $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
405 $object->fetch_optionals();
407 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
408 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
409 foreach ($tmparray as $key => $value) {
411 if (preg_match(
'/logo$/', $key)) {
412 if (file_exists($value)) {
413 $odfHandler->setImage($key, $value);
415 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
418 $odfHandler->setVars($key, $value,
true,
'UTF-8');
420 }
catch (OdfException $e) {
426 $foundtagforlines = 1;
428 $listlines = $odfHandler->setSegment(
'lines');
429 }
catch (OdfExceptionSegmentNotFound $e) {
431 $foundtagforlines = 0;
433 }
catch (OdfException $e) {
434 $foundtagforlines = 0;
437 if ($foundtagforlines) {
438 foreach ($object->members as $u) {
440 unset($tmparray[
'object_pass']);
441 unset($tmparray[
'object_pass_indatabase']);
444 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$u);
445 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
446 foreach ($tmparray as $key => $val) {
448 if (!is_array($val)) {
449 $listlines->setVars($key, $val,
true,
'UTF-8');
451 }
catch (OdfException $e) {
453 }
catch (SegmentException $e) {
459 $odfHandler->mergeSegment($listlines);
461 }
catch (OdfException $e) {
462 $this->error = $e->getMessage();
468 $tmparray = $outputlangs->get_translations_for_substitutions();
469 foreach ($tmparray as $key => $value) {
471 $odfHandler->setVars($key, $value,
true,
'UTF-8');
472 }
catch (OdfException $e) {
478 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
479 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
484 $odfHandler->exportAsAttachedPDF($file);
486 $this->error = $e->getMessage();
492 $odfHandler->saveToDisk($file);
494 $this->error = $e->getMessage();
500 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
506 $this->result = array(
'fullpath'=>$file);
510 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);