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);
472 if (!is_object($outputlangs)) {
473 $outputlangs = $langs;
475 $sav_charset_output = $outputlangs->charset_output;
476 $outputlangs->charset_output =
'UTF-8';
479 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
481 if ($conf->project->dir_output) {
483 if (!is_object($object)) {
485 $object =
new Task($this->db);
486 $result = $object->fetch($id);
492 $project =
new Project($this->db);
493 $project->fetch($object->fk_project);
494 $project->fetch_thirdparty();
496 $dir = $conf->project->dir_output.
"/".$project->ref.
"/";
498 if (!preg_match(
'/specimen/i', $objectref)) {
499 $dir .=
"/".$objectref;
501 $file = $dir.
"/".$objectref.
".odt";
503 if (!file_exists($dir)) {
505 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
511 if (file_exists($dir)) {
513 $newfile = basename($srctemplatepath);
514 $newfiletmp = preg_replace(
'/\.(ods|odt)/i',
'', $newfile);
515 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
516 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
517 $newfiletmp = $objectref .
'_' . $newfiletmp;
519 $file = $dir .
'/' . $newfiletmp .
'.odt';
526 if (!is_writable($conf->project->dir_temp)) {
527 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
528 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
532 $socobject = $project->thirdparty;
535 $substitutionarray = array(
536 '__FROM_NAME__' => $this->emetteur->name,
537 '__FROM_EMAIL__' => $this->emetteur->email,
541 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
542 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
545 require_once ODTPHP_PATH.
'odf.php';
547 $odfHandler =
new Odf(
550 'PATH_TO_TMP' => $conf->project->dir_temp,
551 'ZIP_PROXY' =>
'PclZipProxy',
552 'DELIMITER_LEFT' =>
'{',
553 'DELIMITER_RIGHT' =>
'}'
557 $this->error = $e->getMessage();
576 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
579 foreach ($tmparray as $key => $value) {
581 if (preg_match(
'/logo$/', $key)) {
582 if (file_exists($value)) {
583 $odfHandler->setImage($key, $value);
585 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
589 $odfHandler->setVars($key, $value,
true,
'UTF-8');
591 }
catch (OdfException $e) {
600 if (!empty($project->fk_soc)) {
601 $socid = $project->fk_soc;
606 foreach ($tmparray as $key => $val) {
608 $odfHandler->setVars($key, $val,
true,
'UTF-8');
609 }
catch (OdfException $e) {
611 }
catch (SegmentException $e) {
617 $sourcearray = array(
'internal',
'external');
618 $contact_arrray = array();
619 foreach ($sourcearray as $source) {
620 $contact_temp = $object->liste_contact(-1, $source);
621 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
622 $contact_arrray = array_merge($contact_arrray, $contact_temp);
625 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
626 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
628 foreach ($contact_arrray as $contact) {
629 if ($contact[
'source'] ==
'internal') {
630 $objectdetail =
new User($this->db);
631 $objectdetail->fetch($contact[
'id']);
632 $contact[
'socname'] = $mysoc->name;
633 } elseif ($contact[
'source'] ==
'external') {
634 $objectdetail =
new Contact($this->db);
635 $objectdetail->fetch($contact[
'id']);
638 $soc->fetch($contact[
'socid']);
639 $contact[
'socname'] = $soc->name;
641 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
645 foreach ($tmparray as $key => $val) {
647 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
648 }
catch (OdfException $e) {
650 }
catch (SegmentException $e) {
654 $listlinestaskres->merge();
656 $odfHandler->mergeSegment($listlinestaskres);
660 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
661 $sql .=
", u.lastname, u.firstname";
662 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
663 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
664 $sql .=
" WHERE t.fk_element =".((int) $object->id);
665 $sql .=
" AND t.elementtype = 'task'";
666 $sql .=
" AND t.fk_user = u.rowid";
667 $sql .=
" ORDER BY t.element_date DESC";
669 $resql = $this->db->query($sql);
671 $num = $this->db->num_rows($resql);
674 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
676 $row = $this->db->fetch_array($resql);
677 if (!empty($row[
'fk_user'])) {
678 $objectdetail =
new User($this->db);
679 $objectdetail->fetch($row[
'fk_user']);
681 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
683 $row[
'fullcivname'] =
'';
688 foreach ($tmparray as $key => $val) {
690 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
691 }
catch (OdfException $e) {
693 }
catch (SegmentException $e) {
697 $listlinestasktime->merge();
700 $this->db->free($resql);
702 $odfHandler->mergeSegment($listlinestasktime);
707 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
710 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
713 foreach ($filearray as $filedetail) {
716 foreach ($tmparray as $key => $val) {
718 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
719 }
catch (OdfException $e) {
721 }
catch (SegmentException $e) {
725 $listtasksfiles->merge();
729 $odfHandler->mergeSegment($listtasksfiles);
730 }
catch (OdfException $e) {
731 $this->error = $e->getMessage();
740 $listlines = $odfHandler->setSegment(
'projectfiles');
743 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
746 foreach ($filearray as $filedetail) {
750 foreach ($tmparray as $key => $val) {
752 $listlines->setVars($key, $val,
true,
'UTF-8');
753 }
catch (OdfException $e) {
755 }
catch (SegmentException $e) {
761 $odfHandler->mergeSegment($listlines);
762 }
catch (OdfException $e) {
763 $this->error = $e->getMessage();
769 $sourcearray = array(
'internal',
'external');
770 $contact_arrray = array();
771 foreach ($sourcearray as $source) {
772 $contact_temp = $project->liste_contact(-1, $source);
773 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
774 $contact_arrray = array_merge($contact_arrray, $contact_temp);
777 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
779 $listlines = $odfHandler->setSegment(
'projectcontacts');
781 foreach ($contact_arrray as $contact) {
782 if ($contact[
'source'] ==
'internal') {
783 $objectdetail =
new User($this->db);
784 $objectdetail->fetch($contact[
'id']);
785 $contact[
'socname'] = $mysoc->name;
786 } elseif ($contact[
'source'] ==
'external') {
787 $objectdetail =
new Contact($this->db);
788 $objectdetail->fetch($contact[
'id']);
791 $soc->fetch($contact[
'socid']);
792 $contact[
'socname'] = $soc->name;
794 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
798 foreach ($tmparray as $key => $val) {
800 $listlines->setVars($key, $val,
true,
'UTF-8');
801 }
catch (OdfException $e) {
803 }
catch (SegmentException $e) {
809 $odfHandler->mergeSegment($listlines);
810 }
catch (OdfException $e) {
811 $this->error = $e->getMessage();
819 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
820 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
824 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
826 $odfHandler->exportAsAttachedPDF($file);
828 $this->error = $e->getMessage();
834 $odfHandler->saveToDisk($file);
836 $this->error = $e->getMessage();
841 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
842 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
848 $this->result = array(
'fullpath'=>$file);
852 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);