dolibarr 22.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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
15 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 */
30
36require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
37require_once DOL_DOCUMENT_ROOT.'/core/class/commonsocialnetworks.class.php';
38require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
39
40
44class Contact extends CommonObject
45{
47 use CommonPeople;
48
52 public $element = 'contact';
53
57 public $table_element = 'socpeople';
58
62 public $picto = 'contact';
63
88 // BEGIN MODULEBUILDER PROPERTIES
92 public $fields = array(
93 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'),
94 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 30, 'index' => 1),
95 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 35),
96 'civility' => array('type' => 'varchar(6)', 'label' => 'Civility', 'enabled' => 1, 'visible' => 3, 'position' => 40),
97 'lastname' => array('type' => 'varchar(50)', 'label' => 'Lastname', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'showoncombobox' => 1, 'searchall' => 1),
98 'name_alias' => array('type' => 'varchar(255)', 'label' => 'Name alias', 'enabled' => 1, 'visible' => -1, 'position' => 46, 'searchall' => 1),
99 'firstname' => array('type' => 'varchar(50)', 'label' => 'Firstname', 'enabled' => 1, 'visible' => 1, 'position' => 50, 'showoncombobox' => 1, 'searchall' => 1),
100 'poste' => array('type' => 'varchar(80)', 'label' => 'PostOrFunction', 'enabled' => 1, 'visible' => -1, 'position' => 52),
101 'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 55),
102 'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 60),
103 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 65),
104 'fk_departement' => array('type' => 'integer', 'label' => 'Fk departement', 'enabled' => 1, 'visible' => 3, 'position' => 70),
105 'fk_pays' => array('type' => 'integer', 'label' => 'Fk pays', 'enabled' => 1, 'visible' => 3, 'position' => 75),
106 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => 1, 'position' => 77, 'searchall' => 1),
107 'birthday' => array('type' => 'date', 'label' => 'Birthday', 'enabled' => 1, 'visible' => -1, 'position' => 80),
108 'phone' => array('type' => 'varchar(30)', 'label' => 'Phone', 'enabled' => 1, 'visible' => 1, 'position' => 90, 'searchall' => 1),
109 'phone_perso' => array('type' => 'varchar(30)', 'label' => 'PhonePerso', 'enabled' => 1, 'visible' => -1, 'position' => 95, 'searchall' => 1),
110 'phone_mobile' => array('type' => 'varchar(30)', 'label' => 'PhoneMobile', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'searchall' => 1),
111 'fax' => array('type' => 'varchar(30)', 'label' => 'Fax', 'enabled' => 1, 'visible' => -1, 'position' => 105, 'searchall' => 1),
112 'email' => array('type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'visible' => 1, 'position' => 110, 'searchall' => 1),
113 'socialnetworks' => array('type' => 'text', 'label' => 'SocialNetworks', 'enabled' => 1, 'visible' => 3, 'position' => 115),
114 'photo' => array('type' => 'varchar(255)', 'label' => 'Photo', 'enabled' => 1, 'visible' => 3, 'position' => 170),
115 'priv' => array('type' => 'smallint(6)', 'label' => 'ContactVisibility', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 175),
116 'fk_stcommcontact' => array('type' => 'integer', 'label' => 'ProspectStatus', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 220),
117 'fk_prospectcontactlevel' => array('type' => 'varchar(12)', 'label' => 'ProspectLevel', 'enabled' => 1, 'visible' => -1, 'position' => 255),
118 //no more used. Replace by a scan of email into mailing_unsubscribe. 'no_email' =>array('type'=>'smallint(6)', 'label'=>'No_Email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),
119 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 3, 'position' => 195, 'searchall' => 1),
120 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 3, 'position' => 200, 'searchall' => 1),
121 'default_lang' => array('type' => 'varchar(6)', 'label' => 'Default lang', 'enabled' => 1, 'visible' => 3, 'position' => 205),
122 'canvas' => array('type' => 'varchar(32)', 'label' => 'Canvas', 'enabled' => 1, 'visible' => 3, 'position' => 210),
123 'ip' => array('type' => 'ip', 'label' => 'IPAddress', 'enabled' => '1', 'position' => 700, 'notnull' => 0, 'visible' => '-2', 'comment' => 'ip used to create record (for public submission page)'),
124 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 300),
125 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305),
126 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 3, 'position' => 310),
127 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 3, 'position' => 315),
128 'statut' => array('type' => 'tinyint(4)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500),
129 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000),
130 );
131
135 public $civility_id; // In fact we store civility_code
139 public $civility_code;
143 public $civility;
144
148 public $gender;
149
153 //private $egroupware_id;
154
158 public $birthday_alert;
159
165 public $civilite;
166
170 public $fullname;
171
175 public $name_alias;
176
180 public $address;
181
185 public $zip;
186
190 public $town;
191
195 public $state_id;
196
200 public $state_code;
201
205 public $state;
206
210 public $poste;
211
215 public $socid; // both socid and fk_soc are used
219 public $fk_soc; // both socid and fk_soc are used
220
224 public $socname;
225
229 public $statut;
230
234 public $code;
235
240 public $email;
241
248 public $mail;
249
254 public $url;
255
261 public $no_email;
262
267 public $socialnetworks;
268
272 public $photo;
273
277 public $phone_pro;
278
282 public $phone_perso;
283
287 public $phone_mobile;
288
292 public $fax;
293
298 public $priv;
299
303 public $birthday;
304
308 public $default_lang;
309
313 public $ref_facturation;
314
318 public $ref_contrat;
319
323 public $ref_commande;
324
328 public $ref_propal;
329
333 public $user_id;
334
338 public $user_login;
339
343 public $ip;
344 // END MODULEBUILDER PROPERTIES
345
349 public $roles;
350
354 public $cacheprospectstatus = array();
355
359 public $fk_prospectlevel;
360
364 public $stcomm_id;
365
369 public $statut_commercial;
370
374 public $stcomm_picto;
375
376
382 public function __construct($db)
383 {
384 $this->db = $db;
385 $this->statut = 1; // By default, status is enabled
386 $this->ismultientitymanaged = 1;
387 $this->isextrafieldmanaged = 1;
388
389 $this->fields['ref_ext']['visible'] = getDolGlobalInt('MAIN_LIST_SHOW_REF_EXT');
390
391 if (!isModEnabled('mailing')) {
392 $this->fields['no_email']['enabled'] = 0;
393 }
394 // typical ['s.nom'] is used for third-parties
395 if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) {
396 $this->fields['fk_soc']['enabled'] = 0;
397 $this->fields['fk_soc']['searchall'] = 0;
398 }
399
400 // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
401 if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) { // Default behaviour
402 $this->fields['fk_stcommcontact']['enabled'] = 0;
403 $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
404 }
405
406 // Unset fields that are disabled
407 foreach ($this->fields as $key => $val) {
408 if (isset($val['enabled']) && empty($val['enabled'])) {
409 unset($this->fields[$key]);
410 }
411 }
412
413 // Translate some data of arrayofkeyval
414 /*if (is_object($langs))
415 {
416 foreach($this->fields as $key => $val)
417 {
418 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
419 {
420 foreach($val['arrayofkeyval'] as $key2 => $val2)
421 {
422 $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
423 }
424 }
425 }
426 }*/
427 }
428
434 public function loadStateBoard()
435 {
436 global $user, $hookmanager;
437
438 $this->nb = array();
439 $clause = "WHERE";
440
441 $sql = "SELECT count(sp.rowid) as nb";
442 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
443 if (!$user->hasRight('societe', 'client', 'voir')) {
444 $sql .= ", ".MAIN_DB_PREFIX."societe as s";
445 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
446 $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
447 $clause = "AND";
448 }
449 $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
450 $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
451 if ($user->socid > 0) {
452 $sql .= " AND sp.fk_soc = ".((int) $user->socid);
453 }
454 // Add where from hooks
455 if (is_object($hookmanager)) {
456 $parameters = array();
457 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
458 $sql .= $hookmanager->resPrint;
459 }
460
461 $resql = $this->db->query($sql);
462 if ($resql) {
463 while ($obj = $this->db->fetch_object($resql)) {
464 $this->nb["contacts"] = $obj->nb;
465 }
466 $this->db->free($resql);
467 return 1;
468 } else {
469 dol_print_error($this->db);
470 $this->error = $this->db->lasterror();
471 return -1;
472 }
473 }
474
482 public function create($user, $notrigger = 0)
483 {
484 global $conf;
485
486 $error = 0;
487 $now = dol_now();
488
489 if (empty($this->date_creation)) {
490 $this->date_creation = $now;
491 }
492
493 $this->db->begin();
494
495 // Clean parameters
496 $this->name_alias = trim($this->name_alias);
497 $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name);
498 $this->firstname = trim($this->firstname);
499 $this->setUpperOrLowerCase();
500 if (empty($this->socid)) {
501 $this->socid = 0;
502 }
503 if (empty($this->priv)) {
504 $this->priv = 0;
505 }
506 if (empty($this->statut)) {
507 $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
508 }
509
510 // setEntity will set entity with the right value if empty or change it for the right value if multicompany module is active
511 $this->entity = setEntity($this);
512
513 $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
514 $sql .= " datec";
515 $sql .= ", fk_soc";
516 $sql .= ", name_alias";
517 $sql .= ", lastname";
518 $sql .= ", firstname";
519 $sql .= ", fk_user_creat";
520 $sql .= ", priv";
521 $sql .= ", fk_stcommcontact";
522 $sql .= ", statut";
523 $sql .= ", canvas";
524 $sql .= ", entity";
525 $sql .= ", ref_ext";
526 $sql .= ", import_key";
527 $sql .= ", ip";
528 $sql .= ") VALUES (";
529 $sql .= "'".$this->db->idate($now)."',";
530 if ($this->socid > 0) {
531 $sql .= " ".((int) $this->socid).",";
532 } else {
533 $sql .= "null,";
534 }
535 $sql .= "'".$this->db->escape($this->name_alias)."',";
536 $sql .= "'".$this->db->escape($this->lastname)."',";
537 $sql .= "'".$this->db->escape($this->firstname)."',";
538 $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
539 $sql .= " ".((int) $this->priv).",";
540 $sql .= " 0,";
541 $sql .= " ".((int) $this->statut).",";
542 $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
543 $sql .= " ".((int) $this->entity).",";
544 $sql .= "'".$this->db->escape($this->ref_ext)."',";
545 $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").",";
546 $sql .= " ".(!empty($this->ip) ? "'".$this->db->escape($this->ip)."'" : "null");
547 $sql .= ")";
548
549 dol_syslog(get_class($this)."::create", LOG_DEBUG);
550 $resql = $this->db->query($sql);
551 if ($resql) {
552 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
553
554 if (!$error) {
555 $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
556 if ($result < 0) {
557 $error++;
558 $this->error = $this->db->lasterror();
559 }
560 }
561
562 if (!$error) {
563 $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
564 if ($result < 0) {
565 $error++;
566 $this->error = $this->db->lasterror();
567 }
568 }
569
570 if (!$error && !$notrigger) {
571 // Call trigger
572 $result = $this->call_trigger('CONTACT_CREATE', $user);
573 if ($result < 0) {
574 $error++;
575 }
576 // End call triggers
577 }
578
579 if (!$error) {
580 $this->db->commit();
581 return $this->id;
582 } else {
583 $this->db->rollback();
584 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
585 return -2;
586 }
587 } else {
588 $this->error = $this->db->lasterror();
589
590 $this->db->rollback();
591 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
592 return -1;
593 }
594 }
595
606 public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
607 {
608 global $conf;
609
610 if (empty($this->country_id) && !empty($this->country_code)) {
611 $country_id = getCountry($this->country_code, '3');
612 $this->country_id = is_int($country_id) ? $country_id : 0;
613 }
614
615 $error = 0;
616
617 $this->id = $id;
618
619 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
620
621 // Clean parameters
622 $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext));
623 $this->name_alias = trim($this->name_alias);
624 $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
625 $this->firstname = trim($this->firstname);
626 $this->email = trim($this->email ?? '');
627 $this->phone_pro = trim($this->phone_pro);
628 $this->phone_perso = trim($this->phone_perso);
629 $this->phone_mobile = trim($this->phone_mobile);
630 $this->photo = trim($this->photo);
631 $this->fax = trim($this->fax);
632 $this->zip = (empty($this->zip) ? '' : trim($this->zip));
633 $this->town = (empty($this->town) ? '' : trim($this->town));
634 $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
635 if (empty($this->statut)) {
636 $this->statut = 0;
637 }
638 if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
639 $this->civility_code = $this->civility_id; // For backward compatibility
640 }
641 $this->setUpperOrLowerCase();
642
643 $this->db->begin();
644
645 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
646 if ($this->socid > 0) {
647 $sql .= " fk_soc = ".((int) $this->socid).",";
648 } elseif ($this->socid == -1) {
649 $sql .= " fk_soc = NULL,";
650 }
651 $sql .= " civility='".$this->db->escape($this->civility_code)."'";
652 $sql .= ", name_alias='".$this->db->escape($this->name_alias)."'";
653 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
654 $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
655 $sql .= ", address='".$this->db->escape((string) $this->address)."'";
656 $sql .= ", zip='".$this->db->escape($this->zip)."'";
657 $sql .= ", town='".$this->db->escape($this->town)."'";
658 $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
659 $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
660 $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
661 $sql .= ", poste='".$this->db->escape($this->poste)."'";
662 $sql .= ", fax='".$this->db->escape($this->fax)."'";
663 $sql .= ", email='".$this->db->escape($this->email)."'";
664 $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
665 $sql .= ", photo='".$this->db->escape($this->photo)."'";
666 $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
667 $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
668 $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
669 $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
670 $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
671 $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
672 $sql .= ", priv = ".((int) $this->priv);
673 $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
674 if (isset($this->stcomm_id)) {
675 $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
676 }
677 $sql .= ", statut = ".((int) $this->statut);
678 $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape((string) $user->id)."'" : "NULL");
679 $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
680 $sql .= ", entity = ".((int) $this->entity);
681 $sql .= " WHERE rowid = ".((int) $id);
682
683 dol_syslog(get_class($this)."::update", LOG_DEBUG);
684 $result = $this->db->query($sql);
685 if ($result) {
686 unset($this->country_code);
687 unset($this->country);
688 unset($this->state_code);
689 unset($this->state);
690
691 $action = 'update';
692
693 // Actions on extra fields
694 if (!$error) {
695 $result = $this->insertExtraFields();
696 if ($result < 0) {
697 $error++;
698 }
699 }
700
701 if (!$error) {
702 $result = $this->updateRoles();
703 if ($result < 0) {
704 $error++;
705 }
706 }
707
708 if (!$error && $this->user_id > 0) {
709 // If contact is linked to a user
710 $tmpobj = new User($this->db);
711 $tmpobj->fetch($this->user_id);
712 $usermustbemodified = 0;
713 if ($tmpobj->office_phone != $this->phone_pro) {
714 $tmpobj->office_phone = $this->phone_pro;
715 $usermustbemodified++;
716 }
717 if ($tmpobj->office_fax != $this->fax) {
718 $tmpobj->office_fax = $this->fax;
719 $usermustbemodified++;
720 }
721 if ($tmpobj->address != $this->address) {
722 $tmpobj->address = $this->address;
723 $usermustbemodified++;
724 }
725 if ($tmpobj->town != $this->town) {
726 $tmpobj->town = $this->town;
727 $usermustbemodified++;
728 }
729 if ($tmpobj->zip != $this->zip) {
730 $tmpobj->zip = $this->zip;
731 $usermustbemodified++;
732 }
733 if ($tmpobj->state_id != $this->state_id) {
734 $tmpobj->state_id = $this->state_id;
735 $usermustbemodified++;
736 }
737 if ($tmpobj->country_id != $this->country_id) {
738 $tmpobj->country_id = $this->country_id;
739 $usermustbemodified++;
740 }
741 if ($tmpobj->email != $this->email) {
742 $tmpobj->email = $this->email;
743 $usermustbemodified++;
744 }
745 $usersocialnetworks = (is_array($tmpobj->socialnetworks) ? $tmpobj->socialnetworks : array());
746 $contactsocialnetworks = (is_array($this->socialnetworks) ? $this->socialnetworks : array());
747 // Compare in both directions, so a network added on the contact is also seen as a difference
748 if (!empty(array_diff_assoc($usersocialnetworks, $contactsocialnetworks)) || !empty(array_diff_assoc($contactsocialnetworks, $usersocialnetworks))) {
749 $tmpobj->socialnetworks = $this->socialnetworks;
750 $usermustbemodified++;
751 }
752 if ($usermustbemodified) {
753 $result = $tmpobj->update($user, 0, 1, 1, 1);
754 if ($result < 0) {
755 $error++;
756 }
757 }
758 }
759
760 if (!$error && !$notrigger) {
761 // Call trigger
762 $result = $this->call_trigger('CONTACT_MODIFY', $user);
763 if ($result < 0) {
764 $error++;
765 }
766 // End call triggers
767 }
768
769 if (!$error) {
770 $this->db->commit();
771 return 1;
772 } else {
773 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
774 $this->db->rollback();
775 return -$error;
776 }
777 } else {
778 $this->error = $this->db->lasterror().' sql='.$sql;
779 $this->db->rollback();
780 return -1;
781 }
782 }
783
784
785 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
786 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
796 public function _load_ldap_dn($info, $mode = 0)
797 {
798 // phpcs:enable
799 global $conf;
800 $dn = '';
801 if ($mode == 0) {
802 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
803 } elseif ($mode == 1) {
804 $dn = getDolGlobalString('LDAP_CONTACT_DN');
805 } elseif ($mode == 2) {
806 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
807 }
808 return $dn;
809 }
810
811
812 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
813 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
819 public function _load_ldap_info()
820 {
821 // phpcs:enable
822 global $conf, $langs;
823
824 $info = array();
825
826 // Object classes
827 $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
828
829 $this->fullname = $this->getFullName($langs);
830
831 // Fields
832 if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
833 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
834 }
835 if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
836 $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
837 }
838 if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
839 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
840 }
841
842 if ($this->poste) {
843 $info["title"] = $this->poste;
844 }
845 if ($this->socid > 0) {
846 $soc = new Societe($this->db);
847 $soc->fetch($this->socid);
848
849 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
850 if ($soc->client == 1) {
851 $info["businessCategory"] = "Customers";
852 }
853 if ($soc->client == 2) {
854 $info["businessCategory"] = "Prospects";
855 }
856 if ($soc->fournisseur == 1) {
857 $info["businessCategory"] = "Suppliers";
858 }
859 }
860 if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
861 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
862 }
863 if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
864 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
865 }
866 if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
867 $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
868 }
869 if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
870 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
871 }
872 if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
873 $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
874 }
875 if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
876 $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
877 }
878 if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
879 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
880 }
881 if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
882 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
883 }
884 if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
885 $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
886 }
887 if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
888 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
889 }
890
891 if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
892 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
893
894 $info['uidnumber'] = $this->id;
895
896 $info['phpgwTz'] = 0;
897 $info['phpgwMailType'] = 'INTERNET';
898 $info['phpgwMailHomeType'] = 'INTERNET';
899
900 $info["phpgwContactTypeId"] = 'n';
901 $info["phpgwContactCatId"] = 0;
902 $info["phpgwContactAccess"] = "public";
903
904 /*
905 if (dol_strlen($this->egroupware_id) == 0) {
906 $this->egroupware_id = 1;
907 }
908 $info["phpgwContactOwner"] = $this->egroupware_id;
909 */
910 $info["phpgwContactOwner"] = 1;
911
912 if ($this->email) {
913 $info["rfc822Mailbox"] = $this->email;
914 }
915 if ($this->phone_mobile) {
916 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
917 }
918 }
919
920 return $info;
921 }
922
923
924 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
933 public function update_perso($id, $user = null, $notrigger = 0)
934 {
935 // phpcs:enable
936 $error = 0;
937 $result = false;
938
939 $this->db->begin();
940
941 // Update the contact
942 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
943 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
944 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
945 if ($user) {
946 $sql .= ", fk_user_modif = ".((int) $user->id);
947 }
948 $sql .= " WHERE rowid = ".((int) $id);
949
950 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
951 $resql = $this->db->query($sql);
952 if (!$resql) {
953 $error++;
954 $this->error = $this->db->lasterror();
955 }
956
957 if ($user) {
958 // Update birthday alert
959 if (!empty($this->birthday_alert)) {
960 //check existing
961 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
962 $result_check = $this->db->query($sql_check);
963 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
964 //insert
965 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
966 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
967 $result = $this->db->query($sql);
968 if (!$result) {
969 $error++;
970 $this->error = $this->db->lasterror();
971 }
972 } else {
973 $result = true;
974 }
975 } else {
976 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
977 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
978 $result = $this->db->query($sql);
979 if (!$result) {
980 $error++;
981 $this->error = $this->db->lasterror();
982 }
983 }
984
985 if (!$error && !$notrigger) {
986 // Call trigger
987 $result = $this->call_trigger('CONTACT_MODIFY', $user);
988 if ($result < 0) {
989 $error++;
990 }
991 // End call triggers
992 }
993 }
994
995 if (!$error) {
996 $this->db->commit();
997 return 1;
998 } else {
999 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
1000 $this->db->rollback();
1001 return -$error;
1002 }
1003 }
1004
1005
1017 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
1018 {
1019 global $langs;
1020
1021 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
1022
1023 if (empty($id) && empty($ref_ext) && empty($email)) {
1024 $this->error = 'BadParameter';
1025 return -1;
1026 }
1027
1028 $langs->loadLangs(array("dict", "companies"));
1029
1030 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1031 $sql .= " c.address, c.statut, c.zip, c.town,";
1032 $sql .= " c.fk_pays as country_id,";
1033 $sql .= " c.fk_departement as state_id,";
1034 $sql .= " c.birthday,";
1035 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1036 $sql .= " c.socialnetworks,";
1037 $sql .= " c.photo,";
1038 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1039 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1040 $sql .= " c.import_key,";
1041 $sql .= " c.datec as date_creation, c.tms as date_modification, c.fk_user_creat, c.fk_user_modif,";
1042 $sql .= " co.label as country, co.code as country_code,";
1043 $sql .= " d.nom as state, d.code_departement as state_code,";
1044 $sql .= " u.rowid as user_id, u.login as user_login,";
1045 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1046 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1047 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1048 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1049 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1050 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1051 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1052 if ($id) {
1053 $sql .= " WHERE c.rowid = ".((int) $id);
1054 } else {
1055 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1056 if ($ref_ext) {
1057 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1058 }
1059 if ($email) {
1060 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1061 }
1062 if ($socid) {
1063 $sql .= " AND c.fk_soc = ".((int) $socid);
1064 }
1065 }
1066
1067 $resql = $this->db->query($sql);
1068 if ($resql) {
1069 $num = $this->db->num_rows($resql);
1070 if ($num > 1) {
1071 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1072 dol_syslog($this->error, LOG_ERR);
1073
1074 return 2;
1075 } elseif ($num) { // $num = 1
1076 $obj = $this->db->fetch_object($resql);
1077
1078 $this->id = $obj->rowid;
1079 $this->entity = $obj->entity;
1080 $this->ref = $obj->rowid;
1081 $this->ref_ext = $obj->ref_ext;
1082
1083 $this->civility_code = $obj->civility_code;
1084 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1085
1086 $this->name_alias = $obj->name_alias;
1087 $this->lastname = $obj->lastname;
1088 $this->firstname = $obj->firstname;
1089 $this->address = $obj->address;
1090 $this->zip = $obj->zip;
1091 $this->town = $obj->town;
1092
1093 $this->date_creation = $this->db->jdate($obj->date_creation);
1094 $this->date_modification = $this->db->jdate($obj->date_modification);
1095 $this->user_creation_id = $obj->fk_user_creat;
1096 $this->user_modification_id = $obj->fk_user_modif;
1097
1098 $this->state_id = $obj->state_id;
1099 $this->state_code = $obj->state_code;
1100 $this->state = $obj->state;
1101
1102 $this->country_id = $obj->country_id;
1103 $this->country_code = $obj->country_id ? $obj->country_code : '';
1104 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1105
1106 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1107 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1108 $this->socname = $obj->socname;
1109 $this->poste = $obj->poste;
1110 $this->statut = $obj->statut;
1111
1112 $this->fk_prospectlevel = $obj->fk_prospectlevel;
1113
1114 $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1115 $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1116 $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1117 $this->statut_commercial = $libelle; // libelle statut commercial
1118 $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1119
1120 $this->phone_pro = trim($obj->phone);
1121 $this->fax = trim($obj->fax);
1122 $this->phone_perso = trim($obj->phone_perso);
1123 $this->phone_mobile = trim($obj->phone_mobile);
1124
1125 $this->email = $obj->email;
1126 $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1127 $this->photo = $obj->photo;
1128 $this->priv = $obj->priv;
1129 $this->mail = $obj->email;
1130
1131 $this->birthday = $this->db->jdate($obj->birthday);
1132 $this->note = $obj->note_private; // deprecated
1133 $this->note_private = $obj->note_private;
1134 $this->note_public = $obj->note_public;
1135 $this->default_lang = $obj->default_lang;
1136 $this->user_id = $obj->user_id;
1137 $this->user_login = $obj->user_login;
1138 $this->canvas = $obj->canvas;
1139
1140 $this->import_key = $obj->import_key;
1141
1142 // Define gender according to civility
1143 $this->setGenderFromCivility();
1144
1145 // Search Dolibarr user linked to this contact
1146 $sql = "SELECT u.rowid ";
1147 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1148 $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1149
1150 $resql = $this->db->query($sql);
1151 if ($resql) {
1152 if ($this->db->num_rows($resql)) {
1153 $uobj = $this->db->fetch_object($resql);
1154
1155 $this->user_id = $uobj->rowid;
1156 }
1157 $this->db->free($resql);
1158 } else {
1159 $this->error = $this->db->error();
1160 return -1;
1161 }
1162
1163 // Retrieve all extrafield
1164 // fetch optionals attributes and labels
1165 $this->fetch_optionals();
1166
1167 // Load also alerts of this user
1168 if ($user) {
1169 $sql = "SELECT fk_user";
1170 $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1171 $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1172
1173 $resql = $this->db->query($sql);
1174 if ($resql) {
1175 if ($this->db->num_rows($resql)) {
1176 $obj = $this->db->fetch_object($resql);
1177
1178 $this->birthday_alert = 1;
1179 }
1180 $this->db->free($resql);
1181 } else {
1182 $this->error = $this->db->error();
1183 return -1;
1184 }
1185 }
1186
1187 // Load also roles of this address
1188 if ($loadalsoroles) {
1189 $resultRole = $this->fetchRoles();
1190 if ($resultRole < 0) {
1191 return $resultRole;
1192 }
1193 }
1194
1195 return $this->id;
1196 } else {
1197 $langs->load('errors');
1198 $this->error = $langs->trans("ErrorRecordNotFound");
1199 return 0;
1200 }
1201 } else {
1202 $this->error = $this->db->error();
1203 return -1;
1204 }
1205 }
1206
1220 public function findNearest($id = 0, $lastname = '', $firstname = '', $ref_ext = '', $email = '', $ref_alias = '', $socid = 0)
1221 {
1222 // A rowid is known, it is a unique key so we found it
1223 if ($id) {
1224 return $id;
1225 }
1226
1227 // We try to find the contact with exact matching on all fields
1228 // TODO Replace this with step by step search
1229 // Then search on email
1230 // Then search on lastname + firstname
1231 // Then search ref_ext or alias with a OR
1232 $tmpcontact = new Contact($this->db);
1233 $result = $tmpcontact->fetch($id, null, $ref_ext, $email, 0, $socid);
1234
1235 return $result;
1236 }
1237
1238
1239
1246 public function setGenderFromCivility()
1247 {
1248 unset($this->gender);
1249
1250 if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1251 $this->gender = 'man';
1252 } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1253 $this->gender = 'woman';
1254 }
1255 }
1256
1257 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1267 public function load_ref_elements()
1268 {
1269 // phpcs:enable
1270 // Count the elements for which it is contact
1271 $sql = "SELECT tc.element, count(ec.rowid) as nb";
1272 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1273 $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1274 $sql .= " AND fk_socpeople = ".((int) $this->id);
1275 $sql .= " AND tc.source = 'external'";
1276 $sql .= " GROUP BY tc.element";
1277
1278 dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1279
1280 $resql = $this->db->query($sql);
1281 if ($resql) {
1282 while ($obj = $this->db->fetch_object($resql)) {
1283 if ($obj->nb) {
1284 if ($obj->element == 'facture') {
1285 $this->ref_facturation = $obj->nb;
1286 } elseif ($obj->element == 'contrat') {
1287 $this->ref_contrat = $obj->nb;
1288 } elseif ($obj->element == 'commande') {
1289 $this->ref_commande = $obj->nb;
1290 } elseif ($obj->element == 'propal') {
1291 $this->ref_propal = $obj->nb;
1292 }
1293 }
1294 }
1295 $this->db->free($resql);
1296 return 0;
1297 } else {
1298 $this->error = $this->db->lasterror();
1299 return -1;
1300 }
1301 }
1302
1310 public function delete($user, $notrigger = 0)
1311 {
1312 global $conf;
1313
1314 $error = 0;
1315
1316 $this->db->begin();
1317
1318 if (!$error && !$notrigger) {
1319 // Call trigger
1320 $result = $this->call_trigger('CONTACT_DELETE', $user);
1321 if ($result < 0) {
1322 $error++;
1323 }
1324 // End call triggers
1325 }
1326
1327 if (!$error) {
1328 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1329 $sql = "SELECT ec.rowid";
1330 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1331 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1332 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1333 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1334 $sql .= " AND tc.source='external'";
1335 dol_syslog(__METHOD__, LOG_DEBUG);
1336 $resql = $this->db->query($sql);
1337 if ($resql) {
1338 $num = $this->db->num_rows($resql);
1339
1340 $i = 0;
1341 while ($i < $num && !$error) {
1342 $obj = $this->db->fetch_object($resql);
1343
1344 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1345 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1346 dol_syslog(__METHOD__, LOG_DEBUG);
1347 $result = $this->db->query($sqldel);
1348 if (!$result) {
1349 $error++;
1350 $this->error = $this->db->error().' sql='.$sqldel;
1351 }
1352
1353 $i++;
1354 }
1355 } else {
1356 $error++;
1357 $this->error = $this->db->error().' sql='.$sql;
1358 }
1359 }
1360
1361 if (!$error) {
1362 // Remove Roles
1363 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1364 dol_syslog(__METHOD__, LOG_DEBUG);
1365 $resql = $this->db->query($sql);
1366 if (!$resql) {
1367 $error++;
1368 $this->error .= $this->db->lasterror();
1369 $errorflag = -1;
1370 }
1371 }
1372
1373 if (!$error) {
1374 // Remove Notifications
1375 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1376 dol_syslog(__METHOD__, LOG_DEBUG);
1377 $resql = $this->db->query($sql);
1378 if (!$resql) {
1379 $error++;
1380 $this->error .= $this->db->lasterror();
1381 $errorflag = -1;
1382 }
1383 }
1384
1385 if (!$error) {
1386 // Remove category
1387 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1388 dol_syslog(__METHOD__, LOG_DEBUG);
1389 $resql = $this->db->query($sql);
1390 if (!$resql) {
1391 $error++;
1392 $this->error .= $this->db->lasterror();
1393 $errorflag = -1;
1394 }
1395 }
1396
1397 if (!$error) {
1398 // Remove the birthday alerts set by users on this contact
1399 $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_alert WHERE fk_contact = ".((int) $this->id);
1400 dol_syslog(__METHOD__, LOG_DEBUG);
1401 $resql = $this->db->query($sql);
1402 if (!$resql) {
1403 $error++;
1404 $this->error .= $this->db->lasterror();
1405 }
1406 }
1407
1408 if (!$error) {
1409 // Remove the link of the user created from this contact (the user is kept)
1410 $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_socpeople = NULL WHERE fk_socpeople = ".((int) $this->id);
1411 dol_syslog(__METHOD__, LOG_DEBUG);
1412 $resql = $this->db->query($sql);
1413 if (!$resql) {
1414 $error++;
1415 $this->error .= $this->db->lasterror();
1416 }
1417 }
1418
1419 if (!$error) {
1420 // Remove the links with other objects
1421 $result = $this->deleteObjectLinked();
1422 if ($result < 0) {
1423 $error++;
1424 }
1425 }
1426
1427 // Remove the index of the documents of the contact (the files are removed after the commit)
1428 $dirofdocuments = '';
1429 if ($this->id > 0 && !empty($conf->societe->multidir_output[$this->entity])) {
1430 $dirofdocuments = $conf->societe->multidir_output[$this->entity].'/contact/'.dol_sanitizeFileName((string) $this->id);
1431 }
1432 if (!$error && $dirofdocuments) {
1433 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1434 if (deleteFilesIntoDatabaseIndex($dirofdocuments, '', '') < 0 || !$this->deleteEcmFiles(1)) {
1435 $error++;
1436 $this->error .= $this->db->lasterror();
1437 }
1438 }
1439
1440 if (!$error) {
1441 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1442 $sql .= " WHERE rowid = ".((int) $this->id);
1443 dol_syslog(__METHOD__, LOG_DEBUG);
1444 $result = $this->db->query($sql);
1445 if (!$result) {
1446 $error++;
1447 $this->error = $this->db->error().' sql='.$sql;
1448 }
1449 }
1450
1451 // Remove extrafields
1452 if (!$error) {
1453 // For avoid conflicts if trigger used
1454 $result = $this->deleteExtraFields();
1455 if ($result < 0) {
1456 $error++;
1457 }
1458 }
1459
1460 if (!$error) {
1461 $this->db->commit();
1462
1463 // Delete the directory of the documents of the contact
1464 if ($dirofdocuments) {
1465 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1466 if (dol_is_dir($dirofdocuments)) {
1467 dol_delete_dir_recursive($dirofdocuments);
1468 }
1469 }
1470
1471 return 1;
1472 } else {
1473 $this->db->rollback();
1474 dol_syslog("Error ".$this->error, LOG_ERR);
1475 return -1;
1476 }
1477 }
1478
1479
1486 public function info($id)
1487 {
1488 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1489 $sql .= " c.tms as tms, c.fk_user_modif";
1490 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1491 $sql .= " WHERE c.rowid = ".((int) $id);
1492
1493 $resql = $this->db->query($sql);
1494 if ($resql) {
1495 if ($this->db->num_rows($resql)) {
1496 $obj = $this->db->fetch_object($resql);
1497
1498 $this->id = $obj->rowid;
1499
1500 $this->user_creation_id = $obj->fk_user_creat;
1501 $this->user_modification_id = $obj->fk_user_modif;
1502 $this->date_creation = $this->db->jdate($obj->datec);
1503 $this->date_modification = $this->db->jdate($obj->tms);
1504 }
1505
1506 $this->db->free($resql);
1507 } else {
1508 print $this->db->error();
1509 }
1510 }
1511
1517 public function getNbOfEMailings()
1518 {
1519 $sql = "SELECT count(mc.email) as nb";
1520 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1521 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1522 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1523
1524 $resql = $this->db->query($sql);
1525 if ($resql) {
1526 $obj = $this->db->fetch_object($resql);
1527 $nb = $obj->nb;
1528
1529 $this->db->free($resql);
1530 return $nb;
1531 } else {
1532 $this->error = $this->db->error();
1533 return -1;
1534 }
1535 }
1536
1543 public function getTooltipContentArray($params)
1544 {
1545 global $conf, $langs, $user;
1546
1547 $datas = [];
1548
1549 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1550 return ['optimize' => $langs->trans("ShowContact")];
1551 }
1552 if (!empty($this->photo) && class_exists('Form')) {
1553 $photo = '<div class="photointooltip floatright">';
1554 $photo .= Form::showphoto('contact', $this, 0, 40, 0, 'photoref', 'mini', 0); // Important, we must force height so image will have height tags and if image is inside a tooltip, the tooltip manager can calculate height and position correctly the tooltip.
1555 $photo .= '</div>';
1556 $datas['photo'] = $photo;
1557 }
1558
1559 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1560 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1561 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1562 if (!empty($this->poste)) {
1563 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1564 }
1565 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1566 $phonelist = array();
1567 $country_code = empty($this->country_code) ? '' : $this->country_code;
1568 if ($this->phone_pro) {
1569 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1570 }
1571 if ($this->phone_mobile) {
1572 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1573 }
1574 if ($this->phone_perso) {
1575 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1576 }
1577 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1578 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1579
1580 return $datas;
1581 }
1582
1596 public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = 'valignmiddle')
1597 {
1598 global $conf, $langs, $hookmanager;
1599
1600 if (!empty($conf->dol_no_mouse_hover)) {
1601 $notooltip = 1; // Force disable tooltips
1602 }
1603
1604 $result = '';
1605 $params = [
1606 'id' => $this->id,
1607 'objecttype' => $this->element,
1608 'option' => $option,
1609 ];
1610 $classfortooltip = 'classfortooltip';
1611 $dataparams = '';
1612 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1613 $classfortooltip = 'classforajaxtooltip';
1614 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1615 $label = '';
1616 } else {
1617 $label = implode($this->getTooltipContentArray($params));
1618 }
1619
1620 $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1621
1622 if ($option !== 'nolink') {
1623 // Add param to save lastsearch_values or not
1624 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1625 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1626 $add_save_lastsearch_values = 1;
1627 }
1628 if ($url && $add_save_lastsearch_values) {
1629 $url .= '&save_lastsearch_values=1';
1630 }
1631 }
1632
1633 $url .= $moreparam;
1634
1635 $linkclose = '';
1636 if (empty($notooltip)) {
1637 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1638 $label = $langs->trans("ShowContact");
1639 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1640 }
1641 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1642 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1643 } else {
1644 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1645 }
1646
1647 if ($option == 'nolink' || empty($url)) {
1648 $linkstart = '<span';
1649 } else {
1650 $linkstart = '<a href="'.$url.'"';
1651 }
1652 $linkstart .= $linkclose.'>';
1653 if ($option == 'nolink' || empty($url)) {
1654 $linkend = '</span>';
1655 } else {
1656 $linkend = '</a>';
1657 }
1658
1659 $result .= $linkstart;
1660
1661 if ($withpicto) {
1662 if ($withpicto < 0) {
1663 $result .= '<!-- picto photo contact --><span class="nopadding userimg'.($morecss ? ' '.$morecss : '').'">'.Form::showphoto('contact', $this, 0, 0, 0, 'userphoto'.($withpicto == -3 ? 'small' : ''), 'mini', 0, 1).'</span>';
1664 if ($withpicto != 2 && $withpicto != -2) {
1665 $result .= ' ';
1666 }
1667 } else {
1668 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="pictofixedwidth valignmiddle"' : '') : 'class="'.(($withpicto != 2) ? 'pictofixedwidth valignmiddle' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1669 }
1670 }
1671 if ($withpicto != 2 && $withpicto != -2) {
1672 $result .= '<span class="valignmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1673 }
1674
1675 $result .= $linkend;
1676
1677 global $action;
1678 $hookmanager->initHooks(array('contactdao'));
1679 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1680 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1681 if ($reshook > 0) {
1682 $result = $hookmanager->resPrint;
1683 } else {
1684 $result .= $hookmanager->resPrint;
1685 }
1686
1687 return $result;
1688 }
1689
1695 public function getCivilityLabel()
1696 {
1697 global $langs;
1698
1699 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1700 if (empty($code)) {
1701 return '';
1702 }
1703
1704 $langs->load("dict");
1705 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1706 }
1707
1714 public function getLibStatut($mode)
1715 {
1716 return $this->LibStatut($this->statut, $mode);
1717 }
1718
1719 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1727 public function LibStatut($status, $mode)
1728 {
1729 // phpcs:enable
1730 global $langs;
1731
1732 $labelStatus = array(
1733 0 => 'ActivityCeased',
1734 1 => 'InActivity',
1735 4 => 'InActivity',
1736 5 => 'ActivityCeased',
1737 );
1738 $labelStatusShort = array(
1739 0 => 'ActivityCeased',
1740 1 => 'InActivity',
1741 4 => 'InActivity',
1742 5 => 'ActivityCeased',
1743 );
1744
1745 $statusType = 'status4';
1746 if ($status == 0 || $status == 5) {
1747 $statusType = 'status5';
1748 }
1749
1750 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1751 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1752
1753 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1754 }
1755
1756
1757 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1764 public function LibPubPriv($status)
1765 {
1766 // phpcs:enable
1767 global $langs;
1768 if ($status == '1') {
1769 return $langs->trans('ContactPrivate');
1770 } else {
1771 return $langs->trans('ContactPublic');
1772 }
1773 }
1774
1775
1783 public function initAsSpecimen()
1784 {
1785 // Get first id of existing company and save it into $socid
1786 $socid = 0;
1787 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1788 $resql = $this->db->query($sql);
1789 if ($resql) {
1790 $obj = $this->db->fetch_object($resql);
1791 if ($obj) {
1792 $socid = $obj->rowid;
1793 }
1794 }
1795
1796 // Initialise parameters
1797 $this->id = 0;
1798 $this->entity = 1;
1799 $this->specimen = 1;
1800 $this->lastname = 'DOLIBARR';
1801 $this->firstname = 'SPECIMEN';
1802 $this->address = '21 jump street';
1803 $this->zip = '99999';
1804 $this->town = 'MyTown';
1805 $this->country_id = 1;
1806 $this->country_code = 'FR';
1807 $this->country = 'France';
1808 $this->email = 'specimen@specimen.com';
1809 $this->socialnetworks = array(
1810 'skype' => 'tom.hanson',
1811 'twitter' => 'tomhanson',
1812 'linkedin' => 'tomhanson',
1813 );
1814 $this->phone_pro = '0909090901';
1815 $this->phone_perso = '0909090902';
1816 $this->phone_mobile = '0909090903';
1817 $this->fax = '0909090909';
1818
1819 $this->note_public = 'This is a comment (public)';
1820 $this->note_private = 'This is a comment (private)';
1821
1822 $this->socid = $socid;
1823 $this->statut = 1;
1824
1825 return 1;
1826 }
1827
1834 public function setstatus($status)
1835 {
1836 global $conf, $langs, $user;
1837
1838 $error = 0;
1839
1840 // Check parameters
1841 if ($this->statut == $status) {
1842 return 0;
1843 } else {
1844 $this->statut = $status;
1845 }
1846
1847 $this->db->begin();
1848
1849 // User disable
1850 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1851 $sql .= " SET statut = ".((int) $this->statut);
1852 $sql .= ", fk_user_modif = ".((int) $user->id);
1853 $sql .= " WHERE rowid = ".((int) $this->id);
1854 $result = $this->db->query($sql);
1855
1856 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1857 if ($result) {
1858 // Call trigger
1859 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1860 if ($result < 0) {
1861 $error++;
1862 }
1863 // End call triggers
1864 }
1865
1866 if ($error) {
1867 $this->db->rollback();
1868 return -$error;
1869 } else {
1870 $this->db->commit();
1871 return 1;
1872 }
1873 }
1874
1885 public function setCategories($categories)
1886 {
1887 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1888 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1889 }
1890
1899 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1900 {
1901 $tables = array(
1902 'socpeople', 'societe_contacts'
1903 );
1904
1905 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1906 }
1907
1915 public function fetchRoles()
1916 {
1917 global $langs;
1918 $error = 0;
1919 $num = 0;
1920
1921 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1922 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1923 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1924 $sql .= " AND tc.source = 'external' AND tc.active=1";
1925 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1926 $sql .= " AND sc.entity IN (".getEntity('societe').')';
1927
1928 $resql = $this->db->query($sql);
1929 if ($resql) {
1930 $this->roles = array();
1931
1932 $num = $this->db->num_rows($resql);
1933 if ($num > 0) {
1934 while ($obj = $this->db->fetch_object($resql)) {
1935 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1936 $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1937 $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));
1938 }
1939 }
1940 } else {
1941 $error++;
1942 $this->error = $this->db->lasterror();
1943 $this->errors[] = $this->db->lasterror();
1944 }
1945
1946 if (empty($error)) {
1947 return $num;
1948 } else {
1949 return $error * -1;
1950 }
1951 }
1952
1960 public function getContactRoles($element = '')
1961 {
1962 $tab = array();
1963
1964 if ($element == 'action') {
1965 $element = 'agenda';
1966 }
1967
1968 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1969 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1970 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1971 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1972 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1973 $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1974 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1975 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1976 $sql .= " AND sp.entity IN (".getEntity('contact').")";
1977 $sql .= " AND tc.active = 1";
1978
1979 dol_syslog(__METHOD__, LOG_DEBUG);
1980 $resql = $this->db->query($sql);
1981 if ($resql) {
1982 $num = $this->db->num_rows($resql);
1983 $i = 0;
1984 while ($i < $num) {
1985 $obj = $this->db->fetch_object($resql);
1986 $tab[] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1987
1988 $i++;
1989 }
1990
1991 return $tab;
1992 } else {
1993 $this->error = $this->db->error();
1994 dol_print_error($this->db);
1995 return -1;
1996 }
1997 }
1998
2006 public function updateRoles()
2007 {
2008 global $conf;
2009
2010 $error = 0;
2011
2012 if (!isset($this->roles)) {
2013 return 0; // Avoid to loose roles when property not set
2014 }
2015
2016 $this->db->begin();
2017
2018 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
2019
2020 $result = $this->db->query($sql);
2021 if (!$result) {
2022 $this->errors[] = $this->db->lasterror().' sql='.$sql;
2023 $error++;
2024 } else {
2025 if (count($this->roles) > 0) {
2026 foreach ($this->roles as $keyRoles => $valRoles) {
2027 if (empty($valRoles)) {
2028 continue;
2029 }
2030 $idrole = 0;
2031 if (is_array($valRoles)) {
2032 $idrole = $valRoles['id'];
2033 } else {
2034 $idrole = $valRoles;
2035 }
2036
2037 $socid = 0;
2038 if (is_array($valRoles)) {
2039 $socid = $valRoles['socid'];
2040 } else {
2041 $socid = $this->socid;
2042 }
2043
2044 if ($socid > 0) {
2045 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
2046 $sql .= " (entity,";
2047 $sql .= "date_creation,";
2048 $sql .= "fk_soc,";
2049 $sql .= "fk_c_type_contact,";
2050 $sql .= "fk_socpeople) ";
2051 $sql .= " VALUES (".$conf->entity.",";
2052 $sql .= "'".$this->db->idate(dol_now())."',";
2053 $sql .= $socid.", ";
2054 $sql .= $idrole." , ";
2055 $sql .= $this->id;
2056 $sql .= ")";
2057
2058 $result = $this->db->query($sql);
2059 if (!$result) {
2060 $this->errors[] = $this->db->lasterror().' sql='.$sql;
2061 $error++;
2062 }
2063 }
2064 }
2065 }
2066 }
2067 if (empty($error)) {
2068 $this->db->commit();
2069 return 1;
2070 } else {
2071 $this->error = implode(' ', $this->errors);
2072 $this->db->rollback();
2073 return $error * -1;
2074 }
2075 }
2076
2083 public function loadCacheOfProspStatus($active = 1)
2084 {
2085 global $langs;
2086
2087 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
2088 if ($active >= 0) {
2089 $sql .= " WHERE active = ".((int) $active);
2090 }
2091 $resql = $this->db->query($sql);
2092 $num = $this->db->num_rows($resql);
2093 $i = 0;
2094 while ($i < $num) {
2095 $obj = $this->db->fetch_object($resql);
2096 $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);
2097 $i++;
2098 }
2099 return 1;
2100 }
2101
2107 public function getLibProspLevel()
2108 {
2109 return $this->libProspLevel($this->fk_prospectlevel);
2110 }
2111
2118 public function libProspLevel($fk_prospectlevel)
2119 {
2120 global $langs;
2121
2122 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2123 // If lib not found in language file, we get label from cache/database
2124 if ($lib == "ProspectLevel".$fk_prospectlevel) {
2125 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2126 }
2127 return $lib;
2128 }
2129
2130
2138 public function setProspectLevel(User $user)
2139 {
2140 return $this->update($this->id, $user);
2141 }
2142
2150 public function getLibProspCommStatut($mode = 0, $label = '')
2151 {
2152 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2153 }
2154
2168 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2169 {
2170 global $langs;
2171 $langs->load('customers');
2172
2173 if ($mode == 2) {
2174 if ($statut == '-1' || $statut == 'ST_NO') {
2175 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2176 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2177 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2178 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2179 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2180 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2181 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2182 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2183 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2184 } else {
2185 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);
2186 }
2187 }
2188 if ($mode == 3) {
2189 if ($statut == '-1' || $statut == 'ST_NO') {
2190 return img_action($langs->trans("StatusProspect-1"), '-1', $picto);
2191 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2192 return img_action($langs->trans("StatusProspect0"), '0', $picto);
2193 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2194 return img_action($langs->trans("StatusProspect1"), '1', $picto);
2195 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2196 return img_action($langs->trans("StatusProspect2"), '2', $picto);
2197 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2198 return img_action($langs->trans("StatusProspect3"), '3', $picto);
2199 } else {
2200 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, '0', $picto);
2201 }
2202 }
2203 if ($mode == 4) {
2204 if ($statut == '-1' || $statut == 'ST_NO') {
2205 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2206 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2207 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2208 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2209 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2210 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2211 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2212 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2213 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2214 } else {
2215 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);
2216 }
2217 }
2218
2219 return "Error, mode/status not found";
2220 }
2221
2222
2229 public function setNoEmail($no_email)
2230 {
2231 $error = 0;
2232
2233 // Update mass emailing flag into table mailing_unsubscribe
2234 if ($this->email) {
2235 $this->db->begin();
2236
2237 if ($no_email) {
2238 $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)."'";
2239 $resql = $this->db->query($sql);
2240 if ($resql) {
2241 $obj = $this->db->fetch_object($resql);
2242 $noemail = $obj->nb;
2243 if (empty($noemail)) {
2244 $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())."')";
2245 $resql = $this->db->query($sql);
2246 if (!$resql) {
2247 $error++;
2248 $this->error = $this->db->lasterror();
2249 $this->errors[] = $this->error;
2250 }
2251 }
2252 } else {
2253 $error++;
2254 $this->error = $this->db->lasterror();
2255 $this->errors[] = $this->error;
2256 }
2257 } else {
2258 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2259 $resql = $this->db->query($sql);
2260 if (!$resql) {
2261 $error++;
2262 $this->error = $this->db->lasterror();
2263 $this->errors[] = $this->error;
2264 }
2265 }
2266
2267 if (empty($error)) {
2268 $this->no_email = $no_email;
2269 $this->db->commit();
2270 return 1;
2271 } else {
2272 $this->db->rollback();
2273 return $error * -1;
2274 }
2275 }
2276
2277 return 0;
2278 }
2279
2286 public function getNoEmail()
2287 {
2288 if ($this->email) {
2289 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2290 $resql = $this->db->query($sql);
2291 if ($resql) {
2292 $obj = $this->db->fetch_object($resql);
2293 $this->no_email = $obj->nb;
2294 return 1;
2295 } else {
2296 $this->error = $this->db->lasterror();
2297 $this->errors[] = $this->error;
2298 return -1;
2299 }
2300 }
2301 return 0;
2302 }
2303
2304
2312 public function getKanbanView($option = '', $arraydata = null)
2313 {
2314 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2315
2316 $return = '<div class="box-flex-item box-flex-grow-zero">';
2317 $return .= '<div class="info-box info-box-sm">';
2318 $return .= '<span class="info-box-icon bg-infobox-action">';
2319 //var_dump($this->photo);exit;
2320 if (property_exists($this, 'photo') && !is_null($this->photo)) {
2321 $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2322 } else {
2323 $return .= img_picto('', $this->picto);
2324 }
2325 $return .= '</span>';
2326 $return .= '<div class="info-box-content">';
2327 $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref).'</div>';
2328 if ($selected >= 0) {
2329 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2330 }
2331 if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2332 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2333 }
2334 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2335 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2336 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2337 }*/
2338 /*if (method_exists($this, 'LibPubPriv')) {
2339 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2340 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2341 }*/
2342 if (method_exists($this, 'getLibStatut')) {
2343 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2344 }
2345 $return .= '</div>';
2346 $return .= '</div>';
2347 $return .= '</div>';
2348 return $return;
2349 }
2350}
$object ref
Definition info.php:90
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...
deleteEcmFiles($mode=0)
Delete related files of object in database.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage contact/addresses.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
__construct($db)
Constructor.
update_perso($id, $user=null, $notrigger=0)
Update field alert birthday.
getLibProspCommStatut($mode=0, $label='')
Return status of prospect.
$fields
'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter...
create($user, $notrigger=0)
Add a contact into database.
loadStateBoard()
Load indicators into this->nb for board.
getNoEmail()
get "blacklist" mailing status set no_email attribute to 1 or 0
getNbOfEMailings()
Return number of mass Emailing received by these contacts with its email.
libProspCommStatut($statut, $mode=0, $label='', $picto='')
Return label of a given status.
getTooltipContentArray($params)
getTooltipContentArray
libProspLevel($fk_prospectlevel)
Return label of prospect level.
getCivilityLabel()
Return civility label of contact.
setProspectLevel(User $user)
Set prospect level.
fetch($id, $user=null, $ref_ext='', $email='', $loadalsoroles=0, $socid=0)
Load object contact.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
load_ref_elements()
Load number of elements the contact is used as a link for ref_facturation ref_contrat ref_commande (f...
getLibProspLevel()
Return prospect level.
fetchRoles()
Fetch roles (default contact of some companies) for the current contact.
getContactRoles($element='')
Get thirdparty contact roles of a given contact.
_load_ldap_dn($info, $mode=0)
Return DN string complete in the LDAP directory for the object.
LibStatut($status, $mode)
Return the label of a given status.
setGenderFromCivility()
Set the property "gender" of this class, based on the property "civility_id" or use property "civilit...
update($id, $user=null, $notrigger=0, $action='update', $nosyncuser=0)
Update information into database.
getNomUrl($withpicto=0, $option='', $maxlen=0, $moreparam='', $save_lastsearch_value=-1, $notooltip=0, $morecss='valignmiddle')
Return name of contact with link (and eventually picto) Use $this->id, $this->lastname,...
setstatus($status)
Change status of a user.
loadCacheOfProspStatus($active=1)
Load array of prospect status.
_load_ldap_info()
Initialize info table (LDAP attributes table)
updateRoles()
Updates all roles (default contact for companies) according to values inside the ->roles array.
info($id)
Load contact information from the database.
initAsSpecimen()
Initialise an instance with random values.
getLibStatut($mode)
Return the label of the status.
setNoEmail($no_email)
Set "blacklist" mailing status.
LibPubPriv($status)
Return translated label of Public or Private.
setCategories($categories)
Sets object to supplied categories.
findNearest($id=0, $lastname='', $firstname='', $ref_ext='', $email='', $ref_alias='', $socid=0)
Search the contact that match the most the provided parameters.
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.
getFullName($langs, $option=0, $nameorder=-1, $maxlen=0)
Return full name (civility+' '+name+' '+lastname)
setUpperOrLowerCase()
Set to upper or ucwords/lower if needed.
trait CommonSocialNetworks
Superclass for social networks.
getCountry($searchkey, $withcode='', $dbtouse=null, $outputlangs=null, $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_is_dir($folder)
Test if filename is a directory.
deleteFilesIntoDatabaseIndex($dir, $file, $mode='uploaded', $object=null)
Delete files into database index using search criteria.
setEntity($currentobject)
Set entity id to use when to create an object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_format_address($object, $withcountry=0, $sep="\n", $outputlangs=null, $mode=0, $extralangcode='')
Return a formatted address (part address/zip/town/state) according to country rules.
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
img_action($titlealt, $numaction, $picto='', $moreatt='')
Show logo action.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
div refaddress div address
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:161