contact.lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /* Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010-2017 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2015-2021 Frederic France <frederic.france@netlogic.fr>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. * or see https://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/lib/contact.lib.php
  23. * \brief Ensemble de fonctions de base pour les contacts
  24. */
  25. /**
  26. * Prepare array with list of tabs
  27. *
  28. * @param Contact $object Object related to tabs
  29. * @return array Array of tabs to show
  30. */
  31. function contact_prepare_head(Contact $object)
  32. {
  33. global $db, $langs, $conf, $user;
  34. $tab = 0;
  35. $head = array();
  36. $head[$tab][0] = DOL_URL_ROOT.'/contact/card.php?id='.$object->id;
  37. $head[$tab][1] = $langs->trans("Contact");
  38. $head[$tab][2] = 'card';
  39. $tab++;
  40. if ((!empty($conf->ldap->enabled) && !empty($conf->global->LDAP_CONTACT_ACTIVE))
  41. && (empty($conf->global->MAIN_DISABLE_LDAP_TAB) || !empty($user->admin))) {
  42. $langs->load("ldap");
  43. $head[$tab][0] = DOL_URL_ROOT.'/contact/ldap.php?id='.$object->id;
  44. $head[$tab][1] = $langs->trans("LDAPCard");
  45. $head[$tab][2] = 'ldap';
  46. $tab++;
  47. }
  48. $head[$tab][0] = DOL_URL_ROOT.'/contact/perso.php?id='.$object->id;
  49. $head[$tab][1] = $langs->trans("PersonalInformations");
  50. $head[$tab][2] = 'perso';
  51. $tab++;
  52. if (isModEnabled('project') && $user->hasRight('project', 'lire')) {
  53. $nbProject = 0;
  54. // Enable caching of thirdrparty count projects
  55. require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
  56. $cachekey = 'count_projects_contact_'.$object->id;
  57. $dataretrieved = dol_getcache($cachekey);
  58. if (!is_null($dataretrieved)) {
  59. $nbProject = $dataretrieved;
  60. } else {
  61. $sql = 'SELECT COUNT(n.rowid) as nb';
  62. $sql .= ' FROM '.MAIN_DB_PREFIX.'projet as n';
  63. $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact as cc ON (n.rowid = cc.element_id)';
  64. $sql .= " WHERE cc.fk_socpeople = ".((int) $object->id);
  65. $sql .= " AND cc.fk_c_type_contact IN (SELECT rowid FROM ".MAIN_DB_PREFIX."c_type_contact WHERE element='project' AND source='external')";
  66. $sql .= " AND n.entity IN (".getEntity('project').")";
  67. $resql = $db->query($sql);
  68. if ($resql) {
  69. $obj = $db->fetch_object($resql);
  70. $nbProject = $obj->nb;
  71. } else {
  72. dol_print_error($db);
  73. }
  74. dol_setcache($cachekey, $nbProject, 120); // If setting cache fails, this is not a problem, so we do not test result.
  75. }
  76. $head[$tab][0] = DOL_URL_ROOT.'/contact/project.php?id='.$object->id;
  77. $head[$tab][1] = $langs->trans("Projects");
  78. if ($nbProject > 0) {
  79. $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.$nbProject.'</span>';
  80. }
  81. $head[$tab][2] = 'project';
  82. $tab++;
  83. }
  84. // Related items
  85. if (isModEnabled('commande') || isModEnabled("propal") || isModEnabled('facture') || isModEnabled('ficheinter') || (isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) {
  86. $head[$tab][0] = DOL_URL_ROOT.'/contact/consumption.php?id='.$object->id;
  87. $head[$tab][1] = $langs->trans("Referers");
  88. $head[$tab][2] = 'consumption';
  89. $tab++;
  90. }
  91. // Show more tabs from modules
  92. // Entries must be declared in modules descriptor with line
  93. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  94. // $this->tabs = array('entity:-tabname); to remove a tab
  95. complete_head_from_modules($conf, $langs, $object, $head, $tab, 'contact', 'add', 'core');
  96. // Notes
  97. if (empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
  98. $nbNote = (empty($object->note_private) ? 0 : 1) + (empty($object->note_public) ? 0 : 1);
  99. $head[$tab][0] = DOL_URL_ROOT.'/contact/note.php?id='.$object->id;
  100. $head[$tab][1] = $langs->trans("Note");
  101. if ($nbNote > 0) {
  102. $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  103. }
  104. $head[$tab][2] = 'note';
  105. $tab++;
  106. }
  107. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  108. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  109. $upload_dir = $conf->societe->dir_output."/contact/".dol_sanitizeFileName($object->ref);
  110. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  111. $nbLinks = Link::count($db, $object->element, $object->id);
  112. $head[$tab][0] = DOL_URL_ROOT.'/contact/document.php?id='.$object->id;
  113. $head[$tab][1] = $langs->trans("Documents");
  114. if (($nbFiles + $nbLinks) > 0) {
  115. $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  116. }
  117. $head[$tab][2] = 'documents';
  118. $tab++;
  119. // Agenda / Events
  120. $head[$tab][0] = DOL_URL_ROOT.'/contact/agenda.php?id='.$object->id;
  121. $head[$tab][1] = $langs->trans("Events");
  122. if (isModEnabled('agenda') && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) {
  123. $head[$tab][1] .= '/';
  124. $head[$tab][1] .= $langs->trans("Agenda");
  125. }
  126. $head[$tab][2] = 'agenda';
  127. $tab++;
  128. // Log
  129. /*
  130. $head[$tab][0] = DOL_URL_ROOT.'/contact/info.php?id='.$object->id;
  131. $head[$tab][1] = $langs->trans("Info");
  132. $head[$tab][2] = 'info';
  133. $tab++;*/
  134. complete_head_from_modules($conf, $langs, $object, $head, $tab, 'contact', 'add', 'external');
  135. complete_head_from_modules($conf, $langs, $object, $head, $tab, 'contact', 'remove');
  136. return $head;
  137. }
  138. /**
  139. * Show html area for list of projects
  140. *
  141. * @param Conf $conf Object conf
  142. * @param Translate $langs Object langs
  143. * @param DoliDB $db Database handler
  144. * @param Object $object Third party object
  145. * @param string $backtopage Url to go once contact is created
  146. * @param int $nocreatelink 1=Hide create project link
  147. * @param string $morehtmlright More html on right of title
  148. * @return int
  149. */
  150. function show_contacts_projects($conf, $langs, $db, $object, $backtopage = '', $nocreatelink = 0, $morehtmlright = '')
  151. {
  152. global $user;
  153. $i = -1;
  154. if (isModEnabled('project') && $user->rights->projet->lire) {
  155. $langs->load("projects");
  156. $newcardbutton = '';
  157. if (isModEnabled('project') && $user->rights->projet->creer && empty($nocreatelink)) {
  158. $newcardbutton .= dolGetButtonTitle($langs->trans('AddProject'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/projet/card.php?socid='.$object->id.'&amp;action=create&amp;backtopage='.urlencode($backtopage));
  159. }
  160. print "\n";
  161. print load_fiche_titre($langs->trans("ProjectsHavingThisContact"), $newcardbutton.$morehtmlright, '');
  162. print '<div class="div-table-responsive">';
  163. print "\n".'<table class="noborder" width=100%>';
  164. $sql = 'SELECT p.rowid as id, p.entity, p.title, p.ref, p.public, p.dateo as do, p.datee as de, p.fk_statut as status, p.fk_opp_status, p.opp_amount, p.opp_percent, p.tms as date_update, p.budget_amount';
  165. $sql .= ', cls.code as opp_status_code, ctc.libelle';
  166. $sql .= ' FROM '.MAIN_DB_PREFIX.'projet as p';
  167. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_lead_status as cls on p.fk_opp_status = cls.rowid';
  168. $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'element_contact as cc ON (p.rowid = cc.element_id)';
  169. $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'c_type_contact as ctc ON (ctc.rowid = cc.fk_c_type_contact)';
  170. $sql .= " WHERE cc.fk_socpeople = ".((int) $object->id);
  171. $sql .= " AND ctc.element='project' AND ctc.source='external'";
  172. $sql .= " AND p.entity IN (".getEntity('project').")";
  173. $sql .= " ORDER BY p.dateo DESC";
  174. $result = $db->query($sql);
  175. if ($result) {
  176. $num = $db->num_rows($result);
  177. print '<tr class="liste_titre">';
  178. print '<td>'.$langs->trans("Ref").'</td>';
  179. print '<td>'.$langs->trans("Name").'</td>';
  180. print '<td>'.$langs->trans("ContactType").'</td>';
  181. print '<td class="center">'.$langs->trans("DateStart").'</td>';
  182. print '<td class="center">'.$langs->trans("DateEnd").'</td>';
  183. print '<td class="right">'.$langs->trans("OpportunityAmountShort").'</td>';
  184. print '<td class="center">'.$langs->trans("OpportunityStatusShort").'</td>';
  185. print '<td class="right">'.$langs->trans("OpportunityProbabilityShort").'</td>';
  186. print '<td class="right">'.$langs->trans("Status").'</td>';
  187. print '</tr>';
  188. if ($num > 0) {
  189. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  190. $projecttmp = new Project($db);
  191. $i = 0;
  192. while ($i < $num) {
  193. $obj = $db->fetch_object($result);
  194. $projecttmp->fetch($obj->id);
  195. // To verify role of users
  196. $userAccess = $projecttmp->restrictedProjectArea($user);
  197. if ($user->rights->projet->lire && $userAccess > 0) {
  198. print '<tr class="oddeven">';
  199. // Ref
  200. print '<td>';
  201. print $projecttmp->getNomUrl(1);
  202. print '</td>';
  203. // Label
  204. print '<td>'.$obj->title.'</td>';
  205. print '<td>'.$obj->libelle.'</td>';
  206. // Date start
  207. print '<td class="center">'.dol_print_date($db->jdate($obj->do), "day").'</td>';
  208. // Date end
  209. print '<td class="center">'.dol_print_date($db->jdate($obj->de), "day").'</td>';
  210. // Opp amount
  211. print '<td class="right">';
  212. if ($obj->opp_status_code) {
  213. print price($obj->opp_amount, 1, '', 1, -1, -1, '');
  214. }
  215. print '</td>';
  216. // Opp status
  217. print '<td class="center">';
  218. if ($obj->opp_status_code) {
  219. print $langs->trans("OppStatus".$obj->opp_status_code);
  220. }
  221. print '</td>';
  222. // Opp percent
  223. print '<td class="right">';
  224. if ($obj->opp_percent) {
  225. print price($obj->opp_percent, 1, '', 1, 0).'%';
  226. }
  227. print '</td>';
  228. // Status
  229. print '<td class="right">'.$projecttmp->getLibStatut(5).'</td>';
  230. print '</tr>';
  231. }
  232. $i++;
  233. }
  234. } else {
  235. print '<tr class="oddeven"><td colspan="8"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  236. }
  237. $db->free($result);
  238. } else {
  239. dol_print_error($db);
  240. }
  241. print "</table>";
  242. print '</div>';
  243. print "<br>\n";
  244. }
  245. return $i;
  246. }