contact.class.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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@inodbox.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 <aspangaro@open-dsi.fr>
  10. * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
  11. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  12. * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/contact/class/contact.class.php
  29. * \ingroup societe
  30. * \brief File of contacts class
  31. */
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  33. /**
  34. * Class to manage contact/addresses
  35. */
  36. class Contact extends CommonObject
  37. {
  38. /**
  39. * @var string ID to identify managed object
  40. */
  41. public $element = 'contact';
  42. /**
  43. * @var string Name of table without prefix where object is stored
  44. */
  45. public $table_element = 'socpeople';
  46. /**
  47. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  48. * @var int
  49. */
  50. public $ismultientitymanaged = 1;
  51. /**
  52. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  53. */
  54. public $picto = 'contact';
  55. // BEGIN MODULEBUILDER PROPERTIES
  56. /**
  57. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  58. */
  59. public $fields = array(
  60. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  61. 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>15),
  62. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>20),
  63. 'fk_soc' =>array('type'=>'integer', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
  64. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>30, 'index'=>1),
  65. 'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>0, 'position'=>35),
  66. 'civility' =>array('type'=>'varchar(6)', 'label'=>'Civility', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
  67. 'lastname' =>array('type'=>'varchar(50)', 'label'=>'Lastname', 'enabled'=>1, 'visible'=>-1, 'position'=>45, 'showoncombobox'=>1),
  68. 'firstname' =>array('type'=>'varchar(50)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>-1, 'position'=>50, 'showoncombobox'=>1),
  69. 'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
  70. 'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
  71. 'town' =>array('type'=>'text', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
  72. 'fk_departement' =>array('type'=>'integer', 'label'=>'Fk departement', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
  73. 'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
  74. 'birthday' =>array('type'=>'date', 'label'=>'Birthday', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
  75. 'poste' =>array('type'=>'varchar(80)', 'label'=>'PostOrFunction', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
  76. 'phone' =>array('type'=>'varchar(30)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
  77. 'phone_perso' =>array('type'=>'varchar(30)', 'label'=>'Phone perso', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
  78. 'phone_mobile' =>array('type'=>'varchar(30)', 'label'=>'Phone mobile', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
  79. 'fax' =>array('type'=>'varchar(30)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
  80. 'email' =>array('type'=>'varchar(255)', 'label'=>'Email', 'enabled'=>1, 'visible'=>-1, 'position'=>110),
  81. 'socialnetworks' =>array('type'=>'text', 'label'=>'SocialNetworks', 'enabled'=>1, 'visible'=>-1, 'position'=>115),
  82. 'photo' =>array('type'=>'varchar(255)', 'label'=>'Photo', 'enabled'=>1, 'visible'=>-1, 'position'=>170),
  83. 'priv' =>array('type'=>'smallint(6)', 'label'=>'Private', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>175),
  84. 'no_email' =>array('type'=>'smallint(6)', 'label'=>'No email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),
  85. 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
  86. 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-1, 'position'=>190),
  87. 'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>195),
  88. 'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>200),
  89. 'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>-1, 'position'=>205),
  90. 'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>-1, 'position'=>210),
  91. 'statut' =>array('type'=>'tinyint(4)', 'label'=>'Statut', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
  92. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000),
  93. );
  94. public $civility_id; // In fact we store civility_code
  95. public $civility_code;
  96. public $civility;
  97. /**
  98. * Address
  99. * @var string
  100. */
  101. public $address;
  102. public $zip;
  103. public $town;
  104. public $state_id; // Id of department
  105. public $state_code; // Code of department
  106. public $state; // Label of department
  107. public $poste; // Position
  108. public $socid; // fk_soc
  109. public $statut; // 0=inactif, 1=actif
  110. public $code;
  111. /**
  112. * Email
  113. * @var string
  114. */
  115. public $email;
  116. /**
  117. * Unsuscribe all : 1 = contact has globaly unsubscribe of all mass emailings
  118. * @var int
  119. */
  120. public $no_email;
  121. /**
  122. * @var array array of socialnetworks
  123. */
  124. public $socialnetworks;
  125. /**
  126. * Skype username
  127. * @var string
  128. * @deprecated
  129. */
  130. public $skype;
  131. /**
  132. * Twitter username
  133. * @var string
  134. * @deprecated
  135. */
  136. public $twitter;
  137. /**
  138. * Facebook username
  139. * @var string
  140. * @deprecated
  141. */
  142. public $facebook;
  143. /**
  144. * Linkedin username
  145. * @var string
  146. * @deprecated
  147. */
  148. public $linkedin;
  149. /**
  150. * Jabber username
  151. * @var string
  152. * @deprecated
  153. */
  154. public $jabberid;
  155. public $photo;
  156. public $phone_pro;
  157. public $phone_perso;
  158. public $phone_mobile;
  159. public $fax;
  160. /**
  161. * Private or public
  162. * @var int
  163. */
  164. public $priv;
  165. public $birthday;
  166. public $default_lang;
  167. public $ref_facturation; // Reference number of invoice for which it is contact
  168. public $ref_contrat; // Nb de reference contrat pour lequel il est contact
  169. public $ref_commande; // Nb de reference commande pour lequel il est contact
  170. public $ref_propal; // Nb de reference propal pour lequel il est contact
  171. public $user_id;
  172. public $user_login;
  173. // END MODULEBUILDER PROPERTIES
  174. /**
  175. * Old copy
  176. * @var Contact
  177. */
  178. public $oldcopy; // To contains a clone of this when we need to save old properties of object
  179. public $roles = array();
  180. /**
  181. * Constructor
  182. *
  183. * @param DoliDB $db Database handler
  184. */
  185. public function __construct($db)
  186. {
  187. global $conf, $langs;
  188. $this->db = $db;
  189. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible'] = 0;
  190. if (empty($conf->mailing->enabled)) $this->fields['no_email']['enabled'] = 0;
  191. if (!empty($conf->global->SOCIETE_DISABLE_CONTACTS)) $this->fields['thirdparty']['enabled'] = 0;
  192. $this->statut = 1; // By default, status is enabled
  193. // Unset fields that are disabled
  194. foreach ($this->fields as $key => $val)
  195. {
  196. if (isset($val['enabled']) && empty($val['enabled']))
  197. {
  198. unset($this->fields[$key]);
  199. }
  200. }
  201. // Translate some data of arrayofkeyval
  202. /*if (is_object($langs))
  203. {
  204. foreach($this->fields as $key => $val)
  205. {
  206. if (is_array($val['arrayofkeyval']))
  207. {
  208. foreach($val['arrayofkeyval'] as $key2 => $val2)
  209. {
  210. $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
  211. }
  212. }
  213. }
  214. }*/
  215. }
  216. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  217. /**
  218. * Load indicators into this->nb for board
  219. *
  220. * @return int <0 if KO, >0 if OK
  221. */
  222. public function load_state_board()
  223. {
  224. // phpcs:enable
  225. global $user;
  226. $this->nb = array();
  227. $clause = "WHERE";
  228. $sql = "SELECT count(sp.rowid) as nb";
  229. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  230. if (!$user->rights->societe->client->voir && !$user->socid)
  231. {
  232. $sql .= ", ".MAIN_DB_PREFIX."societe as s";
  233. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  234. $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
  235. $clause = "AND";
  236. }
  237. $sql .= ' '.$clause.' sp.entity IN ('.getEntity($this->element).')';
  238. $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat=".$user->id."))";
  239. if ($user->socid > 0) $sql .= " AND sp.fk_soc = ".$user->socid;
  240. $resql = $this->db->query($sql);
  241. if ($resql)
  242. {
  243. while ($obj = $this->db->fetch_object($resql))
  244. {
  245. $this->nb["contacts"] = $obj->nb;
  246. }
  247. $this->db->free($resql);
  248. return 1;
  249. }
  250. else
  251. {
  252. dol_print_error($this->db);
  253. $this->error = $this->db->lasterror();
  254. return -1;
  255. }
  256. }
  257. /**
  258. * Add a contact into database
  259. *
  260. * @param User $user Object user that create
  261. * @return int <0 if KO, >0 if OK
  262. */
  263. public function create($user)
  264. {
  265. global $conf, $langs;
  266. $error = 0;
  267. $now = dol_now();
  268. $this->db->begin();
  269. // Clean parameters
  270. $this->lastname = $this->lastname ?trim($this->lastname) : trim($this->name);
  271. $this->firstname = trim($this->firstname);
  272. if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname = ucwords(strtolower($this->lastname));
  273. if (!empty($conf->global->MAIN_ALL_TO_UPPER)) $this->lastname = strtoupper($this->lastname);
  274. if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname = ucwords(strtolower($this->firstname));
  275. if (empty($this->socid)) $this->socid = 0;
  276. if (empty($this->priv)) $this->priv = 0;
  277. if (empty($this->statut)) $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
  278. $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
  279. $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
  280. $sql .= " datec";
  281. $sql .= ", fk_soc";
  282. $sql .= ", lastname";
  283. $sql .= ", firstname";
  284. $sql .= ", fk_user_creat";
  285. $sql .= ", priv";
  286. $sql .= ", statut";
  287. $sql .= ", canvas";
  288. $sql .= ", entity";
  289. $sql .= ", ref_ext";
  290. $sql .= ", import_key";
  291. $sql .= ") VALUES (";
  292. $sql .= "'".$this->db->idate($now)."',";
  293. if ($this->socid > 0) $sql .= " ".$this->db->escape($this->socid).",";
  294. else $sql .= "null,";
  295. $sql .= "'".$this->db->escape($this->lastname)."',";
  296. $sql .= "'".$this->db->escape($this->firstname)."',";
  297. $sql .= " ".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "null").",";
  298. $sql .= " ".$this->db->escape($this->priv).",";
  299. $sql .= " ".$this->db->escape($this->statut).",";
  300. $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
  301. $sql .= " ".$this->db->escape($this->entity).",";
  302. $sql .= "'".$this->db->escape($this->ref_ext)."',";
  303. $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
  304. $sql .= ")";
  305. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  306. $resql = $this->db->query($sql);
  307. if ($resql)
  308. {
  309. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
  310. if (!$error)
  311. {
  312. $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
  313. if ($result < 0)
  314. {
  315. $error++;
  316. $this->error = $this->db->lasterror();
  317. }
  318. }
  319. if (!$error)
  320. {
  321. $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
  322. if ($result < 0)
  323. {
  324. $error++;
  325. $this->error = $this->db->lasterror();
  326. }
  327. }
  328. if (!$error)
  329. {
  330. // Call trigger
  331. $result = $this->call_trigger('CONTACT_CREATE', $user);
  332. if ($result < 0) { $error++; }
  333. // End call triggers
  334. }
  335. if (!$error)
  336. {
  337. $this->db->commit();
  338. return $this->id;
  339. }
  340. else
  341. {
  342. $this->db->rollback();
  343. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  344. return -2;
  345. }
  346. }
  347. else
  348. {
  349. $this->error = $this->db->lasterror();
  350. $this->db->rollback();
  351. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  352. return -1;
  353. }
  354. }
  355. /**
  356. * Update informations into database
  357. *
  358. * @param int $id Id of contact/address to update
  359. * @param User $user Objet user making change
  360. * @param int $notrigger 0=no, 1=yes
  361. * @param string $action Current action for hookmanager
  362. * @param int $nosyncuser No sync linked user (external users and contacts are linked)
  363. * @return int <0 if KO, >0 if OK
  364. */
  365. public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
  366. {
  367. global $conf, $langs, $hookmanager;
  368. $error = 0;
  369. $this->id = $id;
  370. $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
  371. // Clean parameters
  372. if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname = ucwords(strtolower($this->lastname));
  373. if (!empty($conf->global->MAIN_ALL_TO_UPPER)) $this->lastname = strtoupper($this->lastname);
  374. if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname = ucwords(strtolower($this->firstname));
  375. $this->lastname = trim($this->lastname) ?trim($this->lastname) : trim($this->lastname);
  376. $this->firstname = trim($this->firstname);
  377. $this->email = trim($this->email);
  378. $this->phone_pro = trim($this->phone_pro);
  379. $this->phone_perso = trim($this->phone_perso);
  380. $this->phone_mobile = trim($this->phone_mobile);
  381. $this->jabberid = trim($this->jabberid);
  382. $this->skype = trim($this->skype);
  383. $this->photo = trim($this->photo);
  384. $this->fax = trim($this->fax);
  385. $this->zip = (empty($this->zip) ? '' : $this->zip);
  386. $this->town = (empty($this->town) ? '' : $this->town);
  387. $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
  388. if (empty($this->statut)) $this->statut = 0;
  389. if (empty($this->civility_code) && !is_numeric($this->civility_id)) $this->civility_code = $this->civility_id; // For backward compatibility
  390. $this->db->begin();
  391. $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET ";
  392. if ($this->socid > 0) $sql .= " fk_soc='".$this->db->escape($this->socid)."',";
  393. elseif ($this->socid == -1) $sql .= " fk_soc=null,";
  394. $sql .= " civility='".$this->db->escape($this->civility_code)."'";
  395. $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
  396. $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
  397. $sql .= ", address='".$this->db->escape($this->address)."'";
  398. $sql .= ", zip='".$this->db->escape($this->zip)."'";
  399. $sql .= ", town='".$this->db->escape($this->town)."'";
  400. $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
  401. $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
  402. $sql .= ", poste='".$this->db->escape($this->poste)."'";
  403. $sql .= ", fax='".$this->db->escape($this->fax)."'";
  404. $sql .= ", email='".$this->db->escape($this->email)."'";
  405. $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
  406. $sql .= ", photo='".$this->db->escape($this->photo)."'";
  407. $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
  408. $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");
  409. $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null");
  410. $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "null");
  411. $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "null");
  412. $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "null");
  413. $sql .= ", priv = '".$this->db->escape($this->priv)."'";
  414. $sql .= ", statut = ".$this->db->escape($this->statut);
  415. $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
  416. $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
  417. $sql .= ", entity = ".$this->db->escape($this->entity);
  418. $sql .= " WHERE rowid=".$this->db->escape($id);
  419. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  420. $result = $this->db->query($sql);
  421. if ($result)
  422. {
  423. unset($this->country_code);
  424. unset($this->country);
  425. unset($this->state_code);
  426. unset($this->state);
  427. $action = 'update';
  428. // Actions on extra fields
  429. if (!$error)
  430. {
  431. $result = $this->insertExtraFields();
  432. if ($result < 0)
  433. {
  434. $error++;
  435. }
  436. }
  437. if (!$error) {
  438. $result = $this->updateRoles();
  439. if ($result < 0)
  440. {
  441. $error++;
  442. }
  443. }
  444. if (!$error && $this->user_id > 0)
  445. {
  446. // If contact is linked to a user
  447. $tmpobj = new User($this->db);
  448. $tmpobj->fetch($this->user_id);
  449. $usermustbemodified = 0;
  450. if ($tmpobj->office_phone != $this->phone_pro)
  451. {
  452. $tmpobj->office_phone = $this->phone_pro;
  453. $usermustbemodified++;
  454. }
  455. if ($tmpobj->office_fax != $this->fax)
  456. {
  457. $tmpobj->office_fax = $this->fax;
  458. $usermustbemodified++;
  459. }
  460. if ($tmpobj->address != $this->address)
  461. {
  462. $tmpobj->address = $this->address;
  463. $usermustbemodified++;
  464. }
  465. if ($tmpobj->town != $this->town)
  466. {
  467. $tmpobj->town = $this->town;
  468. $usermustbemodified++;
  469. }
  470. if ($tmpobj->zip != $this->zip)
  471. {
  472. $tmpobj->zip = $this->zip;
  473. $usermustbemodified++;
  474. }
  475. if ($tmpobj->zip != $this->zip)
  476. {
  477. $tmpobj->state_id = $this->state_id;
  478. $usermustbemodified++;
  479. }
  480. if ($tmpobj->country_id != $this->country_id)
  481. {
  482. $tmpobj->country_id = $this->country_id;
  483. $usermustbemodified++;
  484. }
  485. if ($tmpobj->email != $this->email)
  486. {
  487. $tmpobj->email = $this->email;
  488. $usermustbemodified++;
  489. }
  490. if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks)))
  491. {
  492. $tmpobj->socialnetworks = $this->socialnetworks;
  493. $usermustbemodified++;
  494. }
  495. // if ($tmpobj->skype != $this->skype)
  496. // {
  497. // $tmpobj->skype = $this->skype;
  498. // $usermustbemodified++;
  499. // }
  500. // if ($tmpobj->twitter != $this->twitter)
  501. // {
  502. // $tmpobj->twitter = $this->twitter;
  503. // $usermustbemodified++;
  504. // }
  505. // if ($tmpobj->facebook != $this->facebook)
  506. // {
  507. // $tmpobj->facebook = $this->facebook;
  508. // $usermustbemodified++;
  509. // }
  510. // if ($tmpobj->linkedin != $this->linkedin)
  511. // {
  512. // $tmpobj->linkedin = $this->linkedin;
  513. // $usermustbemodified++;
  514. // }
  515. if ($usermustbemodified)
  516. {
  517. $result = $tmpobj->update($user, 0, 1, 1, 1);
  518. if ($result < 0) { $error++; }
  519. }
  520. }
  521. if (!$error && !$notrigger)
  522. {
  523. // Call trigger
  524. $result = $this->call_trigger('CONTACT_MODIFY', $user);
  525. if ($result < 0) { $error++; }
  526. // End call triggers
  527. }
  528. if (!$error)
  529. {
  530. $this->db->commit();
  531. return 1;
  532. }
  533. else
  534. {
  535. dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
  536. $this->db->rollback();
  537. return -$error;
  538. }
  539. }
  540. else
  541. {
  542. $this->error = $this->db->lasterror().' sql='.$sql;
  543. $this->db->rollback();
  544. return -1;
  545. }
  546. }
  547. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  548. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  549. /**
  550. * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
  551. *
  552. * @param array $info Info string loaded by _load_ldap_info
  553. * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
  554. * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
  555. * 2=Return key only (uid=qqq)
  556. * @return string DN
  557. */
  558. public function _load_ldap_dn($info, $mode = 0)
  559. {
  560. // phpcs:enable
  561. global $conf;
  562. $dn = '';
  563. if ($mode == 0) $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;
  564. elseif ($mode == 1) $dn = $conf->global->LDAP_CONTACT_DN;
  565. elseif ($mode == 2) $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS];
  566. return $dn;
  567. }
  568. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  569. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  570. /**
  571. * Initialise tableau info (tableau des attributs LDAP)
  572. *
  573. * @return array Tableau info des attributs
  574. */
  575. public function _load_ldap_info()
  576. {
  577. // phpcs:enable
  578. global $conf, $langs;
  579. $info = array();
  580. // Object classes
  581. $info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);
  582. $this->fullname = $this->getFullName($langs);
  583. // Fields
  584. if ($this->fullname && !empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME)) $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
  585. if ($this->lastname && !empty($conf->global->LDAP_CONTACT_FIELD_NAME)) $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname;
  586. if ($this->firstname && !empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME)) $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
  587. if ($this->poste) $info["title"] = $this->poste;
  588. if ($this->socid > 0)
  589. {
  590. $soc = new Societe($this->db);
  591. $soc->fetch($this->socid);
  592. $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->name;
  593. if ($soc->client == 1) $info["businessCategory"] = "Customers";
  594. if ($soc->client == 2) $info["businessCategory"] = "Prospects";
  595. if ($soc->fournisseur == 1) $info["businessCategory"] = "Suppliers";
  596. }
  597. if ($this->address && !empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS)) $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
  598. if ($this->zip && !empty($conf->global->LDAP_CONTACT_FIELD_ZIP)) $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->zip;
  599. if ($this->town && !empty($conf->global->LDAP_CONTACT_FIELD_TOWN)) $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->town;
  600. if ($this->country_code && !empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY)) $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code;
  601. if ($this->phone_pro && !empty($conf->global->LDAP_CONTACT_FIELD_PHONE)) $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
  602. if ($this->phone_perso && !empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
  603. if ($this->phone_mobile && !empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
  604. if ($this->fax && !empty($conf->global->LDAP_CONTACT_FIELD_FAX)) $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
  605. if ($this->skype && !empty($conf->global->LDAP_CONTACT_FIELD_SKYPE)) $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype;
  606. if ($this->note_private && !empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
  607. if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL)) $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
  608. if ($conf->global->LDAP_SERVER_TYPE == 'egroupware')
  609. {
  610. $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
  611. $info['uidnumber'] = $this->id;
  612. $info['phpgwTz'] = 0;
  613. $info['phpgwMailType'] = 'INTERNET';
  614. $info['phpgwMailHomeType'] = 'INTERNET';
  615. $info["phpgwContactTypeId"] = 'n';
  616. $info["phpgwContactCatId"] = 0;
  617. $info["phpgwContactAccess"] = "public";
  618. if (dol_strlen($this->egroupware_id) == 0)
  619. {
  620. $this->egroupware_id = 1;
  621. }
  622. $info["phpgwContactOwner"] = $this->egroupware_id;
  623. if ($this->email) $info["rfc822Mailbox"] = $this->email;
  624. if ($this->phone_mobile) $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
  625. }
  626. return $info;
  627. }
  628. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  629. /**
  630. * Update field alert birthday
  631. *
  632. * @param int $id Id of contact
  633. * @param User $user User asking to change alert or birthday
  634. * @param int $notrigger 0=no, 1=yes
  635. * @return int <0 if KO, >=0 if OK
  636. */
  637. public function update_perso($id, $user = null, $notrigger = 0)
  638. {
  639. // phpcs:enable
  640. $error = 0;
  641. $result = false;
  642. $this->db->begin();
  643. // Mis a jour contact
  644. $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
  645. $sql .= " birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
  646. $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
  647. if ($user) $sql .= ", fk_user_modif=".$user->id;
  648. $sql .= " WHERE rowid=".$this->db->escape($id);
  649. dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
  650. $resql = $this->db->query($sql);
  651. if (!$resql)
  652. {
  653. $error++;
  654. $this->error = $this->db->lasterror();
  655. }
  656. // Mis a jour alerte birthday
  657. if ($this->birthday_alert)
  658. {
  659. //check existing
  660. $sql_check = "SELECT rowid FROM ".MAIN_DB_PREFIX."user_alert WHERE type=1 AND fk_contact=".$this->db->escape($id)." AND fk_user=".$user->id;
  661. $result_check = $this->db->query($sql_check);
  662. if (!$result_check || ($this->db->num_rows($result_check) < 1))
  663. {
  664. //insert
  665. $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_alert(type,fk_contact,fk_user) ";
  666. $sql .= "VALUES (1,".$this->db->escape($id).",".$user->id.")";
  667. $result = $this->db->query($sql);
  668. if (!$result)
  669. {
  670. $error++;
  671. $this->error = $this->db->lasterror();
  672. }
  673. }
  674. else
  675. {
  676. $result = true;
  677. }
  678. }
  679. else
  680. {
  681. $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_alert ";
  682. $sql .= "WHERE type=1 AND fk_contact=".$this->db->escape($id)." AND fk_user=".$user->id;
  683. $result = $this->db->query($sql);
  684. if (!$result)
  685. {
  686. $error++;
  687. $this->error = $this->db->lasterror();
  688. }
  689. }
  690. if (!$error && !$notrigger)
  691. {
  692. // Call trigger
  693. $result = $this->call_trigger('CONTACT_MODIFY', $user);
  694. if ($result < 0) { $error++; }
  695. // End call triggers
  696. }
  697. if (!$error)
  698. {
  699. $this->db->commit();
  700. return 1;
  701. }
  702. else
  703. {
  704. dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
  705. $this->db->rollback();
  706. return -$error;
  707. }
  708. }
  709. /**
  710. * Load object contact.
  711. *
  712. * @param int $id Id of contact
  713. * @param User $user Load also alerts of this user (subscribing to alerts) that want alerts about this contact
  714. * @param string $ref_ext External reference, not given by Dolibarr
  715. * @param string $email Email
  716. * @param int $loadalsoroles Load also roles
  717. * @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.
  718. */
  719. public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
  720. {
  721. global $langs;
  722. dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
  723. if (empty($id) && empty($ref_ext) && empty($email))
  724. {
  725. $this->error = 'BadParameter';
  726. return -1;
  727. }
  728. $langs->loadLangs(array("dict", "companies"));
  729. $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.lastname, c.firstname,";
  730. $sql .= " c.address, c.statut, c.zip, c.town,";
  731. $sql .= " c.fk_pays as country_id,";
  732. $sql .= " c.fk_departement as state_id,";
  733. $sql .= " c.birthday,";
  734. $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
  735. $sql .= " c.socialnetworks,";
  736. $sql .= " c.photo,";
  737. $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
  738. $sql .= " c.import_key,";
  739. $sql .= " c.datec as date_creation, c.tms as date_modification,";
  740. $sql .= " co.label as country, co.code as country_code,";
  741. $sql .= " d.nom as state, d.code_departement as state_code,";
  742. $sql .= " u.rowid as user_id, u.login as user_login,";
  743. $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
  744. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  745. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
  746. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
  747. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
  748. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
  749. if ($id) $sql .= " WHERE c.rowid = ".$id;
  750. else
  751. {
  752. $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
  753. if ($ref_ext) {
  754. $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
  755. }
  756. if ($email) {
  757. $sql .= " AND c.email = '".$this->db->escape($email)."'";
  758. }
  759. }
  760. $resql = $this->db->query($sql);
  761. if ($resql)
  762. {
  763. $num = $this->db->num_rows($resql);
  764. if ($num > 1)
  765. {
  766. $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
  767. dol_syslog($this->error, LOG_ERR);
  768. return 2;
  769. }
  770. elseif ($num) // $num = 1
  771. {
  772. $obj = $this->db->fetch_object($resql);
  773. $this->id = $obj->rowid;
  774. $this->entity = $obj->entity;
  775. $this->ref = $obj->rowid;
  776. $this->ref_ext = $obj->ref_ext;
  777. $this->civility_code = $obj->civility_code;
  778. $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
  779. $this->lastname = $obj->lastname;
  780. $this->firstname = $obj->firstname;
  781. $this->address = $obj->address;
  782. $this->zip = $obj->zip;
  783. $this->town = $obj->town;
  784. $this->date_creation = $this->db->jdate($obj->date_creation);
  785. $this->date_modification = $this->db->jdate($obj->date_modification);
  786. $this->state_id = $obj->state_id;
  787. $this->state_code = $obj->state_code;
  788. $this->state = $obj->state;
  789. $this->country_id = $obj->country_id;
  790. $this->country_code = $obj->country_id ? $obj->country_code : '';
  791. $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
  792. $this->socid = $obj->fk_soc;
  793. $this->socname = $obj->socname;
  794. $this->poste = $obj->poste;
  795. $this->statut = $obj->statut;
  796. $this->phone_pro = trim($obj->phone);
  797. $this->fax = trim($obj->fax);
  798. $this->phone_perso = trim($obj->phone_perso);
  799. $this->phone_mobile = trim($obj->phone_mobile);
  800. $this->email = $obj->email;
  801. $this->socialnetworks = (array) json_decode($obj->socialnetworks, true);
  802. $this->photo = $obj->photo;
  803. $this->priv = $obj->priv;
  804. $this->mail = $obj->email;
  805. $this->birthday = $this->db->jdate($obj->birthday);
  806. $this->note = $obj->note_private; // deprecated
  807. $this->note_private = $obj->note_private;
  808. $this->note_public = $obj->note_public;
  809. $this->default_lang = $obj->default_lang;
  810. $this->user_id = $obj->user_id;
  811. $this->user_login = $obj->user_login;
  812. $this->canvas = $obj->canvas;
  813. $this->import_key = $obj->import_key;
  814. // Define gender according to civility
  815. $this->setGenderFromCivility();
  816. // Search Dolibarr user linked to this contact
  817. $sql = "SELECT u.rowid ";
  818. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  819. $sql .= " WHERE u.fk_socpeople = ".$this->id;
  820. $resql = $this->db->query($sql);
  821. if ($resql)
  822. {
  823. if ($this->db->num_rows($resql))
  824. {
  825. $uobj = $this->db->fetch_object($resql);
  826. $this->user_id = $uobj->rowid;
  827. }
  828. $this->db->free($resql);
  829. }
  830. else
  831. {
  832. $this->error = $this->db->error();
  833. return -1;
  834. }
  835. // Retreive all extrafield
  836. // fetch optionals attributes and labels
  837. $this->fetch_optionals();
  838. // Load also alerts of this user
  839. if ($user)
  840. {
  841. $sql = "SELECT fk_user";
  842. $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
  843. $sql .= " WHERE fk_user = ".$user->id." AND fk_contact = ".$this->db->escape($id);
  844. $resql = $this->db->query($sql);
  845. if ($resql)
  846. {
  847. if ($this->db->num_rows($resql))
  848. {
  849. $obj = $this->db->fetch_object($resql);
  850. $this->birthday_alert = 1;
  851. }
  852. $this->db->free($resql);
  853. }
  854. else
  855. {
  856. $this->error = $this->db->error();
  857. return -1;
  858. }
  859. }
  860. // Load also roles of this address
  861. if ($loadalsoroles) {
  862. $resultRole = $this->fetchRoles();
  863. if ($resultRole < 0) {
  864. return $resultRole;
  865. }
  866. }
  867. return 1;
  868. }
  869. else
  870. {
  871. $this->error = $langs->trans("RecordNotFound");
  872. return 0;
  873. }
  874. }
  875. else
  876. {
  877. $this->error = $this->db->error();
  878. return -1;
  879. }
  880. }
  881. /**
  882. * Set the property "gender" of this class, based on the property "civility_id"
  883. * or use property "civility_code" as fallback, when "civility_id" is not available.
  884. *
  885. * @return void
  886. */
  887. public function setGenderFromCivility()
  888. {
  889. unset($this->gender);
  890. if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR')))
  891. {
  892. $this->gender = 'man';
  893. }
  894. elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE')))
  895. {
  896. $this->gender = 'woman';
  897. }
  898. }
  899. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  900. /**
  901. * Load number of elements the contact is used as a link for
  902. * ref_facturation
  903. * ref_contrat
  904. * ref_commande (for order and/or shipments)
  905. * ref_propale
  906. *
  907. * @return int <0 if KO, >=0 if OK
  908. */
  909. public function load_ref_elements()
  910. {
  911. // phpcs:enable
  912. // Compte les elements pour lesquels il est contact
  913. $sql = "SELECT tc.element, count(ec.rowid) as nb";
  914. $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
  915. $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
  916. $sql .= " AND fk_socpeople = ".$this->id;
  917. $sql .= " AND tc.source = 'external'";
  918. $sql .= " GROUP BY tc.element";
  919. dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
  920. $resql = $this->db->query($sql);
  921. if ($resql)
  922. {
  923. while ($obj = $this->db->fetch_object($resql))
  924. {
  925. if ($obj->nb)
  926. {
  927. if ($obj->element == 'facture') $this->ref_facturation = $obj->nb;
  928. elseif ($obj->element == 'contrat') $this->ref_contrat = $obj->nb;
  929. elseif ($obj->element == 'commande') $this->ref_commande = $obj->nb;
  930. elseif ($obj->element == 'propal') $this->ref_propal = $obj->nb;
  931. }
  932. }
  933. $this->db->free($resql);
  934. return 0;
  935. }
  936. else
  937. {
  938. $this->error = $this->db->lasterror();
  939. return -1;
  940. }
  941. }
  942. /**
  943. * Efface le contact de la base
  944. *
  945. * @param int $notrigger Disable all trigger
  946. * @return int <0 if KO, >0 if OK
  947. */
  948. public function delete($notrigger = 0)
  949. {
  950. global $conf, $langs, $user;
  951. $error = 0;
  952. //$this->old_lastname = $obj->lastname;
  953. //$this->old_firstname = $obj->firstname;
  954. $this->db->begin();
  955. if (!$error)
  956. {
  957. // Get all rowid of element_contact linked to a type that is link to llx_socpeople
  958. $sql = "SELECT ec.rowid";
  959. $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
  960. $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
  961. $sql .= " WHERE ec.fk_socpeople=".$this->id;
  962. $sql .= " AND ec.fk_c_type_contact=tc.rowid";
  963. $sql .= " AND tc.source='external'";
  964. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  965. $resql = $this->db->query($sql);
  966. if ($resql)
  967. {
  968. $num = $this->db->num_rows($resql);
  969. $i = 0;
  970. while ($i < $num && !$error)
  971. {
  972. $obj = $this->db->fetch_object($resql);
  973. $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
  974. $sqldel .= " WHERE rowid = ".$obj->rowid;
  975. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  976. $result = $this->db->query($sqldel);
  977. if (!$result)
  978. {
  979. $error++;
  980. $this->error = $this->db->error().' sql='.$sqldel;
  981. }
  982. $i++;
  983. }
  984. }
  985. else
  986. {
  987. $error++;
  988. $this->error = $this->db->error().' sql='.$sql;
  989. }
  990. }
  991. if (!$error)
  992. {
  993. // Remove Roles
  994. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".$this->id;
  995. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  996. $resql = $this->db->query($sql);
  997. if (!$resql)
  998. {
  999. $error++;
  1000. $this->error .= $this->db->lasterror();
  1001. $errorflag = -1;
  1002. }
  1003. }
  1004. if (!$error)
  1005. {
  1006. // Remove category
  1007. $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".$this->id;
  1008. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1009. $resql = $this->db->query($sql);
  1010. if (!$resql)
  1011. {
  1012. $error++;
  1013. $this->error .= $this->db->lasterror();
  1014. $errorflag = -1;
  1015. }
  1016. }
  1017. if (!$error)
  1018. {
  1019. $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
  1020. $sql .= " WHERE rowid=".$this->id;
  1021. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1022. $result = $this->db->query($sql);
  1023. if (!$result)
  1024. {
  1025. $error++;
  1026. $this->error = $this->db->error().' sql='.$sql;
  1027. }
  1028. }
  1029. // Removed extrafields
  1030. if (!$error) {
  1031. // For avoid conflicts if trigger used
  1032. $result = $this->deleteExtraFields();
  1033. if ($result < 0) $error++;
  1034. }
  1035. if (!$error && !$notrigger)
  1036. {
  1037. // Call trigger
  1038. $result = $this->call_trigger('CONTACT_DELETE', $user);
  1039. if ($result < 0) { $error++; }
  1040. // End call triggers
  1041. }
  1042. if (!$error)
  1043. {
  1044. $this->db->commit();
  1045. return 1;
  1046. }
  1047. else
  1048. {
  1049. $this->db->rollback();
  1050. dol_syslog("Error ".$this->error, LOG_ERR);
  1051. return -1;
  1052. }
  1053. }
  1054. /**
  1055. * Charge les informations sur le contact, depuis la base
  1056. *
  1057. * @param int $id Id du contact a charger
  1058. * @return void
  1059. */
  1060. public function info($id)
  1061. {
  1062. $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
  1063. $sql .= " c.tms as tms, c.fk_user_modif";
  1064. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  1065. $sql .= " WHERE c.rowid = ".$this->db->escape($id);
  1066. $resql = $this->db->query($sql);
  1067. if ($resql)
  1068. {
  1069. if ($this->db->num_rows($resql))
  1070. {
  1071. $obj = $this->db->fetch_object($resql);
  1072. $this->id = $obj->rowid;
  1073. if ($obj->fk_user_creat) {
  1074. $cuser = new User($this->db);
  1075. $cuser->fetch($obj->fk_user_creat);
  1076. $this->user_creation = $cuser;
  1077. }
  1078. if ($obj->fk_user_modif) {
  1079. $muser = new User($this->db);
  1080. $muser->fetch($obj->fk_user_modif);
  1081. $this->user_modification = $muser;
  1082. }
  1083. $this->date_creation = $this->db->jdate($obj->datec);
  1084. $this->date_modification = $this->db->jdate($obj->tms);
  1085. }
  1086. $this->db->free($resql);
  1087. }
  1088. else
  1089. {
  1090. print $this->db->error();
  1091. }
  1092. }
  1093. /**
  1094. * Return number of mass Emailing received by this contacts with its email
  1095. *
  1096. * @return int Number of EMailings
  1097. */
  1098. public function getNbOfEMailings()
  1099. {
  1100. $sql = "SELECT count(mc.email) as nb";
  1101. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
  1102. $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
  1103. $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
  1104. $resql = $this->db->query($sql);
  1105. if ($resql)
  1106. {
  1107. $obj = $this->db->fetch_object($resql);
  1108. $nb = $obj->nb;
  1109. $this->db->free($resql);
  1110. return $nb;
  1111. }
  1112. else
  1113. {
  1114. $this->error = $this->db->error();
  1115. return -1;
  1116. }
  1117. }
  1118. /**
  1119. * Return name of contact with link (and eventually picto)
  1120. * Use $this->id, $this->lastname, $this->firstname, this->civility_id
  1121. *
  1122. * @param int $withpicto Include picto with link
  1123. * @param string $option Where the link point to
  1124. * @param int $maxlen Max length of
  1125. * @param string $moreparam Add more param into URL
  1126. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  1127. * @param int $notooltip 1=Disable tooltip
  1128. * @return string String with URL
  1129. */
  1130. public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0)
  1131. {
  1132. global $conf, $langs, $hookmanager;
  1133. $result = ''; $label = '';
  1134. if (!empty($this->photo) && class_exists('Form'))
  1135. {
  1136. $label .= '<div class="photointooltip">';
  1137. $label .= Form::showphoto('contact', $this, 0, 40, 0, '', 'mini', 0); // Important, we must force height so image will have height tags and if image is inside a tooltip, the tooltip manager can calculate height and position correctly the tooltip.
  1138. $label .= '</div><div style="clear: both;"></div>';
  1139. }
  1140. $label .= '<u>'.$langs->trans("Contact").'</u>';
  1141. $label .= '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
  1142. //if ($this->civility_id) $label.= '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate cibilty_id code
  1143. if (!empty($this->poste)) $label .= '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
  1144. $label .= '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
  1145. $phonelist = array();
  1146. if ($this->phone_pro) $phonelist[] = dol_print_phone($this->phone_pro, $this->country_code, $this->id, 0, '', '&nbsp;', 'phone');
  1147. if ($this->phone_mobile) $phonelist[] = dol_print_phone($this->phone_mobile, $this->country_code, $this->id, 0, '', '&nbsp;', 'mobile');
  1148. if ($this->phone_perso) $phonelist[] = dol_print_phone($this->phone_perso, $this->country_code, $this->id, 0, '', '&nbsp;', 'phone');
  1149. $label .= '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
  1150. $label .= '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
  1151. $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
  1152. if ($option !== 'nolink')
  1153. {
  1154. // Add param to save lastsearch_values or not
  1155. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  1156. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
  1157. if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
  1158. }
  1159. $url .= $moreparam;
  1160. $linkclose = "";
  1161. if (empty($notooltip)) {
  1162. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  1163. {
  1164. $label = $langs->trans("ShowContact");
  1165. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  1166. }
  1167. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  1168. $linkclose .= ' class="classfortooltip"';
  1169. /*
  1170. $hookmanager->initHooks(array('contactdao'));
  1171. $parameters=array('id'=>$this->id);
  1172. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  1173. if ($reshook > 0) $linkclose = $hookmanager->resPrint;
  1174. */
  1175. }
  1176. $linkstart = '<a href="'.$url.'"';
  1177. $linkstart .= $linkclose.'>';
  1178. $linkend = '</a>';
  1179. if ($option == 'xxx')
  1180. {
  1181. $linkstart = '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$this->id.$moreparam.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  1182. $linkend = '</a>';
  1183. }
  1184. $result .= $linkstart;
  1185. if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  1186. if ($withpicto != 2) $result .= ($maxlen ?dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs));
  1187. $result .= $linkend;
  1188. global $action;
  1189. $hookmanager->initHooks(array('contactdao'));
  1190. $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
  1191. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1192. if ($reshook > 0) $result = $hookmanager->resPrint;
  1193. else $result .= $hookmanager->resPrint;
  1194. return $result;
  1195. }
  1196. /**
  1197. * Return civility label of contact
  1198. *
  1199. * @return string Translated name of civility
  1200. */
  1201. public function getCivilityLabel()
  1202. {
  1203. global $langs;
  1204. $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
  1205. if (empty($code)) return '';
  1206. $langs->load("dict");
  1207. return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
  1208. }
  1209. /**
  1210. * Return label of contact status
  1211. *
  1212. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1213. * @return string Label of contact status
  1214. */
  1215. public function getLibStatut($mode)
  1216. {
  1217. return $this->LibStatut($this->statut, $mode);
  1218. }
  1219. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1220. /**
  1221. * Renvoi le libelle d'un statut donne
  1222. *
  1223. * @param int $status Id statut
  1224. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1225. * @return string Libelle
  1226. */
  1227. public function LibStatut($status, $mode)
  1228. {
  1229. // phpcs:enable
  1230. global $langs;
  1231. $labelStatus = array(
  1232. 0 => 'ActivityCeased',
  1233. 1 => 'InActivity',
  1234. 4 => 'InActivity',
  1235. 5 => 'ActivityCeased',
  1236. );
  1237. $labelStatusShort = array(
  1238. 0 => 'ActivityCeased',
  1239. 1 => 'InActivity',
  1240. 4 => 'InActivity',
  1241. 5 => 'ActivityCeased',
  1242. );
  1243. $statusType = 'status4';
  1244. if ($status == 0 || $status == 5) $statusType = 'status5';
  1245. $label = $langs->trans($labelStatus[$status]);
  1246. $labelshort = $langs->trans($labelStatusShort[$status]);
  1247. return dolGetStatus($label, $labelshort, '', $statusType, $mode);
  1248. }
  1249. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1250. /**
  1251. * Return translated label of Public or Private
  1252. *
  1253. * @param int $status Type (0 = public, 1 = private)
  1254. * @return string Label translated
  1255. */
  1256. public function LibPubPriv($status)
  1257. {
  1258. // phpcs:enable
  1259. global $langs;
  1260. if ($status == '1') return $langs->trans('ContactPrivate');
  1261. else return $langs->trans('ContactPublic');
  1262. }
  1263. /**
  1264. * Initialise an instance with random values.
  1265. * Used to build previews or test instances.
  1266. * id must be 0 if object instance is a specimen.
  1267. *
  1268. * @return int >0 if ok
  1269. */
  1270. public function initAsSpecimen()
  1271. {
  1272. // Get first id of existing company and save it into $socid
  1273. $socid = 0;
  1274. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
  1275. $resql = $this->db->query($sql);
  1276. if ($resql) {
  1277. $obj = $this->db->fetch_object($resql);
  1278. if ($obj) $socid = $obj->rowid;
  1279. }
  1280. // Initialise parameters
  1281. $this->id = 0;
  1282. $this->specimen = 1;
  1283. $this->lastname = 'DOLIBARR';
  1284. $this->firstname = 'SPECIMEN';
  1285. $this->address = '21 jump street';
  1286. $this->zip = '99999';
  1287. $this->town = 'MyTown';
  1288. $this->country_id = 1;
  1289. $this->country_code = 'FR';
  1290. $this->country = 'France';
  1291. $this->email = 'specimen@specimen.com';
  1292. $this->skype = 'tom.hanson';
  1293. $this->socialnetworks = array(
  1294. 'skype' => 'tom.hanson',
  1295. );
  1296. $this->phone_pro = '0909090901';
  1297. $this->phone_perso = '0909090902';
  1298. $this->phone_mobile = '0909090903';
  1299. $this->fax = '0909090909';
  1300. $this->note_public = 'This is a comment (public)';
  1301. $this->note_private = 'This is a comment (private)';
  1302. $this->socid = $socid;
  1303. $this->statut = 1;
  1304. return 1;
  1305. }
  1306. /**
  1307. * Change status of a user
  1308. *
  1309. * @param int $status Status to set
  1310. * @return int <0 if KO, 0 if nothing is done, >0 if OK
  1311. */
  1312. public function setstatus($status)
  1313. {
  1314. global $conf, $langs, $user;
  1315. $error = 0;
  1316. // Check parameters
  1317. if ($this->statut == $status) return 0;
  1318. else $this->statut = $status;
  1319. $this->db->begin();
  1320. // Desactive utilisateur
  1321. $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
  1322. $sql .= " SET statut = ".$this->statut;
  1323. $sql .= " WHERE rowid = ".$this->id;
  1324. $result = $this->db->query($sql);
  1325. dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
  1326. if ($result)
  1327. {
  1328. // Call trigger
  1329. $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
  1330. if ($result < 0) { $error++; }
  1331. // End call triggers
  1332. }
  1333. if ($error)
  1334. {
  1335. $this->db->rollback();
  1336. return -$error;
  1337. }
  1338. else
  1339. {
  1340. $this->db->commit();
  1341. return 1;
  1342. }
  1343. }
  1344. /**
  1345. * Sets object to supplied categories.
  1346. *
  1347. * Deletes object from existing categories not supplied.
  1348. * Adds it to non existing supplied categories.
  1349. * Existing categories are left untouch.
  1350. *
  1351. * @param int[]|int $categories Category or categories IDs
  1352. * @return void
  1353. */
  1354. public function setCategories($categories)
  1355. {
  1356. // Handle single category
  1357. if (!is_array($categories)) {
  1358. $categories = array($categories);
  1359. }
  1360. // Get current categories
  1361. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1362. $c = new Categorie($this->db);
  1363. $existing = $c->containing($this->id, Categorie::TYPE_CONTACT, 'id');
  1364. // Diff
  1365. if (is_array($existing)) {
  1366. $to_del = array_diff($existing, $categories);
  1367. $to_add = array_diff($categories, $existing);
  1368. } else {
  1369. $to_del = array(); // Nothing to delete
  1370. $to_add = $categories;
  1371. }
  1372. // Process
  1373. foreach ($to_del as $del) {
  1374. if ($c->fetch($del) > 0) {
  1375. $c->del_type($this, Categorie::TYPE_CONTACT);
  1376. }
  1377. }
  1378. foreach ($to_add as $add) {
  1379. if ($c->fetch($add) > 0) {
  1380. $c->add_type($this, Categorie::TYPE_CONTACT);
  1381. }
  1382. }
  1383. return;
  1384. }
  1385. /**
  1386. * Function used to replace a thirdparty id with another one.
  1387. *
  1388. * @param DoliDB $db Database handler
  1389. * @param int $origin_id Old thirdparty id
  1390. * @param int $dest_id New thirdparty id
  1391. * @return bool
  1392. */
  1393. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  1394. {
  1395. $tables = array(
  1396. 'socpeople', 'societe_contacts'
  1397. );
  1398. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  1399. }
  1400. /**
  1401. * Fetch Roles for a contact
  1402. *
  1403. * @return float|int
  1404. * @throws Exception
  1405. */
  1406. public function fetchRoles()
  1407. {
  1408. global $langs;
  1409. $error = 0;
  1410. $num = 0;
  1411. $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle, sc.rowid as contactroleid";
  1412. $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc";
  1413. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."c_type_contact as tc";
  1414. $sql .= " ON tc.rowid = sc.fk_c_type_contact";
  1415. $sql .= " AND sc.fk_socpeople = ".$this->id;
  1416. $sql .= " AND tc.source = 'external' AND tc.active=1";
  1417. $sql .= " AND sc.entity IN (".getEntity('societe').')';
  1418. dol_syslog(__METHOD__, LOG_DEBUG);
  1419. $this->roles = array();
  1420. $resql = $this->db->query($sql);
  1421. if ($resql) {
  1422. $num = $this->db->num_rows($resql);
  1423. if ($num > 0) {
  1424. while ($obj = $this->db->fetch_object($resql)) {
  1425. $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
  1426. $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
  1427. $this->roles[$obj->contactroleid] = array('id'=>$obj->rowid, 'element'=>$obj->element, 'source'=>$obj->source, 'code'=>$obj->code, 'label'=>$libelle_element.' - '.($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->libelle));
  1428. }
  1429. }
  1430. } else {
  1431. $error++;
  1432. $this->error = $this->db->lasterror();
  1433. $this->errors[] = $this->db->lasterror();
  1434. }
  1435. if (empty($error)) {
  1436. return $num;
  1437. } else {
  1438. return $error * -1;
  1439. }
  1440. }
  1441. /**
  1442. * Get Contact roles for a thirdparty
  1443. *
  1444. * @param string $element Element type
  1445. * @return array|int Array of contact roles or -1
  1446. * @throws Exception
  1447. */
  1448. public function getContactRoles($element = '')
  1449. {
  1450. $tab = array();
  1451. if ($element == 'action') {
  1452. $element = 'agenda';
  1453. }
  1454. $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
  1455. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
  1456. $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
  1457. $sql .= " WHERE sc.fk_soc =".$this->socid;
  1458. $sql .= " AND sc.fk_c_type_contact=tc.rowid";
  1459. $sql .= " AND tc.element='".$element."'";
  1460. $sql .= " AND tc.active=1";
  1461. dol_syslog(__METHOD__, LOG_DEBUG);
  1462. $resql = $this->db->query($sql);
  1463. if ($resql)
  1464. {
  1465. $num = $this->db->num_rows($resql);
  1466. $i = 0;
  1467. while ($i < $num)
  1468. {
  1469. $obj = $this->db->fetch_object($resql);
  1470. $tab[] = array('fk_socpeople'=>$obj->id, 'type_contact'=>$obj->fk_c_type_contact);
  1471. $i++;
  1472. }
  1473. return $tab;
  1474. }
  1475. else
  1476. {
  1477. $this->error = $this->db->error();
  1478. dol_print_error($this->db);
  1479. return -1;
  1480. }
  1481. }
  1482. /**
  1483. * Updates Roles
  1484. *
  1485. * @return float|int
  1486. * @throws Exception
  1487. */
  1488. public function updateRoles()
  1489. {
  1490. global $conf;
  1491. $error = 0;
  1492. $this->db->begin();
  1493. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_soc=".$this->socid." AND fk_socpeople=".$this->id; ;
  1494. dol_syslog(__METHOD__, LOG_DEBUG);
  1495. $result = $this->db->query($sql);
  1496. if (!$result) {
  1497. $this->errors[] = $this->db->lasterror().' sql='.$sql;
  1498. $error++;
  1499. } else {
  1500. if (count($this->roles) > 0) {
  1501. foreach ($this->roles as $keyRoles => $valRoles) {
  1502. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
  1503. $sql .= " (entity,";
  1504. $sql .= "date_creation,";
  1505. $sql .= "fk_soc,";
  1506. $sql .= "fk_c_type_contact,";
  1507. $sql .= "fk_socpeople) ";
  1508. $sql .= " VALUES (".$conf->entity.",";
  1509. $sql .= "'".$this->db->idate(dol_now())."',";
  1510. $sql .= $this->socid.", ";
  1511. $sql .= $valRoles." , ";
  1512. $sql .= $this->id;
  1513. $sql .= ")";
  1514. dol_syslog(__METHOD__, LOG_DEBUG);
  1515. $result = $this->db->query($sql);
  1516. if (!$result)
  1517. {
  1518. $this->errors[] = $this->db->lasterror().' sql='.$sql;
  1519. $error++;
  1520. }
  1521. }
  1522. }
  1523. }
  1524. if (empty($error)) {
  1525. $this->db->commit();
  1526. return 1;
  1527. } else {
  1528. $this->error = implode(' ', $this->errors);
  1529. $this->db->rollback();
  1530. return $error * -1;
  1531. }
  1532. }
  1533. }