dolibarr  16.0.5
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_prospectlevel' =>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 $address;
144 
148  public $zip;
149 
153  public $town;
154 
158  public $state_id;
159 
163  public $state_code;
164 
168  public $state;
169 
170  public $poste; // Position
171 
175  public $socid; // both socid and fk_soc are used
176  public $fk_soc; // both socid and fk_soc are used
177 
181  public $statut;
182 
183  public $code;
184 
189  public $email;
190 
195  public $url;
196 
201  public $no_email;
202 
206  public $socialnetworks;
207 
213  public $skype;
214 
220  public $twitter;
221 
227  public $facebook;
228 
234  public $linkedin;
235 
241  public $jabberid;
242 
246  public $photo;
247 
251  public $phone_pro;
252 
256  public $phone_perso;
257 
261  public $phone_mobile;
262 
266  public $fax;
267 
272  public $priv;
273 
274  public $birthday;
275  public $default_lang;
276 
280  public $ref_facturation;
281 
285  public $ref_contrat;
286 
290  public $ref_commande;
291 
295  public $ref_propal;
296 
300  public $user_id;
301 
305  public $user_login;
306 
307  // END MODULEBUILDER PROPERTIES
308 
309 
314  public $oldcopy; // To contains a clone of this when we need to save old properties of object
315 
319  public $roles;
320 
321  public $cacheprospectstatus = array();
322  public $fk_prospectlevel;
323  public $stcomm_id;
324  public $statut_commercial;
325 
329  public $stcomm_picto;
330 
331 
337  public function __construct($db)
338  {
339  global $conf, $langs;
340 
341  $this->db = $db;
342  $this->statut = 1; // By default, status is enabled
343 
344  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
345  $this->fields['rowid']['visible'] = 0;
346  }
347  if (empty($conf->mailing->enabled)) {
348  $this->fields['no_email']['enabled'] = 0;
349  }
350  // typical ['s.nom'] is used for third-parties
351  if (empty($conf->global->SOCIETE_DISABLE_CONTACTS)) {
352  $this->fields['fk_soc']['enabled'] = 0;
353  $this->fields['fk_soc']['searchall'] = 0;
354  }
355 
356  if (empty($conf->global->THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES)) { // Default behaviour
357  $this->field['fk_stcommcontact']['enabled'] = 0;
358  $this->field['fk_prospectcontactlevel']['enabled'] = 0;
359  }
360 
361  // Unset fields that are disabled
362  foreach ($this->fields as $key => $val) {
363  if (isset($val['enabled']) && empty($val['enabled'])) {
364  unset($this->fields[$key]);
365  }
366  }
367 
368  // Translate some data of arrayofkeyval
369  /*if (is_object($langs))
370  {
371  foreach($this->fields as $key => $val)
372  {
373  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
374  {
375  foreach($val['arrayofkeyval'] as $key2 => $val2)
376  {
377  $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
378  }
379  }
380  }
381  }*/
382  }
383 
384  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
390  public function load_state_board()
391  {
392  // phpcs:enable
393  global $user, $hookmanager;
394 
395  $this->nb = array();
396  $clause = "WHERE";
397 
398  $sql = "SELECT count(sp.rowid) as nb";
399  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
400  if (empty($user->rights->societe->client->voir) && !$user->socid) {
401  $sql .= ", ".MAIN_DB_PREFIX."societe as s";
402  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
403  $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
404  $clause = "AND";
405  }
406  $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
407  $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
408  if ($user->socid > 0) {
409  $sql .= " AND sp.fk_soc = ".((int) $user->socid);
410  }
411  // Add where from hooks
412  if (is_object($hookmanager)) {
413  $parameters = array();
414  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
415  $sql .= $hookmanager->resPrint;
416  }
417 
418  $resql = $this->db->query($sql);
419  if ($resql) {
420  while ($obj = $this->db->fetch_object($resql)) {
421  $this->nb["contacts"] = $obj->nb;
422  }
423  $this->db->free($resql);
424  return 1;
425  } else {
426  dol_print_error($this->db);
427  $this->error = $this->db->lasterror();
428  return -1;
429  }
430  }
431 
438  public function create($user)
439  {
440  global $conf, $langs;
441 
442  $error = 0;
443  $now = dol_now();
444 
445  $this->db->begin();
446 
447  // Clean parameters
448  $this->lastname = $this->lastname ?trim($this->lastname) : trim($this->name);
449  $this->firstname = trim($this->firstname);
450  $this->setUpperOrLowerCase();
451  if (empty($this->socid)) {
452  $this->socid = 0;
453  }
454  if (empty($this->priv)) {
455  $this->priv = 0;
456  }
457  if (empty($this->statut)) {
458  $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
459  }
460 
461  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
462 
463  $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
464  $sql .= " datec";
465  $sql .= ", fk_soc";
466  $sql .= ", lastname";
467  $sql .= ", firstname";
468  $sql .= ", fk_user_creat";
469  $sql .= ", priv";
470  $sql .= ", fk_stcommcontact";
471  $sql .= ", statut";
472  $sql .= ", canvas";
473  $sql .= ", entity";
474  $sql .= ", ref_ext";
475  $sql .= ", import_key";
476  $sql .= ") VALUES (";
477  $sql .= "'".$this->db->idate($now)."',";
478  if ($this->socid > 0) {
479  $sql .= " ".((int) $this->socid).",";
480  } else {
481  $sql .= "null,";
482  }
483  $sql .= "'".$this->db->escape($this->lastname)."',";
484  $sql .= "'".$this->db->escape($this->firstname)."',";
485  $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
486  $sql .= " ".((int) $this->priv).",";
487  $sql .= " 0,";
488  $sql .= " ".((int) $this->statut).",";
489  $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
490  $sql .= " ".((int) $this->entity).",";
491  $sql .= "'".$this->db->escape($this->ref_ext)."',";
492  $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
493  $sql .= ")";
494 
495  dol_syslog(get_class($this)."::create", LOG_DEBUG);
496  $resql = $this->db->query($sql);
497  if ($resql) {
498  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
499 
500  if (!$error) {
501  $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
502  if ($result < 0) {
503  $error++;
504  $this->error = $this->db->lasterror();
505  }
506  }
507 
508  if (!$error) {
509  $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
510  if ($result < 0) {
511  $error++;
512  $this->error = $this->db->lasterror();
513  }
514  }
515 
516  if (!$error) {
517  // Call trigger
518  $result = $this->call_trigger('CONTACT_CREATE', $user);
519  if ($result < 0) {
520  $error++;
521  }
522  // End call triggers
523  }
524 
525  if (!$error) {
526  $this->db->commit();
527  return $this->id;
528  } else {
529  $this->db->rollback();
530  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
531  return -2;
532  }
533  } else {
534  $this->error = $this->db->lasterror();
535 
536  $this->db->rollback();
537  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
538  return -1;
539  }
540  }
541 
552  public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
553  {
554  global $conf, $langs, $hookmanager;
555 
556  $error = 0;
557 
558  $this->id = $id;
559 
560  $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
561 
562  // Clean parameters
563  $this->ref_ext = trim($this->ref_ext);
564  $this->lastname = trim($this->lastname) ?trim($this->lastname) : trim($this->lastname);
565  $this->firstname = trim($this->firstname);
566  $this->email = trim($this->email);
567  $this->phone_pro = trim($this->phone_pro);
568  $this->phone_perso = trim($this->phone_perso);
569  $this->phone_mobile = trim($this->phone_mobile);
570  $this->photo = trim($this->photo);
571  $this->fax = trim($this->fax);
572  $this->zip = (empty($this->zip) ? '' : trim($this->zip));
573  $this->town = (empty($this->town) ? '' : trim($this->town));
574  $this->setUpperOrLowerCase();
575  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
576  if (empty($this->statut)) {
577  $this->statut = 0;
578  }
579  if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
580  $this->civility_code = $this->civility_id; // For backward compatibility
581  }
582  $this->db->begin();
583 
584  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
585  if ($this->socid > 0) {
586  $sql .= " fk_soc = ".((int) $this->socid).",";
587  } elseif ($this->socid == -1) {
588  $sql .= " fk_soc = NULL,";
589  }
590  $sql .= " civility='".$this->db->escape($this->civility_code)."'";
591  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
592  $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
593  $sql .= ", address='".$this->db->escape($this->address)."'";
594  $sql .= ", zip='".$this->db->escape($this->zip)."'";
595  $sql .= ", town='".$this->db->escape($this->town)."'";
596  $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
597  $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
598  $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
599  $sql .= ", poste='".$this->db->escape($this->poste)."'";
600  $sql .= ", fax='".$this->db->escape($this->fax)."'";
601  $sql .= ", email='".$this->db->escape($this->email)."'";
602  $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
603  $sql .= ", photo='".$this->db->escape($this->photo)."'";
604  $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
605  $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
606  $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
607  $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
608  $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
609  $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
610  $sql .= ", priv = '".$this->db->escape($this->priv)."'";
611  $sql .= ", fk_prospectcontactlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
612  if (isset($this->stcomm_id)) {
613  $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
614  }
615  $sql .= ", statut = ".((int) $this->statut);
616  $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
617  $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
618  $sql .= ", entity = ".((int) $this->entity);
619  $sql .= " WHERE rowid = ".((int) $id);
620 
621  dol_syslog(get_class($this)."::update", LOG_DEBUG);
622  $result = $this->db->query($sql);
623  if ($result) {
624  unset($this->country_code);
625  unset($this->country);
626  unset($this->state_code);
627  unset($this->state);
628 
629  $action = 'update';
630 
631  // Actions on extra fields
632  if (!$error) {
633  $result = $this->insertExtraFields();
634  if ($result < 0) {
635  $error++;
636  }
637  }
638 
639  if (!$error) {
640  $result = $this->updateRoles();
641  if ($result < 0) {
642  $error++;
643  }
644  }
645 
646  if (!$error && $this->user_id > 0) {
647  // If contact is linked to a user
648  $tmpobj = new User($this->db);
649  $tmpobj->fetch($this->user_id);
650  $usermustbemodified = 0;
651  if ($tmpobj->office_phone != $this->phone_pro) {
652  $tmpobj->office_phone = $this->phone_pro;
653  $usermustbemodified++;
654  }
655  if ($tmpobj->office_fax != $this->fax) {
656  $tmpobj->office_fax = $this->fax;
657  $usermustbemodified++;
658  }
659  if ($tmpobj->address != $this->address) {
660  $tmpobj->address = $this->address;
661  $usermustbemodified++;
662  }
663  if ($tmpobj->town != $this->town) {
664  $tmpobj->town = $this->town;
665  $usermustbemodified++;
666  }
667  if ($tmpobj->zip != $this->zip) {
668  $tmpobj->zip = $this->zip;
669  $usermustbemodified++;
670  }
671  if ($tmpobj->zip != $this->zip) {
672  $tmpobj->state_id = $this->state_id;
673  $usermustbemodified++;
674  }
675  if ($tmpobj->country_id != $this->country_id) {
676  $tmpobj->country_id = $this->country_id;
677  $usermustbemodified++;
678  }
679  if ($tmpobj->email != $this->email) {
680  $tmpobj->email = $this->email;
681  $usermustbemodified++;
682  }
683  if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
684  $tmpobj->socialnetworks = $this->socialnetworks;
685  $usermustbemodified++;
686  }
687  // if ($tmpobj->skype != $this->skype) {
688  // $tmpobj->skype = $this->skype;
689  // $usermustbemodified++;
690  // }
691  // if ($tmpobj->twitter != $this->twitter) {
692  // $tmpobj->twitter = $this->twitter;
693  // $usermustbemodified++;
694  // }
695  // if ($tmpobj->facebook != $this->facebook) {
696  // $tmpobj->facebook = $this->facebook;
697  // $usermustbemodified++;
698  // }
699  // if ($tmpobj->linkedin != $this->linkedin) {
700  // $tmpobj->linkedin = $this->linkedin;
701  // $usermustbemodified++;
702  // }
703  if ($usermustbemodified) {
704  $result = $tmpobj->update($user, 0, 1, 1, 1);
705  if ($result < 0) {
706  $error++;
707  }
708  }
709  }
710 
711  if (!$error && !$notrigger) {
712  // Call trigger
713  $result = $this->call_trigger('CONTACT_MODIFY', $user);
714  if ($result < 0) {
715  $error++;
716  }
717  // End call triggers
718  }
719 
720  if (!$error) {
721  $this->db->commit();
722  return 1;
723  } else {
724  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
725  $this->db->rollback();
726  return -$error;
727  }
728  } else {
729  $this->error = $this->db->lasterror().' sql='.$sql;
730  $this->db->rollback();
731  return -1;
732  }
733  }
734 
735 
736  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
737  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
747  public function _load_ldap_dn($info, $mode = 0)
748  {
749  // phpcs:enable
750  global $conf;
751  $dn = '';
752  if ($mode == 0) {
753  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;
754  } elseif ($mode == 1) {
755  $dn = $conf->global->LDAP_CONTACT_DN;
756  } elseif ($mode == 2) {
757  $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS];
758  }
759  return $dn;
760  }
761 
762 
763  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
764  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
770  public function _load_ldap_info()
771  {
772  // phpcs:enable
773  global $conf, $langs;
774 
775  $info = array();
776 
777  // Object classes
778  $info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);
779 
780  $this->fullname = $this->getFullName($langs);
781 
782  // Fields
783  if ($this->fullname && !empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME)) {
784  $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
785  }
786  if ($this->lastname && !empty($conf->global->LDAP_CONTACT_FIELD_NAME)) {
787  $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname;
788  }
789  if ($this->firstname && !empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME)) {
790  $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
791  }
792 
793  if ($this->poste) {
794  $info["title"] = $this->poste;
795  }
796  if ($this->socid > 0) {
797  $soc = new Societe($this->db);
798  $soc->fetch($this->socid);
799 
800  $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->name;
801  if ($soc->client == 1) {
802  $info["businessCategory"] = "Customers";
803  }
804  if ($soc->client == 2) {
805  $info["businessCategory"] = "Prospects";
806  }
807  if ($soc->fournisseur == 1) {
808  $info["businessCategory"] = "Suppliers";
809  }
810  }
811  if ($this->address && !empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS)) {
812  $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
813  }
814  if ($this->zip && !empty($conf->global->LDAP_CONTACT_FIELD_ZIP)) {
815  $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->zip;
816  }
817  if ($this->town && !empty($conf->global->LDAP_CONTACT_FIELD_TOWN)) {
818  $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->town;
819  }
820  if ($this->country_code && !empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY)) {
821  $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code;
822  }
823  if ($this->phone_pro && !empty($conf->global->LDAP_CONTACT_FIELD_PHONE)) {
824  $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
825  }
826  if ($this->phone_perso && !empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) {
827  $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
828  }
829  if ($this->phone_mobile && !empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) {
830  $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
831  }
832  if ($this->fax && !empty($conf->global->LDAP_CONTACT_FIELD_FAX)) {
833  $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
834  }
835  if ($this->skype && !empty($conf->global->LDAP_CONTACT_FIELD_SKYPE)) {
836  $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype;
837  }
838  if ($this->note_private && !empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) {
839  $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
840  }
841  if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL)) {
842  $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
843  }
844 
845  if ($conf->global->LDAP_SERVER_TYPE == 'egroupware') {
846  $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
847 
848  $info['uidnumber'] = $this->id;
849 
850  $info['phpgwTz'] = 0;
851  $info['phpgwMailType'] = 'INTERNET';
852  $info['phpgwMailHomeType'] = 'INTERNET';
853 
854  $info["phpgwContactTypeId"] = 'n';
855  $info["phpgwContactCatId"] = 0;
856  $info["phpgwContactAccess"] = "public";
857 
858  if (dol_strlen($this->egroupware_id) == 0) {
859  $this->egroupware_id = 1;
860  }
861 
862  $info["phpgwContactOwner"] = $this->egroupware_id;
863 
864  if ($this->email) {
865  $info["rfc822Mailbox"] = $this->email;
866  }
867  if ($this->phone_mobile) {
868  $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
869  }
870  }
871 
872  return $info;
873  }
874 
875 
876  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
885  public function update_perso($id, $user = null, $notrigger = 0)
886  {
887  // phpcs:enable
888  $error = 0;
889  $result = false;
890 
891  $this->db->begin();
892 
893  // Mis a jour contact
894  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
895  $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
896  $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
897  if ($user) {
898  $sql .= ", fk_user_modif = ".((int) $user->id);
899  }
900  $sql .= " WHERE rowid = ".((int) $id);
901 
902  dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
903  $resql = $this->db->query($sql);
904  if (!$resql) {
905  $error++;
906  $this->error = $this->db->lasterror();
907  }
908 
909  if ($user) {
910  // Update birthday alert
911  if (!empty($this->birthday_alert)) {
912  //check existing
913  $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
914  $result_check = $this->db->query($sql_check);
915  if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
916  //insert
917  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
918  $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
919  $result = $this->db->query($sql);
920  if (!$result) {
921  $error++;
922  $this->error = $this->db->lasterror();
923  }
924  } else {
925  $result = true;
926  }
927  } else {
928  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
929  $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
930  $result = $this->db->query($sql);
931  if (!$result) {
932  $error++;
933  $this->error = $this->db->lasterror();
934  }
935  }
936  }
937 
938  if (!$error && !$notrigger) {
939  // Call trigger
940  $result = $this->call_trigger('CONTACT_MODIFY', $user);
941  if ($result < 0) {
942  $error++;
943  }
944  // End call triggers
945  }
946 
947  if (!$error) {
948  $this->db->commit();
949  return 1;
950  } else {
951  dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
952  $this->db->rollback();
953  return -$error;
954  }
955  }
956 
957 
968  public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
969  {
970  global $langs;
971 
972  dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
973 
974  if (empty($id) && empty($ref_ext) && empty($email)) {
975  $this->error = 'BadParameter';
976  return -1;
977  }
978 
979  $langs->loadLangs(array("dict", "companies"));
980 
981  $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.lastname, c.firstname,";
982  $sql .= " c.address, c.statut, c.zip, c.town,";
983  $sql .= " c.fk_pays as country_id,";
984  $sql .= " c.fk_departement as state_id,";
985  $sql .= " c.birthday,";
986  $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
987  $sql .= " c.socialnetworks,";
988  $sql .= " c.photo,";
989  $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
990  $sql .= " c.fk_prospectcontactlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
991  $sql .= " c.import_key,";
992  $sql .= " c.datec as date_creation, c.tms as date_modification,";
993  $sql .= " co.label as country, co.code as country_code,";
994  $sql .= " d.nom as state, d.code_departement as state_code,";
995  $sql .= " u.rowid as user_id, u.login as user_login,";
996  $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
997  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
998  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
999  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1000  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1001  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1002  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1003  if ($id) {
1004  $sql .= " WHERE c.rowid = ".((int) $id);
1005  } else {
1006  $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1007  if ($ref_ext) {
1008  $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1009  }
1010  if ($email) {
1011  $sql .= " AND c.email = '".$this->db->escape($email)."'";
1012  }
1013  }
1014 
1015  $resql = $this->db->query($sql);
1016  if ($resql) {
1017  $num = $this->db->num_rows($resql);
1018  if ($num > 1) {
1019  $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1020  dol_syslog($this->error, LOG_ERR);
1021 
1022  return 2;
1023  } elseif ($num) { // $num = 1
1024  $obj = $this->db->fetch_object($resql);
1025 
1026  $this->id = $obj->rowid;
1027  $this->entity = $obj->entity;
1028  $this->ref = $obj->rowid;
1029  $this->ref_ext = $obj->ref_ext;
1030 
1031  $this->civility_code = $obj->civility_code;
1032  $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1033 
1034  $this->lastname = $obj->lastname;
1035  $this->firstname = $obj->firstname;
1036  $this->address = $obj->address;
1037  $this->zip = $obj->zip;
1038  $this->town = $obj->town;
1039 
1040  $this->date_creation = $this->db->jdate($obj->date_creation);
1041  $this->date_modification = $this->db->jdate($obj->date_modification);
1042 
1043  $this->state_id = $obj->state_id;
1044  $this->state_code = $obj->state_code;
1045  $this->state = $obj->state;
1046 
1047  $this->country_id = $obj->country_id;
1048  $this->country_code = $obj->country_id ? $obj->country_code : '';
1049  $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1050 
1051  $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1052  $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1053  $this->socname = $obj->socname;
1054  $this->poste = $obj->poste;
1055  $this->statut = $obj->statut;
1056 
1057  $this->fk_prospectlevel = $obj->fk_prospectcontactlevel;
1058 
1059  $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1060  $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1061  $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1062  $this->statut_commercial = $libelle; // libelle statut commercial
1063  $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1064 
1065  $this->phone_pro = trim($obj->phone);
1066  $this->fax = trim($obj->fax);
1067  $this->phone_perso = trim($obj->phone_perso);
1068  $this->phone_mobile = trim($obj->phone_mobile);
1069 
1070  $this->email = $obj->email;
1071  $this->socialnetworks = (array) json_decode($obj->socialnetworks, true);
1072  $this->photo = $obj->photo;
1073  $this->priv = $obj->priv;
1074  $this->mail = $obj->email;
1075 
1076  $this->birthday = $this->db->jdate($obj->birthday);
1077  $this->note = $obj->note_private; // deprecated
1078  $this->note_private = $obj->note_private;
1079  $this->note_public = $obj->note_public;
1080  $this->default_lang = $obj->default_lang;
1081  $this->user_id = $obj->user_id;
1082  $this->user_login = $obj->user_login;
1083  $this->canvas = $obj->canvas;
1084 
1085  $this->import_key = $obj->import_key;
1086 
1087  // Define gender according to civility
1088  $this->setGenderFromCivility();
1089 
1090  // Search Dolibarr user linked to this contact
1091  $sql = "SELECT u.rowid ";
1092  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1093  $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1094 
1095  $resql = $this->db->query($sql);
1096  if ($resql) {
1097  if ($this->db->num_rows($resql)) {
1098  $uobj = $this->db->fetch_object($resql);
1099 
1100  $this->user_id = $uobj->rowid;
1101  }
1102  $this->db->free($resql);
1103  } else {
1104  $this->error = $this->db->error();
1105  return -1;
1106  }
1107 
1108  // Retrieve all extrafield
1109  // fetch optionals attributes and labels
1110  $this->fetch_optionals();
1111 
1112  // Load also alerts of this user
1113  if ($user) {
1114  $sql = "SELECT fk_user";
1115  $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1116  $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1117 
1118  $resql = $this->db->query($sql);
1119  if ($resql) {
1120  if ($this->db->num_rows($resql)) {
1121  $obj = $this->db->fetch_object($resql);
1122 
1123  $this->birthday_alert = 1;
1124  }
1125  $this->db->free($resql);
1126  } else {
1127  $this->error = $this->db->error();
1128  return -1;
1129  }
1130  }
1131 
1132  // Load also roles of this address
1133  if ($loadalsoroles) {
1134  $resultRole = $this->fetchRoles();
1135  if ($resultRole < 0) {
1136  return $resultRole;
1137  }
1138  }
1139 
1140  return 1;
1141  } else {
1142  $this->error = $langs->trans("RecordNotFound");
1143  return 0;
1144  }
1145  } else {
1146  $this->error = $this->db->error();
1147  return -1;
1148  }
1149  }
1150 
1151 
1152 
1159  public function setGenderFromCivility()
1160  {
1161  unset($this->gender);
1162 
1163  if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1164  $this->gender = 'man';
1165  } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1166  $this->gender = 'woman';
1167  }
1168  }
1169 
1170  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1180  public function load_ref_elements()
1181  {
1182  // phpcs:enable
1183  // Compte les elements pour lesquels il est contact
1184  $sql = "SELECT tc.element, count(ec.rowid) as nb";
1185  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1186  $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1187  $sql .= " AND fk_socpeople = ".((int) $this->id);
1188  $sql .= " AND tc.source = 'external'";
1189  $sql .= " GROUP BY tc.element";
1190 
1191  dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1192 
1193  $resql = $this->db->query($sql);
1194  if ($resql) {
1195  while ($obj = $this->db->fetch_object($resql)) {
1196  if ($obj->nb) {
1197  if ($obj->element == 'facture') {
1198  $this->ref_facturation = $obj->nb;
1199  } elseif ($obj->element == 'contrat') {
1200  $this->ref_contrat = $obj->nb;
1201  } elseif ($obj->element == 'commande') {
1202  $this->ref_commande = $obj->nb;
1203  } elseif ($obj->element == 'propal') {
1204  $this->ref_propal = $obj->nb;
1205  }
1206  }
1207  }
1208  $this->db->free($resql);
1209  return 0;
1210  } else {
1211  $this->error = $this->db->lasterror();
1212  return -1;
1213  }
1214  }
1215 
1223  public function delete($notrigger = 0)
1224  {
1225  global $conf, $langs, $user;
1226 
1227  $error = 0;
1228 
1229  $this->db->begin();
1230 
1231  if (!$error && !$notrigger) {
1232  // Call trigger
1233  $result = $this->call_trigger('CONTACT_DELETE', $user);
1234  if ($result < 0) {
1235  $error++;
1236  }
1237  // End call triggers
1238  }
1239 
1240  if (!$error) {
1241  // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1242  $sql = "SELECT ec.rowid";
1243  $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1244  $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1245  $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1246  $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1247  $sql .= " AND tc.source='external'";
1248  dol_syslog(__METHOD__, LOG_DEBUG);
1249  $resql = $this->db->query($sql);
1250  if ($resql) {
1251  $num = $this->db->num_rows($resql);
1252 
1253  $i = 0;
1254  while ($i < $num && !$error) {
1255  $obj = $this->db->fetch_object($resql);
1256 
1257  $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1258  $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1259  dol_syslog(__METHOD__, LOG_DEBUG);
1260  $result = $this->db->query($sqldel);
1261  if (!$result) {
1262  $error++;
1263  $this->error = $this->db->error().' sql='.$sqldel;
1264  }
1265 
1266  $i++;
1267  }
1268  } else {
1269  $error++;
1270  $this->error = $this->db->error().' sql='.$sql;
1271  }
1272  }
1273 
1274  if (!$error) {
1275  // Remove Roles
1276  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1277  dol_syslog(__METHOD__, LOG_DEBUG);
1278  $resql = $this->db->query($sql);
1279  if (!$resql) {
1280  $error++;
1281  $this->error .= $this->db->lasterror();
1282  $errorflag = -1;
1283  }
1284  }
1285 
1286  if (!$error) {
1287  // Remove Roles
1288  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1289  dol_syslog(__METHOD__, LOG_DEBUG);
1290  $resql = $this->db->query($sql);
1291  if (!$resql) {
1292  $error++;
1293  $this->error .= $this->db->lasterror();
1294  $errorflag = -1;
1295  }
1296  }
1297 
1298  if (!$error) {
1299  // Remove category
1300  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1301  dol_syslog(__METHOD__, LOG_DEBUG);
1302  $resql = $this->db->query($sql);
1303  if (!$resql) {
1304  $error++;
1305  $this->error .= $this->db->lasterror();
1306  $errorflag = -1;
1307  }
1308  }
1309 
1310  if (!$error) {
1311  $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1312  $sql .= " WHERE rowid = ".((int) $this->id);
1313  dol_syslog(__METHOD__, LOG_DEBUG);
1314  $result = $this->db->query($sql);
1315  if (!$result) {
1316  $error++;
1317  $this->error = $this->db->error().' sql='.$sql;
1318  }
1319  }
1320 
1321  // Removed extrafields
1322  if (!$error) {
1323  // For avoid conflicts if trigger used
1324  $result = $this->deleteExtraFields();
1325  if ($result < 0) {
1326  $error++;
1327  }
1328  }
1329 
1330  if (!$error) {
1331  $this->db->commit();
1332  return 1;
1333  } else {
1334  $this->db->rollback();
1335  dol_syslog("Error ".$this->error, LOG_ERR);
1336  return -1;
1337  }
1338  }
1339 
1340 
1347  public function info($id)
1348  {
1349  $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1350  $sql .= " c.tms as tms, c.fk_user_modif";
1351  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1352  $sql .= " WHERE c.rowid = ".((int) $id);
1353 
1354  $resql = $this->db->query($sql);
1355  if ($resql) {
1356  if ($this->db->num_rows($resql)) {
1357  $obj = $this->db->fetch_object($resql);
1358 
1359  $this->id = $obj->rowid;
1360 
1361  if ($obj->fk_user_creat) {
1362  $cuser = new User($this->db);
1363  $cuser->fetch($obj->fk_user_creat);
1364  $this->user_creation = $cuser;
1365  }
1366 
1367  if ($obj->fk_user_modif) {
1368  $muser = new User($this->db);
1369  $muser->fetch($obj->fk_user_modif);
1370  $this->user_modification = $muser;
1371  }
1372 
1373  $this->date_creation = $this->db->jdate($obj->datec);
1374  $this->date_modification = $this->db->jdate($obj->tms);
1375  }
1376 
1377  $this->db->free($resql);
1378  } else {
1379  print $this->db->error();
1380  }
1381  }
1382 
1388  public function getNbOfEMailings()
1389  {
1390  $sql = "SELECT count(mc.email) as nb";
1391  $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1392  $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1393  $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1394 
1395  $resql = $this->db->query($sql);
1396  if ($resql) {
1397  $obj = $this->db->fetch_object($resql);
1398  $nb = $obj->nb;
1399 
1400  $this->db->free($resql);
1401  return $nb;
1402  } else {
1403  $this->error = $this->db->error();
1404  return -1;
1405  }
1406  }
1407 
1421  public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1422  {
1423  global $conf, $langs, $hookmanager;
1424 
1425  $result = ''; $label = '';
1426  if (!empty($this->photo) && class_exists('Form')) {
1427  $label .= '<div class="photointooltip floatright">';
1428  $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.
1429  $label .= '</div>';
1430  //$label .= '<div style="clear: both;"></div>';
1431  }
1432 
1433  $label .= img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u>';
1434  $label .= ' '.$this->getLibStatut(4);
1435  $label .= '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1436  //if ($this->civility_id) $label.= '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate cibilty_id code
1437  if (!empty($this->poste)) {
1438  $label .= '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1439  }
1440  $label .= '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1441  $phonelist = array();
1442  $country_code = empty($this->country_code) ? '': $this->country_code;
1443  if ($this->phone_pro) {
1444  $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1445  }
1446  if ($this->phone_mobile) {
1447  $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1448  }
1449  if ($this->phone_perso) {
1450  $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1451  }
1452  $label .= '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1453  $label .= '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1454 
1455  $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1456 
1457  if ($option !== 'nolink') {
1458  // Add param to save lastsearch_values or not
1459  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1460  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1461  $add_save_lastsearch_values = 1;
1462  }
1463  if ($add_save_lastsearch_values) {
1464  $url .= '&save_lastsearch_values=1';
1465  }
1466  }
1467 
1468  $url .= $moreparam;
1469 
1470  $linkclose = "";
1471  if (empty($notooltip)) {
1472  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1473  $label = $langs->trans("ShowContact");
1474  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1475  }
1476  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1477  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1478  }
1479 
1480  $linkstart = '<a href="'.$url.'"';
1481  $linkstart .= $linkclose.'>';
1482  $linkend = '</a>';
1483 
1484  if ($option == 'xxx') {
1485  $linkstart = '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$this->id.$moreparam.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1486  $linkend = '</a>';
1487  }
1488 
1489  $result .= $linkstart;
1490  if ($withpicto) {
1491  if ($withpicto < 0) {
1492  $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>';
1493  } else {
1494  $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);
1495  }
1496  }
1497  if ($withpicto != 2 && $withpicto != -2) {
1498  $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1499  }
1500  $result .= $linkend;
1501 
1502  global $action;
1503  $hookmanager->initHooks(array('contactdao'));
1504  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1505  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1506  if ($reshook > 0) {
1507  $result = $hookmanager->resPrint;
1508  } else {
1509  $result .= $hookmanager->resPrint;
1510  }
1511 
1512  return $result;
1513  }
1514 
1520  public function getCivilityLabel()
1521  {
1522  global $langs;
1523 
1524  $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1525  if (empty($code)) {
1526  return '';
1527  }
1528 
1529  $langs->load("dict");
1530  return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1531  }
1532 
1539  public function getLibStatut($mode)
1540  {
1541  return $this->LibStatut($this->statut, $mode);
1542  }
1543 
1544  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1552  public function LibStatut($status, $mode)
1553  {
1554  // phpcs:enable
1555  global $langs;
1556 
1557  $labelStatus = array(
1558  0 => 'ActivityCeased',
1559  1 => 'InActivity',
1560  4 => 'InActivity',
1561  5 => 'ActivityCeased',
1562  );
1563  $labelStatusShort = array(
1564  0 => 'ActivityCeased',
1565  1 => 'InActivity',
1566  4 => 'InActivity',
1567  5 => 'ActivityCeased',
1568  );
1569 
1570  $statusType = 'status4';
1571  if ($status == 0 || $status == 5) {
1572  $statusType = 'status5';
1573  }
1574 
1575  $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1576  $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1577 
1578  return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1579  }
1580 
1581 
1582  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1589  public function LibPubPriv($status)
1590  {
1591  // phpcs:enable
1592  global $langs;
1593  if ($status == '1') {
1594  return $langs->trans('ContactPrivate');
1595  } else {
1596  return $langs->trans('ContactPublic');
1597  }
1598  }
1599 
1600 
1608  public function initAsSpecimen()
1609  {
1610  // Get first id of existing company and save it into $socid
1611  $socid = 0;
1612  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1613  $resql = $this->db->query($sql);
1614  if ($resql) {
1615  $obj = $this->db->fetch_object($resql);
1616  if ($obj) {
1617  $socid = $obj->rowid;
1618  }
1619  }
1620 
1621  // Initialise parameters
1622  $this->id = 0;
1623  $this->entity = 1;
1624  $this->specimen = 1;
1625  $this->lastname = 'DOLIBARR';
1626  $this->firstname = 'SPECIMEN';
1627  $this->address = '21 jump street';
1628  $this->zip = '99999';
1629  $this->town = 'MyTown';
1630  $this->country_id = 1;
1631  $this->country_code = 'FR';
1632  $this->country = 'France';
1633  $this->email = 'specimen@specimen.com';
1634  $this->socialnetworks = array(
1635  'skype' => 'tom.hanson',
1636  );
1637  $this->phone_pro = '0909090901';
1638  $this->phone_perso = '0909090902';
1639  $this->phone_mobile = '0909090903';
1640  $this->fax = '0909090909';
1641 
1642  $this->note_public = 'This is a comment (public)';
1643  $this->note_private = 'This is a comment (private)';
1644 
1645  $this->socid = $socid;
1646  $this->statut = 1;
1647  return 1;
1648  }
1649 
1656  public function setstatus($status)
1657  {
1658  global $conf, $langs, $user;
1659 
1660  $error = 0;
1661 
1662  // Check parameters
1663  if ($this->statut == $status) {
1664  return 0;
1665  } else {
1666  $this->statut = $status;
1667  }
1668 
1669  $this->db->begin();
1670 
1671  // Desactive utilisateur
1672  $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1673  $sql .= " SET statut = ".((int) $this->statut);
1674  $sql .= ", fk_user_modif = ".((int) $user->id);
1675  $sql .= " WHERE rowid = ".((int) $this->id);
1676  $result = $this->db->query($sql);
1677 
1678  dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1679  if ($result) {
1680  // Call trigger
1681  $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1682  if ($result < 0) {
1683  $error++;
1684  }
1685  // End call triggers
1686  }
1687 
1688  if ($error) {
1689  $this->db->rollback();
1690  return -$error;
1691  } else {
1692  $this->db->commit();
1693  return 1;
1694  }
1695  }
1696 
1707  public function setCategories($categories)
1708  {
1709  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1710  return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1711  }
1712 
1721  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1722  {
1723  $tables = array(
1724  'socpeople', 'societe_contacts'
1725  );
1726 
1727  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1728  }
1729 
1737  public function fetchRoles()
1738  {
1739  global $langs;
1740  $error = 0;
1741  $num = 0;
1742 
1743  $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1744  $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1745  $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1746  $sql .= " AND tc.source = 'external' AND tc.active=1";
1747  $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1748  $sql .= " AND sc.entity IN (".getEntity('societe').')';
1749 
1750  $resql = $this->db->query($sql);
1751  if ($resql) {
1752  $this->roles = array();
1753 
1754  $num = $this->db->num_rows($resql);
1755  if ($num > 0) {
1756  while ($obj = $this->db->fetch_object($resql)) {
1757  $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1758  $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1759  $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));
1760  }
1761  }
1762  } else {
1763  $error++;
1764  $this->error = $this->db->lasterror();
1765  $this->errors[] = $this->db->lasterror();
1766  }
1767 
1768  if (empty($error)) {
1769  return $num;
1770  } else {
1771  return $error * -1;
1772  }
1773  }
1774 
1782  public function getContactRoles($element = '')
1783  {
1784  $tab = array();
1785 
1786  if ($element == 'action') {
1787  $element = 'agenda';
1788  }
1789 
1790  $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1791  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1792  $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1793  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1794  $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1795  $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1796  $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1797  $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1798  $sql .= " AND tc.active = 1";
1799 
1800  dol_syslog(__METHOD__, LOG_DEBUG);
1801  $resql = $this->db->query($sql);
1802  if ($resql) {
1803  $num = $this->db->num_rows($resql);
1804  $i = 0;
1805  while ($i < $num) {
1806  $obj = $this->db->fetch_object($resql);
1807  $tab[] = array('fk_socpeople'=>$obj->id, 'type_contact'=>$obj->fk_c_type_contact);
1808 
1809  $i++;
1810  }
1811 
1812  return $tab;
1813  } else {
1814  $this->error = $this->db->error();
1815  dol_print_error($this->db);
1816  return -1;
1817  }
1818  }
1819 
1827  public function updateRoles()
1828  {
1829  global $conf;
1830 
1831  $error = 0;
1832 
1833  if (!isset($this->roles)) {
1834  return; // Avoid to loose roles when property not set
1835  }
1836 
1837  $this->db->begin();
1838 
1839  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1840 
1841  $result = $this->db->query($sql);
1842  if (!$result) {
1843  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1844  $error++;
1845  } else {
1846  if (count($this->roles) > 0) {
1847  foreach ($this->roles as $keyRoles => $valRoles) {
1848  $idrole = 0;
1849  if (is_array($valRoles)) {
1850  $idrole = $valRoles['id'];
1851  } else {
1852  $idrole = $valRoles;
1853  }
1854 
1855  $socid = 0;
1856  if (is_array($valRoles)) {
1857  $socid = $valRoles['socid'];
1858  } else {
1859  $socid = $this->socid;
1860  }
1861 
1862  if ($socid > 0) {
1863  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1864  $sql .= " (entity,";
1865  $sql .= "date_creation,";
1866  $sql .= "fk_soc,";
1867  $sql .= "fk_c_type_contact,";
1868  $sql .= "fk_socpeople) ";
1869  $sql .= " VALUES (".$conf->entity.",";
1870  $sql .= "'".$this->db->idate(dol_now())."',";
1871  $sql .= $socid.", ";
1872  $sql .= $idrole." , ";
1873  $sql .= $this->id;
1874  $sql .= ")";
1875 
1876  $result = $this->db->query($sql);
1877  if (!$result) {
1878  $this->errors[] = $this->db->lasterror().' sql='.$sql;
1879  $error++;
1880  }
1881  }
1882  }
1883  }
1884  }
1885  if (empty($error)) {
1886  $this->db->commit();
1887  return 1;
1888  } else {
1889  $this->error = implode(' ', $this->errors);
1890  $this->db->rollback();
1891  return $error * -1;
1892  }
1893  }
1894 
1901  public function loadCacheOfProspStatus($active = 1)
1902  {
1903  global $langs;
1904 
1905  $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
1906  if ($active >= 0) {
1907  $sql .= " WHERE active = ".((int) $active);
1908  }
1909  $resql = $this->db->query($sql);
1910  $num = $this->db->num_rows($resql);
1911  $i = 0;
1912  while ($i < $num) {
1913  $obj = $this->db->fetch_object($resql);
1914  $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);
1915  $i++;
1916  }
1917  return 1;
1918  }
1919 
1925  public function getLibProspLevel()
1926  {
1927  return $this->libProspLevel($this->fk_prospectlevel);
1928  }
1929 
1936  public function libProspLevel($fk_prospectlevel)
1937  {
1938  global $langs;
1939 
1940  $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
1941  // If lib not found in language file, we get label from cache/databse
1942  if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel)) {
1943  $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
1944  }
1945  return $lib;
1946  }
1947 
1948 
1956  public function setProspectLevel(User $user)
1957  {
1958  return $this->update($this->id, $user);
1959  }
1960 
1968  public function getLibProspCommStatut($mode = 0, $label = '')
1969  {
1970  return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
1971  }
1972 
1986  public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
1987  {
1988  global $langs;
1989  $langs->load('customers');
1990 
1991  if ($mode == 2) {
1992  if ($statut == '-1' || $statut == 'ST_NO') {
1993  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
1994  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
1995  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
1996  } elseif ($statut == '1' || $statut == 'ST_TODO') {
1997  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
1998  } elseif ($statut == '2' || $statut == 'ST_PEND') {
1999  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2000  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2001  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2002  } else {
2003  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);
2004  }
2005  }
2006  if ($mode == 3) {
2007  if ($statut == '-1' || $statut == 'ST_NO') {
2008  return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2009  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2010  return img_action($langs->trans("StatusProspect0"), 0, $picto);
2011  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2012  return img_action($langs->trans("StatusProspect1"), 1, $picto);
2013  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2014  return img_action($langs->trans("StatusProspect2"), 2, $picto);
2015  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2016  return img_action($langs->trans("StatusProspect3"), 3, $picto);
2017  } else {
2018  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);
2019  }
2020  }
2021  if ($mode == 4) {
2022  if ($statut == '-1' || $statut == 'ST_NO') {
2023  return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2024  } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2025  return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2026  } elseif ($statut == '1' || $statut == 'ST_TODO') {
2027  return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2028  } elseif ($statut == '2' || $statut == 'ST_PEND') {
2029  return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2030  } elseif ($statut == '3' || $statut == 'ST_DONE') {
2031  return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2032  } else {
2033  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);
2034  }
2035  }
2036 
2037  return "Error, mode/status not found";
2038  }
2039 
2040 
2047  public function setNoEmail($no_email)
2048  {
2049  $error = 0;
2050 
2051  // Update mass emailing flag into table mailing_unsubscribe
2052  if ($this->email) {
2053  $this->db->begin();
2054 
2055  if ($no_email) {
2056  $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)."'";
2057  $resql = $this->db->query($sql);
2058  if ($resql) {
2059  $obj = $this->db->fetch_object($resql);
2060  $noemail = $obj->nb;
2061  if (empty($noemail)) {
2062  $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())."')";
2063  $resql = $this->db->query($sql);
2064  if (!$resql) {
2065  $error++;
2066  $this->error = $this->db->lasterror();
2067  $this->errors[] = $this->error;
2068  }
2069  }
2070  } else {
2071  $error++;
2072  $this->error = $this->db->lasterror();
2073  $this->errors[] = $this->error;
2074  }
2075  } else {
2076  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2077  $resql = $this->db->query($sql);
2078  if (!$resql) {
2079  $error++;
2080  $this->error = $this->db->lasterror();
2081  $this->errors[] = $this->error;
2082  }
2083  }
2084 
2085  if (empty($error)) {
2086  $this->no_email = $no_email;
2087  $this->db->commit();
2088  return 1;
2089  } else {
2090  $this->db->rollback();
2091  return $error * -1;
2092  }
2093  }
2094 
2095  return 0;
2096  }
2097 
2104  public function getNoEmail()
2105  {
2106  if ($this->email) {
2107  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2108  $resql = $this->db->query($sql);
2109  if ($resql) {
2110  $obj = $this->db->fetch_object($resql);
2111  $this->no_email = $obj->nb;
2112  return 1;
2113  } else {
2114  $this->error = $this->db->lasterror();
2115  $this->errors[] = $this->error;
2116  return -1;
2117  }
2118  }
2119  return 0;
2120  }
2121 }
Contact\getLibProspLevel
getLibProspLevel()
Return prostect level.
Definition: contact.class.php:1925
Contact\setProspectLevel
setProspectLevel(User $user)
Set prospect level.
Definition: contact.class.php:1956
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
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:1468
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:3805
Contact\loadCacheOfProspStatus
loadCacheOfProspStatus($active=1)
Load array of prospect status.
Definition: contact.class.php:1901
Contact\load_state_board
load_state_board()
Load indicators into this->nb for board.
Definition: contact.class.php:390
img_action
img_action($titlealt, $numaction, $picto='')
Show logo action.
Definition: functions.lib.php:4296
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:552
Contact\LibStatut
LibStatut($status, $mode)
Renvoi le libelle d'un statut donne.
Definition: contact.class.php:1552
Contact\replaceThirdparty
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: contact.class.php:1721
CommonObject\setUpperOrLowerCase
setUpperOrLowerCase()
Set to upper or ucwords/lower if needed.
Definition: commonobject.class.php:740
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:4844
Contact\libProspCommStatut
libProspCommStatut($statut, $mode=0, $label='', $picto='')
Return label of a given status.
Definition: contact.class.php:1986
Contact\update_perso
update_perso($id, $user=null, $notrigger=0)
Update field alert birthday.
Definition: contact.class.php:885
ref
$object ref
Definition: info.php:77
Contact\updateRoles
updateRoles()
Updates all roles (default contact for companies) according to values inside the ->roles array.
Definition: contact.class.php:1827
Contact\setNoEmail
setNoEmail($no_email)
Set "blacklist" mailing status.
Definition: contact.class.php:2047
Contact\getContactRoles
getContactRoles($element='')
Get Contact roles for a thirdparty.
Definition: contact.class.php:1782
name
$conf db name
Definition: repair.php:122
Contact\create
create($user)
Add a contact into database.
Definition: contact.class.php:438
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
CommonObject\getFullName
getFullName($langs, $option=0, $nameorder=-1, $maxlen=0)
Return full name (civility+' '+name+' '+lastname)
Definition: commonobject.class.php:712
Contact\info
info($id)
Charge les informations sur le contact, depuis la base.
Definition: contact.class.php:1347
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:1180
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:3880
Contact\setCategories
setCategories($categories)
Sets object to supplied categories.
Definition: contact.class.php:1707
Contact\getNbOfEMailings
getNbOfEMailings()
Return number of mass Emailing received by this contacts with its email.
Definition: contact.class.php:1388
Contact\libProspLevel
libProspLevel($fk_prospectlevel)
Return label of prospect level.
Definition: contact.class.php:1936
Contact\__construct
__construct($db)
Constructor.
Definition: contact.class.php:337
Contact\fetchRoles
fetchRoles()
Fetch roles (default contact of some companies) for the current contact.
Definition: contact.class.php:1737
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:148
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6156
Contact\fetch
fetch($id, $user=null, $ref_ext='', $email='', $loadalsoroles=0)
Load object contact.
Definition: contact.class.php:968
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:6694
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:1421
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8347
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Contact
Class to manage contact/addresses.
Definition: contact.class.php:40
Contact\setGenderFromCivility
setGenderFromCivility()
Set the property "gender" of this class, based on the property "civility_id" or use property "civilit...
Definition: contact.class.php:1159
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:2393
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:6007
Contact\getCivilityLabel
getCivilityLabel()
Return civility label of contact.
Definition: contact.class.php:1520
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
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:747
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Contact\_load_ldap_info
_load_ldap_info()
Initialise tableau info (tableau des attributs LDAP)
Definition: contact.class.php:770
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6116
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
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:4211
Contact\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: contact.class.php:1608
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Contact\getLibStatut
getLibStatut($mode)
Return label of contact status.
Definition: contact.class.php:1539
Contact\getLibProspCommStatut
getLibProspCommStatut($mode=0, $label='')
Return status of prospect.
Definition: contact.class.php:1968
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:3185
Form\showphoto
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.
Definition: html.form.class.php:9263
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
Contact\getNoEmail
getNoEmail()
get "blacklist" mailing status set no_email attribut to 1 or 0
Definition: contact.class.php:2104
Contact\LibPubPriv
LibPubPriv($status)
Return translated label of Public or Private.
Definition: contact.class.php:1589
Contact\setstatus
setstatus($status)
Change status of a user.
Definition: contact.class.php:1656