462 public function write_file($object, $outputlangs, $srctemplatepath)
465 global $user, $langs, $conf, $mysoc, $hookmanager;
467 if (empty($srctemplatepath)) {
468 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
473 if (!is_object($hookmanager)) {
474 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
477 $hookmanager->initHooks(array(
'odtgeneration'));
480 if (!is_object($outputlangs)) {
481 $outputlangs = $langs;
483 $sav_charset_output = $outputlangs->charset_output;
484 $outputlangs->charset_output =
'UTF-8';
487 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
489 if ($conf->project->dir_output) {
491 if (!is_object($object)) {
493 $object =
new Task($this->db);
494 $result = $object->fetch($id);
500 $project =
new Project($this->db);
501 $project->fetch($object->fk_project);
502 $project->fetch_thirdparty();
504 $dir = $conf->project->dir_output.
"/".$project->ref.
"/";
506 if (!preg_match(
'/specimen/i', $objectref)) {
507 $dir .=
"/".$objectref;
509 $file = $dir.
"/".$objectref.
".odt";
511 if (!file_exists($dir)) {
513 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
519 if (file_exists($dir)) {
521 $newfile = basename($srctemplatepath);
522 $newfiletmp = preg_replace(
'/\.(ods|odt)/i',
'', $newfile);
523 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
524 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
525 $newfiletmp = $objectref .
'_' . $newfiletmp;
527 $file = $dir .
'/' . $newfiletmp .
'.odt';
534 if (!is_writable($conf->project->dir_temp)) {
535 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
536 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
540 $socobject = $project->thirdparty;
543 $substitutionarray = array(
544 '__FROM_NAME__' => $this->emetteur->name,
545 '__FROM_EMAIL__' => $this->emetteur->email,
551 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
552 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
555 require_once ODTPHP_PATH.
'odf.php';
557 $odfHandler =
new Odf(
560 'PATH_TO_TMP' => $conf->project->dir_temp,
562 'DELIMITER_LEFT' =>
'{',
563 'DELIMITER_RIGHT' =>
'}'
567 $this->error = $e->getMessage();
586 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
589 foreach ($tmparray as $key => $value) {
591 if (preg_match(
'/logo$/', $key)) {
592 if (file_exists($value)) {
593 $odfHandler->setImage($key, $value);
595 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
598 $odfHandler->setVars($key, $value,
true,
'UTF-8');
600 }
catch (OdfException $e) {
609 if (!empty($project->fk_soc)) {
610 $socid = $project->fk_soc;
615 foreach ($tmparray as $key => $val) {
617 $odfHandler->setVars($key, $val,
true,
'UTF-8');
618 }
catch (OdfException $e) {
620 }
catch (SegmentException $e) {
626 $sourcearray = array(
'internal',
'external');
627 $contact_arrray = array();
628 foreach ($sourcearray as $source) {
629 $contact_temp = $object->liste_contact(-1, $source);
630 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
631 $contact_arrray = array_merge($contact_arrray, $contact_temp);
634 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
635 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
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 (OdfException $e) {
659 }
catch (SegmentException $e) {
663 $listlinestaskres->merge();
665 $odfHandler->mergeSegment($listlinestaskres);
669 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
670 $sql .=
", u.lastname, u.firstname";
671 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
672 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
673 $sql .=
" WHERE t.fk_element =".((int) $object->id);
674 $sql .=
" AND t.elementtype = 'task'";
675 $sql .=
" AND t.fk_user = u.rowid";
676 $sql .=
" ORDER BY t.element_date DESC";
678 $resql = $this->db->query($sql);
680 $num = $this->db->num_rows($resql);
683 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
685 $row = $this->db->fetch_array($resql);
686 if (!empty($row[
'fk_user'])) {
687 $objectdetail =
new User($this->db);
688 $objectdetail->fetch($row[
'fk_user']);
690 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
692 $row[
'fullcivname'] =
'';
697 foreach ($tmparray as $key => $val) {
699 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
700 }
catch (OdfException $e) {
702 }
catch (SegmentException $e) {
706 $listlinestasktime->merge();
709 $this->db->free($resql);
711 $odfHandler->mergeSegment($listlinestasktime);
716 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
719 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
722 foreach ($filearray as $filedetail) {
725 foreach ($tmparray as $key => $val) {
727 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
728 }
catch (OdfException $e) {
730 }
catch (SegmentException $e) {
734 $listtasksfiles->merge();
738 $odfHandler->mergeSegment($listtasksfiles);
739 }
catch (OdfException $e) {
740 $this->error = $e->getMessage();
749 $listlines = $odfHandler->setSegment(
'projectfiles');
752 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
755 foreach ($filearray as $filedetail) {
759 foreach ($tmparray as $key => $val) {
761 $listlines->setVars($key, $val,
true,
'UTF-8');
762 }
catch (OdfException $e) {
764 }
catch (SegmentException $e) {
770 $odfHandler->mergeSegment($listlines);
771 }
catch (OdfException $e) {
772 $this->error = $e->getMessage();
778 $sourcearray = array(
'internal',
'external');
779 $contact_arrray = array();
780 foreach ($sourcearray as $source) {
781 $contact_temp = $project->liste_contact(-1, $source);
782 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
783 $contact_arrray = array_merge($contact_arrray, $contact_temp);
786 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
788 $listlines = $odfHandler->setSegment(
'projectcontacts');
790 foreach ($contact_arrray as $contact) {
791 if ($contact[
'source'] ==
'internal') {
792 $objectdetail =
new User($this->db);
793 $objectdetail->fetch($contact[
'id']);
794 $contact[
'socname'] = $mysoc->name;
795 } elseif ($contact[
'source'] ==
'external') {
796 $objectdetail =
new Contact($this->db);
797 $objectdetail->fetch($contact[
'id']);
800 $soc->fetch($contact[
'socid']);
801 $contact[
'socname'] = $soc->name;
803 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
807 foreach ($tmparray as $key => $val) {
809 $listlines->setVars($key, $val,
true,
'UTF-8');
810 }
catch (OdfException $e) {
812 }
catch (SegmentException $e) {
818 $odfHandler->mergeSegment($listlines);
819 }
catch (OdfException $e) {
820 $this->error = $e->getMessage();
828 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
829 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
835 $odfHandler->exportAsAttachedPDF($file);
837 $this->error = $e->getMessage();
843 $odfHandler->saveToDisk($file);
845 $this->error = $e->getMessage();
850 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
851 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
857 $this->result = array(
'fullpath'=>$file);
861 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);