box_contacts.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  6. * Copyright (C) 2018 Josep Lluís Amador <joseplluis@lliuretic.cat>
  7. * Copyright (C) 2020 Ferran Marcet <fmarcet@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/core/boxes/box_contacts.php
  24. * \ingroup contacts
  25. * \brief Module to show box of contacts
  26. */
  27. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  28. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  29. /**
  30. * Class to manage the box to show last contacts
  31. */
  32. class box_contacts extends ModeleBoxes
  33. {
  34. public $boxcode = "lastcontacts";
  35. public $boximg = "object_contact";
  36. public $boxlabel = "BoxLastContacts";
  37. public $depends = array("societe");
  38. /**
  39. * @var DoliDB Database handler.
  40. */
  41. public $db;
  42. public $param;
  43. public $info_box_head = array();
  44. public $info_box_contents = array();
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDB $db Database handler
  49. * @param string $param More parameters
  50. */
  51. public function __construct($db, $param)
  52. {
  53. global $user;
  54. $this->db = $db;
  55. $this->hidden = !($user->hasRight('societe', 'lire') && $user->hasRight('societe', 'contact', 'lire'));
  56. }
  57. /**
  58. * Load data into info_box_contents array to show array later.
  59. *
  60. * @param int $max Maximum number of records to load
  61. * @return void
  62. */
  63. public function loadBox($max = 5)
  64. {
  65. global $user, $langs, $conf, $hookmanager;
  66. $langs->load("boxes");
  67. $this->max = $max;
  68. $contactstatic = new Contact($this->db);
  69. $societestatic = new Societe($this->db);
  70. $this->info_box_head = array('text' => $langs->trans("BoxTitleLastModifiedContacts", $max));
  71. if ($user->hasRight('societe', 'lire') && $user->rights->societe->contact->lire) {
  72. $sql = "SELECT sp.rowid as id, sp.lastname, sp.firstname, sp.civility as civility_id, sp.datec, sp.tms, sp.fk_soc, sp.statut as status";
  73. $sql .= ", sp.address, sp.zip, sp.town, sp.phone, sp.phone_perso, sp.phone_mobile, sp.email as spemail";
  74. $sql .= ", s.rowid as socid, s.nom as name, s.name_alias";
  75. $sql .= ", s.code_client, s.client";
  76. $sql .= ", s.code_fournisseur, s.code_compta_fournisseur, s.fournisseur";
  77. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  78. $sql .= ", spe.accountancy_code_customer as code_compta";
  79. $sql .= ", spe.accountancy_code_supplier as code_compta_fournisseur";
  80. } else {
  81. $sql .= ", s.code_compta";
  82. $sql .= ", s.code_compta_fournisseur";
  83. }
  84. $sql .= ", s.logo, s.email, s.entity";
  85. $sql .= ", co.label as country, co.code as country_code";
  86. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  87. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON sp.fk_pays = co.rowid";
  88. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON sp.fk_soc = s.rowid";
  89. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  90. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
  91. }
  92. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  93. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  94. }
  95. $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
  96. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  97. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  98. }
  99. // Add where from hooks
  100. $parameters = array('socid' => $user->socid, 'boxcode' => $this->boxcode);
  101. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $contactstatic); // Note that $action and $object may have been modified by hook
  102. if (empty($reshook)) {
  103. if ($user->socid > 0) {
  104. $sql .= " AND sp.fk_soc = ".((int) $user->socid);
  105. }
  106. }
  107. $sql .= $hookmanager->resPrint;
  108. $sql .= " ORDER BY sp.tms DESC";
  109. $sql .= $this->db->plimit($max, 0);
  110. $result = $this->db->query($sql);
  111. if ($result) {
  112. $num = $this->db->num_rows($result);
  113. $line = 0;
  114. while ($line < $num) {
  115. $objp = $this->db->fetch_object($result);
  116. $datec = $this->db->jdate($objp->datec);
  117. $datem = $this->db->jdate($objp->tms);
  118. $contactstatic->id = $objp->id;
  119. $contactstatic->lastname = $objp->lastname;
  120. $contactstatic->firstname = $objp->firstname;
  121. $contactstatic->civility_id = $objp->civility_id;
  122. $contactstatic->statut = $objp->status;
  123. $contactstatic->phone_pro = $objp->phone;
  124. $contactstatic->phone_perso = $objp->phone_perso;
  125. $contactstatic->phone_mobile = $objp->phone_mobile;
  126. $contactstatic->email = $objp->spemail;
  127. $contactstatic->address = $objp->address;
  128. $contactstatic->zip = $objp->zip;
  129. $contactstatic->town = $objp->town;
  130. $contactstatic->country = $objp->country;
  131. $contactstatic->country_code = $objp->country_code;
  132. $societestatic->id = $objp->socid;
  133. $societestatic->name = $objp->name;
  134. //$societestatic->name_alias = $objp->name_alias;
  135. $societestatic->code_client = $objp->code_client;
  136. $societestatic->code_compta = $objp->code_compta;
  137. $societestatic->client = $objp->client;
  138. $societestatic->code_fournisseur = $objp->code_fournisseur;
  139. $societestatic->code_compta_fournisseur = $objp->code_compta_fournisseur;
  140. $societestatic->fournisseur = $objp->fournisseur;
  141. $societestatic->logo = $objp->logo;
  142. $societestatic->email = $objp->email;
  143. $societestatic->entity = $objp->entity;
  144. $this->info_box_contents[$line][] = array(
  145. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  146. 'text' => $contactstatic->getNomUrl(1),
  147. 'asis' => 1,
  148. );
  149. $this->info_box_contents[$line][] = array(
  150. 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"',
  151. 'text' => ($societestatic->id > 0 ? $societestatic->getNomUrl(1) : ''),
  152. 'asis' => 1,
  153. );
  154. $this->info_box_contents[$line][] = array(
  155. 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateModification").': '.dol_print_date($datem, 'dayhour', 'tzuserrel')).'"',
  156. 'text' => dol_print_date($datem, "day", 'tzuserrel'),
  157. );
  158. $this->info_box_contents[$line][] = array(
  159. 'td' => 'class="nowrap right" width="18"',
  160. 'text' => $contactstatic->getLibStatut(3),
  161. 'asis'=>1,
  162. );
  163. $line++;
  164. }
  165. if ($num == 0) {
  166. $this->info_box_contents[$line][0] = array(
  167. 'td' => 'class="center"',
  168. 'text'=> '<span class="opacitymedium">'.$langs->trans("NoRecordedContacts").'</span>',
  169. 'asis'=> 1
  170. );
  171. }
  172. $this->db->free($result);
  173. } else {
  174. $this->info_box_contents[0][0] = array(
  175. 'td' => '',
  176. 'maxlength'=>500,
  177. 'text' => ($this->db->error().' sql='.$sql),
  178. );
  179. }
  180. } else {
  181. $this->info_box_contents[0][0] = array(
  182. 'td' => 'class="nohover left"',
  183. 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
  184. );
  185. }
  186. }
  187. /**
  188. * Method to show box
  189. *
  190. * @param array $head Array with properties of box title
  191. * @param array $contents Array with properties of box lines
  192. * @param int $nooutput No print, only return string
  193. * @return string
  194. */
  195. public function showBox($head = null, $contents = null, $nooutput = 0)
  196. {
  197. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  198. }
  199. }