contact.class.php 34 KB

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