dolibarr 21.0.0-alpha
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 Frédéric France <frederic.france@free.fr>
15 * Copyright (C) 2024 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' => 'Ref ext', 'enabled' => 1, 'visible' => 3, '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', '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 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 300),
124 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305),
125 'fk_user_creat' => array('type' => 'integer', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 3, 'position' => 310),
126 'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 3, 'position' => 315),
127 'statut' => array('type' => 'tinyint(4)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500),
128 'ip' => array('type' => 'varchar(250)', 'label' => 'Ip', 'enabled' => '1', 'position' => 700, 'notnull' => 0, 'visible' => '0', 'comment' => 'ip used to create record (for public submission page)'),
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
346
351 public $oldcopy; // To contain a clone of this when we need to save old properties of object
352
356 public $roles;
357
361 public $cacheprospectstatus = array();
362
366 public $fk_prospectlevel;
367
371 public $stcomm_id;
372
376 public $statut_commercial;
377
381 public $stcomm_picto;
382
383
389 public function __construct($db)
390 {
391 $this->db = $db;
392 $this->statut = 1; // By default, status is enabled
393 $this->ismultientitymanaged = 1;
394 $this->isextrafieldmanaged = 1;
395
396 if (!isModEnabled('mailing')) {
397 $this->fields['no_email']['enabled'] = 0;
398 }
399 // typical ['s.nom'] is used for third-parties
400 if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) {
401 $this->fields['fk_soc']['enabled'] = 0;
402 $this->fields['fk_soc']['searchall'] = 0;
403 }
404
405 // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
406 if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) { // Default behaviour
407 $this->fields['fk_stcommcontact']['enabled'] = 0;
408 $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
409 }
410
411 // Unset fields that are disabled
412 foreach ($this->fields as $key => $val) {
413 if (isset($val['enabled']) && empty($val['enabled'])) {
414 unset($this->fields[$key]);
415 }
416 }
417
418 // Translate some data of arrayofkeyval
419 /*if (is_object($langs))
420 {
421 foreach($this->fields as $key => $val)
422 {
423 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
424 {
425 foreach($val['arrayofkeyval'] as $key2 => $val2)
426 {
427 $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
428 }
429 }
430 }
431 }*/
432 }
433
439 public function loadStateBoard()
440 {
441 global $user, $hookmanager;
442
443 $this->nb = array();
444 $clause = "WHERE";
445
446 $sql = "SELECT count(sp.rowid) as nb";
447 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
448 if (!$user->hasRight('societe', 'client', 'voir')) {
449 $sql .= ", ".MAIN_DB_PREFIX."societe as s";
450 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
451 $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
452 $clause = "AND";
453 }
454 $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
455 $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
456 if ($user->socid > 0) {
457 $sql .= " AND sp.fk_soc = ".((int) $user->socid);
458 }
459 // Add where from hooks
460 if (is_object($hookmanager)) {
461 $parameters = array();
462 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
463 $sql .= $hookmanager->resPrint;
464 }
465
466 $resql = $this->db->query($sql);
467 if ($resql) {
468 while ($obj = $this->db->fetch_object($resql)) {
469 $this->nb["contacts"] = $obj->nb;
470 }
471 $this->db->free($resql);
472 return 1;
473 } else {
474 dol_print_error($this->db);
475 $this->error = $this->db->lasterror();
476 return -1;
477 }
478 }
479
487 public function create($user, $notrigger = 0)
488 {
489 global $conf;
490
491 $error = 0;
492 $now = dol_now();
493
494 $this->db->begin();
495
496 // Clean parameters
497 $this->name_alias = trim($this->name_alias);
498 $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name);
499 $this->firstname = trim($this->firstname);
500 $this->setUpperOrLowerCase();
501 if (empty($this->socid)) {
502 $this->socid = 0;
503 }
504 if (empty($this->priv)) {
505 $this->priv = 0;
506 }
507 if (empty($this->statut)) {
508 $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
509 }
510
511 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
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 $error = 0;
611
612 $this->id = $id;
613
614 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
615
616 // Clean parameters
617 $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext));
618 $this->name_alias = trim($this->name_alias);
619 $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
620 $this->firstname = trim($this->firstname);
621 $this->email = trim($this->email);
622 $this->phone_pro = trim($this->phone_pro);
623 $this->phone_perso = trim($this->phone_perso);
624 $this->phone_mobile = trim($this->phone_mobile);
625 $this->photo = trim($this->photo);
626 $this->fax = trim($this->fax);
627 $this->zip = (empty($this->zip) ? '' : trim($this->zip));
628 $this->town = (empty($this->town) ? '' : trim($this->town));
629 $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
630 if (empty($this->statut)) {
631 $this->statut = 0;
632 }
633 if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
634 $this->civility_code = $this->civility_id; // For backward compatibility
635 }
636 $this->setUpperOrLowerCase();
637
638 $this->db->begin();
639
640 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
641 if ($this->socid > 0) {
642 $sql .= " fk_soc = ".((int) $this->socid).",";
643 } elseif ($this->socid == -1) {
644 $sql .= " fk_soc = NULL,";
645 }
646 $sql .= " civility='".$this->db->escape($this->civility_code)."'";
647 $sql .= ", name_alias='".$this->db->escape($this->name_alias)."'";
648 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
649 $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
650 $sql .= ", address='".$this->db->escape($this->address)."'";
651 $sql .= ", zip='".$this->db->escape($this->zip)."'";
652 $sql .= ", town='".$this->db->escape($this->town)."'";
653 $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
654 $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
655 $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
656 $sql .= ", poste='".$this->db->escape($this->poste)."'";
657 $sql .= ", fax='".$this->db->escape($this->fax)."'";
658 $sql .= ", email='".$this->db->escape($this->email)."'";
659 $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
660 $sql .= ", photo='".$this->db->escape($this->photo)."'";
661 $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
662 $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
663 $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
664 $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
665 $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
666 $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
667 $sql .= ", priv = '".$this->db->escape($this->priv)."'";
668 $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
669 if (isset($this->stcomm_id)) {
670 $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
671 }
672 $sql .= ", statut = ".((int) $this->statut);
673 $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
674 $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
675 $sql .= ", entity = ".((int) $this->entity);
676 $sql .= " WHERE rowid = ".((int) $id);
677
678 dol_syslog(get_class($this)."::update", LOG_DEBUG);
679 $result = $this->db->query($sql);
680 if ($result) {
681 unset($this->country_code);
682 unset($this->country);
683 unset($this->state_code);
684 unset($this->state);
685
686 $action = 'update';
687
688 // Actions on extra fields
689 if (!$error) {
690 $result = $this->insertExtraFields();
691 if ($result < 0) {
692 $error++;
693 }
694 }
695
696 if (!$error) {
697 $result = $this->updateRoles();
698 if ($result < 0) {
699 $error++;
700 }
701 }
702
703 if (!$error && $this->user_id > 0) {
704 // If contact is linked to a user
705 $tmpobj = new User($this->db);
706 $tmpobj->fetch($this->user_id);
707 $usermustbemodified = 0;
708 if ($tmpobj->office_phone != $this->phone_pro) {
709 $tmpobj->office_phone = $this->phone_pro;
710 $usermustbemodified++;
711 }
712 if ($tmpobj->office_fax != $this->fax) {
713 $tmpobj->office_fax = $this->fax;
714 $usermustbemodified++;
715 }
716 if ($tmpobj->address != $this->address) {
717 $tmpobj->address = $this->address;
718 $usermustbemodified++;
719 }
720 if ($tmpobj->town != $this->town) {
721 $tmpobj->town = $this->town;
722 $usermustbemodified++;
723 }
724 if ($tmpobj->zip != $this->zip) {
725 $tmpobj->zip = $this->zip;
726 $usermustbemodified++;
727 }
728 if ($tmpobj->zip != $this->zip) {
729 $tmpobj->state_id = $this->state_id;
730 $usermustbemodified++;
731 }
732 if ($tmpobj->country_id != $this->country_id) {
733 $tmpobj->country_id = $this->country_id;
734 $usermustbemodified++;
735 }
736 if ($tmpobj->email != $this->email) {
737 $tmpobj->email = $this->email;
738 $usermustbemodified++;
739 }
740 if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
741 $tmpobj->socialnetworks = $this->socialnetworks;
742 $usermustbemodified++;
743 }
744 if ($usermustbemodified) {
745 $result = $tmpobj->update($user, 0, 1, 1, 1);
746 if ($result < 0) {
747 $error++;
748 }
749 }
750 }
751
752 if (!$error && !$notrigger) {
753 // Call trigger
754 $result = $this->call_trigger('CONTACT_MODIFY', $user);
755 if ($result < 0) {
756 $error++;
757 }
758 // End call triggers
759 }
760
761 if (!$error) {
762 $this->db->commit();
763 return 1;
764 } else {
765 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
766 $this->db->rollback();
767 return -$error;
768 }
769 } else {
770 $this->error = $this->db->lasterror().' sql='.$sql;
771 $this->db->rollback();
772 return -1;
773 }
774 }
775
776
777 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
778 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
788 public function _load_ldap_dn($info, $mode = 0)
789 {
790 // phpcs:enable
791 global $conf;
792 $dn = '';
793 if ($mode == 0) {
794 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
795 } elseif ($mode == 1) {
796 $dn = getDolGlobalString('LDAP_CONTACT_DN');
797 } elseif ($mode == 2) {
798 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
799 }
800 return $dn;
801 }
802
803
804 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
805 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
811 public function _load_ldap_info()
812 {
813 // phpcs:enable
814 global $conf, $langs;
815
816 $info = array();
817
818 // Object classes
819 $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
820
821 $this->fullname = $this->getFullName($langs);
822
823 // Fields
824 if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
825 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
826 }
827 if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
828 $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
829 }
830 if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
831 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
832 }
833
834 if ($this->poste) {
835 $info["title"] = $this->poste;
836 }
837 if ($this->socid > 0) {
838 $soc = new Societe($this->db);
839 $soc->fetch($this->socid);
840
841 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
842 if ($soc->client == 1) {
843 $info["businessCategory"] = "Customers";
844 }
845 if ($soc->client == 2) {
846 $info["businessCategory"] = "Prospects";
847 }
848 if ($soc->fournisseur == 1) {
849 $info["businessCategory"] = "Suppliers";
850 }
851 }
852 if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
853 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
854 }
855 if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
856 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
857 }
858 if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
859 $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
860 }
861 if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
862 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
863 }
864 if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
865 $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
866 }
867 if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
868 $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
869 }
870 if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
871 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
872 }
873 if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
874 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
875 }
876 if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
877 $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
878 }
879 if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
880 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
881 }
882
883 if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
884 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
885
886 $info['uidnumber'] = $this->id;
887
888 $info['phpgwTz'] = 0;
889 $info['phpgwMailType'] = 'INTERNET';
890 $info['phpgwMailHomeType'] = 'INTERNET';
891
892 $info["phpgwContactTypeId"] = 'n';
893 $info["phpgwContactCatId"] = 0;
894 $info["phpgwContactAccess"] = "public";
895
896 /*
897 if (dol_strlen($this->egroupware_id) == 0) {
898 $this->egroupware_id = 1;
899 }
900 $info["phpgwContactOwner"] = $this->egroupware_id;
901 */
902 $info["phpgwContactOwner"] = 1;
903
904 if ($this->email) {
905 $info["rfc822Mailbox"] = $this->email;
906 }
907 if ($this->phone_mobile) {
908 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
909 }
910 }
911
912 return $info;
913 }
914
915
916 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
925 public function update_perso($id, $user = null, $notrigger = 0)
926 {
927 // phpcs:enable
928 $error = 0;
929 $result = false;
930
931 $this->db->begin();
932
933 // Update the contact
934 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
935 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
936 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
937 if ($user) {
938 $sql .= ", fk_user_modif = ".((int) $user->id);
939 }
940 $sql .= " WHERE rowid = ".((int) $id);
941
942 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
943 $resql = $this->db->query($sql);
944 if (!$resql) {
945 $error++;
946 $this->error = $this->db->lasterror();
947 }
948
949 if ($user) {
950 // Update birthday alert
951 if (!empty($this->birthday_alert)) {
952 //check existing
953 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
954 $result_check = $this->db->query($sql_check);
955 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
956 //insert
957 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
958 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
959 $result = $this->db->query($sql);
960 if (!$result) {
961 $error++;
962 $this->error = $this->db->lasterror();
963 }
964 } else {
965 $result = true;
966 }
967 } else {
968 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
969 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
970 $result = $this->db->query($sql);
971 if (!$result) {
972 $error++;
973 $this->error = $this->db->lasterror();
974 }
975 }
976 }
977
978 if (!$error && !$notrigger) {
979 // Call trigger
980 $result = $this->call_trigger('CONTACT_MODIFY', $user);
981 if ($result < 0) {
982 $error++;
983 }
984 // End call triggers
985 }
986
987 if (!$error) {
988 $this->db->commit();
989 return 1;
990 } else {
991 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
992 $this->db->rollback();
993 return -$error;
994 }
995 }
996
997
1009 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
1010 {
1011 global $langs;
1012
1013 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
1014
1015 if (empty($id) && empty($ref_ext) && empty($email)) {
1016 $this->error = 'BadParameter';
1017 return -1;
1018 }
1019
1020 $langs->loadLangs(array("dict", "companies"));
1021
1022 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1023 $sql .= " c.address, c.statut, c.zip, c.town,";
1024 $sql .= " c.fk_pays as country_id,";
1025 $sql .= " c.fk_departement as state_id,";
1026 $sql .= " c.birthday,";
1027 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1028 $sql .= " c.socialnetworks,";
1029 $sql .= " c.photo,";
1030 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1031 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1032 $sql .= " c.import_key,";
1033 $sql .= " c.datec as date_creation, c.tms as date_modification, c.fk_user_creat, c.fk_user_modif,";
1034 $sql .= " co.label as country, co.code as country_code,";
1035 $sql .= " d.nom as state, d.code_departement as state_code,";
1036 $sql .= " u.rowid as user_id, u.login as user_login,";
1037 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1038 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1039 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1040 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1041 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1042 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1043 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1044 if ($id) {
1045 $sql .= " WHERE c.rowid = ".((int) $id);
1046 } else {
1047 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1048 if ($ref_ext) {
1049 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1050 }
1051 if ($email) {
1052 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1053 }
1054 if ($socid) {
1055 $sql .= " AND c.fk_soc = ".((int) $socid);
1056 }
1057 }
1058
1059 $resql = $this->db->query($sql);
1060 if ($resql) {
1061 $num = $this->db->num_rows($resql);
1062 if ($num > 1) {
1063 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1064 dol_syslog($this->error, LOG_ERR);
1065
1066 return 2;
1067 } elseif ($num) { // $num = 1
1068 $obj = $this->db->fetch_object($resql);
1069
1070 $this->id = $obj->rowid;
1071 $this->entity = $obj->entity;
1072 $this->ref = $obj->rowid;
1073 $this->ref_ext = $obj->ref_ext;
1074
1075 $this->civility_code = $obj->civility_code;
1076 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1077
1078 $this->name_alias = $obj->name_alias;
1079 $this->lastname = $obj->lastname;
1080 $this->firstname = $obj->firstname;
1081 $this->address = $obj->address;
1082 $this->zip = $obj->zip;
1083 $this->town = $obj->town;
1084
1085 $this->date_creation = $this->db->jdate($obj->date_creation);
1086 $this->date_modification = $this->db->jdate($obj->date_modification);
1087 $this->user_creation_id = $obj->fk_user_creat;
1088 $this->user_modification_id = $obj->fk_user_modif;
1089
1090 $this->state_id = $obj->state_id;
1091 $this->state_code = $obj->state_code;
1092 $this->state = $obj->state;
1093
1094 $this->country_id = $obj->country_id;
1095 $this->country_code = $obj->country_id ? $obj->country_code : '';
1096 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1097
1098 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1099 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1100 $this->socname = $obj->socname;
1101 $this->poste = $obj->poste;
1102 $this->statut = $obj->statut;
1103
1104 $this->fk_prospectlevel = $obj->fk_prospectlevel;
1105
1106 $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1107 $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1108 $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1109 $this->statut_commercial = $libelle; // libelle statut commercial
1110 $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1111
1112 $this->phone_pro = trim($obj->phone);
1113 $this->fax = trim($obj->fax);
1114 $this->phone_perso = trim($obj->phone_perso);
1115 $this->phone_mobile = trim($obj->phone_mobile);
1116
1117 $this->email = $obj->email;
1118 $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1119 $this->photo = $obj->photo;
1120 $this->priv = $obj->priv;
1121 $this->mail = $obj->email;
1122
1123 $this->birthday = $this->db->jdate($obj->birthday);
1124 $this->note = $obj->note_private; // deprecated
1125 $this->note_private = $obj->note_private;
1126 $this->note_public = $obj->note_public;
1127 $this->default_lang = $obj->default_lang;
1128 $this->user_id = $obj->user_id;
1129 $this->user_login = $obj->user_login;
1130 $this->canvas = $obj->canvas;
1131
1132 $this->import_key = $obj->import_key;
1133
1134 // Define gender according to civility
1135 $this->setGenderFromCivility();
1136
1137 // Search Dolibarr user linked to this contact
1138 $sql = "SELECT u.rowid ";
1139 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1140 $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1141
1142 $resql = $this->db->query($sql);
1143 if ($resql) {
1144 if ($this->db->num_rows($resql)) {
1145 $uobj = $this->db->fetch_object($resql);
1146
1147 $this->user_id = $uobj->rowid;
1148 }
1149 $this->db->free($resql);
1150 } else {
1151 $this->error = $this->db->error();
1152 return -1;
1153 }
1154
1155 // Retrieve all extrafield
1156 // fetch optionals attributes and labels
1157 $this->fetch_optionals();
1158
1159 // Load also alerts of this user
1160 if ($user) {
1161 $sql = "SELECT fk_user";
1162 $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1163 $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1164
1165 $resql = $this->db->query($sql);
1166 if ($resql) {
1167 if ($this->db->num_rows($resql)) {
1168 $obj = $this->db->fetch_object($resql);
1169
1170 $this->birthday_alert = 1;
1171 }
1172 $this->db->free($resql);
1173 } else {
1174 $this->error = $this->db->error();
1175 return -1;
1176 }
1177 }
1178
1179 // Load also roles of this address
1180 if ($loadalsoroles) {
1181 $resultRole = $this->fetchRoles();
1182 if ($resultRole < 0) {
1183 return $resultRole;
1184 }
1185 }
1186
1187 return 1;
1188 } else {
1189 $this->error = $langs->trans("RecordNotFound");
1190 return 0;
1191 }
1192 } else {
1193 $this->error = $this->db->error();
1194 return -1;
1195 }
1196 }
1197
1211 public function findNearest($id = 0, $lastname = '', $firstname = '', $ref_ext = '', $email = '', $ref_alias = '', $socid = 0)
1212 {
1213 // A rowid is known, it is a unique key so we found it
1214 if ($id) {
1215 return $id;
1216 }
1217
1218 // We try to find the contact with exact matching on all fields
1219 // TODO Replace this with step by step search
1220 // Then search on email
1221 // Then search on lastname + firstname
1222 // Then search ref_ext or alias with a OR
1223 $tmpcontact = new Contact($this->db);
1224 $result = $tmpcontact->fetch($id, null, $ref_ext, $email, 0, $socid);
1225
1226 return $result;
1227 }
1228
1229
1230
1237 public function setGenderFromCivility()
1238 {
1239 unset($this->gender);
1240
1241 if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1242 $this->gender = 'man';
1243 } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1244 $this->gender = 'woman';
1245 }
1246 }
1247
1248 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1258 public function load_ref_elements()
1259 {
1260 // phpcs:enable
1261 // Count the elements for which it is contact
1262 $sql = "SELECT tc.element, count(ec.rowid) as nb";
1263 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1264 $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1265 $sql .= " AND fk_socpeople = ".((int) $this->id);
1266 $sql .= " AND tc.source = 'external'";
1267 $sql .= " GROUP BY tc.element";
1268
1269 dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1270
1271 $resql = $this->db->query($sql);
1272 if ($resql) {
1273 while ($obj = $this->db->fetch_object($resql)) {
1274 if ($obj->nb) {
1275 if ($obj->element == 'facture') {
1276 $this->ref_facturation = $obj->nb;
1277 } elseif ($obj->element == 'contrat') {
1278 $this->ref_contrat = $obj->nb;
1279 } elseif ($obj->element == 'commande') {
1280 $this->ref_commande = $obj->nb;
1281 } elseif ($obj->element == 'propal') {
1282 $this->ref_propal = $obj->nb;
1283 }
1284 }
1285 }
1286 $this->db->free($resql);
1287 return 0;
1288 } else {
1289 $this->error = $this->db->lasterror();
1290 return -1;
1291 }
1292 }
1293
1301 public function delete($user, $notrigger = 0)
1302 {
1303 $error = 0;
1304
1305 $this->db->begin();
1306
1307 if (!$error && !$notrigger) {
1308 // Call trigger
1309 $result = $this->call_trigger('CONTACT_DELETE', $user);
1310 if ($result < 0) {
1311 $error++;
1312 }
1313 // End call triggers
1314 }
1315
1316 if (!$error) {
1317 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1318 $sql = "SELECT ec.rowid";
1319 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1320 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1321 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1322 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1323 $sql .= " AND tc.source='external'";
1324 dol_syslog(__METHOD__, LOG_DEBUG);
1325 $resql = $this->db->query($sql);
1326 if ($resql) {
1327 $num = $this->db->num_rows($resql);
1328
1329 $i = 0;
1330 while ($i < $num && !$error) {
1331 $obj = $this->db->fetch_object($resql);
1332
1333 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1334 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1335 dol_syslog(__METHOD__, LOG_DEBUG);
1336 $result = $this->db->query($sqldel);
1337 if (!$result) {
1338 $error++;
1339 $this->error = $this->db->error().' sql='.$sqldel;
1340 }
1341
1342 $i++;
1343 }
1344 } else {
1345 $error++;
1346 $this->error = $this->db->error().' sql='.$sql;
1347 }
1348 }
1349
1350 if (!$error) {
1351 // Remove Roles
1352 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1353 dol_syslog(__METHOD__, LOG_DEBUG);
1354 $resql = $this->db->query($sql);
1355 if (!$resql) {
1356 $error++;
1357 $this->error .= $this->db->lasterror();
1358 $errorflag = -1;
1359 }
1360 }
1361
1362 if (!$error) {
1363 // Remove Notifications
1364 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1365 dol_syslog(__METHOD__, LOG_DEBUG);
1366 $resql = $this->db->query($sql);
1367 if (!$resql) {
1368 $error++;
1369 $this->error .= $this->db->lasterror();
1370 $errorflag = -1;
1371 }
1372 }
1373
1374 if (!$error) {
1375 // Remove category
1376 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1377 dol_syslog(__METHOD__, LOG_DEBUG);
1378 $resql = $this->db->query($sql);
1379 if (!$resql) {
1380 $error++;
1381 $this->error .= $this->db->lasterror();
1382 $errorflag = -1;
1383 }
1384 }
1385
1386 if (!$error) {
1387 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1388 $sql .= " WHERE rowid = ".((int) $this->id);
1389 dol_syslog(__METHOD__, LOG_DEBUG);
1390 $result = $this->db->query($sql);
1391 if (!$result) {
1392 $error++;
1393 $this->error = $this->db->error().' sql='.$sql;
1394 }
1395 }
1396
1397 // Remove extrafields
1398 if (!$error) {
1399 // For avoid conflicts if trigger used
1400 $result = $this->deleteExtraFields();
1401 if ($result < 0) {
1402 $error++;
1403 }
1404 }
1405
1406 if (!$error) {
1407 $this->db->commit();
1408 return 1;
1409 } else {
1410 $this->db->rollback();
1411 dol_syslog("Error ".$this->error, LOG_ERR);
1412 return -1;
1413 }
1414 }
1415
1416
1423 public function info($id)
1424 {
1425 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1426 $sql .= " c.tms as tms, c.fk_user_modif";
1427 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1428 $sql .= " WHERE c.rowid = ".((int) $id);
1429
1430 $resql = $this->db->query($sql);
1431 if ($resql) {
1432 if ($this->db->num_rows($resql)) {
1433 $obj = $this->db->fetch_object($resql);
1434
1435 $this->id = $obj->rowid;
1436
1437 $this->user_creation_id = $obj->fk_user_creat;
1438 $this->user_modification_id = $obj->fk_user_modif;
1439 $this->date_creation = $this->db->jdate($obj->datec);
1440 $this->date_modification = $this->db->jdate($obj->tms);
1441 }
1442
1443 $this->db->free($resql);
1444 } else {
1445 print $this->db->error();
1446 }
1447 }
1448
1454 public function getNbOfEMailings()
1455 {
1456 $sql = "SELECT count(mc.email) as nb";
1457 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1458 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1459 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1460
1461 $resql = $this->db->query($sql);
1462 if ($resql) {
1463 $obj = $this->db->fetch_object($resql);
1464 $nb = $obj->nb;
1465
1466 $this->db->free($resql);
1467 return $nb;
1468 } else {
1469 $this->error = $this->db->error();
1470 return -1;
1471 }
1472 }
1473
1480 public function getTooltipContentArray($params)
1481 {
1482 global $conf, $langs, $user;
1483
1484 $datas = [];
1485
1486 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1487 return ['optimize' => $langs->trans("ShowContact")];
1488 }
1489 if (!empty($this->photo) && class_exists('Form')) {
1490 $photo = '<div class="photointooltip floatright">';
1491 $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.
1492 $photo .= '</div>';
1493 $datas['photo'] = $photo;
1494 }
1495
1496 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1497 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1498 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1499 if (!empty($this->poste)) {
1500 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1501 }
1502 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1503 $phonelist = array();
1504 $country_code = empty($this->country_code) ? '' : $this->country_code;
1505 if ($this->phone_pro) {
1506 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1507 }
1508 if ($this->phone_mobile) {
1509 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1510 }
1511 if ($this->phone_perso) {
1512 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1513 }
1514 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1515 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1516
1517 return $datas;
1518 }
1519
1533 public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1534 {
1535 global $conf, $langs, $hookmanager;
1536
1537 if (!empty($conf->dol_no_mouse_hover)) {
1538 $notooltip = 1; // Force disable tooltips
1539 }
1540
1541 $result = '';
1542 $params = [
1543 'id' => $this->id,
1544 'objecttype' => $this->element,
1545 'option' => $option,
1546 ];
1547 $classfortooltip = 'classfortooltip';
1548 $dataparams = '';
1549 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1550 $classfortooltip = 'classforajaxtooltip';
1551 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1552 $label = '';
1553 } else {
1554 $label = implode($this->getTooltipContentArray($params));
1555 }
1556
1557 $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1558
1559 if ($option !== 'nolink') {
1560 // Add param to save lastsearch_values or not
1561 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1562 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1563 $add_save_lastsearch_values = 1;
1564 }
1565 if ($url && $add_save_lastsearch_values) {
1566 $url .= '&save_lastsearch_values=1';
1567 }
1568 }
1569
1570 $url .= $moreparam;
1571
1572 $linkclose = '';
1573 if (empty($notooltip)) {
1574 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1575 $label = $langs->trans("ShowContact");
1576 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1577 }
1578 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1579 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1580 } else {
1581 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1582 }
1583
1584 if ($option == 'nolink' || empty($url)) {
1585 $linkstart = '<span';
1586 } else {
1587 $linkstart = '<a href="'.$url.'"';
1588 }
1589 $linkstart .= $linkclose.'>';
1590 if ($option == 'nolink' || empty($url)) {
1591 $linkend = '</span>';
1592 } else {
1593 $linkend = '</a>';
1594 }
1595
1596 $result .= $linkstart;
1597
1598 if ($withpicto) {
1599 if ($withpicto < 0) {
1600 $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>';
1601 if ($withpicto != 2 && $withpicto != -2) {
1602 $result .= ' ';
1603 }
1604 } else {
1605 $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);
1606 }
1607 }
1608 if ($withpicto != 2 && $withpicto != -2) {
1609 $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1610 }
1611
1612 $result .= $linkend;
1613
1614 global $action;
1615 $hookmanager->initHooks(array('contactdao'));
1616 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1617 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1618 if ($reshook > 0) {
1619 $result = $hookmanager->resPrint;
1620 } else {
1621 $result .= $hookmanager->resPrint;
1622 }
1623
1624 return $result;
1625 }
1626
1632 public function getCivilityLabel()
1633 {
1634 global $langs;
1635
1636 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1637 if (empty($code)) {
1638 return '';
1639 }
1640
1641 $langs->load("dict");
1642 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1643 }
1644
1651 public function getLibStatut($mode)
1652 {
1653 return $this->LibStatut($this->statut, $mode);
1654 }
1655
1656 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1664 public function LibStatut($status, $mode)
1665 {
1666 // phpcs:enable
1667 global $langs;
1668
1669 $labelStatus = array(
1670 0 => 'ActivityCeased',
1671 1 => 'InActivity',
1672 4 => 'InActivity',
1673 5 => 'ActivityCeased',
1674 );
1675 $labelStatusShort = array(
1676 0 => 'ActivityCeased',
1677 1 => 'InActivity',
1678 4 => 'InActivity',
1679 5 => 'ActivityCeased',
1680 );
1681
1682 $statusType = 'status4';
1683 if ($status == 0 || $status == 5) {
1684 $statusType = 'status5';
1685 }
1686
1687 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1688 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1689
1690 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1691 }
1692
1693
1694 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1701 public function LibPubPriv($status)
1702 {
1703 // phpcs:enable
1704 global $langs;
1705 if ($status == '1') {
1706 return $langs->trans('ContactPrivate');
1707 } else {
1708 return $langs->trans('ContactPublic');
1709 }
1710 }
1711
1712
1720 public function initAsSpecimen()
1721 {
1722 // Get first id of existing company and save it into $socid
1723 $socid = 0;
1724 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1725 $resql = $this->db->query($sql);
1726 if ($resql) {
1727 $obj = $this->db->fetch_object($resql);
1728 if ($obj) {
1729 $socid = $obj->rowid;
1730 }
1731 }
1732
1733 // Initialise parameters
1734 $this->id = 0;
1735 $this->entity = 1;
1736 $this->specimen = 1;
1737 $this->lastname = 'DOLIBARR';
1738 $this->firstname = 'SPECIMEN';
1739 $this->address = '21 jump street';
1740 $this->zip = '99999';
1741 $this->town = 'MyTown';
1742 $this->country_id = 1;
1743 $this->country_code = 'FR';
1744 $this->country = 'France';
1745 $this->email = 'specimen@specimen.com';
1746 $this->socialnetworks = array(
1747 'skype' => 'tom.hanson',
1748 'twitter' => 'tomhanson',
1749 'linkedin' => 'tomhanson',
1750 );
1751 $this->phone_pro = '0909090901';
1752 $this->phone_perso = '0909090902';
1753 $this->phone_mobile = '0909090903';
1754 $this->fax = '0909090909';
1755
1756 $this->note_public = 'This is a comment (public)';
1757 $this->note_private = 'This is a comment (private)';
1758
1759 $this->socid = $socid;
1760 $this->statut = 1;
1761
1762 return 1;
1763 }
1764
1771 public function setstatus($status)
1772 {
1773 global $conf, $langs, $user;
1774
1775 $error = 0;
1776
1777 // Check parameters
1778 if ($this->statut == $status) {
1779 return 0;
1780 } else {
1781 $this->statut = $status;
1782 }
1783
1784 $this->db->begin();
1785
1786 // User disable
1787 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1788 $sql .= " SET statut = ".((int) $this->statut);
1789 $sql .= ", fk_user_modif = ".((int) $user->id);
1790 $sql .= " WHERE rowid = ".((int) $this->id);
1791 $result = $this->db->query($sql);
1792
1793 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1794 if ($result) {
1795 // Call trigger
1796 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1797 if ($result < 0) {
1798 $error++;
1799 }
1800 // End call triggers
1801 }
1802
1803 if ($error) {
1804 $this->db->rollback();
1805 return -$error;
1806 } else {
1807 $this->db->commit();
1808 return 1;
1809 }
1810 }
1811
1822 public function setCategories($categories)
1823 {
1824 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1825 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1826 }
1827
1836 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1837 {
1838 $tables = array(
1839 'socpeople', 'societe_contacts'
1840 );
1841
1842 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1843 }
1844
1852 public function fetchRoles()
1853 {
1854 global $langs;
1855 $error = 0;
1856 $num = 0;
1857
1858 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1859 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1860 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1861 $sql .= " AND tc.source = 'external' AND tc.active=1";
1862 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1863 $sql .= " AND sc.entity IN (".getEntity('societe').')';
1864
1865 $resql = $this->db->query($sql);
1866 if ($resql) {
1867 $this->roles = array();
1868
1869 $num = $this->db->num_rows($resql);
1870 if ($num > 0) {
1871 while ($obj = $this->db->fetch_object($resql)) {
1872 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1873 $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1874 $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));
1875 }
1876 }
1877 } else {
1878 $error++;
1879 $this->error = $this->db->lasterror();
1880 $this->errors[] = $this->db->lasterror();
1881 }
1882
1883 if (empty($error)) {
1884 return $num;
1885 } else {
1886 return $error * -1;
1887 }
1888 }
1889
1897 public function getContactRoles($element = '')
1898 {
1899 $tab = array();
1900
1901 if ($element == 'action') {
1902 $element = 'agenda';
1903 }
1904
1905 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1906 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1907 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1908 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1909 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1910 $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1911 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1912 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1913 $sql .= " AND sp.entity IN (".getEntity('contact').")";
1914 $sql .= " AND tc.active = 1";
1915
1916 dol_syslog(__METHOD__, LOG_DEBUG);
1917 $resql = $this->db->query($sql);
1918 if ($resql) {
1919 $num = $this->db->num_rows($resql);
1920 $i = 0;
1921 while ($i < $num) {
1922 $obj = $this->db->fetch_object($resql);
1923 $tab[] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1924
1925 $i++;
1926 }
1927
1928 return $tab;
1929 } else {
1930 $this->error = $this->db->error();
1931 dol_print_error($this->db);
1932 return -1;
1933 }
1934 }
1935
1943 public function updateRoles()
1944 {
1945 global $conf;
1946
1947 $error = 0;
1948
1949 if (!isset($this->roles)) {
1950 return 0; // Avoid to loose roles when property not set
1951 }
1952
1953 $this->db->begin();
1954
1955 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1956
1957 $result = $this->db->query($sql);
1958 if (!$result) {
1959 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1960 $error++;
1961 } else {
1962 if (count($this->roles) > 0) {
1963 foreach ($this->roles as $keyRoles => $valRoles) {
1964 if (empty($valRoles)) {
1965 continue;
1966 }
1967 $idrole = 0;
1968 if (is_array($valRoles)) {
1969 $idrole = $valRoles['id'];
1970 } else {
1971 $idrole = $valRoles;
1972 }
1973
1974 $socid = 0;
1975 if (is_array($valRoles)) {
1976 $socid = $valRoles['socid'];
1977 } else {
1978 $socid = $this->socid;
1979 }
1980
1981 if ($socid > 0) {
1982 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1983 $sql .= " (entity,";
1984 $sql .= "date_creation,";
1985 $sql .= "fk_soc,";
1986 $sql .= "fk_c_type_contact,";
1987 $sql .= "fk_socpeople) ";
1988 $sql .= " VALUES (".$conf->entity.",";
1989 $sql .= "'".$this->db->idate(dol_now())."',";
1990 $sql .= $socid.", ";
1991 $sql .= $idrole." , ";
1992 $sql .= $this->id;
1993 $sql .= ")";
1994
1995 $result = $this->db->query($sql);
1996 if (!$result) {
1997 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1998 $error++;
1999 }
2000 }
2001 }
2002 }
2003 }
2004 if (empty($error)) {
2005 $this->db->commit();
2006 return 1;
2007 } else {
2008 $this->error = implode(' ', $this->errors);
2009 $this->db->rollback();
2010 return $error * -1;
2011 }
2012 }
2013
2020 public function loadCacheOfProspStatus($active = 1)
2021 {
2022 global $langs;
2023
2024 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
2025 if ($active >= 0) {
2026 $sql .= " WHERE active = ".((int) $active);
2027 }
2028 $resql = $this->db->query($sql);
2029 $num = $this->db->num_rows($resql);
2030 $i = 0;
2031 while ($i < $num) {
2032 $obj = $this->db->fetch_object($resql);
2033 $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);
2034 $i++;
2035 }
2036 return 1;
2037 }
2038
2044 public function getLibProspLevel()
2045 {
2046 return $this->libProspLevel($this->fk_prospectlevel);
2047 }
2048
2055 public function libProspLevel($fk_prospectlevel)
2056 {
2057 global $langs;
2058
2059 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2060 // If lib not found in language file, we get label from cache/database
2061 if ($lib == "ProspectLevel".$fk_prospectlevel) {
2062 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2063 }
2064 return $lib;
2065 }
2066
2067
2075 public function setProspectLevel(User $user)
2076 {
2077 return $this->update($this->id, $user);
2078 }
2079
2087 public function getLibProspCommStatut($mode = 0, $label = '')
2088 {
2089 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2090 }
2091
2105 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2106 {
2107 global $langs;
2108 $langs->load('customers');
2109
2110 if ($mode == 2) {
2111 if ($statut == '-1' || $statut == 'ST_NO') {
2112 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2113 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2114 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2115 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2116 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2117 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2118 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2119 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2120 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2121 } else {
2122 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);
2123 }
2124 }
2125 if ($mode == 3) {
2126 if ($statut == '-1' || $statut == 'ST_NO') {
2127 return img_action($langs->trans("StatusProspect-1"), '-1', $picto);
2128 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2129 return img_action($langs->trans("StatusProspect0"), '0', $picto);
2130 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2131 return img_action($langs->trans("StatusProspect1"), '1', $picto);
2132 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2133 return img_action($langs->trans("StatusProspect2"), '2', $picto);
2134 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2135 return img_action($langs->trans("StatusProspect3"), '3', $picto);
2136 } else {
2137 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, '0', $picto);
2138 }
2139 }
2140 if ($mode == 4) {
2141 if ($statut == '-1' || $statut == 'ST_NO') {
2142 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2143 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2144 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2145 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2146 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2147 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2148 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2149 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2150 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2151 } else {
2152 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);
2153 }
2154 }
2155
2156 return "Error, mode/status not found";
2157 }
2158
2159
2166 public function setNoEmail($no_email)
2167 {
2168 $error = 0;
2169
2170 // Update mass emailing flag into table mailing_unsubscribe
2171 if ($this->email) {
2172 $this->db->begin();
2173
2174 if ($no_email) {
2175 $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)."'";
2176 $resql = $this->db->query($sql);
2177 if ($resql) {
2178 $obj = $this->db->fetch_object($resql);
2179 $noemail = $obj->nb;
2180 if (empty($noemail)) {
2181 $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())."')";
2182 $resql = $this->db->query($sql);
2183 if (!$resql) {
2184 $error++;
2185 $this->error = $this->db->lasterror();
2186 $this->errors[] = $this->error;
2187 }
2188 }
2189 } else {
2190 $error++;
2191 $this->error = $this->db->lasterror();
2192 $this->errors[] = $this->error;
2193 }
2194 } else {
2195 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2196 $resql = $this->db->query($sql);
2197 if (!$resql) {
2198 $error++;
2199 $this->error = $this->db->lasterror();
2200 $this->errors[] = $this->error;
2201 }
2202 }
2203
2204 if (empty($error)) {
2205 $this->no_email = $no_email;
2206 $this->db->commit();
2207 return 1;
2208 } else {
2209 $this->db->rollback();
2210 return $error * -1;
2211 }
2212 }
2213
2214 return 0;
2215 }
2216
2223 public function getNoEmail()
2224 {
2225 if ($this->email) {
2226 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2227 $resql = $this->db->query($sql);
2228 if ($resql) {
2229 $obj = $this->db->fetch_object($resql);
2230 $this->no_email = $obj->nb;
2231 return 1;
2232 } else {
2233 $this->error = $this->db->lasterror();
2234 $this->errors[] = $this->error;
2235 return -1;
2236 }
2237 }
2238 return 0;
2239 }
2240
2241
2249 public function getKanbanView($option = '', $arraydata = null)
2250 {
2251 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2252
2253 $return = '<div class="box-flex-item box-flex-grow-zero">';
2254 $return .= '<div class="info-box info-box-sm">';
2255 $return .= '<span class="info-box-icon bg-infobox-action">';
2256 //var_dump($this->photo);exit;
2257 if (property_exists($this, 'photo') && !is_null($this->photo)) {
2258 $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2259 } else {
2260 $return .= img_picto('', $this->picto);
2261 }
2262 $return .= '</span>';
2263 $return .= '<div class="info-box-content">';
2264 $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref).'</div>';
2265 if ($selected >= 0) {
2266 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2267 }
2268 if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2269 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2270 }
2271 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2272 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2273 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2274 }*/
2275 /*if (method_exists($this, 'LibPubPriv')) {
2276 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2277 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2278 }*/
2279 if (method_exists($this, 'getLibStatut')) {
2280 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2281 }
2282 $return .= '</div>';
2283 $return .= '</div>';
2284 $return .= '</div>';
2285 return $return;
2286 }
2287}
$object ref
Definition info.php:79
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.
getNomUrl($withpicto=0, $option='', $maxlen=0, $moreparam='', $save_lastsearch_value=-1, $notooltip=0, $morecss='')
Return name of contact with link (and eventually picto) Use $this->id, $this->lastname,...
getNoEmail()
get "blacklist" mailing status set no_email 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.
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.
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").
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
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.
dol_now($mode='auto')
Return date for now.
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.
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:140