dolibarr 20.0.2
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 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000),
129 );
130
131 public $civility_id; // In fact we store civility_code
132 public $civility_code;
133 public $civility;
134
138 public $gender;
139
143 //private $egroupware_id;
144
148 public $birthday_alert;
149
155 public $civilite;
156
160 public $fullname;
161
165 public $name_alias;
166
170 public $address;
171
175 public $zip;
176
180 public $town;
181
185 public $state_id;
186
190 public $state_code;
191
195 public $state;
196
200 public $poste;
201
205 public $socid; // both socid and fk_soc are used
206 public $fk_soc; // both socid and fk_soc are used
207
211 public $socname;
212
216 public $statut;
217
218 public $code;
219
224 public $email;
225
232 public $mail;
233
238 public $url;
239
245 public $no_email;
246
251 public $socialnetworks;
252
256 public $photo;
257
261 public $phone_pro;
262
266 public $phone_perso;
267
271 public $phone_mobile;
272
276 public $fax;
277
282 public $priv;
283
287 public $birthday;
288
292 public $default_lang;
293
297 public $ref_facturation;
298
302 public $ref_contrat;
303
307 public $ref_commande;
308
312 public $ref_propal;
313
317 public $user_id;
318
322 public $user_login;
323
324 // END MODULEBUILDER PROPERTIES
325
326
331 public $oldcopy; // To contains a clone of this when we need to save old properties of object
332
336 public $roles;
337
338 public $cacheprospectstatus = array();
339
343 public $fk_prospectlevel;
344
345 public $stcomm_id;
346
347 public $statut_commercial;
348
352 public $stcomm_picto;
353
354
360 public function __construct($db)
361 {
362 $this->db = $db;
363 $this->statut = 1; // By default, status is enabled
364 $this->ismultientitymanaged = 1;
365 $this->isextrafieldmanaged = 1;
366
367 if (!isModEnabled('mailing')) {
368 $this->fields['no_email']['enabled'] = 0;
369 }
370 // typical ['s.nom'] is used for third-parties
371 if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) {
372 $this->fields['fk_soc']['enabled'] = 0;
373 $this->fields['fk_soc']['searchall'] = 0;
374 }
375
376 // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
377 if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) { // Default behaviour
378 $this->fields['fk_stcommcontact']['enabled'] = 0;
379 $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
380 }
381
382 // Unset fields that are disabled
383 foreach ($this->fields as $key => $val) {
384 if (isset($val['enabled']) && empty($val['enabled'])) {
385 unset($this->fields[$key]);
386 }
387 }
388
389 // Translate some data of arrayofkeyval
390 /*if (is_object($langs))
391 {
392 foreach($this->fields as $key => $val)
393 {
394 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
395 {
396 foreach($val['arrayofkeyval'] as $key2 => $val2)
397 {
398 $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
399 }
400 }
401 }
402 }*/
403 }
404
410 public function loadStateBoard()
411 {
412 global $user, $hookmanager;
413
414 $this->nb = array();
415 $clause = "WHERE";
416
417 $sql = "SELECT count(sp.rowid) as nb";
418 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
419 if (!$user->hasRight('societe', 'client', 'voir')) {
420 $sql .= ", ".MAIN_DB_PREFIX."societe as s";
421 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
422 $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
423 $clause = "AND";
424 }
425 $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
426 $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
427 if ($user->socid > 0) {
428 $sql .= " AND sp.fk_soc = ".((int) $user->socid);
429 }
430 // Add where from hooks
431 if (is_object($hookmanager)) {
432 $parameters = array();
433 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
434 $sql .= $hookmanager->resPrint;
435 }
436
437 $resql = $this->db->query($sql);
438 if ($resql) {
439 while ($obj = $this->db->fetch_object($resql)) {
440 $this->nb["contacts"] = $obj->nb;
441 }
442 $this->db->free($resql);
443 return 1;
444 } else {
445 dol_print_error($this->db);
446 $this->error = $this->db->lasterror();
447 return -1;
448 }
449 }
450
458 public function create($user, $notrigger = 0)
459 {
460 global $conf;
461
462 $error = 0;
463 $now = dol_now();
464
465 $this->db->begin();
466
467 // Clean parameters
468 $this->name_alias = trim($this->name_alias);
469 $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name);
470 $this->firstname = trim($this->firstname);
471 $this->setUpperOrLowerCase();
472 if (empty($this->socid)) {
473 $this->socid = 0;
474 }
475 if (empty($this->priv)) {
476 $this->priv = 0;
477 }
478 if (empty($this->statut)) {
479 $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
480 }
481
482 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
483
484 $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
485 $sql .= " datec";
486 $sql .= ", fk_soc";
487 $sql .= ", name_alias";
488 $sql .= ", lastname";
489 $sql .= ", firstname";
490 $sql .= ", fk_user_creat";
491 $sql .= ", priv";
492 $sql .= ", fk_stcommcontact";
493 $sql .= ", statut";
494 $sql .= ", canvas";
495 $sql .= ", entity";
496 $sql .= ", ref_ext";
497 $sql .= ", import_key";
498 $sql .= ") VALUES (";
499 $sql .= "'".$this->db->idate($now)."',";
500 if ($this->socid > 0) {
501 $sql .= " ".((int) $this->socid).",";
502 } else {
503 $sql .= "null,";
504 }
505 $sql .= "'".$this->db->escape($this->name_alias)."',";
506 $sql .= "'".$this->db->escape($this->lastname)."',";
507 $sql .= "'".$this->db->escape($this->firstname)."',";
508 $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
509 $sql .= " ".((int) $this->priv).",";
510 $sql .= " 0,";
511 $sql .= " ".((int) $this->statut).",";
512 $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
513 $sql .= " ".((int) $this->entity).",";
514 $sql .= "'".$this->db->escape($this->ref_ext)."',";
515 $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
516 $sql .= ")";
517
518 dol_syslog(get_class($this)."::create", LOG_DEBUG);
519 $resql = $this->db->query($sql);
520 if ($resql) {
521 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
522
523 if (!$error) {
524 $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
525 if ($result < 0) {
526 $error++;
527 $this->error = $this->db->lasterror();
528 }
529 }
530
531 if (!$error) {
532 $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
533 if ($result < 0) {
534 $error++;
535 $this->error = $this->db->lasterror();
536 }
537 }
538
539 if (!$error && !$notrigger) {
540 // Call trigger
541 $result = $this->call_trigger('CONTACT_CREATE', $user);
542 if ($result < 0) {
543 $error++;
544 }
545 // End call triggers
546 }
547
548 if (!$error) {
549 $this->db->commit();
550 return $this->id;
551 } else {
552 $this->db->rollback();
553 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
554 return -2;
555 }
556 } else {
557 $this->error = $this->db->lasterror();
558
559 $this->db->rollback();
560 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
561 return -1;
562 }
563 }
564
575 public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
576 {
577 global $conf;
578
579 $error = 0;
580
581 $this->id = $id;
582
583 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
584
585 // Clean parameters
586 $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext));
587 $this->name_alias = trim($this->name_alias);
588 $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
589 $this->firstname = trim($this->firstname);
590 $this->email = trim($this->email);
591 $this->phone_pro = trim($this->phone_pro);
592 $this->phone_perso = trim($this->phone_perso);
593 $this->phone_mobile = trim($this->phone_mobile);
594 $this->photo = trim($this->photo);
595 $this->fax = trim($this->fax);
596 $this->zip = (empty($this->zip) ? '' : trim($this->zip));
597 $this->town = (empty($this->town) ? '' : trim($this->town));
598 $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
599 if (empty($this->statut)) {
600 $this->statut = 0;
601 }
602 if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
603 $this->civility_code = $this->civility_id; // For backward compatibility
604 }
605 $this->setUpperOrLowerCase();
606
607 $this->db->begin();
608
609 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
610 if ($this->socid > 0) {
611 $sql .= " fk_soc = ".((int) $this->socid).",";
612 } elseif ($this->socid == -1) {
613 $sql .= " fk_soc = NULL,";
614 }
615 $sql .= " civility='".$this->db->escape($this->civility_code)."'";
616 $sql .= ", name_alias='".$this->db->escape($this->name_alias)."'";
617 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
618 $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
619 $sql .= ", address='".$this->db->escape($this->address)."'";
620 $sql .= ", zip='".$this->db->escape($this->zip)."'";
621 $sql .= ", town='".$this->db->escape($this->town)."'";
622 $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
623 $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
624 $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
625 $sql .= ", poste='".$this->db->escape($this->poste)."'";
626 $sql .= ", fax='".$this->db->escape($this->fax)."'";
627 $sql .= ", email='".$this->db->escape($this->email)."'";
628 $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
629 $sql .= ", photo='".$this->db->escape($this->photo)."'";
630 $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
631 $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
632 $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
633 $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
634 $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
635 $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
636 $sql .= ", priv = '".$this->db->escape($this->priv)."'";
637 $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
638 if (isset($this->stcomm_id)) {
639 $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
640 }
641 $sql .= ", statut = ".((int) $this->statut);
642 $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
643 $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
644 $sql .= ", entity = ".((int) $this->entity);
645 $sql .= " WHERE rowid = ".((int) $id);
646
647 dol_syslog(get_class($this)."::update", LOG_DEBUG);
648 $result = $this->db->query($sql);
649 if ($result) {
650 unset($this->country_code);
651 unset($this->country);
652 unset($this->state_code);
653 unset($this->state);
654
655 $action = 'update';
656
657 // Actions on extra fields
658 if (!$error) {
659 $result = $this->insertExtraFields();
660 if ($result < 0) {
661 $error++;
662 }
663 }
664
665 if (!$error) {
666 $result = $this->updateRoles();
667 if ($result < 0) {
668 $error++;
669 }
670 }
671
672 if (!$error && $this->user_id > 0) {
673 // If contact is linked to a user
674 $tmpobj = new User($this->db);
675 $tmpobj->fetch($this->user_id);
676 $usermustbemodified = 0;
677 if ($tmpobj->office_phone != $this->phone_pro) {
678 $tmpobj->office_phone = $this->phone_pro;
679 $usermustbemodified++;
680 }
681 if ($tmpobj->office_fax != $this->fax) {
682 $tmpobj->office_fax = $this->fax;
683 $usermustbemodified++;
684 }
685 if ($tmpobj->address != $this->address) {
686 $tmpobj->address = $this->address;
687 $usermustbemodified++;
688 }
689 if ($tmpobj->town != $this->town) {
690 $tmpobj->town = $this->town;
691 $usermustbemodified++;
692 }
693 if ($tmpobj->zip != $this->zip) {
694 $tmpobj->zip = $this->zip;
695 $usermustbemodified++;
696 }
697 if ($tmpobj->zip != $this->zip) {
698 $tmpobj->state_id = $this->state_id;
699 $usermustbemodified++;
700 }
701 if ($tmpobj->country_id != $this->country_id) {
702 $tmpobj->country_id = $this->country_id;
703 $usermustbemodified++;
704 }
705 if ($tmpobj->email != $this->email) {
706 $tmpobj->email = $this->email;
707 $usermustbemodified++;
708 }
709 if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
710 $tmpobj->socialnetworks = $this->socialnetworks;
711 $usermustbemodified++;
712 }
713 if ($usermustbemodified) {
714 $result = $tmpobj->update($user, 0, 1, 1, 1);
715 if ($result < 0) {
716 $error++;
717 }
718 }
719 }
720
721 if (!$error && !$notrigger) {
722 // Call trigger
723 $result = $this->call_trigger('CONTACT_MODIFY', $user);
724 if ($result < 0) {
725 $error++;
726 }
727 // End call triggers
728 }
729
730 if (!$error) {
731 $this->db->commit();
732 return 1;
733 } else {
734 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
735 $this->db->rollback();
736 return -$error;
737 }
738 } else {
739 $this->error = $this->db->lasterror().' sql='.$sql;
740 $this->db->rollback();
741 return -1;
742 }
743 }
744
745
746 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
747 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
757 public function _load_ldap_dn($info, $mode = 0)
758 {
759 // phpcs:enable
760 global $conf;
761 $dn = '';
762 if ($mode == 0) {
763 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
764 } elseif ($mode == 1) {
765 $dn = getDolGlobalString('LDAP_CONTACT_DN');
766 } elseif ($mode == 2) {
767 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
768 }
769 return $dn;
770 }
771
772
773 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
774 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
780 public function _load_ldap_info()
781 {
782 // phpcs:enable
783 global $conf, $langs;
784
785 $info = array();
786
787 // Object classes
788 $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
789
790 $this->fullname = $this->getFullName($langs);
791
792 // Fields
793 if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
794 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
795 }
796 if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
797 $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
798 }
799 if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
800 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
801 }
802
803 if ($this->poste) {
804 $info["title"] = $this->poste;
805 }
806 if ($this->socid > 0) {
807 $soc = new Societe($this->db);
808 $soc->fetch($this->socid);
809
810 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
811 if ($soc->client == 1) {
812 $info["businessCategory"] = "Customers";
813 }
814 if ($soc->client == 2) {
815 $info["businessCategory"] = "Prospects";
816 }
817 if ($soc->fournisseur == 1) {
818 $info["businessCategory"] = "Suppliers";
819 }
820 }
821 if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
822 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
823 }
824 if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
825 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
826 }
827 if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
828 $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
829 }
830 if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
831 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
832 }
833 if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
834 $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
835 }
836 if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
837 $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
838 }
839 if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
840 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
841 }
842 if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
843 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
844 }
845 if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
846 $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
847 }
848 if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
849 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
850 }
851
852 if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
853 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
854
855 $info['uidnumber'] = $this->id;
856
857 $info['phpgwTz'] = 0;
858 $info['phpgwMailType'] = 'INTERNET';
859 $info['phpgwMailHomeType'] = 'INTERNET';
860
861 $info["phpgwContactTypeId"] = 'n';
862 $info["phpgwContactCatId"] = 0;
863 $info["phpgwContactAccess"] = "public";
864
865 /*
866 if (dol_strlen($this->egroupware_id) == 0) {
867 $this->egroupware_id = 1;
868 }
869 $info["phpgwContactOwner"] = $this->egroupware_id;
870 */
871 $info["phpgwContactOwner"] = 1;
872
873 if ($this->email) {
874 $info["rfc822Mailbox"] = $this->email;
875 }
876 if ($this->phone_mobile) {
877 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
878 }
879 }
880
881 return $info;
882 }
883
884
885 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
894 public function update_perso($id, $user = null, $notrigger = 0)
895 {
896 // phpcs:enable
897 $error = 0;
898 $result = false;
899
900 $this->db->begin();
901
902 // Update the contact
903 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
904 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
905 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
906 if ($user) {
907 $sql .= ", fk_user_modif = ".((int) $user->id);
908 }
909 $sql .= " WHERE rowid = ".((int) $id);
910
911 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
912 $resql = $this->db->query($sql);
913 if (!$resql) {
914 $error++;
915 $this->error = $this->db->lasterror();
916 }
917
918 if ($user) {
919 // Update birthday alert
920 if (!empty($this->birthday_alert)) {
921 //check existing
922 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
923 $result_check = $this->db->query($sql_check);
924 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
925 //insert
926 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
927 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
928 $result = $this->db->query($sql);
929 if (!$result) {
930 $error++;
931 $this->error = $this->db->lasterror();
932 }
933 } else {
934 $result = true;
935 }
936 } else {
937 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
938 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
939 $result = $this->db->query($sql);
940 if (!$result) {
941 $error++;
942 $this->error = $this->db->lasterror();
943 }
944 }
945 }
946
947 if (!$error && !$notrigger) {
948 // Call trigger
949 $result = $this->call_trigger('CONTACT_MODIFY', $user);
950 if ($result < 0) {
951 $error++;
952 }
953 // End call triggers
954 }
955
956 if (!$error) {
957 $this->db->commit();
958 return 1;
959 } else {
960 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
961 $this->db->rollback();
962 return -$error;
963 }
964 }
965
966
978 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
979 {
980 global $langs;
981
982 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
983
984 if (empty($id) && empty($ref_ext) && empty($email)) {
985 $this->error = 'BadParameter';
986 return -1;
987 }
988
989 $langs->loadLangs(array("dict", "companies"));
990
991 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
992 $sql .= " c.address, c.statut, c.zip, c.town,";
993 $sql .= " c.fk_pays as country_id,";
994 $sql .= " c.fk_departement as state_id,";
995 $sql .= " c.birthday,";
996 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
997 $sql .= " c.socialnetworks,";
998 $sql .= " c.photo,";
999 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1000 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1001 $sql .= " c.import_key,";
1002 $sql .= " c.datec as date_creation, c.tms as date_modification, c.fk_user_creat, c.fk_user_modif,";
1003 $sql .= " co.label as country, co.code as country_code,";
1004 $sql .= " d.nom as state, d.code_departement as state_code,";
1005 $sql .= " u.rowid as user_id, u.login as user_login,";
1006 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1007 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1008 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1009 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1010 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1011 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1012 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1013 if ($id) {
1014 $sql .= " WHERE c.rowid = ".((int) $id);
1015 } else {
1016 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1017 if ($ref_ext) {
1018 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1019 }
1020 if ($email) {
1021 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1022 }
1023 if ($socid) {
1024 $sql .= " AND c.fk_soc = ".((int) $socid);
1025 }
1026 }
1027
1028 $resql = $this->db->query($sql);
1029 if ($resql) {
1030 $num = $this->db->num_rows($resql);
1031 if ($num > 1) {
1032 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1033 dol_syslog($this->error, LOG_ERR);
1034
1035 return 2;
1036 } elseif ($num) { // $num = 1
1037 $obj = $this->db->fetch_object($resql);
1038
1039 $this->id = $obj->rowid;
1040 $this->entity = $obj->entity;
1041 $this->ref = $obj->rowid;
1042 $this->ref_ext = $obj->ref_ext;
1043
1044 $this->civility_code = $obj->civility_code;
1045 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1046
1047 $this->name_alias = $obj->name_alias;
1048 $this->lastname = $obj->lastname;
1049 $this->firstname = $obj->firstname;
1050 $this->address = $obj->address;
1051 $this->zip = $obj->zip;
1052 $this->town = $obj->town;
1053
1054 $this->date_creation = $this->db->jdate($obj->date_creation);
1055 $this->date_modification = $this->db->jdate($obj->date_modification);
1056 $this->user_creation_id = $obj->fk_user_creat;
1057 $this->user_modification_id = $obj->fk_user_modif;
1058
1059 $this->state_id = $obj->state_id;
1060 $this->state_code = $obj->state_code;
1061 $this->state = $obj->state;
1062
1063 $this->country_id = $obj->country_id;
1064 $this->country_code = $obj->country_id ? $obj->country_code : '';
1065 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1066
1067 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1068 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1069 $this->socname = $obj->socname;
1070 $this->poste = $obj->poste;
1071 $this->statut = $obj->statut;
1072
1073 $this->fk_prospectlevel = $obj->fk_prospectlevel;
1074
1075 $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1076 $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1077 $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1078 $this->statut_commercial = $libelle; // libelle statut commercial
1079 $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1080
1081 $this->phone_pro = trim($obj->phone);
1082 $this->fax = trim($obj->fax);
1083 $this->phone_perso = trim($obj->phone_perso);
1084 $this->phone_mobile = trim($obj->phone_mobile);
1085
1086 $this->email = $obj->email;
1087 $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1088 $this->photo = $obj->photo;
1089 $this->priv = $obj->priv;
1090 $this->mail = $obj->email;
1091
1092 $this->birthday = $this->db->jdate($obj->birthday);
1093 $this->note = $obj->note_private; // deprecated
1094 $this->note_private = $obj->note_private;
1095 $this->note_public = $obj->note_public;
1096 $this->default_lang = $obj->default_lang;
1097 $this->user_id = $obj->user_id;
1098 $this->user_login = $obj->user_login;
1099 $this->canvas = $obj->canvas;
1100
1101 $this->import_key = $obj->import_key;
1102
1103 // Define gender according to civility
1104 $this->setGenderFromCivility();
1105
1106 // Search Dolibarr user linked to this contact
1107 $sql = "SELECT u.rowid ";
1108 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1109 $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1110
1111 $resql = $this->db->query($sql);
1112 if ($resql) {
1113 if ($this->db->num_rows($resql)) {
1114 $uobj = $this->db->fetch_object($resql);
1115
1116 $this->user_id = $uobj->rowid;
1117 }
1118 $this->db->free($resql);
1119 } else {
1120 $this->error = $this->db->error();
1121 return -1;
1122 }
1123
1124 // Retrieve all extrafield
1125 // fetch optionals attributes and labels
1126 $this->fetch_optionals();
1127
1128 // Load also alerts of this user
1129 if ($user) {
1130 $sql = "SELECT fk_user";
1131 $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1132 $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1133
1134 $resql = $this->db->query($sql);
1135 if ($resql) {
1136 if ($this->db->num_rows($resql)) {
1137 $obj = $this->db->fetch_object($resql);
1138
1139 $this->birthday_alert = 1;
1140 }
1141 $this->db->free($resql);
1142 } else {
1143 $this->error = $this->db->error();
1144 return -1;
1145 }
1146 }
1147
1148 // Load also roles of this address
1149 if ($loadalsoroles) {
1150 $resultRole = $this->fetchRoles();
1151 if ($resultRole < 0) {
1152 return $resultRole;
1153 }
1154 }
1155
1156 return 1;
1157 } else {
1158 $this->error = $langs->trans("RecordNotFound");
1159 return 0;
1160 }
1161 } else {
1162 $this->error = $this->db->error();
1163 return -1;
1164 }
1165 }
1166
1180 public function findNearest($id = 0, $lastname = '', $firstname = '', $ref_ext = '', $email = '', $ref_alias = '', $socid = 0)
1181 {
1182 // A rowid is known, it is a unique key so we found it
1183 if ($id) {
1184 return $id;
1185 }
1186
1187 // We try to find the contact with exact matching on all fields
1188 // TODO Replace this with step by step search
1189 // Then search on email
1190 // Then search on lastname + firstname
1191 // Then search ref_ext or alias with a OR
1192 $tmpcontact = new Contact($this->db);
1193 $result = $tmpcontact->fetch($id, null, $ref_ext, $email, 0, $socid);
1194
1195 return $result;
1196 }
1197
1198
1199
1206 public function setGenderFromCivility()
1207 {
1208 unset($this->gender);
1209
1210 if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1211 $this->gender = 'man';
1212 } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1213 $this->gender = 'woman';
1214 }
1215 }
1216
1217 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1227 public function load_ref_elements()
1228 {
1229 // phpcs:enable
1230 // Count the elements for which it is contact
1231 $sql = "SELECT tc.element, count(ec.rowid) as nb";
1232 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1233 $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1234 $sql .= " AND fk_socpeople = ".((int) $this->id);
1235 $sql .= " AND tc.source = 'external'";
1236 $sql .= " GROUP BY tc.element";
1237
1238 dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1239
1240 $resql = $this->db->query($sql);
1241 if ($resql) {
1242 while ($obj = $this->db->fetch_object($resql)) {
1243 if ($obj->nb) {
1244 if ($obj->element == 'facture') {
1245 $this->ref_facturation = $obj->nb;
1246 } elseif ($obj->element == 'contrat') {
1247 $this->ref_contrat = $obj->nb;
1248 } elseif ($obj->element == 'commande') {
1249 $this->ref_commande = $obj->nb;
1250 } elseif ($obj->element == 'propal') {
1251 $this->ref_propal = $obj->nb;
1252 }
1253 }
1254 }
1255 $this->db->free($resql);
1256 return 0;
1257 } else {
1258 $this->error = $this->db->lasterror();
1259 return -1;
1260 }
1261 }
1262
1270 public function delete($user, $notrigger = 0)
1271 {
1272 $error = 0;
1273
1274 $this->db->begin();
1275
1276 if (!$error && !$notrigger) {
1277 // Call trigger
1278 $result = $this->call_trigger('CONTACT_DELETE', $user);
1279 if ($result < 0) {
1280 $error++;
1281 }
1282 // End call triggers
1283 }
1284
1285 if (!$error) {
1286 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1287 $sql = "SELECT ec.rowid";
1288 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1289 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1290 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1291 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1292 $sql .= " AND tc.source='external'";
1293 dol_syslog(__METHOD__, LOG_DEBUG);
1294 $resql = $this->db->query($sql);
1295 if ($resql) {
1296 $num = $this->db->num_rows($resql);
1297
1298 $i = 0;
1299 while ($i < $num && !$error) {
1300 $obj = $this->db->fetch_object($resql);
1301
1302 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1303 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1304 dol_syslog(__METHOD__, LOG_DEBUG);
1305 $result = $this->db->query($sqldel);
1306 if (!$result) {
1307 $error++;
1308 $this->error = $this->db->error().' sql='.$sqldel;
1309 }
1310
1311 $i++;
1312 }
1313 } else {
1314 $error++;
1315 $this->error = $this->db->error().' sql='.$sql;
1316 }
1317 }
1318
1319 if (!$error) {
1320 // Remove Roles
1321 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1322 dol_syslog(__METHOD__, LOG_DEBUG);
1323 $resql = $this->db->query($sql);
1324 if (!$resql) {
1325 $error++;
1326 $this->error .= $this->db->lasterror();
1327 $errorflag = -1;
1328 }
1329 }
1330
1331 if (!$error) {
1332 // Remove Notifications
1333 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1334 dol_syslog(__METHOD__, LOG_DEBUG);
1335 $resql = $this->db->query($sql);
1336 if (!$resql) {
1337 $error++;
1338 $this->error .= $this->db->lasterror();
1339 $errorflag = -1;
1340 }
1341 }
1342
1343 if (!$error) {
1344 // Remove category
1345 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1346 dol_syslog(__METHOD__, LOG_DEBUG);
1347 $resql = $this->db->query($sql);
1348 if (!$resql) {
1349 $error++;
1350 $this->error .= $this->db->lasterror();
1351 $errorflag = -1;
1352 }
1353 }
1354
1355 if (!$error) {
1356 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1357 $sql .= " WHERE rowid = ".((int) $this->id);
1358 dol_syslog(__METHOD__, LOG_DEBUG);
1359 $result = $this->db->query($sql);
1360 if (!$result) {
1361 $error++;
1362 $this->error = $this->db->error().' sql='.$sql;
1363 }
1364 }
1365
1366 // Remove extrafields
1367 if (!$error) {
1368 // For avoid conflicts if trigger used
1369 $result = $this->deleteExtraFields();
1370 if ($result < 0) {
1371 $error++;
1372 }
1373 }
1374
1375 if (!$error) {
1376 $this->db->commit();
1377 return 1;
1378 } else {
1379 $this->db->rollback();
1380 dol_syslog("Error ".$this->error, LOG_ERR);
1381 return -1;
1382 }
1383 }
1384
1385
1392 public function info($id)
1393 {
1394 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1395 $sql .= " c.tms as tms, c.fk_user_modif";
1396 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1397 $sql .= " WHERE c.rowid = ".((int) $id);
1398
1399 $resql = $this->db->query($sql);
1400 if ($resql) {
1401 if ($this->db->num_rows($resql)) {
1402 $obj = $this->db->fetch_object($resql);
1403
1404 $this->id = $obj->rowid;
1405
1406 $this->user_creation_id = $obj->fk_user_creat;
1407 $this->user_modification_id = $obj->fk_user_modif;
1408 $this->date_creation = $this->db->jdate($obj->datec);
1409 $this->date_modification = $this->db->jdate($obj->tms);
1410 }
1411
1412 $this->db->free($resql);
1413 } else {
1414 print $this->db->error();
1415 }
1416 }
1417
1423 public function getNbOfEMailings()
1424 {
1425 $sql = "SELECT count(mc.email) as nb";
1426 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1427 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1428 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1429
1430 $resql = $this->db->query($sql);
1431 if ($resql) {
1432 $obj = $this->db->fetch_object($resql);
1433 $nb = $obj->nb;
1434
1435 $this->db->free($resql);
1436 return $nb;
1437 } else {
1438 $this->error = $this->db->error();
1439 return -1;
1440 }
1441 }
1442
1449 public function getTooltipContentArray($params)
1450 {
1451 global $conf, $langs, $user;
1452
1453 $datas = [];
1454
1455 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1456 return ['optimize' => $langs->trans("ShowContact")];
1457 }
1458 if (!empty($this->photo) && class_exists('Form')) {
1459 $photo = '<div class="photointooltip floatright">';
1460 $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.
1461 $photo .= '</div>';
1462 $datas['photo'] = $photo;
1463 }
1464
1465 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1466 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1467 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1468 if (!empty($this->poste)) {
1469 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1470 }
1471 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1472 $phonelist = array();
1473 $country_code = empty($this->country_code) ? '' : $this->country_code;
1474 if ($this->phone_pro) {
1475 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1476 }
1477 if ($this->phone_mobile) {
1478 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1479 }
1480 if ($this->phone_perso) {
1481 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1482 }
1483 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1484 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1485
1486 return $datas;
1487 }
1488
1502 public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1503 {
1504 global $conf, $langs, $hookmanager;
1505
1506 if (!empty($conf->dol_no_mouse_hover)) {
1507 $notooltip = 1; // Force disable tooltips
1508 }
1509
1510 $result = '';
1511 $params = [
1512 'id' => $this->id,
1513 'objecttype' => $this->element,
1514 'option' => $option,
1515 ];
1516 $classfortooltip = 'classfortooltip';
1517 $dataparams = '';
1518 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1519 $classfortooltip = 'classforajaxtooltip';
1520 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1521 $label = '';
1522 } else {
1523 $label = implode($this->getTooltipContentArray($params));
1524 }
1525
1526 $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1527
1528 if ($option !== 'nolink') {
1529 // Add param to save lastsearch_values or not
1530 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1531 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1532 $add_save_lastsearch_values = 1;
1533 }
1534 if ($url && $add_save_lastsearch_values) {
1535 $url .= '&save_lastsearch_values=1';
1536 }
1537 }
1538
1539 $url .= $moreparam;
1540
1541 $linkclose = '';
1542 if (empty($notooltip)) {
1543 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1544 $label = $langs->trans("ShowContact");
1545 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1546 }
1547 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1548 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1549 } else {
1550 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1551 }
1552
1553 if ($option == 'nolink' || empty($url)) {
1554 $linkstart = '<span';
1555 } else {
1556 $linkstart = '<a href="'.$url.'"';
1557 }
1558 $linkstart .= $linkclose.'>';
1559 if ($option == 'nolink' || empty($url)) {
1560 $linkend = '</span>';
1561 } else {
1562 $linkend = '</a>';
1563 }
1564
1565 $result .= $linkstart;
1566
1567 if ($withpicto) {
1568 if ($withpicto < 0) {
1569 $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>';
1570 if ($withpicto != 2 && $withpicto != -2) {
1571 $result .= ' ';
1572 }
1573 } else {
1574 $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);
1575 }
1576 }
1577 if ($withpicto != 2 && $withpicto != -2) {
1578 $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1579 }
1580
1581 $result .= $linkend;
1582
1583 global $action;
1584 $hookmanager->initHooks(array('contactdao'));
1585 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1586 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1587 if ($reshook > 0) {
1588 $result = $hookmanager->resPrint;
1589 } else {
1590 $result .= $hookmanager->resPrint;
1591 }
1592
1593 return $result;
1594 }
1595
1601 public function getCivilityLabel()
1602 {
1603 global $langs;
1604
1605 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1606 if (empty($code)) {
1607 return '';
1608 }
1609
1610 $langs->load("dict");
1611 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1612 }
1613
1620 public function getLibStatut($mode)
1621 {
1622 return $this->LibStatut($this->statut, $mode);
1623 }
1624
1625 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1633 public function LibStatut($status, $mode)
1634 {
1635 // phpcs:enable
1636 global $langs;
1637
1638 $labelStatus = array(
1639 0 => 'ActivityCeased',
1640 1 => 'InActivity',
1641 4 => 'InActivity',
1642 5 => 'ActivityCeased',
1643 );
1644 $labelStatusShort = array(
1645 0 => 'ActivityCeased',
1646 1 => 'InActivity',
1647 4 => 'InActivity',
1648 5 => 'ActivityCeased',
1649 );
1650
1651 $statusType = 'status4';
1652 if ($status == 0 || $status == 5) {
1653 $statusType = 'status5';
1654 }
1655
1656 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1657 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1658
1659 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1660 }
1661
1662
1663 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1670 public function LibPubPriv($status)
1671 {
1672 // phpcs:enable
1673 global $langs;
1674 if ($status == '1') {
1675 return $langs->trans('ContactPrivate');
1676 } else {
1677 return $langs->trans('ContactPublic');
1678 }
1679 }
1680
1681
1689 public function initAsSpecimen()
1690 {
1691 // Get first id of existing company and save it into $socid
1692 $socid = 0;
1693 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1694 $resql = $this->db->query($sql);
1695 if ($resql) {
1696 $obj = $this->db->fetch_object($resql);
1697 if ($obj) {
1698 $socid = $obj->rowid;
1699 }
1700 }
1701
1702 // Initialise parameters
1703 $this->id = 0;
1704 $this->entity = 1;
1705 $this->specimen = 1;
1706 $this->lastname = 'DOLIBARR';
1707 $this->firstname = 'SPECIMEN';
1708 $this->address = '21 jump street';
1709 $this->zip = '99999';
1710 $this->town = 'MyTown';
1711 $this->country_id = 1;
1712 $this->country_code = 'FR';
1713 $this->country = 'France';
1714 $this->email = 'specimen@specimen.com';
1715 $this->socialnetworks = array(
1716 'skype' => 'tom.hanson',
1717 'twitter' => 'tomhanson',
1718 'linkedin' => 'tomhanson',
1719 );
1720 $this->phone_pro = '0909090901';
1721 $this->phone_perso = '0909090902';
1722 $this->phone_mobile = '0909090903';
1723 $this->fax = '0909090909';
1724
1725 $this->note_public = 'This is a comment (public)';
1726 $this->note_private = 'This is a comment (private)';
1727
1728 $this->socid = $socid;
1729 $this->statut = 1;
1730
1731 return 1;
1732 }
1733
1740 public function setstatus($status)
1741 {
1742 global $conf, $langs, $user;
1743
1744 $error = 0;
1745
1746 // Check parameters
1747 if ($this->statut == $status) {
1748 return 0;
1749 } else {
1750 $this->statut = $status;
1751 }
1752
1753 $this->db->begin();
1754
1755 // User disable
1756 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1757 $sql .= " SET statut = ".((int) $this->statut);
1758 $sql .= ", fk_user_modif = ".((int) $user->id);
1759 $sql .= " WHERE rowid = ".((int) $this->id);
1760 $result = $this->db->query($sql);
1761
1762 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1763 if ($result) {
1764 // Call trigger
1765 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1766 if ($result < 0) {
1767 $error++;
1768 }
1769 // End call triggers
1770 }
1771
1772 if ($error) {
1773 $this->db->rollback();
1774 return -$error;
1775 } else {
1776 $this->db->commit();
1777 return 1;
1778 }
1779 }
1780
1791 public function setCategories($categories)
1792 {
1793 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1794 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1795 }
1796
1805 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1806 {
1807 $tables = array(
1808 'socpeople', 'societe_contacts'
1809 );
1810
1811 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1812 }
1813
1821 public function fetchRoles()
1822 {
1823 global $langs;
1824 $error = 0;
1825 $num = 0;
1826
1827 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1828 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1829 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1830 $sql .= " AND tc.source = 'external' AND tc.active=1";
1831 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1832 $sql .= " AND sc.entity IN (".getEntity('societe').')';
1833
1834 $resql = $this->db->query($sql);
1835 if ($resql) {
1836 $this->roles = array();
1837
1838 $num = $this->db->num_rows($resql);
1839 if ($num > 0) {
1840 while ($obj = $this->db->fetch_object($resql)) {
1841 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1842 $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1843 $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));
1844 }
1845 }
1846 } else {
1847 $error++;
1848 $this->error = $this->db->lasterror();
1849 $this->errors[] = $this->db->lasterror();
1850 }
1851
1852 if (empty($error)) {
1853 return $num;
1854 } else {
1855 return $error * -1;
1856 }
1857 }
1858
1866 public function getContactRoles($element = '')
1867 {
1868 $tab = array();
1869
1870 if ($element == 'action') {
1871 $element = 'agenda';
1872 }
1873
1874 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1875 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1876 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1877 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1878 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1879 $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1880 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1881 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1882 $sql .= " AND sp.entity IN (".getEntity('contact').")";
1883 $sql .= " AND tc.active = 1";
1884
1885 dol_syslog(__METHOD__, LOG_DEBUG);
1886 $resql = $this->db->query($sql);
1887 if ($resql) {
1888 $num = $this->db->num_rows($resql);
1889 $i = 0;
1890 while ($i < $num) {
1891 $obj = $this->db->fetch_object($resql);
1892 $tab[] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1893
1894 $i++;
1895 }
1896
1897 return $tab;
1898 } else {
1899 $this->error = $this->db->error();
1900 dol_print_error($this->db);
1901 return -1;
1902 }
1903 }
1904
1912 public function updateRoles()
1913 {
1914 global $conf;
1915
1916 $error = 0;
1917
1918 if (!isset($this->roles)) {
1919 return 0; // Avoid to loose roles when property not set
1920 }
1921
1922 $this->db->begin();
1923
1924 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1925
1926 $result = $this->db->query($sql);
1927 if (!$result) {
1928 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1929 $error++;
1930 } else {
1931 if (count($this->roles) > 0) {
1932 foreach ($this->roles as $keyRoles => $valRoles) {
1933 if (empty($valRoles)) {
1934 continue;
1935 }
1936 $idrole = 0;
1937 if (is_array($valRoles)) {
1938 $idrole = $valRoles['id'];
1939 } else {
1940 $idrole = $valRoles;
1941 }
1942
1943 $socid = 0;
1944 if (is_array($valRoles)) {
1945 $socid = $valRoles['socid'];
1946 } else {
1947 $socid = $this->socid;
1948 }
1949
1950 if ($socid > 0) {
1951 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1952 $sql .= " (entity,";
1953 $sql .= "date_creation,";
1954 $sql .= "fk_soc,";
1955 $sql .= "fk_c_type_contact,";
1956 $sql .= "fk_socpeople) ";
1957 $sql .= " VALUES (".$conf->entity.",";
1958 $sql .= "'".$this->db->idate(dol_now())."',";
1959 $sql .= $socid.", ";
1960 $sql .= $idrole." , ";
1961 $sql .= $this->id;
1962 $sql .= ")";
1963
1964 $result = $this->db->query($sql);
1965 if (!$result) {
1966 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1967 $error++;
1968 }
1969 }
1970 }
1971 }
1972 }
1973 if (empty($error)) {
1974 $this->db->commit();
1975 return 1;
1976 } else {
1977 $this->error = implode(' ', $this->errors);
1978 $this->db->rollback();
1979 return $error * -1;
1980 }
1981 }
1982
1989 public function loadCacheOfProspStatus($active = 1)
1990 {
1991 global $langs;
1992
1993 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
1994 if ($active >= 0) {
1995 $sql .= " WHERE active = ".((int) $active);
1996 }
1997 $resql = $this->db->query($sql);
1998 $num = $this->db->num_rows($resql);
1999 $i = 0;
2000 while ($i < $num) {
2001 $obj = $this->db->fetch_object($resql);
2002 $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);
2003 $i++;
2004 }
2005 return 1;
2006 }
2007
2013 public function getLibProspLevel()
2014 {
2015 return $this->libProspLevel($this->fk_prospectlevel);
2016 }
2017
2024 public function libProspLevel($fk_prospectlevel)
2025 {
2026 global $langs;
2027
2028 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2029 // If lib not found in language file, we get label from cache/database
2030 if ($lib == "ProspectLevel".$fk_prospectlevel) {
2031 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2032 }
2033 return $lib;
2034 }
2035
2036
2044 public function setProspectLevel(User $user)
2045 {
2046 return $this->update($this->id, $user);
2047 }
2048
2056 public function getLibProspCommStatut($mode = 0, $label = '')
2057 {
2058 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2059 }
2060
2074 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2075 {
2076 global $langs;
2077 $langs->load('customers');
2078
2079 if ($mode == 2) {
2080 if ($statut == '-1' || $statut == 'ST_NO') {
2081 return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2082 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2083 return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2084 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2085 return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2086 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2087 return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2088 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2089 return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2090 } else {
2091 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);
2092 }
2093 }
2094 if ($mode == 3) {
2095 if ($statut == '-1' || $statut == 'ST_NO') {
2096 return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2097 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2098 return img_action($langs->trans("StatusProspect0"), 0, $picto);
2099 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2100 return img_action($langs->trans("StatusProspect1"), 1, $picto);
2101 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2102 return img_action($langs->trans("StatusProspect2"), 2, $picto);
2103 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2104 return img_action($langs->trans("StatusProspect3"), 3, $picto);
2105 } else {
2106 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);
2107 }
2108 }
2109 if ($mode == 4) {
2110 if ($statut == '-1' || $statut == 'ST_NO') {
2111 return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2112 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2113 return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2114 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2115 return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2116 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2117 return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2118 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2119 return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2120 } else {
2121 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);
2122 }
2123 }
2124
2125 return "Error, mode/status not found";
2126 }
2127
2128
2135 public function setNoEmail($no_email)
2136 {
2137 $error = 0;
2138
2139 // Update mass emailing flag into table mailing_unsubscribe
2140 if ($this->email) {
2141 $this->db->begin();
2142
2143 if ($no_email) {
2144 $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)."'";
2145 $resql = $this->db->query($sql);
2146 if ($resql) {
2147 $obj = $this->db->fetch_object($resql);
2148 $noemail = $obj->nb;
2149 if (empty($noemail)) {
2150 $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())."')";
2151 $resql = $this->db->query($sql);
2152 if (!$resql) {
2153 $error++;
2154 $this->error = $this->db->lasterror();
2155 $this->errors[] = $this->error;
2156 }
2157 }
2158 } else {
2159 $error++;
2160 $this->error = $this->db->lasterror();
2161 $this->errors[] = $this->error;
2162 }
2163 } else {
2164 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2165 $resql = $this->db->query($sql);
2166 if (!$resql) {
2167 $error++;
2168 $this->error = $this->db->lasterror();
2169 $this->errors[] = $this->error;
2170 }
2171 }
2172
2173 if (empty($error)) {
2174 $this->no_email = $no_email;
2175 $this->db->commit();
2176 return 1;
2177 } else {
2178 $this->db->rollback();
2179 return $error * -1;
2180 }
2181 }
2182
2183 return 0;
2184 }
2185
2192 public function getNoEmail()
2193 {
2194 if ($this->email) {
2195 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2196 $resql = $this->db->query($sql);
2197 if ($resql) {
2198 $obj = $this->db->fetch_object($resql);
2199 $this->no_email = $obj->nb;
2200 return 1;
2201 } else {
2202 $this->error = $this->db->lasterror();
2203 $this->errors[] = $this->error;
2204 return -1;
2205 }
2206 }
2207 return 0;
2208 }
2209
2210
2218 public function getKanbanView($option = '', $arraydata = null)
2219 {
2220 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2221
2222 $return = '<div class="box-flex-item box-flex-grow-zero">';
2223 $return .= '<div class="info-box info-box-sm">';
2224 $return .= '<span class="info-box-icon bg-infobox-action">';
2225 //var_dump($this->photo);exit;
2226 if (property_exists($this, 'photo') && !is_null($this->photo)) {
2227 $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2228 } else {
2229 $return .= img_picto('', $this->picto);
2230 }
2231 $return .= '</span>';
2232 $return .= '<div class="info-box-content">';
2233 $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref).'</div>';
2234 if ($selected >= 0) {
2235 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2236 }
2237 if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2238 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2239 }
2240 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2241 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2242 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2243 }*/
2244 /*if (method_exists($this, 'LibPubPriv')) {
2245 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2246 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2247 }*/
2248 if (method_exists($this, 'getLibStatut')) {
2249 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2250 }
2251 $return .= '</div>';
2252 $return .= '</div>';
2253 $return .= '</div>';
2254 return $return;
2255 }
2256}
$object ref
Definition info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage contact/addresses.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
__construct($db)
Constructor.
update_perso($id, $user=null, $notrigger=0)
Update field alert birthday.
getLibProspCommStatut($mode=0, $label='')
Return status of prospect.
create($user, $notrigger=0)
Add a contact into database.
loadStateBoard()
Load indicators into this->nb for board.
getNomUrl($withpicto=0, $option='', $maxlen=0, $moreparam='', $save_lastsearch_value=-1, $notooltip=0, $morecss='')
Return name of contact with link (and eventually picto) Use $this->id, $this->lastname,...
getNoEmail()
get "blacklist" mailing status set no_email attribute to 1 or 0
getNbOfEMailings()
Return number of mass Emailing received by these contacts with its email.
libProspCommStatut($statut, $mode=0, $label='', $picto='')
Return label of a given status.
getTooltipContentArray($params)
getTooltipContentArray
libProspLevel($fk_prospectlevel)
Return label of prospect level.
getCivilityLabel()
Return civility label of contact.
setProspectLevel(User $user)
Set prospect level.
fetch($id, $user=null, $ref_ext='', $email='', $loadalsoroles=0, $socid=0)
Load object contact.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
load_ref_elements()
Load number of elements the contact is used as a link for ref_facturation ref_contrat ref_commande (f...
getLibProspLevel()
Return prospect level.
fetchRoles()
Fetch roles (default contact of some companies) for the current contact.
getContactRoles($element='')
Get thirdparty contact roles of a given contact.
_load_ldap_dn($info, $mode=0)
Return DN string complete in the LDAP directory for the object.
LibStatut($status, $mode)
Return the label of a given status.
setGenderFromCivility()
Set the property "gender" of this class, based on the property "civility_id" or use property "civilit...
update($id, $user=null, $notrigger=0, $action='update', $nosyncuser=0)
Update information into database.
setstatus($status)
Change status of a user.
loadCacheOfProspStatus($active=1)
Load array of prospect status.
_load_ldap_info()
Initialize info table (LDAP attributes table)
updateRoles()
Updates all roles (default contact for companies) according to values inside the ->roles array.
info($id)
Load contact information from the database.
initAsSpecimen()
Initialise an instance with random values.
getLibStatut($mode)
Return the label of the status.
setNoEmail($no_email)
Set "blacklist" mailing status.
LibPubPriv($status)
Return translated label of Public or Private.
setCategories($categories)
Sets object to supplied categories.
findNearest($id=0, $lastname='', $firstname='', $ref_ext='', $email='', $ref_alias='', $socid=0)
Search the contact that match the most the provided parameters.
Class to manage Dolibarr database access.
static showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='', $addlinktofullsize=1, $cache=0, $forcecapture='', $noexternsourceoverwrite=0)
Return HTML code to output a photo.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
trait CommonPeople
Support class for thirdparties, contacts, members, users or resources.
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='')
Format phone numbers according to country.
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_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
img_action($titlealt, $numaction, $picto='', $moreatt='')
Show logo action.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return 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.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:140
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut
Definition invoice.php:2010