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,
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,
559 'ZIP_PROXY' =>
'PclZipProxy',
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');
597 $odfHandler->setVars($key, $value,
true,
'UTF-8');
599 }
catch (OdfException $e) {
608 if (!empty($project->fk_soc)) {
609 $socid = $project->fk_soc;
614 foreach ($tmparray as $key => $val) {
616 $odfHandler->setVars($key, $val,
true,
'UTF-8');
617 }
catch (OdfException $e) {
619 }
catch (SegmentException $e) {
625 $sourcearray = array(
'internal',
'external');
626 $contact_arrray = array();
627 foreach ($sourcearray as $source) {
628 $contact_temp = $object->liste_contact(-1, $source);
629 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
630 $contact_arrray = array_merge($contact_arrray, $contact_temp);
633 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
634 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
636 foreach ($contact_arrray as $contact) {
637 if ($contact[
'source'] ==
'internal') {
638 $objectdetail =
new User($this->db);
639 $objectdetail->fetch($contact[
'id']);
640 $contact[
'socname'] = $mysoc->name;
641 } elseif ($contact[
'source'] ==
'external') {
642 $objectdetail =
new Contact($this->db);
643 $objectdetail->fetch($contact[
'id']);
646 $soc->fetch($contact[
'socid']);
647 $contact[
'socname'] = $soc->name;
649 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
653 foreach ($tmparray as $key => $val) {
655 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
656 }
catch (OdfException $e) {
658 }
catch (SegmentException $e) {
662 $listlinestaskres->merge();
664 $odfHandler->mergeSegment($listlinestaskres);
668 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
669 $sql .=
", u.lastname, u.firstname";
670 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
671 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
672 $sql .=
" WHERE t.fk_element =".((int) $object->id);
673 $sql .=
" AND t.elementtype = 'task'";
674 $sql .=
" AND t.fk_user = u.rowid";
675 $sql .=
" ORDER BY t.element_date DESC";
677 $resql = $this->db->query($sql);
679 $num = $this->db->num_rows($resql);
682 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
684 $row = $this->db->fetch_array($resql);
685 if (!empty($row[
'fk_user'])) {
686 $objectdetail =
new User($this->db);
687 $objectdetail->fetch($row[
'fk_user']);
689 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
691 $row[
'fullcivname'] =
'';
696 foreach ($tmparray as $key => $val) {
698 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
699 }
catch (OdfException $e) {
701 }
catch (SegmentException $e) {
705 $listlinestasktime->merge();
708 $this->db->free($resql);
710 $odfHandler->mergeSegment($listlinestasktime);
715 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
718 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
721 foreach ($filearray as $filedetail) {
724 foreach ($tmparray as $key => $val) {
726 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
727 }
catch (OdfException $e) {
729 }
catch (SegmentException $e) {
733 $listtasksfiles->merge();
737 $odfHandler->mergeSegment($listtasksfiles);
738 }
catch (OdfException $e) {
739 $this->error = $e->getMessage();
748 $listlines = $odfHandler->setSegment(
'projectfiles');
751 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
754 foreach ($filearray as $filedetail) {
758 foreach ($tmparray as $key => $val) {
760 $listlines->setVars($key, $val,
true,
'UTF-8');
761 }
catch (OdfException $e) {
763 }
catch (SegmentException $e) {
769 $odfHandler->mergeSegment($listlines);
770 }
catch (OdfException $e) {
771 $this->error = $e->getMessage();
777 $sourcearray = array(
'internal',
'external');
778 $contact_arrray = array();
779 foreach ($sourcearray as $source) {
780 $contact_temp = $project->liste_contact(-1, $source);
781 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
782 $contact_arrray = array_merge($contact_arrray, $contact_temp);
785 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
787 $listlines = $odfHandler->setSegment(
'projectcontacts');
789 foreach ($contact_arrray as $contact) {
790 if ($contact[
'source'] ==
'internal') {
791 $objectdetail =
new User($this->db);
792 $objectdetail->fetch($contact[
'id']);
793 $contact[
'socname'] = $mysoc->name;
794 } elseif ($contact[
'source'] ==
'external') {
795 $objectdetail =
new Contact($this->db);
796 $objectdetail->fetch($contact[
'id']);
799 $soc->fetch($contact[
'socid']);
800 $contact[
'socname'] = $soc->name;
802 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
806 foreach ($tmparray as $key => $val) {
808 $listlines->setVars($key, $val,
true,
'UTF-8');
809 }
catch (OdfException $e) {
811 }
catch (SegmentException $e) {
817 $odfHandler->mergeSegment($listlines);
818 }
catch (OdfException $e) {
819 $this->error = $e->getMessage();
827 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
828 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
832 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
834 $odfHandler->exportAsAttachedPDF($file);
836 $this->error = $e->getMessage();
842 $odfHandler->saveToDisk($file);
844 $this->error = $e->getMessage();
849 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
850 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
856 $this->result = array(
'fullpath'=>$file);
860 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);