463 global $user, $langs, $conf, $mysoc, $hookmanager;
465 if (empty($srctemplatepath)) {
466 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
471 if (!is_object($hookmanager)) {
472 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
475 $hookmanager->initHooks(array(
'odtgeneration'));
478 if (!is_object($outputlangs)) {
479 $outputlangs = $langs;
481 $sav_charset_output = $outputlangs->charset_output;
482 $outputlangs->charset_output =
'UTF-8';
485 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
487 if ($conf->project->dir_output) {
498 $project =
new Project($this->db);
499 $project->fetch(
$object->fk_project);
500 $project->fetch_thirdparty();
502 $dir = $conf->project->dir_output.
"/".$project->ref.
"/";
504 if (!preg_match(
'/specimen/i', $objectref)) {
505 $dir .=
"/".$objectref;
507 $file = $dir.
"/".$objectref.
".odt";
509 if (!file_exists($dir)) {
511 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
517 if (file_exists($dir)) {
519 $newfile = basename($srctemplatepath);
520 $newfiletmp = preg_replace(
'/\.(ods|odt)/i',
'', $newfile);
521 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
522 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
523 $newfiletmp = $objectref .
'_' . $newfiletmp;
525 $file = $dir .
'/' . $newfiletmp .
'.odt';
532 if (!is_writable($conf->project->dir_temp)) {
533 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
534 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
538 $socobject = $project->thirdparty;
541 $substitutionarray = array(
542 '__FROM_NAME__' => $this->emetteur->name,
543 '__FROM_EMAIL__' => $this->emetteur->email,
549 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
550 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
553 require_once ODTPHP_PATH.
'odf.php';
555 $odfHandler =
new Odf(
558 'PATH_TO_TMP' => $conf->project->dir_temp,
560 'DELIMITER_LEFT' =>
'{',
561 'DELIMITER_RIGHT' =>
'}'
565 $this->error = $e->getMessage();
584 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
587 foreach ($tmparray as $key => $value) {
589 if (preg_match(
'/logo$/', $key)) {
590 if (file_exists($value)) {
591 $odfHandler->setImage($key, $value);
593 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
596 $odfHandler->setVars($key, $value,
true,
'UTF-8');
598 }
catch (OdfException $e) {
607 if (!empty($project->fk_soc)) {
608 $socid = $project->fk_soc;
613 foreach ($tmparray as $key => $val) {
615 $odfHandler->setVars($key, $val,
true,
'UTF-8');
616 }
catch (OdfException $e) {
618 }
catch (SegmentException $e) {
624 $sourcearray = array(
'internal',
'external');
625 $contact_arrray = array();
626 foreach ($sourcearray as $source) {
627 $contact_temp =
$object->liste_contact(-1, $source);
628 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
629 $contact_arrray = array_merge($contact_arrray, $contact_temp);
632 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
633 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
635 foreach ($contact_arrray as $contact) {
636 if ($contact[
'source'] ==
'internal') {
637 $objectdetail =
new User($this->db);
638 $objectdetail->fetch($contact[
'id']);
639 $contact[
'socname'] = $mysoc->name;
640 } elseif ($contact[
'source'] ==
'external') {
641 $objectdetail =
new Contact($this->db);
642 $objectdetail->fetch($contact[
'id']);
645 $soc->fetch($contact[
'socid']);
646 $contact[
'socname'] = $soc->name;
648 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
652 foreach ($tmparray as $key => $val) {
654 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
655 }
catch (OdfException $e) {
657 }
catch (SegmentException $e) {
661 $listlinestaskres->merge();
663 $odfHandler->mergeSegment($listlinestaskres);
667 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
668 $sql .=
", u.lastname, u.firstname";
669 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
670 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
671 $sql .=
" WHERE t.fk_element =".((int)
$object->id);
672 $sql .=
" AND t.elementtype = 'task'";
673 $sql .=
" AND t.fk_user = u.rowid";
674 $sql .=
" ORDER BY t.element_date DESC";
676 $resql = $this->db->query($sql);
678 $num = $this->db->num_rows($resql);
681 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
683 $row = $this->db->fetch_array($resql);
684 if (!empty($row[
'fk_user'])) {
685 $objectdetail =
new User($this->db);
686 $objectdetail->fetch($row[
'fk_user']);
688 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
690 $row[
'fullcivname'] =
'';
695 foreach ($tmparray as $key => $val) {
697 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
698 }
catch (OdfException $e) {
700 }
catch (SegmentException $e) {
704 $listlinestasktime->merge();
707 $this->db->free($resql);
709 $odfHandler->mergeSegment($listlinestasktime);
714 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
717 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
720 foreach ($filearray as $filedetail) {
723 foreach ($tmparray as $key => $val) {
725 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
726 }
catch (OdfException $e) {
728 }
catch (SegmentException $e) {
732 $listtasksfiles->merge();
736 $odfHandler->mergeSegment($listtasksfiles);
737 }
catch (OdfException $e) {
738 $this->error = $e->getMessage();
747 $listlines = $odfHandler->setSegment(
'projectfiles');
750 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
753 foreach ($filearray as $filedetail) {
757 foreach ($tmparray as $key => $val) {
759 $listlines->setVars($key, $val,
true,
'UTF-8');
760 }
catch (OdfException $e) {
762 }
catch (SegmentException $e) {
768 $odfHandler->mergeSegment($listlines);
769 }
catch (OdfException $e) {
770 $this->error = $e->getMessage();
776 $sourcearray = array(
'internal',
'external');
777 $contact_arrray = array();
778 foreach ($sourcearray as $source) {
779 $contact_temp = $project->liste_contact(-1, $source);
780 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
781 $contact_arrray = array_merge($contact_arrray, $contact_temp);
784 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
786 $listlines = $odfHandler->setSegment(
'projectcontacts');
788 foreach ($contact_arrray as $contact) {
789 if ($contact[
'source'] ==
'internal') {
790 $objectdetail =
new User($this->db);
791 $objectdetail->fetch($contact[
'id']);
792 $contact[
'socname'] = $mysoc->name;
793 } elseif ($contact[
'source'] ==
'external') {
794 $objectdetail =
new Contact($this->db);
795 $objectdetail->fetch($contact[
'id']);
798 $soc->fetch($contact[
'socid']);
799 $contact[
'socname'] = $soc->name;
801 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
805 foreach ($tmparray as $key => $val) {
807 $listlines->setVars($key, $val,
true,
'UTF-8');
808 }
catch (OdfException $e) {
810 }
catch (SegmentException $e) {
816 $odfHandler->mergeSegment($listlines);
817 }
catch (OdfException $e) {
818 $this->error = $e->getMessage();
826 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
827 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
833 $odfHandler->exportAsAttachedPDF($file);
835 $this->error = $e->getMessage();
841 $odfHandler->saveToDisk($file);
843 $this->error = $e->getMessage();
848 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
849 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
855 $this->result = array(
'fullpath' => $file);
859 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);