PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/contact/class/contact.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 1019 lines | 552 code | 112 blank | 355 comment | 106 complexity | b9124e46e1b2ec633f574ced46eb4355 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  4. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerker@telenet.be>
  7. * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  8. * Copyright (C) 2012 Herve Prot <herve.prot@symeos.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/contact/class/contact.class.php
  25. * \ingroup societe
  26. * \brief File of contacts class
  27. */
  28. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  29. /**
  30. * \class Contact
  31. * \brief Classe permettant la gestion des contacts
  32. */
  33. class Contact extends nosqlDocument {
  34. public $element = 'contact';
  35. public $table_element = 'socpeople';
  36. var $id;
  37. var $civilite_id; // In fact we stor civility_code
  38. var $lastname;
  39. var $name; // TODO deprecated
  40. var $firstname;
  41. var $address;
  42. var $zip;
  43. var $town;
  44. var $state_id; // Id of department
  45. var $country_id; // Id of country
  46. var $societe; // fk_soc
  47. var $status; // 0=brouillon, 1=4=actif, 5=inactif
  48. var $code;
  49. var $email;
  50. var $birthday;
  51. var $default_lang;
  52. var $notes; // Private note
  53. var $no_email; // 1=Don't send e-mail to this contact, 0=do
  54. var $ref_facturation; // Nb de reference facture pour lequel il est contact
  55. var $ref_contrat; // Nb de reference contrat pour lequel il est contact
  56. var $ref_commande; // Nb de reference commande pour lequel il est contact
  57. var $ref_propal; // Nb de reference propal pour lequel il est contact
  58. var $user_login;
  59. var $import_key;
  60. var $oldcopy; // To contains a clone of this when we need to save old properties of object
  61. /**
  62. * Constructor
  63. *
  64. * @param DoliDB $db Database handler
  65. */
  66. function __construct() {
  67. parent::__construct($db);
  68. $this->fk_extrafields = new ExtraFields($db);
  69. $this->fk_extrafields->fetch(get_class($this));
  70. $this->societe = new stdClass();
  71. return 1;
  72. }
  73. /**
  74. * Add a contact into database
  75. *
  76. * @param User $user Object user that create
  77. * @return int <0 if KO, >0 if OK
  78. */
  79. function create($user) {
  80. global $conf, $langs;
  81. $error = 0;
  82. $now = dol_now();
  83. // Clean parameters
  84. $this->lastname = $this->lastname ? trim($this->lastname) : $this->name;
  85. $this->firstname = trim($this->firstname);
  86. if (!empty($conf->global->MAIN_FIRST_TO_UPPER))
  87. $this->lastname = ucwords($this->lastname);
  88. if (!empty($conf->global->MAIN_FIRST_TO_UPPER))
  89. $this->firstname = ucwords($this->firstname);
  90. $result = $this->update($this->id, $user, 1);
  91. if ($result < 0) {
  92. $error++;
  93. $this->error = $this->db->lasterror();
  94. }
  95. $this->date_create = dol_now();
  96. if (!$error) {
  97. // Appel des triggers
  98. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  99. $interface = new Interfaces($this->db);
  100. $result = $interface->run_triggers('CONTACT_CREATE', $this, $user, $langs, $conf);
  101. if ($result < 0) {
  102. $error++;
  103. $this->errors = $interface->errors;
  104. }
  105. // Fin appel triggers
  106. }
  107. return $this->id;
  108. }
  109. /**
  110. * Update informations into database
  111. *
  112. * @param int $id Id of contact/address to update
  113. * @param User $user Objet user making change
  114. * @param int $notrigger 0=no, 1=yesi
  115. * @return int <0 if KO, >0 if OK
  116. */
  117. function update($id, $user = 0, $notrigger = 0) {
  118. global $conf, $langs, $hookmanager;
  119. $error = 0;
  120. $this->id = $id;
  121. // Clean parameters
  122. $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->name);
  123. $this->firstname = trim($this->firstname);
  124. $this->email = trim($this->email);
  125. $this->phone_pro = trim($this->phone_pro);
  126. $this->phone_perso = trim($this->phone_perso);
  127. $this->phone_mobile = trim($this->phone_mobile);
  128. $this->fax = trim($this->fax);
  129. $this->zip = $this->zip;
  130. $this->town = $this->town;
  131. $this->country_id = $this->country_id;
  132. $this->state_id = $this->state_id;
  133. $this->fk_user_modif = $user->login;
  134. $this->tms = dol_now();
  135. $this->name = $this->firstname . " " . $this->lastname;
  136. if (!empty($this->societe->id)) {
  137. $object = new Societe($this->db);
  138. $object->load($this->societe->id);
  139. $this->societe->name = $object->name;
  140. } else {
  141. unset($this->societe->name);
  142. }
  143. $this->record();
  144. // Actions on extra fields (by external module or standard code)
  145. // TODO deprecated, replace with card builder
  146. $hookmanager->initHooks(array('contactdao'));
  147. $parameters = array('socid' => $this->id);
  148. $reshook = $hookmanager->executeHooks('insertExtraFields', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  149. if (empty($reshook)) {
  150. $result = $this->insertExtraFields();
  151. if ($result < 0) {
  152. $error++;
  153. }
  154. } else if ($reshook < 0)
  155. $error++;
  156. if (!$error && !$notrigger) {
  157. // Appel des triggers
  158. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  159. $interface = new Interfaces($this->db);
  160. $result = $interface->run_triggers('CONTACT_MODIFY', $this, $user, $langs, $conf);
  161. if ($result < 0) {
  162. $error++;
  163. $this->errors = $interface->errors;
  164. }
  165. // Fin appel triggers
  166. return 1;
  167. }
  168. }
  169. /**
  170. * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
  171. *
  172. * @param array $info Info string loaded by _load_ldap_info
  173. * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
  174. * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
  175. * 2=Return key only (uid=qqq)
  176. * @return string DN
  177. */
  178. function _load_ldap_dn($info, $mode = 0) {
  179. global $conf;
  180. $dn = '';
  181. if ($mode == 0)
  182. $dn = $conf->global->LDAP_KEY_CONTACTS . "=" . $info[$conf->global->LDAP_KEY_CONTACTS] . "," . $conf->global->LDAP_CONTACT_DN;
  183. if ($mode == 1)
  184. $dn = $conf->global->LDAP_CONTACT_DN;
  185. if ($mode == 2)
  186. $dn = $conf->global->LDAP_KEY_CONTACTS . "=" . $info[$conf->global->LDAP_KEY_CONTACTS];
  187. return $dn;
  188. }
  189. /**
  190. * Initialise tableau info (tableau des attributs LDAP)
  191. *
  192. * @return array Tableau info des attributs
  193. */
  194. function _load_ldap_info() {
  195. global $conf, $langs;
  196. // Object classes
  197. $info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);
  198. $this->fullname = $this->getFullName($langs);
  199. // Fields
  200. if ($this->fullname && !empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME))
  201. $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
  202. if ($this->lastname && !empty($conf->global->LDAP_CONTACT_FIELD_NAME))
  203. $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname;
  204. if ($this->firstname && !empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME))
  205. $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
  206. if ($this->poste)
  207. $info["title"] = $this->poste;
  208. if (isset($this->societe->id)) {
  209. $soc = new Societe($this->db);
  210. $soc->fetch($this->socete->id);
  211. $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->nom;
  212. if ($soc->client == 1)
  213. $info["businessCategory"] = "Customers";
  214. if ($soc->client == 2)
  215. $info["businessCategory"] = "Prospects";
  216. if ($soc->fournisseur == 1)
  217. $info["businessCategory"] = "Suppliers";
  218. }
  219. if ($this->address && !empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS))
  220. $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
  221. if ($this->cp && !empty($conf->global->LDAP_CONTACT_FIELD_ZIP))
  222. $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->cp;
  223. if ($this->ville && !empty($conf->global->LDAP_CONTACT_FIELD_TOWN))
  224. $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->ville;
  225. if ($this->country_code && !empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY))
  226. $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code;
  227. if ($this->phone_pro && !empty($conf->global->LDAP_CONTACT_FIELD_PHONE))
  228. $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
  229. if ($this->phone_perso && !empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE))
  230. $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
  231. if ($this->phone_mobile && !empty($conf->global->LDAP_CONTACT_FIELD_MOBILE))
  232. $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
  233. if ($this->fax && !empty($conf->global->LDAP_CONTACT_FIELD_FAX))
  234. $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
  235. if ($this->note && !empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION))
  236. $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = $this->note;
  237. if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL))
  238. $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
  239. if ($conf->global->LDAP_SERVER_TYPE == 'egroupware') {
  240. $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
  241. $info['uidnumber'] = $this->id;
  242. $info['phpgwTz'] = 0;
  243. $info['phpgwMailType'] = 'INTERNET';
  244. $info['phpgwMailHomeType'] = 'INTERNET';
  245. $info["phpgwContactTypeId"] = 'n';
  246. $info["phpgwContactCatId"] = 0;
  247. $info["phpgwContactAccess"] = "public";
  248. if (dol_strlen($this->egroupware_id) == 0) {
  249. $this->egroupware_id = 1;
  250. }
  251. $info["phpgwContactOwner"] = $this->egroupware_id;
  252. if ($this->email)
  253. $info["rfc822Mailbox"] = $this->email;
  254. if ($this->phone_mobile)
  255. $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
  256. }
  257. return $info;
  258. }
  259. /**
  260. * Charge l'objet contact
  261. *
  262. * @param int $id id du contact
  263. * @param User $user Utilisateur (abonnes aux alertes) qui veut les alertes de ce contact
  264. * @return int -1 if KO, 0 if OK but not found, 1 if OK
  265. */
  266. function fetch($id, $user = 0) {
  267. global $langs;
  268. $langs->load("companies");
  269. try {
  270. $this->load($id);
  271. return 1;
  272. } catch (Exception $e) {
  273. $this->error = $e->getMessage();
  274. return -1;
  275. }
  276. $sql = "SELECT c.rowid, c.fk_soc, c.civilite as civilite_id, c.name as lastname, c.firstname,";
  277. $sql.= " c.address, c.cp as zip, c.ville as town,";
  278. $sql.= " c.fk_pays as country_id,";
  279. $sql.= " c.fk_departement,";
  280. $sql.= " c.birthday,";
  281. $sql.= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid,";
  282. $sql.= " c.priv, c.note, c.default_lang, c.no_email, c.canvas,";
  283. $sql.= " c.import_key,";
  284. $sql.= " p.libelle as country, p.code as country_code,";
  285. $sql.= " d.nom as state, d.code_departement as state_code,";
  286. $sql.= " u.rowid as user_id, u.login as user_login,";
  287. $sql.= " s.nom as socname, s.address as socaddress, s.cp as soccp, s.ville as soccity, s.default_lang as socdefault_lang";
  288. $sql.= " FROM " . MAIN_DB_PREFIX . "socpeople as c";
  289. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "c_pays as p ON c.fk_pays = p.rowid";
  290. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "c_departements as d ON c.fk_departement = d.rowid";
  291. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u ON c.rowid = u.fk_socpeople";
  292. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON c.fk_soc = s.rowid";
  293. $sql.= " WHERE c.rowid = " . $id;
  294. dol_syslog(get_class($this) . "::fetch sql=" . $sql);
  295. $resql = $this->db->query($sql);
  296. if ($resql) {
  297. if ($this->db->num_rows($resql)) {
  298. $obj = $this->db->fetch_object($resql);
  299. $this->id = $obj->rowid;
  300. $this->ref = $obj->rowid;
  301. $this->civilite_id = $obj->civilite_id;
  302. $this->lastname = $obj->lastname;
  303. $this->name = $obj->lastname; // TODO deprecated
  304. $this->firstname = $obj->firstname;
  305. $this->nom = $obj->lastname; // TODO deprecated
  306. $this->prenom = $obj->firstname; // TODO deprecated
  307. $this->address = $obj->address;
  308. $this->adresse = $obj->address; // TODO deprecated
  309. $this->cp = $obj->zip; // TODO deprecated
  310. $this->zip = $obj->zip;
  311. $this->ville = $obj->town; // TODO deprecated
  312. $this->town = $obj->town;
  313. $this->fk_departement = $obj->fk_departement; // deprecated
  314. $this->state_id = $obj->fk_departement;
  315. $this->departement_code = $obj->state_code; // deprecated
  316. $this->state_code = $obj->state_code;
  317. $this->departement = $obj->state; // deprecated
  318. $this->state = $obj->state;
  319. $this->fk_pays = $obj->country_id;
  320. $this->country_id = $obj->country_id;
  321. $this->pays_code = $obj->country_id ? $obj->country_code : '';
  322. $this->country_code = $obj->country_id ? $obj->country_code : '';
  323. $this->pays = ($obj->country_id > 0) ? $langs->transnoentitiesnoconv("Country" . $obj->country_code) : '';
  324. $this->country = ($obj->country_id > 0) ? $langs->transnoentitiesnoconv("Country" . $obj->country_code) : '';
  325. $this->socname = $obj->socname;
  326. $this->poste = $obj->poste;
  327. $this->phone_pro = trim($obj->phone);
  328. $this->fax = trim($obj->fax);
  329. $this->phone_perso = trim($obj->phone_perso);
  330. $this->phone_mobile = trim($obj->phone_mobile);
  331. $this->email = $obj->email;
  332. $this->jabberid = $obj->jabberid;
  333. $this->mail = $obj->email;
  334. $this->birthday = $this->db->jdate($obj->birthday);
  335. $this->note = $obj->note;
  336. $this->default_lang = $obj->default_lang;
  337. $this->no_email = $obj->no_email;
  338. $this->user_id = $obj->user_id;
  339. $this->user_login = $obj->user_login;
  340. $this->canvas = $obj->canvas;
  341. $this->import_key = $obj->import_key;
  342. // Recherche le user Dolibarr lie a ce contact
  343. $sql = "SELECT u.rowid ";
  344. $sql .= " FROM " . MAIN_DB_PREFIX . "user as u";
  345. $sql .= " WHERE u.fk_socpeople = " . $this->id;
  346. $resql = $this->db->query($sql);
  347. if ($resql) {
  348. if ($this->db->num_rows($resql)) {
  349. $uobj = $this->db->fetch_object($resql);
  350. $this->user_id = $uobj->rowid;
  351. }
  352. $this->db->free($resql);
  353. } else {
  354. $this->error = $this->db->error();
  355. dol_syslog(get_class($this) . "::fetch " . $this->error, LOG_ERR);
  356. return -1;
  357. }
  358. // Charge alertes du user
  359. if ($user) {
  360. $sql = "SELECT fk_user";
  361. $sql .= " FROM " . MAIN_DB_PREFIX . "user_alert";
  362. $sql .= " WHERE fk_user = " . $user->id . " AND fk_contact = " . $id;
  363. $resql = $this->db->query($sql);
  364. if ($resql) {
  365. if ($this->db->num_rows($resql)) {
  366. $obj = $this->db->fetch_object($resql);
  367. $this->birthday_alert = 1;
  368. }
  369. $this->db->free($resql);
  370. } else {
  371. $this->error = $this->db->error();
  372. dol_syslog(get_class($this) . "::fetch " . $this->error, LOG_ERR);
  373. return -1;
  374. }
  375. }
  376. return 1;
  377. } else {
  378. $this->error = $langs->trans("RecordNotFound");
  379. return 0;
  380. }
  381. } else {
  382. $this->error = $this->db->error();
  383. dol_syslog(get_class($this) . "::fetch " . $this->error, LOG_ERR);
  384. return -1;
  385. }
  386. }
  387. /**
  388. * Charge le nombre d'elements auquel est lie ce contact
  389. * ref_facturation
  390. * ref_contrat
  391. * ref_commande
  392. * ref_propale
  393. *
  394. * @return int <0 if KO, >=0 if OK
  395. */
  396. function load_ref_elements() {
  397. return false;
  398. /*
  399. // Compte les elements pour lesquels il est contact
  400. $sql = "SELECT tc.element, count(ec.rowid) as nb";
  401. $sql.=" FROM " . MAIN_DB_PREFIX . "element_contact as ec, " . MAIN_DB_PREFIX . "c_type_contact as tc";
  402. $sql.=" WHERE ec.fk_c_type_contact = tc.rowid";
  403. $sql.=" AND fk_socpeople = " . $this->id;
  404. $sql.=" GROUP BY tc.element";
  405. dol_syslog(get_class($this) . "::load_ref_elements sql=" . $sql);
  406. $resql = $this->db->query($sql);
  407. if ($resql) {
  408. while ($obj = $this->db->fetch_object($resql)) {
  409. if ($obj->nb) {
  410. if ($obj->element == 'facture')
  411. $this->ref_facturation = $obj->nb;
  412. if ($obj->element == 'contrat')
  413. $this->ref_contrat = $obj->nb;
  414. if ($obj->element == 'commande')
  415. $this->ref_commande = $obj->nb;
  416. if ($obj->element == 'propal')
  417. $this->ref_propal = $obj->nb;
  418. }
  419. }
  420. $this->db->free($resql);
  421. return 0;
  422. }
  423. else {
  424. $this->error = $this->db->error() . " - " . $sql;
  425. dol_syslog(get_class($this) . "::load_ref_elements Error " . $this->error, LOG_ERR);
  426. return -1;
  427. }
  428. */
  429. }
  430. /**
  431. * Efface le contact de la base
  432. *
  433. * @param int $notrigger Disable all trigger
  434. * @return int <0 if KO, >0 if OK
  435. */
  436. function delete($notrigger = 0) {
  437. global $conf, $langs, $user;
  438. $error = 0;
  439. $this->old_name = $obj->name;
  440. $this->old_firstname = $obj->firstname;
  441. $this->db->begin();
  442. if (!$error) {
  443. // Get all rowid of element_contact linked to a type that is link to llx_socpeople
  444. $sql = "SELECT ec.rowid";
  445. $sql.= " FROM " . MAIN_DB_PREFIX . "element_contact ec,";
  446. $sql.= " " . MAIN_DB_PREFIX . "c_type_contact tc";
  447. $sql.= " WHERE ec.fk_socpeople=" . $this->id;
  448. $sql.= " AND ec.fk_c_type_contact=tc.rowid";
  449. $sql.= " AND tc.source='external'";
  450. dol_syslog(get_class($this) . "::delete sql=" . $sql);
  451. $resql = $this->db->query($sql);
  452. if ($resql) {
  453. $num = $this->db->num_rows($resql);
  454. $i = 0;
  455. while ($i < $num && !$error) {
  456. $obj = $this->db->fetch_object($resql);
  457. $sqldel = "DELETE FROM " . MAIN_DB_PREFIX . "element_contact";
  458. $sqldel.=" WHERE rowid = " . $obj->rowid;
  459. dol_syslog(get_class($this) . "::delete sql=" . $sqldel);
  460. $result = $this->db->query($sqldel);
  461. if (!$result) {
  462. $error++;
  463. $this->error = $this->db->error() . ' sql=' . $sqldel;
  464. }
  465. $i++;
  466. }
  467. } else {
  468. $error++;
  469. $this->error = $this->db->error() . ' sql=' . $sql;
  470. }
  471. }
  472. if (!$error) {
  473. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "socpeople";
  474. $sql .= " WHERE rowid=" . $this->id;
  475. dol_syslog(get_class($this) . "::delete sql=" . $sql);
  476. $result = $this->db->query($sql);
  477. if (!$result) {
  478. $error++;
  479. $this->error = $this->db->error() . ' sql=' . $sql;
  480. }
  481. }
  482. if (!$error && !$notrigger) {
  483. // Appel des triggers
  484. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  485. $interface = new Interfaces($this->db);
  486. $result = $interface->run_triggers('CONTACT_DELETE', $this, $user, $langs, $conf);
  487. if ($result < 0) {
  488. $error++;
  489. $this->errors = $interface->errors;
  490. }
  491. // Fin appel triggers
  492. if ($error)
  493. $this->error = join(',', $this->errors);
  494. }
  495. if (!$error) {
  496. $this->db->commit();
  497. return 1;
  498. } else {
  499. $this->db->rollback();
  500. dol_syslog("Error " . $this->error, LOG_ERR);
  501. return -1;
  502. }
  503. }
  504. /**
  505. * Charge les informations sur le contact, depuis la base
  506. *
  507. * @param int $id Id du contact a charger
  508. * @return void
  509. */
  510. function info($id) {
  511. $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
  512. $sql.= " c.tms as tms, c.fk_user_modif";
  513. $sql.= " FROM " . MAIN_DB_PREFIX . "socpeople as c";
  514. $sql.= " WHERE c.rowid = " . $id;
  515. $resql = $this->db->query($sql);
  516. if ($resql) {
  517. if ($this->db->num_rows($resql)) {
  518. $obj = $this->db->fetch_object($resql);
  519. $this->id = $obj->rowid;
  520. if ($obj->fk_user_creat) {
  521. $cuser = new User($this->db);
  522. $cuser->fetch($obj->fk_user_creat);
  523. $this->user_creation = $cuser;
  524. }
  525. if ($obj->fk_user_modif) {
  526. $muser = new User($this->db);
  527. $muser->fetch($obj->fk_user_modif);
  528. $this->user_modification = $muser;
  529. }
  530. $this->date_creation = $this->db->jdate($obj->datec);
  531. $this->date_modification = $this->db->jdate($obj->tms);
  532. }
  533. $this->db->free($resql);
  534. } else {
  535. print $this->db->error();
  536. }
  537. }
  538. /**
  539. * Return number of mass Emailing received by this contacts with its email
  540. *
  541. * @return int Number of EMailings
  542. */
  543. function getNbOfEMailings() {
  544. $sql = "SELECT count(mc.email) as nb";
  545. $sql.= " FROM " . MAIN_DB_PREFIX . "mailing_cibles as mc";
  546. $sql.= " WHERE mc.email = '" . $this->db->escape($this->email) . "'";
  547. $sql.= " AND mc.statut NOT IN (-1,0)"; // -1 erreur, 0 non envoye, 1 envoye avec succes
  548. dol_syslog(get_class($this) . "::getNbOfEMailings sql=" . $sql, LOG_DEBUG);
  549. $resql = $this->db->query($sql);
  550. if ($resql) {
  551. $obj = $this->db->fetch_object($resql);
  552. $nb = $obj->nb;
  553. $this->db->free($resql);
  554. return $nb;
  555. } else {
  556. $this->error = $this->db->error();
  557. return -1;
  558. }
  559. }
  560. /**
  561. * Return name of contact with link (and eventually picto)
  562. * Use $this->id, $this->name, $this->firstname, this->civilite_id
  563. *
  564. * @param int $withpicto Include picto with link
  565. * @param string $option Where the link point to
  566. * @param int $maxlen Max length of
  567. * @return string String with URL
  568. */
  569. function getNomUrl($withpicto = 0, $option = '', $maxlen = 0) {
  570. global $langs;
  571. $result = '';
  572. $lien = '<a href="' . DOL_URL_ROOT . '/contact/fiche.php?id=' . $this->id . '">';
  573. $lienfin = '</a>';
  574. if ($option == 'xxx') {
  575. $lien = '<a href="' . DOL_URL_ROOT . '/contact/fiche.php?id=' . $this->id . '">';
  576. $lienfin = '</a>';
  577. }
  578. if ($withpicto)
  579. $result.=($lien . img_object($langs->trans("ShowContact") . ': ' . $this->getFullName($langs), 'contact') . $lienfin . ' ');
  580. $result.=$lien . ($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)) . $lienfin;
  581. return $result;
  582. }
  583. /**
  584. * Return full address of contact
  585. *
  586. * @param int $withcountry 1=Add country into address string
  587. * @param string $sep Separator to use to build string
  588. * @return string Full address string
  589. */
  590. function getFullAddress($withcountry = 0, $sep = "\n") {
  591. $ret = '';
  592. if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country))) {
  593. require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
  594. $tmparray = getCountry($this->country_id, 'all');
  595. $this->country_code = $tmparray['code'];
  596. $this->country = $tmparray['label'];
  597. }
  598. if (in_array($this->country_code, array('US'))) {
  599. $ret.=($this->address ? $this->address . $sep : '');
  600. $ret.=trim($this->zip . ' ' . $this->town);
  601. if ($withcountry)
  602. $ret.=($this->country ? $sep . $this->country : '');
  603. }
  604. else {
  605. $ret.=($this->address ? $this->address . $sep : '');
  606. $ret.=trim($this->zip . ' ' . $this->town);
  607. if ($withcountry)
  608. $ret.=($this->country ? $sep . $this->country : '');
  609. }
  610. return trim($ret);
  611. }
  612. /**
  613. * Renvoi le libelle d'un statut donne
  614. *
  615. * @param int $statut Id statut
  616. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  617. * @return string Libelle
  618. */
  619. /* function LibStatut($statut, $mode) {
  620. global $langs;
  621. if ($mode == 0) {
  622. if ($statut == 0)
  623. return $langs->trans('StatusContactDraft');
  624. elseif ($statut == 1)
  625. return $langs->trans('StatusContactValidated');
  626. elseif ($statut == 4)
  627. return $langs->trans('StatusContactValidated');
  628. elseif ($statut == 5)
  629. return $langs->trans('StatusContactValidated');
  630. }
  631. elseif ($mode == 1) {
  632. if ($statut == 0)
  633. return $langs->trans('StatusContactDraftShort');
  634. elseif ($statut == 1)
  635. return $langs->trans('StatusContactValidatedShort');
  636. elseif ($statut == 4)
  637. return $langs->trans('StatusContactValidatedShort');
  638. elseif ($statut == 5)
  639. return $langs->trans('StatusContactValidatedShort');
  640. }
  641. elseif ($mode == 2) {
  642. if ($statut == 0)
  643. return img_picto($langs->trans('StatusContactDraftShort'), 'statut0') . ' ' . $langs->trans('StatusContactDraft');
  644. elseif ($statut == 1)
  645. return img_picto($langs->trans('StatusContactValidatedShort'), 'statut1') . ' ' . $langs->trans('StatusContactValidated');
  646. elseif ($statut == 4)
  647. return img_picto($langs->trans('StatusContactValidatedShort'), 'statut4') . ' ' . $langs->trans('StatusContactValidated');
  648. elseif ($statut == 5)
  649. return img_picto($langs->trans('StatusContactValidatedShort'), 'statut5') . ' ' . $langs->trans('StatusContactValidated');
  650. }
  651. elseif ($mode == 3) {
  652. if ($statut == 0)
  653. return img_picto($langs->trans('StatusContactDraft'), 'statut0');
  654. elseif ($statut == 1)
  655. return img_picto($langs->trans('StatusContactValidated'), 'statut1');
  656. elseif ($statut == 4)
  657. return img_picto($langs->trans('StatusContactValidated'), 'statut4');
  658. elseif ($statut == 5)
  659. return img_picto($langs->trans('StatusContactValidated'), 'statut5');
  660. }
  661. elseif ($mode == 4) {
  662. if ($statut == 0)
  663. return img_picto($langs->trans('StatusContactDraft'), 'statut0') . ' ' . $langs->trans('StatusContactDraft');
  664. elseif ($statut == 1)
  665. return img_picto($langs->trans('StatusContactValidated'), 'statut1') . ' ' . $langs->trans('StatusContactValidated');
  666. elseif ($statut == 4)
  667. return img_picto($langs->trans('StatusContactValidated'), 'statut4') . ' ' . $langs->trans('StatusContactValidated');
  668. elseif ($statut == 5)
  669. return img_picto($langs->trans('StatusContactValidated'), 'statut5') . ' ' . $langs->trans('StatusContactValidated');
  670. }
  671. elseif ($mode == 5) {
  672. if ($statut == 0)
  673. return $langs->trans('StatusContactDraftShort') . ' ' . img_picto($langs->trans('StatusContactDraftShort'), 'statut0');
  674. elseif ($statut == 1)
  675. return $langs->trans('StatusContactValidatedShort') . ' ' . img_picto($langs->trans('StatusContactValidatedShort'), 'statut1');
  676. elseif ($statut == 4)
  677. return $langs->trans('StatusContactValidatedShort') . ' ' . img_picto($langs->trans('StatusContactValidatedShort'), 'statut4');
  678. elseif ($statut == 5)
  679. return $langs->trans('StatusContactValidatedShort') . ' ' . img_picto($langs->trans('StatusContactValidatedShort'), 'statut5');
  680. }
  681. } */
  682. /**
  683. * Initialise an instance with random values.
  684. * Used to build previews or test instances.
  685. * id must be 0 if object instance is a specimen.
  686. *
  687. * @return void
  688. */
  689. function initAsSpecimen() {
  690. global $user, $langs;
  691. // Charge tableau des id de societe socids
  692. $socids = array();
  693. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "societe LIMIT 10";
  694. $resql = $this->db->query($sql);
  695. if ($resql) {
  696. $num_socs = $this->db->num_rows($resql);
  697. $i = 0;
  698. while ($i < $num_socs) {
  699. $i++;
  700. $row = $this->db->fetch_row($resql);
  701. $socids[$i] = $row[0];
  702. }
  703. }
  704. // Initialise parameters
  705. $this->id = 0;
  706. $this->specimen = 1;
  707. $this->lastname = 'DOLIBARR';
  708. $this->firstname = 'SPECIMEN';
  709. $this->address = '61 jump street';
  710. $this->zip = '75000';
  711. $this->town = 'Paris';
  712. $this->country_id = 1;
  713. $this->country_code = 'FR';
  714. $this->country = 'France';
  715. $this->email = 'specimen@specimen.com';
  716. $socid = rand(1, $num_socs);
  717. $this->socid = $socids[$socid];
  718. }
  719. /**
  720. * Show html area for list of contacts
  721. *
  722. * @param Conf $conf Object conf
  723. * @param Translate $langs Object langs
  724. * @param DoliDB $db Database handler
  725. * @param Object $object Third party object
  726. * @param string $backtopage Url to go once contact is created
  727. * @return void
  728. */
  729. function show($max = 5, $id = 0) {
  730. global $langs, $conf, $user, $db, $bc;
  731. $titre = $langs->trans("ContactsForCompany");
  732. if ($user->rights->societe->contact->creer) {
  733. $h = 0;
  734. $head[$h] = new stdClass();
  735. $head[$h]->href = 'contact/fiche.php?action=create&socid=' . $id;
  736. $head[$h]->title = $langs->trans("NewContact");
  737. $head[$h]->icon = "icon-pencil";
  738. print start_box($titre, "icon-users", $head);
  739. }
  740. else
  741. print start_box($titre, "icon-users");
  742. $i = 0;
  743. $obj = new stdClass();
  744. $societe = new Societe($this->db);
  745. print '<table class="display dt_act" id="contacts_datatable" >';
  746. // Ligne des titres
  747. print '<thead>';
  748. print'<tr>';
  749. print'<th>';
  750. print'</th>';
  751. $obj->aoColumns[$i] = new stdClass();
  752. $obj->aoColumns[$i]->mDataProp = "_id";
  753. $obj->aoColumns[$i]->bUseRendered = false;
  754. $obj->aoColumns[$i]->bSearchable = false;
  755. $obj->aoColumns[$i]->bVisible = false;
  756. $i++;
  757. print'<th class="essential">';
  758. print $langs->trans("Name");
  759. print'</th>';
  760. $obj->aoColumns[$i] = new stdClass();
  761. $obj->aoColumns[$i]->mDataProp = "name";
  762. $obj->aoColumns[$i]->bUseRendered = false;
  763. $obj->aoColumns[$i]->bSearchable = true;
  764. $obj->aoColumns[$i]->fnRender = $this->datatablesFnRender("name", "url");
  765. $i++;
  766. print'<th class="essential">';
  767. print $langs->trans('PostOrFunction');
  768. print'</th>';
  769. $obj->aoColumns[$i] = new stdClass();
  770. $obj->aoColumns[$i]->mDataProp = "poste";
  771. $obj->aoColumns[$i]->sDefaultContent = "";
  772. $i++;
  773. print'<th class="essential">';
  774. print $langs->trans('Mobile');
  775. print'</th>';
  776. $obj->aoColumns[$i] = new stdClass();
  777. $obj->aoColumns[$i]->mDataProp = "phone_mobile";
  778. $obj->aoColumns[$i]->sDefaultContent = "";
  779. $i++;
  780. print'<th class="essential">';
  781. print $langs->trans('EMail');
  782. print'</th>';
  783. $obj->aoColumns[$i] = new stdClass();
  784. $obj->aoColumns[$i]->mDataProp = "email";
  785. $obj->aoColumns[$i]->sDefaultContent = "";
  786. $i++;
  787. print '</tr>';
  788. print '</thead>';
  789. print'<tfoot>';
  790. print'</tfoot>';
  791. print'<tbody>';
  792. print'</tbody>';
  793. print "</table>";
  794. $obj->iDisplayLength = $max;
  795. $obj->sAjaxSource = DOL_URL_ROOT . "/core/ajax/listdatatables.php?json=listSociete&class=" . get_class($this) . "&key=" . $id;
  796. $this->datatablesCreate($obj, "contacts_datatable", true);
  797. print end_box();
  798. }
  799. /* function show_contacts($conf, $langs, $db, $object, $backtopage = '') {
  800. global $user, $couch;
  801. global $bc;
  802. $i = -1;
  803. $titre = $langs->trans("ContactsForCompany");
  804. print start_box($titre, "twelve", "16-Users.png");
  805. $contactstatic = new Contact($db);
  806. if (!empty($conf->clicktodial->enabled)) {
  807. $user->fetch_clicktodial(); // lecture des infos de clicktodial
  808. }
  809. $buttoncreate = '';
  810. if ($user->rights->societe->contact->creer) {
  811. $addcontact = (!empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("AddContact") : $langs->trans("AddContactAddress"));
  812. $buttoncreate = '<a class="addnewrecord" href="' . DOL_URL_ROOT . '/contact/fiche.php?socid=' . $object->id . '&amp;action=create&amp;backtopage=' . urlencode($backtopage) . '">' . $addcontact . ' ' . img_picto($addcontact, 'filenew') . '</a>' . "\n";
  813. }
  814. print "\n";
  815. $title = (!empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("ContactsForCompany") : $langs->trans("ContactsAddressesForCompany"));
  816. //print_fiche_titre($title,$buttoncreate,'');
  817. print "\n" . '<table class="noborder" width="100%">' . "\n";
  818. print '<tr class="liste_titre"><td>' . $langs->trans("Name") . '</td>';
  819. print '<td>' . $langs->trans("Poste") . '</td><td>' . $langs->trans("Tel") . '</td>';
  820. print '<td>' . $langs->trans("Fax") . '</td><td>' . $langs->trans("EMail") . '</td>';
  821. print "<td>&nbsp;</td>";
  822. if (!empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create) {
  823. print '<td>&nbsp;</td>';
  824. }
  825. print "</tr>";
  826. $sql = "SELECT p.rowid, p.name, p.firstname, p.fk_pays, p.poste, p.phone, p.fax, p.email, p.note ";
  827. $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as p";
  828. $sql .= " WHERE p.fk_soc = " . $object->id;
  829. $sql .= " ORDER by p.datec";
  830. $result = $db->query($sql);
  831. $num = $db->num_rows($result);
  832. if ($num) {
  833. $i = 0;
  834. $var = true;
  835. while ($i < $num) {
  836. $obj = $db->fetch_object($result);
  837. $var = !$var;
  838. print "<tr " . $bc[$var] . ">";
  839. print '<td>';
  840. $contactstatic->id = $obj->rowid;
  841. $contactstatic->name = $obj->name;
  842. $contactstatic->firstname = $obj->firstname;
  843. print $contactstatic->getNomUrl(1);
  844. print '</td>';
  845. print '<td>' . $obj->poste . '</td>';
  846. $country_code = getCountry($obj->fk_pays, 2);
  847. // Lien click to dial
  848. print '<td>';
  849. print dol_print_phone($obj->phone, $country_code, $obj->rowid, $object->id, 'AC_TEL');
  850. print '</td>';
  851. print '<td>';
  852. print dol_print_phone($obj->fax, $country_code, $obj->rowid, $object->id, 'AC_FAX');
  853. print '</td>';
  854. print '<td>';
  855. print dol_print_email($obj->email, $obj->rowid, $object->id, 'AC_EMAIL');
  856. print '</td>';
  857. if (!empty($conf->agenda->enabled) && $user->rights->agenda->myactions->create) {
  858. print '<td align="center"><a href="' . DOL_URL_ROOT . '/comm/action/fiche.php?action=create&actioncode=AC_RDV&contactid=' . $obj->rowid . '&socid=' . $object->id . '&backtopage=' . urlencode($backtopage) . '">';
  859. print img_object($langs->trans("Rendez-Vous"), "action");
  860. print '</a></td>';
  861. }
  862. if ($user->rights->societe->contact->creer) {
  863. print '<td align="right">';
  864. print '<a href="' . DOL_URL_ROOT . '/contact/fiche.php?action=edit&amp;id=' . $obj->rowid . '&amp;backtopage=' . urlencode($backtopage) . '">';
  865. print img_edit();
  866. print '</a></td>';
  867. }
  868. print "</tr>\n";
  869. $i++;
  870. }
  871. } else {
  872. //print "<tr ".$bc[$var].">";
  873. //print '<td>'.$langs->trans("NoContactsYetDefined").'</td>';
  874. //print "</tr>\n";
  875. }
  876. print "\n</table>\n";
  877. print end_box();
  878. return $i;
  879. } */
  880. }
  881. ?>