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,
543 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
544 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
547 require_once ODTPHP_PATH.
'odf.php';
549 $odfHandler =
new Odf(
552 'PATH_TO_TMP' => $conf->project->dir_temp,
554 'DELIMITER_LEFT' =>
'{',
555 'DELIMITER_RIGHT' =>
'}'
559 $this->error = $e->getMessage();
578 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other);
581 foreach ($tmparray as $key => $value) {
583 if (preg_match(
'/logo$/', $key)) {
584 if (file_exists($value)) {
585 $odfHandler->setImage($key, $value);
587 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
590 $odfHandler->setVars($key, $value,
true,
'UTF-8');
592 }
catch (OdfException $e) {
601 if (!empty($project->fk_soc)) {
602 $socid = $project->fk_soc;
607 foreach ($tmparray as $key => $val) {
609 $odfHandler->setVars($key, $val,
true,
'UTF-8');
610 }
catch (OdfException $e) {
612 }
catch (SegmentException $e) {
618 $sourcearray = array(
'internal',
'external');
619 $contact_arrray = array();
620 foreach ($sourcearray as $source) {
621 $contact_temp = $object->liste_contact(-1, $source);
622 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
623 $contact_arrray = array_merge($contact_arrray, $contact_temp);
626 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
627 $listlinestaskres = $odfHandler->setSegment(
'tasksressources');
629 foreach ($contact_arrray as $contact) {
630 if ($contact[
'source'] ==
'internal') {
631 $objectdetail =
new User($this->db);
632 $objectdetail->fetch($contact[
'id']);
633 $contact[
'socname'] = $mysoc->name;
634 } elseif ($contact[
'source'] ==
'external') {
635 $objectdetail =
new Contact($this->db);
636 $objectdetail->fetch($contact[
'id']);
639 $soc->fetch($contact[
'socid']);
640 $contact[
'socname'] = $soc->name;
642 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
646 foreach ($tmparray as $key => $val) {
648 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
649 }
catch (OdfException $e) {
651 }
catch (SegmentException $e) {
655 $listlinestaskres->merge();
657 $odfHandler->mergeSegment($listlinestaskres);
661 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
662 $sql .=
", u.lastname, u.firstname";
663 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
664 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
665 $sql .=
" WHERE t.fk_element =".((int) $object->id);
666 $sql .=
" AND t.elementtype = 'task'";
667 $sql .=
" AND t.fk_user = u.rowid";
668 $sql .=
" ORDER BY t.element_date DESC";
670 $resql = $this->db->query($sql);
672 $num = $this->db->num_rows($resql);
675 $listlinestasktime = $odfHandler->setSegment(
'taskstimes');
677 $row = $this->db->fetch_array($resql);
678 if (!empty($row[
'fk_user'])) {
679 $objectdetail =
new User($this->db);
680 $objectdetail->fetch($row[
'fk_user']);
682 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
684 $row[
'fullcivname'] =
'';
689 foreach ($tmparray as $key => $val) {
691 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
692 }
catch (OdfException $e) {
694 }
catch (SegmentException $e) {
698 $listlinestasktime->merge();
701 $this->db->free($resql);
703 $odfHandler->mergeSegment($listlinestasktime);
708 $listtasksfiles = $odfHandler->setSegment(
'tasksfiles');
711 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
714 foreach ($filearray as $filedetail) {
717 foreach ($tmparray as $key => $val) {
719 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
720 }
catch (OdfException $e) {
722 }
catch (SegmentException $e) {
726 $listtasksfiles->merge();
730 $odfHandler->mergeSegment($listtasksfiles);
731 }
catch (OdfException $e) {
732 $this->error = $e->getMessage();
741 $listlines = $odfHandler->setSegment(
'projectfiles');
744 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
747 foreach ($filearray as $filedetail) {
751 foreach ($tmparray as $key => $val) {
753 $listlines->setVars($key, $val,
true,
'UTF-8');
754 }
catch (OdfException $e) {
756 }
catch (SegmentException $e) {
762 $odfHandler->mergeSegment($listlines);
763 }
catch (OdfException $e) {
764 $this->error = $e->getMessage();
770 $sourcearray = array(
'internal',
'external');
771 $contact_arrray = array();
772 foreach ($sourcearray as $source) {
773 $contact_temp = $project->liste_contact(-1, $source);
774 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
775 $contact_arrray = array_merge($contact_arrray, $contact_temp);
778 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
780 $listlines = $odfHandler->setSegment(
'projectcontacts');
782 foreach ($contact_arrray as $contact) {
783 if ($contact[
'source'] ==
'internal') {
784 $objectdetail =
new User($this->db);
785 $objectdetail->fetch($contact[
'id']);
786 $contact[
'socname'] = $mysoc->name;
787 } elseif ($contact[
'source'] ==
'external') {
788 $objectdetail =
new Contact($this->db);
789 $objectdetail->fetch($contact[
'id']);
792 $soc->fetch($contact[
'socid']);
793 $contact[
'socname'] = $soc->name;
795 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
799 foreach ($tmparray as $key => $val) {
801 $listlines->setVars($key, $val,
true,
'UTF-8');
802 }
catch (OdfException $e) {
804 }
catch (SegmentException $e) {
810 $odfHandler->mergeSegment($listlines);
811 }
catch (OdfException $e) {
812 $this->error = $e->getMessage();
820 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
821 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
827 $odfHandler->exportAsAttachedPDF($file);
829 $this->error = $e->getMessage();
835 $odfHandler->saveToDisk($file);
837 $this->error = $e->getMessage();
842 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
843 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
849 $this->result = array(
'fullpath'=>$file);
853 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);