PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/asterix14/dolibarr
PHP | 1046 lines | 736 code | 129 blank | 181 comment | 139 complexity | c5b3cca17f80fdabe68bf251a46cefcb MD5 | raw file
Possible License(s): LGPL-2.0
  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-2008 Regis Houssin <regis@dolibarr.fr>
  6. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerker@telenet.be>
  7. * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  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 2 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 <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/contact/class/contact.class.php
  24. * \ingroup societe
  25. * \brief File of contacts class
  26. */
  27. require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php");
  28. /**
  29. * \class Contact
  30. * \brief Classe permettant la gestion des contacts
  31. */
  32. class Contact extends CommonObject
  33. {
  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 $nom; // TODO deprecated
  41. var $firstname;
  42. var $prenom; // TODO deprecated
  43. var $address;
  44. var $cp; // TODO deprecated
  45. var $zip;
  46. var $ville; // TODO deprecated
  47. var $town;
  48. var $fk_departement; // Id of department
  49. var $departement_code; // Code of department
  50. var $departement; // Label of department
  51. var $fk_pays; // Id of country
  52. var $pays_code; // Code of country
  53. var $pays; // Label of country
  54. var $socid; // fk_soc
  55. var $status; // 0=brouillon, 1=4=actif, 5=inactif
  56. var $code;
  57. var $email;
  58. var $birthday;
  59. var $default_lang;
  60. var $ref_facturation; // Nb de reference facture pour lequel il est contact
  61. var $ref_contrat; // Nb de reference contrat pour lequel il est contact
  62. var $ref_commande; // Nb de reference commande pour lequel il est contact
  63. var $ref_propal; // Nb de reference propal pour lequel il est contact
  64. var $user_id;
  65. var $user_login;
  66. var $oldcopy; // To contains a clone of this when we need to save old properties of object
  67. /**
  68. * Constructor
  69. *
  70. * @param DoliDB $DB Database handler
  71. */
  72. function Contact($DB)
  73. {
  74. $this->db = $DB;
  75. }
  76. /**
  77. * Add a contact into database
  78. *
  79. * @param User $user Object user that create
  80. * @return int <0 if KO, >0 if OK
  81. */
  82. function create($user)
  83. {
  84. global $conf, $langs;
  85. $error=0;
  86. $now=dol_now();
  87. $this->db->begin();
  88. // Clean parameters
  89. $this->name=trim($this->name);
  90. $this->firstname=trim($this->firstname);
  91. if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->name=ucwords($this->name);
  92. if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname=ucwords($this->firstname);
  93. if (! $this->socid) $this->socid = 0;
  94. if (! $this->priv) $this->priv = 0;
  95. $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
  96. $sql.= " datec";
  97. $sql.= ", fk_soc";
  98. $sql.= ", name";
  99. $sql.= ", firstname";
  100. $sql.= ", fk_user_creat";
  101. $sql.= ", priv";
  102. $sql.= ", canvas";
  103. $sql.= ", entity";
  104. $sql.= ") VALUES (";
  105. $sql.= "'".$this->db->idate($now)."',";
  106. if ($this->socid > 0) $sql.= " ".$this->socid.",";
  107. else $sql.= "null,";
  108. $sql.= "'".$this->db->escape($this->name)."',";
  109. $sql.= "'".$this->db->escape($this->firstname)."',";
  110. $sql.= " ".($user->id > 0 ? "'".$user->id."'":"null").",";
  111. $sql.= " ".$this->priv.",";
  112. $sql.= " ".($this->canvas?"'".$this->canvas."'":"null").",";
  113. $sql.= " ".$conf->entity;
  114. $sql.= ")";
  115. dol_syslog("Contact::create sql=".$sql);
  116. $resql=$this->db->query($sql);
  117. if ($resql)
  118. {
  119. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
  120. if (! $error)
  121. {
  122. $result=$this->update($this->id, $user, 1);
  123. if ($result < 0)
  124. {
  125. $error++;
  126. $this->error=$this->db->lasterror();
  127. }
  128. }
  129. if (! $error)
  130. {
  131. $result=$this->update_perso($this->id, $user);
  132. if ($result < 0)
  133. {
  134. $error++;
  135. $this->error=$this->db->lasterror();
  136. }
  137. }
  138. if (! $error)
  139. {
  140. // Appel des triggers
  141. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  142. $interface=new Interfaces($this->db);
  143. $result=$interface->run_triggers('CONTACT_CREATE',$this,$user,$langs,$conf);
  144. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  145. // Fin appel triggers
  146. }
  147. if (! $error)
  148. {
  149. $this->db->commit();
  150. return $this->id;
  151. }
  152. else
  153. {
  154. $this->db->rollback();
  155. dol_syslog("Contact::create ".$this->error, LOG_ERR);
  156. return -2;
  157. }
  158. }
  159. else
  160. {
  161. $this->error=$this->db->lasterror();
  162. $this->db->rollback();
  163. dol_syslog("Contact::create ".$this->error, LOG_ERR);
  164. return -1;
  165. }
  166. }
  167. /**
  168. * Update informations into database
  169. *
  170. * @param int $id Id of contact/address to update
  171. * @param User $user Objet user making change
  172. * @param int $notrigger 0=no, 1=yesi
  173. * @return int <0 if KO, >0 if OK
  174. */
  175. function update($id, $user=0, $notrigger=0)
  176. {
  177. global $conf, $langs;
  178. $error=0;
  179. $this->id = $id;
  180. // Nettoyage parametres
  181. $this->name=trim($this->name);
  182. $this->firstname=trim($this->firstname);
  183. $this->email=trim($this->email);
  184. $this->phone_pro=trim($this->phone_pro);
  185. $this->phone_perso=trim($this->phone_perso);
  186. $this->phone_mobile=trim($this->phone_mobile);
  187. $this->fax=trim($this->fax);
  188. $this->db->begin();
  189. $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET ";
  190. if ($this->socid > 0) $sql .= " fk_soc='".$this->db->escape($this->socid)."',";
  191. if ($this->socid == -1) $sql .= " fk_soc=null,";
  192. $sql .= " civilite='".$this->db->escape($this->civilite_id)."'";
  193. $sql .= ", name='".$this->db->escape($this->name)."'";
  194. $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
  195. $sql .= ", address='".$this->db->escape($this->address)."'";
  196. $sql .= ", cp='".$this->db->escape($this->zip)."'";
  197. $sql .= ", ville='".$this->db->escape($this->town)."'";
  198. $sql .= ", fk_pays=".($this->fk_pays>0?$this->fk_pays:'NULL');
  199. $sql .= ", fk_departement=".($this->fk_departement>0?$this->fk_departement:'NULL');
  200. $sql .= ", poste='".$this->db->escape($this->poste)."'";
  201. $sql .= ", fax='".$this->db->escape($this->fax)."'";
  202. $sql .= ", email='".$this->db->escape($this->email)."'";
  203. $sql .= ", note='".$this->db->escape($this->note)."'";
  204. $sql .= ", phone = '".$this->db->escape($this->phone_pro)."'";
  205. $sql .= ", phone_perso = '".$this->db->escape($this->phone_perso)."'";
  206. $sql .= ", phone_mobile = '".$this->db->escape($this->phone_mobile)."'";
  207. $sql .= ", jabberid = '".$this->db->escape($this->jabberid)."'";
  208. $sql .= ", priv = '".$this->priv."'";
  209. $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$user->id."'":"null");
  210. $sql .= ", default_lang=".($this->default_lang?"'".$this->default_lang."'":"null");
  211. $sql .= " WHERE rowid=".$id;
  212. dol_syslog("Contact::update sql=".$sql,LOG_DEBUG);
  213. $result = $this->db->query($sql);
  214. if ($result)
  215. {
  216. if (! $error && ! $notrigger)
  217. {
  218. // Appel des triggers
  219. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  220. $interface=new Interfaces($this->db);
  221. $result=$interface->run_triggers('CONTACT_MODIFY',$this,$user,$langs,$conf);
  222. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  223. // Fin appel triggers
  224. }
  225. if (! $error)
  226. {
  227. $this->db->commit();
  228. return 1;
  229. }
  230. else
  231. {
  232. $this->error=join(',',$this->errors);
  233. dol_syslog("Contact::update Error ".$this->error,LOG_ERR);
  234. $this->db->rollback();
  235. return -$error;
  236. }
  237. }
  238. else
  239. {
  240. $this->error=$this->db->lasterror().' sql='.$sql;
  241. dol_syslog("Contact::update Error ".$this->error,LOG_ERR);
  242. $this->db->rollback();
  243. return -1;
  244. }
  245. }
  246. /**
  247. * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
  248. *
  249. * @param array $info Info string loaded by _load_ldap_info
  250. * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
  251. * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
  252. * 2=Return key only (uid=qqq)
  253. * @return string DN
  254. */
  255. function _load_ldap_dn($info,$mode=0)
  256. {
  257. global $conf;
  258. $dn='';
  259. if ($mode==0) $dn=$conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;
  260. if ($mode==1) $dn=$conf->global->LDAP_CONTACT_DN;
  261. if ($mode==2) $dn=$conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS];
  262. return $dn;
  263. }
  264. /**
  265. * Initialise tableau info (tableau des attributs LDAP)
  266. *
  267. * @return array Tableau info des attributs
  268. */
  269. function _load_ldap_info()
  270. {
  271. global $conf,$langs;
  272. // Object classes
  273. $info["objectclass"]=explode(',',$conf->global->LDAP_CONTACT_OBJECT_CLASS);
  274. $this->fullname=$this->getFullName($langs);
  275. // Fields
  276. if ($this->fullname && $conf->global->LDAP_CONTACT_FIELD_FULLNAME) $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
  277. if ($this->name && $conf->global->LDAP_CONTACT_FIELD_NAME) $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->name;
  278. if ($this->firstname && $conf->global->LDAP_CONTACT_FIELD_FIRSTNAME) $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
  279. if ($this->poste) $info["title"] = $this->poste;
  280. if ($this->socid > 0)
  281. {
  282. $soc = new Societe($this->db);
  283. $soc->fetch($this->socid);
  284. $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->nom;
  285. if ($soc->client == 1) $info["businessCategory"] = "Customers";
  286. if ($soc->client == 2) $info["businessCategory"] = "Prospects";
  287. if ($soc->fournisseur == 1) $info["businessCategory"] = "Suppliers";
  288. }
  289. if ($this->address && $conf->global->LDAP_CONTACT_FIELD_ADDRESS) $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
  290. if ($this->cp && $conf->global->LDAP_CONTACT_FIELD_ZIP) $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->cp;
  291. if ($this->ville && $conf->global->LDAP_CONTACT_FIELD_TOWN) $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->ville;
  292. if ($this->pays_code && $conf->global->LDAP_CONTACT_FIELD_COUNTRY) $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->pays_code;
  293. if ($this->phone_pro && $conf->global->LDAP_CONTACT_FIELD_PHONE) $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
  294. if ($this->phone_perso && $conf->global->LDAP_CONTACT_FIELD_HOMEPHONE) $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
  295. if ($this->phone_mobile && $conf->global->LDAP_CONTACT_FIELD_MOBILE) $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
  296. if ($this->fax && $conf->global->LDAP_CONTACT_FIELD_FAX) $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
  297. if ($this->note && $conf->global->LDAP_CONTACT_FIELD_DESCRIPTION) $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = $this->note;
  298. if ($this->email && $conf->global->LDAP_CONTACT_FIELD_MAIL) $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
  299. if ($conf->global->LDAP_SERVER_TYPE == 'egroupware')
  300. {
  301. $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
  302. $info['uidnumber'] = $this->id;
  303. $info['phpgwTz'] = 0;
  304. $info['phpgwMailType'] = 'INTERNET';
  305. $info['phpgwMailHomeType'] = 'INTERNET';
  306. $info["phpgwContactTypeId"] = 'n';
  307. $info["phpgwContactCatId"] = 0;
  308. $info["phpgwContactAccess"] = "public";
  309. if (dol_strlen($this->egroupware_id) == 0)
  310. {
  311. $this->egroupware_id = 1;
  312. }
  313. $info["phpgwContactOwner"] = $this->egroupware_id;
  314. if ($this->email) $info["rfc822Mailbox"] = $this->email;
  315. if ($this->phone_mobile) $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
  316. }
  317. return $info;
  318. }
  319. /**
  320. * Update field alert birthday
  321. *
  322. * @param int $id Id of contact
  323. * @param User $user User asking to change alert or birthday
  324. * @return int <0 if KO, >=0 if OK
  325. */
  326. function update_perso($id, $user=0)
  327. {
  328. $error=0;
  329. $result=false;
  330. // Mis a jour contact
  331. $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET rowid=".$id;
  332. $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
  333. if ($user) $sql .= ", fk_user_modif=".$user->id;
  334. $sql .= " WHERE rowid=".$id;
  335. //print "update_perso: ".$this->birthday.'-'.$this->db->idate($this->birthday);
  336. dol_syslog("Contact::update_perso this->birthday=".$this->birthday." - sql=".$sql);
  337. $resql = $this->db->query($sql);
  338. if (! $resql)
  339. {
  340. $error++;
  341. $this->error=$this->db->lasterror();
  342. }
  343. // Mis a jour alerte birthday
  344. if ($this->birthday_alert)
  345. {
  346. //check existing
  347. $sql_check = "SELECT * FROM ".MAIN_DB_PREFIX."user_alert WHERE type=1 AND fk_contact=".$id." AND fk_user=".$user->id;
  348. $result_check = $this->db->query($sql_check);
  349. if (!$result_check or ($this->db->num_rows($result_check)<1))
  350. {
  351. //insert
  352. $sql = "INSERT into ".MAIN_DB_PREFIX."user_alert(type,fk_contact,fk_user) ";
  353. $sql.= "values (1,".$id.",".$user->id.")";
  354. $result = $this->db->query($sql);
  355. if (!$result)
  356. {
  357. $error++;
  358. $this->error=$this->db->lasterror();
  359. }
  360. }
  361. else
  362. {
  363. $result = true;
  364. }
  365. }
  366. else
  367. {
  368. $sql = "DELETE from ".MAIN_DB_PREFIX."user_alert ";
  369. $sql.= "where type=1 AND fk_contact=".$id." AND fk_user=".$user->id;
  370. $result = $this->db->query($sql);
  371. if (! $result)
  372. {
  373. $error++;
  374. $this->error=$this->db->lasterror();
  375. }
  376. }
  377. return $result;
  378. }
  379. /**
  380. * Charge l'objet contact
  381. *
  382. * @param int $id id du contact
  383. * @param User $user Utilisateur (abonnes aux alertes) qui veut les alertes de ce contact
  384. * @return int -1 if KO, 0 if OK but not found, 1 if OK
  385. */
  386. function fetch($id, $user=0)
  387. {
  388. global $langs;
  389. $langs->load("companies");
  390. $sql = "SELECT c.rowid, c.fk_soc, c.civilite as civilite_id, c.name, c.firstname,";
  391. $sql.= " c.address, c.cp, c.ville,";
  392. $sql.= " c.fk_pays,";
  393. $sql.= " c.fk_departement,";
  394. $sql.= " c.birthday,";
  395. $sql.= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid,";
  396. $sql.= " c.priv, c.note, c.default_lang, c.canvas,";
  397. $sql.= " p.libelle as pays, p.code as pays_code,";
  398. $sql.= " d.nom as departement, d.code_departement as departement_code,";
  399. $sql.= " u.rowid as user_id, u.login as user_login,";
  400. $sql.= " s.nom as socname, s.address as socaddress, s.cp as soccp, s.ville as soccity, s.default_lang as socdefault_lang";
  401. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  402. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_pays as p ON c.fk_pays = p.rowid";
  403. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
  404. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
  405. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
  406. $sql.= " WHERE c.rowid = ". $id;
  407. dol_syslog("Contact::fetch sql=".$sql);
  408. $resql=$this->db->query($sql);
  409. if ($resql)
  410. {
  411. if ($this->db->num_rows($resql))
  412. {
  413. $obj = $this->db->fetch_object($resql);
  414. $this->id = $obj->rowid;
  415. $this->ref = $obj->rowid;
  416. $this->civilite_id = $obj->civilite_id;
  417. $this->name = $obj->name;
  418. $this->firstname = $obj->firstname;
  419. $this->nom = $obj->name; // TODO deprecated
  420. $this->prenom = $obj->firstname; // TODO deprecated
  421. $this->address = $obj->address;
  422. $this->adresse = $obj->address; // TODO deprecated
  423. $this->cp = $obj->cp; // TODO deprecated
  424. $this->zip = $obj->cp;
  425. $this->ville = $obj->ville; // TODO deprecated
  426. $this->town = $obj->ville;
  427. $this->fk_departement = $obj->fk_departement;
  428. $this->departement_code = $obj->departement_code;
  429. $this->departement = $obj->departement; // TODO deprecated
  430. $this->state = $obj->departement;
  431. $this->fk_pays = $obj->fk_pays;
  432. $this->pays_code = $obj->fk_pays?$obj->pays_code:'';
  433. $this->pays = ($obj->fk_pays > 0)?$langs->transnoentitiesnoconv("Country".$obj->pays_code):'';
  434. $this->country = ($obj->fk_pays > 0)?$langs->transnoentitiesnoconv("Country".$obj->pays_code):'';
  435. $this->socid = $obj->fk_soc;
  436. $this->socname = $obj->socname;
  437. $this->poste = $obj->poste;
  438. $this->phone_pro = trim($obj->phone);
  439. $this->fax = trim($obj->fax);
  440. $this->phone_perso = trim($obj->phone_perso);
  441. $this->phone_mobile = trim($obj->phone_mobile);
  442. $this->email = $obj->email;
  443. $this->jabberid = $obj->jabberid;
  444. $this->priv = $obj->priv;
  445. $this->mail = $obj->email;
  446. $this->birthday = dol_stringtotime($obj->birthday);
  447. //print "fetch: ".$obj->birthday.'-'.$this->birthday;
  448. $this->birthday_alert = $obj->birthday_alert;
  449. $this->note = $obj->note;
  450. $this->default_lang = $obj->default_lang;
  451. $this->user_id = $obj->user_id;
  452. $this->user_login = $obj->user_login;
  453. $this->canvas = $obj->canvas;
  454. // Recherche le user Dolibarr lie a ce contact
  455. $sql = "SELECT u.rowid ";
  456. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  457. $sql .= " WHERE u.fk_socpeople = ". $this->id;
  458. $resql=$this->db->query($sql);
  459. if ($resql)
  460. {
  461. if ($this->db->num_rows($resql))
  462. {
  463. $uobj = $this->db->fetch_object($resql);
  464. $this->user_id = $uobj->rowid;
  465. }
  466. $this->db->free($resql);
  467. }
  468. else
  469. {
  470. $this->error=$this->db->error();
  471. dol_syslog("Contact::fetch ".$this->error, LOG_ERR);
  472. return -1;
  473. }
  474. // Charge alertes du user
  475. if ($user)
  476. {
  477. $sql = "SELECT fk_user";
  478. $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
  479. $sql .= " WHERE fk_user = ".$user->id." AND fk_contact = ".$id;
  480. $resql=$this->db->query($sql);
  481. if ($resql)
  482. {
  483. if ($this->db->num_rows($resql))
  484. {
  485. $obj = $this->db->fetch_object($resql);
  486. $this->birthday_alert = 1;
  487. }
  488. $this->db->free($resql);
  489. }
  490. else
  491. {
  492. $this->error=$this->db->error();
  493. dol_syslog("Contact::fetch ".$this->error, LOG_ERR);
  494. return -1;
  495. }
  496. }
  497. return 1;
  498. }
  499. else
  500. {
  501. $this->error=$langs->trans("RecordNotFound");
  502. return 0;
  503. }
  504. }
  505. else
  506. {
  507. $this->error=$this->db->error();
  508. dol_syslog("Contact::fetch ".$this->error, LOG_ERR);
  509. return -1;
  510. }
  511. }
  512. /**
  513. * Charge le nombre d'elements auquel est lie ce contact
  514. * ref_facturation
  515. * ref_contrat
  516. * ref_commande
  517. * ref_propale
  518. *
  519. * @return int <0 if KO, >=0 if OK
  520. */
  521. function load_ref_elements()
  522. {
  523. // Compte les elements pour lesquels il est contact
  524. $sql ="SELECT tc.element, count(ec.rowid) as nb";
  525. $sql.=" FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
  526. $sql.=" WHERE ec.fk_c_type_contact = tc.rowid";
  527. $sql.=" AND fk_socpeople = ". $this->id;
  528. $sql.=" GROUP BY tc.element";
  529. dol_syslog("Contact::load_ref_elements sql=".$sql);
  530. $resql=$this->db->query($sql);
  531. if ($resql)
  532. {
  533. while($obj=$this->db->fetch_object($resql))
  534. {
  535. if ($obj->nb)
  536. {
  537. if ($obj->element=='facture') $this->ref_facturation = $obj->nb;
  538. if ($obj->element=='contrat') $this->ref_contrat = $obj->nb;
  539. if ($obj->element=='commande') $this->ref_commande = $obj->nb;
  540. if ($obj->element=='propal') $this->ref_propal = $obj->nb;
  541. }
  542. }
  543. $this->db->free($resql);
  544. return 0;
  545. }
  546. else
  547. {
  548. $this->error=$this->db->error()." - ".$sql;
  549. dol_syslog("Contact::load_ref_elements Error ".$this->error, LOG_ERR);
  550. return -1;
  551. }
  552. }
  553. /**
  554. * Efface le contact de la base
  555. *
  556. * @param int $notrigger Disable all trigger
  557. * @return int <0 if KO, >0 if OK
  558. */
  559. function delete($notrigger=0)
  560. {
  561. global $conf, $langs, $user;
  562. $error=0;
  563. $this->old_name = $obj->name;
  564. $this->old_firstname = $obj->firstname;
  565. $this->db->begin();
  566. if (! $error)
  567. {
  568. // Get all rowid of element_contact linked to a type that is link to llx_socpeople
  569. $sql = "SELECT ec.rowid";
  570. $sql.= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
  571. $sql.= " ".MAIN_DB_PREFIX."c_type_contact tc";
  572. $sql.= " WHERE ec.fk_socpeople=".$this->id;
  573. $sql.= " AND ec.fk_c_type_contact=tc.rowid";
  574. $sql.= " AND tc.source='external'";
  575. dol_syslog("Contact::delete sql=".$sql);
  576. $resql = $this->db->query($sql);
  577. if ($resql)
  578. {
  579. $num=$this->db->num_rows($resql);
  580. $i=0;
  581. while ($i < $num && ! $error)
  582. {
  583. $obj = $this->db->fetch_object($resql);
  584. $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
  585. $sqldel.=" WHERE rowid = ".$obj->rowid;
  586. dol_syslog("Contact::delete sql=".$sqldel);
  587. $result = $this->db->query($sqldel);
  588. if (! $result)
  589. {
  590. $error++;
  591. $this->error=$this->db->error().' sql='.$sqldel;
  592. }
  593. $i++;
  594. }
  595. }
  596. else
  597. {
  598. $error++;
  599. $this->error=$this->db->error().' sql='.$sql;
  600. }
  601. }
  602. if (! $error)
  603. {
  604. $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
  605. $sql .= " WHERE rowid=".$this->id;
  606. dol_syslog("Contact::delete sql=".$sql);
  607. $result = $this->db->query($sql);
  608. if (! $result)
  609. {
  610. $error++;
  611. $this->error=$this->db->error().' sql='.$sql;
  612. }
  613. }
  614. if (! $error && ! $notrigger)
  615. {
  616. // Appel des triggers
  617. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  618. $interface=new Interfaces($this->db);
  619. $result=$interface->run_triggers('CONTACT_DELETE',$this,$user,$langs,$conf);
  620. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  621. // Fin appel triggers
  622. if ($error) $this->error=join(',',$this->errors);
  623. }
  624. if (! $error)
  625. {
  626. $this->db->commit();
  627. return 1;
  628. }
  629. else
  630. {
  631. $this->db->rollback();
  632. return -1;
  633. }
  634. }
  635. /**
  636. * Charge les informations sur le contact, depuis la base
  637. *
  638. * @param int $id Id du contact a charger
  639. * @return void
  640. */
  641. function info($id)
  642. {
  643. $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
  644. $sql.= " c.tms as tms, c.fk_user_modif";
  645. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  646. $sql.= " WHERE c.rowid = ".$id;
  647. $resql=$this->db->query($sql);
  648. if ($resql)
  649. {
  650. if ($this->db->num_rows($resql))
  651. {
  652. $obj = $this->db->fetch_object($resql);
  653. $this->id = $obj->rowid;
  654. if ($obj->fk_user_creat) {
  655. $cuser = new User($this->db);
  656. $cuser->fetch($obj->fk_user_creat);
  657. $this->user_creation = $cuser;
  658. }
  659. if ($obj->fk_user_modif) {
  660. $muser = new User($this->db);
  661. $muser->fetch($obj->fk_user_modif);
  662. $this->user_modification = $muser;
  663. }
  664. $this->date_creation = $this->db->jdate($obj->datec);
  665. $this->date_modification = $this->db->jdate($obj->tms);
  666. }
  667. $this->db->free($resql);
  668. }
  669. else
  670. {
  671. print $this->db->error();
  672. }
  673. }
  674. /**
  675. * Return number of mass Emailing received by this contacts with its email
  676. *
  677. * @return int Number of EMailings
  678. */
  679. function getNbOfEMailings()
  680. {
  681. $sql = "SELECT count(mc.email) as nb";
  682. $sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  683. $sql.= " WHERE mc.email = '".$this->db->escape($this->email)."'";
  684. $sql.= " AND mc.statut=1"; // -1 erreur, 0 non envoye, 1 envoye avec succes
  685. $resql=$this->db->query($sql);
  686. if ($resql)
  687. {
  688. $obj = $this->db->fetch_object($resql);
  689. $nb=$obj->nb;
  690. $this->db->free($resql);
  691. return $nb;
  692. }
  693. else
  694. {
  695. $this->error=$this->db->error();
  696. return -1;
  697. }
  698. }
  699. /**
  700. * Return name of contact with link (and eventually picto)
  701. * Use $this->id, $this->name, $this->firstname, this->civilite_id
  702. *
  703. * @param int $withpicto Include picto with link
  704. * @param string $option Where the link point to
  705. * @param int $maxlen Max length of
  706. * @return string String with URL
  707. */
  708. function getNomUrl($withpicto=0,$option='',$maxlen=0)
  709. {
  710. global $langs;
  711. $result='';
  712. $lien = '<a href="'.DOL_URL_ROOT.'/contact/fiche.php?id='.$this->id.'">';
  713. $lienfin='</a>';
  714. if ($option == 'xxx')
  715. {
  716. $lien = '<a href="'.DOL_URL_ROOT.'/contact/fiche.php?id='.$this->id.'">';
  717. $lienfin='</a>';
  718. }
  719. if ($withpicto) $result.=($lien.img_object($langs->trans("ShowContact").': '.$this->getFullName($langs),'contact').$lienfin.' ');
  720. $result.=$lien.($maxlen?dol_trunc($this->getFullName($langs),$maxlen):$this->getFullName($langs)).$lienfin;
  721. return $result;
  722. }
  723. /**
  724. * Return full address of contact
  725. *
  726. * @param int $withcountry 1=Add country into address string
  727. * @param string $sep Separator to use to build string
  728. * @return string Full address string
  729. */
  730. function getFullAddress($withcountry=0,$sep="\n")
  731. {
  732. $ret='';
  733. if (in_array($this->country,array('us')))
  734. {
  735. $ret.=($this->address?$this->address.$sep:'');
  736. $ret.=trim($this->zip.' '.$this->town);
  737. if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
  738. }
  739. else
  740. {
  741. $ret.=($this->address?$this->address.$sep:'');
  742. $ret.=trim($this->zip.' '.$this->town);
  743. if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
  744. }
  745. return trim($ret);
  746. }
  747. /**
  748. * Return label of a civility contact
  749. *
  750. * @return string Translated name of civility
  751. */
  752. function getCivilityLabel()
  753. {
  754. global $langs;
  755. $langs->load("dict");
  756. $code=$this->civilite_id;
  757. return $langs->trans("Civility".$code)!="Civility".$code ? $langs->trans("Civility".$code) : '';
  758. if (empty($ret))
  759. {
  760. $ret=$code;
  761. $langs->getLabelFromKey($db,$reg[1],'c_civilite','code','civilite');
  762. //$ret=dol_getIdFromCode($this->db,$code,'c_civilite',
  763. }
  764. return $ret;
  765. }
  766. /**
  767. * Return full name (civility+' '+name+' '+lastname)
  768. *
  769. * @param Translate $langs Language object for translation of civility
  770. * @param string $option 0=No option, 1=Add civility
  771. * @param int $nameorder -1=Auto, 0=Lastname+Firstname, 1=Firstname+Lastname
  772. * @return string String with full name
  773. */
  774. function getFullName($langs,$option=0,$nameorder=-1)
  775. {
  776. global $conf;
  777. $ret='';
  778. if ($option && $this->civilite_id)
  779. {
  780. if ($langs->transnoentitiesnoconv("Civility".$this->civilite_id)!="Civility".$this->civilite_id) $ret.=$langs->transnoentitiesnoconv("Civility".$this->civilite_id).' ';
  781. else $ret.=$this->civilite_id.' ';
  782. }
  783. // If order not defined, we use the setup
  784. if ($nameorder < 0) $nameorder=(! $conf->global->MAIN_FIRSTNAME_NAME_POSITION);
  785. if ($nameorder)
  786. {
  787. if ($this->firstname) $ret.=$this->firstname;
  788. if ($this->firstname && $this->name) $ret.=' ';
  789. if ($this->name) $ret.=$this->name;
  790. }
  791. else
  792. {
  793. if ($this->name) $ret.=$this->name;
  794. if ($this->firstname && $this->name) $ret.=' ';
  795. if ($this->firstname) $ret.=$this->firstname;
  796. }
  797. return trim($ret);
  798. }
  799. /**
  800. * Retourne le libelle du statut du contact
  801. *
  802. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  803. * @return string Libelle
  804. */
  805. function getLibStatut($mode)
  806. {
  807. return $this->LibStatut($this->status,$mode);
  808. }
  809. /**
  810. * Renvoi le libelle d'un statut donne
  811. *
  812. * @param int $statut Id statut
  813. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  814. * @return string Libelle
  815. */
  816. function LibStatut($statut, $mode)
  817. {
  818. global $langs;
  819. if ($mode == 0)
  820. {
  821. if ($statut==0) return $langs->trans('StatusContactDraft');
  822. elseif ($statut==1) return $langs->trans('StatusContactValidated');
  823. elseif ($statut==4) return $langs->trans('StatusContactValidated');
  824. elseif ($statut==5) return $langs->trans('StatusContactValidated');
  825. }
  826. elseif ($mode == 1)
  827. {
  828. if ($statut==0) return $langs->trans('StatusContactDraftShort');
  829. elseif ($statut==1) return $langs->trans('StatusContactValidatedShort');
  830. elseif ($statut==4) return $langs->trans('StatusContactValidatedShort');
  831. elseif ($statut==5) return $langs->trans('StatusContactValidatedShort');
  832. }
  833. elseif ($mode == 2)
  834. {
  835. if ($statut==0) return img_picto($langs->trans('StatusContactDraftShort'),'statut0').' '.$langs->trans('StatusContactDraft');
  836. elseif ($statut==1) return img_picto($langs->trans('StatusContactValidatedShort'),'statut1').' '.$langs->trans('StatusContactValidated');
  837. elseif ($statut==4) return img_picto($langs->trans('StatusContactValidatedShort'),'statut4').' '.$langs->trans('StatusContactValidated');
  838. elseif ($statut==5) return img_picto($langs->trans('StatusContactValidatedShort'),'statut5').' '.$langs->trans('StatusContactValidated');
  839. }
  840. elseif ($mode == 3)
  841. {
  842. if ($statut==0) return img_picto($langs->trans('StatusContactDraft'),'statut0');
  843. elseif ($statut==1) return img_picto($langs->trans('StatusContactValidated'),'statut1');
  844. elseif ($statut==4) return img_picto($langs->trans('StatusContactValidated'),'statut4');
  845. elseif ($statut==5) return img_picto($langs->trans('StatusContactValidated'),'statut5');
  846. }
  847. elseif ($mode == 4)
  848. {
  849. if ($statut==0) return img_picto($langs->trans('StatusContactDraft'),'statut0').' '.$langs->trans('StatusContactDraft');
  850. elseif ($statut==1) return img_picto($langs->trans('StatusContactValidated'),'statut1').' '.$langs->trans('StatusContactValidated');
  851. elseif ($statut==4) return img_picto($langs->trans('StatusContactValidated'),'statut4').' '.$langs->trans('StatusContactValidated');
  852. elseif ($statut==5) return img_picto($langs->trans('StatusContactValidated'),'statut5').' '.$langs->trans('StatusContactValidated');
  853. }
  854. elseif ($mode == 5)
  855. {
  856. if ($statut==0) return $langs->trans('StatusContactDraftShort').' '.img_picto($langs->trans('StatusContactDraftShort'),'statut0');
  857. elseif ($statut==1) return $langs->trans('StatusContactValidatedShort').' '.img_picto($langs->trans('StatusContactValidatedShort'),'statut1');
  858. elseif ($statut==4) return $langs->trans('StatusContactValidatedShort').' '.img_picto($langs->trans('StatusContactValidatedShort'),'statut4');
  859. elseif ($statut==5) return $langs->trans('StatusContactValidatedShort').' '.img_picto($langs->trans('StatusContactValidatedShort'),'statut5');
  860. }
  861. }
  862. /**
  863. * Return translated label of Public or Private
  864. *
  865. * @param int $statut Type (0 = public, 1 = private)
  866. * @return string Label translated
  867. */
  868. function LibPubPriv($statut)
  869. {
  870. global $langs;
  871. if ($statut=='1') return $langs->trans('ContactPrivate');
  872. else return $langs->trans('ContactPublic');
  873. }
  874. /**
  875. * Initialise an instance with random values.
  876. * Used to build previews or test instances.
  877. * id must be 0 if object instance is a specimen.
  878. *
  879. * @return void
  880. */
  881. function initAsSpecimen()
  882. {
  883. global $user,$langs;
  884. // Charge tableau des id de societe socids
  885. $socids = array();
  886. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe LIMIT 10";
  887. $resql = $this->db->query($sql);
  888. if ($resql)
  889. {
  890. $num_socs = $this->db->num_rows($resql);
  891. $i = 0;
  892. while ($i < $num_socs)
  893. {
  894. $i++;
  895. $row = $this->db->fetch_row($resql);
  896. $socids[$i] = $row[0];
  897. }
  898. }
  899. // Initialise parameters
  900. $this->id=0;
  901. $this->specimen=1;
  902. $this->nom = 'DOLIBARR';
  903. $this->name = $this->nom;
  904. $this->prenom = 'SPECIMEN';
  905. $this->firstname = $this->prenom;
  906. $this->address = '61 jump street';
  907. $this->cp = '75000';
  908. $this->ville = 'Paris';
  909. $this->fk_pays = 1;
  910. $this->pays_code = 'FR';
  911. $this->pays = 'France';
  912. $this->email = 'specimen@specimen.com';
  913. $socid = rand(1, $num_socs);
  914. $this->socid = $socids[$socid];
  915. }
  916. }
  917. ?>