dolibarr  17.0.4
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 
36 
40 class Contact extends CommonObject
41 {
45  public $element = 'contact';
46 
50  public $table_element = 'socpeople';
51 
56  public $ismultientitymanaged = 1;
57 
61  public $picto = 'contact';
62 
87  // BEGIN MODULEBUILDER PROPERTIES
91  public $fields = array(
92  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
93  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>3, 'notnull'=>1, 'position'=>30, 'index'=>1),
94  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>3, 'position'=>35),
95  'civility' =>array('type'=>'varchar(6)', 'label'=>'Civility', 'enabled'=>1, 'visible'=>3, 'position'=>40),
96  'lastname' =>array('type'=>'varchar(50)', 'label'=>'Lastname', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'showoncombobox'=>1, 'searchall'=>1),
97  'firstname' =>array('type'=>'varchar(50)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'showoncombobox'=>1, 'searchall'=>1),
98  'poste' =>array('type'=>'varchar(80)', 'label'=>'PostOrFunction', 'enabled'=>1, 'visible'=>-1, 'position'=>52),
99  'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
100  'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>1, 'position'=>60),
101  'town' =>array('type'=>'text', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
102  'fk_departement' =>array('type'=>'integer', 'label'=>'Fk departement', 'enabled'=>1, 'visible'=>3, 'position'=>70),
103  'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>3, 'position'=>75),
104  'fk_soc' =>array('type'=>'integer', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>1, 'position'=>77, 'searchall'=>1),
105  'birthday' =>array('type'=>'date', 'label'=>'Birthday', 'enabled'=>1, 'visible'=>3, 'position'=>80),
106  'phone' =>array('type'=>'varchar(30)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>1, 'position'=>90, 'searchall'=>1),
107  'phone_perso' =>array('type'=>'varchar(30)', 'label'=>'PhonePerso', 'enabled'=>1, 'visible'=>-1, 'position'=>95, 'searchall'=>1),
108  'phone_mobile' =>array('type'=>'varchar(30)', 'label'=>'PhoneMobile', 'enabled'=>1, 'visible'=>1, 'position'=>100, 'searchall'=>1),
109  'fax' =>array('type'=>'varchar(30)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'searchall'=>1),
110  'email' =>array('type'=>'varchar(255)', 'label'=>'Email', 'enabled'=>1, 'visible'=>1, 'position'=>110, 'searchall'=>1),
111  'socialnetworks' =>array('type'=>'text', 'label'=>'SocialNetworks', 'enabled'=>1, 'visible'=>3, 'position'=>115),
112  'photo' =>array('type'=>'varchar(255)', 'label'=>'Photo', 'enabled'=>1, 'visible'=>3, 'position'=>170),
113  'priv' =>array('type'=>'smallint(6)', 'label'=>'ContactVisibility', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>175),
114  'fk_stcommcontact' =>array('type'=>'integer', 'label'=>'ProspectStatus', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>220),
115  'fk_prospectcontactlevel' =>array('type'=>'varchar(12)', 'label'=>'ProspectLevel', 'enabled'=>1, 'visible'=>-1, 'position'=>255),
116  'no_email' =>array('type'=>'smallint(6)', 'label'=>'No_Email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),
117  'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>3, 'position'=>195, 'searchall'=>1),
118  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>3, 'position'=>200, 'searchall'=>1),
119  'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>3, 'position'=>205),
120  'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>3, 'position'=>210),
121  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>300),
122  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>305),
123  'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>3, 'position'=>310),
124  'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>3, 'position'=>315),
125  'statut' =>array('type'=>'tinyint(4)', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),
126  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'position'=>1000),
127  );
128 
129  public $civility_id; // In fact we store civility_code
130  public $civility_code;
131  public $civility;
132 
138  public $civilite;
139 
143  public $gender;
144 
148  public $address;
149 
153  public $zip;
154 
158  public $town;
159 
163  public $state_id;
164 
168  public $state_code;
169 
173  public $state;
174 
175  public $poste; // Position
176 
180  public $socid; // both socid and fk_soc are used
181  public $fk_soc; // both socid and fk_soc are used
182 
186  public $statut;
187 
188  public $code;
189 
194  public $email;
195 
200  public $url;
201 
206  public $no_email;
207 
211  public $socialnetworks;
212 
218  public $skype;
219 
225  public $twitter;
226 
232  public $facebook;
233 
239  public $linkedin;
240 
246  public $jabberid;
247 
251  public $photo;
252 
256  public $phone_pro;
257 
261  public $phone_perso;
262 
266  public $phone_mobile;
267 
271  public $fax;
272 
277  public $priv;
278 
279  public $birthday;
280  public $default_lang;
281 
285  public $ref_facturation;
286 
290  public $ref_contrat;
291 
295  public $ref_commande;
296 
300  public $ref_propal;
301 
305  public $user_id;
306 
310  public $user_login;
311 
312  // END MODULEBUILDER PROPERTIES
313 
314 
319  public $oldcopy; // To contains a clone of this when we need to save old properties of object
320 
324  public $roles;
325 
326  public $cacheprospectstatus = array();
327  public $fk_prospectlevel;
328  public $stcomm_id;
329  public $statut_commercial;
330 
334  public $stcomm_picto;
335 
336 
342  public function __construct($db)
343  {
344  global $conf, $langs;
345 
346  $this->db = $db;
347  $this->statut = 1; // By default, status is enabled
348 
349  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
350  $this->fields['rowid']['visible'] = 0;
351  }
352  if (!isModEnabled('mailing')) {
353  $this->fields['no_email']['enabled'] = 0;
354  }
355  // typical ['s.nom'] is used for third-parties
356  if (empty($conf->global->SOCIETE_DISABLE_CONTACTS)) {
357  $this->fields['fk_soc']['enabled'] = 0;
358  $this->fields['fk_soc']['searchall'] = 0;
359  }
360 
361  if (empty($conf->global->THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES)) { // Default behaviour
362  $this->fields['fk_stcommcontact']['enabled'] = 0;
363  $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
364  }
365 
366  // Unset fields that are disabled
367  foreach ($this->fields as $key => $val) {
368  if (isset($val['enabled']) && empty($val['enabled'])) {
369  unset($this->fields[$key]);
370  }
371  }
372 
373  // Translate some data of arrayofkeyval
374  /*if (is_object($langs))
375  {
376  foreach($this->fields as $key => $val)
377  {
378  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
379  {
380  foreach($val['arrayofkeyval'] as $key2 => $val2)
381  {
382  $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
383  }
384  }
385  }
386  }*/
387  }
388 
389  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
395  public function load_state_board()
396  {
397  // phpcs:enable
398  global $user, $hookmanager;
399 
400  $this->nb = array();
401  $clause = "WHERE";
402 
403  $sql = "SELECT count(sp.rowid) as nb";
404  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
405  if (empty($user->rights->societe->client->voir) && !$user->socid) {
406  $sql .= ", ".MAIN_DB_PREFIX."societe as s";
407  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
408  $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
409  $clause = "AND";
410  }
411  $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
412  $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
413  if ($user->socid > 0) {
414  $sql .= " AND sp.fk_soc = ".((int) $user->socid);
415  }
416  // Add where from hooks
417  if (is_object($hookmanager)) {
418  $parameters = array();
419  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
420  $sql .= $hookmanager->resPrint;
421  }
422 
423  $resql = $this->db->query($sql);
424  if ($resql) {
425  while ($obj = $this->db->fetch_object($resql)) {
426  $this->nb["contacts"] = $obj->nb;
427  }
428  $this->db->free($resql);
429  return 1;
430  } else {
431  dol_print_error($this->db);
432  $this->error = $this->db->lasterror();
433  return -1;
434  }
435  }
436 
444  public function create($user, $notrigger = 0)
445  {
446  global $conf;
447 
448  $error = 0;
449  $now = dol_now();
450 
451  $this->db->begin();
452 
453  // Clean parameters
454  $this->lastname = $this->lastname ?trim($this->lastname) : trim($this->name);
455  $this->firstname = trim($this->firstname);
456  $this->setUpperOrLowerCase();
457  if (empty($this->socid)) {
458  $this->socid = 0;
459  }
460  if (empty($this->priv)) {
461  $this->priv = 0;
462  }
463  if (empty($this->statut)) {
464  $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
465  }
466 
467  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
468 
469  $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
470  $sql .= " datec";
471  $sql .= ", fk_soc";
472  $sql .= ", lastname";
473  $sql .= ", firstname";
474  $sql .= ", fk_user_creat";
475  $sql .= ", priv";
476  $sql .= ", fk_stcommcontact";
477  $sql .= ", statut";
478  $sql .= ", canvas";
479  $sql .= ", entity";
480  $sql .= ", ref_ext";
481  $sql .= ", import_key";
482  $sql .= ") VALUES (";
483  $sql .= "'".$this->db->idate($now)."',";
484  if ($this->socid > 0) {
485  $sql .= " ".((int) $this->socid).",";
486  } else {
487  $sql .= "null,";
488  }
489  $sql .= "'".$this->db->escape($this->lastname)."',";
490  $sql .= "'".$this->db->escape($this->firstname)."',";
491  $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
492  $sql .= " ".((int) $this->priv).",";
493  $sql .= " 0,";
494  $sql .= " ".((int) $this->statut).",";
495  $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
496  $sql .= " ".((int) $this->entity).",";
497  $sql .= "'".$this->db->escape($this->ref_ext)."',";
498  $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
499  $sql .= ")";
500 
501  dol_syslog(get_class($this)."::create", LOG_DEBUG);
502  $resql = $this->db->query($sql);
503  if ($resql) {
504  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
505 
506  if (!$error) {
507  $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
508  if ($result < 0) {
509  $error++;
510  $this->error = $this->db->lasterror();
511  }
512  }
513 
514  if (!$error) {
515  $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
516  if ($result < 0) {
517  $error++;
518  $this->error = $this->db->lasterror();
519  }
520  }
521 
522  if (!$error && !$notrigger) {
523  // Call trigger
524  $result = $this->call_trigger('CONTACT_CREATE', $user);
525  if ($result < 0) {
526  $error++;
527  }
528  // End call triggers
529  }
530 
531  if (!$error) {
532  $this->db->commit();
533  return $this->id;
534  } else {
535  $this->db->rollback();
536  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
537  return -2;
538  }
539  } else {
540  $this->error = $this->db->lasterror();
541 
542  $this->db->rollback();
543  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
544  return -1;
545  }
546  }
547 
558  public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
559  {
560  global $conf, $langs, $hookmanager;
561 
562  $error = 0;
563 
564  $this->id = $id;
565 
566  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
567 
568  // Clean parameters
569  $this->ref_ext = trim($this->ref_ext);
570  $this->lastname = trim($this->lastname) ?trim($this->lastname) : trim($this->lastname);
571  $this->firstname = trim($this->firstname);
572  $this->email = trim($this->email);
573  $this->phone_pro = trim($this->phone_pro);
574  $this->phone_perso = trim($this->phone_perso);
575  $this->phone_mobile = trim($this->phone_mobile);
576  $this->photo = trim($this->photo);
577  $this->fax = trim($this->fax);
578  $this->zip = (empty($this->zip) ? '' : trim($this->zip));
579  $this->town = (empty($this->town) ? '' : trim($this->town));
580  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
581  if (empty($this->statut)) {
582  $this->statut = 0;
583  }
584  if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
585  $this->civility_code = $this->civility_id; // For backward compatibility
586  }
587  $this->setUpperOrLowerCase();
588  $this->db->begin();
589 
590  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
591  if ($this->socid > 0) {
592  $sql .= " fk_soc = ".((int) $this->socid).",";
593  } elseif ($this->socid == -1) {
594  $sql .= " fk_soc = NULL,";
595  }
596  $sql .= " civility='".$this->db->escape($this->civility_code)."'";
597  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
598  $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
599  $sql .= ", address='".$this->db->escape($this->address)."'";
600  $sql .= ", zip='".$this->db->escape($this->zip)."'";
601  $sql .= ", town='".$this->db->escape($this->town)."'";
602  $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
603  $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
604  $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
605  $sql .= ", poste='".$this->db->escape($this->poste)."'";
606  $sql .= ", fax='".$this->db->escape($this->fax)."'";
607  $sql .= ", email='".$this->db->escape($this->email)."'";
608  $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
609  $sql .= ", photo='".$this->db->escape($this->photo)."'";
610  $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
611  $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
612  $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
613  $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
614  $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
615  $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
616  $sql .= ", priv = '".$this->db->escape($this->priv)."'";
617  $sql .= ", fk_prospectcontactlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
618  if (isset($this->stcomm_id)) {
619  $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
620  }
621  $sql .= ", statut = ".((int) $this->statut);
622  $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
623  $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
624  $sql .= ", entity = ".((int) $this->entity);
625  $sql .= " WHERE rowid = ".((int) $id);
626 
627  dol_syslog(get_class($this)."::update", LOG_DEBUG);
628  $result = $this->db->query($sql);
629  if ($result) {
630  unset($this->country_code);
631  unset($this->country);
632  unset($this->state_code);
633  unset($this->state);
634 
635  $action = 'update';
636 
637  // Actions on extra fields
638  if (!$error) {
639  $result = $this->insertExtraFields();
640  if ($result < 0) {
641  $error++;
642  }
643  }
644 
645  if (!$error) {
646  $result = $this->updateRoles();
647  if ($result < 0) {
648  $error++;
649  }
650  }
651 
652  if (!$error && $this->user_id > 0) {
653  // If contact is linked to a user
654  $tmpobj = new User($this->db);
655  $tmpobj->fetch($this->user_id);
656  $usermustbemodified = 0;
657  if ($tmpobj->office_phone != $this->phone_pro) {
658  $tmpobj->office_phone = $this->phone_pro;
659  $usermustbemodified++;
660  }
661  if ($tmpobj->office_fax != $this->fax) {
662  $tmpobj->office_fax = $this->fax;
663  $usermustbemodified++;
664  }
665  if ($tmpobj->address != $this->address) {
666  $tmpobj->address = $this->address;
667  $usermustbemodified++;
668  }
669  if ($tmpobj->town != $this->town) {
670  $tmpobj->town = $this->town;
671  $usermustbemodified++;
672  }
673  if ($tmpobj->zip != $this->zip) {
674  $tmpobj->zip = $this->zip;
675  $usermustbemodified++;
676  }
677  if ($tmpobj->zip != $this->zip) {
678  $tmpobj->state_id = $this->state_id;
679  $usermustbemodified++;
680  }
681  if ($tmpobj->country_id != $this->country_id) {
682  $tmpobj->country_id = $this->country_id;
683  $usermustbemodified++;
684  }
685  if ($tmpobj->email != $this->email) {
686  $tmpobj->email = $this->email;
687  $usermustbemodified++;
688  }
689  if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
690  $tmpobj->socialnetworks = $this->socialnetworks;
691  $usermustbemodified++;
692  }
693  // if ($tmpobj->skype != $this->skype) {
694  // $tmpobj->skype = $this->skype;
695  // $usermustbemodified++;
696  // }
697  // if ($tmpobj->twitter != $this->twitter) {
698  // $tmpobj->twitter = $this->twitter;
699  // $usermustbemodified++;
700  // }
701  // if ($tmpobj->facebook != $this->facebook) {
702  // $tmpobj->facebook = $this->facebook;
703  // $usermustbemodified++;
704  // }
705  // if ($tmpobj->linkedin != $this->linkedin) {
706  // $tmpobj->linkedin = $this->linkedin;
707  // $usermustbemodified++;
708  // }
709  if ($usermustbemodified) {
710  $result = $tmpobj->update($user, 0, 1, 1, 1);
711  if ($result < 0) {
712  $error++;
713  }
714  }
715  }
716 
717  if (!$error && !$notrigger) {
718  // Call trigger
719  $result = $this->call_trigger('CONTACT_MODIFY', $user);
720  if ($result < 0) {
721  $error++;
722  }
723  // End call triggers
724  }
725 
726  if (!$error) {
727  $this->db->commit();
728  return 1;
729  } else {
730  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
731  $this->db->rollback();
732  return -$error;
733  }
734  } else {
735  $this->error = $this->db->lasterror().' sql='.$sql;
736  $this->db->rollback();
737  return -1;
738  }
739  }
740 
741 
742  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
743  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
753  public function _load_ldap_dn($info, $mode = 0)
754  {
755  // phpcs:enable
756  global $conf;
757  $dn = '';
758  if ($mode == 0) {
759  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;
760  } elseif ($mode == 1) {
761  $dn = $conf->global->LDAP_CONTACT_DN;
762  } elseif ($mode == 2) {
763  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS];
764  }
765  return $dn;
766  }
767 
768 
769  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
770  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
776  public function _load_ldap_info()
777  {
778  // phpcs:enable
779  global $conf, $langs;
780 
781  $info = array();
782 
783  // Object classes
784  $info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);
785 
786  $this->fullname = $this->getFullName($langs);
787 
788  // Fields
789  if ($this->fullname && !empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME)) {
790  $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
791  }
792  if ($this->lastname && !empty($conf->global->LDAP_CONTACT_FIELD_NAME)) {
793  $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname;
794  }
795  if ($this->firstname && !empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME)) {
796  $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
797  }
798 
799  if ($this->poste) {
800  $info["title"] = $this->poste;
801  }
802  if ($this->socid > 0) {
803  $soc = new Societe($this->db);
804  $soc->fetch($this->socid);
805 
806  $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->name;
807  if ($soc->client == 1) {
808  $info["businessCategory"] = "Customers";
809  }
810  if ($soc->client == 2) {
811  $info["businessCategory"] = "Prospects";
812  }
813  if ($soc->fournisseur == 1) {
814  $info["businessCategory"] = "Suppliers";
815  }
816  }
817  if ($this->address && !empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS)) {
818  $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
819  }
820  if ($this->zip && !empty($conf->global->LDAP_CONTACT_FIELD_ZIP)) {
821  $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->zip;
822  }
823  if ($this->town && !empty($conf->global->LDAP_CONTACT_FIELD_TOWN)) {
824  $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->town;
825  }
826  if ($this->country_code && !empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY)) {
827  $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code;
828  }
829  if ($this->phone_pro && !empty($conf->global->LDAP_CONTACT_FIELD_PHONE)) {
830  $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
831  }
832  if ($this->phone_perso && !empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) {
833  $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
834  }
835  if ($this->phone_mobile && !empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) {
836  $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
837  }
838  if ($this->fax && !empty($conf->global->LDAP_CONTACT_FIELD_FAX)) {
839  $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
840  }
841  if ($this->skype && !empty($conf->global->LDAP_CONTACT_FIELD_SKYPE)) {
842  $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype;
843  }
844  if ($this->note_private && !empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) {
845  $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
846  }
847  if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL)) {
848  $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
849  }
850 
851  if ($conf->global->LDAP_SERVER_TYPE == 'egroupware') {
852  $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
853 
854  $info['uidnumber'] = $this->id;
855 
856  $info['phpgwTz'] = 0;
857  $info['phpgwMailType'] = 'INTERNET';
858  $info['phpgwMailHomeType'] = 'INTERNET';
859 
860  $info["phpgwContactTypeId"] = 'n';
861  $info["phpgwContactCatId"] = 0;
862  $info["phpgwContactAccess"] = "public";
863 
864  if (dol_strlen($this->egroupware_id) == 0) {
865  $this->egroupware_id = 1;
866  }
867 
868  $info["phpgwContactOwner"] = $this->egroupware_id;
869 
870  if ($this->email) {
871  $info["rfc822Mailbox"] = $this->email;
872  }
873  if ($this->phone_mobile) {
874  $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
875  }
876  }
877 
878  return $info;
879  }
880 
881 
882  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
891  public function update_perso($id, $user = null, $notrigger = 0)
892  {
893  // phpcs:enable
894  $error = 0;
895  $result = false;
896 
897  $this->db->begin();
898 
899  // Mis a jour contact
900  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
901  $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
902  $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
903  if ($user) {
904  $sql .= ", fk_user_modif = ".((int) $user->id);
905  }
906  $sql .= " WHERE rowid = ".((int) $id);
907 
908  dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
909  $resql = $this->db->query($sql);
910  if (!$resql) {
911  $error++;
912  $this->error = $this->db->lasterror();
913  }
914 
915  if ($user) {
916  // Update birthday alert
917  if (!empty($this->birthday_alert)) {
918  //check existing
919  $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
920  $result_check = $this->db->query($sql_check);
921  if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
922  //insert
923  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
924  $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
925  $result = $this->db->query($sql);
926  if (!$result) {
927  $error++;
928  $this->error = $this->db->lasterror();
929  }
930  } else {
931  $result = true;
932  }
933  } else {
934  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
935  $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
936  $result = $this->db->query($sql);
937  if (!$result) {
938  $error++;
939  $this->error = $this->db->lasterror();
940  }
941  }
942  }
943 
944  if (!$error && !$notrigger) {
945  // Call trigger
946  $result = $this->call_trigger('CONTACT_MODIFY', $user);
947  if ($result < 0) {
948  $error++;
949  }
950  // End call triggers
951  }
952 
953  if (!$error) {
954  $this->db->commit();
955  return 1;
956  } else {
957  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
958  $this->db->rollback();
959  return -$error;
960  }
961  }
962 
963 
974  public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
975  {
976  global $langs;
977 
978  dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
979 
980  if (empty($id) && empty($ref_ext) && empty($email)) {
981  $this->error = 'BadParameter';
982  return -1;
983  }
984 
985  $langs->loadLangs(array("dict", "companies"));
986 
987  $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.lastname, c.firstname,";
988  $sql .= " c.address, c.statut, c.zip, c.town,";
989  $sql .= " c.fk_pays as country_id,";
990  $sql .= " c.fk_departement as state_id,";
991  $sql .= " c.birthday,";
992  $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
993  $sql .= " c.socialnetworks,";
994  $sql .= " c.photo,";
995  $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
996  $sql .= " c.fk_prospectcontactlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
997  $sql .= " c.import_key,";
998  $sql .= " c.datec as date_creation, c.tms as date_modification,";
999  $sql .= " co.label as country, co.code as country_code,";
1000  $sql .= " d.nom as state, d.code_departement as state_code,";
1001  $sql .= " u.rowid as user_id, u.login as user_login,";
1002  $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1003  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1004  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1005  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1006  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1007  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1008  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1009  if ($id) {
1010  $sql .= " WHERE c.rowid = ".((int) $id);
1011  } else {
1012  $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1013  if ($ref_ext) {
1014  $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1015  }
1016  if ($email) {
1017  $sql .= " AND c.email = '".$this->db->escape($email)."'";
1018  }
1019  }
1020 
1021  $resql = $this->db->query($sql);
1022  if ($resql) {
1023  $num = $this->db->num_rows($resql);
1024  if ($num > 1) {
1025  $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1026  dol_syslog($this->error, LOG_ERR);
1027 
1028  return 2;
1029  } elseif ($num) { // $num = 1
1030  $obj = $this->db->fetch_object($resql);
1031 
1032  $this->id = $obj->rowid;
1033  $this->entity = $obj->entity;
1034  $this->ref = $obj->rowid;
1035  $this->ref_ext = $obj->ref_ext;
1036 
1037  $this->civility_code = $obj->civility_code;
1038  $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1039 
1040  $this->lastname = $obj->lastname;
1041  $this->firstname = $obj->firstname;
1042  $this->address = $obj->address;
1043  $this->zip = $obj->zip;
1044  $this->town = $obj->town;
1045 
1046  $this->date_creation = $this->db->jdate($obj->date_creation);
1047  $this->date_modification = $this->db->jdate($obj->date_modification);
1048 
1049  $this->state_id = $obj->state_id;
1050  $this->state_code = $obj->state_code;
1051  $this->state = $obj->state;
1052 
1053  $this->country_id = $obj->country_id;
1054  $this->country_code = $obj->country_id ? $obj->country_code : '';
1055  $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1056 
1057  $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1058  $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1059  $this->socname = $obj->socname;
1060  $this->poste = $obj->poste;
1061  $this->statut = $obj->statut;
1062 
1063  $this->fk_prospectlevel = $obj->fk_prospectcontactlevel;
1064 
1065  $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1066  $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1067  $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1068  $this->statut_commercial = $libelle; // libelle statut commercial
1069  $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1070 
1071  $this->phone_pro = trim($obj->phone);
1072  $this->fax = trim($obj->fax);
1073  $this->phone_perso = trim($obj->phone_perso);
1074  $this->phone_mobile = trim($obj->phone_mobile);
1075 
1076  $this->email = $obj->email;
1077  $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1078  $this->photo = $obj->photo;
1079  $this->priv = $obj->priv;
1080  $this->mail = $obj->email;
1081 
1082  $this->birthday = $this->db->jdate($obj->birthday);
1083  $this->note = $obj->note_private; // deprecated
1084  $this->note_private = $obj->note_private;
1085  $this->note_public = $obj->note_public;
1086  $this->default_lang = $obj->default_lang;
1087  $this->user_id = $obj->user_id;
1088  $this->user_login = $obj->user_login;
1089  $this->canvas = $obj->canvas;
1090 
1091  $this->import_key = $obj->import_key;
1092 
1093  // Define gender according to civility
1094  $this->setGenderFromCivility();
1095 
1096  // Search Dolibarr user linked to this contact
1097  $sql = "SELECT u.rowid ";
1098  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1099  $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1100 
1101  $resql = $this->db->query($sql);
1102  if ($resql) {
1103  if ($this->db->num_rows($resql)) {
1104  $uobj = $this->db->fetch_object($resql);
1105 
1106  $this->user_id = $uobj->rowid;
1107  }
1108  $this->db->free($resql);
1109  } else {
1110  $this->error = $this->db->error();
1111  return -1;
1112  }
1113 
1114  // Retrieve all extrafield
1115  // fetch optionals attributes and labels
1116  $this->fetch_optionals();
1117 
1118  // Load also alerts of this user
1119  if ($user) {
1120  $sql = "SELECT fk_user";
1121  $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1122  $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1123 
1124  $resql = $this->db->query($sql);
1125  if ($resql) {
1126  if ($this->db->num_rows($resql)) {
1127  $obj = $this->db->fetch_object($resql);
1128 
1129  $this->birthday_alert = 1;
1130  }
1131  $this->db->free($resql);
1132  } else {
1133  $this->error = $this->db->error();
1134  return -1;
1135  }
1136  }
1137 
1138  // Load also roles of this address
1139  if ($loadalsoroles) {
1140  $resultRole = $this->fetchRoles();
1141  if ($resultRole < 0) {
1142  return $resultRole;
1143  }
1144  }
1145 
1146  return 1;
1147  } else {
1148  $this->error = $langs->trans("RecordNotFound");
1149  return 0;
1150  }
1151  } else {
1152  $this->error = $this->db->error();
1153  return -1;
1154  }
1155  }
1156 
1157 
1158 
1165  public function setGenderFromCivility()
1166  {
1167  unset($this->gender);
1168 
1169  if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1170  $this->gender = 'man';
1171  } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1172  $this->gender = 'woman';
1173  }
1174  }
1175 
1176  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1186  public function load_ref_elements()
1187  {
1188  // phpcs:enable
1189  // Compte les elements pour lesquels il est contact
1190  $sql = "SELECT tc.element, count(ec.rowid) as nb";
1191  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1192  $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1193  $sql .= " AND fk_socpeople = ".((int) $this->id);
1194  $sql .= " AND tc.source = 'external'";
1195  $sql .= " GROUP BY tc.element";
1196 
1197  dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1198 
1199  $resql = $this->db->query($sql);
1200  if ($resql) {
1201  while ($obj = $this->db->fetch_object($resql)) {
1202  if ($obj->nb) {
1203  if ($obj->element == 'facture') {
1204  $this->ref_facturation = $obj->nb;
1205  } elseif ($obj->element == 'contrat') {
1206  $this->ref_contrat = $obj->nb;
1207  } elseif ($obj->element == 'commande') {
1208  $this->ref_commande = $obj->nb;
1209  } elseif ($obj->element == 'propal') {
1210  $this->ref_propal = $obj->nb;
1211  }
1212  }
1213  }
1214  $this->db->free($resql);
1215  return 0;
1216  } else {
1217  $this->error = $this->db->lasterror();
1218  return -1;
1219  }
1220  }
1221 
1229  public function delete($notrigger = 0)
1230  {
1231  global $conf, $langs, $user;
1232 
1233  $error = 0;
1234 
1235  $this->db->begin();
1236 
1237  if (!$error && !$notrigger) {
1238  // Call trigger
1239  $result = $this->call_trigger('CONTACT_DELETE', $user);
1240  if ($result < 0) {
1241  $error++;
1242  }
1243  // End call triggers
1244  }
1245 
1246  if (!$error) {
1247  // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1248  $sql = "SELECT ec.rowid";
1249  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1250  $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1251  $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1252  $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1253  $sql .= " AND tc.source='external'";
1254  dol_syslog(__METHOD__, LOG_DEBUG);
1255  $resql = $this->db->query($sql);
1256  if ($resql) {
1257  $num = $this->db->num_rows($resql);
1258 
1259  $i = 0;
1260  while ($i < $num && !$error) {
1261  $obj = $this->db->fetch_object($resql);
1262 
1263  $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1264  $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1265  dol_syslog(__METHOD__, LOG_DEBUG);
1266  $result = $this->db->query($sqldel);
1267  if (!$result) {
1268  $error++;
1269  $this->error = $this->db->error().' sql='.$sqldel;
1270  }
1271 
1272  $i++;
1273  }
1274  } else {
1275  $error++;
1276  $this->error = $this->db->error().' sql='.$sql;
1277  }
1278  }
1279 
1280  if (!$error) {
1281  // Remove Roles
1282  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1283  dol_syslog(__METHOD__, LOG_DEBUG);
1284  $resql = $this->db->query($sql);
1285  if (!$resql) {
1286  $error++;
1287  $this->error .= $this->db->lasterror();
1288  $errorflag = -1;
1289  }
1290  }
1291 
1292  if (!$error) {
1293  // Remove Roles
1294  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1295  dol_syslog(__METHOD__, LOG_DEBUG);
1296  $resql = $this->db->query($sql);
1297  if (!$resql) {
1298  $error++;
1299  $this->error .= $this->db->lasterror();
1300  $errorflag = -1;
1301  }
1302  }
1303 
1304  if (!$error) {
1305  // Remove category
1306  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1307  dol_syslog(__METHOD__, LOG_DEBUG);
1308  $resql = $this->db->query($sql);
1309  if (!$resql) {
1310  $error++;
1311  $this->error .= $this->db->lasterror();
1312  $errorflag = -1;
1313  }
1314  }
1315 
1316  if (!$error) {
1317  $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1318  $sql .= " WHERE rowid = ".((int) $this->id);
1319  dol_syslog(__METHOD__, LOG_DEBUG);
1320  $result = $this->db->query($sql);
1321  if (!$result) {
1322  $error++;
1323  $this->error = $this->db->error().' sql='.$sql;
1324  }
1325  }
1326 
1327  // Removed extrafields
1328  if (!$error) {
1329  // For avoid conflicts if trigger used
1330  $result = $this->deleteExtraFields();
1331  if ($result < 0) {
1332  $error++;
1333  }
1334  }
1335 
1336  if (!$error) {
1337  $this->db->commit();
1338  return 1;
1339  } else {
1340  $this->db->rollback();
1341  dol_syslog("Error ".$this->error, LOG_ERR);
1342  return -1;
1343  }
1344  }
1345 
1346 
1353  public function info($id)
1354  {
1355  $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1356  $sql .= " c.tms as tms, c.fk_user_modif";
1357  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1358  $sql .= " WHERE c.rowid = ".((int) $id);
1359 
1360  $resql = $this->db->query($sql);
1361  if ($resql) {
1362  if ($this->db->num_rows($resql)) {
1363  $obj = $this->db->fetch_object($resql);
1364 
1365  $this->id = $obj->rowid;
1366 
1367  if ($obj->fk_user_creat) {
1368  $cuser = new User($this->db);
1369  $cuser->fetch($obj->fk_user_creat);
1370  $this->user_creation = $cuser;
1371  }
1372 
1373  if ($obj->fk_user_modif) {
1374  $muser = new User($this->db);
1375  $muser->fetch($obj->fk_user_modif);
1376  $this->user_modification = $muser;
1377  }
1378 
1379  $this->date_creation = $this->db->jdate($obj->datec);
1380  $this->date_modification = $this->db->jdate($obj->tms);
1381  }
1382 
1383  $this->db->free($resql);
1384  } else {
1385  print $this->db->error();
1386  }
1387  }
1388 
1394  public function getNbOfEMailings()
1395  {
1396  $sql = "SELECT count(mc.email) as nb";
1397  $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1398  $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1399  $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1400 
1401  $resql = $this->db->query($sql);
1402  if ($resql) {
1403  $obj = $this->db->fetch_object($resql);
1404  $nb = $obj->nb;
1405 
1406  $this->db->free($resql);
1407  return $nb;
1408  } else {
1409  $this->error = $this->db->error();
1410  return -1;
1411  }
1412  }
1413 
1427  public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1428  {
1429  global $conf, $langs, $hookmanager;
1430 
1431  $result = ''; $label = '';
1432  if (!empty($this->photo) && class_exists('Form')) {
1433  $label .= '<div class="photointooltip floatright">';
1434  $label .= 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.
1435  $label .= '</div>';
1436  //$label .= '<div style="clear: both;"></div>';
1437  }
1438 
1439  $label .= img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u>';
1440  $label .= ' '.$this->getLibStatut(4);
1441  $label .= '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1442  //if ($this->civility_id) $label.= '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate cibilty_id code
1443  if (!empty($this->poste)) {
1444  $label .= '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1445  }
1446  $label .= '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1447  $phonelist = array();
1448  $country_code = empty($this->country_code) ? '': $this->country_code;
1449  if ($this->phone_pro) {
1450  $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1451  }
1452  if ($this->phone_mobile) {
1453  $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1454  }
1455  if ($this->phone_perso) {
1456  $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1457  }
1458  $label .= '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1459  $label .= '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1460 
1461  $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1462 
1463  if ($option !== 'nolink') {
1464  // Add param to save lastsearch_values or not
1465  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1466  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1467  $add_save_lastsearch_values = 1;
1468  }
1469  if ($add_save_lastsearch_values) {
1470  $url .= '&save_lastsearch_values=1';
1471  }
1472  }
1473 
1474  $url .= $moreparam;
1475 
1476  $linkclose = "";
1477  if (empty($notooltip)) {
1478  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1479  $label = $langs->trans("ShowContact");
1480  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1481  }
1482  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1483  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1484  }
1485 
1486  $linkstart = '<a href="'.$url.'"';
1487  $linkstart .= $linkclose.'>';
1488  $linkend = '</a>';
1489 
1490  if ($option == 'xxx') {
1491  $linkstart = '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$this->id.$moreparam.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1492  $linkend = '</a>';
1493  }
1494 
1495  $result .= $linkstart;
1496  if ($withpicto) {
1497  if ($withpicto < 0) {
1498  $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>';
1499  } else {
1500  $result .= img_object(($notooltip ? '' : $label), ( $this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1501  }
1502  }
1503  if ($withpicto != 2 && $withpicto != -2) {
1504  $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1505  }
1506  $result .= $linkend;
1507 
1508  global $action;
1509  $hookmanager->initHooks(array('contactdao'));
1510  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1511  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1512  if ($reshook > 0) {
1513  $result = $hookmanager->resPrint;
1514  } else {
1515  $result .= $hookmanager->resPrint;
1516  }
1517 
1518  return $result;
1519  }
1520 
1526  public function getCivilityLabel()
1527  {
1528  global $langs;
1529 
1530  $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1531  if (empty($code)) {
1532  return '';
1533  }
1534 
1535  $langs->load("dict");
1536  return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1537  }
1538 
1545  public function getLibStatut($mode)
1546  {
1547  return $this->LibStatut($this->statut, $mode);
1548  }
1549 
1550  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1558  public function LibStatut($status, $mode)
1559  {
1560  // phpcs:enable
1561  global $langs;
1562 
1563  $labelStatus = array(
1564  0 => 'ActivityCeased',
1565  1 => 'InActivity',
1566  4 => 'InActivity',
1567  5 => 'ActivityCeased',
1568  );
1569  $labelStatusShort = array(
1570  0 => 'ActivityCeased',
1571  1 => 'InActivity',
1572  4 => 'InActivity',
1573  5 => 'ActivityCeased',
1574  );
1575 
1576  $statusType = 'status4';
1577  if ($status == 0 || $status == 5) {
1578  $statusType = 'status5';
1579  }
1580 
1581  $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1582  $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1583 
1584  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1585  }
1586 
1587 
1588  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1595  public function LibPubPriv($status)
1596  {
1597  // phpcs:enable
1598  global $langs;
1599  if ($status == '1') {
1600  return $langs->trans('ContactPrivate');
1601  } else {
1602  return $langs->trans('ContactPublic');
1603  }
1604  }
1605 
1606 
1614  public function initAsSpecimen()
1615  {
1616  // Get first id of existing company and save it into $socid
1617  $socid = 0;
1618  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1619  $resql = $this->db->query($sql);
1620  if ($resql) {
1621  $obj = $this->db->fetch_object($resql);
1622  if ($obj) {
1623  $socid = $obj->rowid;
1624  }
1625  }
1626 
1627  // Initialise parameters
1628  $this->id = 0;
1629  $this->entity = 1;
1630  $this->specimen = 1;
1631  $this->lastname = 'DOLIBARR';
1632  $this->firstname = 'SPECIMEN';
1633  $this->address = '21 jump street';
1634  $this->zip = '99999';
1635  $this->town = 'MyTown';
1636  $this->country_id = 1;
1637  $this->country_code = 'FR';
1638  $this->country = 'France';
1639  $this->email = 'specimen@specimen.com';
1640  $this->socialnetworks = array(
1641  'skype' => 'tom.hanson',
1642  );
1643  $this->phone_pro = '0909090901';
1644  $this->phone_perso = '0909090902';
1645  $this->phone_mobile = '0909090903';
1646  $this->fax = '0909090909';
1647 
1648  $this->note_public = 'This is a comment (public)';
1649  $this->note_private = 'This is a comment (private)';
1650 
1651  $this->socid = $socid;
1652  $this->statut = 1;
1653  return 1;
1654  }
1655 
1662  public function setstatus($status)
1663  {
1664  global $conf, $langs, $user;
1665 
1666  $error = 0;
1667 
1668  // Check parameters
1669  if ($this->statut == $status) {
1670  return 0;
1671  } else {
1672  $this->statut = $status;
1673  }
1674 
1675  $this->db->begin();
1676 
1677  // Desactive utilisateur
1678  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1679  $sql .= " SET statut = ".((int) $this->statut);
1680  $sql .= ", fk_user_modif = ".((int) $user->id);
1681  $sql .= " WHERE rowid = ".((int) $this->id);
1682  $result = $this->db->query($sql);
1683 
1684  dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1685  if ($result) {
1686  // Call trigger
1687  $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1688  if ($result < 0) {
1689  $error++;
1690  }
1691  // End call triggers
1692  }
1693 
1694  if ($error) {
1695  $this->db->rollback();
1696  return -$error;
1697  } else {
1698  $this->db->commit();
1699  return 1;
1700  }
1701  }
1702 
1713  public function setCategories($categories)
1714  {
1715  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1716  return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1717  }
1718 
1727  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1728  {
1729  $tables = array(
1730  'socpeople', 'societe_contacts'
1731  );
1732 
1733  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1734  }
1735 
1743  public function fetchRoles()
1744  {
1745  global $langs;
1746  $error = 0;
1747  $num = 0;
1748 
1749  $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1750  $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1751  $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1752  $sql .= " AND tc.source = 'external' AND tc.active=1";
1753  $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1754  $sql .= " AND sc.entity IN (".getEntity('societe').')';
1755 
1756  $resql = $this->db->query($sql);
1757  if ($resql) {
1758  $this->roles = array();
1759 
1760  $num = $this->db->num_rows($resql);
1761  if ($num > 0) {
1762  while ($obj = $this->db->fetch_object($resql)) {
1763  $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1764  $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1765  $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));
1766  }
1767  }
1768  } else {
1769  $error++;
1770  $this->error = $this->db->lasterror();
1771  $this->errors[] = $this->db->lasterror();
1772  }
1773 
1774  if (empty($error)) {
1775  return $num;
1776  } else {
1777  return $error * -1;
1778  }
1779  }
1780 
1788  public function getContactRoles($element = '')
1789  {
1790  $tab = array();
1791 
1792  if ($element == 'action') {
1793  $element = 'agenda';
1794  }
1795 
1796  $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1797  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1798  $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1799  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1800  $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1801  $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1802  $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1803  $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1804  $sql .= " AND sp.entity IN (".getEntity('contact').")";
1805  $sql .= " AND tc.active = 1";
1806 
1807  dol_syslog(__METHOD__, LOG_DEBUG);
1808  $resql = $this->db->query($sql);
1809  if ($resql) {
1810  $num = $this->db->num_rows($resql);
1811  $i = 0;
1812  while ($i < $num) {
1813  $obj = $this->db->fetch_object($resql);
1814  $tab[] = array('fk_socpeople'=>$obj->id, 'type_contact'=>$obj->fk_c_type_contact);
1815 
1816  $i++;
1817  }
1818 
1819  return $tab;
1820  } else {
1821  $this->error = $this->db->error();
1822  dol_print_error($this->db);
1823  return -1;
1824  }
1825  }
1826 
1834  public function updateRoles()
1835  {
1836  global $conf;
1837 
1838  $error = 0;
1839 
1840  if (!isset($this->roles)) {
1841  return; // Avoid to loose roles when property not set
1842  }
1843 
1844  $this->db->begin();
1845 
1846  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1847 
1848  $result = $this->db->query($sql);
1849  if (!$result) {
1850  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1851  $error++;
1852  } else {
1853  if (count($this->roles) > 0) {
1854  foreach ($this->roles as $keyRoles => $valRoles) {
1855  $idrole = 0;
1856  if (is_array($valRoles)) {
1857  $idrole = $valRoles['id'];
1858  } else {
1859  $idrole = $valRoles;
1860  }
1861 
1862  $socid = 0;
1863  if (is_array($valRoles)) {
1864  $socid = $valRoles['socid'];
1865  } else {
1866  $socid = $this->socid;
1867  }
1868 
1869  if ($socid > 0) {
1870  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1871  $sql .= " (entity,";
1872  $sql .= "date_creation,";
1873  $sql .= "fk_soc,";
1874  $sql .= "fk_c_type_contact,";
1875  $sql .= "fk_socpeople) ";
1876  $sql .= " VALUES (".$conf->entity.",";
1877  $sql .= "'".$this->db->idate(dol_now())."',";
1878  $sql .= $socid.", ";
1879  $sql .= $idrole." , ";
1880  $sql .= $this->id;
1881  $sql .= ")";
1882 
1883  $result = $this->db->query($sql);
1884  if (!$result) {
1885  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1886  $error++;
1887  }
1888  }
1889  }
1890  }
1891  }
1892  if (empty($error)) {
1893  $this->db->commit();
1894  return 1;
1895  } else {
1896  $this->error = implode(' ', $this->errors);
1897  $this->db->rollback();
1898  return $error * -1;
1899  }
1900  }
1901 
1908  public function loadCacheOfProspStatus($active = 1)
1909  {
1910  global $langs;
1911 
1912  $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
1913  if ($active >= 0) {
1914  $sql .= " WHERE active = ".((int) $active);
1915  }
1916  $resql = $this->db->query($sql);
1917  $num = $this->db->num_rows($resql);
1918  $i = 0;
1919  while ($i < $num) {
1920  $obj = $this->db->fetch_object($resql);
1921  $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);
1922  $i++;
1923  }
1924  return 1;
1925  }
1926 
1932  public function getLibProspLevel()
1933  {
1934  return $this->libProspLevel($this->fk_prospectlevel);
1935  }
1936 
1943  public function libProspLevel($fk_prospectlevel)
1944  {
1945  global $langs;
1946 
1947  $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
1948  // If lib not found in language file, we get label from cache/databse
1949  if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel)) {
1950  $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
1951  }
1952  return $lib;
1953  }
1954 
1955 
1963  public function setProspectLevel(User $user)
1964  {
1965  return $this->update($this->id, $user);
1966  }
1967 
1975  public function getLibProspCommStatut($mode = 0, $label = '')
1976  {
1977  return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
1978  }
1979 
1993  public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
1994  {
1995  global $langs;
1996  $langs->load('customers');
1997 
1998  if ($mode == 2) {
1999  if ($statut == '-1' || $statut == 'ST_NO') {
2000  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2001  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2002  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2003  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2004  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2005  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2006  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2007  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2008  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2009  } else {
2010  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);
2011  }
2012  }
2013  if ($mode == 3) {
2014  if ($statut == '-1' || $statut == 'ST_NO') {
2015  return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2016  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2017  return img_action($langs->trans("StatusProspect0"), 0, $picto);
2018  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2019  return img_action($langs->trans("StatusProspect1"), 1, $picto);
2020  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2021  return img_action($langs->trans("StatusProspect2"), 2, $picto);
2022  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2023  return img_action($langs->trans("StatusProspect3"), 3, $picto);
2024  } else {
2025  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);
2026  }
2027  }
2028  if ($mode == 4) {
2029  if ($statut == '-1' || $statut == 'ST_NO') {
2030  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2031  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2032  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2033  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2034  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2035  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2036  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2037  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2038  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2039  } else {
2040  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);
2041  }
2042  }
2043 
2044  return "Error, mode/status not found";
2045  }
2046 
2047 
2054  public function setNoEmail($no_email)
2055  {
2056  $error = 0;
2057 
2058  // Update mass emailing flag into table mailing_unsubscribe
2059  if ($this->email) {
2060  $this->db->begin();
2061 
2062  if ($no_email) {
2063  $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)."'";
2064  $resql = $this->db->query($sql);
2065  if ($resql) {
2066  $obj = $this->db->fetch_object($resql);
2067  $noemail = $obj->nb;
2068  if (empty($noemail)) {
2069  $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())."')";
2070  $resql = $this->db->query($sql);
2071  if (!$resql) {
2072  $error++;
2073  $this->error = $this->db->lasterror();
2074  $this->errors[] = $this->error;
2075  }
2076  }
2077  } else {
2078  $error++;
2079  $this->error = $this->db->lasterror();
2080  $this->errors[] = $this->error;
2081  }
2082  } else {
2083  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2084  $resql = $this->db->query($sql);
2085  if (!$resql) {
2086  $error++;
2087  $this->error = $this->db->lasterror();
2088  $this->errors[] = $this->error;
2089  }
2090  }
2091 
2092  if (empty($error)) {
2093  $this->no_email = $no_email;
2094  $this->db->commit();
2095  return 1;
2096  } else {
2097  $this->db->rollback();
2098  return $error * -1;
2099  }
2100  }
2101 
2102  return 0;
2103  }
2104 
2111  public function getNoEmail()
2112  {
2113  if ($this->email) {
2114  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2115  $resql = $this->db->query($sql);
2116  if ($resql) {
2117  $obj = $this->db->fetch_object($resql);
2118  $this->no_email = $obj->nb;
2119  return 1;
2120  } else {
2121  $this->error = $this->db->lasterror();
2122  $this->errors[] = $this->error;
2123  return -1;
2124  }
2125  }
2126  return 0;
2127  }
2128 }
$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...
setUpperOrLowerCase()
Set to upper or ucwords/lower if needed.
getFullName($langs, $option=0, $nameorder=-1, $maxlen=0)
Return full name (civility+' '+name+' '+lastname)
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $db, $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.
__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.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
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.
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.
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 Contact roles for a thirdparty.
_load_ldap_dn($info, $mode=0)
Retourne chaine DN complete dans l'annuaire LDAP pour l'objet.
LibStatut($status, $mode)
Renvoi le libelle d'un statut donne.
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 label of contact status.
setNoEmail($no_email)
Set "blacklist" mailing status.
LibPubPriv($status)
Return translated label of Public or Private.
setCategories($categories)
Sets object to supplied categories.
Class to manage Dolibarr database access.
static showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='', $addlinktofullsize=1, $cache=0, $forcecapture='', $noexternsourceoverwrite=0)
Return HTML code to output a photo.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:47
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)) $resql
Social contributions to pay.
Definition: index.php:745
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.
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.
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
img_action($titlealt, $numaction, $picto='')
Show logo action.
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:122
$conf db
API class for accounts.
Definition: inc.php:41