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 $this->db->begin();
488
489 // Clean parameters
490 $this->name_alias = trim($this->name_alias);
491 $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name);
492 $this->firstname = trim($this->firstname);
493 $this->setUpperOrLowerCase();
494 if (empty($this->socid)) {
495 $this->socid = 0;
496 }
497 if (empty($this->priv)) {
498 $this->priv = 0;
499 }
500 if (empty($this->statut)) {
501 $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
502 }
503
504 // setEntity will set entity with the right value if empty or change it for the right value if multicompany module is active
505 $this->entity = setEntity($this);
506
507 $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
508 $sql .= " datec";
509 $sql .= ", fk_soc";
510 $sql .= ", name_alias";
511 $sql .= ", lastname";
512 $sql .= ", firstname";
513 $sql .= ", fk_user_creat";
514 $sql .= ", priv";
515 $sql .= ", fk_stcommcontact";
516 $sql .= ", statut";
517 $sql .= ", canvas";
518 $sql .= ", entity";
519 $sql .= ", ref_ext";
520 $sql .= ", import_key";
521 $sql .= ", ip";
522 $sql .= ") VALUES (";
523 $sql .= "'".$this->db->idate($now)."',";
524 if ($this->socid > 0) {
525 $sql .= " ".((int) $this->socid).",";
526 } else {
527 $sql .= "null,";
528 }
529 $sql .= "'".$this->db->escape($this->name_alias)."',";
530 $sql .= "'".$this->db->escape($this->lastname)."',";
531 $sql .= "'".$this->db->escape($this->firstname)."',";
532 $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
533 $sql .= " ".((int) $this->priv).",";
534 $sql .= " 0,";
535 $sql .= " ".((int) $this->statut).",";
536 $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
537 $sql .= " ".((int) $this->entity).",";
538 $sql .= "'".$this->db->escape($this->ref_ext)."',";
539 $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").",";
540 $sql .= " ".(!empty($this->ip) ? "'".$this->db->escape($this->ip)."'" : "null");
541 $sql .= ")";
542
543 dol_syslog(get_class($this)."::create", LOG_DEBUG);
544 $resql = $this->db->query($sql);
545 if ($resql) {
546 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
547
548 if (!$error) {
549 $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
550 if ($result < 0) {
551 $error++;
552 $this->error = $this->db->lasterror();
553 }
554 }
555
556 if (!$error) {
557 $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
558 if ($result < 0) {
559 $error++;
560 $this->error = $this->db->lasterror();
561 }
562 }
563
564 if (!$error && !$notrigger) {
565 // Call trigger
566 $result = $this->call_trigger('CONTACT_CREATE', $user);
567 if ($result < 0) {
568 $error++;
569 }
570 // End call triggers
571 }
572
573 if (!$error) {
574 $this->db->commit();
575 return $this->id;
576 } else {
577 $this->db->rollback();
578 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
579 return -2;
580 }
581 } else {
582 $this->error = $this->db->lasterror();
583
584 $this->db->rollback();
585 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
586 return -1;
587 }
588 }
589
600 public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
601 {
602 global $conf;
603
604 $error = 0;
605
606 $this->id = $id;
607
608 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
609
610 // Clean parameters
611 $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext));
612 $this->name_alias = trim($this->name_alias);
613 $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
614 $this->firstname = trim($this->firstname);
615 $this->email = trim($this->email);
616 $this->phone_pro = trim($this->phone_pro);
617 $this->phone_perso = trim($this->phone_perso);
618 $this->phone_mobile = trim($this->phone_mobile);
619 $this->photo = trim($this->photo);
620 $this->fax = trim($this->fax);
621 $this->zip = (empty($this->zip) ? '' : trim($this->zip));
622 $this->town = (empty($this->town) ? '' : trim($this->town));
623 $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
624 if (empty($this->statut)) {
625 $this->statut = 0;
626 }
627 if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
628 $this->civility_code = $this->civility_id; // For backward compatibility
629 }
630 $this->setUpperOrLowerCase();
631
632 $this->db->begin();
633
634 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
635 if ($this->socid > 0) {
636 $sql .= " fk_soc = ".((int) $this->socid).",";
637 } elseif ($this->socid == -1) {
638 $sql .= " fk_soc = NULL,";
639 }
640 $sql .= " civility='".$this->db->escape($this->civility_code)."'";
641 $sql .= ", name_alias='".$this->db->escape($this->name_alias)."'";
642 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
643 $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
644 $sql .= ", address='".$this->db->escape($this->address)."'";
645 $sql .= ", zip='".$this->db->escape($this->zip)."'";
646 $sql .= ", town='".$this->db->escape($this->town)."'";
647 $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
648 $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
649 $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
650 $sql .= ", poste='".$this->db->escape($this->poste)."'";
651 $sql .= ", fax='".$this->db->escape($this->fax)."'";
652 $sql .= ", email='".$this->db->escape($this->email)."'";
653 $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
654 $sql .= ", photo='".$this->db->escape($this->photo)."'";
655 $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
656 $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
657 $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
658 $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
659 $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
660 $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
661 $sql .= ", priv = '".$this->db->escape($this->priv)."'";
662 $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
663 if (isset($this->stcomm_id)) {
664 $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
665 }
666 $sql .= ", statut = ".((int) $this->statut);
667 $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
668 $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
669 $sql .= ", entity = ".((int) $this->entity);
670 $sql .= " WHERE rowid = ".((int) $id);
671
672 dol_syslog(get_class($this)."::update", LOG_DEBUG);
673 $result = $this->db->query($sql);
674 if ($result) {
675 unset($this->country_code);
676 unset($this->country);
677 unset($this->state_code);
678 unset($this->state);
679
680 $action = 'update';
681
682 // Actions on extra fields
683 if (!$error) {
684 $result = $this->insertExtraFields();
685 if ($result < 0) {
686 $error++;
687 }
688 }
689
690 if (!$error) {
691 $result = $this->updateRoles();
692 if ($result < 0) {
693 $error++;
694 }
695 }
696
697 if (!$error && $this->user_id > 0) {
698 // If contact is linked to a user
699 $tmpobj = new User($this->db);
700 $tmpobj->fetch($this->user_id);
701 $usermustbemodified = 0;
702 if ($tmpobj->office_phone != $this->phone_pro) {
703 $tmpobj->office_phone = $this->phone_pro;
704 $usermustbemodified++;
705 }
706 if ($tmpobj->office_fax != $this->fax) {
707 $tmpobj->office_fax = $this->fax;
708 $usermustbemodified++;
709 }
710 if ($tmpobj->address != $this->address) {
711 $tmpobj->address = $this->address;
712 $usermustbemodified++;
713 }
714 if ($tmpobj->town != $this->town) {
715 $tmpobj->town = $this->town;
716 $usermustbemodified++;
717 }
718 if ($tmpobj->zip != $this->zip) {
719 $tmpobj->zip = $this->zip;
720 $usermustbemodified++;
721 }
722 if ($tmpobj->zip != $this->zip) {
723 $tmpobj->state_id = $this->state_id;
724 $usermustbemodified++;
725 }
726 if ($tmpobj->country_id != $this->country_id) {
727 $tmpobj->country_id = $this->country_id;
728 $usermustbemodified++;
729 }
730 if ($tmpobj->email != $this->email) {
731 $tmpobj->email = $this->email;
732 $usermustbemodified++;
733 }
734 if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
735 $tmpobj->socialnetworks = $this->socialnetworks;
736 $usermustbemodified++;
737 }
738 if ($usermustbemodified) {
739 $result = $tmpobj->update($user, 0, 1, 1, 1);
740 if ($result < 0) {
741 $error++;
742 }
743 }
744 }
745
746 if (!$error && !$notrigger) {
747 // Call trigger
748 $result = $this->call_trigger('CONTACT_MODIFY', $user);
749 if ($result < 0) {
750 $error++;
751 }
752 // End call triggers
753 }
754
755 if (!$error) {
756 $this->db->commit();
757 return 1;
758 } else {
759 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
760 $this->db->rollback();
761 return -$error;
762 }
763 } else {
764 $this->error = $this->db->lasterror().' sql='.$sql;
765 $this->db->rollback();
766 return -1;
767 }
768 }
769
770
771 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
772 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
782 public function _load_ldap_dn($info, $mode = 0)
783 {
784 // phpcs:enable
785 global $conf;
786 $dn = '';
787 if ($mode == 0) {
788 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
789 } elseif ($mode == 1) {
790 $dn = getDolGlobalString('LDAP_CONTACT_DN');
791 } elseif ($mode == 2) {
792 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
793 }
794 return $dn;
795 }
796
797
798 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
799 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
805 public function _load_ldap_info()
806 {
807 // phpcs:enable
808 global $conf, $langs;
809
810 $info = array();
811
812 // Object classes
813 $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
814
815 $this->fullname = $this->getFullName($langs);
816
817 // Fields
818 if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
819 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
820 }
821 if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
822 $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
823 }
824 if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
825 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
826 }
827
828 if ($this->poste) {
829 $info["title"] = $this->poste;
830 }
831 if ($this->socid > 0) {
832 $soc = new Societe($this->db);
833 $soc->fetch($this->socid);
834
835 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
836 if ($soc->client == 1) {
837 $info["businessCategory"] = "Customers";
838 }
839 if ($soc->client == 2) {
840 $info["businessCategory"] = "Prospects";
841 }
842 if ($soc->fournisseur == 1) {
843 $info["businessCategory"] = "Suppliers";
844 }
845 }
846 if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
847 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
848 }
849 if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
850 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
851 }
852 if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
853 $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
854 }
855 if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
856 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
857 }
858 if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
859 $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
860 }
861 if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
862 $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
863 }
864 if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
865 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
866 }
867 if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
868 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
869 }
870 if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
871 $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
872 }
873 if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
874 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
875 }
876
877 if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
878 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
879
880 $info['uidnumber'] = $this->id;
881
882 $info['phpgwTz'] = 0;
883 $info['phpgwMailType'] = 'INTERNET';
884 $info['phpgwMailHomeType'] = 'INTERNET';
885
886 $info["phpgwContactTypeId"] = 'n';
887 $info["phpgwContactCatId"] = 0;
888 $info["phpgwContactAccess"] = "public";
889
890 /*
891 if (dol_strlen($this->egroupware_id) == 0) {
892 $this->egroupware_id = 1;
893 }
894 $info["phpgwContactOwner"] = $this->egroupware_id;
895 */
896 $info["phpgwContactOwner"] = 1;
897
898 if ($this->email) {
899 $info["rfc822Mailbox"] = $this->email;
900 }
901 if ($this->phone_mobile) {
902 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
903 }
904 }
905
906 return $info;
907 }
908
909
910 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
919 public function update_perso($id, $user = null, $notrigger = 0)
920 {
921 // phpcs:enable
922 $error = 0;
923 $result = false;
924
925 $this->db->begin();
926
927 // Update the contact
928 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
929 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
930 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
931 if ($user) {
932 $sql .= ", fk_user_modif = ".((int) $user->id);
933 }
934 $sql .= " WHERE rowid = ".((int) $id);
935
936 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
937 $resql = $this->db->query($sql);
938 if (!$resql) {
939 $error++;
940 $this->error = $this->db->lasterror();
941 }
942
943 if ($user) {
944 // Update birthday alert
945 if (!empty($this->birthday_alert)) {
946 //check existing
947 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
948 $result_check = $this->db->query($sql_check);
949 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
950 //insert
951 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
952 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
953 $result = $this->db->query($sql);
954 if (!$result) {
955 $error++;
956 $this->error = $this->db->lasterror();
957 }
958 } else {
959 $result = true;
960 }
961 } else {
962 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
963 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
964 $result = $this->db->query($sql);
965 if (!$result) {
966 $error++;
967 $this->error = $this->db->lasterror();
968 }
969 }
970 }
971
972 if (!$error && !$notrigger) {
973 // Call trigger
974 $result = $this->call_trigger('CONTACT_MODIFY', $user);
975 if ($result < 0) {
976 $error++;
977 }
978 // End call triggers
979 }
980
981 if (!$error) {
982 $this->db->commit();
983 return 1;
984 } else {
985 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
986 $this->db->rollback();
987 return -$error;
988 }
989 }
990
991
1003 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
1004 {
1005 global $langs;
1006
1007 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
1008
1009 if (empty($id) && empty($ref_ext) && empty($email)) {
1010 $this->error = 'BadParameter';
1011 return -1;
1012 }
1013
1014 $langs->loadLangs(array("dict", "companies"));
1015
1016 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1017 $sql .= " c.address, c.statut, c.zip, c.town,";
1018 $sql .= " c.fk_pays as country_id,";
1019 $sql .= " c.fk_departement as state_id,";
1020 $sql .= " c.birthday,";
1021 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1022 $sql .= " c.socialnetworks,";
1023 $sql .= " c.photo,";
1024 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1025 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1026 $sql .= " c.import_key,";
1027 $sql .= " c.datec as date_creation, c.tms as date_modification, c.fk_user_creat, c.fk_user_modif,";
1028 $sql .= " co.label as country, co.code as country_code,";
1029 $sql .= " d.nom as state, d.code_departement as state_code,";
1030 $sql .= " u.rowid as user_id, u.login as user_login,";
1031 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1032 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1033 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1034 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1035 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1036 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1037 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1038 if ($id) {
1039 $sql .= " WHERE c.rowid = ".((int) $id);
1040 } else {
1041 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1042 if ($ref_ext) {
1043 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1044 }
1045 if ($email) {
1046 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1047 }
1048 if ($socid) {
1049 $sql .= " AND c.fk_soc = ".((int) $socid);
1050 }
1051 }
1052
1053 $resql = $this->db->query($sql);
1054 if ($resql) {
1055 $num = $this->db->num_rows($resql);
1056 if ($num > 1) {
1057 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1058 dol_syslog($this->error, LOG_ERR);
1059
1060 return 2;
1061 } elseif ($num) { // $num = 1
1062 $obj = $this->db->fetch_object($resql);
1063
1064 $this->id = $obj->rowid;
1065 $this->entity = $obj->entity;
1066 $this->ref = $obj->rowid;
1067 $this->ref_ext = $obj->ref_ext;
1068
1069 $this->civility_code = $obj->civility_code;
1070 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1071
1072 $this->name_alias = $obj->name_alias;
1073 $this->lastname = $obj->lastname;
1074 $this->firstname = $obj->firstname;
1075 $this->address = $obj->address;
1076 $this->zip = $obj->zip;
1077 $this->town = $obj->town;
1078
1079 $this->date_creation = $this->db->jdate($obj->date_creation);
1080 $this->date_modification = $this->db->jdate($obj->date_modification);
1081 $this->user_creation_id = $obj->fk_user_creat;
1082 $this->user_modification_id = $obj->fk_user_modif;
1083
1084 $this->state_id = $obj->state_id;
1085 $this->state_code = $obj->state_code;
1086 $this->state = $obj->state;
1087
1088 $this->country_id = $obj->country_id;
1089 $this->country_code = $obj->country_id ? $obj->country_code : '';
1090 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1091
1092 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1093 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1094 $this->socname = $obj->socname;
1095 $this->poste = $obj->poste;
1096 $this->statut = $obj->statut;
1097
1098 $this->fk_prospectlevel = $obj->fk_prospectlevel;
1099
1100 $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1101 $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1102 $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1103 $this->statut_commercial = $libelle; // libelle statut commercial
1104 $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1105
1106 $this->phone_pro = trim($obj->phone);
1107 $this->fax = trim($obj->fax);
1108 $this->phone_perso = trim($obj->phone_perso);
1109 $this->phone_mobile = trim($obj->phone_mobile);
1110
1111 $this->email = $obj->email;
1112 $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1113 $this->photo = $obj->photo;
1114 $this->priv = $obj->priv;
1115 $this->mail = $obj->email;
1116
1117 $this->birthday = $this->db->jdate($obj->birthday);
1118 $this->note = $obj->note_private; // deprecated
1119 $this->note_private = $obj->note_private;
1120 $this->note_public = $obj->note_public;
1121 $this->default_lang = $obj->default_lang;
1122 $this->user_id = $obj->user_id;
1123 $this->user_login = $obj->user_login;
1124 $this->canvas = $obj->canvas;
1125
1126 $this->import_key = $obj->import_key;
1127
1128 // Define gender according to civility
1129 $this->setGenderFromCivility();
1130
1131 // Search Dolibarr user linked to this contact
1132 $sql = "SELECT u.rowid ";
1133 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1134 $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1135
1136 $resql = $this->db->query($sql);
1137 if ($resql) {
1138 if ($this->db->num_rows($resql)) {
1139 $uobj = $this->db->fetch_object($resql);
1140
1141 $this->user_id = $uobj->rowid;
1142 }
1143 $this->db->free($resql);
1144 } else {
1145 $this->error = $this->db->error();
1146 return -1;
1147 }
1148
1149 // Retrieve all extrafield
1150 // fetch optionals attributes and labels
1151 $this->fetch_optionals();
1152
1153 // Load also alerts of this user
1154 if ($user) {
1155 $sql = "SELECT fk_user";
1156 $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1157 $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1158
1159 $resql = $this->db->query($sql);
1160 if ($resql) {
1161 if ($this->db->num_rows($resql)) {
1162 $obj = $this->db->fetch_object($resql);
1163
1164 $this->birthday_alert = 1;
1165 }
1166 $this->db->free($resql);
1167 } else {
1168 $this->error = $this->db->error();
1169 return -1;
1170 }
1171 }
1172
1173 // Load also roles of this address
1174 if ($loadalsoroles) {
1175 $resultRole = $this->fetchRoles();
1176 if ($resultRole < 0) {
1177 return $resultRole;
1178 }
1179 }
1180
1181 return 1;
1182 } else {
1183 $this->error = $langs->trans("RecordNotFound");
1184 return 0;
1185 }
1186 } else {
1187 $this->error = $this->db->error();
1188 return -1;
1189 }
1190 }
1191
1205 public function findNearest($id = 0, $lastname = '', $firstname = '', $ref_ext = '', $email = '', $ref_alias = '', $socid = 0)
1206 {
1207 // A rowid is known, it is a unique key so we found it
1208 if ($id) {
1209 return $id;
1210 }
1211
1212 // We try to find the contact with exact matching on all fields
1213 // TODO Replace this with step by step search
1214 // Then search on email
1215 // Then search on lastname + firstname
1216 // Then search ref_ext or alias with a OR
1217 $tmpcontact = new Contact($this->db);
1218 $result = $tmpcontact->fetch($id, null, $ref_ext, $email, 0, $socid);
1219
1220 return $result;
1221 }
1222
1223
1224
1231 public function setGenderFromCivility()
1232 {
1233 unset($this->gender);
1234
1235 if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1236 $this->gender = 'man';
1237 } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1238 $this->gender = 'woman';
1239 }
1240 }
1241
1242 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1252 public function load_ref_elements()
1253 {
1254 // phpcs:enable
1255 // Count the elements for which it is contact
1256 $sql = "SELECT tc.element, count(ec.rowid) as nb";
1257 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1258 $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1259 $sql .= " AND fk_socpeople = ".((int) $this->id);
1260 $sql .= " AND tc.source = 'external'";
1261 $sql .= " GROUP BY tc.element";
1262
1263 dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1264
1265 $resql = $this->db->query($sql);
1266 if ($resql) {
1267 while ($obj = $this->db->fetch_object($resql)) {
1268 if ($obj->nb) {
1269 if ($obj->element == 'facture') {
1270 $this->ref_facturation = $obj->nb;
1271 } elseif ($obj->element == 'contrat') {
1272 $this->ref_contrat = $obj->nb;
1273 } elseif ($obj->element == 'commande') {
1274 $this->ref_commande = $obj->nb;
1275 } elseif ($obj->element == 'propal') {
1276 $this->ref_propal = $obj->nb;
1277 }
1278 }
1279 }
1280 $this->db->free($resql);
1281 return 0;
1282 } else {
1283 $this->error = $this->db->lasterror();
1284 return -1;
1285 }
1286 }
1287
1295 public function delete($user, $notrigger = 0)
1296 {
1297 $error = 0;
1298
1299 $this->db->begin();
1300
1301 if (!$error && !$notrigger) {
1302 // Call trigger
1303 $result = $this->call_trigger('CONTACT_DELETE', $user);
1304 if ($result < 0) {
1305 $error++;
1306 }
1307 // End call triggers
1308 }
1309
1310 if (!$error) {
1311 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1312 $sql = "SELECT ec.rowid";
1313 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1314 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1315 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1316 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1317 $sql .= " AND tc.source='external'";
1318 dol_syslog(__METHOD__, LOG_DEBUG);
1319 $resql = $this->db->query($sql);
1320 if ($resql) {
1321 $num = $this->db->num_rows($resql);
1322
1323 $i = 0;
1324 while ($i < $num && !$error) {
1325 $obj = $this->db->fetch_object($resql);
1326
1327 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1328 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1329 dol_syslog(__METHOD__, LOG_DEBUG);
1330 $result = $this->db->query($sqldel);
1331 if (!$result) {
1332 $error++;
1333 $this->error = $this->db->error().' sql='.$sqldel;
1334 }
1335
1336 $i++;
1337 }
1338 } else {
1339 $error++;
1340 $this->error = $this->db->error().' sql='.$sql;
1341 }
1342 }
1343
1344 if (!$error) {
1345 // Remove Roles
1346 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1347 dol_syslog(__METHOD__, LOG_DEBUG);
1348 $resql = $this->db->query($sql);
1349 if (!$resql) {
1350 $error++;
1351 $this->error .= $this->db->lasterror();
1352 $errorflag = -1;
1353 }
1354 }
1355
1356 if (!$error) {
1357 // Remove Notifications
1358 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1359 dol_syslog(__METHOD__, LOG_DEBUG);
1360 $resql = $this->db->query($sql);
1361 if (!$resql) {
1362 $error++;
1363 $this->error .= $this->db->lasterror();
1364 $errorflag = -1;
1365 }
1366 }
1367
1368 if (!$error) {
1369 // Remove category
1370 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1371 dol_syslog(__METHOD__, LOG_DEBUG);
1372 $resql = $this->db->query($sql);
1373 if (!$resql) {
1374 $error++;
1375 $this->error .= $this->db->lasterror();
1376 $errorflag = -1;
1377 }
1378 }
1379
1380 if (!$error) {
1381 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1382 $sql .= " WHERE rowid = ".((int) $this->id);
1383 dol_syslog(__METHOD__, LOG_DEBUG);
1384 $result = $this->db->query($sql);
1385 if (!$result) {
1386 $error++;
1387 $this->error = $this->db->error().' sql='.$sql;
1388 }
1389 }
1390
1391 // Remove extrafields
1392 if (!$error) {
1393 // For avoid conflicts if trigger used
1394 $result = $this->deleteExtraFields();
1395 if ($result < 0) {
1396 $error++;
1397 }
1398 }
1399
1400 if (!$error) {
1401 $this->db->commit();
1402 return 1;
1403 } else {
1404 $this->db->rollback();
1405 dol_syslog("Error ".$this->error, LOG_ERR);
1406 return -1;
1407 }
1408 }
1409
1410
1417 public function info($id)
1418 {
1419 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1420 $sql .= " c.tms as tms, c.fk_user_modif";
1421 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1422 $sql .= " WHERE c.rowid = ".((int) $id);
1423
1424 $resql = $this->db->query($sql);
1425 if ($resql) {
1426 if ($this->db->num_rows($resql)) {
1427 $obj = $this->db->fetch_object($resql);
1428
1429 $this->id = $obj->rowid;
1430
1431 $this->user_creation_id = $obj->fk_user_creat;
1432 $this->user_modification_id = $obj->fk_user_modif;
1433 $this->date_creation = $this->db->jdate($obj->datec);
1434 $this->date_modification = $this->db->jdate($obj->tms);
1435 }
1436
1437 $this->db->free($resql);
1438 } else {
1439 print $this->db->error();
1440 }
1441 }
1442
1448 public function getNbOfEMailings()
1449 {
1450 $sql = "SELECT count(mc.email) as nb";
1451 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1452 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1453 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1454
1455 $resql = $this->db->query($sql);
1456 if ($resql) {
1457 $obj = $this->db->fetch_object($resql);
1458 $nb = $obj->nb;
1459
1460 $this->db->free($resql);
1461 return $nb;
1462 } else {
1463 $this->error = $this->db->error();
1464 return -1;
1465 }
1466 }
1467
1474 public function getTooltipContentArray($params)
1475 {
1476 global $conf, $langs, $user;
1477
1478 $datas = [];
1479
1480 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1481 return ['optimize' => $langs->trans("ShowContact")];
1482 }
1483 if (!empty($this->photo) && class_exists('Form')) {
1484 $photo = '<div class="photointooltip floatright">';
1485 $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.
1486 $photo .= '</div>';
1487 $datas['photo'] = $photo;
1488 }
1489
1490 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1491 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1492 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1493 if (!empty($this->poste)) {
1494 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1495 }
1496 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1497 $phonelist = array();
1498 $country_code = empty($this->country_code) ? '' : $this->country_code;
1499 if ($this->phone_pro) {
1500 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1501 }
1502 if ($this->phone_mobile) {
1503 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1504 }
1505 if ($this->phone_perso) {
1506 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1507 }
1508 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1509 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1510
1511 return $datas;
1512 }
1513
1527 public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = 'valignmiddle')
1528 {
1529 global $conf, $langs, $hookmanager;
1530
1531 if (!empty($conf->dol_no_mouse_hover)) {
1532 $notooltip = 1; // Force disable tooltips
1533 }
1534
1535 $result = '';
1536 $params = [
1537 'id' => $this->id,
1538 'objecttype' => $this->element,
1539 'option' => $option,
1540 ];
1541 $classfortooltip = 'classfortooltip';
1542 $dataparams = '';
1543 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1544 $classfortooltip = 'classforajaxtooltip';
1545 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1546 $label = '';
1547 } else {
1548 $label = implode($this->getTooltipContentArray($params));
1549 }
1550
1551 $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1552
1553 if ($option !== 'nolink') {
1554 // Add param to save lastsearch_values or not
1555 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1556 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1557 $add_save_lastsearch_values = 1;
1558 }
1559 if ($url && $add_save_lastsearch_values) {
1560 $url .= '&save_lastsearch_values=1';
1561 }
1562 }
1563
1564 $url .= $moreparam;
1565
1566 $linkclose = '';
1567 if (empty($notooltip)) {
1568 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1569 $label = $langs->trans("ShowContact");
1570 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1571 }
1572 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1573 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1574 } else {
1575 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1576 }
1577
1578 if ($option == 'nolink' || empty($url)) {
1579 $linkstart = '<span';
1580 } else {
1581 $linkstart = '<a href="'.$url.'"';
1582 }
1583 $linkstart .= $linkclose.'>';
1584 if ($option == 'nolink' || empty($url)) {
1585 $linkend = '</span>';
1586 } else {
1587 $linkend = '</a>';
1588 }
1589
1590 $result .= $linkstart;
1591
1592 if ($withpicto) {
1593 if ($withpicto < 0) {
1594 $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>';
1595 if ($withpicto != 2 && $withpicto != -2) {
1596 $result .= ' ';
1597 }
1598 } else {
1599 $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);
1600 }
1601 }
1602 if ($withpicto != 2 && $withpicto != -2) {
1603 $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1604 }
1605
1606 $result .= $linkend;
1607
1608 global $action;
1609 $hookmanager->initHooks(array('contactdao'));
1610 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1611 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1612 if ($reshook > 0) {
1613 $result = $hookmanager->resPrint;
1614 } else {
1615 $result .= $hookmanager->resPrint;
1616 }
1617
1618 return $result;
1619 }
1620
1626 public function getCivilityLabel()
1627 {
1628 global $langs;
1629
1630 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1631 if (empty($code)) {
1632 return '';
1633 }
1634
1635 $langs->load("dict");
1636 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1637 }
1638
1645 public function getLibStatut($mode)
1646 {
1647 return $this->LibStatut($this->statut, $mode);
1648 }
1649
1650 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1658 public function LibStatut($status, $mode)
1659 {
1660 // phpcs:enable
1661 global $langs;
1662
1663 $labelStatus = array(
1664 0 => 'ActivityCeased',
1665 1 => 'InActivity',
1666 4 => 'InActivity',
1667 5 => 'ActivityCeased',
1668 );
1669 $labelStatusShort = array(
1670 0 => 'ActivityCeased',
1671 1 => 'InActivity',
1672 4 => 'InActivity',
1673 5 => 'ActivityCeased',
1674 );
1675
1676 $statusType = 'status4';
1677 if ($status == 0 || $status == 5) {
1678 $statusType = 'status5';
1679 }
1680
1681 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1682 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1683
1684 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1685 }
1686
1687
1688 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1695 public function LibPubPriv($status)
1696 {
1697 // phpcs:enable
1698 global $langs;
1699 if ($status == '1') {
1700 return $langs->trans('ContactPrivate');
1701 } else {
1702 return $langs->trans('ContactPublic');
1703 }
1704 }
1705
1706
1714 public function initAsSpecimen()
1715 {
1716 // Get first id of existing company and save it into $socid
1717 $socid = 0;
1718 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1719 $resql = $this->db->query($sql);
1720 if ($resql) {
1721 $obj = $this->db->fetch_object($resql);
1722 if ($obj) {
1723 $socid = $obj->rowid;
1724 }
1725 }
1726
1727 // Initialise parameters
1728 $this->id = 0;
1729 $this->entity = 1;
1730 $this->specimen = 1;
1731 $this->lastname = 'DOLIBARR';
1732 $this->firstname = 'SPECIMEN';
1733 $this->address = '21 jump street';
1734 $this->zip = '99999';
1735 $this->town = 'MyTown';
1736 $this->country_id = 1;
1737 $this->country_code = 'FR';
1738 $this->country = 'France';
1739 $this->email = 'specimen@specimen.com';
1740 $this->socialnetworks = array(
1741 'skype' => 'tom.hanson',
1742 'twitter' => 'tomhanson',
1743 'linkedin' => 'tomhanson',
1744 );
1745 $this->phone_pro = '0909090901';
1746 $this->phone_perso = '0909090902';
1747 $this->phone_mobile = '0909090903';
1748 $this->fax = '0909090909';
1749
1750 $this->note_public = 'This is a comment (public)';
1751 $this->note_private = 'This is a comment (private)';
1752
1753 $this->socid = $socid;
1754 $this->statut = 1;
1755
1756 return 1;
1757 }
1758
1765 public function setstatus($status)
1766 {
1767 global $conf, $langs, $user;
1768
1769 $error = 0;
1770
1771 // Check parameters
1772 if ($this->statut == $status) {
1773 return 0;
1774 } else {
1775 $this->statut = $status;
1776 }
1777
1778 $this->db->begin();
1779
1780 // User disable
1781 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1782 $sql .= " SET statut = ".((int) $this->statut);
1783 $sql .= ", fk_user_modif = ".((int) $user->id);
1784 $sql .= " WHERE rowid = ".((int) $this->id);
1785 $result = $this->db->query($sql);
1786
1787 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1788 if ($result) {
1789 // Call trigger
1790 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1791 if ($result < 0) {
1792 $error++;
1793 }
1794 // End call triggers
1795 }
1796
1797 if ($error) {
1798 $this->db->rollback();
1799 return -$error;
1800 } else {
1801 $this->db->commit();
1802 return 1;
1803 }
1804 }
1805
1816 public function setCategories($categories)
1817 {
1818 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1819 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1820 }
1821
1830 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1831 {
1832 $tables = array(
1833 'socpeople', 'societe_contacts'
1834 );
1835
1836 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1837 }
1838
1846 public function fetchRoles()
1847 {
1848 global $langs;
1849 $error = 0;
1850 $num = 0;
1851
1852 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1853 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1854 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1855 $sql .= " AND tc.source = 'external' AND tc.active=1";
1856 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1857 $sql .= " AND sc.entity IN (".getEntity('societe').')';
1858
1859 $resql = $this->db->query($sql);
1860 if ($resql) {
1861 $this->roles = array();
1862
1863 $num = $this->db->num_rows($resql);
1864 if ($num > 0) {
1865 while ($obj = $this->db->fetch_object($resql)) {
1866 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1867 $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1868 $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));
1869 }
1870 }
1871 } else {
1872 $error++;
1873 $this->error = $this->db->lasterror();
1874 $this->errors[] = $this->db->lasterror();
1875 }
1876
1877 if (empty($error)) {
1878 return $num;
1879 } else {
1880 return $error * -1;
1881 }
1882 }
1883
1891 public function getContactRoles($element = '')
1892 {
1893 $tab = array();
1894
1895 if ($element == 'action') {
1896 $element = 'agenda';
1897 }
1898
1899 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1900 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1901 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1902 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1903 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1904 $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1905 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1906 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1907 $sql .= " AND sp.entity IN (".getEntity('contact').")";
1908 $sql .= " AND tc.active = 1";
1909
1910 dol_syslog(__METHOD__, LOG_DEBUG);
1911 $resql = $this->db->query($sql);
1912 if ($resql) {
1913 $num = $this->db->num_rows($resql);
1914 $i = 0;
1915 while ($i < $num) {
1916 $obj = $this->db->fetch_object($resql);
1917 $tab[] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1918
1919 $i++;
1920 }
1921
1922 return $tab;
1923 } else {
1924 $this->error = $this->db->error();
1925 dol_print_error($this->db);
1926 return -1;
1927 }
1928 }
1929
1937 public function updateRoles()
1938 {
1939 global $conf;
1940
1941 $error = 0;
1942
1943 if (!isset($this->roles)) {
1944 return 0; // Avoid to loose roles when property not set
1945 }
1946
1947 $this->db->begin();
1948
1949 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1950
1951 $result = $this->db->query($sql);
1952 if (!$result) {
1953 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1954 $error++;
1955 } else {
1956 if (count($this->roles) > 0) {
1957 foreach ($this->roles as $keyRoles => $valRoles) {
1958 if (empty($valRoles)) {
1959 continue;
1960 }
1961 $idrole = 0;
1962 if (is_array($valRoles)) {
1963 $idrole = $valRoles['id'];
1964 } else {
1965 $idrole = $valRoles;
1966 }
1967
1968 $socid = 0;
1969 if (is_array($valRoles)) {
1970 $socid = $valRoles['socid'];
1971 } else {
1972 $socid = $this->socid;
1973 }
1974
1975 if ($socid > 0) {
1976 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1977 $sql .= " (entity,";
1978 $sql .= "date_creation,";
1979 $sql .= "fk_soc,";
1980 $sql .= "fk_c_type_contact,";
1981 $sql .= "fk_socpeople) ";
1982 $sql .= " VALUES (".$conf->entity.",";
1983 $sql .= "'".$this->db->idate(dol_now())."',";
1984 $sql .= $socid.", ";
1985 $sql .= $idrole." , ";
1986 $sql .= $this->id;
1987 $sql .= ")";
1988
1989 $result = $this->db->query($sql);
1990 if (!$result) {
1991 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1992 $error++;
1993 }
1994 }
1995 }
1996 }
1997 }
1998 if (empty($error)) {
1999 $this->db->commit();
2000 return 1;
2001 } else {
2002 $this->error = implode(' ', $this->errors);
2003 $this->db->rollback();
2004 return $error * -1;
2005 }
2006 }
2007
2014 public function loadCacheOfProspStatus($active = 1)
2015 {
2016 global $langs;
2017
2018 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
2019 if ($active >= 0) {
2020 $sql .= " WHERE active = ".((int) $active);
2021 }
2022 $resql = $this->db->query($sql);
2023 $num = $this->db->num_rows($resql);
2024 $i = 0;
2025 while ($i < $num) {
2026 $obj = $this->db->fetch_object($resql);
2027 $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);
2028 $i++;
2029 }
2030 return 1;
2031 }
2032
2038 public function getLibProspLevel()
2039 {
2040 return $this->libProspLevel($this->fk_prospectlevel);
2041 }
2042
2049 public function libProspLevel($fk_prospectlevel)
2050 {
2051 global $langs;
2052
2053 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2054 // If lib not found in language file, we get label from cache/database
2055 if ($lib == "ProspectLevel".$fk_prospectlevel) {
2056 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2057 }
2058 return $lib;
2059 }
2060
2061
2069 public function setProspectLevel(User $user)
2070 {
2071 return $this->update($this->id, $user);
2072 }
2073
2081 public function getLibProspCommStatut($mode = 0, $label = '')
2082 {
2083 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2084 }
2085
2099 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2100 {
2101 global $langs;
2102 $langs->load('customers');
2103
2104 if ($mode == 2) {
2105 if ($statut == '-1' || $statut == 'ST_NO') {
2106 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2107 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2108 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2109 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2110 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2111 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2112 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2113 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2114 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2115 } else {
2116 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);
2117 }
2118 }
2119 if ($mode == 3) {
2120 if ($statut == '-1' || $statut == 'ST_NO') {
2121 return img_action($langs->trans("StatusProspect-1"), '-1', $picto);
2122 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2123 return img_action($langs->trans("StatusProspect0"), '0', $picto);
2124 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2125 return img_action($langs->trans("StatusProspect1"), '1', $picto);
2126 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2127 return img_action($langs->trans("StatusProspect2"), '2', $picto);
2128 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2129 return img_action($langs->trans("StatusProspect3"), '3', $picto);
2130 } else {
2131 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, '0', $picto);
2132 }
2133 }
2134 if ($mode == 4) {
2135 if ($statut == '-1' || $statut == 'ST_NO') {
2136 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2137 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2138 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2139 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2140 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2141 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2142 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2143 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2144 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2145 } else {
2146 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);
2147 }
2148 }
2149
2150 return "Error, mode/status not found";
2151 }
2152
2153
2160 public function setNoEmail($no_email)
2161 {
2162 $error = 0;
2163
2164 // Update mass emailing flag into table mailing_unsubscribe
2165 if ($this->email) {
2166 $this->db->begin();
2167
2168 if ($no_email) {
2169 $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)."'";
2170 $resql = $this->db->query($sql);
2171 if ($resql) {
2172 $obj = $this->db->fetch_object($resql);
2173 $noemail = $obj->nb;
2174 if (empty($noemail)) {
2175 $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())."')";
2176 $resql = $this->db->query($sql);
2177 if (!$resql) {
2178 $error++;
2179 $this->error = $this->db->lasterror();
2180 $this->errors[] = $this->error;
2181 }
2182 }
2183 } else {
2184 $error++;
2185 $this->error = $this->db->lasterror();
2186 $this->errors[] = $this->error;
2187 }
2188 } else {
2189 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2190 $resql = $this->db->query($sql);
2191 if (!$resql) {
2192 $error++;
2193 $this->error = $this->db->lasterror();
2194 $this->errors[] = $this->error;
2195 }
2196 }
2197
2198 if (empty($error)) {
2199 $this->no_email = $no_email;
2200 $this->db->commit();
2201 return 1;
2202 } else {
2203 $this->db->rollback();
2204 return $error * -1;
2205 }
2206 }
2207
2208 return 0;
2209 }
2210
2217 public function getNoEmail()
2218 {
2219 if ($this->email) {
2220 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2221 $resql = $this->db->query($sql);
2222 if ($resql) {
2223 $obj = $this->db->fetch_object($resql);
2224 $this->no_email = $obj->nb;
2225 return 1;
2226 } else {
2227 $this->error = $this->db->lasterror();
2228 $this->errors[] = $this->error;
2229 return -1;
2230 }
2231 }
2232 return 0;
2233 }
2234
2235
2243 public function getKanbanView($option = '', $arraydata = null)
2244 {
2245 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2246
2247 $return = '<div class="box-flex-item box-flex-grow-zero">';
2248 $return .= '<div class="info-box info-box-sm">';
2249 $return .= '<span class="info-box-icon bg-infobox-action">';
2250 //var_dump($this->photo);exit;
2251 if (property_exists($this, 'photo') && !is_null($this->photo)) {
2252 $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2253 } else {
2254 $return .= img_picto('', $this->picto);
2255 }
2256 $return .= '</span>';
2257 $return .= '<div class="info-box-content">';
2258 $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref).'</div>';
2259 if ($selected >= 0) {
2260 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2261 }
2262 if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2263 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2264 }
2265 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2266 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2267 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2268 }*/
2269 /*if (method_exists($this, 'LibPubPriv')) {
2270 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2271 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2272 }*/
2273 if (method_exists($this, 'getLibStatut')) {
2274 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2275 }
2276 $return .= '</div>';
2277 $return .= '</div>';
2278 $return .= '</div>';
2279 return $return;
2280 }
2281}
$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