463 global $user, $langs, $conf, $mysoc, $hookmanager;
465 if (empty($srctemplatepath)) {
466 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
470 if (!is_object($outputlangs)) {
471 $outputlangs = $langs;
473 $sav_charset_output = $outputlangs->charset_output;
474 $outputlangs->charset_output =
'UTF-8';
477 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
479 if ($conf->project->dir_output) {
490 $project =
new Project($this->db);
491 $project->fetch(
$object->fk_project);
492 $project->fetch_thirdparty();
494 $dir = $conf->project->dir_output.
"/".$project->ref.
"/";
496 if (!preg_match(
'/specimen/i', $objectref)) {
497 $dir .=
"/".$objectref;
499 $file = $dir.
"/".$objectref.
".odt";
501 if (!file_exists($dir)) {
503 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
509 if (file_exists($dir)) {
511 $newfile = basename($srctemplatepath);
512 $newfiletmp = preg_replace(
'/\.(ods|odt)/i',
'', $newfile);
513 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
514 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
515 $newfiletmp = $objectref .
'_' . $newfiletmp;
517 $file = $dir .
'/' . $newfiletmp .
'.odt';
524 if (!is_writable($conf->project->dir_temp)) {
525 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
526 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
530 $socobject = $project->thirdparty;
533 $substitutionarray = array(
534 '__FROM_NAME__' => $this->emetteur->name,
535 '__FROM_EMAIL__' => $this->emetteur->email,
541 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
542 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
545 require_once ODTPHP_PATH.
'odf.php';
547 $odfHandler =
new Odf(
550 'PATH_TO_TMP' => $conf->project->dir_temp,
552 'DELIMITER_LEFT' =>
'{',
553 'DELIMITER_RIGHT' =>
'}'
557 $this->error = $e->getMessage();
576 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
579 foreach ($tmparray as $key => $value) {
581 if (preg_match(
'/logo$/', $key)) {
582 if (file_exists($value)) {
583 $odfHandler->setImage($key, $value);
585 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
588 $odfHandler->setVars($key, $value,
true,
'UTF-8');
590 }
catch (OdfException $e) {
599 if (!empty($project->fk_soc)) {
600 $socid = $project->fk_soc;
605 foreach ($tmparray as $key => $val) {
607 $odfHandler->setVars($key, $val,
true,
'UTF-8');
608 }
catch (OdfException $e) {
610 }
catch (SegmentException $e) {
616 $sourcearray = array(
'internal',
'external');
617 $contact_arrray = array();
618 foreach ($sourcearray as $source) {
619 $contact_temp =
$object->liste_contact(-1, $source);
620 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
621 $contact_arrray = array_merge($contact_arrray, $contact_temp);
624 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
625 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
627 foreach ($contact_arrray as $contact) {
628 if ($contact[
'source'] ==
'internal') {
629 $objectdetail =
new User($this->db);
630 $objectdetail->fetch($contact[
'id']);
631 $contact[
'socname'] = $mysoc->name;
632 } elseif ($contact[
'source'] ==
'external') {
633 $objectdetail =
new Contact($this->db);
634 $objectdetail->fetch($contact[
'id']);
637 $soc->fetch($contact[
'socid']);
638 $contact[
'socname'] = $soc->name;
640 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
644 foreach ($tmparray as $key => $val) {
646 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
647 }
catch (OdfException $e) {
649 }
catch (SegmentException $e) {
653 $listlinestaskres->merge();
655 $odfHandler->mergeSegment($listlinestaskres);
659 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
660 $sql .=
", u.lastname, u.firstname";
661 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
662 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
663 $sql .=
" WHERE t.fk_element =".((int)
$object->id);
664 $sql .=
" AND t.elementtype = 'task'";
665 $sql .=
" AND t.fk_user = u.rowid";
666 $sql .=
" ORDER BY t.element_date DESC";
668 $resql = $this->db->query($sql);
670 $num = $this->db->num_rows($resql);
673 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
675 $row = $this->db->fetch_array($resql);
676 if (!empty($row[
'fk_user'])) {
677 $objectdetail =
new User($this->db);
678 $objectdetail->fetch($row[
'fk_user']);
680 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
682 $row[
'fullcivname'] =
'';
687 foreach ($tmparray as $key => $val) {
689 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
690 }
catch (OdfException $e) {
692 }
catch (SegmentException $e) {
696 $listlinestasktime->merge();
699 $this->db->free($resql);
701 $odfHandler->mergeSegment($listlinestasktime);
706 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
709 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
712 foreach ($filearray as $filedetail) {
715 foreach ($tmparray as $key => $val) {
717 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
718 }
catch (OdfException $e) {
720 }
catch (SegmentException $e) {
724 $listtasksfiles->merge();
728 $odfHandler->mergeSegment($listtasksfiles);
729 }
catch (OdfException $e) {
730 $this->error = $e->getMessage();
739 $listlines = $odfHandler->setSegment(
'projectfiles');
742 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
745 foreach ($filearray as $filedetail) {
749 foreach ($tmparray as $key => $val) {
751 $listlines->setVars($key, $val,
true,
'UTF-8');
752 }
catch (OdfException $e) {
754 }
catch (SegmentException $e) {
760 $odfHandler->mergeSegment($listlines);
761 }
catch (OdfException $e) {
762 $this->error = $e->getMessage();
768 $sourcearray = array(
'internal',
'external');
769 $contact_arrray = array();
770 foreach ($sourcearray as $source) {
771 $contact_temp = $project->liste_contact(-1, $source);
772 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
773 $contact_arrray = array_merge($contact_arrray, $contact_temp);
776 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
778 $listlines = $odfHandler->setSegment(
'projectcontacts');
780 foreach ($contact_arrray as $contact) {
781 if ($contact[
'source'] ==
'internal') {
782 $objectdetail =
new User($this->db);
783 $objectdetail->fetch($contact[
'id']);
784 $contact[
'socname'] = $mysoc->name;
785 } elseif ($contact[
'source'] ==
'external') {
786 $objectdetail =
new Contact($this->db);
787 $objectdetail->fetch($contact[
'id']);
790 $soc->fetch($contact[
'socid']);
791 $contact[
'socname'] = $soc->name;
793 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
797 foreach ($tmparray as $key => $val) {
799 $listlines->setVars($key, $val,
true,
'UTF-8');
800 }
catch (OdfException $e) {
802 }
catch (SegmentException $e) {
808 $odfHandler->mergeSegment($listlines);
809 }
catch (OdfException $e) {
810 $this->error = $e->getMessage();
818 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
819 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
825 $odfHandler->exportAsAttachedPDF($file);
827 $this->error = $e->getMessage();
833 $odfHandler->saveToDisk($file);
835 $this->error = $e->getMessage();
840 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
841 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
847 $this->result = array(
'fullpath' => $file);
851 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);