468 global $user, $langs,
$conf, $mysoc, $hookmanager;
470 if (empty($srctemplatepath)) {
471 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
476 if (!is_object($hookmanager)) {
477 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
480 $hookmanager->initHooks(array(
'odtgeneration'));
483 if (!is_object($outputlangs)) {
484 $outputlangs = $langs;
486 $sav_charset_output = $outputlangs->charset_output;
487 $outputlangs->charset_output =
'UTF-8';
490 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
492 if (
$conf->project->dir_output) {
503 $project =
new Project($this->db);
504 $project->fetch(
$object->fk_project);
505 $project->fetch_thirdparty();
507 $dir =
$conf->project->dir_output.
"/".$project->ref.
"/";
509 if (!preg_match(
'/specimen/i', $objectref)) {
510 $dir .=
"/".$objectref;
512 $file = $dir.
"/".$objectref.
".odt";
514 if (!file_exists($dir)) {
516 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
522 if (file_exists($dir)) {
524 $newfile = basename($srctemplatepath);
525 $newfiletmp = preg_replace(
'/\.(ods|odt)/i',
'', $newfile);
526 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
527 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
528 $newfiletmp = $objectref .
'_' . $newfiletmp;
530 $file = $dir .
'/' . $newfiletmp .
'.odt';
537 if (!is_writable(
$conf->project->dir_temp)) {
538 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->project->dir_temp);
539 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
543 $socobject = $project->thirdparty;
546 $substitutionarray = array(
547 '__FROM_NAME__' => $this->emetteur->name,
548 '__FROM_EMAIL__' => $this->emetteur->email,
554 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
555 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
558 require_once ODTPHP_PATH.
'odf.php';
560 $odfHandler =
new Odf(
563 'PATH_TO_TMP' =>
$conf->project->dir_temp,
565 'DELIMITER_LEFT' =>
'{',
566 'DELIMITER_RIGHT' =>
'}'
570 $this->error = $e->getMessage();
589 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
592 foreach ($tmparray as $key => $value) {
594 if (preg_match(
'/logo$/', $key)) {
595 if (file_exists($value)) {
596 $odfHandler->setImage($key, $value);
598 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
601 $odfHandler->setVars($key, $value,
true,
'UTF-8');
603 }
catch (OdfException $e) {
612 if (!empty($project->fk_soc)) {
613 $socid = $project->fk_soc;
618 foreach ($tmparray as $key => $val) {
620 $odfHandler->setVars($key, $val,
true,
'UTF-8');
621 }
catch (OdfException $e) {
627 $sourcearray = array(
'internal',
'external');
628 $contact_arrray = array();
629 foreach ($sourcearray as $source) {
630 $contact_temp =
$object->liste_contact(-1, $source);
631 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
632 $contact_arrray = array_merge($contact_arrray, $contact_temp);
636 $foundtagforlines = 1;
638 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
639 }
catch (OdfExceptionSegmentNotFound $e) {
641 $foundtagforlines = 0;
644 if ($foundtagforlines && (is_array($contact_arrray) && count($contact_arrray) > 0)) {
645 foreach ($contact_arrray as $contact) {
646 if ($contact[
'source'] ==
'internal') {
647 $objectdetail =
new User($this->db);
648 $objectdetail->fetch($contact[
'id']);
649 $contact[
'socname'] = $mysoc->name;
650 } elseif ($contact[
'source'] ==
'external') {
651 $objectdetail =
new Contact($this->db);
652 $objectdetail->fetch($contact[
'id']);
655 $soc->fetch($contact[
'socid']);
656 $contact[
'socname'] = $soc->name;
658 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
662 foreach ($tmparray as $key => $val) {
664 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
665 }
catch (SegmentException $e) {
669 $listlinestaskres->merge();
671 $odfHandler->mergeSegment($listlinestaskres);
675 $foundtagforlines = 1;
677 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
678 }
catch (OdfExceptionSegmentNotFound $e) {
680 $foundtagforlines = 0;
685 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
686 $sql .=
", u.lastname, u.firstname";
687 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
688 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
689 $sql .=
" WHERE t.fk_element =".((int)
$object->id);
690 $sql .=
" AND t.elementtype = 'task'";
691 $sql .=
" AND t.fk_user = u.rowid";
692 $sql .=
" ORDER BY t.element_date DESC";
694 $resql = $this->db->query($sql);
695 if ($foundtagforlines && $resql) {
696 $num = $this->db->num_rows($resql);
701 $row = $this->db->fetch_array($resql);
702 if (!empty($row[
'fk_user'])) {
703 $objectdetail =
new User($this->db);
704 $objectdetail->fetch($row[
'fk_user']);
706 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
708 $row[
'fullcivname'] =
'';
713 foreach ($tmparray as $key => $val) {
715 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
716 }
catch (SegmentException $e) {
720 $listlinestasktime->merge();
723 $this->db->free($resql);
725 $odfHandler->mergeSegment($listlinestasktime);
731 $foundtagforlines = 1;
733 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
734 }
catch (OdfExceptionSegmentNotFound $e) {
736 $foundtagforlines = 0;
739 if ($foundtagforlines) {
741 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
743 foreach ($filearray as $filedetail) {
746 foreach ($tmparray as $key => $val) {
748 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
749 }
catch (SegmentException $e) {
753 $listtasksfiles->merge();
757 $odfHandler->mergeSegment($listtasksfiles);
759 }
catch (OdfException $e) {
760 $this->error = $e->getMessage();
768 $foundtagforlines = 1;
770 $listlines = $odfHandler->setSegment(
'projectfiles');
771 }
catch (OdfExceptionSegmentNotFound $e) {
773 $foundtagforlines = 0;
776 if ($foundtagforlines) {
779 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
781 foreach ($filearray as $filedetail) {
785 foreach ($tmparray as $key => $val) {
787 $listlines->setVars($key, $val,
true,
'UTF-8');
788 }
catch (SegmentException $e) {
794 $odfHandler->mergeSegment($listlines);
795 }
catch (OdfException $e) {
796 $this->error = $e->getMessage();
803 $sourcearray = array(
'internal',
'external');
804 $contact_arrray = array();
805 foreach ($sourcearray as $source) {
806 $contact_temp = $project->liste_contact(-1, $source);
807 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
808 $contact_arrray = array_merge($contact_arrray, $contact_temp);
812 $foundtagforlines = 1;
814 $listlines = $odfHandler->setSegment(
'projectcontacts');
815 }
catch (OdfExceptionSegmentNotFound $e) {
817 $foundtagforlines = 0;
820 if ($foundtagforlines && (is_array($contact_arrray) && count($contact_arrray) > 0)) {
822 foreach ($contact_arrray as $contact) {
823 if ($contact[
'source'] ==
'internal') {
824 $objectdetail =
new User($this->db);
825 $objectdetail->fetch($contact[
'id']);
826 $contact[
'socname'] = $mysoc->name;
827 } elseif ($contact[
'source'] ==
'external') {
828 $objectdetail =
new Contact($this->db);
829 $objectdetail->fetch($contact[
'id']);
832 $soc->fetch($contact[
'socid']);
833 $contact[
'socname'] = $soc->name;
835 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
839 foreach ($tmparray as $key => $val) {
841 $listlines->setVars($key, $val,
true,
'UTF-8');
842 }
catch (SegmentException $e) {
848 $odfHandler->mergeSegment($listlines);
849 }
catch (OdfException $e) {
850 $this->error = $e->getMessage();
858 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
859 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
865 $odfHandler->exportAsAttachedPDF($file);
867 $this->error = $e->getMessage();
873 $odfHandler->saveToDisk($file);
875 $this->error = $e->getMessage();
880 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
881 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
887 $this->result = array(
'fullpath' => $file);
891 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);