dolibarr  18.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  *
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 
37 
41 class Contact extends CommonObject
42 {
44 
48  public $element = 'contact';
49 
53  public $table_element = 'socpeople';
54 
59  public $ismultientitymanaged = 1;
60 
64  public $picto = 'contact';
65 
90  // BEGIN MODULEBUILDER PROPERTIES
94  public $fields = array(
95  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
96  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>3, 'notnull'=>1, 'position'=>30, 'index'=>1),
97  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>3, 'position'=>35),
98  'civility' =>array('type'=>'varchar(6)', 'label'=>'Civility', 'enabled'=>1, 'visible'=>3, 'position'=>40),
99  'lastname' =>array('type'=>'varchar(50)', 'label'=>'Lastname', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'showoncombobox'=>1, 'searchall'=>1),
100  'firstname' =>array('type'=>'varchar(50)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'showoncombobox'=>1, 'searchall'=>1),
101  'poste' =>array('type'=>'varchar(80)', 'label'=>'PostOrFunction', 'enabled'=>1, 'visible'=>-1, 'position'=>52),
102  'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
103  'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>1, 'position'=>60),
104  'town' =>array('type'=>'varchar(50)', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
105  'fk_departement' =>array('type'=>'integer', 'label'=>'Fk departement', 'enabled'=>1, 'visible'=>3, 'position'=>70),
106  'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>3, 'position'=>75),
107  'fk_soc' =>array('type'=>'integer', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>1, 'position'=>77, 'searchall'=>1),
108  'birthday' =>array('type'=>'date', 'label'=>'Birthday', 'enabled'=>1, 'visible'=>3, 'position'=>80),
109  'phone' =>array('type'=>'varchar(30)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>1, 'position'=>90, 'searchall'=>1),
110  'phone_perso' =>array('type'=>'varchar(30)', 'label'=>'PhonePerso', 'enabled'=>1, 'visible'=>-1, 'position'=>95, 'searchall'=>1),
111  'phone_mobile' =>array('type'=>'varchar(30)', 'label'=>'PhoneMobile', 'enabled'=>1, 'visible'=>1, 'position'=>100, 'searchall'=>1),
112  'fax' =>array('type'=>'varchar(30)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'searchall'=>1),
113  'email' =>array('type'=>'varchar(255)', 'label'=>'Email', 'enabled'=>1, 'visible'=>1, 'position'=>110, 'searchall'=>1),
114  'socialnetworks' =>array('type'=>'text', 'label'=>'SocialNetworks', 'enabled'=>1, 'visible'=>3, 'position'=>115),
115  'photo' =>array('type'=>'varchar(255)', 'label'=>'Photo', 'enabled'=>1, 'visible'=>3, 'position'=>170),
116  'priv' =>array('type'=>'smallint(6)', 'label'=>'ContactVisibility', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>175),
117  'fk_stcommcontact' =>array('type'=>'integer', 'label'=>'ProspectStatus', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>220),
118  'fk_prospectlevel' =>array('type'=>'varchar(12)', 'label'=>'ProspectLevel', 'enabled'=>1, 'visible'=>-1, 'position'=>255),
119  'no_email' =>array('type'=>'smallint(6)', 'label'=>'No_Email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),
120  'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>3, 'position'=>195, 'searchall'=>1),
121  'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>3, 'position'=>200, 'searchall'=>1),
122  'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>3, 'position'=>205),
123  'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>3, 'position'=>210),
124  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>300),
125  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>305),
126  'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>3, 'position'=>310),
127  'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>3, 'position'=>315),
128  'statut' =>array('type'=>'tinyint(4)', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),
129  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'position'=>1000),
130  );
131 
132  public $civility_id; // In fact we store civility_code
133  public $civility_code;
134  public $civility;
135 
139  public $gender;
140 
144  public $egroupware_id;
145 
149  public $birthday_alert;
150 
156  public $civilite;
157 
161  public $fullname;
162 
166  public $address;
167 
171  public $zip;
172 
176  public $town;
177 
181  public $state_id;
182 
186  public $state_code;
187 
191  public $state;
192 
193  public $poste; // Position
194 
198  public $socid; // both socid and fk_soc are used
199  public $fk_soc; // both socid and fk_soc are used
200 
204  public $socname;
205 
209  public $statut;
210 
211  public $code;
212 
217  public $email;
218 
225  public $mail;
226 
231  public $url;
232 
237  public $no_email;
238 
242  public $socialnetworks;
243 
249  public $skype;
250 
256  public $twitter;
257 
263  public $facebook;
264 
270  public $linkedin;
271 
277  public $jabberid;
278 
282  public $photo;
283 
287  public $phone_pro;
288 
292  public $phone_perso;
293 
297  public $phone_mobile;
298 
302  public $fax;
303 
308  public $priv;
309 
310  public $birthday;
311  public $default_lang;
312 
316  public $ref_facturation;
317 
321  public $ref_contrat;
322 
326  public $ref_commande;
327 
331  public $ref_propal;
332 
336  public $user_id;
337 
341  public $user_login;
342 
343  // END MODULEBUILDER PROPERTIES
344 
345 
350  public $oldcopy; // To contains a clone of this when we need to save old properties of object
351 
355  public $roles;
356 
357  public $cacheprospectstatus = array();
358  public $fk_prospectlevel;
359  public $stcomm_id;
360  public $statut_commercial;
361 
365  public $stcomm_picto;
366 
367 
373  public function __construct($db)
374  {
375  global $conf, $langs;
376 
377  $this->db = $db;
378  $this->statut = 1; // By default, status is enabled
379 
380  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
381  $this->fields['rowid']['visible'] = 0;
382  }
383  if (!isModEnabled('mailing')) {
384  $this->fields['no_email']['enabled'] = 0;
385  }
386  // typical ['s.nom'] is used for third-parties
387  if (empty($conf->global->SOCIETE_DISABLE_CONTACTS)) {
388  $this->fields['fk_soc']['enabled'] = 0;
389  $this->fields['fk_soc']['searchall'] = 0;
390  }
391 
392  if (!empty($conf->global->SOCIETE_DISABLE_PROSPECTS) || empty($conf->global->THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES)) { // Default behaviour
393  $this->fields['fk_stcommcontact']['enabled'] = 0;
394  $this->fields['fk_prospectlevel']['enabled'] = 0;
395  }
396 
397  // Unset fields that are disabled
398  foreach ($this->fields as $key => $val) {
399  if (isset($val['enabled']) && empty($val['enabled'])) {
400  unset($this->fields[$key]);
401  }
402  }
403 
404  // Translate some data of arrayofkeyval
405  /*if (is_object($langs))
406  {
407  foreach($this->fields as $key => $val)
408  {
409  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
410  {
411  foreach($val['arrayofkeyval'] as $key2 => $val2)
412  {
413  $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
414  }
415  }
416  }
417  }*/
418  }
419 
420  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
426  public function load_state_board()
427  {
428  // phpcs:enable
429  global $user, $hookmanager;
430 
431  $this->nb = array();
432  $clause = "WHERE";
433 
434  $sql = "SELECT count(sp.rowid) as nb";
435  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
436  if (empty($user->rights->societe->client->voir) && !$user->socid) {
437  $sql .= ", ".MAIN_DB_PREFIX."societe as s";
438  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
439  $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
440  $clause = "AND";
441  }
442  $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
443  $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
444  if ($user->socid > 0) {
445  $sql .= " AND sp.fk_soc = ".((int) $user->socid);
446  }
447  // Add where from hooks
448  if (is_object($hookmanager)) {
449  $parameters = array();
450  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
451  $sql .= $hookmanager->resPrint;
452  }
453 
454  $resql = $this->db->query($sql);
455  if ($resql) {
456  while ($obj = $this->db->fetch_object($resql)) {
457  $this->nb["contacts"] = $obj->nb;
458  }
459  $this->db->free($resql);
460  return 1;
461  } else {
462  dol_print_error($this->db);
463  $this->error = $this->db->lasterror();
464  return -1;
465  }
466  }
467 
474  public function create($user)
475  {
476  global $conf, $langs;
477 
478  $error = 0;
479  $now = dol_now();
480 
481  $this->db->begin();
482 
483  // Clean parameters
484  $this->lastname = $this->lastname ?trim($this->lastname) : trim($this->name);
485  $this->firstname = trim($this->firstname);
486  $this->setUpperOrLowerCase();
487  if (empty($this->socid)) {
488  $this->socid = 0;
489  }
490  if (empty($this->priv)) {
491  $this->priv = 0;
492  }
493  if (empty($this->statut)) {
494  $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
495  }
496 
497  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
498 
499  $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
500  $sql .= " datec";
501  $sql .= ", fk_soc";
502  $sql .= ", lastname";
503  $sql .= ", firstname";
504  $sql .= ", fk_user_creat";
505  $sql .= ", priv";
506  $sql .= ", fk_stcommcontact";
507  $sql .= ", statut";
508  $sql .= ", canvas";
509  $sql .= ", entity";
510  $sql .= ", ref_ext";
511  $sql .= ", import_key";
512  $sql .= ") VALUES (";
513  $sql .= "'".$this->db->idate($now)."',";
514  if ($this->socid > 0) {
515  $sql .= " ".((int) $this->socid).",";
516  } else {
517  $sql .= "null,";
518  }
519  $sql .= "'".$this->db->escape($this->lastname)."',";
520  $sql .= "'".$this->db->escape($this->firstname)."',";
521  $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
522  $sql .= " ".((int) $this->priv).",";
523  $sql .= " 0,";
524  $sql .= " ".((int) $this->statut).",";
525  $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
526  $sql .= " ".((int) $this->entity).",";
527  $sql .= "'".$this->db->escape($this->ref_ext)."',";
528  $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
529  $sql .= ")";
530 
531  dol_syslog(get_class($this)."::create", LOG_DEBUG);
532  $resql = $this->db->query($sql);
533  if ($resql) {
534  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
535 
536  if (!$error) {
537  $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
538  if ($result < 0) {
539  $error++;
540  $this->error = $this->db->lasterror();
541  }
542  }
543 
544  if (!$error) {
545  $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
546  if ($result < 0) {
547  $error++;
548  $this->error = $this->db->lasterror();
549  }
550  }
551 
552  if (!$error) {
553  // Call trigger
554  $result = $this->call_trigger('CONTACT_CREATE', $user);
555  if ($result < 0) {
556  $error++;
557  }
558  // End call triggers
559  }
560 
561  if (!$error) {
562  $this->db->commit();
563  return $this->id;
564  } else {
565  $this->db->rollback();
566  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
567  return -2;
568  }
569  } else {
570  $this->error = $this->db->lasterror();
571 
572  $this->db->rollback();
573  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
574  return -1;
575  }
576  }
577 
588  public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
589  {
590  global $conf, $langs, $hookmanager;
591 
592  $error = 0;
593 
594  $this->id = $id;
595 
596  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
597 
598  // Clean parameters
599  $this->ref_ext = trim($this->ref_ext);
600  $this->lastname = trim($this->lastname) ?trim($this->lastname) : trim($this->lastname);
601  $this->firstname = trim($this->firstname);
602  $this->email = trim($this->email);
603  $this->phone_pro = trim($this->phone_pro);
604  $this->phone_perso = trim($this->phone_perso);
605  $this->phone_mobile = trim($this->phone_mobile);
606  $this->photo = trim($this->photo);
607  $this->fax = trim($this->fax);
608  $this->zip = (empty($this->zip) ? '' : trim($this->zip));
609  $this->town = (empty($this->town) ? '' : trim($this->town));
610  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
611  if (empty($this->statut)) {
612  $this->statut = 0;
613  }
614  if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
615  $this->civility_code = $this->civility_id; // For backward compatibility
616  }
617  $this->setUpperOrLowerCase();
618  $this->db->begin();
619 
620  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
621  if ($this->socid > 0) {
622  $sql .= " fk_soc = ".((int) $this->socid).",";
623  } elseif ($this->socid == -1) {
624  $sql .= " fk_soc = NULL,";
625  }
626  $sql .= " civility='".$this->db->escape($this->civility_code)."'";
627  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
628  $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
629  $sql .= ", address='".$this->db->escape($this->address)."'";
630  $sql .= ", zip='".$this->db->escape($this->zip)."'";
631  $sql .= ", town='".$this->db->escape($this->town)."'";
632  $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
633  $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
634  $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
635  $sql .= ", poste='".$this->db->escape($this->poste)."'";
636  $sql .= ", fax='".$this->db->escape($this->fax)."'";
637  $sql .= ", email='".$this->db->escape($this->email)."'";
638  $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
639  $sql .= ", photo='".$this->db->escape($this->photo)."'";
640  $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
641  $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
642  $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
643  $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
644  $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
645  $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
646  $sql .= ", priv = '".$this->db->escape($this->priv)."'";
647  $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
648  if (isset($this->stcomm_id)) {
649  $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
650  }
651  $sql .= ", statut = ".((int) $this->statut);
652  $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
653  $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
654  $sql .= ", entity = ".((int) $this->entity);
655  $sql .= " WHERE rowid = ".((int) $id);
656 
657  dol_syslog(get_class($this)."::update", LOG_DEBUG);
658  $result = $this->db->query($sql);
659  if ($result) {
660  unset($this->country_code);
661  unset($this->country);
662  unset($this->state_code);
663  unset($this->state);
664 
665  $action = 'update';
666 
667  // Actions on extra fields
668  if (!$error) {
669  $result = $this->insertExtraFields();
670  if ($result < 0) {
671  $error++;
672  }
673  }
674 
675  if (!$error) {
676  $result = $this->updateRoles();
677  if ($result < 0) {
678  $error++;
679  }
680  }
681 
682  if (!$error && $this->user_id > 0) {
683  // If contact is linked to a user
684  $tmpobj = new User($this->db);
685  $tmpobj->fetch($this->user_id);
686  $usermustbemodified = 0;
687  if ($tmpobj->office_phone != $this->phone_pro) {
688  $tmpobj->office_phone = $this->phone_pro;
689  $usermustbemodified++;
690  }
691  if ($tmpobj->office_fax != $this->fax) {
692  $tmpobj->office_fax = $this->fax;
693  $usermustbemodified++;
694  }
695  if ($tmpobj->address != $this->address) {
696  $tmpobj->address = $this->address;
697  $usermustbemodified++;
698  }
699  if ($tmpobj->town != $this->town) {
700  $tmpobj->town = $this->town;
701  $usermustbemodified++;
702  }
703  if ($tmpobj->zip != $this->zip) {
704  $tmpobj->zip = $this->zip;
705  $usermustbemodified++;
706  }
707  if ($tmpobj->zip != $this->zip) {
708  $tmpobj->state_id = $this->state_id;
709  $usermustbemodified++;
710  }
711  if ($tmpobj->country_id != $this->country_id) {
712  $tmpobj->country_id = $this->country_id;
713  $usermustbemodified++;
714  }
715  if ($tmpobj->email != $this->email) {
716  $tmpobj->email = $this->email;
717  $usermustbemodified++;
718  }
719  if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
720  $tmpobj->socialnetworks = $this->socialnetworks;
721  $usermustbemodified++;
722  }
723  if ($usermustbemodified) {
724  $result = $tmpobj->update($user, 0, 1, 1, 1);
725  if ($result < 0) {
726  $error++;
727  }
728  }
729  }
730 
731  if (!$error && !$notrigger) {
732  // Call trigger
733  $result = $this->call_trigger('CONTACT_MODIFY', $user);
734  if ($result < 0) {
735  $error++;
736  }
737  // End call triggers
738  }
739 
740  if (!$error) {
741  $this->db->commit();
742  return 1;
743  } else {
744  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
745  $this->db->rollback();
746  return -$error;
747  }
748  } else {
749  $this->error = $this->db->lasterror().' sql='.$sql;
750  $this->db->rollback();
751  return -1;
752  }
753  }
754 
755 
756  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
757  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
767  public function _load_ldap_dn($info, $mode = 0)
768  {
769  // phpcs:enable
770  global $conf;
771  $dn = '';
772  if ($mode == 0) {
773  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;
774  } elseif ($mode == 1) {
775  $dn = $conf->global->LDAP_CONTACT_DN;
776  } elseif ($mode == 2) {
777  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS];
778  }
779  return $dn;
780  }
781 
782 
783  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
784  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
790  public function _load_ldap_info()
791  {
792  // phpcs:enable
793  global $conf, $langs;
794 
795  $info = array();
796 
797  // Object classes
798  $info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);
799 
800  $this->fullname = $this->getFullName($langs);
801 
802  // Fields
803  if ($this->fullname && !empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME)) {
804  $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
805  }
806  if ($this->lastname && !empty($conf->global->LDAP_CONTACT_FIELD_NAME)) {
807  $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname;
808  }
809  if ($this->firstname && !empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME)) {
810  $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
811  }
812 
813  if ($this->poste) {
814  $info["title"] = $this->poste;
815  }
816  if ($this->socid > 0) {
817  $soc = new Societe($this->db);
818  $soc->fetch($this->socid);
819 
820  $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->name;
821  if ($soc->client == 1) {
822  $info["businessCategory"] = "Customers";
823  }
824  if ($soc->client == 2) {
825  $info["businessCategory"] = "Prospects";
826  }
827  if ($soc->fournisseur == 1) {
828  $info["businessCategory"] = "Suppliers";
829  }
830  }
831  if ($this->address && !empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS)) {
832  $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
833  }
834  if ($this->zip && !empty($conf->global->LDAP_CONTACT_FIELD_ZIP)) {
835  $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->zip;
836  }
837  if ($this->town && !empty($conf->global->LDAP_CONTACT_FIELD_TOWN)) {
838  $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->town;
839  }
840  if ($this->country_code && !empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY)) {
841  $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code;
842  }
843  if ($this->phone_pro && !empty($conf->global->LDAP_CONTACT_FIELD_PHONE)) {
844  $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
845  }
846  if ($this->phone_perso && !empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) {
847  $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
848  }
849  if ($this->phone_mobile && !empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) {
850  $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
851  }
852  if ($this->fax && !empty($conf->global->LDAP_CONTACT_FIELD_FAX)) {
853  $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
854  }
855  if ($this->note_private && !empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) {
856  $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
857  }
858  if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL)) {
859  $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
860  }
861 
862  if ($conf->global->LDAP_SERVER_TYPE == 'egroupware') {
863  $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
864 
865  $info['uidnumber'] = $this->id;
866 
867  $info['phpgwTz'] = 0;
868  $info['phpgwMailType'] = 'INTERNET';
869  $info['phpgwMailHomeType'] = 'INTERNET';
870 
871  $info["phpgwContactTypeId"] = 'n';
872  $info["phpgwContactCatId"] = 0;
873  $info["phpgwContactAccess"] = "public";
874 
875  if (dol_strlen($this->egroupware_id) == 0) {
876  $this->egroupware_id = 1;
877  }
878 
879  $info["phpgwContactOwner"] = $this->egroupware_id;
880 
881  if ($this->email) {
882  $info["rfc822Mailbox"] = $this->email;
883  }
884  if ($this->phone_mobile) {
885  $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
886  }
887  }
888 
889  return $info;
890  }
891 
892 
893  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
902  public function update_perso($id, $user = null, $notrigger = 0)
903  {
904  // phpcs:enable
905  $error = 0;
906  $result = false;
907 
908  $this->db->begin();
909 
910  // Mis a jour contact
911  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
912  $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
913  $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
914  if ($user) {
915  $sql .= ", fk_user_modif = ".((int) $user->id);
916  }
917  $sql .= " WHERE rowid = ".((int) $id);
918 
919  dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
920  $resql = $this->db->query($sql);
921  if (!$resql) {
922  $error++;
923  $this->error = $this->db->lasterror();
924  }
925 
926  if ($user) {
927  // Update birthday alert
928  if (!empty($this->birthday_alert)) {
929  //check existing
930  $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
931  $result_check = $this->db->query($sql_check);
932  if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
933  //insert
934  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
935  $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
936  $result = $this->db->query($sql);
937  if (!$result) {
938  $error++;
939  $this->error = $this->db->lasterror();
940  }
941  } else {
942  $result = true;
943  }
944  } else {
945  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
946  $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
947  $result = $this->db->query($sql);
948  if (!$result) {
949  $error++;
950  $this->error = $this->db->lasterror();
951  }
952  }
953  }
954 
955  if (!$error && !$notrigger) {
956  // Call trigger
957  $result = $this->call_trigger('CONTACT_MODIFY', $user);
958  if ($result < 0) {
959  $error++;
960  }
961  // End call triggers
962  }
963 
964  if (!$error) {
965  $this->db->commit();
966  return 1;
967  } else {
968  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
969  $this->db->rollback();
970  return -$error;
971  }
972  }
973 
974 
985  public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
986  {
987  global $langs;
988 
989  dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
990 
991  if (empty($id) && empty($ref_ext) && empty($email)) {
992  $this->error = 'BadParameter';
993  return -1;
994  }
995 
996  $langs->loadLangs(array("dict", "companies"));
997 
998  $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.lastname, c.firstname,";
999  $sql .= " c.address, c.statut, c.zip, c.town,";
1000  $sql .= " c.fk_pays as country_id,";
1001  $sql .= " c.fk_departement as state_id,";
1002  $sql .= " c.birthday,";
1003  $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1004  $sql .= " c.socialnetworks,";
1005  $sql .= " c.photo,";
1006  $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1007  $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1008  $sql .= " c.import_key,";
1009  $sql .= " c.datec as date_creation, c.tms as date_modification,";
1010  $sql .= " co.label as country, co.code as country_code,";
1011  $sql .= " d.nom as state, d.code_departement as state_code,";
1012  $sql .= " u.rowid as user_id, u.login as user_login,";
1013  $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1014  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1015  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1016  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1017  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1018  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1019  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1020  if ($id) {
1021  $sql .= " WHERE c.rowid = ".((int) $id);
1022  } else {
1023  $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1024  if ($ref_ext) {
1025  $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1026  }
1027  if ($email) {
1028  $sql .= " AND c.email = '".$this->db->escape($email)."'";
1029  }
1030  }
1031 
1032  $resql = $this->db->query($sql);
1033  if ($resql) {
1034  $num = $this->db->num_rows($resql);
1035  if ($num > 1) {
1036  $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1037  dol_syslog($this->error, LOG_ERR);
1038 
1039  return 2;
1040  } elseif ($num) { // $num = 1
1041  $obj = $this->db->fetch_object($resql);
1042 
1043  $this->id = $obj->rowid;
1044  $this->entity = $obj->entity;
1045  $this->ref = $obj->rowid;
1046  $this->ref_ext = $obj->ref_ext;
1047 
1048  $this->civility_code = $obj->civility_code;
1049  $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1050 
1051  $this->lastname = $obj->lastname;
1052  $this->firstname = $obj->firstname;
1053  $this->address = $obj->address;
1054  $this->zip = $obj->zip;
1055  $this->town = $obj->town;
1056 
1057  $this->date_creation = $this->db->jdate($obj->date_creation);
1058  $this->date_modification = $this->db->jdate($obj->date_modification);
1059 
1060  $this->state_id = $obj->state_id;
1061  $this->state_code = $obj->state_code;
1062  $this->state = $obj->state;
1063 
1064  $this->country_id = $obj->country_id;
1065  $this->country_code = $obj->country_id ? $obj->country_code : '';
1066  $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1067 
1068  $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1069  $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1070  $this->socname = $obj->socname;
1071  $this->poste = $obj->poste;
1072  $this->statut = $obj->statut;
1073 
1074  $this->fk_prospectlevel = $obj->fk_prospectlevel;
1075 
1076  $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1077  $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1078  $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1079  $this->statut_commercial = $libelle; // libelle statut commercial
1080  $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1081 
1082  $this->phone_pro = trim($obj->phone);
1083  $this->fax = trim($obj->fax);
1084  $this->phone_perso = trim($obj->phone_perso);
1085  $this->phone_mobile = trim($obj->phone_mobile);
1086 
1087  $this->email = $obj->email;
1088  $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1089  $this->photo = $obj->photo;
1090  $this->priv = $obj->priv;
1091  $this->mail = $obj->email;
1092 
1093  $this->birthday = $this->db->jdate($obj->birthday);
1094  $this->note = $obj->note_private; // deprecated
1095  $this->note_private = $obj->note_private;
1096  $this->note_public = $obj->note_public;
1097  $this->default_lang = $obj->default_lang;
1098  $this->user_id = $obj->user_id;
1099  $this->user_login = $obj->user_login;
1100  $this->canvas = $obj->canvas;
1101 
1102  $this->import_key = $obj->import_key;
1103 
1104  // Define gender according to civility
1105  $this->setGenderFromCivility();
1106 
1107  // Search Dolibarr user linked to this contact
1108  $sql = "SELECT u.rowid ";
1109  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1110  $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1111 
1112  $resql = $this->db->query($sql);
1113  if ($resql) {
1114  if ($this->db->num_rows($resql)) {
1115  $uobj = $this->db->fetch_object($resql);
1116 
1117  $this->user_id = $uobj->rowid;
1118  }
1119  $this->db->free($resql);
1120  } else {
1121  $this->error = $this->db->error();
1122  return -1;
1123  }
1124 
1125  // Retrieve all extrafield
1126  // fetch optionals attributes and labels
1127  $this->fetch_optionals();
1128 
1129  // Load also alerts of this user
1130  if ($user) {
1131  $sql = "SELECT fk_user";
1132  $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1133  $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1134 
1135  $resql = $this->db->query($sql);
1136  if ($resql) {
1137  if ($this->db->num_rows($resql)) {
1138  $obj = $this->db->fetch_object($resql);
1139 
1140  $this->birthday_alert = 1;
1141  }
1142  $this->db->free($resql);
1143  } else {
1144  $this->error = $this->db->error();
1145  return -1;
1146  }
1147  }
1148 
1149  // Load also roles of this address
1150  if ($loadalsoroles) {
1151  $resultRole = $this->fetchRoles();
1152  if ($resultRole < 0) {
1153  return $resultRole;
1154  }
1155  }
1156 
1157  return 1;
1158  } else {
1159  $this->error = $langs->trans("RecordNotFound");
1160  return 0;
1161  }
1162  } else {
1163  $this->error = $this->db->error();
1164  return -1;
1165  }
1166  }
1167 
1168 
1169 
1176  public function setGenderFromCivility()
1177  {
1178  unset($this->gender);
1179 
1180  if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1181  $this->gender = 'man';
1182  } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1183  $this->gender = 'woman';
1184  }
1185  }
1186 
1187  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1197  public function load_ref_elements()
1198  {
1199  // phpcs:enable
1200  // Compte les elements pour lesquels il est contact
1201  $sql = "SELECT tc.element, count(ec.rowid) as nb";
1202  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1203  $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1204  $sql .= " AND fk_socpeople = ".((int) $this->id);
1205  $sql .= " AND tc.source = 'external'";
1206  $sql .= " GROUP BY tc.element";
1207 
1208  dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1209 
1210  $resql = $this->db->query($sql);
1211  if ($resql) {
1212  while ($obj = $this->db->fetch_object($resql)) {
1213  if ($obj->nb) {
1214  if ($obj->element == 'facture') {
1215  $this->ref_facturation = $obj->nb;
1216  } elseif ($obj->element == 'contrat') {
1217  $this->ref_contrat = $obj->nb;
1218  } elseif ($obj->element == 'commande') {
1219  $this->ref_commande = $obj->nb;
1220  } elseif ($obj->element == 'propal') {
1221  $this->ref_propal = $obj->nb;
1222  }
1223  }
1224  }
1225  $this->db->free($resql);
1226  return 0;
1227  } else {
1228  $this->error = $this->db->lasterror();
1229  return -1;
1230  }
1231  }
1232 
1240  public function delete($notrigger = 0)
1241  {
1242  global $conf, $langs, $user;
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 Roles
1305  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((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  // Removed 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  if ($obj->fk_user_creat) {
1379  $cuser = new User($this->db);
1380  $cuser->fetch($obj->fk_user_creat);
1381  $this->user_creation = $cuser;
1382  }
1383 
1384  if ($obj->fk_user_modif) {
1385  $muser = new User($this->db);
1386  $muser->fetch($obj->fk_user_modif);
1387  $this->user_modification = $muser;
1388  }
1389 
1390  $this->date_creation = $this->db->jdate($obj->datec);
1391  $this->date_modification = $this->db->jdate($obj->tms);
1392  }
1393 
1394  $this->db->free($resql);
1395  } else {
1396  print $this->db->error();
1397  }
1398  }
1399 
1405  public function getNbOfEMailings()
1406  {
1407  $sql = "SELECT count(mc.email) as nb";
1408  $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1409  $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1410  $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1411 
1412  $resql = $this->db->query($sql);
1413  if ($resql) {
1414  $obj = $this->db->fetch_object($resql);
1415  $nb = $obj->nb;
1416 
1417  $this->db->free($resql);
1418  return $nb;
1419  } else {
1420  $this->error = $this->db->error();
1421  return -1;
1422  }
1423  }
1424 
1431  public function getTooltipContentArray($params)
1432  {
1433  global $conf, $langs, $user;
1434 
1435  $datas = [];
1436 
1437  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1438  return ['optimize' => $langs->trans("ShowContact")];
1439  }
1440  if (!empty($this->photo) && class_exists('Form')) {
1441  $photo = '<div class="photointooltip floatright">';
1442  $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.
1443  $photo .= '</div>';
1444  $datas['photo'] = $photo;
1445  }
1446 
1447  $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1448  $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1449  // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1450  if (!empty($this->poste)) {
1451  $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1452  }
1453  $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1454  $phonelist = array();
1455  $country_code = empty($this->country_code) ? '': $this->country_code;
1456  if ($this->phone_pro) {
1457  $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1458  }
1459  if ($this->phone_mobile) {
1460  $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1461  }
1462  if ($this->phone_perso) {
1463  $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1464  }
1465  $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1466  $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1467 
1468  return $datas;
1469  }
1470 
1484  public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1485  {
1486  global $conf, $langs, $hookmanager;
1487 
1488  $result = '';
1489  $params = [
1490  'id' => $this->id,
1491  'objecttype' => $this->element,
1492  'option' => $option,
1493  ];
1494  $classfortooltip = 'classfortooltip';
1495  $dataparams = '';
1496  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1497  $classfortooltip = 'classforajaxtooltip';
1498  $dataparams = ' data-params='.json_encode($params);
1499  // $label = $langs->trans('Loading');
1500  }
1501  $label = implode($this->getTooltipContentArray($params));
1502 
1503  $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1504 
1505  if ($option !== 'nolink') {
1506  // Add param to save lastsearch_values or not
1507  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1508  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1509  $add_save_lastsearch_values = 1;
1510  }
1511  if ($add_save_lastsearch_values) {
1512  $url .= '&save_lastsearch_values=1';
1513  }
1514  }
1515 
1516  $url .= $moreparam;
1517 
1518  $linkclose = "";
1519  if (empty($notooltip)) {
1520  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1521  $label = $langs->trans("ShowContact");
1522  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1523  }
1524  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1525  $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1526  }
1527 
1528  $linkstart = '<a href="'.$url.'"';
1529  $linkstart .= $linkclose.'>';
1530  $linkend = '</a>';
1531 
1532  if ($option == 'xxx') {
1533  $linkstart = '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$this->id.$moreparam.'"'.$dataparams.' title="'.dol_escape_htmltag($label, 1).'" class="'.$classfortooltip.'">';
1534  $linkend = '</a>';
1535  }
1536 
1537  $result .= $linkstart;
1538  if ($withpicto) {
1539  if ($withpicto < 0) {
1540  $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>';
1541  } else {
1542  $result .= img_object(($notooltip ? '' : $label), ( $this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
1543  }
1544  }
1545  if ($withpicto != 2 && $withpicto != -2) {
1546  $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1547  }
1548  $result .= $linkend;
1549 
1550  global $action;
1551  $hookmanager->initHooks(array('contactdao'));
1552  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1553  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1554  if ($reshook > 0) {
1555  $result = $hookmanager->resPrint;
1556  } else {
1557  $result .= $hookmanager->resPrint;
1558  }
1559 
1560  return $result;
1561  }
1562 
1568  public function getCivilityLabel()
1569  {
1570  global $langs;
1571 
1572  $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1573  if (empty($code)) {
1574  return '';
1575  }
1576 
1577  $langs->load("dict");
1578  return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1579  }
1580 
1587  public function getLibStatut($mode)
1588  {
1589  return $this->LibStatut($this->statut, $mode);
1590  }
1591 
1592  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1600  public function LibStatut($status, $mode)
1601  {
1602  // phpcs:enable
1603  global $langs;
1604 
1605  $labelStatus = array(
1606  0 => 'ActivityCeased',
1607  1 => 'InActivity',
1608  4 => 'InActivity',
1609  5 => 'ActivityCeased',
1610  );
1611  $labelStatusShort = array(
1612  0 => 'ActivityCeased',
1613  1 => 'InActivity',
1614  4 => 'InActivity',
1615  5 => 'ActivityCeased',
1616  );
1617 
1618  $statusType = 'status4';
1619  if ($status == 0 || $status == 5) {
1620  $statusType = 'status5';
1621  }
1622 
1623  $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1624  $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1625 
1626  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1627  }
1628 
1629 
1630  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1637  public function LibPubPriv($status)
1638  {
1639  // phpcs:enable
1640  global $langs;
1641  if ($status == '1') {
1642  return $langs->trans('ContactPrivate');
1643  } else {
1644  return $langs->trans('ContactPublic');
1645  }
1646  }
1647 
1648 
1656  public function initAsSpecimen()
1657  {
1658  // Get first id of existing company and save it into $socid
1659  $socid = 0;
1660  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1661  $resql = $this->db->query($sql);
1662  if ($resql) {
1663  $obj = $this->db->fetch_object($resql);
1664  if ($obj) {
1665  $socid = $obj->rowid;
1666  }
1667  }
1668 
1669  // Initialise parameters
1670  $this->id = 0;
1671  $this->entity = 1;
1672  $this->specimen = 1;
1673  $this->lastname = 'DOLIBARR';
1674  $this->firstname = 'SPECIMEN';
1675  $this->address = '21 jump street';
1676  $this->zip = '99999';
1677  $this->town = 'MyTown';
1678  $this->country_id = 1;
1679  $this->country_code = 'FR';
1680  $this->country = 'France';
1681  $this->email = 'specimen@specimen.com';
1682  $this->socialnetworks = array(
1683  'skype' => 'tom.hanson',
1684  );
1685  $this->phone_pro = '0909090901';
1686  $this->phone_perso = '0909090902';
1687  $this->phone_mobile = '0909090903';
1688  $this->fax = '0909090909';
1689 
1690  $this->note_public = 'This is a comment (public)';
1691  $this->note_private = 'This is a comment (private)';
1692 
1693  $this->socid = $socid;
1694  $this->statut = 1;
1695  return 1;
1696  }
1697 
1704  public function setstatus($status)
1705  {
1706  global $conf, $langs, $user;
1707 
1708  $error = 0;
1709 
1710  // Check parameters
1711  if ($this->statut == $status) {
1712  return 0;
1713  } else {
1714  $this->statut = $status;
1715  }
1716 
1717  $this->db->begin();
1718 
1719  // Desactive utilisateur
1720  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1721  $sql .= " SET statut = ".((int) $this->statut);
1722  $sql .= " WHERE rowid = ".((int) $this->id);
1723  $result = $this->db->query($sql);
1724 
1725  dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1726  if ($result) {
1727  // Call trigger
1728  $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1729  if ($result < 0) {
1730  $error++;
1731  }
1732  // End call triggers
1733  }
1734 
1735  if ($error) {
1736  $this->db->rollback();
1737  return -$error;
1738  } else {
1739  $this->db->commit();
1740  return 1;
1741  }
1742  }
1743 
1754  public function setCategories($categories)
1755  {
1756  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1757  return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1758  }
1759 
1768  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1769  {
1770  $tables = array(
1771  'socpeople', 'societe_contacts'
1772  );
1773 
1774  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1775  }
1776 
1784  public function fetchRoles()
1785  {
1786  global $langs;
1787  $error = 0;
1788  $num = 0;
1789 
1790  $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1791  $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1792  $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1793  $sql .= " AND tc.source = 'external' AND tc.active=1";
1794  $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1795  $sql .= " AND sc.entity IN (".getEntity('societe').')';
1796 
1797  $resql = $this->db->query($sql);
1798  if ($resql) {
1799  $this->roles = array();
1800 
1801  $num = $this->db->num_rows($resql);
1802  if ($num > 0) {
1803  while ($obj = $this->db->fetch_object($resql)) {
1804  $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1805  $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1806  $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));
1807  }
1808  }
1809  } else {
1810  $error++;
1811  $this->error = $this->db->lasterror();
1812  $this->errors[] = $this->db->lasterror();
1813  }
1814 
1815  if (empty($error)) {
1816  return $num;
1817  } else {
1818  return $error * -1;
1819  }
1820  }
1821 
1829  public function getContactRoles($element = '')
1830  {
1831  $tab = array();
1832 
1833  if ($element == 'action') {
1834  $element = 'agenda';
1835  }
1836 
1837  $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1838  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1839  $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1840  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1841  $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1842  $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1843  $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1844  $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1845  $sql .= " AND tc.active = 1";
1846 
1847  dol_syslog(__METHOD__, LOG_DEBUG);
1848  $resql = $this->db->query($sql);
1849  if ($resql) {
1850  $num = $this->db->num_rows($resql);
1851  $i = 0;
1852  while ($i < $num) {
1853  $obj = $this->db->fetch_object($resql);
1854  $tab[] = array('fk_socpeople'=>$obj->id, 'type_contact'=>$obj->fk_c_type_contact);
1855 
1856  $i++;
1857  }
1858 
1859  return $tab;
1860  } else {
1861  $this->error = $this->db->error();
1862  dol_print_error($this->db);
1863  return -1;
1864  }
1865  }
1866 
1874  public function updateRoles()
1875  {
1876  global $conf;
1877 
1878  $error = 0;
1879 
1880  if (!isset($this->roles)) {
1881  return 0; // Avoid to loose roles when property not set
1882  }
1883 
1884  $this->db->begin();
1885 
1886  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1887 
1888  $result = $this->db->query($sql);
1889  if (!$result) {
1890  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1891  $error++;
1892  } else {
1893  if (count($this->roles) > 0) {
1894  foreach ($this->roles as $keyRoles => $valRoles) {
1895  $idrole = 0;
1896  if (is_array($valRoles)) {
1897  $idrole = $valRoles['id'];
1898  } else {
1899  $idrole = $valRoles;
1900  }
1901 
1902  $socid = 0;
1903  if (is_array($valRoles)) {
1904  $socid = $valRoles['socid'];
1905  } else {
1906  $socid = $this->socid;
1907  }
1908 
1909  if ($socid > 0) {
1910  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1911  $sql .= " (entity,";
1912  $sql .= "date_creation,";
1913  $sql .= "fk_soc,";
1914  $sql .= "fk_c_type_contact,";
1915  $sql .= "fk_socpeople) ";
1916  $sql .= " VALUES (".$conf->entity.",";
1917  $sql .= "'".$this->db->idate(dol_now())."',";
1918  $sql .= $socid.", ";
1919  $sql .= $idrole." , ";
1920  $sql .= $this->id;
1921  $sql .= ")";
1922 
1923  $result = $this->db->query($sql);
1924  if (!$result) {
1925  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1926  $error++;
1927  }
1928  }
1929  }
1930  }
1931  }
1932  if (empty($error)) {
1933  $this->db->commit();
1934  return 1;
1935  } else {
1936  $this->error = implode(' ', $this->errors);
1937  $this->db->rollback();
1938  return $error * -1;
1939  }
1940  }
1941 
1948  public function loadCacheOfProspStatus($active = 1)
1949  {
1950  global $langs;
1951 
1952  $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
1953  if ($active >= 0) {
1954  $sql .= " WHERE active = ".((int) $active);
1955  }
1956  $resql = $this->db->query($sql);
1957  $num = $this->db->num_rows($resql);
1958  $i = 0;
1959  while ($i < $num) {
1960  $obj = $this->db->fetch_object($resql);
1961  $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);
1962  $i++;
1963  }
1964  return 1;
1965  }
1966 
1972  public function getLibProspLevel()
1973  {
1974  return $this->libProspLevel($this->fk_prospectlevel);
1975  }
1976 
1983  public function libProspLevel($fk_prospectlevel)
1984  {
1985  global $langs;
1986 
1987  $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
1988  // If lib not found in language file, we get label from cache/databse
1989  if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel)) {
1990  $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
1991  }
1992  return $lib;
1993  }
1994 
1995 
2003  public function setProspectLevel(User $user)
2004  {
2005  return $this->update($this->id, $user);
2006  }
2007 
2015  public function getLibProspCommStatut($mode = 0, $label = '')
2016  {
2017  return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2018  }
2019 
2033  public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2034  {
2035  global $langs;
2036  $langs->load('customers');
2037 
2038  if ($mode == 2) {
2039  if ($statut == '-1' || $statut == 'ST_NO') {
2040  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2041  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2042  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2043  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2044  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2045  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2046  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2047  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2048  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2049  } else {
2050  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);
2051  }
2052  }
2053  if ($mode == 3) {
2054  if ($statut == '-1' || $statut == 'ST_NO') {
2055  return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2056  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2057  return img_action($langs->trans("StatusProspect0"), 0, $picto);
2058  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2059  return img_action($langs->trans("StatusProspect1"), 1, $picto);
2060  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2061  return img_action($langs->trans("StatusProspect2"), 2, $picto);
2062  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2063  return img_action($langs->trans("StatusProspect3"), 3, $picto);
2064  } else {
2065  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);
2066  }
2067  }
2068  if ($mode == 4) {
2069  if ($statut == '-1' || $statut == 'ST_NO') {
2070  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2071  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2072  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2073  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2074  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2075  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2076  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2077  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2078  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2079  } else {
2080  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);
2081  }
2082  }
2083 
2084  return "Error, mode/status not found";
2085  }
2086 
2087 
2094  public function setNoEmail($no_email)
2095  {
2096  $error = 0;
2097 
2098  // Update mass emailing flag into table mailing_unsubscribe
2099  if ($this->email) {
2100  $this->db->begin();
2101 
2102  if ($no_email) {
2103  $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)."'";
2104  $resql = $this->db->query($sql);
2105  if ($resql) {
2106  $obj = $this->db->fetch_object($resql);
2107  $noemail = $obj->nb;
2108  if (empty($noemail)) {
2109  $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())."')";
2110  $resql = $this->db->query($sql);
2111  if (!$resql) {
2112  $error++;
2113  $this->error = $this->db->lasterror();
2114  $this->errors[] = $this->error;
2115  }
2116  }
2117  } else {
2118  $error++;
2119  $this->error = $this->db->lasterror();
2120  $this->errors[] = $this->error;
2121  }
2122  } else {
2123  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2124  $resql = $this->db->query($sql);
2125  if (!$resql) {
2126  $error++;
2127  $this->error = $this->db->lasterror();
2128  $this->errors[] = $this->error;
2129  }
2130  }
2131 
2132  if (empty($error)) {
2133  $this->no_email = $no_email;
2134  $this->db->commit();
2135  return 1;
2136  } else {
2137  $this->db->rollback();
2138  return $error * -1;
2139  }
2140  }
2141 
2142  return 0;
2143  }
2144 
2151  public function getNoEmail()
2152  {
2153  if ($this->email) {
2154  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2155  $resql = $this->db->query($sql);
2156  if ($resql) {
2157  $obj = $this->db->fetch_object($resql);
2158  $this->no_email = $obj->nb;
2159  return 1;
2160  } else {
2161  $this->error = $this->db->lasterror();
2162  $this->errors[] = $this->error;
2163  return -1;
2164  }
2165  }
2166  return 0;
2167  }
2168 
2169 
2177  public function getKanbanView($option = '', $arraydata = null)
2178  {
2179  global $langs;
2180 
2181  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2182 
2183  $return = '<div class="box-flex-item box-flex-grow-zero">';
2184  $return .= '<div class="info-box info-box-sm">';
2185  $return .= '<span class="info-box-icon bg-infobox-action">';
2186  //var_dump($this->photo);exit;
2187  if (property_exists($this, 'photo') && !is_null($this->photo)) {
2188  $return.= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2189  } else {
2190  $return .= img_picto('', $this->picto);
2191  }
2192  $return .= '</span>';
2193  $return .= '<div class="info-box-content">';
2194  $return .= '<div class="info-box-ref">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</div>';
2195  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2196 
2197  if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2198  $return .= '<div class="info-box-ref opacitymedium tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2199  }
2200  /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2201  $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2202  $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2203  }*/
2204  /*if (method_exists($this, 'LibPubPriv')) {
2205  $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2206  $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2207  }*/
2208  if (method_exists($this, 'getLibStatut')) {
2209  $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
2210  }
2211  $return .= '</div>';
2212  $return .= '</div>';
2213  $return .= '</div>';
2214  return $return;
2215  }
2216 }
Contact\getLibProspLevel
getLibProspLevel()
Return prostect level.
Definition: contact.class.php:1972
Contact\setProspectLevel
setProspectLevel(User $user)
Set prospect level.
Definition: contact.class.php:2003
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:50
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1534
dol_trunc
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.
Definition: functions.lib.php:3985
Contact\loadCacheOfProspStatus
loadCacheOfProspStatus($active=1)
Load array of prospect status.
Definition: contact.class.php:1948
Contact\load_state_board
load_state_board()
Load indicators into this->nb for board.
Definition: contact.class.php:426
img_action
img_action($titlealt, $numaction, $picto='')
Show logo action.
Definition: functions.lib.php:4483
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Contact\update
update($id, $user=null, $notrigger=0, $action='update', $nosyncuser=0)
Update informations into database.
Definition: contact.class.php:588
Contact\LibStatut
LibStatut($status, $mode)
Renvoi le libelle d'un statut donne.
Definition: contact.class.php:1600
$sql
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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:745
CommonObject\setUpperOrLowerCase
setUpperOrLowerCase()
Set to upper or ucwords/lower if needed.
Definition: commonobject.class.php:854
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5031
Contact\libProspCommStatut
libProspCommStatut($statut, $mode=0, $label='', $picto='')
Return label of a given status.
Definition: contact.class.php:2033
Contact\update_perso
update_perso($id, $user=null, $notrigger=0)
Update field alert birthday.
Definition: contact.class.php:902
Contact\updateRoles
updateRoles()
Updates all roles (default contact for companies) according to values inside the ->roles array.
Definition: contact.class.php:1874
Contact\setNoEmail
setNoEmail($no_email)
Set "blacklist" mailing status.
Definition: contact.class.php:2094
Contact\getContactRoles
getContactRoles($element='')
Get Contact roles for a thirdparty.
Definition: contact.class.php:1829
name
$conf db name
Definition: repair.php:123
Contact\create
create($user)
Add a contact into database.
Definition: contact.class.php:474
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:45
CommonObject\getFullName
getFullName($langs, $option=0, $nameorder=-1, $maxlen=0)
Return full name (civility+' '+name+' '+lastname)
Definition: commonobject.class.php:826
Contact\getKanbanView
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
Definition: contact.class.php:2177
Contact\info
info($id)
Charge les informations sur le contact, depuis la base.
Definition: contact.class.php:1364
Contact\load_ref_elements
load_ref_elements()
Load number of elements the contact is used as a link for ref_facturation ref_contrat ref_commande (f...
Definition: contact.class.php:1197
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4061
Contact\setCategories
setCategories($categories)
Sets object to supplied categories.
Definition: contact.class.php:1754
Contact\replaceThirdparty
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: contact.class.php:1768
Contact\getNbOfEMailings
getNbOfEMailings()
Return number of mass Emailing received by this contacts with its email.
Definition: contact.class.php:1405
Contact\libProspLevel
libProspLevel($fk_prospectlevel)
Return label of prospect level.
Definition: contact.class.php:1983
Contact\__construct
__construct($db)
Constructor.
Definition: contact.class.php:373
Contact\fetchRoles
fetchRoles()
Fetch roles (default contact of some companies) for the current contact.
Definition: contact.class.php:1784
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:209
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6280
Contact\fetch
fetch($id, $user=null, $ref_ext='', $email='', $loadalsoroles=0)
Load object contact.
Definition: contact.class.php:985
dol_string_nohtmltag
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Definition: functions.lib.php:6965
Contact\getNomUrl
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,...
Definition: contact.class.php:1484
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1669
Contact
Class to manage contact/addresses.
Definition: contact.class.php:41
Contact\setGenderFromCivility
setGenderFromCivility()
Set the property "gender" of this class, based on the property "civility_id" or use property "civilit...
Definition: contact.class.php:1176
dol_format_address
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.
Definition: functions.lib.php:2480
CommonObject\fetch_optionals
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...
Definition: commonobject.class.php:6129
Contact\getCivilityLabel
getCivilityLabel()
Return civility label of contact.
Definition: contact.class.php:1568
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3923
CommonSocialNetworks
trait CommonSocialNetworks
Superclass for social networks.
Definition: commonsocialnetworks.class.php:29
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:166
ref
$object ref
Definition: info.php:78
Contact\_load_ldap_dn
_load_ldap_dn($info, $mode=0)
Retourne chaine DN complete dans l'annuaire LDAP pour l'objet.
Definition: contact.class.php:767
User
Class to manage Dolibarr users.
Definition: user.class.php:46
Contact\_load_ldap_info
_load_ldap_info()
Initialise tableau info (tableau des attributs LDAP)
Definition: contact.class.php:790
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6240
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10816
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4398
Contact\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: contact.class.php:1656
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2982
Contact\getTooltipContentArray
getTooltipContentArray($params)
getTooltipContentArray
Definition: contact.class.php:1431
Contact\getLibStatut
getLibStatut($mode)
Return label of contact status.
Definition: contact.class.php:1587
Contact\getLibProspCommStatut
getLibProspCommStatut($mode=0, $label='')
Return status of prospect.
Definition: contact.class.php:2015
dol_print_phone
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0)
Format phone numbers according to country.
Definition: functions.lib.php:3327
Form\showphoto
static showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='', $addlinktofullsize=1, $cache=0, $forcecapture='', $noexternsourceoverwrite=0)
Definition: html.form.class.php:9561
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5913
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:115
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8621
Contact\getNoEmail
getNoEmail()
get "blacklist" mailing status set no_email attribut to 1 or 0
Definition: contact.class.php:2151
Contact\LibPubPriv
LibPubPriv($status)
Return translated label of Public or Private.
Definition: contact.class.php:1637
Contact\setstatus
setstatus($status)
Change status of a user.
Definition: contact.class.php:1704