dolibarr 24.0.0-beta
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
53 public $TRIGGER_PREFIX = 'CONTACT';
54
58 public $element = 'contact';
59
63 public $table_element = 'socpeople';
64
68 public $picto = 'contact';
69
94 // BEGIN MODULEBUILDER PROPERTIES
98 public $fields = array(
99 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'),
100 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 30, 'index' => 1),
101 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 35),
102 'civility' => array('type' => 'varchar(6)', 'label' => 'Civility', 'enabled' => 1, 'visible' => 3, 'position' => 40),
103 'lastname' => array('type' => 'varchar(50)', 'label' => 'Lastname', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'showoncombobox' => 1, 'searchall' => 1),
104 'name_alias' => array('type' => 'varchar(255)', 'label' => 'Name alias', 'enabled' => 1, 'visible' => -1, 'position' => 46, 'searchall' => 1),
105 'firstname' => array('type' => 'varchar(50)', 'label' => 'Firstname', 'enabled' => 1, 'visible' => 1, 'position' => 50, 'showoncombobox' => 1, 'searchall' => 1),
106 'poste' => array('type' => 'varchar(80)', 'label' => 'PostOrFunction', 'enabled' => 1, 'visible' => -1, 'position' => 52),
107 'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 55),
108 'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 60),
109 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 65),
110 'fk_departement' => array('type' => 'integer', 'label' => 'Fk departement', 'enabled' => 1, 'visible' => 3, 'position' => 70),
111 'fk_pays' => array('type' => 'integer', 'label' => 'Fk pays', 'enabled' => 1, 'visible' => 3, 'position' => 75),
112 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => 1, 'position' => 77, 'searchall' => 1),
113 'birthday' => array('type' => 'date', 'label' => 'Birthday', 'enabled' => 1, 'visible' => -1, 'position' => 80),
114 'phone' => array('type' => 'varchar(30)', 'label' => 'Phone', 'enabled' => 1, 'visible' => 1, 'position' => 90, 'searchall' => 1),
115 'phone_perso' => array('type' => 'varchar(30)', 'label' => 'PhonePerso', 'enabled' => 1, 'visible' => -1, 'position' => 95, 'searchall' => 1),
116 'phone_mobile' => array('type' => 'varchar(30)', 'label' => 'PhoneMobile', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'searchall' => 1),
117 'fax' => array('type' => 'varchar(30)', 'label' => 'Fax', 'enabled' => 1, 'visible' => -1, 'position' => 105, 'searchall' => 1),
118 'email' => array('type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'visible' => 1, 'position' => 110, 'searchall' => 1),
119 'socialnetworks' => array('type' => 'text', 'label' => 'SocialNetworks', 'enabled' => 1, 'visible' => 3, 'position' => 115),
120 'photo' => array('type' => 'varchar(255)', 'label' => 'Photo', 'enabled' => 1, 'visible' => 3, 'position' => 170),
121 'priv' => array('type' => 'smallint(6)', 'label' => 'ContactVisibility', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 175),
122 'fk_stcommcontact' => array('type' => 'integer', 'label' => 'ProspectStatus', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 220),
123 'fk_prospectcontactlevel' => array('type' => 'varchar(12)', 'label' => 'ProspectLevel', 'enabled' => 1, 'visible' => -1, 'position' => 255),
124 //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),
125 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 195, 'searchall' => 1),
126 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 200, 'searchall' => 1),
127 'default_lang' => array('type' => 'varchar(6)', 'label' => 'Default lang', 'enabled' => 1, 'visible' => 3, 'position' => 205),
128 'canvas' => array('type' => 'varchar(32)', 'label' => 'Canvas', 'enabled' => 1, 'visible' => 3, 'position' => 210),
129 'ip' => array('type' => 'ip', 'label' => 'IPAddress', 'enabled' => '1', 'position' => 700, 'notnull' => 0, 'visible' => '-2', 'comment' => 'ip used to create record (for public submission page)'),
130 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 300),
131 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305),
132 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 3, 'position' => 310),
133 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 3, 'position' => 315),
134 'statut' => array('type' => 'tinyint(4)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500),
135 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000),
136 );
137
141 public $civility_id; // In fact we store civility_code
145 public $civility_code;
149 public $civility;
150
154 public $gender;
155
159 public $birthday_alert;
160
166 public $civilite;
167
171 public $fullname;
172
176 public $name_alias;
177
181 public $address;
182
186 public $zip;
187
191 public $town;
192
196 public $state_id;
197
201 public $state_code;
202
206 public $state;
207
211 public $poste;
212
216 public $socid; // both socid and fk_soc are used
220 public $fk_soc; // both socid and fk_soc are used
221
225 public $socname;
226
231 public $statut;
232
236 public $code;
237
242 public $email;
243
250 public $mail;
251
256 public $url;
257
263 public $no_email;
264
269 public $socialnetworks;
270
274 public $photo;
275
279 public $phone_pro;
280
284 public $phone_perso;
285
289 public $phone_mobile;
290
294 public $fax;
295
300 public $priv;
301
305 public $birthday;
306
310 public $default_lang;
311
315 public $ref_facturation;
316
320 public $ref_contrat;
321
325 public $ref_commande;
326
330 public $ref_propal;
331
335 public $user_id;
336
340 public $user_login;
341
345 public $ip;
346 // END MODULEBUILDER PROPERTIES
347
351 public $roles;
352
356 public $cacheprospectstatus = array();
357
361 public $fk_prospectlevel;
362
366 public $stcomm_id;
367
371 public $statut_commercial;
372
376 public $stcomm_picto;
377
378
384 public function __construct($db)
385 {
386 $this->db = $db;
387 $this->statut = 1; // By default, status is enabled
388 $this->status = 1; // By default, status is enabled
389 $this->ismultientitymanaged = 1;
390 $this->isextrafieldmanaged = 1;
391
392 $this->fields['ref_ext']['visible'] = getDolGlobalInt('MAIN_LIST_SHOW_REF_EXT');
393
394 if (!isModEnabled('mailing')) {
395 $this->fields['no_email']['enabled'] = 0;
396 }
397 // typical ['s.nom'] is used for third-parties
398 if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) {
399 $this->fields['fk_soc']['enabled'] = 0;
400 $this->fields['fk_soc']['searchall'] = 0;
401 }
402
403 // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
404 if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) { // Default behaviour
405 $this->fields['fk_stcommcontact']['enabled'] = 0;
406 $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
407 }
408
409 // Unset fields that are disabled
410 foreach ($this->fields as $key => $val) {
411 if (isset($val['enabled']) && empty($val['enabled'])) {
412 unset($this->fields[$key]);
413 }
414 }
415
416 // Translate some data of arrayofkeyval
417 /*if (is_object($langs))
418 {
419 foreach($this->fields as $key => $val)
420 {
421 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
422 {
423 foreach($val['arrayofkeyval'] as $key2 => $val2)
424 {
425 $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
426 }
427 }
428 }
429 }*/
430 }
431
437 public function loadStateBoard()
438 {
439 global $user, $hookmanager;
440
441 $this->nb = array();
442 $clause = "WHERE";
443
444 $sql = "SELECT count(sp.rowid) as nb";
445 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
446 if (!$user->hasRight('societe', 'client', 'voir')) {
447 $sql .= ", ".MAIN_DB_PREFIX."societe as s";
448 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
449 $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
450 $clause = "AND";
451 }
452 $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
453 $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
454 if ($user->socid > 0) {
455 $sql .= " AND sp.fk_soc = ".((int) $user->socid);
456 }
457 // Add where from hooks
458 if (is_object($hookmanager)) {
459 $parameters = array();
460 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
461 $sql .= $hookmanager->resPrint;
462 }
463
464 $resql = $this->db->query($sql);
465 if ($resql) {
466 while ($obj = $this->db->fetch_object($resql)) {
467 $this->nb["contacts"] = $obj->nb;
468 }
469 $this->db->free($resql);
470 return 1;
471 } else {
472 dol_print_error($this->db);
473 $this->error = $this->db->lasterror();
474 return -1;
475 }
476 }
477
485 public function create($user, $notrigger = 0)
486 {
487 global $conf;
488
489 $error = 0;
490 $now = dol_now();
491
492 if (empty($this->date_creation)) {
493 $this->date_creation = $now;
494 }
495
496 $this->db->begin();
497
498 // Clean parameters
499 $this->name_alias = trim($this->name_alias);
500 $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name);
501 $this->firstname = trim($this->firstname);
502 $this->setUpperOrLowerCase();
503 if (empty($this->socid)) {
504 $this->socid = 0;
505 }
506 if (empty($this->priv)) {
507 $this->priv = 0;
508 }
509 if (!empty($this->statut) && empty($this->status)) {
510 $this->status = 1;
511 }
512 if (empty($this->status)) {
513 $this->status = 0; // This is to convert '' into '0' to avoid bad sql request
514 $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
515 }
516
517 // setEntity will set entity with the right value if empty or change it for the right value if multicompany module is active
518 $this->entity = setEntity($this);
519
520 $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
521 $sql .= " datec";
522 $sql .= ", fk_soc";
523 $sql .= ", name_alias";
524 $sql .= ", lastname";
525 $sql .= ", firstname";
526 $sql .= ", fk_user_creat";
527 $sql .= ", priv";
528 $sql .= ", fk_stcommcontact";
529 $sql .= ", statut";
530 $sql .= ", canvas";
531 $sql .= ", entity";
532 $sql .= ", ref_ext";
533 $sql .= ", import_key";
534 $sql .= ", ip";
535 $sql .= ") VALUES (";
536 $sql .= "'".$this->db->idate($now)."',";
537 if ($this->socid > 0) {
538 $sql .= " ".((int) $this->socid).",";
539 } else {
540 $sql .= "null,";
541 }
542 $sql .= "'".$this->db->escape($this->name_alias)."',";
543 $sql .= "'".$this->db->escape($this->lastname)."',";
544 $sql .= "'".$this->db->escape($this->firstname)."',";
545 $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
546 $sql .= " ".((int) $this->priv).",";
547 $sql .= " 0,";
548 $sql .= " ".((int) $this->status).",";
549 $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
550 $sql .= " ".((int) $this->entity).",";
551 $sql .= "'".$this->db->escape($this->ref_ext)."',";
552 $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").",";
553 $sql .= " ".(!empty($this->ip) ? "'".$this->db->escape($this->ip)."'" : "null");
554 $sql .= ")";
555
556 dol_syslog(get_class($this)."::create", LOG_DEBUG);
557 $resql = $this->db->query($sql);
558 if ($resql) {
559 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
560
561 $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
562 if ($result < 0) {
563 $error++;
564 $this->error = $this->db->lasterror();
565 }
566
567 if (!$error) {
568 $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
569 if ($result < 0) {
570 $error++;
571 $this->error = $this->db->lasterror();
572 }
573 }
574
575 if (!$error && !$notrigger) {
576 // Call trigger
577 $result = $this->call_trigger('CONTACT_CREATE', $user);
578 if ($result < 0) {
579 $error++;
580 }
581 // End call triggers
582 }
583
584 if (!$error) {
585 $this->db->commit();
586 return $this->id;
587 } else {
588 $this->db->rollback();
589 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
590 return -2;
591 }
592 } else {
593 $this->error = $this->db->lasterror();
594
595 $this->db->rollback();
596 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
597 return -1;
598 }
599 }
600
611 public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
612 {
613 global $conf;
614
615 if (empty($this->country_id) && !empty($this->country_code)) {
616 $country_id = getCountry($this->country_code, '3');
617 $this->country_id = is_int($country_id) ? $country_id : 0;
618 }
619
620 $error = 0;
621
622 $this->id = $id;
623
624 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
625
626 // Clean parameters
627 $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext));
628 $this->name_alias = trim($this->name_alias);
629 $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
630 $this->firstname = trim($this->firstname);
631 $this->email = trim($this->email ?? '');
632 $this->phone_pro = trim($this->phone_pro);
633 $this->phone_perso = trim($this->phone_perso);
634 $this->phone_mobile = trim($this->phone_mobile);
635 $this->photo = trim($this->photo);
636 $this->fax = trim($this->fax);
637 $this->zip = (empty($this->zip) ? '' : trim($this->zip));
638 $this->town = (empty($this->town) ? '' : trim($this->town));
639 $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
640 if (!empty($this->statut) && empty($this->status)) {
641 $this->status = 1;
642 }
643 if (empty($this->status)) {
644 $this->status = 0;
645 $this->statut = 0;
646 }
647 if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
648 $this->civility_code = $this->civility_id; // For backward compatibility
649 }
650 $this->setUpperOrLowerCase();
651
652 $this->db->begin();
653
654 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
655 if ($this->socid > 0) {
656 $sql .= " fk_soc = ".((int) $this->socid).",";
657 } elseif ($this->socid == -1) {
658 $sql .= " fk_soc = NULL,";
659 }
660 $sql .= " civility='".$this->db->escape($this->civility_code)."'";
661 $sql .= ", name_alias='".$this->db->escape($this->name_alias)."'";
662 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
663 $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
664 $sql .= ", address='".$this->db->escape((string) $this->address)."'";
665 $sql .= ", zip='".$this->db->escape($this->zip)."'";
666 $sql .= ", town='".$this->db->escape($this->town)."'";
667 $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
668 $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
669 $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
670 $sql .= ", poste='".$this->db->escape($this->poste)."'";
671 $sql .= ", fax='".$this->db->escape($this->fax)."'";
672 $sql .= ", email='".$this->db->escape($this->email)."'";
673 $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
674 $sql .= ", photo='".$this->db->escape($this->photo)."'";
675 $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
676 $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
677 $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
678 $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
679 $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
680 $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
681 $sql .= ", priv = ".((int) $this->priv);
682 $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
683 if (isset($this->stcomm_id)) {
684 $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
685 }
686 $sql .= ", statut = ".((int) $this->status);
687 $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape((string) $user->id)."'" : "NULL");
688 $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
689 $sql .= ", entity = ".((int) $this->entity);
690 $sql .= " WHERE rowid = ".((int) $id);
691
692 dol_syslog(get_class($this)."::update", LOG_DEBUG);
693 $result = $this->db->query($sql);
694 if ($result) {
695 unset($this->country_code);
696 unset($this->country);
697 unset($this->state_code);
698 unset($this->state);
699
700 $action = 'update';
701
702 // Actions on extra fields
703 $result = $this->insertExtraFields();
704 if ($result < 0) {
705 $error++;
706 }
707
708 if (!$error) {
709 $result = $this->updateRoles();
710 if ($result < 0) {
711 $error++;
712 }
713 }
714
715 if (!$error && $this->user_id > 0) {
716 // If contact is linked to a user
717 $tmpobj = new User($this->db);
718 $tmpobj->fetch($this->user_id);
719 $usermustbemodified = 0;
720 if ($tmpobj->office_phone != $this->phone_pro) {
721 $tmpobj->office_phone = $this->phone_pro;
722 $usermustbemodified++;
723 }
724 if ($tmpobj->office_fax != $this->fax) {
725 $tmpobj->office_fax = $this->fax;
726 $usermustbemodified++;
727 }
728 if ($tmpobj->address != $this->address) {
729 $tmpobj->address = $this->address;
730 $usermustbemodified++;
731 }
732 if ($tmpobj->town != $this->town) {
733 $tmpobj->town = $this->town;
734 $usermustbemodified++;
735 }
736 if ($tmpobj->zip != $this->zip) {
737 $tmpobj->zip = $this->zip;
738 $usermustbemodified++;
739 }
740 if ($tmpobj->zip != $this->zip) {
741 $tmpobj->state_id = $this->state_id;
742 $usermustbemodified++;
743 }
744 if ($tmpobj->country_id != $this->country_id) {
745 $tmpobj->country_id = $this->country_id;
746 $usermustbemodified++;
747 }
748 if ($tmpobj->email != $this->email) {
749 $tmpobj->email = $this->email;
750 $usermustbemodified++;
751 }
752 if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
753 $tmpobj->socialnetworks = $this->socialnetworks;
754 $usermustbemodified++;
755 }
756 if ($usermustbemodified) {
757 $result = $tmpobj->update($user, 0, 1, 1, 1);
758 if ($result < 0) {
759 $error++;
760 }
761 }
762 }
763
764 if (!$error && !$notrigger) {
765 // Call trigger
766 $result = $this->call_trigger('CONTACT_MODIFY', $user);
767 if ($result < 0) {
768 $error++;
769 }
770 // End call triggers
771 }
772
773 if (!$error) {
774 $this->db->commit();
775 return 1;
776 } else {
777 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
778 $this->db->rollback();
779 return -$error;
780 }
781 } else {
782 $this->error = $this->db->lasterror().' sql='.$sql;
783 $this->db->rollback();
784 return -1;
785 }
786 }
787
788
789 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
790 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
800 public function _load_ldap_dn($info, $mode = 0)
801 {
802 // phpcs:enable
803 global $conf;
804 $dn = '';
805 if ($mode == 0) {
806 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
807 } elseif ($mode == 1) {
808 $dn = getDolGlobalString('LDAP_CONTACT_DN');
809 } elseif ($mode == 2) {
810 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
811 }
812 return $dn;
813 }
814
815
816 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
817 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
823 public function _load_ldap_info()
824 {
825 // phpcs:enable
826 global $conf, $langs;
827
828 $info = array();
829
830 // Object classes
831 $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
832
833 $this->fullname = $this->getFullName($langs);
834
835 // Fields
836 if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
837 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
838 }
839 if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
840 $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
841 }
842 if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
843 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
844 }
845
846 if ($this->poste) {
847 $info["title"] = $this->poste;
848 }
849 if ($this->socid > 0) {
850 $soc = new Societe($this->db);
851 $soc->fetch($this->socid);
852
853 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
854 if ($soc->client == 1) {
855 $info["businessCategory"] = "Customers";
856 }
857 if ($soc->client == 2) {
858 $info["businessCategory"] = "Prospects";
859 }
860 if ($soc->fournisseur == 1) {
861 $info["businessCategory"] = "Suppliers";
862 }
863 }
864 if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
865 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
866 }
867 if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
868 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
869 }
870 if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
871 $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
872 }
873 if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
874 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
875 }
876 if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
877 $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
878 }
879 if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
880 $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
881 }
882 if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
883 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
884 }
885 if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
886 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
887 }
888 if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
889 $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
890 }
891 if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
892 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
893 }
894
895 if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
896 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
897
898 $info['uidnumber'] = $this->id;
899
900 $info['phpgwTz'] = 0;
901 $info['phpgwMailType'] = 'INTERNET';
902 $info['phpgwMailHomeType'] = 'INTERNET';
903
904 $info["phpgwContactTypeId"] = 'n';
905 $info["phpgwContactCatId"] = 0;
906 $info["phpgwContactAccess"] = "public";
907
908 $info["phpgwContactOwner"] = 1;
909
910 if ($this->email) {
911 $info["rfc822Mailbox"] = $this->email;
912 }
913 if ($this->phone_mobile) {
914 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
915 }
916 }
917
918 return $info;
919 }
920
921
922 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
931 public function update_perso($id, $user = null, $notrigger = 0)
932 {
933 // phpcs:enable
934 $error = 0;
935 $result = false;
936
937 $this->db->begin();
938
939 // Update the contact
940 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
941 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
942 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
943 if ($user) {
944 $sql .= ", fk_user_modif = ".((int) $user->id);
945 }
946 $sql .= " WHERE rowid = ".((int) $id);
947
948 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
949 $resql = $this->db->query($sql);
950 if (!$resql) {
951 $error++;
952 $this->error = $this->db->lasterror();
953 }
954
955 if ($user) {
956 // Update birthday alert
957 if (!empty($this->birthday_alert)) {
958 //check existing
959 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
960 $result_check = $this->db->query($sql_check);
961 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
962 //insert
963 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
964 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
965 $result = $this->db->query($sql);
966 if (!$result) {
967 $error++;
968 $this->error = $this->db->lasterror();
969 }
970 } else {
971 $result = true;
972 }
973 } else {
974 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
975 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
976 $result = $this->db->query($sql);
977 if (!$result) {
978 $error++;
979 $this->error = $this->db->lasterror();
980 }
981 }
982
983 if (!$error && !$notrigger) {
984 // Call trigger
985 $result = $this->call_trigger('CONTACT_MODIFY', $user);
986 if ($result < 0) {
987 $error++;
988 }
989 // End call triggers
990 }
991 }
992
993 if (!$error) {
994 $this->db->commit();
995 return 1;
996 } else {
997 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
998 $this->db->rollback();
999 return -$error;
1000 }
1001 }
1002
1003
1015 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
1016 {
1017 global $langs;
1018
1019 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
1020
1021 if (empty($id) && empty($ref_ext) && empty($email)) {
1022 $this->error = 'BadParameter';
1023 return -1;
1024 }
1025
1026 $langs->loadLangs(array("dict", "companies"));
1027
1028 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1029 $sql .= " c.address, c.statut as status, c.zip, c.town,";
1030 $sql .= " c.fk_pays as country_id,";
1031 $sql .= " c.fk_departement as state_id,";
1032 $sql .= " c.birthday,";
1033 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1034 $sql .= " c.socialnetworks,";
1035 $sql .= " c.photo,";
1036 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1037 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1038 $sql .= " c.import_key,";
1039 $sql .= " c.datec as date_creation, GREATEST(c.tms, cef.tms) as date_modification, c.fk_user_creat, c.fk_user_modif,";
1040 $sql .= " co.label as country, co.code as country_code,";
1041 $sql .= " d.nom as state, d.code_departement as state_code,";
1042 $sql .= " u.rowid as user_id, u.login as user_login,";
1043 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1044 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1045 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as cef ON cef.fk_object=c.rowid";
1046 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1047 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1048 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1049 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1050 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1051 if ($id) {
1052 $sql .= " WHERE c.rowid = ".((int) $id);
1053 } else {
1054 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1055 if ($ref_ext) {
1056 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1057 }
1058 if ($email) {
1059 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1060 }
1061 if ($socid) {
1062 $sql .= " AND c.fk_soc = ".((int) $socid);
1063 }
1064 }
1065
1066 $resql = $this->db->query($sql);
1067 if ($resql) {
1068 $num = $this->db->num_rows($resql);
1069 if ($num > 1) {
1070 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1071 dol_syslog($this->error, LOG_ERR);
1072
1073 return 2;
1074 } elseif ($num) { // $num = 1
1075 $obj = $this->db->fetch_object($resql);
1076
1077 $this->id = $obj->rowid;
1078 $this->entity = $obj->entity;
1079 $this->ref = $obj->rowid;
1080 $this->ref_ext = $obj->ref_ext;
1081
1082 $this->civility_code = $obj->civility_code;
1083 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1084
1085 $this->name_alias = $obj->name_alias;
1086 $this->lastname = $obj->lastname;
1087 $this->firstname = $obj->firstname;
1088 $this->address = $obj->address;
1089 $this->zip = $obj->zip;
1090 $this->town = $obj->town;
1091
1092 $this->date_creation = $this->db->jdate($obj->date_creation);
1093 $this->date_modification = $this->db->jdate($obj->date_modification);
1094 $this->user_creation_id = $obj->fk_user_creat;
1095 $this->user_modification_id = $obj->fk_user_modif;
1096
1097 $this->state_id = $obj->state_id;
1098 $this->state_code = $obj->state_code;
1099 $this->state = $obj->state;
1100
1101 $this->country_id = $obj->country_id;
1102 $this->country_code = $obj->country_id ? $obj->country_code : '';
1103 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1104
1105 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1106 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1107 $this->socname = $obj->socname;
1108 $this->poste = $obj->poste;
1109 $this->status = $obj->status;
1110 $this->statut = $obj->status; // deprecated
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 $error = 0;
1313
1314 $this->db->begin();
1315
1316 if (!$notrigger) {
1317 // Call trigger
1318 $result = $this->call_trigger('CONTACT_DELETE', $user);
1319 if ($result < 0) {
1320 $error++;
1321 }
1322 // End call triggers
1323 }
1324
1325 if (!$error) {
1326 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1327 $sql = "SELECT ec.rowid";
1328 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1329 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1330 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1331 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1332 $sql .= " AND tc.source='external'";
1333 dol_syslog(__METHOD__, LOG_DEBUG);
1334 $resql = $this->db->query($sql);
1335 if ($resql) {
1336 $num = $this->db->num_rows($resql);
1337
1338 $i = 0;
1339 while ($i < $num && !$error) {
1340 $obj = $this->db->fetch_object($resql);
1341
1342 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1343 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1344 dol_syslog(__METHOD__, LOG_DEBUG);
1345 $result = $this->db->query($sqldel);
1346 if (!$result) {
1347 $error++;
1348 $this->error = $this->db->error().' sql='.$sqldel;
1349 }
1350
1351 $i++;
1352 }
1353 } else {
1354 $error++;
1355 $this->error = $this->db->error().' sql='.$sql;
1356 }
1357 }
1358
1359 if (!$error) {
1360 // Remove Roles
1361 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1362 dol_syslog(__METHOD__, LOG_DEBUG);
1363 $resql = $this->db->query($sql);
1364 if (!$resql) {
1365 $error++;
1366 $this->error .= $this->db->lasterror();
1367 $errorflag = -1;
1368 }
1369 }
1370
1371 if (!$error) {
1372 // Remove Notifications
1373 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1374 dol_syslog(__METHOD__, LOG_DEBUG);
1375 $resql = $this->db->query($sql);
1376 if (!$resql) {
1377 $error++;
1378 $this->error .= $this->db->lasterror();
1379 $errorflag = -1;
1380 }
1381 }
1382
1383 if (!$error) {
1384 // Remove category
1385 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1386 dol_syslog(__METHOD__, LOG_DEBUG);
1387 $resql = $this->db->query($sql);
1388 if (!$resql) {
1389 $error++;
1390 $this->error .= $this->db->lasterror();
1391 $errorflag = -1;
1392 }
1393 }
1394
1395 if (!$error) {
1396 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1397 $sql .= " WHERE rowid = ".((int) $this->id);
1398 dol_syslog(__METHOD__, LOG_DEBUG);
1399 $result = $this->db->query($sql);
1400 if (!$result) {
1401 $error++;
1402 $this->error = $this->db->error().' sql='.$sql;
1403 }
1404 }
1405
1406 // Remove extrafields
1407 if (!$error) {
1408 // For avoid conflicts if trigger used
1409 $result = $this->deleteExtraFields();
1410 if ($result < 0) {
1411 $error++;
1412 }
1413 }
1414
1415 if (!$error) {
1416 $this->db->commit();
1417 return 1;
1418 } else {
1419 $this->db->rollback();
1420 dol_syslog("Error ".$this->error, LOG_ERR);
1421 return -1;
1422 }
1423 }
1424
1425
1432 public function info($id)
1433 {
1434 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1435 $sql .= " GREATEST(c.tms, cef.tms) as tms, c.fk_user_modif";
1436 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1437 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as cef ON cef.fk_object=c.rowid";
1438 $sql .= " WHERE c.rowid = ".((int) $id);
1439
1440 $resql = $this->db->query($sql);
1441 if ($resql) {
1442 if ($this->db->num_rows($resql)) {
1443 $obj = $this->db->fetch_object($resql);
1444
1445 $this->id = $obj->rowid;
1446
1447 $this->user_creation_id = $obj->fk_user_creat;
1448 $this->user_modification_id = $obj->fk_user_modif;
1449 $this->date_creation = $this->db->jdate($obj->datec);
1450 $this->date_modification = $this->db->jdate($obj->tms);
1451 }
1452
1453 $this->db->free($resql);
1454 } else {
1455 print $this->db->error();
1456 }
1457 }
1458
1464 public function getNbOfEMailings()
1465 {
1466 $sql = "SELECT count(mc.email) as nb";
1467 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1468 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1469 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1470
1471 $resql = $this->db->query($sql);
1472 if ($resql) {
1473 $obj = $this->db->fetch_object($resql);
1474 $nb = (int) $obj->nb;
1475
1476 $this->db->free($resql);
1477 return $nb;
1478 } else {
1479 $this->error = $this->db->error();
1480 return -1;
1481 }
1482 }
1483
1490 public function getTooltipContentArray($params)
1491 {
1492 global $conf, $langs, $user;
1493
1494 $datas = [];
1495
1496 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1497 return ['optimize' => $langs->trans("ShowContact")];
1498 }
1499 if (!empty($this->photo) && class_exists('Form')) {
1500 $photo = '<div class="photointooltip floatright">';
1501 $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.
1502 $photo .= '</div>';
1503 $datas['photo'] = $photo;
1504 }
1505
1506 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1507 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1508 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1509 if (!empty($this->poste)) {
1510 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1511 }
1512 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1513 $phonelist = array();
1514 $country_code = empty($this->country_code) ? '' : $this->country_code;
1515 if ($this->phone_pro) {
1516 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1517 }
1518 if ($this->phone_mobile) {
1519 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1520 }
1521 if ($this->phone_perso) {
1522 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1523 }
1524 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1525 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1526
1527 return $datas;
1528 }
1529
1543 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $moreparam = '', $save_lastsearch_value = -1, $maxlen = 0, $morecss = 'valignmiddle')
1544 {
1545 global $conf, $langs, $hookmanager;
1546
1547 if (!empty($conf->dol_no_mouse_hover)) {
1548 $notooltip = 1; // Force disable tooltips
1549 }
1550
1551 $result = '';
1552 $params = [
1553 'id' => $this->id,
1554 'objecttype' => $this->element,
1555 'option' => $option,
1556 ];
1557 $classfortooltip = 'classfortooltip';
1558 $dataparams = '';
1559 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1560 $classfortooltip = 'classforajaxtooltip';
1561 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1562 $label = '';
1563 } else {
1564 $label = implode($this->getTooltipContentArray($params));
1565 }
1566
1567 $baseurl = DOL_URL_ROOT . '/contact/card.php';
1568 $query = ['id' => $this->id];
1569 if ($option !== 'nolink') {
1570 // Add param to save lastsearch_values or not
1571 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1572 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1573 $add_save_lastsearch_values = 1;
1574 }
1575 if ($add_save_lastsearch_values) {
1576 $query = array_merge($query, ['save_lastsearch_values' => 1]);
1577 }
1578 }
1579 $url = dolBuildUrl($baseurl, $query);
1580
1581 $url .= $moreparam;
1582
1583 $linkclose = '';
1584 if (empty($notooltip)) {
1585 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1586 $label = $langs->trans("ShowContact");
1587 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1588 }
1589 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1590 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1591 } else {
1592 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1593 }
1594
1595 if ($option == 'nolink') {
1596 $linkstart = '<span';
1597 } else {
1598 $linkstart = '<a href="'.$url.'"';
1599 }
1600 $linkstart .= $linkclose.'>';
1601 if ($option == 'nolink') {
1602 $linkend = '</span>';
1603 } else {
1604 $linkend = '</a>';
1605 }
1606
1607 $result .= $linkstart;
1608
1609 if ($withpicto) {
1610 if ($withpicto < 0) {
1611 $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>';
1612 if ($withpicto != 2 && $withpicto != -2) {
1613 $result .= ' ';
1614 }
1615 } else {
1616 $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);
1617 }
1618 }
1619 if ($withpicto != 2 && $withpicto != -2) {
1620 $result .= '<span class="valignmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1621 }
1622
1623 $result .= $linkend;
1624
1625 global $action;
1626 $hookmanager->initHooks(array('contactdao'));
1627 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1628 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1629 if ($reshook > 0) {
1630 $result = $hookmanager->resPrint;
1631 } else {
1632 $result .= $hookmanager->resPrint;
1633 }
1634
1635 return $result;
1636 }
1637
1643 public function getCivilityLabel()
1644 {
1645 global $langs;
1646
1647 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1648 if (empty($code)) {
1649 return '';
1650 }
1651
1652 $langs->load("dict");
1653 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1654 }
1655
1662 public function getLibStatut($mode)
1663 {
1664 return $this->LibStatut($this->status, $mode);
1665 }
1666
1667 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1675 public function LibStatut($status, $mode)
1676 {
1677 // phpcs:enable
1678 global $langs;
1679
1680 $labelStatus = array(
1681 0 => 'ActivityCeased',
1682 1 => 'InActivity',
1683 4 => 'InActivity',
1684 5 => 'ActivityCeased',
1685 );
1686 $labelStatusShort = array(
1687 0 => 'ActivityCeased',
1688 1 => 'InActivity',
1689 4 => 'InActivity',
1690 5 => 'ActivityCeased',
1691 );
1692
1693 $statusType = 'status4';
1694 if ($status == 0 || $status == 5) {
1695 $statusType = 'status5';
1696 }
1697
1698 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1699 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1700
1701 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1702 }
1703
1704
1705 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1712 public function LibPubPriv($status)
1713 {
1714 // phpcs:enable
1715 global $langs;
1716 if ($status == '1') {
1717 return $langs->trans('ContactPrivate');
1718 } else {
1719 return $langs->trans('ContactPublic');
1720 }
1721 }
1722
1723
1731 public function initAsSpecimen()
1732 {
1733 // Get first id of existing company and save it into $socid
1734 $socid = 0;
1735 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1736 $resql = $this->db->query($sql);
1737 if ($resql) {
1738 $obj = $this->db->fetch_object($resql);
1739 if ($obj) {
1740 $socid = $obj->rowid;
1741 }
1742 }
1743
1744 // Initialise parameters
1745 $this->id = 0;
1746 $this->entity = 1;
1747 $this->specimen = 1;
1748 $this->lastname = 'DOLIBARR';
1749 $this->firstname = 'SPECIMEN';
1750 $this->address = '21 jump street';
1751 $this->zip = '99999';
1752 $this->town = 'MyTown';
1753 $this->country_id = 1;
1754 $this->country_code = 'FR';
1755 $this->country = 'France';
1756 $this->email = 'specimen@specimen.com';
1757 $this->socialnetworks = array(
1758 'skype' => 'tom.hanson',
1759 'twitter' => 'tomhanson',
1760 'linkedin' => 'tomhanson',
1761 );
1762 $this->phone_pro = '0909090901';
1763 $this->phone_perso = '0909090902';
1764 $this->phone_mobile = '0909090903';
1765 $this->fax = '0909090909';
1766
1767 $this->note_public = 'This is a comment (public)';
1768 $this->note_private = 'This is a comment (private)';
1769
1770 $this->socid = $socid;
1771 $this->status = 1;
1772
1773 return 1;
1774 }
1775
1782 public function setstatus($status)
1783 {
1784 global $conf, $langs, $user;
1785
1786 $error = 0;
1787
1788 // Check parameters
1789 if (!empty($this->statut) && empty($this->status)) {
1790 $this->status = 1;
1791 }
1792 if ($this->status == $status) {
1793 return 0;
1794 } else {
1795 $this->status = $status;
1796 $this->statut = $status;
1797 }
1798
1799 $this->db->begin();
1800
1801 // User disable
1802 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1803 $sql .= " SET statut = ".((int) $this->status);
1804 $sql .= ", fk_user_modif = ".((int) $user->id);
1805 $sql .= " WHERE rowid = ".((int) $this->id);
1806 $result = $this->db->query($sql);
1807
1808 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1809 if ($result) {
1810 // Call trigger
1811 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1812 if ($result < 0) {
1813 $error++;
1814 }
1815 // End call triggers
1816 }
1817
1818 if ($error) {
1819 $this->db->rollback();
1820 return -$error;
1821 } else {
1822 $this->db->commit();
1823 return 1;
1824 }
1825 }
1826
1838 public function setCategories($categories, $remove_existing = true)
1839 {
1840 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1841 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT, $remove_existing);
1842 }
1843
1852 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1853 {
1854 $tables = array(
1855 'socpeople', 'societe_contacts'
1856 );
1857
1858 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1859 }
1860
1868 public function fetchRoles()
1869 {
1870 global $langs;
1871 $error = 0;
1872 $num = 0;
1873
1874 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1875 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1876 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1877 $sql .= " AND tc.source = 'external' AND tc.active=1";
1878 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1879 $sql .= " AND sc.entity IN (".getEntity('societe').')';
1880
1881 $resql = $this->db->query($sql);
1882 if ($resql) {
1883 $this->roles = array();
1884
1885 $num = $this->db->num_rows($resql);
1886 if ($num > 0) {
1887 while ($obj = $this->db->fetch_object($resql)) {
1888 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1889 $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1890 $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));
1891 }
1892 }
1893 } else {
1894 $error++;
1895 $this->error = $this->db->lasterror();
1896 $this->errors[] = $this->db->lasterror();
1897 }
1898
1899 if (empty($error)) {
1900 return $num;
1901 } else {
1902 return $error * -1;
1903 }
1904 }
1905
1913 public function getContactRoles($element = '')
1914 {
1915 $tab = array();
1916
1917 if ($element == 'action') {
1918 $element = 'agenda';
1919 }
1920
1921 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1922 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1923 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1924 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1925 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1926 $sql .= " WHERE sc.fk_soc = ".((int) $this->socid);
1927 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1928 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1929 $sql .= " AND sp.entity IN (".getEntity('contact').")";
1930 $sql .= " AND tc.active = 1";
1931
1932 $resql = $this->db->query($sql);
1933 if ($resql) {
1934 $num = $this->db->num_rows($resql);
1935 $i = 0;
1936 while ($i < $num) {
1937 $obj = $this->db->fetch_object($resql);
1938 $tab[$obj->id] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1939
1940 $i++;
1941 }
1942
1943 return $tab;
1944 } else {
1945 $this->error = $this->db->error();
1946 dol_print_error($this->db);
1947 return -1;
1948 }
1949 }
1950
1958 public function updateRoles()
1959 {
1960 global $conf;
1961
1962 $error = 0;
1963
1964 if (!isset($this->roles)) {
1965 return 0; // Avoid to loose roles when property not set
1966 }
1967
1968 $this->db->begin();
1969
1970 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1971
1972 $result = $this->db->query($sql);
1973 if (!$result) {
1974 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1975 $error++;
1976 } else {
1977 if (count($this->roles) > 0) {
1978 foreach ($this->roles as $keyRoles => $valRoles) {
1979 if (empty($valRoles)) {
1980 continue;
1981 }
1982 $idrole = 0;
1983 if (is_array($valRoles)) {
1984 $idrole = $valRoles['id'];
1985 } else {
1986 $idrole = $valRoles;
1987 }
1988
1989 $socid = 0;
1990 if (is_array($valRoles)) {
1991 $socid = $valRoles['socid'];
1992 } else {
1993 $socid = $this->socid;
1994 }
1995
1996 if ($socid > 0) {
1997 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1998 $sql .= " (entity,";
1999 $sql .= "date_creation,";
2000 $sql .= "fk_soc,";
2001 $sql .= "fk_c_type_contact,";
2002 $sql .= "fk_socpeople) ";
2003 $sql .= " VALUES (".$conf->entity.",";
2004 $sql .= "'".$this->db->idate(dol_now())."',";
2005 $sql .= $socid.", ";
2006 $sql .= $idrole." , ";
2007 $sql .= $this->id;
2008 $sql .= ")";
2009
2010 $result = $this->db->query($sql);
2011 if (!$result) {
2012 $this->errors[] = $this->db->lasterror().' sql='.$sql;
2013 $error++;
2014 }
2015 }
2016 }
2017 }
2018 }
2019 if (empty($error)) {
2020 $this->db->commit();
2021 return 1;
2022 } else {
2023 $this->error = implode(' ', $this->errors);
2024 $this->db->rollback();
2025 return $error * -1;
2026 }
2027 }
2028
2035 public function loadCacheOfProspStatus($active = 1)
2036 {
2037 global $langs;
2038
2039 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
2040 if ($active >= 0) {
2041 $sql .= " WHERE active = ".((int) $active);
2042 }
2043 $resql = $this->db->query($sql);
2044 $num = $this->db->num_rows($resql);
2045 $i = 0;
2046 while ($i < $num) {
2047 $obj = $this->db->fetch_object($resql);
2048 $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);
2049 $i++;
2050 }
2051 return 1;
2052 }
2053
2059 public function getLibProspLevel()
2060 {
2061 return $this->libProspLevel($this->fk_prospectlevel);
2062 }
2063
2070 public function libProspLevel($fk_prospectlevel)
2071 {
2072 global $langs;
2073
2074 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2075 // If lib not found in language file, we get label from cache/database
2076 if ($lib == "ProspectLevel".$fk_prospectlevel) {
2077 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2078 }
2079 return $lib;
2080 }
2081
2082
2090 public function setProspectLevel(User $user)
2091 {
2092 return $this->update($this->id, $user);
2093 }
2094
2102 public function getLibProspCommStatut($mode = 0, $label = '')
2103 {
2104 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2105 }
2106
2120 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2121 {
2122 global $langs;
2123 $langs->load('customers');
2124
2125 if ($mode == 2) {
2126 if ($statut == '-1' || $statut == 'ST_NO') {
2127 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2128 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2129 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2130 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2131 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2132 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2133 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2134 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2135 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2136 } else {
2137 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);
2138 }
2139 }
2140 if ($mode == 3) {
2141 if ($statut == '-1' || $statut == 'ST_NO') {
2142 return img_action($langs->trans("StatusProspect-1"), '-1', $picto);
2143 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2144 return img_action($langs->trans("StatusProspect0"), '0', $picto);
2145 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2146 return img_action($langs->trans("StatusProspect1"), '1', $picto);
2147 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2148 return img_action($langs->trans("StatusProspect2"), '2', $picto);
2149 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2150 return img_action($langs->trans("StatusProspect3"), '3', $picto);
2151 } else {
2152 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, '0', $picto);
2153 }
2154 }
2155 if ($mode == 4) {
2156 if ($statut == '-1' || $statut == 'ST_NO') {
2157 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2158 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2159 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2160 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2161 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2162 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2163 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2164 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2165 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2166 } else {
2167 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);
2168 }
2169 }
2170
2171 return "Error, mode/status not found";
2172 }
2173
2174
2181 public function setNoEmail($no_email)
2182 {
2183 $error = 0;
2184
2185 // Update mass emailing flag into table mailing_unsubscribe
2186 if ($this->email) {
2187 $this->db->begin();
2188
2189 if ($no_email) {
2190 $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)."'";
2191 $resql = $this->db->query($sql);
2192 if ($resql) {
2193 $obj = $this->db->fetch_object($resql);
2194 $noemail = $obj->nb;
2195 if (empty($noemail)) {
2196 $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())."')";
2197 $resql = $this->db->query($sql);
2198 if (!$resql) {
2199 $error++;
2200 $this->error = $this->db->lasterror();
2201 $this->errors[] = $this->error;
2202 }
2203 }
2204 } else {
2205 $error++;
2206 $this->error = $this->db->lasterror();
2207 $this->errors[] = $this->error;
2208 }
2209 } else {
2210 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2211 $resql = $this->db->query($sql);
2212 if (!$resql) {
2213 $error++;
2214 $this->error = $this->db->lasterror();
2215 $this->errors[] = $this->error;
2216 }
2217 }
2218
2219 if (empty($error)) {
2220 $this->no_email = $no_email;
2221 $this->db->commit();
2222 return 1;
2223 } else {
2224 $this->db->rollback();
2225 return $error * -1;
2226 }
2227 }
2228
2229 return 0;
2230 }
2231
2238 public function getNoEmail()
2239 {
2240 if ($this->email) {
2241 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2242 $resql = $this->db->query($sql);
2243 if ($resql) {
2244 $obj = $this->db->fetch_object($resql);
2245 $this->no_email = $obj->nb;
2246 return 1;
2247 } else {
2248 $this->error = $this->db->lasterror();
2249 $this->errors[] = $this->error;
2250 return -1;
2251 }
2252 }
2253 return 0;
2254 }
2255
2256
2264 public function getKanbanView($option = '', $arraydata = null)
2265 {
2266 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2267
2268 $return = '<div class="box-flex-item box-flex-grow-zero">';
2269 $return .= '<div class="info-box info-box-sm">';
2270 $return .= '<span class="info-box-icon bg-infobox-action">';
2271 if (!is_null($this->photo)) {
2272 $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2273 } else {
2274 $return .= img_picto('', $this->picto);
2275 }
2276 $return .= '</span>';
2277 $return .= '<div class="info-box-content">';
2278 $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->getNomUrl(0) . '</div>';
2279 if ($selected >= 0) {
2280 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2281 }
2282 if (is_object($this->thirdparty)) {
2283 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2284 }
2285 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2286 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2287 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2288 }*/
2289 /*if (method_exists($this, 'LibPubPriv')) {
2290 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2291 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2292 }*/
2293 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2294 $return .= '</div>';
2295 $return .= '</div>';
2296 $return .= '</div>';
2297 return $return;
2298 }
2299}
$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.
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
getNomUrl($withpicto=0, $option='', $notooltip=0, $moreparam='', $save_lastsearch_value=-1, $maxlen=0, $morecss='valignmiddle')
Return name of contact with link (and eventually picto) Use $this->id, $this->lastname,...
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.
setCategories($categories, $remove_existing=true)
Sets object to supplied categories.
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.
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.
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, $usesharelinkifavailable=0)
Return HTML code to output a photo.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:168
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").
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
setEntity($currentobject)
Set entity id to use when to create an object.
dol_print_phone($phone, $countrycode='', $contactid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
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_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
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)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
div refaddress div address
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133