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->zip != $this->zip) {
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 if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
746 $tmpobj->socialnetworks = $this->socialnetworks;
747 $usermustbemodified++;
748 }
749 if ($usermustbemodified) {
750 $result = $tmpobj->update($user, 0, 1, 1, 1);
751 if ($result < 0) {
752 $error++;
753 }
754 }
755 }
756
757 if (!$error && !$notrigger) {
758 // Call trigger
759 $result = $this->call_trigger('CONTACT_MODIFY', $user);
760 if ($result < 0) {
761 $error++;
762 }
763 // End call triggers
764 }
765
766 if (!$error) {
767 $this->db->commit();
768 return 1;
769 } else {
770 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
771 $this->db->rollback();
772 return -$error;
773 }
774 } else {
775 $this->error = $this->db->lasterror().' sql='.$sql;
776 $this->db->rollback();
777 return -1;
778 }
779 }
780
781
782 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
783 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
793 public function _load_ldap_dn($info, $mode = 0)
794 {
795 // phpcs:enable
796 global $conf;
797 $dn = '';
798 if ($mode == 0) {
799 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
800 } elseif ($mode == 1) {
801 $dn = getDolGlobalString('LDAP_CONTACT_DN');
802 } elseif ($mode == 2) {
803 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
804 }
805 return $dn;
806 }
807
808
809 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
810 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
816 public function _load_ldap_info()
817 {
818 // phpcs:enable
819 global $conf, $langs;
820
821 $info = array();
822
823 // Object classes
824 $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
825
826 $this->fullname = $this->getFullName($langs);
827
828 // Fields
829 if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
830 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
831 }
832 if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
833 $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
834 }
835 if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
836 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
837 }
838
839 if ($this->poste) {
840 $info["title"] = $this->poste;
841 }
842 if ($this->socid > 0) {
843 $soc = new Societe($this->db);
844 $soc->fetch($this->socid);
845
846 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
847 if ($soc->client == 1) {
848 $info["businessCategory"] = "Customers";
849 }
850 if ($soc->client == 2) {
851 $info["businessCategory"] = "Prospects";
852 }
853 if ($soc->fournisseur == 1) {
854 $info["businessCategory"] = "Suppliers";
855 }
856 }
857 if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
858 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
859 }
860 if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
861 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
862 }
863 if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
864 $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
865 }
866 if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
867 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
868 }
869 if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
870 $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
871 }
872 if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
873 $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
874 }
875 if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
876 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
877 }
878 if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
879 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
880 }
881 if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
882 $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
883 }
884 if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
885 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
886 }
887
888 if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
889 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
890
891 $info['uidnumber'] = $this->id;
892
893 $info['phpgwTz'] = 0;
894 $info['phpgwMailType'] = 'INTERNET';
895 $info['phpgwMailHomeType'] = 'INTERNET';
896
897 $info["phpgwContactTypeId"] = 'n';
898 $info["phpgwContactCatId"] = 0;
899 $info["phpgwContactAccess"] = "public";
900
901 /*
902 if (dol_strlen($this->egroupware_id) == 0) {
903 $this->egroupware_id = 1;
904 }
905 $info["phpgwContactOwner"] = $this->egroupware_id;
906 */
907 $info["phpgwContactOwner"] = 1;
908
909 if ($this->email) {
910 $info["rfc822Mailbox"] = $this->email;
911 }
912 if ($this->phone_mobile) {
913 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
914 }
915 }
916
917 return $info;
918 }
919
920
921 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
930 public function update_perso($id, $user = null, $notrigger = 0)
931 {
932 // phpcs:enable
933 $error = 0;
934 $result = false;
935
936 $this->db->begin();
937
938 // Update the contact
939 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
940 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
941 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
942 if ($user) {
943 $sql .= ", fk_user_modif = ".((int) $user->id);
944 }
945 $sql .= " WHERE rowid = ".((int) $id);
946
947 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
948 $resql = $this->db->query($sql);
949 if (!$resql) {
950 $error++;
951 $this->error = $this->db->lasterror();
952 }
953
954 if ($user) {
955 // Update birthday alert
956 if (!empty($this->birthday_alert)) {
957 //check existing
958 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
959 $result_check = $this->db->query($sql_check);
960 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
961 //insert
962 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
963 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
964 $result = $this->db->query($sql);
965 if (!$result) {
966 $error++;
967 $this->error = $this->db->lasterror();
968 }
969 } else {
970 $result = true;
971 }
972 } else {
973 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
974 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
975 $result = $this->db->query($sql);
976 if (!$result) {
977 $error++;
978 $this->error = $this->db->lasterror();
979 }
980 }
981
982 if (!$error && !$notrigger) {
983 // Call trigger
984 $result = $this->call_trigger('CONTACT_MODIFY', $user);
985 if ($result < 0) {
986 $error++;
987 }
988 // End call triggers
989 }
990 }
991
992 if (!$error) {
993 $this->db->commit();
994 return 1;
995 } else {
996 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
997 $this->db->rollback();
998 return -$error;
999 }
1000 }
1001
1002
1014 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
1015 {
1016 global $langs;
1017
1018 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
1019
1020 if (empty($id) && empty($ref_ext) && empty($email)) {
1021 $this->error = 'BadParameter';
1022 return -1;
1023 }
1024
1025 $langs->loadLangs(array("dict", "companies"));
1026
1027 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1028 $sql .= " c.address, c.statut, c.zip, c.town,";
1029 $sql .= " c.fk_pays as country_id,";
1030 $sql .= " c.fk_departement as state_id,";
1031 $sql .= " c.birthday,";
1032 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1033 $sql .= " c.socialnetworks,";
1034 $sql .= " c.photo,";
1035 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1036 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1037 $sql .= " c.import_key,";
1038 $sql .= " c.datec as date_creation, c.tms as date_modification, c.fk_user_creat, c.fk_user_modif,";
1039 $sql .= " co.label as country, co.code as country_code,";
1040 $sql .= " d.nom as state, d.code_departement as state_code,";
1041 $sql .= " u.rowid as user_id, u.login as user_login,";
1042 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1043 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1044 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1045 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1046 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1047 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1048 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1049 if ($id) {
1050 $sql .= " WHERE c.rowid = ".((int) $id);
1051 } else {
1052 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1053 if ($ref_ext) {
1054 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1055 }
1056 if ($email) {
1057 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1058 }
1059 if ($socid) {
1060 $sql .= " AND c.fk_soc = ".((int) $socid);
1061 }
1062 }
1063
1064 $resql = $this->db->query($sql);
1065 if ($resql) {
1066 $num = $this->db->num_rows($resql);
1067 if ($num > 1) {
1068 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1069 dol_syslog($this->error, LOG_ERR);
1070
1071 return 2;
1072 } elseif ($num) { // $num = 1
1073 $obj = $this->db->fetch_object($resql);
1074
1075 $this->id = $obj->rowid;
1076 $this->entity = $obj->entity;
1077 $this->ref = $obj->rowid;
1078 $this->ref_ext = $obj->ref_ext;
1079
1080 $this->civility_code = $obj->civility_code;
1081 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1082
1083 $this->name_alias = $obj->name_alias;
1084 $this->lastname = $obj->lastname;
1085 $this->firstname = $obj->firstname;
1086 $this->address = $obj->address;
1087 $this->zip = $obj->zip;
1088 $this->town = $obj->town;
1089
1090 $this->date_creation = $this->db->jdate($obj->date_creation);
1091 $this->date_modification = $this->db->jdate($obj->date_modification);
1092 $this->user_creation_id = $obj->fk_user_creat;
1093 $this->user_modification_id = $obj->fk_user_modif;
1094
1095 $this->state_id = $obj->state_id;
1096 $this->state_code = $obj->state_code;
1097 $this->state = $obj->state;
1098
1099 $this->country_id = $obj->country_id;
1100 $this->country_code = $obj->country_id ? $obj->country_code : '';
1101 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1102
1103 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1104 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1105 $this->socname = $obj->socname;
1106 $this->poste = $obj->poste;
1107 $this->statut = $obj->statut;
1108
1109 $this->fk_prospectlevel = $obj->fk_prospectlevel;
1110
1111 $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1112 $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1113 $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1114 $this->statut_commercial = $libelle; // libelle statut commercial
1115 $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1116
1117 $this->phone_pro = trim($obj->phone);
1118 $this->fax = trim($obj->fax);
1119 $this->phone_perso = trim($obj->phone_perso);
1120 $this->phone_mobile = trim($obj->phone_mobile);
1121
1122 $this->email = $obj->email;
1123 $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1124 $this->photo = $obj->photo;
1125 $this->priv = $obj->priv;
1126 $this->mail = $obj->email;
1127
1128 $this->birthday = $this->db->jdate($obj->birthday);
1129 $this->note = $obj->note_private; // deprecated
1130 $this->note_private = $obj->note_private;
1131 $this->note_public = $obj->note_public;
1132 $this->default_lang = $obj->default_lang;
1133 $this->user_id = $obj->user_id;
1134 $this->user_login = $obj->user_login;
1135 $this->canvas = $obj->canvas;
1136
1137 $this->import_key = $obj->import_key;
1138
1139 // Define gender according to civility
1140 $this->setGenderFromCivility();
1141
1142 // Search Dolibarr user linked to this contact
1143 $sql = "SELECT u.rowid ";
1144 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1145 $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1146
1147 $resql = $this->db->query($sql);
1148 if ($resql) {
1149 if ($this->db->num_rows($resql)) {
1150 $uobj = $this->db->fetch_object($resql);
1151
1152 $this->user_id = $uobj->rowid;
1153 }
1154 $this->db->free($resql);
1155 } else {
1156 $this->error = $this->db->error();
1157 return -1;
1158 }
1159
1160 // Retrieve all extrafield
1161 // fetch optionals attributes and labels
1162 $this->fetch_optionals();
1163
1164 // Load also alerts of this user
1165 if ($user) {
1166 $sql = "SELECT fk_user";
1167 $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1168 $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1169
1170 $resql = $this->db->query($sql);
1171 if ($resql) {
1172 if ($this->db->num_rows($resql)) {
1173 $obj = $this->db->fetch_object($resql);
1174
1175 $this->birthday_alert = 1;
1176 }
1177 $this->db->free($resql);
1178 } else {
1179 $this->error = $this->db->error();
1180 return -1;
1181 }
1182 }
1183
1184 // Load also roles of this address
1185 if ($loadalsoroles) {
1186 $resultRole = $this->fetchRoles();
1187 if ($resultRole < 0) {
1188 return $resultRole;
1189 }
1190 }
1191
1192 return $this->id;
1193 } else {
1194 $langs->load('errors');
1195 $this->error = $langs->trans("ErrorRecordNotFound");
1196 return 0;
1197 }
1198 } else {
1199 $this->error = $this->db->error();
1200 return -1;
1201 }
1202 }
1203
1217 public function findNearest($id = 0, $lastname = '', $firstname = '', $ref_ext = '', $email = '', $ref_alias = '', $socid = 0)
1218 {
1219 // A rowid is known, it is a unique key so we found it
1220 if ($id) {
1221 return $id;
1222 }
1223
1224 // We try to find the contact with exact matching on all fields
1225 // TODO Replace this with step by step search
1226 // Then search on email
1227 // Then search on lastname + firstname
1228 // Then search ref_ext or alias with a OR
1229 $tmpcontact = new Contact($this->db);
1230 $result = $tmpcontact->fetch($id, null, $ref_ext, $email, 0, $socid);
1231
1232 return $result;
1233 }
1234
1235
1236
1243 public function setGenderFromCivility()
1244 {
1245 unset($this->gender);
1246
1247 if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1248 $this->gender = 'man';
1249 } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1250 $this->gender = 'woman';
1251 }
1252 }
1253
1254 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1264 public function load_ref_elements()
1265 {
1266 // phpcs:enable
1267 // Count the elements for which it is contact
1268 $sql = "SELECT tc.element, count(ec.rowid) as nb";
1269 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1270 $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1271 $sql .= " AND fk_socpeople = ".((int) $this->id);
1272 $sql .= " AND tc.source = 'external'";
1273 $sql .= " GROUP BY tc.element";
1274
1275 dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1276
1277 $resql = $this->db->query($sql);
1278 if ($resql) {
1279 while ($obj = $this->db->fetch_object($resql)) {
1280 if ($obj->nb) {
1281 if ($obj->element == 'facture') {
1282 $this->ref_facturation = $obj->nb;
1283 } elseif ($obj->element == 'contrat') {
1284 $this->ref_contrat = $obj->nb;
1285 } elseif ($obj->element == 'commande') {
1286 $this->ref_commande = $obj->nb;
1287 } elseif ($obj->element == 'propal') {
1288 $this->ref_propal = $obj->nb;
1289 }
1290 }
1291 }
1292 $this->db->free($resql);
1293 return 0;
1294 } else {
1295 $this->error = $this->db->lasterror();
1296 return -1;
1297 }
1298 }
1299
1307 public function delete($user, $notrigger = 0)
1308 {
1309 $error = 0;
1310
1311 $this->db->begin();
1312
1313 if (!$error && !$notrigger) {
1314 // Call trigger
1315 $result = $this->call_trigger('CONTACT_DELETE', $user);
1316 if ($result < 0) {
1317 $error++;
1318 }
1319 // End call triggers
1320 }
1321
1322 if (!$error) {
1323 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1324 $sql = "SELECT ec.rowid";
1325 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1326 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1327 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1328 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1329 $sql .= " AND tc.source='external'";
1330 dol_syslog(__METHOD__, LOG_DEBUG);
1331 $resql = $this->db->query($sql);
1332 if ($resql) {
1333 $num = $this->db->num_rows($resql);
1334
1335 $i = 0;
1336 while ($i < $num && !$error) {
1337 $obj = $this->db->fetch_object($resql);
1338
1339 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1340 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1341 dol_syslog(__METHOD__, LOG_DEBUG);
1342 $result = $this->db->query($sqldel);
1343 if (!$result) {
1344 $error++;
1345 $this->error = $this->db->error().' sql='.$sqldel;
1346 }
1347
1348 $i++;
1349 }
1350 } else {
1351 $error++;
1352 $this->error = $this->db->error().' sql='.$sql;
1353 }
1354 }
1355
1356 if (!$error) {
1357 // Remove Roles
1358 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1359 dol_syslog(__METHOD__, LOG_DEBUG);
1360 $resql = $this->db->query($sql);
1361 if (!$resql) {
1362 $error++;
1363 $this->error .= $this->db->lasterror();
1364 $errorflag = -1;
1365 }
1366 }
1367
1368 if (!$error) {
1369 // Remove Notifications
1370 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1371 dol_syslog(__METHOD__, LOG_DEBUG);
1372 $resql = $this->db->query($sql);
1373 if (!$resql) {
1374 $error++;
1375 $this->error .= $this->db->lasterror();
1376 $errorflag = -1;
1377 }
1378 }
1379
1380 if (!$error) {
1381 // Remove category
1382 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1383 dol_syslog(__METHOD__, LOG_DEBUG);
1384 $resql = $this->db->query($sql);
1385 if (!$resql) {
1386 $error++;
1387 $this->error .= $this->db->lasterror();
1388 $errorflag = -1;
1389 }
1390 }
1391
1392 if (!$error) {
1393 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1394 $sql .= " WHERE rowid = ".((int) $this->id);
1395 dol_syslog(__METHOD__, LOG_DEBUG);
1396 $result = $this->db->query($sql);
1397 if (!$result) {
1398 $error++;
1399 $this->error = $this->db->error().' sql='.$sql;
1400 }
1401 }
1402
1403 // Remove extrafields
1404 if (!$error) {
1405 // For avoid conflicts if trigger used
1406 $result = $this->deleteExtraFields();
1407 if ($result < 0) {
1408 $error++;
1409 }
1410 }
1411
1412 if (!$error) {
1413 $this->db->commit();
1414 return 1;
1415 } else {
1416 $this->db->rollback();
1417 dol_syslog("Error ".$this->error, LOG_ERR);
1418 return -1;
1419 }
1420 }
1421
1422
1429 public function info($id)
1430 {
1431 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1432 $sql .= " c.tms as tms, c.fk_user_modif";
1433 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1434 $sql .= " WHERE c.rowid = ".((int) $id);
1435
1436 $resql = $this->db->query($sql);
1437 if ($resql) {
1438 if ($this->db->num_rows($resql)) {
1439 $obj = $this->db->fetch_object($resql);
1440
1441 $this->id = $obj->rowid;
1442
1443 $this->user_creation_id = $obj->fk_user_creat;
1444 $this->user_modification_id = $obj->fk_user_modif;
1445 $this->date_creation = $this->db->jdate($obj->datec);
1446 $this->date_modification = $this->db->jdate($obj->tms);
1447 }
1448
1449 $this->db->free($resql);
1450 } else {
1451 print $this->db->error();
1452 }
1453 }
1454
1460 public function getNbOfEMailings()
1461 {
1462 $sql = "SELECT count(mc.email) as nb";
1463 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1464 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1465 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1466
1467 $resql = $this->db->query($sql);
1468 if ($resql) {
1469 $obj = $this->db->fetch_object($resql);
1470 $nb = $obj->nb;
1471
1472 $this->db->free($resql);
1473 return $nb;
1474 } else {
1475 $this->error = $this->db->error();
1476 return -1;
1477 }
1478 }
1479
1486 public function getTooltipContentArray($params)
1487 {
1488 global $conf, $langs, $user;
1489
1490 $datas = [];
1491
1492 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1493 return ['optimize' => $langs->trans("ShowContact")];
1494 }
1495 if (!empty($this->photo) && class_exists('Form')) {
1496 $photo = '<div class="photointooltip floatright">';
1497 $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.
1498 $photo .= '</div>';
1499 $datas['photo'] = $photo;
1500 }
1501
1502 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1503 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1504 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1505 if (!empty($this->poste)) {
1506 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1507 }
1508 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1509 $phonelist = array();
1510 $country_code = empty($this->country_code) ? '' : $this->country_code;
1511 if ($this->phone_pro) {
1512 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1513 }
1514 if ($this->phone_mobile) {
1515 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1516 }
1517 if ($this->phone_perso) {
1518 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1519 }
1520 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1521 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1522
1523 return $datas;
1524 }
1525
1539 public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = 'valignmiddle')
1540 {
1541 global $conf, $langs, $hookmanager;
1542
1543 if (!empty($conf->dol_no_mouse_hover)) {
1544 $notooltip = 1; // Force disable tooltips
1545 }
1546
1547 $result = '';
1548 $params = [
1549 'id' => $this->id,
1550 'objecttype' => $this->element,
1551 'option' => $option,
1552 ];
1553 $classfortooltip = 'classfortooltip';
1554 $dataparams = '';
1555 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1556 $classfortooltip = 'classforajaxtooltip';
1557 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1558 $label = '';
1559 } else {
1560 $label = implode($this->getTooltipContentArray($params));
1561 }
1562
1563 $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1564
1565 if ($option !== 'nolink') {
1566 // Add param to save lastsearch_values or not
1567 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1568 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1569 $add_save_lastsearch_values = 1;
1570 }
1571 if ($url && $add_save_lastsearch_values) {
1572 $url .= '&save_lastsearch_values=1';
1573 }
1574 }
1575
1576 $url .= $moreparam;
1577
1578 $linkclose = '';
1579 if (empty($notooltip)) {
1580 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1581 $label = $langs->trans("ShowContact");
1582 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1583 }
1584 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1585 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1586 } else {
1587 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1588 }
1589
1590 if ($option == 'nolink' || empty($url)) {
1591 $linkstart = '<span';
1592 } else {
1593 $linkstart = '<a href="'.$url.'"';
1594 }
1595 $linkstart .= $linkclose.'>';
1596 if ($option == 'nolink' || empty($url)) {
1597 $linkend = '</span>';
1598 } else {
1599 $linkend = '</a>';
1600 }
1601
1602 $result .= $linkstart;
1603
1604 if ($withpicto) {
1605 if ($withpicto < 0) {
1606 $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>';
1607 if ($withpicto != 2 && $withpicto != -2) {
1608 $result .= ' ';
1609 }
1610 } else {
1611 $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);
1612 }
1613 }
1614 if ($withpicto != 2 && $withpicto != -2) {
1615 $result .= '<span class="valignmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1616 }
1617
1618 $result .= $linkend;
1619
1620 global $action;
1621 $hookmanager->initHooks(array('contactdao'));
1622 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1623 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1624 if ($reshook > 0) {
1625 $result = $hookmanager->resPrint;
1626 } else {
1627 $result .= $hookmanager->resPrint;
1628 }
1629
1630 return $result;
1631 }
1632
1638 public function getCivilityLabel()
1639 {
1640 global $langs;
1641
1642 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1643 if (empty($code)) {
1644 return '';
1645 }
1646
1647 $langs->load("dict");
1648 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1649 }
1650
1657 public function getLibStatut($mode)
1658 {
1659 return $this->LibStatut($this->statut, $mode);
1660 }
1661
1662 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1670 public function LibStatut($status, $mode)
1671 {
1672 // phpcs:enable
1673 global $langs;
1674
1675 $labelStatus = array(
1676 0 => 'ActivityCeased',
1677 1 => 'InActivity',
1678 4 => 'InActivity',
1679 5 => 'ActivityCeased',
1680 );
1681 $labelStatusShort = array(
1682 0 => 'ActivityCeased',
1683 1 => 'InActivity',
1684 4 => 'InActivity',
1685 5 => 'ActivityCeased',
1686 );
1687
1688 $statusType = 'status4';
1689 if ($status == 0 || $status == 5) {
1690 $statusType = 'status5';
1691 }
1692
1693 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1694 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1695
1696 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1697 }
1698
1699
1700 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1707 public function LibPubPriv($status)
1708 {
1709 // phpcs:enable
1710 global $langs;
1711 if ($status == '1') {
1712 return $langs->trans('ContactPrivate');
1713 } else {
1714 return $langs->trans('ContactPublic');
1715 }
1716 }
1717
1718
1726 public function initAsSpecimen()
1727 {
1728 // Get first id of existing company and save it into $socid
1729 $socid = 0;
1730 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1731 $resql = $this->db->query($sql);
1732 if ($resql) {
1733 $obj = $this->db->fetch_object($resql);
1734 if ($obj) {
1735 $socid = $obj->rowid;
1736 }
1737 }
1738
1739 // Initialise parameters
1740 $this->id = 0;
1741 $this->entity = 1;
1742 $this->specimen = 1;
1743 $this->lastname = 'DOLIBARR';
1744 $this->firstname = 'SPECIMEN';
1745 $this->address = '21 jump street';
1746 $this->zip = '99999';
1747 $this->town = 'MyTown';
1748 $this->country_id = 1;
1749 $this->country_code = 'FR';
1750 $this->country = 'France';
1751 $this->email = 'specimen@specimen.com';
1752 $this->socialnetworks = array(
1753 'skype' => 'tom.hanson',
1754 'twitter' => 'tomhanson',
1755 'linkedin' => 'tomhanson',
1756 );
1757 $this->phone_pro = '0909090901';
1758 $this->phone_perso = '0909090902';
1759 $this->phone_mobile = '0909090903';
1760 $this->fax = '0909090909';
1761
1762 $this->note_public = 'This is a comment (public)';
1763 $this->note_private = 'This is a comment (private)';
1764
1765 $this->socid = $socid;
1766 $this->statut = 1;
1767
1768 return 1;
1769 }
1770
1777 public function setstatus($status)
1778 {
1779 global $conf, $langs, $user;
1780
1781 $error = 0;
1782
1783 // Check parameters
1784 if ($this->statut == $status) {
1785 return 0;
1786 } else {
1787 $this->statut = $status;
1788 }
1789
1790 $this->db->begin();
1791
1792 // User disable
1793 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1794 $sql .= " SET statut = ".((int) $this->statut);
1795 $sql .= ", fk_user_modif = ".((int) $user->id);
1796 $sql .= " WHERE rowid = ".((int) $this->id);
1797 $result = $this->db->query($sql);
1798
1799 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1800 if ($result) {
1801 // Call trigger
1802 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1803 if ($result < 0) {
1804 $error++;
1805 }
1806 // End call triggers
1807 }
1808
1809 if ($error) {
1810 $this->db->rollback();
1811 return -$error;
1812 } else {
1813 $this->db->commit();
1814 return 1;
1815 }
1816 }
1817
1828 public function setCategories($categories)
1829 {
1830 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1831 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1832 }
1833
1842 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1843 {
1844 $tables = array(
1845 'socpeople', 'societe_contacts'
1846 );
1847
1848 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1849 }
1850
1858 public function fetchRoles()
1859 {
1860 global $langs;
1861 $error = 0;
1862 $num = 0;
1863
1864 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1865 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1866 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1867 $sql .= " AND tc.source = 'external' AND tc.active=1";
1868 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1869 $sql .= " AND sc.entity IN (".getEntity('societe').')';
1870
1871 $resql = $this->db->query($sql);
1872 if ($resql) {
1873 $this->roles = array();
1874
1875 $num = $this->db->num_rows($resql);
1876 if ($num > 0) {
1877 while ($obj = $this->db->fetch_object($resql)) {
1878 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1879 $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1880 $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));
1881 }
1882 }
1883 } else {
1884 $error++;
1885 $this->error = $this->db->lasterror();
1886 $this->errors[] = $this->db->lasterror();
1887 }
1888
1889 if (empty($error)) {
1890 return $num;
1891 } else {
1892 return $error * -1;
1893 }
1894 }
1895
1903 public function getContactRoles($element = '')
1904 {
1905 $tab = array();
1906
1907 if ($element == 'action') {
1908 $element = 'agenda';
1909 }
1910
1911 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1912 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1913 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1914 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1915 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1916 $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1917 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1918 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1919 $sql .= " AND sp.entity IN (".getEntity('contact').")";
1920 $sql .= " AND tc.active = 1";
1921
1922 dol_syslog(__METHOD__, LOG_DEBUG);
1923 $resql = $this->db->query($sql);
1924 if ($resql) {
1925 $num = $this->db->num_rows($resql);
1926 $i = 0;
1927 while ($i < $num) {
1928 $obj = $this->db->fetch_object($resql);
1929 $tab[] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1930
1931 $i++;
1932 }
1933
1934 return $tab;
1935 } else {
1936 $this->error = $this->db->error();
1937 dol_print_error($this->db);
1938 return -1;
1939 }
1940 }
1941
1949 public function updateRoles()
1950 {
1951 global $conf;
1952
1953 $error = 0;
1954
1955 if (!isset($this->roles)) {
1956 return 0; // Avoid to loose roles when property not set
1957 }
1958
1959 $this->db->begin();
1960
1961 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1962
1963 $result = $this->db->query($sql);
1964 if (!$result) {
1965 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1966 $error++;
1967 } else {
1968 if (count($this->roles) > 0) {
1969 foreach ($this->roles as $keyRoles => $valRoles) {
1970 if (empty($valRoles)) {
1971 continue;
1972 }
1973 $idrole = 0;
1974 if (is_array($valRoles)) {
1975 $idrole = $valRoles['id'];
1976 } else {
1977 $idrole = $valRoles;
1978 }
1979
1980 $socid = 0;
1981 if (is_array($valRoles)) {
1982 $socid = $valRoles['socid'];
1983 } else {
1984 $socid = $this->socid;
1985 }
1986
1987 if ($socid > 0) {
1988 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1989 $sql .= " (entity,";
1990 $sql .= "date_creation,";
1991 $sql .= "fk_soc,";
1992 $sql .= "fk_c_type_contact,";
1993 $sql .= "fk_socpeople) ";
1994 $sql .= " VALUES (".$conf->entity.",";
1995 $sql .= "'".$this->db->idate(dol_now())."',";
1996 $sql .= $socid.", ";
1997 $sql .= $idrole." , ";
1998 $sql .= $this->id;
1999 $sql .= ")";
2000
2001 $result = $this->db->query($sql);
2002 if (!$result) {
2003 $this->errors[] = $this->db->lasterror().' sql='.$sql;
2004 $error++;
2005 }
2006 }
2007 }
2008 }
2009 }
2010 if (empty($error)) {
2011 $this->db->commit();
2012 return 1;
2013 } else {
2014 $this->error = implode(' ', $this->errors);
2015 $this->db->rollback();
2016 return $error * -1;
2017 }
2018 }
2019
2026 public function loadCacheOfProspStatus($active = 1)
2027 {
2028 global $langs;
2029
2030 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
2031 if ($active >= 0) {
2032 $sql .= " WHERE active = ".((int) $active);
2033 }
2034 $resql = $this->db->query($sql);
2035 $num = $this->db->num_rows($resql);
2036 $i = 0;
2037 while ($i < $num) {
2038 $obj = $this->db->fetch_object($resql);
2039 $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);
2040 $i++;
2041 }
2042 return 1;
2043 }
2044
2050 public function getLibProspLevel()
2051 {
2052 return $this->libProspLevel($this->fk_prospectlevel);
2053 }
2054
2061 public function libProspLevel($fk_prospectlevel)
2062 {
2063 global $langs;
2064
2065 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2066 // If lib not found in language file, we get label from cache/database
2067 if ($lib == "ProspectLevel".$fk_prospectlevel) {
2068 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2069 }
2070 return $lib;
2071 }
2072
2073
2081 public function setProspectLevel(User $user)
2082 {
2083 return $this->update($this->id, $user);
2084 }
2085
2093 public function getLibProspCommStatut($mode = 0, $label = '')
2094 {
2095 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2096 }
2097
2111 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2112 {
2113 global $langs;
2114 $langs->load('customers');
2115
2116 if ($mode == 2) {
2117 if ($statut == '-1' || $statut == 'ST_NO') {
2118 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2119 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2120 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2121 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2122 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2123 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2124 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2125 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2126 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2127 } else {
2128 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);
2129 }
2130 }
2131 if ($mode == 3) {
2132 if ($statut == '-1' || $statut == 'ST_NO') {
2133 return img_action($langs->trans("StatusProspect-1"), '-1', $picto);
2134 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2135 return img_action($langs->trans("StatusProspect0"), '0', $picto);
2136 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2137 return img_action($langs->trans("StatusProspect1"), '1', $picto);
2138 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2139 return img_action($langs->trans("StatusProspect2"), '2', $picto);
2140 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2141 return img_action($langs->trans("StatusProspect3"), '3', $picto);
2142 } else {
2143 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, '0', $picto);
2144 }
2145 }
2146 if ($mode == 4) {
2147 if ($statut == '-1' || $statut == 'ST_NO') {
2148 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2149 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2150 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2151 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2152 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2153 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2154 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2155 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2156 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2157 } else {
2158 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);
2159 }
2160 }
2161
2162 return "Error, mode/status not found";
2163 }
2164
2165
2172 public function setNoEmail($no_email)
2173 {
2174 $error = 0;
2175
2176 // Update mass emailing flag into table mailing_unsubscribe
2177 if ($this->email) {
2178 $this->db->begin();
2179
2180 if ($no_email) {
2181 $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)."'";
2182 $resql = $this->db->query($sql);
2183 if ($resql) {
2184 $obj = $this->db->fetch_object($resql);
2185 $noemail = $obj->nb;
2186 if (empty($noemail)) {
2187 $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())."')";
2188 $resql = $this->db->query($sql);
2189 if (!$resql) {
2190 $error++;
2191 $this->error = $this->db->lasterror();
2192 $this->errors[] = $this->error;
2193 }
2194 }
2195 } else {
2196 $error++;
2197 $this->error = $this->db->lasterror();
2198 $this->errors[] = $this->error;
2199 }
2200 } else {
2201 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2202 $resql = $this->db->query($sql);
2203 if (!$resql) {
2204 $error++;
2205 $this->error = $this->db->lasterror();
2206 $this->errors[] = $this->error;
2207 }
2208 }
2209
2210 if (empty($error)) {
2211 $this->no_email = $no_email;
2212 $this->db->commit();
2213 return 1;
2214 } else {
2215 $this->db->rollback();
2216 return $error * -1;
2217 }
2218 }
2219
2220 return 0;
2221 }
2222
2229 public function getNoEmail()
2230 {
2231 if ($this->email) {
2232 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2233 $resql = $this->db->query($sql);
2234 if ($resql) {
2235 $obj = $this->db->fetch_object($resql);
2236 $this->no_email = $obj->nb;
2237 return 1;
2238 } else {
2239 $this->error = $this->db->lasterror();
2240 $this->errors[] = $this->error;
2241 return -1;
2242 }
2243 }
2244 return 0;
2245 }
2246
2247
2255 public function getKanbanView($option = '', $arraydata = null)
2256 {
2257 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2258
2259 $return = '<div class="box-flex-item box-flex-grow-zero">';
2260 $return .= '<div class="info-box info-box-sm">';
2261 $return .= '<span class="info-box-icon bg-infobox-action">';
2262 //var_dump($this->photo);exit;
2263 if (property_exists($this, 'photo') && !is_null($this->photo)) {
2264 $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2265 } else {
2266 $return .= img_picto('', $this->picto);
2267 }
2268 $return .= '</span>';
2269 $return .= '<div class="info-box-content">';
2270 $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref).'</div>';
2271 if ($selected >= 0) {
2272 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2273 }
2274 if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2275 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2276 }
2277 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2278 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2279 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2280 }*/
2281 /*if (method_exists($this, 'LibPubPriv')) {
2282 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2283 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2284 }*/
2285 if (method_exists($this, 'getLibStatut')) {
2286 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2287 }
2288 $return .= '</div>';
2289 $return .= '</div>';
2290 $return .= '</div>';
2291 return $return;
2292 }
2293}
$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...
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").
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.
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