dolibarr  20.0.0-alpha
contact.class.php
Go to the documentation of this file.
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  * Copyright (C) 2020 Open-Dsi <support@open-dsi.fr>
14  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
15  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <https://www.gnu.org/licenses/>.
29  */
30 
36 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/class/commonsocialnetworks.class.php';
38 require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
39 
40 
44 class Contact extends CommonObject
45 {
47  use CommonPeople;
48 
52  public $element = 'contact';
53 
57  public $table_element = 'socpeople';
58 
63  public $ismultientitymanaged = 1;
64 
68  public $isextrafieldmanaged = 1;
69 
73  public $picto = 'contact';
74 
99  // BEGIN MODULEBUILDER PROPERTIES
103  public $fields = array(
104  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'),
105  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 30, 'index' => 1),
106  'ref_ext' => array('type' => 'varchar(255)', 'label' => 'Ref ext', 'enabled' => 1, 'visible' => 3, 'position' => 35),
107  'civility' => array('type' => 'varchar(6)', 'label' => 'Civility', 'enabled' => 1, 'visible' => 3, 'position' => 40),
108  'lastname' => array('type' => 'varchar(50)', 'label' => 'Lastname', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'showoncombobox' => 1, 'searchall' => 1),
109  'name_alias' => array('type' => 'varchar(255)', 'label' => 'Name alias', 'enabled' => 1, 'visible' => -1, 'position' => 46, 'searchall' => 1),
110  'firstname' => array('type' => 'varchar(50)', 'label' => 'Firstname', 'enabled' => 1, 'visible' => 1, 'position' => 50, 'showoncombobox' => 1, 'searchall' => 1),
111  'poste' => array('type' => 'varchar(80)', 'label' => 'PostOrFunction', 'enabled' => 1, 'visible' => -1, 'position' => 52),
112  'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 55),
113  'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => 1, 'position' => 60),
114  'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 65),
115  'fk_departement' => array('type' => 'integer', 'label' => 'Fk departement', 'enabled' => 1, 'visible' => 3, 'position' => 70),
116  'fk_pays' => array('type' => 'integer', 'label' => 'Fk pays', 'enabled' => 1, 'visible' => 3, 'position' => 75),
117  'fk_soc' => array('type' => 'integer', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => 1, 'position' => 77, 'searchall' => 1),
118  'birthday' => array('type' => 'date', 'label' => 'Birthday', 'enabled' => 1, 'visible' => -1, 'position' => 80),
119  'phone' => array('type' => 'varchar(30)', 'label' => 'Phone', 'enabled' => 1, 'visible' => 1, 'position' => 90, 'searchall' => 1),
120  'phone_perso' => array('type' => 'varchar(30)', 'label' => 'PhonePerso', 'enabled' => 1, 'visible' => -1, 'position' => 95, 'searchall' => 1),
121  'phone_mobile' => array('type' => 'varchar(30)', 'label' => 'PhoneMobile', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'searchall' => 1),
122  'fax' => array('type' => 'varchar(30)', 'label' => 'Fax', 'enabled' => 1, 'visible' => -1, 'position' => 105, 'searchall' => 1),
123  'email' => array('type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'visible' => 1, 'position' => 110, 'searchall' => 1),
124  'socialnetworks' => array('type' => 'text', 'label' => 'SocialNetworks', 'enabled' => 1, 'visible' => 3, 'position' => 115),
125  'photo' => array('type' => 'varchar(255)', 'label' => 'Photo', 'enabled' => 1, 'visible' => 3, 'position' => 170),
126  'priv' => array('type' => 'smallint(6)', 'label' => 'ContactVisibility', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 175),
127  'fk_stcommcontact' => array('type' => 'integer', 'label' => 'ProspectStatus', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 220),
128  'fk_prospectcontactlevel' => array('type' => 'varchar(12)', 'label' => 'ProspectLevel', 'enabled' => 1, 'visible' => -1, 'position' => 255),
129  //no more used. Replace by a scan of email into mailing_unsubscribe. 'no_email' =>array('type'=>'smallint(6)', 'label'=>'No_Email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),
130  'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 3, 'position' => 195, 'searchall' => 1),
131  'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 3, 'position' => 200, 'searchall' => 1),
132  'default_lang' => array('type' => 'varchar(6)', 'label' => 'Default lang', 'enabled' => 1, 'visible' => 3, 'position' => 205),
133  'canvas' => array('type' => 'varchar(32)', 'label' => 'Canvas', 'enabled' => 1, 'visible' => 3, 'position' => 210),
134  'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 300),
135  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305),
136  'fk_user_creat' => array('type' => 'integer', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 3, 'position' => 310),
137  'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 3, 'position' => 315),
138  'statut' => array('type' => 'tinyint(4)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500),
139  'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000),
140  );
141 
142  public $civility_id; // In fact we store civility_code
143  public $civility_code;
144  public $civility;
145 
149  public $gender;
150 
154  //private $egroupware_id;
155 
159  public $birthday_alert;
160 
166  public $civilite;
167 
171  public $fullname;
172 
176  public $name_alias;
177 
181  public $address;
182 
186  public $zip;
187 
191  public $town;
192 
196  public $state_id;
197 
201  public $state_code;
202 
206  public $state;
207 
211  public $poste;
212 
216  public $socid; // both socid and fk_soc are used
217  public $fk_soc; // both socid and fk_soc are used
218 
222  public $socname;
223 
227  public $statut;
228 
229  public $code;
230 
235  public $email;
236 
243  public $mail;
244 
249  public $url;
250 
256  public $no_email;
257 
262  public $socialnetworks;
263 
267  public $photo;
268 
272  public $phone_pro;
273 
277  public $phone_perso;
278 
282  public $phone_mobile;
283 
287  public $fax;
288 
293  public $priv;
294 
298  public $birthday;
299 
303  public $default_lang;
304 
308  public $ref_facturation;
309 
313  public $ref_contrat;
314 
318  public $ref_commande;
319 
323  public $ref_propal;
324 
328  public $user_id;
329 
333  public $user_login;
334 
335  // END MODULEBUILDER PROPERTIES
336 
337 
342  public $oldcopy; // To contains a clone of this when we need to save old properties of object
343 
347  public $roles;
348 
349  public $cacheprospectstatus = array();
350 
354  public $fk_prospectlevel;
355 
356  public $stcomm_id;
357 
358  public $statut_commercial;
359 
363  public $stcomm_picto;
364 
365 
371  public function __construct($db)
372  {
373  $this->db = $db;
374  $this->statut = 1; // By default, status is enabled
375 
376  if (!isModEnabled('mailing')) {
377  $this->fields['no_email']['enabled'] = 0;
378  }
379  // typical ['s.nom'] is used for third-parties
380  if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) {
381  $this->fields['fk_soc']['enabled'] = 0;
382  $this->fields['fk_soc']['searchall'] = 0;
383  }
384 
385  // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
386  if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) { // Default behaviour
387  $this->fields['fk_stcommcontact']['enabled'] = 0;
388  $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
389  }
390 
391  // Unset fields that are disabled
392  foreach ($this->fields as $key => $val) {
393  if (isset($val['enabled']) && empty($val['enabled'])) {
394  unset($this->fields[$key]);
395  }
396  }
397 
398  // Translate some data of arrayofkeyval
399  /*if (is_object($langs))
400  {
401  foreach($this->fields as $key => $val)
402  {
403  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
404  {
405  foreach($val['arrayofkeyval'] as $key2 => $val2)
406  {
407  $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
408  }
409  }
410  }
411  }*/
412  }
413 
419  public function loadStateBoard()
420  {
421  global $user, $hookmanager;
422 
423  $this->nb = array();
424  $clause = "WHERE";
425 
426  $sql = "SELECT count(sp.rowid) as nb";
427  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
428  if (!$user->hasRight('societe', 'client', 'voir')) {
429  $sql .= ", ".MAIN_DB_PREFIX."societe as s";
430  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
431  $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
432  $clause = "AND";
433  }
434  $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
435  $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
436  if ($user->socid > 0) {
437  $sql .= " AND sp.fk_soc = ".((int) $user->socid);
438  }
439  // Add where from hooks
440  if (is_object($hookmanager)) {
441  $parameters = array();
442  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
443  $sql .= $hookmanager->resPrint;
444  }
445 
446  $resql = $this->db->query($sql);
447  if ($resql) {
448  while ($obj = $this->db->fetch_object($resql)) {
449  $this->nb["contacts"] = $obj->nb;
450  }
451  $this->db->free($resql);
452  return 1;
453  } else {
454  dol_print_error($this->db);
455  $this->error = $this->db->lasterror();
456  return -1;
457  }
458  }
459 
467  public function create($user, $notrigger = 0)
468  {
469  global $conf;
470 
471  $error = 0;
472  $now = dol_now();
473 
474  $this->db->begin();
475 
476  // Clean parameters
477  $this->name_alias = trim($this->name_alias);
478  $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name);
479  $this->firstname = trim($this->firstname);
480  $this->setUpperOrLowerCase();
481  if (empty($this->socid)) {
482  $this->socid = 0;
483  }
484  if (empty($this->priv)) {
485  $this->priv = 0;
486  }
487  if (empty($this->statut)) {
488  $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
489  }
490 
491  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
492 
493  $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
494  $sql .= " datec";
495  $sql .= ", fk_soc";
496  $sql .= ", name_alias";
497  $sql .= ", lastname";
498  $sql .= ", firstname";
499  $sql .= ", fk_user_creat";
500  $sql .= ", priv";
501  $sql .= ", fk_stcommcontact";
502  $sql .= ", statut";
503  $sql .= ", canvas";
504  $sql .= ", entity";
505  $sql .= ", ref_ext";
506  $sql .= ", import_key";
507  $sql .= ") VALUES (";
508  $sql .= "'".$this->db->idate($now)."',";
509  if ($this->socid > 0) {
510  $sql .= " ".((int) $this->socid).",";
511  } else {
512  $sql .= "null,";
513  }
514  $sql .= "'".$this->db->escape($this->name_alias)."',";
515  $sql .= "'".$this->db->escape($this->lastname)."',";
516  $sql .= "'".$this->db->escape($this->firstname)."',";
517  $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
518  $sql .= " ".((int) $this->priv).",";
519  $sql .= " 0,";
520  $sql .= " ".((int) $this->statut).",";
521  $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
522  $sql .= " ".((int) $this->entity).",";
523  $sql .= "'".$this->db->escape($this->ref_ext)."',";
524  $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
525  $sql .= ")";
526 
527  dol_syslog(get_class($this)."::create", LOG_DEBUG);
528  $resql = $this->db->query($sql);
529  if ($resql) {
530  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
531 
532  if (!$error) {
533  $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
534  if ($result < 0) {
535  $error++;
536  $this->error = $this->db->lasterror();
537  }
538  }
539 
540  if (!$error) {
541  $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
542  if ($result < 0) {
543  $error++;
544  $this->error = $this->db->lasterror();
545  }
546  }
547 
548  if (!$error && !$notrigger) {
549  // Call trigger
550  $result = $this->call_trigger('CONTACT_CREATE', $user);
551  if ($result < 0) {
552  $error++;
553  }
554  // End call triggers
555  }
556 
557  if (!$error) {
558  $this->db->commit();
559  return $this->id;
560  } else {
561  $this->db->rollback();
562  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
563  return -2;
564  }
565  } else {
566  $this->error = $this->db->lasterror();
567 
568  $this->db->rollback();
569  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
570  return -1;
571  }
572  }
573 
584  public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
585  {
586  global $conf;
587 
588  $error = 0;
589 
590  $this->id = $id;
591 
592  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
593 
594  // Clean parameters
595  $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext));
596  $this->name_alias = trim($this->name_alias);
597  $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
598  $this->firstname = trim($this->firstname);
599  $this->email = trim($this->email);
600  $this->phone_pro = trim($this->phone_pro);
601  $this->phone_perso = trim($this->phone_perso);
602  $this->phone_mobile = trim($this->phone_mobile);
603  $this->photo = trim($this->photo);
604  $this->fax = trim($this->fax);
605  $this->zip = (empty($this->zip) ? '' : trim($this->zip));
606  $this->town = (empty($this->town) ? '' : trim($this->town));
607  $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
608  if (empty($this->statut)) {
609  $this->statut = 0;
610  }
611  if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
612  $this->civility_code = $this->civility_id; // For backward compatibility
613  }
614  $this->setUpperOrLowerCase();
615 
616  $this->db->begin();
617 
618  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
619  if ($this->socid > 0) {
620  $sql .= " fk_soc = ".((int) $this->socid).",";
621  } elseif ($this->socid == -1) {
622  $sql .= " fk_soc = NULL,";
623  }
624  $sql .= " civility='".$this->db->escape($this->civility_code)."'";
625  $sql .= ", name_alias='".$this->db->escape($this->name_alias)."'";
626  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
627  $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
628  $sql .= ", address='".$this->db->escape($this->address)."'";
629  $sql .= ", zip='".$this->db->escape($this->zip)."'";
630  $sql .= ", town='".$this->db->escape($this->town)."'";
631  $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
632  $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
633  $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
634  $sql .= ", poste='".$this->db->escape($this->poste)."'";
635  $sql .= ", fax='".$this->db->escape($this->fax)."'";
636  $sql .= ", email='".$this->db->escape($this->email)."'";
637  $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
638  $sql .= ", photo='".$this->db->escape($this->photo)."'";
639  $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
640  $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
641  $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
642  $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
643  $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
644  $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
645  $sql .= ", priv = '".$this->db->escape($this->priv)."'";
646  $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
647  if (isset($this->stcomm_id)) {
648  $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
649  }
650  $sql .= ", statut = ".((int) $this->statut);
651  $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
652  $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
653  $sql .= ", entity = ".((int) $this->entity);
654  $sql .= " WHERE rowid = ".((int) $id);
655 
656  dol_syslog(get_class($this)."::update", LOG_DEBUG);
657  $result = $this->db->query($sql);
658  if ($result) {
659  unset($this->country_code);
660  unset($this->country);
661  unset($this->state_code);
662  unset($this->state);
663 
664  $action = 'update';
665 
666  // Actions on extra fields
667  if (!$error) {
668  $result = $this->insertExtraFields();
669  if ($result < 0) {
670  $error++;
671  }
672  }
673 
674  if (!$error) {
675  $result = $this->updateRoles();
676  if ($result < 0) {
677  $error++;
678  }
679  }
680 
681  if (!$error && $this->user_id > 0) {
682  // If contact is linked to a user
683  $tmpobj = new User($this->db);
684  $tmpobj->fetch($this->user_id);
685  $usermustbemodified = 0;
686  if ($tmpobj->office_phone != $this->phone_pro) {
687  $tmpobj->office_phone = $this->phone_pro;
688  $usermustbemodified++;
689  }
690  if ($tmpobj->office_fax != $this->fax) {
691  $tmpobj->office_fax = $this->fax;
692  $usermustbemodified++;
693  }
694  if ($tmpobj->address != $this->address) {
695  $tmpobj->address = $this->address;
696  $usermustbemodified++;
697  }
698  if ($tmpobj->town != $this->town) {
699  $tmpobj->town = $this->town;
700  $usermustbemodified++;
701  }
702  if ($tmpobj->zip != $this->zip) {
703  $tmpobj->zip = $this->zip;
704  $usermustbemodified++;
705  }
706  if ($tmpobj->zip != $this->zip) {
707  $tmpobj->state_id = $this->state_id;
708  $usermustbemodified++;
709  }
710  if ($tmpobj->country_id != $this->country_id) {
711  $tmpobj->country_id = $this->country_id;
712  $usermustbemodified++;
713  }
714  if ($tmpobj->email != $this->email) {
715  $tmpobj->email = $this->email;
716  $usermustbemodified++;
717  }
718  if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
719  $tmpobj->socialnetworks = $this->socialnetworks;
720  $usermustbemodified++;
721  }
722  if ($usermustbemodified) {
723  $result = $tmpobj->update($user, 0, 1, 1, 1);
724  if ($result < 0) {
725  $error++;
726  }
727  }
728  }
729 
730  if (!$error && !$notrigger) {
731  // Call trigger
732  $result = $this->call_trigger('CONTACT_MODIFY', $user);
733  if ($result < 0) {
734  $error++;
735  }
736  // End call triggers
737  }
738 
739  if (!$error) {
740  $this->db->commit();
741  return 1;
742  } else {
743  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
744  $this->db->rollback();
745  return -$error;
746  }
747  } else {
748  $this->error = $this->db->lasterror().' sql='.$sql;
749  $this->db->rollback();
750  return -1;
751  }
752  }
753 
754 
755  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
756  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
766  public function _load_ldap_dn($info, $mode = 0)
767  {
768  // phpcs:enable
769  global $conf;
770  $dn = '';
771  if ($mode == 0) {
772  $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
773  } elseif ($mode == 1) {
774  $dn = getDolGlobalString('LDAP_CONTACT_DN');
775  } elseif ($mode == 2) {
776  $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
777  }
778  return $dn;
779  }
780 
781 
782  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
783  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
789  public function _load_ldap_info()
790  {
791  // phpcs:enable
792  global $conf, $langs;
793 
794  $info = array();
795 
796  // Object classes
797  $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
798 
799  $this->fullname = $this->getFullName($langs);
800 
801  // Fields
802  if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
803  $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
804  }
805  if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
806  $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
807  }
808  if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
809  $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
810  }
811 
812  if ($this->poste) {
813  $info["title"] = $this->poste;
814  }
815  if ($this->socid > 0) {
816  $soc = new Societe($this->db);
817  $soc->fetch($this->socid);
818 
819  $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
820  if ($soc->client == 1) {
821  $info["businessCategory"] = "Customers";
822  }
823  if ($soc->client == 2) {
824  $info["businessCategory"] = "Prospects";
825  }
826  if ($soc->fournisseur == 1) {
827  $info["businessCategory"] = "Suppliers";
828  }
829  }
830  if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
831  $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
832  }
833  if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
834  $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
835  }
836  if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
837  $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
838  }
839  if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
840  $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
841  }
842  if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
843  $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
844  }
845  if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
846  $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
847  }
848  if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
849  $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
850  }
851  if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
852  $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
853  }
854  if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
855  $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
856  }
857  if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
858  $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
859  }
860 
861  if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
862  $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
863 
864  $info['uidnumber'] = $this->id;
865 
866  $info['phpgwTz'] = 0;
867  $info['phpgwMailType'] = 'INTERNET';
868  $info['phpgwMailHomeType'] = 'INTERNET';
869 
870  $info["phpgwContactTypeId"] = 'n';
871  $info["phpgwContactCatId"] = 0;
872  $info["phpgwContactAccess"] = "public";
873 
874  /*
875  if (dol_strlen($this->egroupware_id) == 0) {
876  $this->egroupware_id = 1;
877  }
878  $info["phpgwContactOwner"] = $this->egroupware_id;
879  */
880  $info["phpgwContactOwner"] = 1;
881 
882  if ($this->email) {
883  $info["rfc822Mailbox"] = $this->email;
884  }
885  if ($this->phone_mobile) {
886  $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
887  }
888  }
889 
890  return $info;
891  }
892 
893 
894  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
903  public function update_perso($id, $user = null, $notrigger = 0)
904  {
905  // phpcs:enable
906  $error = 0;
907  $result = false;
908 
909  $this->db->begin();
910 
911  // Update the contact
912  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
913  $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
914  $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
915  if ($user) {
916  $sql .= ", fk_user_modif = ".((int) $user->id);
917  }
918  $sql .= " WHERE rowid = ".((int) $id);
919 
920  dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
921  $resql = $this->db->query($sql);
922  if (!$resql) {
923  $error++;
924  $this->error = $this->db->lasterror();
925  }
926 
927  if ($user) {
928  // Update birthday alert
929  if (!empty($this->birthday_alert)) {
930  //check existing
931  $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
932  $result_check = $this->db->query($sql_check);
933  if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
934  //insert
935  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
936  $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
937  $result = $this->db->query($sql);
938  if (!$result) {
939  $error++;
940  $this->error = $this->db->lasterror();
941  }
942  } else {
943  $result = true;
944  }
945  } else {
946  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
947  $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
948  $result = $this->db->query($sql);
949  if (!$result) {
950  $error++;
951  $this->error = $this->db->lasterror();
952  }
953  }
954  }
955 
956  if (!$error && !$notrigger) {
957  // Call trigger
958  $result = $this->call_trigger('CONTACT_MODIFY', $user);
959  if ($result < 0) {
960  $error++;
961  }
962  // End call triggers
963  }
964 
965  if (!$error) {
966  $this->db->commit();
967  return 1;
968  } else {
969  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
970  $this->db->rollback();
971  return -$error;
972  }
973  }
974 
975 
986  public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
987  {
988  global $langs;
989 
990  dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
991 
992  if (empty($id) && empty($ref_ext) && empty($email)) {
993  $this->error = 'BadParameter';
994  return -1;
995  }
996 
997  $langs->loadLangs(array("dict", "companies"));
998 
999  $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1000  $sql .= " c.address, c.statut, c.zip, c.town,";
1001  $sql .= " c.fk_pays as country_id,";
1002  $sql .= " c.fk_departement as state_id,";
1003  $sql .= " c.birthday,";
1004  $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1005  $sql .= " c.socialnetworks,";
1006  $sql .= " c.photo,";
1007  $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1008  $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1009  $sql .= " c.import_key,";
1010  $sql .= " c.datec as date_creation, c.tms as date_modification,";
1011  $sql .= " co.label as country, co.code as country_code,";
1012  $sql .= " d.nom as state, d.code_departement as state_code,";
1013  $sql .= " u.rowid as user_id, u.login as user_login,";
1014  $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1015  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1016  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1017  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1018  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1019  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1020  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1021  if ($id) {
1022  $sql .= " WHERE c.rowid = ".((int) $id);
1023  } else {
1024  $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1025  if ($ref_ext) {
1026  $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1027  }
1028  if ($email) {
1029  $sql .= " AND c.email = '".$this->db->escape($email)."'";
1030  }
1031  }
1032 
1033  $resql = $this->db->query($sql);
1034  if ($resql) {
1035  $num = $this->db->num_rows($resql);
1036  if ($num > 1) {
1037  $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1038  dol_syslog($this->error, LOG_ERR);
1039 
1040  return 2;
1041  } elseif ($num) { // $num = 1
1042  $obj = $this->db->fetch_object($resql);
1043 
1044  $this->id = $obj->rowid;
1045  $this->entity = $obj->entity;
1046  $this->ref = $obj->rowid;
1047  $this->ref_ext = $obj->ref_ext;
1048 
1049  $this->civility_code = $obj->civility_code;
1050  $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1051 
1052  $this->name_alias = $obj->name_alias;
1053  $this->lastname = $obj->lastname;
1054  $this->firstname = $obj->firstname;
1055  $this->address = $obj->address;
1056  $this->zip = $obj->zip;
1057  $this->town = $obj->town;
1058 
1059  $this->date_creation = $this->db->jdate($obj->date_creation);
1060  $this->date_modification = $this->db->jdate($obj->date_modification);
1061 
1062  $this->state_id = $obj->state_id;
1063  $this->state_code = $obj->state_code;
1064  $this->state = $obj->state;
1065 
1066  $this->country_id = $obj->country_id;
1067  $this->country_code = $obj->country_id ? $obj->country_code : '';
1068  $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1069 
1070  $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1071  $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1072  $this->socname = $obj->socname;
1073  $this->poste = $obj->poste;
1074  $this->statut = $obj->statut;
1075 
1076  $this->fk_prospectlevel = $obj->fk_prospectlevel;
1077 
1078  $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1079  $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1080  $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1081  $this->statut_commercial = $libelle; // libelle statut commercial
1082  $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1083 
1084  $this->phone_pro = trim($obj->phone);
1085  $this->fax = trim($obj->fax);
1086  $this->phone_perso = trim($obj->phone_perso);
1087  $this->phone_mobile = trim($obj->phone_mobile);
1088 
1089  $this->email = $obj->email;
1090  $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1091  $this->photo = $obj->photo;
1092  $this->priv = $obj->priv;
1093  $this->mail = $obj->email;
1094 
1095  $this->birthday = $this->db->jdate($obj->birthday);
1096  $this->note = $obj->note_private; // deprecated
1097  $this->note_private = $obj->note_private;
1098  $this->note_public = $obj->note_public;
1099  $this->default_lang = $obj->default_lang;
1100  $this->user_id = $obj->user_id;
1101  $this->user_login = $obj->user_login;
1102  $this->canvas = $obj->canvas;
1103 
1104  $this->import_key = $obj->import_key;
1105 
1106  // Define gender according to civility
1107  $this->setGenderFromCivility();
1108 
1109  // Search Dolibarr user linked to this contact
1110  $sql = "SELECT u.rowid ";
1111  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1112  $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1113 
1114  $resql = $this->db->query($sql);
1115  if ($resql) {
1116  if ($this->db->num_rows($resql)) {
1117  $uobj = $this->db->fetch_object($resql);
1118 
1119  $this->user_id = $uobj->rowid;
1120  }
1121  $this->db->free($resql);
1122  } else {
1123  $this->error = $this->db->error();
1124  return -1;
1125  }
1126 
1127  // Retrieve all extrafield
1128  // fetch optionals attributes and labels
1129  $this->fetch_optionals();
1130 
1131  // Load also alerts of this user
1132  if ($user) {
1133  $sql = "SELECT fk_user";
1134  $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1135  $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1136 
1137  $resql = $this->db->query($sql);
1138  if ($resql) {
1139  if ($this->db->num_rows($resql)) {
1140  $obj = $this->db->fetch_object($resql);
1141 
1142  $this->birthday_alert = 1;
1143  }
1144  $this->db->free($resql);
1145  } else {
1146  $this->error = $this->db->error();
1147  return -1;
1148  }
1149  }
1150 
1151  // Load also roles of this address
1152  if ($loadalsoroles) {
1153  $resultRole = $this->fetchRoles();
1154  if ($resultRole < 0) {
1155  return $resultRole;
1156  }
1157  }
1158 
1159  return 1;
1160  } else {
1161  $this->error = $langs->trans("RecordNotFound");
1162  return 0;
1163  }
1164  } else {
1165  $this->error = $this->db->error();
1166  return -1;
1167  }
1168  }
1169 
1170 
1171 
1178  public function setGenderFromCivility()
1179  {
1180  unset($this->gender);
1181 
1182  if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1183  $this->gender = 'man';
1184  } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1185  $this->gender = 'woman';
1186  }
1187  }
1188 
1189  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1199  public function load_ref_elements()
1200  {
1201  // phpcs:enable
1202  // Count the elements for which it is contact
1203  $sql = "SELECT tc.element, count(ec.rowid) as nb";
1204  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1205  $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1206  $sql .= " AND fk_socpeople = ".((int) $this->id);
1207  $sql .= " AND tc.source = 'external'";
1208  $sql .= " GROUP BY tc.element";
1209 
1210  dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1211 
1212  $resql = $this->db->query($sql);
1213  if ($resql) {
1214  while ($obj = $this->db->fetch_object($resql)) {
1215  if ($obj->nb) {
1216  if ($obj->element == 'facture') {
1217  $this->ref_facturation = $obj->nb;
1218  } elseif ($obj->element == 'contrat') {
1219  $this->ref_contrat = $obj->nb;
1220  } elseif ($obj->element == 'commande') {
1221  $this->ref_commande = $obj->nb;
1222  } elseif ($obj->element == 'propal') {
1223  $this->ref_propal = $obj->nb;
1224  }
1225  }
1226  }
1227  $this->db->free($resql);
1228  return 0;
1229  } else {
1230  $this->error = $this->db->lasterror();
1231  return -1;
1232  }
1233  }
1234 
1242  public function delete($user, $notrigger = 0)
1243  {
1244  $error = 0;
1245 
1246  $this->db->begin();
1247 
1248  if (!$error && !$notrigger) {
1249  // Call trigger
1250  $result = $this->call_trigger('CONTACT_DELETE', $user);
1251  if ($result < 0) {
1252  $error++;
1253  }
1254  // End call triggers
1255  }
1256 
1257  if (!$error) {
1258  // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1259  $sql = "SELECT ec.rowid";
1260  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1261  $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1262  $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1263  $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1264  $sql .= " AND tc.source='external'";
1265  dol_syslog(__METHOD__, LOG_DEBUG);
1266  $resql = $this->db->query($sql);
1267  if ($resql) {
1268  $num = $this->db->num_rows($resql);
1269 
1270  $i = 0;
1271  while ($i < $num && !$error) {
1272  $obj = $this->db->fetch_object($resql);
1273 
1274  $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1275  $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1276  dol_syslog(__METHOD__, LOG_DEBUG);
1277  $result = $this->db->query($sqldel);
1278  if (!$result) {
1279  $error++;
1280  $this->error = $this->db->error().' sql='.$sqldel;
1281  }
1282 
1283  $i++;
1284  }
1285  } else {
1286  $error++;
1287  $this->error = $this->db->error().' sql='.$sql;
1288  }
1289  }
1290 
1291  if (!$error) {
1292  // Remove Roles
1293  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1294  dol_syslog(__METHOD__, LOG_DEBUG);
1295  $resql = $this->db->query($sql);
1296  if (!$resql) {
1297  $error++;
1298  $this->error .= $this->db->lasterror();
1299  $errorflag = -1;
1300  }
1301  }
1302 
1303  if (!$error) {
1304  // Remove Notifications
1305  $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1306  dol_syslog(__METHOD__, LOG_DEBUG);
1307  $resql = $this->db->query($sql);
1308  if (!$resql) {
1309  $error++;
1310  $this->error .= $this->db->lasterror();
1311  $errorflag = -1;
1312  }
1313  }
1314 
1315  if (!$error) {
1316  // Remove category
1317  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1318  dol_syslog(__METHOD__, LOG_DEBUG);
1319  $resql = $this->db->query($sql);
1320  if (!$resql) {
1321  $error++;
1322  $this->error .= $this->db->lasterror();
1323  $errorflag = -1;
1324  }
1325  }
1326 
1327  if (!$error) {
1328  $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1329  $sql .= " WHERE rowid = ".((int) $this->id);
1330  dol_syslog(__METHOD__, LOG_DEBUG);
1331  $result = $this->db->query($sql);
1332  if (!$result) {
1333  $error++;
1334  $this->error = $this->db->error().' sql='.$sql;
1335  }
1336  }
1337 
1338  // Remove extrafields
1339  if (!$error) {
1340  // For avoid conflicts if trigger used
1341  $result = $this->deleteExtraFields();
1342  if ($result < 0) {
1343  $error++;
1344  }
1345  }
1346 
1347  if (!$error) {
1348  $this->db->commit();
1349  return 1;
1350  } else {
1351  $this->db->rollback();
1352  dol_syslog("Error ".$this->error, LOG_ERR);
1353  return -1;
1354  }
1355  }
1356 
1357 
1364  public function info($id)
1365  {
1366  $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1367  $sql .= " c.tms as tms, c.fk_user_modif";
1368  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1369  $sql .= " WHERE c.rowid = ".((int) $id);
1370 
1371  $resql = $this->db->query($sql);
1372  if ($resql) {
1373  if ($this->db->num_rows($resql)) {
1374  $obj = $this->db->fetch_object($resql);
1375 
1376  $this->id = $obj->rowid;
1377 
1378  $this->user_creation_id = $obj->fk_user_creat;
1379  $this->user_modification_id = $obj->fk_user_modif;
1380  $this->date_creation = $this->db->jdate($obj->datec);
1381  $this->date_modification = $this->db->jdate($obj->tms);
1382  }
1383 
1384  $this->db->free($resql);
1385  } else {
1386  print $this->db->error();
1387  }
1388  }
1389 
1395  public function getNbOfEMailings()
1396  {
1397  $sql = "SELECT count(mc.email) as nb";
1398  $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1399  $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1400  $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1401 
1402  $resql = $this->db->query($sql);
1403  if ($resql) {
1404  $obj = $this->db->fetch_object($resql);
1405  $nb = $obj->nb;
1406 
1407  $this->db->free($resql);
1408  return $nb;
1409  } else {
1410  $this->error = $this->db->error();
1411  return -1;
1412  }
1413  }
1414 
1421  public function getTooltipContentArray($params)
1422  {
1423  global $conf, $langs, $user;
1424 
1425  $datas = [];
1426 
1427  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1428  return ['optimize' => $langs->trans("ShowContact")];
1429  }
1430  if (!empty($this->photo) && class_exists('Form')) {
1431  $photo = '<div class="photointooltip floatright">';
1432  $photo .= Form::showphoto('contact', $this, 0, 40, 0, 'photoref', '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.
1433  $photo .= '</div>';
1434  $datas['photo'] = $photo;
1435  }
1436 
1437  $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1438  $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1439  // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1440  if (!empty($this->poste)) {
1441  $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1442  }
1443  $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1444  $phonelist = array();
1445  $country_code = empty($this->country_code) ? '' : $this->country_code;
1446  if ($this->phone_pro) {
1447  $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1448  }
1449  if ($this->phone_mobile) {
1450  $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1451  }
1452  if ($this->phone_perso) {
1453  $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1454  }
1455  $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1456  $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1457 
1458  return $datas;
1459  }
1460 
1474  public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1475  {
1476  global $conf, $langs, $hookmanager;
1477 
1478  if (!empty($conf->dol_no_mouse_hover)) {
1479  $notooltip = 1; // Force disable tooltips
1480  }
1481 
1482  $result = '';
1483  $params = [
1484  'id' => $this->id,
1485  'objecttype' => $this->element,
1486  'option' => $option,
1487  ];
1488  $classfortooltip = 'classfortooltip';
1489  $dataparams = '';
1490  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1491  $classfortooltip = 'classforajaxtooltip';
1492  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1493  $label = '';
1494  } else {
1495  $label = implode($this->getTooltipContentArray($params));
1496  }
1497 
1498  $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1499 
1500  if ($option !== 'nolink') {
1501  // Add param to save lastsearch_values or not
1502  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1503  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1504  $add_save_lastsearch_values = 1;
1505  }
1506  if ($url && $add_save_lastsearch_values) {
1507  $url .= '&save_lastsearch_values=1';
1508  }
1509  }
1510 
1511  $url .= $moreparam;
1512 
1513  $linkclose = '';
1514  if (empty($notooltip)) {
1515  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1516  $label = $langs->trans("ShowContact");
1517  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1518  }
1519  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1520  $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1521  } else {
1522  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1523  }
1524 
1525  if ($option == 'nolink' || empty($url)) {
1526  $linkstart = '<span';
1527  } else {
1528  $linkstart = '<a href="'.$url.'"';
1529  }
1530  $linkstart .= $linkclose.'>';
1531  if ($option == 'nolink' || empty($url)) {
1532  $linkend = '</span>';
1533  } else {
1534  $linkend = '</a>';
1535  }
1536 
1537  $result .= $linkstart;
1538 
1539  if ($withpicto) {
1540  if ($withpicto < 0) {
1541  $result .= '<!-- picto photo contact --><span class="nopadding userimg'.($morecss ? ' '.$morecss : '').'">'.Form::showphoto('contact', $this, 0, 0, 0, 'userphoto'.($withpicto == -3 ? 'small' : ''), 'mini', 0, 1).'</span>';
1542  if ($withpicto != 2 && $withpicto != -2) {
1543  $result .= ' ';
1544  }
1545  } else {
1546  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="pictofixedwidth valignmiddle"' : '') : 'class="'.(($withpicto != 2) ? 'pictofixedwidth valignmiddle' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1547  }
1548  }
1549  if ($withpicto != 2 && $withpicto != -2) {
1550  $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1551  }
1552 
1553  $result .= $linkend;
1554 
1555  global $action;
1556  $hookmanager->initHooks(array('contactdao'));
1557  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1558  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1559  if ($reshook > 0) {
1560  $result = $hookmanager->resPrint;
1561  } else {
1562  $result .= $hookmanager->resPrint;
1563  }
1564 
1565  return $result;
1566  }
1567 
1573  public function getCivilityLabel()
1574  {
1575  global $langs;
1576 
1577  $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1578  if (empty($code)) {
1579  return '';
1580  }
1581 
1582  $langs->load("dict");
1583  return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1584  }
1585 
1592  public function getLibStatut($mode)
1593  {
1594  return $this->LibStatut($this->statut, $mode);
1595  }
1596 
1597  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1605  public function LibStatut($status, $mode)
1606  {
1607  // phpcs:enable
1608  global $langs;
1609 
1610  $labelStatus = array(
1611  0 => 'ActivityCeased',
1612  1 => 'InActivity',
1613  4 => 'InActivity',
1614  5 => 'ActivityCeased',
1615  );
1616  $labelStatusShort = array(
1617  0 => 'ActivityCeased',
1618  1 => 'InActivity',
1619  4 => 'InActivity',
1620  5 => 'ActivityCeased',
1621  );
1622 
1623  $statusType = 'status4';
1624  if ($status == 0 || $status == 5) {
1625  $statusType = 'status5';
1626  }
1627 
1628  $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1629  $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1630 
1631  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1632  }
1633 
1634 
1635  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1642  public function LibPubPriv($status)
1643  {
1644  // phpcs:enable
1645  global $langs;
1646  if ($status == '1') {
1647  return $langs->trans('ContactPrivate');
1648  } else {
1649  return $langs->trans('ContactPublic');
1650  }
1651  }
1652 
1653 
1661  public function initAsSpecimen()
1662  {
1663  // Get first id of existing company and save it into $socid
1664  $socid = 0;
1665  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1666  $resql = $this->db->query($sql);
1667  if ($resql) {
1668  $obj = $this->db->fetch_object($resql);
1669  if ($obj) {
1670  $socid = $obj->rowid;
1671  }
1672  }
1673 
1674  // Initialise parameters
1675  $this->id = 0;
1676  $this->entity = 1;
1677  $this->specimen = 1;
1678  $this->lastname = 'DOLIBARR';
1679  $this->firstname = 'SPECIMEN';
1680  $this->address = '21 jump street';
1681  $this->zip = '99999';
1682  $this->town = 'MyTown';
1683  $this->country_id = 1;
1684  $this->country_code = 'FR';
1685  $this->country = 'France';
1686  $this->email = 'specimen@specimen.com';
1687  $this->socialnetworks = array(
1688  'skype' => 'tom.hanson',
1689  'twitter' => 'tomhanson',
1690  'linkedin' => 'tomhanson',
1691  );
1692  $this->phone_pro = '0909090901';
1693  $this->phone_perso = '0909090902';
1694  $this->phone_mobile = '0909090903';
1695  $this->fax = '0909090909';
1696 
1697  $this->note_public = 'This is a comment (public)';
1698  $this->note_private = 'This is a comment (private)';
1699 
1700  $this->socid = $socid;
1701  $this->statut = 1;
1702 
1703  return 1;
1704  }
1705 
1712  public function setstatus($status)
1713  {
1714  global $conf, $langs, $user;
1715 
1716  $error = 0;
1717 
1718  // Check parameters
1719  if ($this->statut == $status) {
1720  return 0;
1721  } else {
1722  $this->statut = $status;
1723  }
1724 
1725  $this->db->begin();
1726 
1727  // User disable
1728  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1729  $sql .= " SET statut = ".((int) $this->statut);
1730  $sql .= ", fk_user_modif = ".((int) $user->id);
1731  $sql .= " WHERE rowid = ".((int) $this->id);
1732  $result = $this->db->query($sql);
1733 
1734  dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1735  if ($result) {
1736  // Call trigger
1737  $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1738  if ($result < 0) {
1739  $error++;
1740  }
1741  // End call triggers
1742  }
1743 
1744  if ($error) {
1745  $this->db->rollback();
1746  return -$error;
1747  } else {
1748  $this->db->commit();
1749  return 1;
1750  }
1751  }
1752 
1763  public function setCategories($categories)
1764  {
1765  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1766  return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1767  }
1768 
1777  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1778  {
1779  $tables = array(
1780  'socpeople', 'societe_contacts'
1781  );
1782 
1783  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1784  }
1785 
1793  public function fetchRoles()
1794  {
1795  global $langs;
1796  $error = 0;
1797  $num = 0;
1798 
1799  $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1800  $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1801  $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1802  $sql .= " AND tc.source = 'external' AND tc.active=1";
1803  $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1804  $sql .= " AND sc.entity IN (".getEntity('societe').')';
1805 
1806  $resql = $this->db->query($sql);
1807  if ($resql) {
1808  $this->roles = array();
1809 
1810  $num = $this->db->num_rows($resql);
1811  if ($num > 0) {
1812  while ($obj = $this->db->fetch_object($resql)) {
1813  $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1814  $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1815  $this->roles[$obj->contactroleid] = array('id' => $obj->rowid, 'socid' => $obj->socid, 'element' => $obj->element, 'source' => $obj->source, 'code' => $obj->code, 'label' => $libelle_element.' - '.($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->label));
1816  }
1817  }
1818  } else {
1819  $error++;
1820  $this->error = $this->db->lasterror();
1821  $this->errors[] = $this->db->lasterror();
1822  }
1823 
1824  if (empty($error)) {
1825  return $num;
1826  } else {
1827  return $error * -1;
1828  }
1829  }
1830 
1838  public function getContactRoles($element = '')
1839  {
1840  $tab = array();
1841 
1842  if ($element == 'action') {
1843  $element = 'agenda';
1844  }
1845 
1846  $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1847  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1848  $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1849  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1850  $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1851  $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1852  $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1853  $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1854  $sql .= " AND sp.entity IN (".getEntity('contact').")";
1855  $sql .= " AND tc.active = 1";
1856 
1857  dol_syslog(__METHOD__, LOG_DEBUG);
1858  $resql = $this->db->query($sql);
1859  if ($resql) {
1860  $num = $this->db->num_rows($resql);
1861  $i = 0;
1862  while ($i < $num) {
1863  $obj = $this->db->fetch_object($resql);
1864  $tab[] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1865 
1866  $i++;
1867  }
1868 
1869  return $tab;
1870  } else {
1871  $this->error = $this->db->error();
1872  dol_print_error($this->db);
1873  return -1;
1874  }
1875  }
1876 
1884  public function updateRoles()
1885  {
1886  global $conf;
1887 
1888  $error = 0;
1889 
1890  if (!isset($this->roles)) {
1891  return 0; // Avoid to loose roles when property not set
1892  }
1893 
1894  $this->db->begin();
1895 
1896  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1897 
1898  $result = $this->db->query($sql);
1899  if (!$result) {
1900  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1901  $error++;
1902  } else {
1903  if (count($this->roles) > 0) {
1904  foreach ($this->roles as $keyRoles => $valRoles) {
1905  $idrole = 0;
1906  if (is_array($valRoles)) {
1907  $idrole = $valRoles['id'];
1908  } else {
1909  $idrole = $valRoles;
1910  }
1911 
1912  $socid = 0;
1913  if (is_array($valRoles)) {
1914  $socid = $valRoles['socid'];
1915  } else {
1916  $socid = $this->socid;
1917  }
1918 
1919  if ($socid > 0) {
1920  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1921  $sql .= " (entity,";
1922  $sql .= "date_creation,";
1923  $sql .= "fk_soc,";
1924  $sql .= "fk_c_type_contact,";
1925  $sql .= "fk_socpeople) ";
1926  $sql .= " VALUES (".$conf->entity.",";
1927  $sql .= "'".$this->db->idate(dol_now())."',";
1928  $sql .= $socid.", ";
1929  $sql .= $idrole." , ";
1930  $sql .= $this->id;
1931  $sql .= ")";
1932 
1933  $result = $this->db->query($sql);
1934  if (!$result) {
1935  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1936  $error++;
1937  }
1938  }
1939  }
1940  }
1941  }
1942  if (empty($error)) {
1943  $this->db->commit();
1944  return 1;
1945  } else {
1946  $this->error = implode(' ', $this->errors);
1947  $this->db->rollback();
1948  return $error * -1;
1949  }
1950  }
1951 
1958  public function loadCacheOfProspStatus($active = 1)
1959  {
1960  global $langs;
1961 
1962  $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
1963  if ($active >= 0) {
1964  $sql .= " WHERE active = ".((int) $active);
1965  }
1966  $resql = $this->db->query($sql);
1967  $num = $this->db->num_rows($resql);
1968  $i = 0;
1969  while ($i < $num) {
1970  $obj = $this->db->fetch_object($resql);
1971  $this->cacheprospectstatus[$obj->id] = array('id' => $obj->id, 'code' => $obj->code, 'label' => ($langs->trans("ST_".strtoupper($obj->code)) == "ST_".strtoupper($obj->code)) ? $obj->label : $langs->trans("ST_".strtoupper($obj->code)), 'picto' => $obj->picto);
1972  $i++;
1973  }
1974  return 1;
1975  }
1976 
1982  public function getLibProspLevel()
1983  {
1984  return $this->libProspLevel($this->fk_prospectlevel);
1985  }
1986 
1993  public function libProspLevel($fk_prospectlevel)
1994  {
1995  global $langs;
1996 
1997  $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
1998  // If lib not found in language file, we get label from cache/database
1999  if ($lib == "ProspectLevel".$fk_prospectlevel) {
2000  $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2001  }
2002  return $lib;
2003  }
2004 
2005 
2013  public function setProspectLevel(User $user)
2014  {
2015  return $this->update($this->id, $user);
2016  }
2017 
2025  public function getLibProspCommStatut($mode = 0, $label = '')
2026  {
2027  return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2028  }
2029 
2043  public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2044  {
2045  global $langs;
2046  $langs->load('customers');
2047 
2048  if ($mode == 2) {
2049  if ($statut == '-1' || $statut == 'ST_NO') {
2050  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2051  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2052  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2053  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2054  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2055  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2056  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2057  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2058  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2059  } else {
2060  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto).' '.(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label);
2061  }
2062  }
2063  if ($mode == 3) {
2064  if ($statut == '-1' || $statut == 'ST_NO') {
2065  return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2066  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2067  return img_action($langs->trans("StatusProspect0"), 0, $picto);
2068  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2069  return img_action($langs->trans("StatusProspect1"), 1, $picto);
2070  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2071  return img_action($langs->trans("StatusProspect2"), 2, $picto);
2072  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2073  return img_action($langs->trans("StatusProspect3"), 3, $picto);
2074  } else {
2075  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);
2076  }
2077  }
2078  if ($mode == 4) {
2079  if ($statut == '-1' || $statut == 'ST_NO') {
2080  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2081  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2082  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2083  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2084  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2085  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2086  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2087  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2088  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2089  } else {
2090  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto).' '.(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label);
2091  }
2092  }
2093 
2094  return "Error, mode/status not found";
2095  }
2096 
2097 
2104  public function setNoEmail($no_email)
2105  {
2106  $error = 0;
2107 
2108  // Update mass emailing flag into table mailing_unsubscribe
2109  if ($this->email) {
2110  $this->db->begin();
2111 
2112  if ($no_email) {
2113  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing', 0).") AND email = '".$this->db->escape($this->email)."'";
2114  $resql = $this->db->query($sql);
2115  if ($resql) {
2116  $obj = $this->db->fetch_object($resql);
2117  $noemail = $obj->nb;
2118  if (empty($noemail)) {
2119  $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_unsubscribe(email, entity, date_creat) VALUES ('".$this->db->escape($this->email)."', ".getEntity('mailing', 0).", '".$this->db->idate(dol_now())."')";
2120  $resql = $this->db->query($sql);
2121  if (!$resql) {
2122  $error++;
2123  $this->error = $this->db->lasterror();
2124  $this->errors[] = $this->error;
2125  }
2126  }
2127  } else {
2128  $error++;
2129  $this->error = $this->db->lasterror();
2130  $this->errors[] = $this->error;
2131  }
2132  } else {
2133  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2134  $resql = $this->db->query($sql);
2135  if (!$resql) {
2136  $error++;
2137  $this->error = $this->db->lasterror();
2138  $this->errors[] = $this->error;
2139  }
2140  }
2141 
2142  if (empty($error)) {
2143  $this->no_email = $no_email;
2144  $this->db->commit();
2145  return 1;
2146  } else {
2147  $this->db->rollback();
2148  return $error * -1;
2149  }
2150  }
2151 
2152  return 0;
2153  }
2154 
2161  public function getNoEmail()
2162  {
2163  if ($this->email) {
2164  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2165  $resql = $this->db->query($sql);
2166  if ($resql) {
2167  $obj = $this->db->fetch_object($resql);
2168  $this->no_email = $obj->nb;
2169  return 1;
2170  } else {
2171  $this->error = $this->db->lasterror();
2172  $this->errors[] = $this->error;
2173  return -1;
2174  }
2175  }
2176  return 0;
2177  }
2178 
2179 
2187  public function getKanbanView($option = '', $arraydata = null)
2188  {
2189  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2190 
2191  $return = '<div class="box-flex-item box-flex-grow-zero">';
2192  $return .= '<div class="info-box info-box-sm">';
2193  $return .= '<span class="info-box-icon bg-infobox-action">';
2194  //var_dump($this->photo);exit;
2195  if (property_exists($this, 'photo') && !is_null($this->photo)) {
2196  $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2197  } else {
2198  $return .= img_picto('', $this->picto);
2199  }
2200  $return .= '</span>';
2201  $return .= '<div class="info-box-content">';
2202  $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</div>';
2203  if ($selected >= 0) {
2204  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2205  }
2206  if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2207  $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2208  }
2209  /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2210  $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2211  $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2212  }*/
2213  /*if (method_exists($this, 'LibPubPriv')) {
2214  $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2215  $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2216  }*/
2217  if (method_exists($this, 'getLibStatut')) {
2218  $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2219  }
2220  $return .= '</div>';
2221  $return .= '</div>';
2222  $return .= '</div>';
2223  return $return;
2224  }
2225 }
$object ref
Definition: info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage contact/addresses.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
__construct($db)
Constructor.
update_perso($id, $user=null, $notrigger=0)
Update field alert birthday.
getLibProspCommStatut($mode=0, $label='')
Return status of prospect.
create($user, $notrigger=0)
Add a contact into database.
loadStateBoard()
Load indicators into this->nb for board.
getNomUrl($withpicto=0, $option='', $maxlen=0, $moreparam='', $save_lastsearch_value=-1, $notooltip=0, $morecss='')
Return name of contact with link (and eventually picto) Use $this->id, $this->lastname,...
getNoEmail()
get "blacklist" mailing status set no_email attribute to 1 or 0
getNbOfEMailings()
Return number of mass Emailing received by these contacts with its email.
libProspCommStatut($statut, $mode=0, $label='', $picto='')
Return label of a given status.
getTooltipContentArray($params)
getTooltipContentArray
libProspLevel($fk_prospectlevel)
Return label of prospect level.
getCivilityLabel()
Return civility label of contact.
fetch($id, $user=null, $ref_ext='', $email='', $loadalsoroles=0)
Load object contact.
setProspectLevel(User $user)
Set prospect level.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
load_ref_elements()
Load number of elements the contact is used as a link for ref_facturation ref_contrat ref_commande (f...
getLibProspLevel()
Return prospect level.
fetchRoles()
Fetch roles (default contact of some companies) for the current contact.
getContactRoles($element='')
Get thirdparty contact roles of a given contact.
_load_ldap_dn($info, $mode=0)
Return DN string complete in the LDAP directory for the object.
LibStatut($status, $mode)
Return the label of a given status.
setGenderFromCivility()
Set the property "gender" of this class, based on the property "civility_id" or use property "civilit...
update($id, $user=null, $notrigger=0, $action='update', $nosyncuser=0)
Update information into database.
setstatus($status)
Change status of a user.
loadCacheOfProspStatus($active=1)
Load array of prospect status.
_load_ldap_info()
Initialize info table (LDAP attributes table)
updateRoles()
Updates all roles (default contact for companies) according to values inside the ->roles array.
info($id)
Load contact information from the database.
initAsSpecimen()
Initialise an instance with random values.
getLibStatut($mode)
Return the label of the status.
setNoEmail($no_email)
Set "blacklist" mailing status.
LibPubPriv($status)
Return translated label of Public or Private.
setCategories($categories)
Sets object to supplied categories.
Class to manage Dolibarr database access.
static showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='', $addlinktofullsize=1, $cache=0, $forcecapture='', $noexternsourceoverwrite=0)
Return HTML code to output a photo.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:50
trait CommonPeople
Support class for thirdparties, contacts, members, users or resources.
getFullName($langs, $option=0, $nameorder=-1, $maxlen=0)
Return full name (civility+' '+name+' '+lastname)
setUpperOrLowerCase()
Set to upper or ucwords/lower if needed.
trait CommonSocialNetworks
Superclass for social networks.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_format_address($object, $withcountry=0, $sep="\n", $outputlangs=null, $mode=0, $extralangcode='')
Return a formatted address (part address/zip/town/state) according to country rules.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
img_action($titlealt, $numaction, $picto='', $moreatt='')
Show logo action.
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0)
Format phone numbers according to country.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:125