496 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
501 if (empty($srctemplatepath)) {
502 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
507 if (!is_object($hookmanager)) {
508 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
511 $hookmanager->initHooks(array(
'odtgeneration'));
514 if (!is_object($outputlangs)) {
515 $outputlangs = $langs;
517 $sav_charset_output = $outputlangs->charset_output;
518 $outputlangs->charset_output =
'UTF-8';
521 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
523 if (
$conf->project->dir_output) {
537 $dir =
$conf->project->dir_output;
539 if (!preg_match(
'/specimen/i', $objectref)) {
540 $dir .=
"/".$objectref;
542 $file = $dir.
"/".$objectref.
".odt";
544 if (!file_exists($dir)) {
546 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
551 if (file_exists($dir)) {
553 $newfile = basename($srctemplatepath);
554 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
555 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
556 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
557 $newfiletmp = $objectref .
'_' . $newfiletmp;
560 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
563 if ($format ==
'1') {
564 $format =
'%Y%m%d%H%M%S';
568 $filename = $newfiletmp .
'.' . $newfileformat;
570 $file = $dir .
'/' . $filename;
577 if (!is_writable(
$conf->project->dir_temp)) {
578 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->project->dir_temp);
579 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
585 $arrayidcontact =
$object->getIdContact(
'external',
'PROJECTLEADER');
586 if (count($arrayidcontact) > 0) {
588 $result =
$object->fetch_contact($arrayidcontact[0]);
592 $contactobject =
null;
593 if (!empty($usecontact)) {
595 $contactobject =
$object->contact;
598 $socobject =
$object->thirdparty;
601 $substitutionarray = array(
602 '__FROM_NAME__' => $this->emetteur->name,
603 '__FROM_EMAIL__' => $this->emetteur->email,
607 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
608 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
611 require_once ODTPHP_PATH.
'odf.php';
613 $odfHandler =
new Odf(
616 'PATH_TO_TMP' =>
$conf->project->dir_temp,
618 'DELIMITER_LEFT' =>
'{',
619 'DELIMITER_RIGHT' =>
'}'
623 $this->error = $e->getMessage();
632 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
640 $array_project_contact = array();
641 if ($usecontact && is_object($contactobject)) {
645 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);
649 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
650 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
659 foreach ($tmparray as $key => $value) {
661 if (preg_match(
'/logo$/', $key)) {
662 if (file_exists($value)) {
663 $odfHandler->setImage($key, $value, $ratio);
665 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
668 $odfHandler->setVars($key, $value,
true,
'UTF-8');
670 }
catch (OdfException $e) {
677 $listlines = $odfHandler->setSegment(
'tasks');
679 $taskstatic =
new Task($this->db);
687 $tasksarray = $taskstatic->getTasksArray(
null,
null,
$object->id, $socid, 0);
690 foreach ($tasksarray as $task) {
693 foreach ($tmparray as $key => $val) {
695 $listlines->setVars($key, $val,
true,
'UTF-8');
696 }
catch (SegmentException $e) {
701 $taskobj =
new Task($this->db);
702 $taskobj->fetch($task->id);
705 $sourcearray = array(
'internal',
'external');
706 $contact_arrray = array();
707 foreach ($sourcearray as $source) {
708 $contact_temp = $taskobj->liste_contact(-1, $source);
709 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
710 $contact_arrray = array_merge($contact_arrray, $contact_temp);
713 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
714 $listlinestaskres = $listlines->__get(
'tasksressources');
716 foreach ($contact_arrray as $contact) {
717 if ($contact[
'source'] ==
'internal') {
718 $objectdetail =
new User($this->db);
719 $objectdetail->fetch($contact[
'id']);
720 $contact[
'socname'] =
$mysoc->name;
721 } elseif ($contact[
'source'] ==
'external') {
722 $objectdetail =
new Contact($this->db);
723 $objectdetail->fetch($contact[
'id']);
726 $soc->fetch($contact[
'socid']);
727 $contact[
'socname'] = $soc->name;
729 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
732 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
736 foreach ($tmparray as $key => $val) {
738 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
739 }
catch (SegmentException $e) {
743 $listlinestaskres->merge();
748 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
749 $sql .=
", u.lastname, u.firstname, t.thm";
750 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
751 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
752 $sql .=
" WHERE t.fk_element =".((int) $task->id);
753 $sql .=
" AND t.elementtype = 'task'";
754 $sql .=
" AND t.fk_user = u.rowid";
755 $sql .=
" ORDER BY t.element_date DESC";
757 $resql = $this->db->query($sql);
759 $num = $this->db->num_rows($resql);
763 $listlinestasktime = $listlines->__get(
'taskstimes');
766 $row[
'task_date'] = 0;
767 $row[
'task_duration'] = 0;
772 $row[
'firstname'] =
'';
773 $row[
'fullcivname'] =
'';
774 $row[
'amountht'] = 0;
775 $row[
'amountttc'] = 0;
778 foreach ($tmparray as $key => $val) {
780 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
781 }
catch (SegmentException $e) {
785 $listlinestasktime->merge();
788 $row = $this->db->fetch_array($resql);
789 if (!empty($row[
'fk_user'])) {
790 $objectdetail =
new User($this->db);
791 $objectdetail->fetch($row[
'fk_user']);
792 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
794 $row[
'fullcivname'] =
'';
797 if (!empty($row[
'thm'])) {
798 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
800 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
802 $row[
'amountht'] = 0;
803 $row[
'amountttc'] = 0;
809 foreach ($tmparray as $key => $val) {
811 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
812 }
catch (SegmentException $e) {
816 $listlinestasktime->merge();
819 $this->db->free($resql);
824 $listtasksfiles = $listlines->__get(
'tasksfiles');
827 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
830 foreach ($filearray as $filedetail) {
833 foreach ($tmparray as $key => $val) {
835 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
836 }
catch (SegmentException $e) {
840 $listtasksfiles->merge();
844 $odfHandler->mergeSegment($listlines);
845 }
catch (OdfException $e) {
846 $ExceptionTrace = $e->getTrace();
848 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
849 $this->error = $e->getMessage();
857 $listlines = $odfHandler->setSegment(
'projectfiles');
860 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
862 foreach ($filearray as $filedetail) {
866 foreach ($tmparray as $key => $val) {
868 $listlines->setVars($key, $val,
true,
'UTF-8');
869 }
catch (SegmentException $e) {
875 $odfHandler->mergeSegment($listlines);
876 }
catch (OdfException $e) {
877 $ExceptionTrace = $e->getTrace();
879 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
880 $this->error = $e->getMessage();
887 $sourcearray = array(
'internal',
'external');
888 $contact_arrray = array();
889 foreach ($sourcearray as $source) {
890 $contact_temp =
$object->liste_contact(-1, $source);
891 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
892 $contact_arrray = array_merge($contact_arrray, $contact_temp);
895 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
897 $listlines = $odfHandler->setSegment(
'projectcontacts');
899 foreach ($contact_arrray as $contact) {
900 $objectdetail =
null;
901 if ($contact[
'source'] ==
'internal') {
902 $objectdetail =
new User($this->db);
903 $objectdetail->fetch($contact[
'id']);
904 $contact[
'socname'] =
$mysoc->name;
905 } elseif ($contact[
'source'] ==
'external') {
906 $objectdetail =
new Contact($this->db);
907 $objectdetail->fetch($contact[
'id']);
910 $soc->fetch($contact[
'socid']);
911 $contact[
'socname'] = $soc->name;
916 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
919 foreach ($tmparray as $key => $val) {
921 $listlines->setVars($key, $val,
true,
'UTF-8');
922 }
catch (SegmentException $e) {
928 $odfHandler->mergeSegment($listlines);
929 }
catch (OdfException $e) {
930 $ExceptionTrace = $e->getTrace();
932 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
933 $this->error = $e->getMessage();
942 $listofreferent = array(
944 'title' =>
"ListProposalsAssociatedProject",
947 'test' =>
isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
950 'title' =>
"ListOrdersAssociatedProject",
951 'class' =>
'Commande',
952 'table' =>
'commande',
953 'test' =>
isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
956 'title' =>
"ListInvoicesAssociatedProject",
957 'class' =>
'Facture',
958 'table' =>
'facture',
959 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
961 'invoice_predefined' => array(
962 'title' =>
"ListPredefinedInvoicesAssociatedProject",
963 'class' =>
'FactureRec',
964 'table' =>
'facture_rec',
965 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
967 'proposal_supplier' => array(
968 'title' =>
"ListSupplierProposalsAssociatedProject",
969 'class' =>
'SupplierProposal',
970 'table' =>
'supplier_proposal',
971 'test' =>
isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
973 'order_supplier' => array(
974 'title' =>
"ListSupplierOrdersAssociatedProject",
975 'table' =>
'commande_fournisseur',
976 'class' =>
'CommandeFournisseur',
977 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (
isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
979 'invoice_supplier' => array(
980 'title' =>
"ListSupplierInvoicesAssociatedProject",
981 'table' =>
'facture_fourn',
982 'class' =>
'FactureFournisseur',
983 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (
isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
986 'title' =>
"ListContractAssociatedProject",
987 'class' =>
'Contrat',
988 'table' =>
'contrat',
989 'test' =>
isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
991 'intervention' => array(
992 'title' =>
"ListFichinterAssociatedProject",
993 'class' =>
'Fichinter',
994 'table' =>
'fichinter',
995 'disableamount' => 1,
996 'test' =>
isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
999 'title' =>
"ListShippingAssociatedProject",
1000 'class' =>
'Expedition',
1001 'table' =>
'expedition',
1002 'disableamount' => 1,
1003 'test' =>
isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
1005 'expensereport' => array(
1006 'title' =>
"ListExpenseReportsAssociatedProject",
1007 'class' =>
'ExpenseReportLine',
1008 'table' =>
'expensereport_det',
1009 'test' =>
isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1011 'donation' => array(
1012 'title' =>
"ListDonationsAssociatedProject",
1015 'test' =>
isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1018 'title' =>
"ListLoanAssociatedProject",
1021 'test' =>
isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1023 'chargesociales' => array(
1024 'title' =>
"ListSocialContributionAssociatedProject",
1025 'class' =>
'ChargeSociales',
1026 'table' =>
'chargesociales',
1027 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1028 'test' =>
isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1030 'stock_mouvement' => array(
1031 'title' =>
"ListMouvementStockProject",
1032 'class' =>
'MouvementStock',
1033 'table' =>
'stock_mouvement',
1037 'title' =>
"ListActionsAssociatedProject",
1038 'class' =>
'ActionComm',
1039 'table' =>
'actioncomm',
1040 'disableamount' => 1,
1041 'test' =>
isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1047 $listlines = $odfHandler->setSegment(
'projectrefs');
1049 foreach ($listofreferent as $keyref => $valueref) {
1050 $title = $valueref[
'title'];
1051 $tablename = $valueref[
'table'];
1052 $classname = $valueref[
'class'];
1053 $qualified = $valueref[
'test'];
1055 $elementarray =
$object->get_element_list($keyref, $tablename);
1056 if (count($elementarray) > 0 && is_array($elementarray)) {
1059 $num = count($elementarray);
1060 for ($i = 0; $i < $num; $i++) {
1061 $ref_array = array();
1062 $ref_array[
'type'] = (
string) $langs->trans($classname);
1064 $element =
new $classname($this->db);
1065 $element->fetch((
int) $elementarray[$i]);
1066 $element->fetch_thirdparty();
1069 $ref_array[
'ref'] = (
string) $element->ref;
1072 $dateref = $element->date;
1073 if (empty($dateref)) {
1074 $dateref = $element->datep;
1076 if (empty($dateref)) {
1077 $dateref = $element->date_contrat;
1079 $ref_array[
'date'] = (
string) $dateref;
1082 if (is_object($element->thirdparty)) {
1083 $ref_array[
'socname'] = $element->thirdparty->name;
1085 $ref_array[
'socname'] =
'';
1089 if (empty($valueref[
'disableamount'])) {
1090 if (!empty($element->total_ht)) {
1091 $ref_array[
'amountht'] = (float) $element->total_ht;
1092 $ref_array[
'amountttc'] = (float) $element->total_ttc;
1094 $ref_array[
'amountht'] = 0;
1095 $ref_array[
'amountttc'] = 0;
1098 $ref_array[
'amountht'] =
'';
1099 $ref_array[
'amountttc'] =
'';
1102 $ref_array[
'status'] = $element->getLibStatut(0);
1106 foreach ($tmparray as $key => $val) {
1108 $listlines->setVars($key, $val,
true,
'UTF-8');
1109 }
catch (SegmentException $e) {
1113 $listlines->merge();
1117 $odfHandler->mergeSegment($listlines);
1119 }
catch (OdfExceptionSegmentNotFound $e) {
1121 }
catch (OdfException $e) {
1122 $this->error = $e->getMessage();
1128 $tmparray = $outputlangs->get_translations_for_substitutions();
1129 foreach ($tmparray as $key => $value) {
1131 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1132 }
catch (OdfException $e) {
1138 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1139 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1145 $odfHandler->exportAsAttachedPDF($file);
1147 $this->error = $e->getMessage();
1152 $odfHandler->saveToDisk($file);
1154 $this->error = $e->getMessage();
1159 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1160 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1166 $this->result = array(
'fullpath' => $file);
1170 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);