dolibarr  19.0.0-dev
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  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program. If not, see <https://www.gnu.org/licenses/>.
27  */
28 
34 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/class/commonsocialnetworks.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
37 
38 
42 class Contact extends CommonObject
43 {
45  use CommonPeople;
46 
50  public $element = 'contact';
51 
55  public $table_element = 'socpeople';
56 
61  public $ismultientitymanaged = 1;
62 
66  public $picto = 'contact';
67 
92  // BEGIN MODULEBUILDER PROPERTIES
96  public $fields = array(
97  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
98  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>3, 'notnull'=>1, 'position'=>30, 'index'=>1),
99  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>3, 'position'=>35),
100  'civility' =>array('type'=>'varchar(6)', 'label'=>'Civility', 'enabled'=>1, 'visible'=>3, 'position'=>40),
101  'lastname' =>array('type'=>'varchar(50)', 'label'=>'Lastname', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'showoncombobox'=>1, 'searchall'=>1),
102  'firstname' =>array('type'=>'varchar(50)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'showoncombobox'=>1, 'searchall'=>1),
103  'poste' =>array('type'=>'varchar(80)', 'label'=>'PostOrFunction', 'enabled'=>1, 'visible'=>-1, 'position'=>52),
104  'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
105  'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>1, 'position'=>60),
106  'town' =>array('type'=>'varchar(50)', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
107  'fk_departement' =>array('type'=>'integer', 'label'=>'Fk departement', 'enabled'=>1, 'visible'=>3, 'position'=>70),
108  'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>3, 'position'=>75),
109  'fk_soc' =>array('type'=>'integer', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>1, 'position'=>77, 'searchall'=>1),
110  'birthday' =>array('type'=>'date', 'label'=>'Birthday', 'enabled'=>1, 'visible'=>3, 'position'=>80),
111  'phone' =>array('type'=>'varchar(30)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>1, 'position'=>90, 'searchall'=>1),
112  'phone_perso' =>array('type'=>'varchar(30)', 'label'=>'PhonePerso', 'enabled'=>1, 'visible'=>-1, 'position'=>95, 'searchall'=>1),
113  'phone_mobile' =>array('type'=>'varchar(30)', 'label'=>'PhoneMobile', 'enabled'=>1, 'visible'=>1, 'position'=>100, 'searchall'=>1),
114  'fax' =>array('type'=>'varchar(30)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'searchall'=>1),
115  'email' =>array('type'=>'varchar(255)', 'label'=>'Email', 'enabled'=>1, 'visible'=>1, 'position'=>110, 'searchall'=>1),
116  'socialnetworks' =>array('type'=>'text', 'label'=>'SocialNetworks', 'enabled'=>1, 'visible'=>3, 'position'=>115),
117  'photo' =>array('type'=>'varchar(255)', 'label'=>'Photo', 'enabled'=>1, 'visible'=>3, 'position'=>170),
118  'priv' =>array('type'=>'smallint(6)', 'label'=>'ContactVisibility', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>175),
119  'fk_stcommcontact' =>array('type'=>'integer', 'label'=>'ProspectStatus', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>220),
120  'fk_prospectlevel' =>array('type'=>'varchar(12)', 'label'=>'ProspectLevel', 'enabled'=>1, 'visible'=>-1, 'position'=>255),
121  'no_email' =>array('type'=>'smallint(6)', 'label'=>'No_Email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),
122  'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>3, 'position'=>195, 'searchall'=>1),
123  'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>3, 'position'=>200, 'searchall'=>1),
124  'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>3, 'position'=>205),
125  'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>3, 'position'=>210),
126  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>300),
127  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>305),
128  'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>3, 'position'=>310),
129  'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>3, 'position'=>315),
130  'statut' =>array('type'=>'tinyint(4)', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),
131  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'position'=>1000),
132  );
133 
134  public $civility_id; // In fact we store civility_code
135  public $civility_code;
136  public $civility;
137 
141  public $gender;
142 
146  public $egroupware_id;
147 
151  public $birthday_alert;
152 
158  public $civilite;
159 
163  public $fullname;
164 
168  public $address;
169 
173  public $zip;
174 
178  public $town;
179 
183  public $state_id;
184 
188  public $state_code;
189 
193  public $state;
194 
195  public $poste; // Position
196 
200  public $socid; // both socid and fk_soc are used
201  public $fk_soc; // both socid and fk_soc are used
202 
206  public $socname;
207 
211  public $statut;
212 
213  public $code;
214 
219  public $email;
220 
227  public $mail;
228 
233  public $url;
234 
239  public $no_email;
240 
244  public $socialnetworks;
245 
251  public $skype;
252 
258  public $twitter;
259 
265  public $facebook;
266 
272  public $linkedin;
273 
279  public $jabberid;
280 
284  public $photo;
285 
289  public $phone_pro;
290 
294  public $phone_perso;
295 
299  public $phone_mobile;
300 
304  public $fax;
305 
310  public $priv;
311 
312  public $birthday;
313  public $default_lang;
314 
318  public $ref_facturation;
319 
323  public $ref_contrat;
324 
328  public $ref_commande;
329 
333  public $ref_propal;
334 
338  public $user_id;
339 
343  public $user_login;
344 
345  // END MODULEBUILDER PROPERTIES
346 
347 
352  public $oldcopy; // To contains a clone of this when we need to save old properties of object
353 
357  public $roles;
358 
359  public $cacheprospectstatus = array();
360  public $fk_prospectlevel;
361  public $stcomm_id;
362  public $statut_commercial;
363 
367  public $stcomm_picto;
368 
369 
375  public function __construct($db)
376  {
377  global $conf, $langs;
378 
379  $this->db = $db;
380  $this->statut = 1; // By default, status is enabled
381 
382  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
383  $this->fields['rowid']['visible'] = 0;
384  }
385  if (!isModEnabled('mailing')) {
386  $this->fields['no_email']['enabled'] = 0;
387  }
388  // typical ['s.nom'] is used for third-parties
389  if (empty($conf->global->SOCIETE_DISABLE_CONTACTS)) {
390  $this->fields['fk_soc']['enabled'] = 0;
391  $this->fields['fk_soc']['searchall'] = 0;
392  }
393 
394  if (!empty($conf->global->SOCIETE_DISABLE_PROSPECTS) || empty($conf->global->THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES)) { // Default behaviour
395  $this->fields['fk_stcommcontact']['enabled'] = 0;
396  $this->fields['fk_prospectlevel']['enabled'] = 0;
397  }
398 
399  // Unset fields that are disabled
400  foreach ($this->fields as $key => $val) {
401  if (isset($val['enabled']) && empty($val['enabled'])) {
402  unset($this->fields[$key]);
403  }
404  }
405 
406  // Translate some data of arrayofkeyval
407  /*if (is_object($langs))
408  {
409  foreach($this->fields as $key => $val)
410  {
411  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
412  {
413  foreach($val['arrayofkeyval'] as $key2 => $val2)
414  {
415  $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
416  }
417  }
418  }
419  }*/
420  }
421 
422  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
428  public function load_state_board()
429  {
430  // phpcs:enable
431  global $user, $hookmanager;
432 
433  $this->nb = array();
434  $clause = "WHERE";
435 
436  $sql = "SELECT count(sp.rowid) as nb";
437  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
438  if (empty($user->rights->societe->client->voir) && !$user->socid) {
439  $sql .= ", ".MAIN_DB_PREFIX."societe as s";
440  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
441  $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
442  $clause = "AND";
443  }
444  $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
445  $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
446  if ($user->socid > 0) {
447  $sql .= " AND sp.fk_soc = ".((int) $user->socid);
448  }
449  // Add where from hooks
450  if (is_object($hookmanager)) {
451  $parameters = array();
452  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
453  $sql .= $hookmanager->resPrint;
454  }
455 
456  $resql = $this->db->query($sql);
457  if ($resql) {
458  while ($obj = $this->db->fetch_object($resql)) {
459  $this->nb["contacts"] = $obj->nb;
460  }
461  $this->db->free($resql);
462  return 1;
463  } else {
464  dol_print_error($this->db);
465  $this->error = $this->db->lasterror();
466  return -1;
467  }
468  }
469 
477  public function create($user, $notrigger = 0)
478  {
479  global $conf;
480 
481  $error = 0;
482  $now = dol_now();
483 
484  $this->db->begin();
485 
486  // Clean parameters
487  $this->lastname = $this->lastname ?trim($this->lastname) : trim($this->name);
488  $this->firstname = trim($this->firstname);
489  $this->setUpperOrLowerCase();
490  if (empty($this->socid)) {
491  $this->socid = 0;
492  }
493  if (empty($this->priv)) {
494  $this->priv = 0;
495  }
496  if (empty($this->statut)) {
497  $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
498  }
499 
500  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
501 
502  $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
503  $sql .= " datec";
504  $sql .= ", fk_soc";
505  $sql .= ", lastname";
506  $sql .= ", firstname";
507  $sql .= ", fk_user_creat";
508  $sql .= ", priv";
509  $sql .= ", fk_stcommcontact";
510  $sql .= ", statut";
511  $sql .= ", canvas";
512  $sql .= ", entity";
513  $sql .= ", ref_ext";
514  $sql .= ", import_key";
515  $sql .= ") VALUES (";
516  $sql .= "'".$this->db->idate($now)."',";
517  if ($this->socid > 0) {
518  $sql .= " ".((int) $this->socid).",";
519  } else {
520  $sql .= "null,";
521  }
522  $sql .= "'".$this->db->escape($this->lastname)."',";
523  $sql .= "'".$this->db->escape($this->firstname)."',";
524  $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
525  $sql .= " ".((int) $this->priv).",";
526  $sql .= " 0,";
527  $sql .= " ".((int) $this->statut).",";
528  $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
529  $sql .= " ".((int) $this->entity).",";
530  $sql .= "'".$this->db->escape($this->ref_ext)."',";
531  $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
532  $sql .= ")";
533 
534  dol_syslog(get_class($this)."::create", LOG_DEBUG);
535  $resql = $this->db->query($sql);
536  if ($resql) {
537  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
538 
539  if (!$error) {
540  $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
541  if ($result < 0) {
542  $error++;
543  $this->error = $this->db->lasterror();
544  }
545  }
546 
547  if (!$error) {
548  $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
549  if ($result < 0) {
550  $error++;
551  $this->error = $this->db->lasterror();
552  }
553  }
554 
555  if (!$error && !$notrigger) {
556  // Call trigger
557  $result = $this->call_trigger('CONTACT_CREATE', $user);
558  if ($result < 0) {
559  $error++;
560  }
561  // End call triggers
562  }
563 
564  if (!$error) {
565  $this->db->commit();
566  return $this->id;
567  } else {
568  $this->db->rollback();
569  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
570  return -2;
571  }
572  } else {
573  $this->error = $this->db->lasterror();
574 
575  $this->db->rollback();
576  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
577  return -1;
578  }
579  }
580 
591  public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
592  {
593  global $conf, $langs, $hookmanager;
594 
595  $error = 0;
596 
597  $this->id = $id;
598 
599  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
600 
601  // Clean parameters
602  $this->ref_ext = trim($this->ref_ext);
603  $this->lastname = trim($this->lastname) ?trim($this->lastname) : trim($this->lastname);
604  $this->firstname = trim($this->firstname);
605  $this->email = trim($this->email);
606  $this->phone_pro = trim($this->phone_pro);
607  $this->phone_perso = trim($this->phone_perso);
608  $this->phone_mobile = trim($this->phone_mobile);
609  $this->photo = trim($this->photo);
610  $this->fax = trim($this->fax);
611  $this->zip = (empty($this->zip) ? '' : trim($this->zip));
612  $this->town = (empty($this->town) ? '' : trim($this->town));
613  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
614  if (empty($this->statut)) {
615  $this->statut = 0;
616  }
617  if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
618  $this->civility_code = $this->civility_id; // For backward compatibility
619  }
620  $this->setUpperOrLowerCase();
621  $this->db->begin();
622 
623  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
624  if ($this->socid > 0) {
625  $sql .= " fk_soc = ".((int) $this->socid).",";
626  } elseif ($this->socid == -1) {
627  $sql .= " fk_soc = NULL,";
628  }
629  $sql .= " civility='".$this->db->escape($this->civility_code)."'";
630  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
631  $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
632  $sql .= ", address='".$this->db->escape($this->address)."'";
633  $sql .= ", zip='".$this->db->escape($this->zip)."'";
634  $sql .= ", town='".$this->db->escape($this->town)."'";
635  $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
636  $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
637  $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
638  $sql .= ", poste='".$this->db->escape($this->poste)."'";
639  $sql .= ", fax='".$this->db->escape($this->fax)."'";
640  $sql .= ", email='".$this->db->escape($this->email)."'";
641  $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
642  $sql .= ", photo='".$this->db->escape($this->photo)."'";
643  $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
644  $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
645  $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
646  $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
647  $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
648  $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
649  $sql .= ", priv = '".$this->db->escape($this->priv)."'";
650  $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
651  if (isset($this->stcomm_id)) {
652  $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
653  }
654  $sql .= ", statut = ".((int) $this->statut);
655  $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
656  $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
657  $sql .= ", entity = ".((int) $this->entity);
658  $sql .= " WHERE rowid = ".((int) $id);
659 
660  dol_syslog(get_class($this)."::update", LOG_DEBUG);
661  $result = $this->db->query($sql);
662  if ($result) {
663  unset($this->country_code);
664  unset($this->country);
665  unset($this->state_code);
666  unset($this->state);
667 
668  $action = 'update';
669 
670  // Actions on extra fields
671  if (!$error) {
672  $result = $this->insertExtraFields();
673  if ($result < 0) {
674  $error++;
675  }
676  }
677 
678  if (!$error) {
679  $result = $this->updateRoles();
680  if ($result < 0) {
681  $error++;
682  }
683  }
684 
685  if (!$error && $this->user_id > 0) {
686  // If contact is linked to a user
687  $tmpobj = new User($this->db);
688  $tmpobj->fetch($this->user_id);
689  $usermustbemodified = 0;
690  if ($tmpobj->office_phone != $this->phone_pro) {
691  $tmpobj->office_phone = $this->phone_pro;
692  $usermustbemodified++;
693  }
694  if ($tmpobj->office_fax != $this->fax) {
695  $tmpobj->office_fax = $this->fax;
696  $usermustbemodified++;
697  }
698  if ($tmpobj->address != $this->address) {
699  $tmpobj->address = $this->address;
700  $usermustbemodified++;
701  }
702  if ($tmpobj->town != $this->town) {
703  $tmpobj->town = $this->town;
704  $usermustbemodified++;
705  }
706  if ($tmpobj->zip != $this->zip) {
707  $tmpobj->zip = $this->zip;
708  $usermustbemodified++;
709  }
710  if ($tmpobj->zip != $this->zip) {
711  $tmpobj->state_id = $this->state_id;
712  $usermustbemodified++;
713  }
714  if ($tmpobj->country_id != $this->country_id) {
715  $tmpobj->country_id = $this->country_id;
716  $usermustbemodified++;
717  }
718  if ($tmpobj->email != $this->email) {
719  $tmpobj->email = $this->email;
720  $usermustbemodified++;
721  }
722  if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
723  $tmpobj->socialnetworks = $this->socialnetworks;
724  $usermustbemodified++;
725  }
726  if ($usermustbemodified) {
727  $result = $tmpobj->update($user, 0, 1, 1, 1);
728  if ($result < 0) {
729  $error++;
730  }
731  }
732  }
733 
734  if (!$error && !$notrigger) {
735  // Call trigger
736  $result = $this->call_trigger('CONTACT_MODIFY', $user);
737  if ($result < 0) {
738  $error++;
739  }
740  // End call triggers
741  }
742 
743  if (!$error) {
744  $this->db->commit();
745  return 1;
746  } else {
747  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
748  $this->db->rollback();
749  return -$error;
750  }
751  } else {
752  $this->error = $this->db->lasterror().' sql='.$sql;
753  $this->db->rollback();
754  return -1;
755  }
756  }
757 
758 
759  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
760  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
770  public function _load_ldap_dn($info, $mode = 0)
771  {
772  // phpcs:enable
773  global $conf;
774  $dn = '';
775  if ($mode == 0) {
776  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;
777  } elseif ($mode == 1) {
778  $dn = $conf->global->LDAP_CONTACT_DN;
779  } elseif ($mode == 2) {
780  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS];
781  }
782  return $dn;
783  }
784 
785 
786  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
787  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
793  public function _load_ldap_info()
794  {
795  // phpcs:enable
796  global $conf, $langs;
797 
798  $info = array();
799 
800  // Object classes
801  $info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);
802 
803  $this->fullname = $this->getFullName($langs);
804 
805  // Fields
806  if ($this->fullname && !empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME)) {
807  $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
808  }
809  if ($this->lastname && !empty($conf->global->LDAP_CONTACT_FIELD_NAME)) {
810  $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname;
811  }
812  if ($this->firstname && !empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME)) {
813  $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
814  }
815 
816  if ($this->poste) {
817  $info["title"] = $this->poste;
818  }
819  if ($this->socid > 0) {
820  $soc = new Societe($this->db);
821  $soc->fetch($this->socid);
822 
823  $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->name;
824  if ($soc->client == 1) {
825  $info["businessCategory"] = "Customers";
826  }
827  if ($soc->client == 2) {
828  $info["businessCategory"] = "Prospects";
829  }
830  if ($soc->fournisseur == 1) {
831  $info["businessCategory"] = "Suppliers";
832  }
833  }
834  if ($this->address && !empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS)) {
835  $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
836  }
837  if ($this->zip && !empty($conf->global->LDAP_CONTACT_FIELD_ZIP)) {
838  $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->zip;
839  }
840  if ($this->town && !empty($conf->global->LDAP_CONTACT_FIELD_TOWN)) {
841  $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->town;
842  }
843  if ($this->country_code && !empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY)) {
844  $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code;
845  }
846  if ($this->phone_pro && !empty($conf->global->LDAP_CONTACT_FIELD_PHONE)) {
847  $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
848  }
849  if ($this->phone_perso && !empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) {
850  $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
851  }
852  if ($this->phone_mobile && !empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) {
853  $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
854  }
855  if ($this->fax && !empty($conf->global->LDAP_CONTACT_FIELD_FAX)) {
856  $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
857  }
858  if ($this->note_private && !empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) {
859  $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
860  }
861  if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL)) {
862  $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
863  }
864 
865  if ($conf->global->LDAP_SERVER_TYPE == 'egroupware') {
866  $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
867 
868  $info['uidnumber'] = $this->id;
869 
870  $info['phpgwTz'] = 0;
871  $info['phpgwMailType'] = 'INTERNET';
872  $info['phpgwMailHomeType'] = 'INTERNET';
873 
874  $info["phpgwContactTypeId"] = 'n';
875  $info["phpgwContactCatId"] = 0;
876  $info["phpgwContactAccess"] = "public";
877 
878  if (dol_strlen($this->egroupware_id) == 0) {
879  $this->egroupware_id = 1;
880  }
881 
882  $info["phpgwContactOwner"] = $this->egroupware_id;
883 
884  if ($this->email) {
885  $info["rfc822Mailbox"] = $this->email;
886  }
887  if ($this->phone_mobile) {
888  $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
889  }
890  }
891 
892  return $info;
893  }
894 
895 
896  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
905  public function update_perso($id, $user = null, $notrigger = 0)
906  {
907  // phpcs:enable
908  $error = 0;
909  $result = false;
910 
911  $this->db->begin();
912 
913  // Mis a jour contact
914  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
915  $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
916  $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
917  if ($user) {
918  $sql .= ", fk_user_modif = ".((int) $user->id);
919  }
920  $sql .= " WHERE rowid = ".((int) $id);
921 
922  dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
923  $resql = $this->db->query($sql);
924  if (!$resql) {
925  $error++;
926  $this->error = $this->db->lasterror();
927  }
928 
929  if ($user) {
930  // Update birthday alert
931  if (!empty($this->birthday_alert)) {
932  //check existing
933  $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
934  $result_check = $this->db->query($sql_check);
935  if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
936  //insert
937  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
938  $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
939  $result = $this->db->query($sql);
940  if (!$result) {
941  $error++;
942  $this->error = $this->db->lasterror();
943  }
944  } else {
945  $result = true;
946  }
947  } else {
948  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
949  $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
950  $result = $this->db->query($sql);
951  if (!$result) {
952  $error++;
953  $this->error = $this->db->lasterror();
954  }
955  }
956  }
957 
958  if (!$error && !$notrigger) {
959  // Call trigger
960  $result = $this->call_trigger('CONTACT_MODIFY', $user);
961  if ($result < 0) {
962  $error++;
963  }
964  // End call triggers
965  }
966 
967  if (!$error) {
968  $this->db->commit();
969  return 1;
970  } else {
971  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
972  $this->db->rollback();
973  return -$error;
974  }
975  }
976 
977 
988  public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
989  {
990  global $langs;
991 
992  dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
993 
994  if (empty($id) && empty($ref_ext) && empty($email)) {
995  $this->error = 'BadParameter';
996  return -1;
997  }
998 
999  $langs->loadLangs(array("dict", "companies"));
1000 
1001  $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.lastname, c.firstname,";
1002  $sql .= " c.address, c.statut, c.zip, c.town,";
1003  $sql .= " c.fk_pays as country_id,";
1004  $sql .= " c.fk_departement as state_id,";
1005  $sql .= " c.birthday,";
1006  $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1007  $sql .= " c.socialnetworks,";
1008  $sql .= " c.photo,";
1009  $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1010  $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1011  $sql .= " c.import_key,";
1012  $sql .= " c.datec as date_creation, c.tms as date_modification,";
1013  $sql .= " co.label as country, co.code as country_code,";
1014  $sql .= " d.nom as state, d.code_departement as state_code,";
1015  $sql .= " u.rowid as user_id, u.login as user_login,";
1016  $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1017  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1018  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1019  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1020  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1021  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1022  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1023  if ($id) {
1024  $sql .= " WHERE c.rowid = ".((int) $id);
1025  } else {
1026  $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1027  if ($ref_ext) {
1028  $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1029  }
1030  if ($email) {
1031  $sql .= " AND c.email = '".$this->db->escape($email)."'";
1032  }
1033  }
1034 
1035  $resql = $this->db->query($sql);
1036  if ($resql) {
1037  $num = $this->db->num_rows($resql);
1038  if ($num > 1) {
1039  $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1040  dol_syslog($this->error, LOG_ERR);
1041 
1042  return 2;
1043  } elseif ($num) { // $num = 1
1044  $obj = $this->db->fetch_object($resql);
1045 
1046  $this->id = $obj->rowid;
1047  $this->entity = $obj->entity;
1048  $this->ref = $obj->rowid;
1049  $this->ref_ext = $obj->ref_ext;
1050 
1051  $this->civility_code = $obj->civility_code;
1052  $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1053 
1054  $this->lastname = $obj->lastname;
1055  $this->firstname = $obj->firstname;
1056  $this->address = $obj->address;
1057  $this->zip = $obj->zip;
1058  $this->town = $obj->town;
1059 
1060  $this->date_creation = $this->db->jdate($obj->date_creation);
1061  $this->date_modification = $this->db->jdate($obj->date_modification);
1062 
1063  $this->state_id = $obj->state_id;
1064  $this->state_code = $obj->state_code;
1065  $this->state = $obj->state;
1066 
1067  $this->country_id = $obj->country_id;
1068  $this->country_code = $obj->country_id ? $obj->country_code : '';
1069  $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1070 
1071  $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1072  $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1073  $this->socname = $obj->socname;
1074  $this->poste = $obj->poste;
1075  $this->statut = $obj->statut;
1076 
1077  $this->fk_prospectlevel = $obj->fk_prospectlevel;
1078 
1079  $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1080  $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1081  $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1082  $this->statut_commercial = $libelle; // libelle statut commercial
1083  $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1084 
1085  $this->phone_pro = trim($obj->phone);
1086  $this->fax = trim($obj->fax);
1087  $this->phone_perso = trim($obj->phone_perso);
1088  $this->phone_mobile = trim($obj->phone_mobile);
1089 
1090  $this->email = $obj->email;
1091  $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1092  $this->photo = $obj->photo;
1093  $this->priv = $obj->priv;
1094  $this->mail = $obj->email;
1095 
1096  $this->birthday = $this->db->jdate($obj->birthday);
1097  $this->note = $obj->note_private; // deprecated
1098  $this->note_private = $obj->note_private;
1099  $this->note_public = $obj->note_public;
1100  $this->default_lang = $obj->default_lang;
1101  $this->user_id = $obj->user_id;
1102  $this->user_login = $obj->user_login;
1103  $this->canvas = $obj->canvas;
1104 
1105  $this->import_key = $obj->import_key;
1106 
1107  // Define gender according to civility
1108  $this->setGenderFromCivility();
1109 
1110  // Search Dolibarr user linked to this contact
1111  $sql = "SELECT u.rowid ";
1112  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1113  $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1114 
1115  $resql = $this->db->query($sql);
1116  if ($resql) {
1117  if ($this->db->num_rows($resql)) {
1118  $uobj = $this->db->fetch_object($resql);
1119 
1120  $this->user_id = $uobj->rowid;
1121  }
1122  $this->db->free($resql);
1123  } else {
1124  $this->error = $this->db->error();
1125  return -1;
1126  }
1127 
1128  // Retrieve all extrafield
1129  // fetch optionals attributes and labels
1130  $this->fetch_optionals();
1131 
1132  // Load also alerts of this user
1133  if ($user) {
1134  $sql = "SELECT fk_user";
1135  $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1136  $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1137 
1138  $resql = $this->db->query($sql);
1139  if ($resql) {
1140  if ($this->db->num_rows($resql)) {
1141  $obj = $this->db->fetch_object($resql);
1142 
1143  $this->birthday_alert = 1;
1144  }
1145  $this->db->free($resql);
1146  } else {
1147  $this->error = $this->db->error();
1148  return -1;
1149  }
1150  }
1151 
1152  // Load also roles of this address
1153  if ($loadalsoroles) {
1154  $resultRole = $this->fetchRoles();
1155  if ($resultRole < 0) {
1156  return $resultRole;
1157  }
1158  }
1159 
1160  return 1;
1161  } else {
1162  $this->error = $langs->trans("RecordNotFound");
1163  return 0;
1164  }
1165  } else {
1166  $this->error = $this->db->error();
1167  return -1;
1168  }
1169  }
1170 
1171 
1172 
1179  public function setGenderFromCivility()
1180  {
1181  unset($this->gender);
1182 
1183  if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1184  $this->gender = 'man';
1185  } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1186  $this->gender = 'woman';
1187  }
1188  }
1189 
1190  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1200  public function load_ref_elements()
1201  {
1202  // phpcs:enable
1203  // Compte les elements pour lesquels il est contact
1204  $sql = "SELECT tc.element, count(ec.rowid) as nb";
1205  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1206  $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1207  $sql .= " AND fk_socpeople = ".((int) $this->id);
1208  $sql .= " AND tc.source = 'external'";
1209  $sql .= " GROUP BY tc.element";
1210 
1211  dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1212 
1213  $resql = $this->db->query($sql);
1214  if ($resql) {
1215  while ($obj = $this->db->fetch_object($resql)) {
1216  if ($obj->nb) {
1217  if ($obj->element == 'facture') {
1218  $this->ref_facturation = $obj->nb;
1219  } elseif ($obj->element == 'contrat') {
1220  $this->ref_contrat = $obj->nb;
1221  } elseif ($obj->element == 'commande') {
1222  $this->ref_commande = $obj->nb;
1223  } elseif ($obj->element == 'propal') {
1224  $this->ref_propal = $obj->nb;
1225  }
1226  }
1227  }
1228  $this->db->free($resql);
1229  return 0;
1230  } else {
1231  $this->error = $this->db->lasterror();
1232  return -1;
1233  }
1234  }
1235 
1243  public function delete($notrigger = 0)
1244  {
1245  global $conf, $langs, $user;
1246 
1247  $error = 0;
1248 
1249  $this->db->begin();
1250 
1251  if (!$error && !$notrigger) {
1252  // Call trigger
1253  $result = $this->call_trigger('CONTACT_DELETE', $user);
1254  if ($result < 0) {
1255  $error++;
1256  }
1257  // End call triggers
1258  }
1259 
1260  if (!$error) {
1261  // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1262  $sql = "SELECT ec.rowid";
1263  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1264  $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1265  $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1266  $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1267  $sql .= " AND tc.source='external'";
1268  dol_syslog(__METHOD__, LOG_DEBUG);
1269  $resql = $this->db->query($sql);
1270  if ($resql) {
1271  $num = $this->db->num_rows($resql);
1272 
1273  $i = 0;
1274  while ($i < $num && !$error) {
1275  $obj = $this->db->fetch_object($resql);
1276 
1277  $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1278  $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1279  dol_syslog(__METHOD__, LOG_DEBUG);
1280  $result = $this->db->query($sqldel);
1281  if (!$result) {
1282  $error++;
1283  $this->error = $this->db->error().' sql='.$sqldel;
1284  }
1285 
1286  $i++;
1287  }
1288  } else {
1289  $error++;
1290  $this->error = $this->db->error().' sql='.$sql;
1291  }
1292  }
1293 
1294  if (!$error) {
1295  // Remove Roles
1296  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1297  dol_syslog(__METHOD__, LOG_DEBUG);
1298  $resql = $this->db->query($sql);
1299  if (!$resql) {
1300  $error++;
1301  $this->error .= $this->db->lasterror();
1302  $errorflag = -1;
1303  }
1304  }
1305 
1306  if (!$error) {
1307  // Remove Roles
1308  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1309  dol_syslog(__METHOD__, LOG_DEBUG);
1310  $resql = $this->db->query($sql);
1311  if (!$resql) {
1312  $error++;
1313  $this->error .= $this->db->lasterror();
1314  $errorflag = -1;
1315  }
1316  }
1317 
1318  if (!$error) {
1319  // Remove category
1320  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1321  dol_syslog(__METHOD__, LOG_DEBUG);
1322  $resql = $this->db->query($sql);
1323  if (!$resql) {
1324  $error++;
1325  $this->error .= $this->db->lasterror();
1326  $errorflag = -1;
1327  }
1328  }
1329 
1330  if (!$error) {
1331  $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1332  $sql .= " WHERE rowid = ".((int) $this->id);
1333  dol_syslog(__METHOD__, LOG_DEBUG);
1334  $result = $this->db->query($sql);
1335  if (!$result) {
1336  $error++;
1337  $this->error = $this->db->error().' sql='.$sql;
1338  }
1339  }
1340 
1341  // Removed extrafields
1342  if (!$error) {
1343  // For avoid conflicts if trigger used
1344  $result = $this->deleteExtraFields();
1345  if ($result < 0) {
1346  $error++;
1347  }
1348  }
1349 
1350  if (!$error) {
1351  $this->db->commit();
1352  return 1;
1353  } else {
1354  $this->db->rollback();
1355  dol_syslog("Error ".$this->error, LOG_ERR);
1356  return -1;
1357  }
1358  }
1359 
1360 
1367  public function info($id)
1368  {
1369  $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1370  $sql .= " c.tms as tms, c.fk_user_modif";
1371  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1372  $sql .= " WHERE c.rowid = ".((int) $id);
1373 
1374  $resql = $this->db->query($sql);
1375  if ($resql) {
1376  if ($this->db->num_rows($resql)) {
1377  $obj = $this->db->fetch_object($resql);
1378 
1379  $this->id = $obj->rowid;
1380 
1381  if ($obj->fk_user_creat) {
1382  $cuser = new User($this->db);
1383  $cuser->fetch($obj->fk_user_creat);
1384  $this->user_creation = $cuser;
1385  }
1386 
1387  if ($obj->fk_user_modif) {
1388  $muser = new User($this->db);
1389  $muser->fetch($obj->fk_user_modif);
1390  $this->user_modification = $muser;
1391  }
1392 
1393  $this->date_creation = $this->db->jdate($obj->datec);
1394  $this->date_modification = $this->db->jdate($obj->tms);
1395  }
1396 
1397  $this->db->free($resql);
1398  } else {
1399  print $this->db->error();
1400  }
1401  }
1402 
1408  public function getNbOfEMailings()
1409  {
1410  $sql = "SELECT count(mc.email) as nb";
1411  $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1412  $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1413  $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1414 
1415  $resql = $this->db->query($sql);
1416  if ($resql) {
1417  $obj = $this->db->fetch_object($resql);
1418  $nb = $obj->nb;
1419 
1420  $this->db->free($resql);
1421  return $nb;
1422  } else {
1423  $this->error = $this->db->error();
1424  return -1;
1425  }
1426  }
1427 
1434  public function getTooltipContentArray($params)
1435  {
1436  global $conf, $langs, $user;
1437 
1438  $datas = [];
1439 
1440  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1441  return ['optimize' => $langs->trans("ShowContact")];
1442  }
1443  if (!empty($this->photo) && class_exists('Form')) {
1444  $photo = '<div class="photointooltip floatright">';
1445  $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.
1446  $photo .= '</div>';
1447  $datas['photo'] = $photo;
1448  }
1449 
1450  $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1451  $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1452  // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1453  if (!empty($this->poste)) {
1454  $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1455  }
1456  $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1457  $phonelist = array();
1458  $country_code = empty($this->country_code) ? '': $this->country_code;
1459  if ($this->phone_pro) {
1460  $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1461  }
1462  if ($this->phone_mobile) {
1463  $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1464  }
1465  if ($this->phone_perso) {
1466  $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1467  }
1468  $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1469  $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1470 
1471  return $datas;
1472  }
1473 
1487  public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1488  {
1489  global $conf, $langs, $hookmanager;
1490 
1491  if (!empty($conf->dol_no_mouse_hover)) {
1492  $notooltip = 1; // Force disable tooltips
1493  }
1494 
1495  $result = '';
1496  $params = [
1497  'id' => $this->id,
1498  'objecttype' => $this->element,
1499  'option' => $option,
1500  ];
1501  $classfortooltip = 'classfortooltip';
1502  $dataparams = '';
1503  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1504  $classfortooltip = 'classforajaxtooltip';
1505  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1506  $label = '';
1507  } else {
1508  $label = implode($this->getTooltipContentArray($params));
1509  }
1510 
1511  $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1512 
1513  if ($option !== 'nolink') {
1514  // Add param to save lastsearch_values or not
1515  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1516  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1517  $add_save_lastsearch_values = 1;
1518  }
1519  if ($url && $add_save_lastsearch_values) {
1520  $url .= '&save_lastsearch_values=1';
1521  }
1522  }
1523 
1524  $url .= $moreparam;
1525 
1526  $linkclose = '';
1527  if (empty($notooltip)) {
1528  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1529  $label = $langs->trans("ShowContact");
1530  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1531  }
1532  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1533  $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1534  } else {
1535  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1536  }
1537 
1538  if ($option == 'nolink' || empty($url)) {
1539  $linkstart = '<span';
1540  } else {
1541  $linkstart = '<a href="'.$url.'"';
1542  }
1543  $linkstart .= $linkclose.'>';
1544  if ($option == 'nolink' || empty($url)) {
1545  $linkend = '</span>';
1546  } else {
1547  $linkend = '</a>';
1548  }
1549 
1550  $result .= $linkstart;
1551 
1552  if ($withpicto) {
1553  if ($withpicto < 0) {
1554  $result .= '<!-- picto photo user --><span class="nopadding userimg'.($morecss ? ' '.$morecss : '').'">'.Form::showphoto('contact', $this, 0, 0, 0, 'userphoto'.($withpicto == -3 ? 'small' : ''), 'mini', 0, 1).'</span>';
1555  if ($withpicto != 2 && $withpicto != -2) {
1556  $result .= ' ';
1557  }
1558  } else {
1559  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="pictofixedwidth"' : '') : 'class="'.(($withpicto != 2) ? 'pictofixedwidth ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1560  }
1561  }
1562  if ($withpicto != 2 && $withpicto != -2) {
1563  $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1564  }
1565 
1566  $result .= $linkend;
1567 
1568  global $action;
1569  $hookmanager->initHooks(array('contactdao'));
1570  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1571  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1572  if ($reshook > 0) {
1573  $result = $hookmanager->resPrint;
1574  } else {
1575  $result .= $hookmanager->resPrint;
1576  }
1577 
1578  return $result;
1579  }
1580 
1586  public function getCivilityLabel()
1587  {
1588  global $langs;
1589 
1590  $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1591  if (empty($code)) {
1592  return '';
1593  }
1594 
1595  $langs->load("dict");
1596  return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1597  }
1598 
1605  public function getLibStatut($mode)
1606  {
1607  return $this->LibStatut($this->statut, $mode);
1608  }
1609 
1610  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1618  public function LibStatut($status, $mode)
1619  {
1620  // phpcs:enable
1621  global $langs;
1622 
1623  $labelStatus = array(
1624  0 => 'ActivityCeased',
1625  1 => 'InActivity',
1626  4 => 'InActivity',
1627  5 => 'ActivityCeased',
1628  );
1629  $labelStatusShort = array(
1630  0 => 'ActivityCeased',
1631  1 => 'InActivity',
1632  4 => 'InActivity',
1633  5 => 'ActivityCeased',
1634  );
1635 
1636  $statusType = 'status4';
1637  if ($status == 0 || $status == 5) {
1638  $statusType = 'status5';
1639  }
1640 
1641  $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1642  $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1643 
1644  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1645  }
1646 
1647 
1648  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1655  public function LibPubPriv($status)
1656  {
1657  // phpcs:enable
1658  global $langs;
1659  if ($status == '1') {
1660  return $langs->trans('ContactPrivate');
1661  } else {
1662  return $langs->trans('ContactPublic');
1663  }
1664  }
1665 
1666 
1674  public function initAsSpecimen()
1675  {
1676  // Get first id of existing company and save it into $socid
1677  $socid = 0;
1678  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1679  $resql = $this->db->query($sql);
1680  if ($resql) {
1681  $obj = $this->db->fetch_object($resql);
1682  if ($obj) {
1683  $socid = $obj->rowid;
1684  }
1685  }
1686 
1687  // Initialise parameters
1688  $this->id = 0;
1689  $this->entity = 1;
1690  $this->specimen = 1;
1691  $this->lastname = 'DOLIBARR';
1692  $this->firstname = 'SPECIMEN';
1693  $this->address = '21 jump street';
1694  $this->zip = '99999';
1695  $this->town = 'MyTown';
1696  $this->country_id = 1;
1697  $this->country_code = 'FR';
1698  $this->country = 'France';
1699  $this->email = 'specimen@specimen.com';
1700  $this->socialnetworks = array(
1701  'skype' => 'tom.hanson',
1702  );
1703  $this->phone_pro = '0909090901';
1704  $this->phone_perso = '0909090902';
1705  $this->phone_mobile = '0909090903';
1706  $this->fax = '0909090909';
1707 
1708  $this->note_public = 'This is a comment (public)';
1709  $this->note_private = 'This is a comment (private)';
1710 
1711  $this->socid = $socid;
1712  $this->statut = 1;
1713  return 1;
1714  }
1715 
1722  public function setstatus($status)
1723  {
1724  global $conf, $langs, $user;
1725 
1726  $error = 0;
1727 
1728  // Check parameters
1729  if ($this->statut == $status) {
1730  return 0;
1731  } else {
1732  $this->statut = $status;
1733  }
1734 
1735  $this->db->begin();
1736 
1737  // Desactive utilisateur
1738  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1739  $sql .= " SET statut = ".((int) $this->statut);
1740  $sql .= " WHERE rowid = ".((int) $this->id);
1741  $result = $this->db->query($sql);
1742 
1743  dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1744  if ($result) {
1745  // Call trigger
1746  $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1747  if ($result < 0) {
1748  $error++;
1749  }
1750  // End call triggers
1751  }
1752 
1753  if ($error) {
1754  $this->db->rollback();
1755  return -$error;
1756  } else {
1757  $this->db->commit();
1758  return 1;
1759  }
1760  }
1761 
1772  public function setCategories($categories)
1773  {
1774  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1775  return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1776  }
1777 
1786  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1787  {
1788  $tables = array(
1789  'socpeople', 'societe_contacts'
1790  );
1791 
1792  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1793  }
1794 
1802  public function fetchRoles()
1803  {
1804  global $langs;
1805  $error = 0;
1806  $num = 0;
1807 
1808  $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1809  $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1810  $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1811  $sql .= " AND tc.source = 'external' AND tc.active=1";
1812  $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1813  $sql .= " AND sc.entity IN (".getEntity('societe').')';
1814 
1815  $resql = $this->db->query($sql);
1816  if ($resql) {
1817  $this->roles = array();
1818 
1819  $num = $this->db->num_rows($resql);
1820  if ($num > 0) {
1821  while ($obj = $this->db->fetch_object($resql)) {
1822  $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1823  $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1824  $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));
1825  }
1826  }
1827  } else {
1828  $error++;
1829  $this->error = $this->db->lasterror();
1830  $this->errors[] = $this->db->lasterror();
1831  }
1832 
1833  if (empty($error)) {
1834  return $num;
1835  } else {
1836  return $error * -1;
1837  }
1838  }
1839 
1847  public function getContactRoles($element = '')
1848  {
1849  $tab = array();
1850 
1851  if ($element == 'action') {
1852  $element = 'agenda';
1853  }
1854 
1855  $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1856  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1857  $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1858  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1859  $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1860  $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1861  $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1862  $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1863  $sql .= " AND tc.active = 1";
1864 
1865  dol_syslog(__METHOD__, LOG_DEBUG);
1866  $resql = $this->db->query($sql);
1867  if ($resql) {
1868  $num = $this->db->num_rows($resql);
1869  $i = 0;
1870  while ($i < $num) {
1871  $obj = $this->db->fetch_object($resql);
1872  $tab[] = array('fk_socpeople'=>$obj->id, 'type_contact'=>$obj->fk_c_type_contact);
1873 
1874  $i++;
1875  }
1876 
1877  return $tab;
1878  } else {
1879  $this->error = $this->db->error();
1880  dol_print_error($this->db);
1881  return -1;
1882  }
1883  }
1884 
1892  public function updateRoles()
1893  {
1894  global $conf;
1895 
1896  $error = 0;
1897 
1898  if (!isset($this->roles)) {
1899  return 0; // Avoid to loose roles when property not set
1900  }
1901 
1902  $this->db->begin();
1903 
1904  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1905 
1906  $result = $this->db->query($sql);
1907  if (!$result) {
1908  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1909  $error++;
1910  } else {
1911  if (count($this->roles) > 0) {
1912  foreach ($this->roles as $keyRoles => $valRoles) {
1913  $idrole = 0;
1914  if (is_array($valRoles)) {
1915  $idrole = $valRoles['id'];
1916  } else {
1917  $idrole = $valRoles;
1918  }
1919 
1920  $socid = 0;
1921  if (is_array($valRoles)) {
1922  $socid = $valRoles['socid'];
1923  } else {
1924  $socid = $this->socid;
1925  }
1926 
1927  if ($socid > 0) {
1928  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1929  $sql .= " (entity,";
1930  $sql .= "date_creation,";
1931  $sql .= "fk_soc,";
1932  $sql .= "fk_c_type_contact,";
1933  $sql .= "fk_socpeople) ";
1934  $sql .= " VALUES (".$conf->entity.",";
1935  $sql .= "'".$this->db->idate(dol_now())."',";
1936  $sql .= $socid.", ";
1937  $sql .= $idrole." , ";
1938  $sql .= $this->id;
1939  $sql .= ")";
1940 
1941  $result = $this->db->query($sql);
1942  if (!$result) {
1943  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1944  $error++;
1945  }
1946  }
1947  }
1948  }
1949  }
1950  if (empty($error)) {
1951  $this->db->commit();
1952  return 1;
1953  } else {
1954  $this->error = implode(' ', $this->errors);
1955  $this->db->rollback();
1956  return $error * -1;
1957  }
1958  }
1959 
1966  public function loadCacheOfProspStatus($active = 1)
1967  {
1968  global $langs;
1969 
1970  $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
1971  if ($active >= 0) {
1972  $sql .= " WHERE active = ".((int) $active);
1973  }
1974  $resql = $this->db->query($sql);
1975  $num = $this->db->num_rows($resql);
1976  $i = 0;
1977  while ($i < $num) {
1978  $obj = $this->db->fetch_object($resql);
1979  $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);
1980  $i++;
1981  }
1982  return 1;
1983  }
1984 
1990  public function getLibProspLevel()
1991  {
1992  return $this->libProspLevel($this->fk_prospectlevel);
1993  }
1994 
2001  public function libProspLevel($fk_prospectlevel)
2002  {
2003  global $langs;
2004 
2005  $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2006  // If lib not found in language file, we get label from cache/databse
2007  if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel)) {
2008  $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2009  }
2010  return $lib;
2011  }
2012 
2013 
2021  public function setProspectLevel(User $user)
2022  {
2023  return $this->update($this->id, $user);
2024  }
2025 
2033  public function getLibProspCommStatut($mode = 0, $label = '')
2034  {
2035  return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2036  }
2037 
2051  public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2052  {
2053  global $langs;
2054  $langs->load('customers');
2055 
2056  if ($mode == 2) {
2057  if ($statut == '-1' || $statut == 'ST_NO') {
2058  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2059  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2060  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2061  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2062  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2063  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2064  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2065  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2066  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2067  } else {
2068  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);
2069  }
2070  }
2071  if ($mode == 3) {
2072  if ($statut == '-1' || $statut == 'ST_NO') {
2073  return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2074  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2075  return img_action($langs->trans("StatusProspect0"), 0, $picto);
2076  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2077  return img_action($langs->trans("StatusProspect1"), 1, $picto);
2078  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2079  return img_action($langs->trans("StatusProspect2"), 2, $picto);
2080  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2081  return img_action($langs->trans("StatusProspect3"), 3, $picto);
2082  } else {
2083  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);
2084  }
2085  }
2086  if ($mode == 4) {
2087  if ($statut == '-1' || $statut == 'ST_NO') {
2088  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2089  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2090  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2091  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2092  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2093  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2094  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2095  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2096  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2097  } else {
2098  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);
2099  }
2100  }
2101 
2102  return "Error, mode/status not found";
2103  }
2104 
2105 
2112  public function setNoEmail($no_email)
2113  {
2114  $error = 0;
2115 
2116  // Update mass emailing flag into table mailing_unsubscribe
2117  if ($this->email) {
2118  $this->db->begin();
2119 
2120  if ($no_email) {
2121  $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)."'";
2122  $resql = $this->db->query($sql);
2123  if ($resql) {
2124  $obj = $this->db->fetch_object($resql);
2125  $noemail = $obj->nb;
2126  if (empty($noemail)) {
2127  $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())."')";
2128  $resql = $this->db->query($sql);
2129  if (!$resql) {
2130  $error++;
2131  $this->error = $this->db->lasterror();
2132  $this->errors[] = $this->error;
2133  }
2134  }
2135  } else {
2136  $error++;
2137  $this->error = $this->db->lasterror();
2138  $this->errors[] = $this->error;
2139  }
2140  } else {
2141  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2142  $resql = $this->db->query($sql);
2143  if (!$resql) {
2144  $error++;
2145  $this->error = $this->db->lasterror();
2146  $this->errors[] = $this->error;
2147  }
2148  }
2149 
2150  if (empty($error)) {
2151  $this->no_email = $no_email;
2152  $this->db->commit();
2153  return 1;
2154  } else {
2155  $this->db->rollback();
2156  return $error * -1;
2157  }
2158  }
2159 
2160  return 0;
2161  }
2162 
2169  public function getNoEmail()
2170  {
2171  if ($this->email) {
2172  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2173  $resql = $this->db->query($sql);
2174  if ($resql) {
2175  $obj = $this->db->fetch_object($resql);
2176  $this->no_email = $obj->nb;
2177  return 1;
2178  } else {
2179  $this->error = $this->db->lasterror();
2180  $this->errors[] = $this->error;
2181  return -1;
2182  }
2183  }
2184  return 0;
2185  }
2186 
2187 
2195  public function getKanbanView($option = '', $arraydata = null)
2196  {
2197  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2198 
2199  $return = '<div class="box-flex-item box-flex-grow-zero">';
2200  $return .= '<div class="info-box info-box-sm">';
2201  $return .= '<span class="info-box-icon bg-infobox-action">';
2202  //var_dump($this->photo);exit;
2203  if (property_exists($this, 'photo') && !is_null($this->photo)) {
2204  $return.= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2205  } else {
2206  $return .= img_picto('', $this->picto);
2207  }
2208  $return .= '</span>';
2209  $return .= '<div class="info-box-content">';
2210  $return .= '<div class="info-box-ref">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</div>';
2211  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2212 
2213  if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2214  $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2215  }
2216  /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2217  $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2218  $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2219  }*/
2220  /*if (method_exists($this, 'LibPubPriv')) {
2221  $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2222  $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2223  }*/
2224  if (method_exists($this, 'getLibStatut')) {
2225  $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
2226  }
2227  $return .= '</div>';
2228  $return .= '</div>';
2229  $return .= '</div>';
2230  return $return;
2231  }
2232 }
$object ref
Definition: info.php:78
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.
load_state_board()
Load indicators into this->nb for board.
create($user, $notrigger=0)
Add a contact into database.
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 attribut to 1 or 0
getNbOfEMailings()
Return number of mass Emailing received by this 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 clicable 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 prostect 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)
Retourne chaine DN complete dans l'annuaire LDAP pour l'objet.
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 informations into database.
setstatus($status)
Change status of a user.
loadCacheOfProspStatus($active=1)
Load array of prospect status.
_load_ldap_info()
Initialise tableau info (tableau des attributs LDAP)
updateRoles()
Updates all roles (default contact for companies) according to values inside the ->roles array.
info($id)
Charge les informations sur le contact, depuis la base.
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)
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:48
trait CommonPeople
Superclass for thirdparties, contacts, members or users.
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('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_format_address($object, $withcountry=0, $sep="\n", $outputlangs='', $mode=0, $extralangcode='')
Return a formated address (part address/zip/town/state) according to country rules.
img_action($titlealt, $numaction, $picto='', $moreatt='')
Show logo action.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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 dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
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.
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:123