497 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
502 if (empty($srctemplatepath)) {
503 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
508 if (!is_object($hookmanager)) {
509 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
512 $hookmanager->initHooks(array(
'odtgeneration'));
515 if (!is_object($outputlangs)) {
516 $outputlangs = $langs;
518 $sav_charset_output = $outputlangs->charset_output;
519 $outputlangs->charset_output =
'UTF-8';
522 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
524 if (
$conf->project->dir_output) {
538 $dir =
$conf->project->dir_output;
540 if (!preg_match(
'/specimen/i', $objectref)) {
541 $dir .=
"/".$objectref;
543 $file = $dir.
"/".$objectref.
".odt";
545 if (!file_exists($dir)) {
547 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
552 if (file_exists($dir)) {
554 $newfile = basename($srctemplatepath);
555 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
556 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
557 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
558 $newfiletmp = $objectref .
'_' . $newfiletmp;
561 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
564 if ($format ==
'1') {
565 $format =
'%Y%m%d%H%M%S';
569 $filename = $newfiletmp .
'.' . $newfileformat;
571 $file = $dir .
'/' . $filename;
578 if (!is_writable(
$conf->project->dir_temp)) {
579 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->project->dir_temp);
580 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
586 $arrayidcontact =
$object->getIdContact(
'external',
'PROJECTLEADER');
587 if (count($arrayidcontact) > 0) {
589 $result =
$object->fetch_contact($arrayidcontact[0]);
593 $contactobject =
null;
594 if (!empty($usecontact)) {
596 $contactobject =
$object->contact;
599 $socobject =
$object->thirdparty;
602 $substitutionarray = array(
603 '__FROM_NAME__' => $this->emetteur->name,
604 '__FROM_EMAIL__' => $this->emetteur->email,
608 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
609 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
612 require_once ODTPHP_PATH.
'odf.php';
614 $odfHandler =
new Odf(
617 'PATH_TO_TMP' =>
$conf->project->dir_temp,
619 'DELIMITER_LEFT' =>
'{',
620 'DELIMITER_RIGHT' =>
'}'
624 $this->error = $e->getMessage();
633 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
641 $array_project_contact = array();
642 if ($usecontact && is_object($contactobject)) {
646 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);
650 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
651 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
660 foreach ($tmparray as $key => $value) {
662 if (preg_match(
'/logo$/', $key)) {
663 if (file_exists($value)) {
664 $odfHandler->setImage($key, $value, $ratio);
666 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
669 $odfHandler->setVars($key, $value,
true,
'UTF-8');
671 }
catch (OdfException $e) {
678 $listlines = $odfHandler->setSegment(
'tasks');
680 $taskstatic =
new Task($this->db);
688 $tasksarray = $taskstatic->getTasksArray(
null,
null,
$object->id, $socid, 0);
691 foreach ($tasksarray as $task) {
694 foreach ($tmparray as $key => $val) {
696 $listlines->setVars($key, $val,
true,
'UTF-8');
697 }
catch (SegmentException $e) {
702 $taskobj =
new Task($this->db);
703 $taskobj->fetch($task->id);
706 $sourcearray = array(
'internal',
'external');
707 $contact_arrray = array();
708 foreach ($sourcearray as $source) {
709 $contact_temp = $taskobj->liste_contact(-1, $source);
710 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
711 $contact_arrray = array_merge($contact_arrray, $contact_temp);
714 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
715 $listlinestaskres = $listlines->__get(
'tasksressources');
717 foreach ($contact_arrray as $contact) {
718 if ($contact[
'source'] ==
'internal') {
719 $objectdetail =
new User($this->db);
720 $objectdetail->fetch($contact[
'id']);
721 $contact[
'socname'] =
$mysoc->name;
722 } elseif ($contact[
'source'] ==
'external') {
723 $objectdetail =
new Contact($this->db);
724 $objectdetail->fetch($contact[
'id']);
727 $soc->fetch($contact[
'socid']);
728 $contact[
'socname'] = $soc->name;
730 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
733 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
737 foreach ($tmparray as $key => $val) {
739 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
740 }
catch (SegmentException $e) {
744 $listlinestaskres->merge();
749 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
750 $sql .=
", u.lastname, u.firstname, t.thm";
751 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
752 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
753 $sql .=
" WHERE t.fk_element =".((int) $task->id);
754 $sql .=
" AND t.elementtype = 'task'";
755 $sql .=
" AND t.fk_user = u.rowid";
756 $sql .=
" ORDER BY t.element_date DESC";
758 $resql = $this->db->query($sql);
760 $num = $this->db->num_rows($resql);
764 $listlinestasktime = $listlines->__get(
'taskstimes');
767 $row[
'task_date'] = 0;
768 $row[
'task_duration'] = 0;
773 $row[
'firstname'] =
'';
774 $row[
'fullcivname'] =
'';
775 $row[
'amountht'] = 0;
776 $row[
'amountttc'] = 0;
779 foreach ($tmparray as $key => $val) {
781 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
782 }
catch (SegmentException $e) {
786 $listlinestasktime->merge();
789 $row = $this->db->fetch_array($resql);
790 if (!empty($row[
'fk_user'])) {
791 $objectdetail =
new User($this->db);
792 $objectdetail->fetch($row[
'fk_user']);
793 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
795 $row[
'fullcivname'] =
'';
798 if (!empty($row[
'thm'])) {
799 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
801 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
803 $row[
'amountht'] = 0;
804 $row[
'amountttc'] = 0;
810 foreach ($tmparray as $key => $val) {
812 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
813 }
catch (SegmentException $e) {
817 $listlinestasktime->merge();
820 $this->db->free($resql);
825 $listtasksfiles = $listlines->__get(
'tasksfiles');
828 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
831 foreach ($filearray as $filedetail) {
834 foreach ($tmparray as $key => $val) {
836 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
837 }
catch (SegmentException $e) {
841 $listtasksfiles->merge();
845 $odfHandler->mergeSegment($listlines);
846 }
catch (OdfException $e) {
847 $ExceptionTrace = $e->getTrace();
849 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
850 $this->error = $e->getMessage();
858 $listlines = $odfHandler->setSegment(
'projectfiles');
861 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
863 foreach ($filearray as $filedetail) {
867 foreach ($tmparray as $key => $val) {
869 $listlines->setVars($key, $val,
true,
'UTF-8');
870 }
catch (SegmentException $e) {
876 $odfHandler->mergeSegment($listlines);
877 }
catch (OdfException $e) {
878 $ExceptionTrace = $e->getTrace();
880 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
881 $this->error = $e->getMessage();
888 $sourcearray = array(
'internal',
'external');
889 $contact_arrray = array();
890 foreach ($sourcearray as $source) {
891 $contact_temp =
$object->liste_contact(-1, $source);
892 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
893 $contact_arrray = array_merge($contact_arrray, $contact_temp);
896 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
898 $listlines = $odfHandler->setSegment(
'projectcontacts');
900 foreach ($contact_arrray as $contact) {
901 $objectdetail =
null;
902 if ($contact[
'source'] ==
'internal') {
903 $objectdetail =
new User($this->db);
904 $objectdetail->fetch($contact[
'id']);
905 $contact[
'socname'] =
$mysoc->name;
906 } elseif ($contact[
'source'] ==
'external') {
907 $objectdetail =
new Contact($this->db);
908 $objectdetail->fetch($contact[
'id']);
911 $soc->fetch($contact[
'socid']);
912 $contact[
'socname'] = $soc->name;
917 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
920 foreach ($tmparray as $key => $val) {
922 $listlines->setVars($key, $val,
true,
'UTF-8');
923 }
catch (SegmentException $e) {
929 $odfHandler->mergeSegment($listlines);
930 }
catch (OdfException $e) {
931 $ExceptionTrace = $e->getTrace();
933 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
934 $this->error = $e->getMessage();
943 $listofreferent = array(
945 'title' =>
"ListProposalsAssociatedProject",
948 'test' =>
isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
951 'title' =>
"ListOrdersAssociatedProject",
952 'class' =>
'Commande',
953 'table' =>
'commande',
954 'test' =>
isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
957 'title' =>
"ListInvoicesAssociatedProject",
958 'class' =>
'Facture',
959 'table' =>
'facture',
960 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
962 'invoice_predefined' => array(
963 'title' =>
"ListPredefinedInvoicesAssociatedProject",
964 'class' =>
'FactureRec',
965 'table' =>
'facture_rec',
966 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
968 'proposal_supplier' => array(
969 'title' =>
"ListSupplierProposalsAssociatedProject",
970 'class' =>
'SupplierProposal',
971 'table' =>
'supplier_proposal',
972 'test' =>
isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
974 'order_supplier' => array(
975 'title' =>
"ListSupplierOrdersAssociatedProject",
976 'table' =>
'commande_fournisseur',
977 'class' =>
'CommandeFournisseur',
978 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (
isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
980 'invoice_supplier' => array(
981 'title' =>
"ListSupplierInvoicesAssociatedProject",
982 'table' =>
'facture_fourn',
983 'class' =>
'FactureFournisseur',
984 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (
isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
987 'title' =>
"ListContractAssociatedProject",
988 'class' =>
'Contrat',
989 'table' =>
'contrat',
990 'test' =>
isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
992 'intervention' => array(
993 'title' =>
"ListFichinterAssociatedProject",
994 'class' =>
'Fichinter',
995 'table' =>
'fichinter',
996 'disableamount' => 1,
997 'test' =>
isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
1000 'title' =>
"ListShippingAssociatedProject",
1001 'class' =>
'Expedition',
1002 'table' =>
'expedition',
1003 'disableamount' => 1,
1004 'test' =>
isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
1006 'expensereport' => array(
1007 'title' =>
"ListExpenseReportsAssociatedProject",
1008 'class' =>
'ExpenseReportLine',
1009 'table' =>
'expensereport_det',
1010 'test' =>
isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1012 'donation' => array(
1013 'title' =>
"ListDonationsAssociatedProject",
1016 'test' =>
isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1019 'title' =>
"ListLoanAssociatedProject",
1022 'test' =>
isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1024 'chargesociales' => array(
1025 'title' =>
"ListSocialContributionAssociatedProject",
1026 'class' =>
'ChargeSociales',
1027 'table' =>
'chargesociales',
1028 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1029 'test' =>
isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1031 'stock_mouvement' => array(
1032 'title' =>
"ListMouvementStockProject",
1033 'class' =>
'MouvementStock',
1034 'table' =>
'stock_mouvement',
1038 'title' =>
"ListActionsAssociatedProject",
1039 'class' =>
'ActionComm',
1040 'table' =>
'actioncomm',
1041 'disableamount' => 1,
1042 'test' =>
isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1048 $listlines = $odfHandler->setSegment(
'projectrefs');
1050 foreach ($listofreferent as $keyref => $valueref) {
1051 $title = $valueref[
'title'];
1052 $tablename = $valueref[
'table'];
1053 $classname = $valueref[
'class'];
1054 $qualified = $valueref[
'test'];
1056 $elementarray =
$object->get_element_list($keyref, $tablename);
1057 if (count($elementarray) > 0 && is_array($elementarray)) {
1060 $num = count($elementarray);
1061 for ($i = 0; $i < $num; $i++) {
1062 $ref_array = array();
1063 $ref_array[
'type'] = (
string) $langs->trans($classname);
1065 $element =
new $classname($this->db);
1066 $element->fetch((
int) $elementarray[$i]);
1067 $element->fetch_thirdparty();
1070 $ref_array[
'ref'] = (
string) $element->ref;
1073 $dateref = $element->date;
1074 if (empty($dateref)) {
1075 $dateref = $element->datep;
1077 if (empty($dateref)) {
1078 $dateref = $element->date_contrat;
1080 $ref_array[
'date'] = (
string) $dateref;
1083 if (is_object($element->thirdparty)) {
1084 $ref_array[
'socname'] = $element->thirdparty->name;
1086 $ref_array[
'socname'] =
'';
1090 if (empty($valueref[
'disableamount'])) {
1091 if (!empty($element->total_ht)) {
1092 $ref_array[
'amountht'] = (float) $element->total_ht;
1093 $ref_array[
'amountttc'] = (float) $element->total_ttc;
1095 $ref_array[
'amountht'] = 0;
1096 $ref_array[
'amountttc'] = 0;
1099 $ref_array[
'amountht'] =
'';
1100 $ref_array[
'amountttc'] =
'';
1103 $ref_array[
'status'] = $element->getLibStatut(0);
1107 foreach ($tmparray as $key => $val) {
1109 $listlines->setVars($key, $val,
true,
'UTF-8');
1110 }
catch (SegmentException $e) {
1114 $listlines->merge();
1119 $odfHandler->mergeSegment($listlines);
1120 }
catch (OdfExceptionSegmentNotFound $e) {
1122 }
catch (OdfException $e) {
1123 $this->error = $e->getMessage();
1129 $tmparray = $outputlangs->get_translations_for_substitutions();
1130 foreach ($tmparray as $key => $value) {
1132 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1133 }
catch (OdfException $e) {
1139 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1140 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1146 $odfHandler->exportAsAttachedPDF($file);
1148 $this->error = $e->getMessage();
1153 $odfHandler->saveToDisk($file);
1155 $this->error = $e->getMessage();
1160 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1161 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1167 $this->result = array(
'fullpath' => $file);
1171 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);