468 global $user, $langs, $conf, $mysoc, $hookmanager;
470 if (empty($srctemplatepath)) {
471 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
475 if (!is_object($outputlangs)) {
476 $outputlangs = $langs;
478 $sav_charset_output = $outputlangs->charset_output;
479 $outputlangs->charset_output =
'UTF-8';
482 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
484 if ($conf->project->dir_output) {
495 $project =
new Project($this->db);
496 $project->fetch(
$object->fk_project);
497 $project->fetch_thirdparty();
499 $dir = $conf->project->dir_output.
"/".$project->ref.
"/";
501 if (!preg_match(
'/specimen/i', $objectref)) {
502 $dir .=
"/".$objectref;
504 $file = $dir.
"/".$objectref.
".odt";
506 if (!file_exists($dir)) {
508 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
514 if (file_exists($dir)) {
516 $newfile = basename($srctemplatepath);
517 $newfiletmp = preg_replace(
'/\.(ods|odt)/i',
'', $newfile);
518 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
519 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
520 $newfiletmp = $objectref .
'_' . $newfiletmp;
522 $file = $dir .
'/' . $newfiletmp .
'.odt';
529 if (!is_writable($conf->project->dir_temp)) {
530 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
531 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
535 $socobject = $project->thirdparty;
538 $substitutionarray = array(
539 '__FROM_NAME__' => $this->emetteur->name,
540 '__FROM_EMAIL__' => $this->emetteur->email,
546 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
547 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
550 require_once ODTPHP_PATH.
'odf.php';
552 $odfHandler =
new Odf(
555 'PATH_TO_TMP' => $conf->project->dir_temp,
557 'DELIMITER_LEFT' =>
'{',
558 'DELIMITER_RIGHT' =>
'}'
562 $this->error = $e->getMessage();
581 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
584 foreach ($tmparray as $key => $value) {
586 if (preg_match(
'/logo$/', $key)) {
587 if (file_exists($value)) {
588 $odfHandler->setImage($key, $value);
590 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
593 $odfHandler->setVars($key, $value,
true,
'UTF-8');
595 }
catch (OdfException $e) {
604 if (!empty($project->fk_soc)) {
605 $socid = $project->fk_soc;
610 foreach ($tmparray as $key => $val) {
612 $odfHandler->setVars($key, $val,
true,
'UTF-8');
613 }
catch (OdfException $e) {
619 $sourcearray = array(
'internal',
'external');
620 $contact_arrray = array();
621 foreach ($sourcearray as $source) {
622 $contact_temp =
$object->liste_contact(-1, $source);
623 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
624 $contact_arrray = array_merge($contact_arrray, $contact_temp);
628 $foundtagforlines = 1;
630 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
631 }
catch (OdfExceptionSegmentNotFound $e) {
633 $foundtagforlines = 0;
636 if ($foundtagforlines && (is_array($contact_arrray) && count($contact_arrray) > 0)) {
637 foreach ($contact_arrray as $contact) {
638 if ($contact[
'source'] ==
'internal') {
639 $objectdetail =
new User($this->db);
640 $objectdetail->fetch($contact[
'id']);
641 $contact[
'socname'] = $mysoc->name;
642 } elseif ($contact[
'source'] ==
'external') {
643 $objectdetail =
new Contact($this->db);
644 $objectdetail->fetch($contact[
'id']);
647 $soc->fetch($contact[
'socid']);
648 $contact[
'socname'] = $soc->name;
650 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
654 foreach ($tmparray as $key => $val) {
656 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
657 }
catch (SegmentException $e) {
661 $listlinestaskres->merge();
663 $odfHandler->mergeSegment($listlinestaskres);
667 $foundtagforlines = 1;
669 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
670 }
catch (OdfExceptionSegmentNotFound $e) {
672 $foundtagforlines = 0;
677 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
678 $sql .=
", u.lastname, u.firstname";
679 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
680 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
681 $sql .=
" WHERE t.fk_element =".((int)
$object->id);
682 $sql .=
" AND t.elementtype = 'task'";
683 $sql .=
" AND t.fk_user = u.rowid";
684 $sql .=
" ORDER BY t.element_date DESC";
686 $resql = $this->db->query($sql);
687 if ($foundtagforlines && $resql) {
688 $num = $this->db->num_rows($resql);
693 $row = $this->db->fetch_array($resql);
694 if (!empty($row[
'fk_user'])) {
695 $objectdetail =
new User($this->db);
696 $objectdetail->fetch($row[
'fk_user']);
698 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
700 $row[
'fullcivname'] =
'';
705 foreach ($tmparray as $key => $val) {
707 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
708 }
catch (SegmentException $e) {
712 $listlinestasktime->merge();
715 $this->db->free($resql);
717 $odfHandler->mergeSegment($listlinestasktime);
723 $foundtagforlines = 1;
725 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
726 }
catch (OdfExceptionSegmentNotFound $e) {
728 $foundtagforlines = 0;
731 if ($foundtagforlines) {
733 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
735 foreach ($filearray as $filedetail) {
738 foreach ($tmparray as $key => $val) {
740 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
741 }
catch (SegmentException $e) {
745 $listtasksfiles->merge();
749 $odfHandler->mergeSegment($listtasksfiles);
751 }
catch (OdfException $e) {
752 $this->error = $e->getMessage();
760 $foundtagforlines = 1;
762 $listlines = $odfHandler->setSegment(
'projectfiles');
763 }
catch (OdfExceptionSegmentNotFound $e) {
765 $foundtagforlines = 0;
768 if ($foundtagforlines) {
771 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
773 foreach ($filearray as $filedetail) {
777 foreach ($tmparray as $key => $val) {
779 $listlines->setVars($key, $val,
true,
'UTF-8');
780 }
catch (SegmentException $e) {
786 $odfHandler->mergeSegment($listlines);
787 }
catch (OdfException $e) {
788 $this->error = $e->getMessage();
795 $sourcearray = array(
'internal',
'external');
796 $contact_arrray = array();
797 foreach ($sourcearray as $source) {
798 $contact_temp = $project->liste_contact(-1, $source);
799 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
800 $contact_arrray = array_merge($contact_arrray, $contact_temp);
804 $foundtagforlines = 1;
806 $listlines = $odfHandler->setSegment(
'projectcontacts');
807 }
catch (OdfExceptionSegmentNotFound $e) {
809 $foundtagforlines = 0;
812 if ($foundtagforlines && (is_array($contact_arrray) && count($contact_arrray) > 0)) {
814 foreach ($contact_arrray as $contact) {
815 if ($contact[
'source'] ==
'internal') {
816 $objectdetail =
new User($this->db);
817 $objectdetail->fetch($contact[
'id']);
818 $contact[
'socname'] = $mysoc->name;
819 } elseif ($contact[
'source'] ==
'external') {
820 $objectdetail =
new Contact($this->db);
821 $objectdetail->fetch($contact[
'id']);
824 $soc->fetch($contact[
'socid']);
825 $contact[
'socname'] = $soc->name;
827 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
831 foreach ($tmparray as $key => $val) {
833 $listlines->setVars($key, $val,
true,
'UTF-8');
834 }
catch (SegmentException $e) {
840 $odfHandler->mergeSegment($listlines);
841 }
catch (OdfException $e) {
842 $this->error = $e->getMessage();
850 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
851 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
857 $odfHandler->exportAsAttachedPDF($file);
859 $this->error = $e->getMessage();
865 $odfHandler->saveToDisk($file);
867 $this->error = $e->getMessage();
872 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
873 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
879 $this->result = array(
'fullpath' => $file);
883 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);