513 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
518 if (empty($srctemplatepath)) {
519 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
524 if (!is_object($hookmanager)) {
525 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
528 $hookmanager->initHooks(array(
'odtgeneration'));
531 if (!is_object($outputlangs)) {
532 $outputlangs = $langs;
534 $sav_charset_output = $outputlangs->charset_output;
535 $outputlangs->charset_output =
'UTF-8';
538 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
540 if (
$conf->project->dir_output) {
547 dol_print_error($this->db,
$object->error);
554 $dir =
$conf->project->dir_output;
556 if (!preg_match(
'/specimen/i', $objectref)) {
557 $dir .=
"/".$objectref;
559 $file = $dir.
"/".$objectref.
".odt";
561 if (!file_exists($dir)) {
563 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
568 if (file_exists($dir)) {
570 $newfile = basename($srctemplatepath);
571 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
572 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
573 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
574 $newfiletmp = $objectref .
'_' . $newfiletmp;
577 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
580 if ($format ==
'1') {
581 $format =
'%Y%m%d%H%M%S';
585 $filename = $newfiletmp .
'.' . $newfileformat;
587 $file = $dir .
'/' . $filename;
594 if (!is_writable(
$conf->project->dir_temp)) {
595 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->project->dir_temp);
596 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
602 $arrayidcontact =
$object->getIdContact(
'external',
'PROJECTLEADER');
603 if (count($arrayidcontact) > 0) {
605 $result =
$object->fetch_contact($arrayidcontact[0]);
609 $contactobject =
null;
610 if (!empty($usecontact)) {
612 $contactobject =
$object->contact;
615 $socobject =
$object->thirdparty;
618 $substitutionarray = array(
619 '__FROM_NAME__' => $this->emetteur->name,
620 '__FROM_EMAIL__' => $this->emetteur->email,
624 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
625 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
628 require_once ODTPHP_PATH.
'odf.php';
630 $odfHandler =
new Odf(
633 'PATH_TO_TMP' =>
$conf->project->dir_temp,
635 'DELIMITER_LEFT' =>
'{',
636 'DELIMITER_RIGHT' =>
'}'
640 $this->error = $e->getMessage();
649 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
657 $array_project_contact = array();
658 if ($usecontact && is_object($contactobject)) {
662 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);
666 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
667 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
676 foreach ($tmparray as $key => $value) {
678 if (preg_match(
'/logo$/', $key)) {
679 if (file_exists($value)) {
680 $odfHandler->setImage($key, $value, $ratio);
682 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
685 $odfHandler->setVars($key, $value,
true,
'UTF-8');
687 }
catch (OdfException $e) {
693 $foundtagfortasks = 1;
695 $listlines = $odfHandler->setSegment(
'tasks');
696 }
catch (OdfExceptionSegmentNotFound $e) {
698 $foundtagfortasks = 0;
701 if ($foundtagfortasks && isset($listlines)) {
702 $taskstatic =
new Task($this->db);
710 $tasksarray = $taskstatic->getTasksArray(
null,
null,
$object->id, $socid, 0);
713 foreach ($tasksarray as $task) {
716 foreach ($tmparray as $key => $val) {
718 $listlines->setVars($key, $val,
true,
'UTF-8');
719 }
catch (SegmentException $e) {
724 $taskobj =
new Task($this->db);
725 $taskobj->fetch($task->id);
728 $sourcearray = array(
'internal',
'external');
729 $contact_arrray = array();
730 foreach ($sourcearray as $source) {
731 $contact_temp = $taskobj->liste_contact(-1, $source);
732 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
733 $contact_arrray = array_merge($contact_arrray, $contact_temp);
736 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
737 $foundtagfortasksressources = 1;
739 $listlinestaskres = $listlines->__get(
'tasksressources');
740 }
catch (SegmentException $e) {
742 $foundtagfortasksressources = 0;
745 if ($foundtagfortasksressources && isset($listlinestaskres)) {
746 foreach ($contact_arrray as $contact) {
747 if ($contact[
'source'] ==
'internal') {
748 $objectdetail =
new User($this->db);
749 $objectdetail->fetch($contact[
'id']);
750 $contact[
'socname'] =
$mysoc->name;
751 } elseif ($contact[
'source'] ==
'external') {
752 $objectdetail =
new Contact($this->db);
753 $objectdetail->fetch($contact[
'id']);
756 $soc->fetch($contact[
'socid']);
757 $contact[
'socname'] = $soc->name;
759 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
762 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
766 foreach ($tmparray as $key => $val) {
768 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
769 }
catch (SegmentException $e) {
773 $listlinestaskres->merge();
779 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
780 $sql .=
", u.lastname, u.firstname, t.thm";
781 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
782 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
783 $sql .=
" WHERE t.fk_element =".((int) $task->id);
784 $sql .=
" AND t.elementtype = 'task'";
785 $sql .=
" AND t.fk_user = u.rowid";
786 $sql .=
" ORDER BY t.element_date DESC";
788 $resql = $this->db->query($sql);
790 $num = $this->db->num_rows($resql);
794 $foundtagfortaskstimes = 1;
796 $listlinestasktime = $listlines->__get(
'taskstimes');
797 }
catch (SegmentException $e) {
799 $foundtagfortaskstimes = 0;
802 if ($foundtagfortaskstimes && isset($listlinestasktime)) {
805 $row[
'task_date'] = 0;
806 $row[
'task_duration'] = 0;
811 $row[
'firstname'] =
'';
812 $row[
'fullcivname'] =
'';
813 $row[
'amountht'] = 0;
814 $row[
'amountttc'] = 0;
817 foreach ($tmparray as $key => $val) {
819 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
820 }
catch (SegmentException $e) {
824 $listlinestasktime->merge();
827 $row = $this->db->fetch_array($resql);
828 if (!empty($row[
'fk_user'])) {
829 $objectdetail =
new User($this->db);
830 $objectdetail->fetch($row[
'fk_user']);
831 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
833 $row[
'fullcivname'] =
'';
836 if (!empty($row[
'thm'])) {
837 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
839 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
841 $row[
'amountht'] = 0;
842 $row[
'amountttc'] = 0;
848 foreach ($tmparray as $key => $val) {
850 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
851 }
catch (SegmentException $e) {
855 $listlinestasktime->merge();
859 $this->db->free($resql);
864 $foundtagfortasksfiles = 1;
866 $listtasksfiles = $listlines->__get(
'tasksfiles');
867 }
catch (SegmentException $e) {
869 $foundtagfortasksfiles = 0;
872 if ($foundtagfortasksfiles && isset($listtasksfiles)) {
874 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
877 foreach ($filearray as $filedetail) {
880 foreach ($tmparray as $key => $val) {
882 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
883 }
catch (SegmentException $e) {
887 $listtasksfiles->merge();
893 $odfHandler->mergeSegment($listlines);
894 }
catch (OdfException $e) {
895 $this->error = $e->getMessage();
902 $foundtagforprojectfiles = 1;
904 $listlines = $odfHandler->setSegment(
'projectfiles');
905 }
catch (OdfExceptionSegmentNotFound $e) {
907 $foundtagforprojectfiles = 0;
910 if ($foundtagforprojectfiles && isset($listlines)) {
912 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
914 foreach ($filearray as $filedetail) {
918 foreach ($tmparray as $key => $val) {
920 $listlines->setVars($key, $val,
true,
'UTF-8');
921 }
catch (SegmentException $e) {
928 $odfHandler->mergeSegment($listlines);
929 }
catch (OdfException $e) {
930 $this->error = $e->getMessage();
937 $sourcearray = array(
'internal',
'external');
938 $contact_arrray = array();
939 foreach ($sourcearray as $source) {
940 $contact_temp =
$object->liste_contact(-1, $source);
941 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
942 $contact_arrray = array_merge($contact_arrray, $contact_temp);
945 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
946 $foundtagforprojectcontacts = 1;
948 $listlines = $odfHandler->setSegment(
'projectcontacts');
949 }
catch (OdfExceptionSegmentNotFound $e) {
951 $foundtagforprojectcontacts = 0;
954 if ($foundtagforprojectcontacts && isset($listlines)) {
955 foreach ($contact_arrray as $contact) {
956 $objectdetail =
null;
957 if ($contact[
'source'] ==
'internal') {
958 $objectdetail =
new User($this->db);
959 $objectdetail->fetch($contact[
'id']);
960 $contact[
'socname'] =
$mysoc->name;
961 } elseif ($contact[
'source'] ==
'external') {
962 $objectdetail =
new Contact($this->db);
963 $objectdetail->fetch($contact[
'id']);
966 $soc->fetch($contact[
'socid']);
967 $contact[
'socname'] = $soc->name;
972 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
975 foreach ($tmparray as $key => $val) {
977 $listlines->setVars($key, $val,
true,
'UTF-8');
978 }
catch (SegmentException $e) {
985 $odfHandler->mergeSegment($listlines);
986 }
catch (OdfException $e) {
987 $this->error = $e->getMessage();
996 $listofreferent = array(
998 'title' =>
"ListProposalsAssociatedProject",
1000 'table' =>
'propal',
1001 'test' =>
isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
1004 'title' =>
"ListOrdersAssociatedProject",
1005 'class' =>
'Commande',
1006 'table' =>
'commande',
1007 'test' =>
isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
1010 'title' =>
"ListInvoicesAssociatedProject",
1011 'class' =>
'Facture',
1012 'table' =>
'facture',
1013 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
1015 'invoice_predefined' => array(
1016 'title' =>
"ListPredefinedInvoicesAssociatedProject",
1017 'class' =>
'FactureRec',
1018 'table' =>
'facture_rec',
1019 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
1021 'proposal_supplier' => array(
1022 'title' =>
"ListSupplierProposalsAssociatedProject",
1023 'class' =>
'SupplierProposal',
1024 'table' =>
'supplier_proposal',
1025 'test' =>
isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
1027 'order_supplier' => array(
1028 'title' =>
"ListSupplierOrdersAssociatedProject",
1029 'table' =>
'commande_fournisseur',
1030 'class' =>
'CommandeFournisseur',
1031 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (
isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
1033 'invoice_supplier' => array(
1034 'title' =>
"ListSupplierInvoicesAssociatedProject",
1035 'table' =>
'facture_fourn',
1036 'class' =>
'FactureFournisseur',
1037 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (
isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
1039 'contract' => array(
1040 'title' =>
"ListContractAssociatedProject",
1041 'class' =>
'Contrat',
1042 'table' =>
'contrat',
1043 'test' =>
isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
1045 'intervention' => array(
1046 'title' =>
"ListFichinterAssociatedProject",
1047 'class' =>
'Fichinter',
1048 'table' =>
'fichinter',
1049 'disableamount' => 1,
1050 'test' =>
isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
1052 'shipping' => array(
1053 'title' =>
"ListShippingAssociatedProject",
1054 'class' =>
'Expedition',
1055 'table' =>
'expedition',
1056 'disableamount' => 1,
1057 'test' =>
isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
1059 'expensereport' => array(
1060 'title' =>
"ListExpenseReportsAssociatedProject",
1061 'class' =>
'ExpenseReportLine',
1062 'table' =>
'expensereport_det',
1063 'test' =>
isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1065 'donation' => array(
1066 'title' =>
"ListDonationsAssociatedProject",
1069 'test' =>
isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1072 'title' =>
"ListLoanAssociatedProject",
1075 'test' =>
isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1077 'chargesociales' => array(
1078 'title' =>
"ListSocialContributionAssociatedProject",
1079 'class' =>
'ChargeSociales',
1080 'table' =>
'chargesociales',
1081 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1082 'test' =>
isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1084 'stock_mouvement' => array(
1085 'title' =>
"ListMouvementStockProject",
1086 'class' =>
'MouvementStock',
1087 'table' =>
'stock_mouvement',
1091 'title' =>
"ListActionsAssociatedProject",
1092 'class' =>
'ActionComm',
1093 'table' =>
'actioncomm',
1094 'disableamount' => 1,
1095 'test' =>
isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1100 $foundtagforprojectrefs = 1;
1102 $listlines = $odfHandler->setSegment(
'projectrefs');
1103 }
catch (OdfExceptionSegmentNotFound $e) {
1105 $foundtagforprojectrefs = 0;
1108 if ($foundtagforprojectrefs && isset($listlines)) {
1109 foreach ($listofreferent as $keyref => $valueref) {
1110 $title = $valueref[
'title'];
1111 $tablename = $valueref[
'table'];
1112 $classname = $valueref[
'class'];
1113 $qualified = $valueref[
'test'];
1115 $elementarray =
$object->get_element_list($keyref, $tablename);
1116 if (count($elementarray) > 0 && is_array($elementarray)) {
1119 $num = count($elementarray);
1120 for ($i = 0; $i < $num; $i++) {
1121 $ref_array = array();
1122 $ref_array[
'type'] = (string) $langs->trans($classname);
1124 $element =
new $classname($this->db);
1125 $element->fetch((
int) $elementarray[$i]);
1126 $element->fetch_thirdparty();
1129 $ref_array[
'ref'] = (string) $element->ref;
1132 $dateref = $element->date;
1133 if (empty($dateref)) {
1134 $dateref = $element->datep;
1136 if (empty($dateref)) {
1137 $dateref = $element->date_contrat;
1139 $ref_array[
'date'] = (string) $dateref;
1142 if (is_object($element->thirdparty)) {
1143 $ref_array[
'socname'] = $element->thirdparty->name;
1145 $ref_array[
'socname'] =
'';
1149 if (empty($valueref[
'disableamount'])) {
1150 if (!empty($element->total_ht)) {
1151 $ref_array[
'amountht'] = (float) $element->total_ht;
1152 $ref_array[
'amountttc'] = (float) $element->total_ttc;
1154 $ref_array[
'amountht'] = 0;
1155 $ref_array[
'amountttc'] = 0;
1158 $ref_array[
'amountht'] =
'';
1159 $ref_array[
'amountttc'] =
'';
1162 $ref_array[
'status'] = method_exists($element,
'getLibStatut') ? $element->getLibStatut(0) :
'';
1166 foreach ($tmparray as $key => $val) {
1168 $listlines->setVars($key, $val,
true,
'UTF-8');
1169 }
catch (SegmentException $e) {
1173 $listlines->merge();
1178 $odfHandler->mergeSegment($listlines);
1179 }
catch (OdfException $e) {
1180 $this->error = $e->getMessage();
1188 $tmparray = $outputlangs->get_translations_for_substitutions();
1189 foreach ($tmparray as $key => $value) {
1191 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1192 }
catch (OdfException $e) {
1198 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1199 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1205 $odfHandler->exportAsAttachedPDF($file);
1207 $this->error = $e->getMessage();
1212 $odfHandler->saveToDisk($file);
1214 $this->error = $e->getMessage();
1219 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1220 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1226 $this->result = array(
'fullpath' => $file);
1230 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);