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