dolibarr 18.0.6
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 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <https://www.gnu.org/licenses/>.
27 */
28
34require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/commonsocialnetworks.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
37
38
42class Contact extends CommonObject
43{
45 use CommonPeople;
46
50 public $element = 'contact';
51
55 public $table_element = 'socpeople';
56
61 public $ismultientitymanaged = 1;
62
66 public $picto = 'contact';
67
92 // BEGIN MODULEBUILDER PROPERTIES
96 public $fields = array(
97 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
98 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>3, 'notnull'=>1, 'position'=>30, 'index'=>1),
99 'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>3, 'position'=>35),
100 'civility' =>array('type'=>'varchar(6)', 'label'=>'Civility', 'enabled'=>1, 'visible'=>3, 'position'=>40),
101 'lastname' =>array('type'=>'varchar(50)', 'label'=>'Lastname', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'showoncombobox'=>1, 'searchall'=>1),
102 'firstname' =>array('type'=>'varchar(50)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'showoncombobox'=>1, 'searchall'=>1),
103 'poste' =>array('type'=>'varchar(80)', 'label'=>'PostOrFunction', 'enabled'=>1, 'visible'=>-1, 'position'=>52),
104 'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
105 'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>1, 'position'=>60),
106 'town' =>array('type'=>'varchar(50)', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
107 'fk_departement' =>array('type'=>'integer', 'label'=>'Fk departement', 'enabled'=>1, 'visible'=>3, 'position'=>70),
108 'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>3, 'position'=>75),
109 'fk_soc' =>array('type'=>'integer', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>1, 'position'=>77, 'searchall'=>1),
110 'birthday' =>array('type'=>'date', 'label'=>'Birthday', 'enabled'=>1, 'visible'=>3, 'position'=>80),
111 'phone' =>array('type'=>'varchar(30)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>1, 'position'=>90, 'searchall'=>1),
112 'phone_perso' =>array('type'=>'varchar(30)', 'label'=>'PhonePerso', 'enabled'=>1, 'visible'=>-1, 'position'=>95, 'searchall'=>1),
113 'phone_mobile' =>array('type'=>'varchar(30)', 'label'=>'PhoneMobile', 'enabled'=>1, 'visible'=>1, 'position'=>100, 'searchall'=>1),
114 'fax' =>array('type'=>'varchar(30)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'searchall'=>1),
115 'email' =>array('type'=>'varchar(255)', 'label'=>'Email', 'enabled'=>1, 'visible'=>1, 'position'=>110, 'searchall'=>1),
116 'socialnetworks' =>array('type'=>'text', 'label'=>'SocialNetworks', 'enabled'=>1, 'visible'=>3, 'position'=>115),
117 'photo' =>array('type'=>'varchar(255)', 'label'=>'Photo', 'enabled'=>1, 'visible'=>3, 'position'=>170),
118 'priv' =>array('type'=>'smallint(6)', 'label'=>'ContactVisibility', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>175),
119 'fk_stcommcontact' =>array('type'=>'integer', 'label'=>'ProspectStatus', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>220),
120 'fk_prospectcontactlevel' =>array('type'=>'varchar(12)', 'label'=>'ProspectLevel', 'enabled'=>1, 'visible'=>-1, 'position'=>255),
121 //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),
122 'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>3, 'position'=>195, 'searchall'=>1),
123 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>3, 'position'=>200, 'searchall'=>1),
124 'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>3, 'position'=>205),
125 'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>3, 'position'=>210),
126 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>300),
127 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>305),
128 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>3, 'position'=>310),
129 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>3, 'position'=>315),
130 'statut' =>array('type'=>'tinyint(4)', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),
131 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'position'=>1000),
132 );
133
134 public $civility_id; // In fact we store civility_code
135 public $civility_code;
136 public $civility;
137
141 public $gender;
142
146 public $egroupware_id;
147
151 public $birthday_alert;
152
158 public $civilite;
159
163 public $fullname;
164
168 public $address;
169
173 public $zip;
174
178 public $town;
179
183 public $state_id;
184
188 public $state_code;
189
193 public $state;
194
195 public $poste; // Position
196
200 public $socid; // both socid and fk_soc are used
201 public $fk_soc; // both socid and fk_soc are used
202
206 public $socname;
207
211 public $statut;
212
213 public $code;
214
219 public $email;
220
227 public $mail;
228
233 public $url;
234
240 public $no_email;
241
245 public $socialnetworks;
246
252 public $skype;
253
259 public $twitter;
260
266 public $facebook;
267
273 public $linkedin;
274
280 public $jabberid;
281
285 public $photo;
286
290 public $phone_pro;
291
295 public $phone_perso;
296
300 public $phone_mobile;
301
305 public $fax;
306
311 public $priv;
312
313 public $birthday;
314 public $default_lang;
315
319 public $ref_facturation;
320
324 public $ref_contrat;
325
329 public $ref_commande;
330
334 public $ref_propal;
335
339 public $user_id;
340
344 public $user_login;
345
346 // END MODULEBUILDER PROPERTIES
347
348
353 public $oldcopy; // To contains a clone of this when we need to save old properties of object
354
358 public $roles;
359
360 public $cacheprospectstatus = array();
361 public $fk_prospectlevel;
362 public $stcomm_id;
363 public $statut_commercial;
364
368 public $stcomm_picto;
369
370
376 public function __construct($db)
377 {
378 global $conf, $langs;
379
380 $this->db = $db;
381 $this->statut = 1; // By default, status is enabled
382
383 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
384 $this->fields['rowid']['visible'] = 0;
385 }
386 if (!isModEnabled('mailing')) {
387 $this->fields['no_email']['enabled'] = 0;
388 }
389 // typical ['s.nom'] is used for third-parties
390 if (empty($conf->global->SOCIETE_DISABLE_CONTACTS)) {
391 $this->fields['fk_soc']['enabled'] = 0;
392 $this->fields['fk_soc']['searchall'] = 0;
393 }
394
395 // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
396 if (!empty($conf->global->SOCIETE_DISABLE_PROSPECTS) || empty($conf->global->THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES)) { // Default behaviour
397 $this->fields['fk_stcommcontact']['enabled'] = 0;
398 $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
399 }
400
401 // Unset fields that are disabled
402 foreach ($this->fields as $key => $val) {
403 if (isset($val['enabled']) && empty($val['enabled'])) {
404 unset($this->fields[$key]);
405 }
406 }
407
408 // Translate some data of arrayofkeyval
409 /*if (is_object($langs))
410 {
411 foreach($this->fields as $key => $val)
412 {
413 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
414 {
415 foreach($val['arrayofkeyval'] as $key2 => $val2)
416 {
417 $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
418 }
419 }
420 }
421 }*/
422 }
423
424 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
430 public function load_state_board()
431 {
432 // phpcs:enable
433 global $user, $hookmanager;
434
435 $this->nb = array();
436 $clause = "WHERE";
437
438 $sql = "SELECT count(sp.rowid) as nb";
439 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
440 if (empty($user->rights->societe->client->voir) && !$user->socid) {
441 $sql .= ", ".MAIN_DB_PREFIX."societe as s";
442 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
443 $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
444 $clause = "AND";
445 }
446 $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
447 $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
448 if ($user->socid > 0) {
449 $sql .= " AND sp.fk_soc = ".((int) $user->socid);
450 }
451 // Add where from hooks
452 if (is_object($hookmanager)) {
453 $parameters = array();
454 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
455 $sql .= $hookmanager->resPrint;
456 }
457
458 $resql = $this->db->query($sql);
459 if ($resql) {
460 while ($obj = $this->db->fetch_object($resql)) {
461 $this->nb["contacts"] = $obj->nb;
462 }
463 $this->db->free($resql);
464 return 1;
465 } else {
466 dol_print_error($this->db);
467 $this->error = $this->db->lasterror();
468 return -1;
469 }
470 }
471
479 public function create($user, $notrigger = 0)
480 {
481 global $conf;
482
483 $error = 0;
484 $now = dol_now();
485
486 $this->db->begin();
487
488 // Clean parameters
489 $this->lastname = $this->lastname ?trim($this->lastname) : trim($this->name);
490 $this->firstname = trim($this->firstname);
491 $this->setUpperOrLowerCase();
492 if (empty($this->socid)) {
493 $this->socid = 0;
494 }
495 if (empty($this->priv)) {
496 $this->priv = 0;
497 }
498 if (empty($this->statut)) {
499 $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
500 }
501
502 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
503
504 $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
505 $sql .= " datec";
506 $sql .= ", fk_soc";
507 $sql .= ", lastname";
508 $sql .= ", firstname";
509 $sql .= ", fk_user_creat";
510 $sql .= ", priv";
511 $sql .= ", fk_stcommcontact";
512 $sql .= ", statut";
513 $sql .= ", canvas";
514 $sql .= ", entity";
515 $sql .= ", ref_ext";
516 $sql .= ", import_key";
517 $sql .= ") VALUES (";
518 $sql .= "'".$this->db->idate($now)."',";
519 if ($this->socid > 0) {
520 $sql .= " ".((int) $this->socid).",";
521 } else {
522 $sql .= "null,";
523 }
524 $sql .= "'".$this->db->escape($this->lastname)."',";
525 $sql .= "'".$this->db->escape($this->firstname)."',";
526 $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
527 $sql .= " ".((int) $this->priv).",";
528 $sql .= " 0,";
529 $sql .= " ".((int) $this->statut).",";
530 $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
531 $sql .= " ".((int) $this->entity).",";
532 $sql .= "'".$this->db->escape($this->ref_ext)."',";
533 $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
534 $sql .= ")";
535
536 dol_syslog(get_class($this)."::create", LOG_DEBUG);
537 $resql = $this->db->query($sql);
538 if ($resql) {
539 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
540
541 if (!$error) {
542 $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
543 if ($result < 0) {
544 $error++;
545 $this->error = $this->db->lasterror();
546 }
547 }
548
549 if (!$error) {
550 $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
551 if ($result < 0) {
552 $error++;
553 $this->error = $this->db->lasterror();
554 }
555 }
556
557 if (!$error && !$notrigger) {
558 // Call trigger
559 $result = $this->call_trigger('CONTACT_CREATE', $user);
560 if ($result < 0) {
561 $error++;
562 }
563 // End call triggers
564 }
565
566 if (!$error) {
567 $this->db->commit();
568 return $this->id;
569 } else {
570 $this->db->rollback();
571 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
572 return -2;
573 }
574 } else {
575 $this->error = $this->db->lasterror();
576
577 $this->db->rollback();
578 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
579 return -1;
580 }
581 }
582
593 public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
594 {
595 global $conf, $langs, $hookmanager;
596
597 $error = 0;
598
599 $this->id = $id;
600
601 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
602
603 // Clean parameters
604 $this->ref_ext = trim($this->ref_ext);
605 $this->lastname = trim($this->lastname) ?trim($this->lastname) : trim($this->lastname);
606 $this->firstname = trim($this->firstname);
607 $this->email = trim($this->email);
608 $this->phone_pro = trim($this->phone_pro);
609 $this->phone_perso = trim($this->phone_perso);
610 $this->phone_mobile = trim($this->phone_mobile);
611 $this->photo = trim($this->photo);
612 $this->fax = trim($this->fax);
613 $this->zip = (empty($this->zip) ? '' : trim($this->zip));
614 $this->town = (empty($this->town) ? '' : trim($this->town));
615 $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
616 if (empty($this->statut)) {
617 $this->statut = 0;
618 }
619 if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
620 $this->civility_code = $this->civility_id; // For backward compatibility
621 }
622 $this->setUpperOrLowerCase();
623 $this->db->begin();
624
625 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
626 if ($this->socid > 0) {
627 $sql .= " fk_soc = ".((int) $this->socid).",";
628 } elseif ($this->socid == -1) {
629 $sql .= " fk_soc = NULL,";
630 }
631 $sql .= " civility='".$this->db->escape($this->civility_code)."'";
632 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
633 $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
634 $sql .= ", address='".$this->db->escape($this->address)."'";
635 $sql .= ", zip='".$this->db->escape($this->zip)."'";
636 $sql .= ", town='".$this->db->escape($this->town)."'";
637 $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
638 $sql .= ", fk_pays=".($this->country_id > 0 ? $this->country_id : 'NULL');
639 $sql .= ", fk_departement=".($this->state_id > 0 ? $this->state_id : 'NULL');
640 $sql .= ", poste='".$this->db->escape($this->poste)."'";
641 $sql .= ", fax='".$this->db->escape($this->fax)."'";
642 $sql .= ", email='".$this->db->escape($this->email)."'";
643 $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
644 $sql .= ", photo='".$this->db->escape($this->photo)."'";
645 $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
646 $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
647 $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
648 $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
649 $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
650 $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
651 $sql .= ", priv = '".$this->db->escape($this->priv)."'";
652 $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
653 if (isset($this->stcomm_id)) {
654 $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
655 }
656 $sql .= ", statut = ".((int) $this->statut);
657 $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "NULL");
658 $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
659 $sql .= ", entity = ".((int) $this->entity);
660 $sql .= " WHERE rowid = ".((int) $id);
661
662 dol_syslog(get_class($this)."::update", LOG_DEBUG);
663 $result = $this->db->query($sql);
664 if ($result) {
665 unset($this->country_code);
666 unset($this->country);
667 unset($this->state_code);
668 unset($this->state);
669
670 $action = 'update';
671
672 // Actions on extra fields
673 if (!$error) {
674 $result = $this->insertExtraFields();
675 if ($result < 0) {
676 $error++;
677 }
678 }
679
680 if (!$error) {
681 $result = $this->updateRoles();
682 if ($result < 0) {
683 $error++;
684 }
685 }
686
687 if (!$error && $this->user_id > 0) {
688 // If contact is linked to a user
689 $tmpobj = new User($this->db);
690 $tmpobj->fetch($this->user_id);
691 $usermustbemodified = 0;
692 if ($tmpobj->office_phone != $this->phone_pro) {
693 $tmpobj->office_phone = $this->phone_pro;
694 $usermustbemodified++;
695 }
696 if ($tmpobj->office_fax != $this->fax) {
697 $tmpobj->office_fax = $this->fax;
698 $usermustbemodified++;
699 }
700 if ($tmpobj->address != $this->address) {
701 $tmpobj->address = $this->address;
702 $usermustbemodified++;
703 }
704 if ($tmpobj->town != $this->town) {
705 $tmpobj->town = $this->town;
706 $usermustbemodified++;
707 }
708 if ($tmpobj->zip != $this->zip) {
709 $tmpobj->zip = $this->zip;
710 $usermustbemodified++;
711 }
712 if ($tmpobj->zip != $this->zip) {
713 $tmpobj->state_id = $this->state_id;
714 $usermustbemodified++;
715 }
716 if ($tmpobj->country_id != $this->country_id) {
717 $tmpobj->country_id = $this->country_id;
718 $usermustbemodified++;
719 }
720 if ($tmpobj->email != $this->email) {
721 $tmpobj->email = $this->email;
722 $usermustbemodified++;
723 }
724 if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
725 $tmpobj->socialnetworks = $this->socialnetworks;
726 $usermustbemodified++;
727 }
728 if ($usermustbemodified) {
729 $result = $tmpobj->update($user, 0, 1, 1, 1);
730 if ($result < 0) {
731 $error++;
732 }
733 }
734 }
735
736 if (!$error && !$notrigger) {
737 // Call trigger
738 $result = $this->call_trigger('CONTACT_MODIFY', $user);
739 if ($result < 0) {
740 $error++;
741 }
742 // End call triggers
743 }
744
745 if (!$error) {
746 $this->db->commit();
747 return 1;
748 } else {
749 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
750 $this->db->rollback();
751 return -$error;
752 }
753 } else {
754 $this->error = $this->db->lasterror().' sql='.$sql;
755 $this->db->rollback();
756 return -1;
757 }
758 }
759
760
761 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
762 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
772 public function _load_ldap_dn($info, $mode = 0)
773 {
774 // phpcs:enable
775 global $conf;
776 $dn = '';
777 if ($mode == 0) {
778 $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;
779 } elseif ($mode == 1) {
780 $dn = $conf->global->LDAP_CONTACT_DN;
781 } elseif ($mode == 2) {
782 $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS];
783 }
784 return $dn;
785 }
786
787
788 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
789 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
795 public function _load_ldap_info()
796 {
797 // phpcs:enable
798 global $conf, $langs;
799
800 $info = array();
801
802 // Object classes
803 $info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);
804
805 $this->fullname = $this->getFullName($langs);
806
807 // Fields
808 if ($this->fullname && !empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME)) {
809 $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname;
810 }
811 if ($this->lastname && !empty($conf->global->LDAP_CONTACT_FIELD_NAME)) {
812 $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname;
813 }
814 if ($this->firstname && !empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME)) {
815 $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname;
816 }
817
818 if ($this->poste) {
819 $info["title"] = $this->poste;
820 }
821 if ($this->socid > 0) {
822 $soc = new Societe($this->db);
823 $soc->fetch($this->socid);
824
825 $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->name;
826 if ($soc->client == 1) {
827 $info["businessCategory"] = "Customers";
828 }
829 if ($soc->client == 2) {
830 $info["businessCategory"] = "Prospects";
831 }
832 if ($soc->fournisseur == 1) {
833 $info["businessCategory"] = "Suppliers";
834 }
835 }
836 if ($this->address && !empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS)) {
837 $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address;
838 }
839 if ($this->zip && !empty($conf->global->LDAP_CONTACT_FIELD_ZIP)) {
840 $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->zip;
841 }
842 if ($this->town && !empty($conf->global->LDAP_CONTACT_FIELD_TOWN)) {
843 $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->town;
844 }
845 if ($this->country_code && !empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY)) {
846 $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code;
847 }
848 if ($this->phone_pro && !empty($conf->global->LDAP_CONTACT_FIELD_PHONE)) {
849 $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro;
850 }
851 if ($this->phone_perso && !empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) {
852 $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
853 }
854 if ($this->phone_mobile && !empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) {
855 $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
856 }
857 if ($this->fax && !empty($conf->global->LDAP_CONTACT_FIELD_FAX)) {
858 $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
859 }
860 if ($this->note_private && !empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) {
861 $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
862 }
863 if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL)) {
864 $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
865 }
866
867 if ($conf->global->LDAP_SERVER_TYPE == 'egroupware') {
868 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
869
870 $info['uidnumber'] = $this->id;
871
872 $info['phpgwTz'] = 0;
873 $info['phpgwMailType'] = 'INTERNET';
874 $info['phpgwMailHomeType'] = 'INTERNET';
875
876 $info["phpgwContactTypeId"] = 'n';
877 $info["phpgwContactCatId"] = 0;
878 $info["phpgwContactAccess"] = "public";
879
880 if (dol_strlen($this->egroupware_id) == 0) {
881 $this->egroupware_id = 1;
882 }
883
884 $info["phpgwContactOwner"] = $this->egroupware_id;
885
886 if ($this->email) {
887 $info["rfc822Mailbox"] = $this->email;
888 }
889 if ($this->phone_mobile) {
890 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
891 }
892 }
893
894 return $info;
895 }
896
897
898 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
907 public function update_perso($id, $user = null, $notrigger = 0)
908 {
909 // phpcs:enable
910 $error = 0;
911 $result = false;
912
913 $this->db->begin();
914
915 // Mis a jour contact
916 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
917 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
918 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
919 if ($user) {
920 $sql .= ", fk_user_modif = ".((int) $user->id);
921 }
922 $sql .= " WHERE rowid = ".((int) $id);
923
924 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
925 $resql = $this->db->query($sql);
926 if (!$resql) {
927 $error++;
928 $this->error = $this->db->lasterror();
929 }
930
931 if ($user) {
932 // Update birthday alert
933 if (!empty($this->birthday_alert)) {
934 //check existing
935 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
936 $result_check = $this->db->query($sql_check);
937 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
938 //insert
939 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
940 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
941 $result = $this->db->query($sql);
942 if (!$result) {
943 $error++;
944 $this->error = $this->db->lasterror();
945 }
946 } else {
947 $result = true;
948 }
949 } else {
950 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
951 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
952 $result = $this->db->query($sql);
953 if (!$result) {
954 $error++;
955 $this->error = $this->db->lasterror();
956 }
957 }
958 }
959
960 if (!$error && !$notrigger) {
961 // Call trigger
962 $result = $this->call_trigger('CONTACT_MODIFY', $user);
963 if ($result < 0) {
964 $error++;
965 }
966 // End call triggers
967 }
968
969 if (!$error) {
970 $this->db->commit();
971 return 1;
972 } else {
973 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
974 $this->db->rollback();
975 return -$error;
976 }
977 }
978
979
990 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
991 {
992 global $langs;
993
994 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
995
996 if (empty($id) && empty($ref_ext) && empty($email)) {
997 $this->error = 'BadParameter';
998 return -1;
999 }
1000
1001 $langs->loadLangs(array("dict", "companies"));
1002
1003 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.lastname, c.firstname,";
1004 $sql .= " c.address, c.statut, c.zip, c.town,";
1005 $sql .= " c.fk_pays as country_id,";
1006 $sql .= " c.fk_departement as state_id,";
1007 $sql .= " c.birthday,";
1008 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1009 $sql .= " c.socialnetworks,";
1010 $sql .= " c.photo,";
1011 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1012 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1013 $sql .= " c.import_key,";
1014 $sql .= " c.datec as date_creation, c.tms as date_modification,";
1015 $sql .= " co.label as country, co.code as country_code,";
1016 $sql .= " d.nom as state, d.code_departement as state_code,";
1017 $sql .= " u.rowid as user_id, u.login as user_login,";
1018 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1019 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1020 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1021 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1022 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1023 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1024 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1025 if ($id) {
1026 $sql .= " WHERE c.rowid = ".((int) $id);
1027 } else {
1028 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1029 if ($ref_ext) {
1030 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1031 }
1032 if ($email) {
1033 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1034 }
1035 }
1036
1037 $resql = $this->db->query($sql);
1038 if ($resql) {
1039 $num = $this->db->num_rows($resql);
1040 if ($num > 1) {
1041 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1042 dol_syslog($this->error, LOG_ERR);
1043
1044 return 2;
1045 } elseif ($num) { // $num = 1
1046 $obj = $this->db->fetch_object($resql);
1047
1048 $this->id = $obj->rowid;
1049 $this->entity = $obj->entity;
1050 $this->ref = $obj->rowid;
1051 $this->ref_ext = $obj->ref_ext;
1052
1053 $this->civility_code = $obj->civility_code;
1054 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1055
1056 $this->lastname = $obj->lastname;
1057 $this->firstname = $obj->firstname;
1058 $this->address = $obj->address;
1059 $this->zip = $obj->zip;
1060 $this->town = $obj->town;
1061
1062 $this->date_creation = $this->db->jdate($obj->date_creation);
1063 $this->date_modification = $this->db->jdate($obj->date_modification);
1064
1065 $this->state_id = $obj->state_id;
1066 $this->state_code = $obj->state_code;
1067 $this->state = $obj->state;
1068
1069 $this->country_id = $obj->country_id;
1070 $this->country_code = $obj->country_id ? $obj->country_code : '';
1071 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1072
1073 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1074 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1075 $this->socname = $obj->socname;
1076 $this->poste = $obj->poste;
1077 $this->statut = $obj->statut;
1078
1079 $this->fk_prospectlevel = $obj->fk_prospectlevel;
1080
1081 $transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);
1082 $libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1083 $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1084 $this->statut_commercial = $libelle; // libelle statut commercial
1085 $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1086
1087 $this->phone_pro = trim($obj->phone);
1088 $this->fax = trim($obj->fax);
1089 $this->phone_perso = trim($obj->phone_perso);
1090 $this->phone_mobile = trim($obj->phone_mobile);
1091
1092 $this->email = $obj->email;
1093 $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1094 $this->photo = $obj->photo;
1095 $this->priv = $obj->priv;
1096 $this->mail = $obj->email;
1097
1098 $this->birthday = $this->db->jdate($obj->birthday);
1099 $this->note = $obj->note_private; // deprecated
1100 $this->note_private = $obj->note_private;
1101 $this->note_public = $obj->note_public;
1102 $this->default_lang = $obj->default_lang;
1103 $this->user_id = $obj->user_id;
1104 $this->user_login = $obj->user_login;
1105 $this->canvas = $obj->canvas;
1106
1107 $this->import_key = $obj->import_key;
1108
1109 // Define gender according to civility
1110 $this->setGenderFromCivility();
1111
1112 // Search Dolibarr user linked to this contact
1113 $sql = "SELECT u.rowid ";
1114 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1115 $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1116
1117 $resql = $this->db->query($sql);
1118 if ($resql) {
1119 if ($this->db->num_rows($resql)) {
1120 $uobj = $this->db->fetch_object($resql);
1121
1122 $this->user_id = $uobj->rowid;
1123 }
1124 $this->db->free($resql);
1125 } else {
1126 $this->error = $this->db->error();
1127 return -1;
1128 }
1129
1130 // Retrieve all extrafield
1131 // fetch optionals attributes and labels
1132 $this->fetch_optionals();
1133
1134 // Load also alerts of this user
1135 if ($user) {
1136 $sql = "SELECT fk_user";
1137 $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1138 $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1139
1140 $resql = $this->db->query($sql);
1141 if ($resql) {
1142 if ($this->db->num_rows($resql)) {
1143 $obj = $this->db->fetch_object($resql);
1144
1145 $this->birthday_alert = 1;
1146 }
1147 $this->db->free($resql);
1148 } else {
1149 $this->error = $this->db->error();
1150 return -1;
1151 }
1152 }
1153
1154 // Load also roles of this address
1155 if ($loadalsoroles) {
1156 $resultRole = $this->fetchRoles();
1157 if ($resultRole < 0) {
1158 return $resultRole;
1159 }
1160 }
1161
1162 return 1;
1163 } else {
1164 $this->error = $langs->trans("RecordNotFound");
1165 return 0;
1166 }
1167 } else {
1168 $this->error = $this->db->error();
1169 return -1;
1170 }
1171 }
1172
1173
1174
1181 public function setGenderFromCivility()
1182 {
1183 unset($this->gender);
1184
1185 if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1186 $this->gender = 'man';
1187 } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1188 $this->gender = 'woman';
1189 }
1190 }
1191
1192 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1202 public function load_ref_elements()
1203 {
1204 // phpcs:enable
1205 // Compte les elements pour lesquels il est contact
1206 $sql = "SELECT tc.element, count(ec.rowid) as nb";
1207 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1208 $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1209 $sql .= " AND fk_socpeople = ".((int) $this->id);
1210 $sql .= " AND tc.source = 'external'";
1211 $sql .= " GROUP BY tc.element";
1212
1213 dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1214
1215 $resql = $this->db->query($sql);
1216 if ($resql) {
1217 while ($obj = $this->db->fetch_object($resql)) {
1218 if ($obj->nb) {
1219 if ($obj->element == 'facture') {
1220 $this->ref_facturation = $obj->nb;
1221 } elseif ($obj->element == 'contrat') {
1222 $this->ref_contrat = $obj->nb;
1223 } elseif ($obj->element == 'commande') {
1224 $this->ref_commande = $obj->nb;
1225 } elseif ($obj->element == 'propal') {
1226 $this->ref_propal = $obj->nb;
1227 }
1228 }
1229 }
1230 $this->db->free($resql);
1231 return 0;
1232 } else {
1233 $this->error = $this->db->lasterror();
1234 return -1;
1235 }
1236 }
1237
1245 public function delete($notrigger = 0)
1246 {
1247 global $conf, $langs, $user;
1248
1249 $error = 0;
1250
1251 $this->db->begin();
1252
1253 if (!$error && !$notrigger) {
1254 // Call trigger
1255 $result = $this->call_trigger('CONTACT_DELETE', $user);
1256 if ($result < 0) {
1257 $error++;
1258 }
1259 // End call triggers
1260 }
1261
1262 if (!$error) {
1263 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1264 $sql = "SELECT ec.rowid";
1265 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1266 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1267 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1268 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1269 $sql .= " AND tc.source='external'";
1270 dol_syslog(__METHOD__, LOG_DEBUG);
1271 $resql = $this->db->query($sql);
1272 if ($resql) {
1273 $num = $this->db->num_rows($resql);
1274
1275 $i = 0;
1276 while ($i < $num && !$error) {
1277 $obj = $this->db->fetch_object($resql);
1278
1279 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1280 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1281 dol_syslog(__METHOD__, LOG_DEBUG);
1282 $result = $this->db->query($sqldel);
1283 if (!$result) {
1284 $error++;
1285 $this->error = $this->db->error().' sql='.$sqldel;
1286 }
1287
1288 $i++;
1289 }
1290 } else {
1291 $error++;
1292 $this->error = $this->db->error().' sql='.$sql;
1293 }
1294 }
1295
1296 if (!$error) {
1297 // Remove Roles
1298 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1299 dol_syslog(__METHOD__, LOG_DEBUG);
1300 $resql = $this->db->query($sql);
1301 if (!$resql) {
1302 $error++;
1303 $this->error .= $this->db->lasterror();
1304 $errorflag = -1;
1305 }
1306 }
1307
1308 if (!$error) {
1309 // Remove Roles
1310 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1311 dol_syslog(__METHOD__, LOG_DEBUG);
1312 $resql = $this->db->query($sql);
1313 if (!$resql) {
1314 $error++;
1315 $this->error .= $this->db->lasterror();
1316 $errorflag = -1;
1317 }
1318 }
1319
1320 if (!$error) {
1321 // Remove category
1322 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1323 dol_syslog(__METHOD__, LOG_DEBUG);
1324 $resql = $this->db->query($sql);
1325 if (!$resql) {
1326 $error++;
1327 $this->error .= $this->db->lasterror();
1328 $errorflag = -1;
1329 }
1330 }
1331
1332 if (!$error) {
1333 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1334 $sql .= " WHERE rowid = ".((int) $this->id);
1335 dol_syslog(__METHOD__, LOG_DEBUG);
1336 $result = $this->db->query($sql);
1337 if (!$result) {
1338 $error++;
1339 $this->error = $this->db->error().' sql='.$sql;
1340 }
1341 }
1342
1343 // Removed extrafields
1344 if (!$error) {
1345 // For avoid conflicts if trigger used
1346 $result = $this->deleteExtraFields();
1347 if ($result < 0) {
1348 $error++;
1349 }
1350 }
1351
1352 if (!$error) {
1353 $this->db->commit();
1354 return 1;
1355 } else {
1356 $this->db->rollback();
1357 dol_syslog("Error ".$this->error, LOG_ERR);
1358 return -1;
1359 }
1360 }
1361
1362
1369 public function info($id)
1370 {
1371 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1372 $sql .= " c.tms as tms, c.fk_user_modif";
1373 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1374 $sql .= " WHERE c.rowid = ".((int) $id);
1375
1376 $resql = $this->db->query($sql);
1377 if ($resql) {
1378 if ($this->db->num_rows($resql)) {
1379 $obj = $this->db->fetch_object($resql);
1380
1381 $this->id = $obj->rowid;
1382
1383 if ($obj->fk_user_creat) {
1384 $cuser = new User($this->db);
1385 $cuser->fetch($obj->fk_user_creat);
1386 $this->user_creation = $cuser;
1387 }
1388
1389 if ($obj->fk_user_modif) {
1390 $muser = new User($this->db);
1391 $muser->fetch($obj->fk_user_modif);
1392 $this->user_modification = $muser;
1393 }
1394
1395 $this->date_creation = $this->db->jdate($obj->datec);
1396 $this->date_modification = $this->db->jdate($obj->tms);
1397 }
1398
1399 $this->db->free($resql);
1400 } else {
1401 print $this->db->error();
1402 }
1403 }
1404
1410 public function getNbOfEMailings()
1411 {
1412 $sql = "SELECT count(mc.email) as nb";
1413 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1414 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1415 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1416
1417 $resql = $this->db->query($sql);
1418 if ($resql) {
1419 $obj = $this->db->fetch_object($resql);
1420 $nb = $obj->nb;
1421
1422 $this->db->free($resql);
1423 return $nb;
1424 } else {
1425 $this->error = $this->db->error();
1426 return -1;
1427 }
1428 }
1429
1436 public function getTooltipContentArray($params)
1437 {
1438 global $conf, $langs, $user;
1439
1440 $datas = [];
1441
1442 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1443 return ['optimize' => $langs->trans("ShowContact")];
1444 }
1445 if (!empty($this->photo) && class_exists('Form')) {
1446 $photo = '<div class="photointooltip floatright">';
1447 $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.
1448 $photo .= '</div>';
1449 $datas['photo'] = $photo;
1450 }
1451
1452 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1453 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1454 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1455 if (!empty($this->poste)) {
1456 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1457 }
1458 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1459 $phonelist = array();
1460 $country_code = empty($this->country_code) ? '': $this->country_code;
1461 if ($this->phone_pro) {
1462 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1463 }
1464 if ($this->phone_mobile) {
1465 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1466 }
1467 if ($this->phone_perso) {
1468 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1469 }
1470 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1471 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1472
1473 return $datas;
1474 }
1475
1489 public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1490 {
1491 global $conf, $langs, $hookmanager;
1492
1493 if (!empty($conf->dol_no_mouse_hover)) {
1494 $notooltip = 1; // Force disable tooltips
1495 }
1496
1497 $result = '';
1498 $params = [
1499 'id' => $this->id,
1500 'objecttype' => $this->element,
1501 'option' => $option,
1502 ];
1503 $classfortooltip = 'classfortooltip';
1504 $dataparams = '';
1505 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1506 $classfortooltip = 'classforajaxtooltip';
1507 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1508 $label = '';
1509 } else {
1510 $label = implode($this->getTooltipContentArray($params));
1511 }
1512
1513 $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id;
1514
1515 if ($option !== 'nolink') {
1516 // Add param to save lastsearch_values or not
1517 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1518 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1519 $add_save_lastsearch_values = 1;
1520 }
1521 if ($url && $add_save_lastsearch_values) {
1522 $url .= '&save_lastsearch_values=1';
1523 }
1524 }
1525
1526 $url .= $moreparam;
1527
1528 $linkclose = '';
1529 if (empty($notooltip)) {
1530 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1531 $label = $langs->trans("ShowContact");
1532 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1533 }
1534 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1535 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1536 } else {
1537 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1538 }
1539
1540 if ($option == 'nolink' || empty($url)) {
1541 $linkstart = '<span';
1542 } else {
1543 $linkstart = '<a href="'.$url.'"';
1544 }
1545 $linkstart .= $linkclose.'>';
1546 if ($option == 'nolink' || empty($url)) {
1547 $linkend = '</span>';
1548 } else {
1549 $linkend = '</a>';
1550 }
1551
1552 $result .= $linkstart;
1553
1554 if ($withpicto) {
1555 if ($withpicto < 0) {
1556 $result .= '<!-- picto photo user --><span class="nopadding userimg'.($morecss ? ' '.$morecss : '').'">'.Form::showphoto('contact', $this, 0, 0, 0, 'userphoto'.($withpicto == -3 ? 'small' : ''), 'mini', 0, 1).'</span>';
1557 if ($withpicto != 2 && $withpicto != -2) {
1558 $result .= ' ';
1559 }
1560 } else {
1561 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="pictofixedwidth"' : '') : 'class="'.(($withpicto != 2) ? 'pictofixedwidth ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1562 }
1563 }
1564 if ($withpicto != 2 && $withpicto != -2) {
1565 $result .= '<span class="valigmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1566 }
1567
1568 $result .= $linkend;
1569
1570 global $action;
1571 $hookmanager->initHooks(array('contactdao'));
1572 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1573 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1574 if ($reshook > 0) {
1575 $result = $hookmanager->resPrint;
1576 } else {
1577 $result .= $hookmanager->resPrint;
1578 }
1579
1580 return $result;
1581 }
1582
1588 public function getCivilityLabel()
1589 {
1590 global $langs;
1591
1592 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1593 if (empty($code)) {
1594 return '';
1595 }
1596
1597 $langs->load("dict");
1598 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1599 }
1600
1607 public function getLibStatut($mode)
1608 {
1609 return $this->LibStatut($this->statut, $mode);
1610 }
1611
1612 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1620 public function LibStatut($status, $mode)
1621 {
1622 // phpcs:enable
1623 global $langs;
1624
1625 $labelStatus = array(
1626 0 => 'ActivityCeased',
1627 1 => 'InActivity',
1628 4 => 'InActivity',
1629 5 => 'ActivityCeased',
1630 );
1631 $labelStatusShort = array(
1632 0 => 'ActivityCeased',
1633 1 => 'InActivity',
1634 4 => 'InActivity',
1635 5 => 'ActivityCeased',
1636 );
1637
1638 $statusType = 'status4';
1639 if ($status == 0 || $status == 5) {
1640 $statusType = 'status5';
1641 }
1642
1643 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1644 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1645
1646 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1647 }
1648
1649
1650 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1657 public function LibPubPriv($status)
1658 {
1659 // phpcs:enable
1660 global $langs;
1661 if ($status == '1') {
1662 return $langs->trans('ContactPrivate');
1663 } else {
1664 return $langs->trans('ContactPublic');
1665 }
1666 }
1667
1668
1676 public function initAsSpecimen()
1677 {
1678 // Get first id of existing company and save it into $socid
1679 $socid = 0;
1680 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1681 $resql = $this->db->query($sql);
1682 if ($resql) {
1683 $obj = $this->db->fetch_object($resql);
1684 if ($obj) {
1685 $socid = $obj->rowid;
1686 }
1687 }
1688
1689 // Initialise parameters
1690 $this->id = 0;
1691 $this->entity = 1;
1692 $this->specimen = 1;
1693 $this->lastname = 'DOLIBARR';
1694 $this->firstname = 'SPECIMEN';
1695 $this->address = '21 jump street';
1696 $this->zip = '99999';
1697 $this->town = 'MyTown';
1698 $this->country_id = 1;
1699 $this->country_code = 'FR';
1700 $this->country = 'France';
1701 $this->email = 'specimen@specimen.com';
1702 $this->socialnetworks = array(
1703 'skype' => 'tom.hanson',
1704 );
1705 $this->phone_pro = '0909090901';
1706 $this->phone_perso = '0909090902';
1707 $this->phone_mobile = '0909090903';
1708 $this->fax = '0909090909';
1709
1710 $this->note_public = 'This is a comment (public)';
1711 $this->note_private = 'This is a comment (private)';
1712
1713 $this->socid = $socid;
1714 $this->statut = 1;
1715 return 1;
1716 }
1717
1724 public function setstatus($status)
1725 {
1726 global $conf, $langs, $user;
1727
1728 $error = 0;
1729
1730 // Check parameters
1731 if ($this->statut == $status) {
1732 return 0;
1733 } else {
1734 $this->statut = $status;
1735 }
1736
1737 $this->db->begin();
1738
1739 // Desactive utilisateur
1740 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1741 $sql .= " SET statut = ".((int) $this->statut);
1742 $sql .= ", fk_user_modif = ".((int) $user->id);
1743 $sql .= " WHERE rowid = ".((int) $this->id);
1744 $result = $this->db->query($sql);
1745
1746 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1747 if ($result) {
1748 // Call trigger
1749 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1750 if ($result < 0) {
1751 $error++;
1752 }
1753 // End call triggers
1754 }
1755
1756 if ($error) {
1757 $this->db->rollback();
1758 return -$error;
1759 } else {
1760 $this->db->commit();
1761 return 1;
1762 }
1763 }
1764
1775 public function setCategories($categories)
1776 {
1777 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1778 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1779 }
1780
1789 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1790 {
1791 $tables = array(
1792 'socpeople', 'societe_contacts'
1793 );
1794
1795 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1796 }
1797
1805 public function fetchRoles()
1806 {
1807 global $langs;
1808 $error = 0;
1809 $num = 0;
1810
1811 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1812 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
1813 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1814 $sql .= " AND tc.source = 'external' AND tc.active=1";
1815 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
1816 $sql .= " AND sc.entity IN (".getEntity('societe').')';
1817
1818 $resql = $this->db->query($sql);
1819 if ($resql) {
1820 $this->roles = array();
1821
1822 $num = $this->db->num_rows($resql);
1823 if ($num > 0) {
1824 while ($obj = $this->db->fetch_object($resql)) {
1825 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
1826 $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
1827 $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));
1828 }
1829 }
1830 } else {
1831 $error++;
1832 $this->error = $this->db->lasterror();
1833 $this->errors[] = $this->db->lasterror();
1834 }
1835
1836 if (empty($error)) {
1837 return $num;
1838 } else {
1839 return $error * -1;
1840 }
1841 }
1842
1850 public function getContactRoles($element = '')
1851 {
1852 $tab = array();
1853
1854 if ($element == 'action') {
1855 $element = 'agenda';
1856 }
1857
1858 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1859 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
1860 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
1861 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
1862 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1863 $sql .= " WHERE sc.fk_soc =".((int) $this->socid);
1864 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1865 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
1866 $sql .= " AND sp.entity IN (".getEntity('contact').")";
1867 $sql .= " AND tc.active = 1";
1868
1869 dol_syslog(__METHOD__, LOG_DEBUG);
1870 $resql = $this->db->query($sql);
1871 if ($resql) {
1872 $num = $this->db->num_rows($resql);
1873 $i = 0;
1874 while ($i < $num) {
1875 $obj = $this->db->fetch_object($resql);
1876 $tab[] = array('fk_socpeople'=>$obj->id, 'type_contact'=>$obj->fk_c_type_contact);
1877
1878 $i++;
1879 }
1880
1881 return $tab;
1882 } else {
1883 $this->error = $this->db->error();
1884 dol_print_error($this->db);
1885 return -1;
1886 }
1887 }
1888
1896 public function updateRoles()
1897 {
1898 global $conf;
1899
1900 $error = 0;
1901
1902 if (!isset($this->roles)) {
1903 return 0; // Avoid to loose roles when property not set
1904 }
1905
1906 $this->db->begin();
1907
1908 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
1909
1910 $result = $this->db->query($sql);
1911 if (!$result) {
1912 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1913 $error++;
1914 } else {
1915 if (count($this->roles) > 0) {
1916 foreach ($this->roles as $keyRoles => $valRoles) {
1917 $idrole = 0;
1918 if (is_array($valRoles)) {
1919 $idrole = $valRoles['id'];
1920 } else {
1921 $idrole = $valRoles;
1922 }
1923
1924 $socid = 0;
1925 if (is_array($valRoles)) {
1926 $socid = $valRoles['socid'];
1927 } else {
1928 $socid = $this->socid;
1929 }
1930
1931 if ($socid > 0) {
1932 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
1933 $sql .= " (entity,";
1934 $sql .= "date_creation,";
1935 $sql .= "fk_soc,";
1936 $sql .= "fk_c_type_contact,";
1937 $sql .= "fk_socpeople) ";
1938 $sql .= " VALUES (".$conf->entity.",";
1939 $sql .= "'".$this->db->idate(dol_now())."',";
1940 $sql .= $socid.", ";
1941 $sql .= $idrole." , ";
1942 $sql .= $this->id;
1943 $sql .= ")";
1944
1945 $result = $this->db->query($sql);
1946 if (!$result) {
1947 $this->errors[] = $this->db->lasterror().' sql='.$sql;
1948 $error++;
1949 }
1950 }
1951 }
1952 }
1953 }
1954 if (empty($error)) {
1955 $this->db->commit();
1956 return 1;
1957 } else {
1958 $this->error = implode(' ', $this->errors);
1959 $this->db->rollback();
1960 return $error * -1;
1961 }
1962 }
1963
1970 public function loadCacheOfProspStatus($active = 1)
1971 {
1972 global $langs;
1973
1974 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
1975 if ($active >= 0) {
1976 $sql .= " WHERE active = ".((int) $active);
1977 }
1978 $resql = $this->db->query($sql);
1979 $num = $this->db->num_rows($resql);
1980 $i = 0;
1981 while ($i < $num) {
1982 $obj = $this->db->fetch_object($resql);
1983 $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);
1984 $i++;
1985 }
1986 return 1;
1987 }
1988
1994 public function getLibProspLevel()
1995 {
1996 return $this->libProspLevel($this->fk_prospectlevel);
1997 }
1998
2005 public function libProspLevel($fk_prospectlevel)
2006 {
2007 global $langs;
2008
2009 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2010 // If lib not found in language file, we get label from cache/databse
2011 if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel)) {
2012 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2013 }
2014 return $lib;
2015 }
2016
2017
2025 public function setProspectLevel(User $user)
2026 {
2027 return $this->update($this->id, $user);
2028 }
2029
2037 public function getLibProspCommStatut($mode = 0, $label = '')
2038 {
2039 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2040 }
2041
2055 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2056 {
2057 global $langs;
2058 $langs->load('customers');
2059
2060 if ($mode == 2) {
2061 if ($statut == '-1' || $statut == 'ST_NO') {
2062 return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2063 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2064 return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2065 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2066 return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2067 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2068 return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2069 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2070 return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2071 } else {
2072 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);
2073 }
2074 }
2075 if ($mode == 3) {
2076 if ($statut == '-1' || $statut == 'ST_NO') {
2077 return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2078 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2079 return img_action($langs->trans("StatusProspect0"), 0, $picto);
2080 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2081 return img_action($langs->trans("StatusProspect1"), 1, $picto);
2082 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2083 return img_action($langs->trans("StatusProspect2"), 2, $picto);
2084 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2085 return img_action($langs->trans("StatusProspect3"), 3, $picto);
2086 } else {
2087 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);
2088 }
2089 }
2090 if ($mode == 4) {
2091 if ($statut == '-1' || $statut == 'ST_NO') {
2092 return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");
2093 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2094 return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");
2095 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2096 return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");
2097 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2098 return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");
2099 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2100 return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");
2101 } else {
2102 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);
2103 }
2104 }
2105
2106 return "Error, mode/status not found";
2107 }
2108
2109
2116 public function setNoEmail($no_email)
2117 {
2118 $error = 0;
2119
2120 // Update mass emailing flag into table mailing_unsubscribe
2121 if ($this->email) {
2122 $this->db->begin();
2123
2124 if ($no_email) {
2125 $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)."'";
2126 $resql = $this->db->query($sql);
2127 if ($resql) {
2128 $obj = $this->db->fetch_object($resql);
2129 $noemail = $obj->nb;
2130 if (empty($noemail)) {
2131 $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())."')";
2132 $resql = $this->db->query($sql);
2133 if (!$resql) {
2134 $error++;
2135 $this->error = $this->db->lasterror();
2136 $this->errors[] = $this->error;
2137 }
2138 }
2139 } else {
2140 $error++;
2141 $this->error = $this->db->lasterror();
2142 $this->errors[] = $this->error;
2143 }
2144 } else {
2145 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2146 $resql = $this->db->query($sql);
2147 if (!$resql) {
2148 $error++;
2149 $this->error = $this->db->lasterror();
2150 $this->errors[] = $this->error;
2151 }
2152 }
2153
2154 if (empty($error)) {
2155 $this->no_email = $no_email;
2156 $this->db->commit();
2157 return 1;
2158 } else {
2159 $this->db->rollback();
2160 return $error * -1;
2161 }
2162 }
2163
2164 return 0;
2165 }
2166
2173 public function getNoEmail()
2174 {
2175 if ($this->email) {
2176 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2177 $resql = $this->db->query($sql);
2178 if ($resql) {
2179 $obj = $this->db->fetch_object($resql);
2180 $this->no_email = $obj->nb;
2181 return 1;
2182 } else {
2183 $this->error = $this->db->lasterror();
2184 $this->errors[] = $this->error;
2185 return -1;
2186 }
2187 }
2188 return 0;
2189 }
2190
2191
2199 public function getKanbanView($option = '', $arraydata = null)
2200 {
2201 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2202
2203 $return = '<div class="box-flex-item box-flex-grow-zero">';
2204 $return .= '<div class="info-box info-box-sm">';
2205 $return .= '<span class="info-box-icon bg-infobox-action">';
2206 //var_dump($this->photo);exit;
2207 if (property_exists($this, 'photo') && !is_null($this->photo)) {
2208 $return.= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2209 } else {
2210 $return .= img_picto('', $this->picto);
2211 }
2212 $return .= '</span>';
2213 $return .= '<div class="info-box-content">';
2214 $return .= '<div class="info-box-ref">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</div>';
2215 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2216
2217 if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2218 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
2219 }
2220 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2221 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2222 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2223 }*/
2224 /*if (method_exists($this, 'LibPubPriv')) {
2225 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2226 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2227 }*/
2228 if (method_exists($this, 'getLibStatut')) {
2229 $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
2230 }
2231 $return .= '</div>';
2232 $return .= '</div>';
2233 $return .= '</div>';
2234 return $return;
2235 }
2236}
$object ref
Definition info.php:78
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.
load_state_board()
Load indicators into this->nb for board.
create($user, $notrigger=0)
Add a contact into database.
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 attribut to 1 or 0
getNbOfEMailings()
Return number of mass Emailing received by this 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.
fetch($id, $user=null, $ref_ext='', $email='', $loadalsoroles=0)
Load object contact.
setProspectLevel(User $user)
Set prospect level.
getKanbanView($option='', $arraydata=null)
Return clicable 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 prostect 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)
Retourne chaine DN complete dans l'annuaire LDAP pour l'objet.
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 informations into database.
setstatus($status)
Change status of a user.
loadCacheOfProspStatus($active=1)
Load array of prospect status.
_load_ldap_info()
Initialise tableau info (tableau des attributs LDAP)
updateRoles()
Updates all roles (default contact for companies) according to values inside the ->roles array.
info($id)
Charge les informations sur le contact, depuis la base.
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.
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)
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
print $langs trans("Ref").' m m m statut
Definition index.php:152
trait CommonPeople
Superclass for thirdparties, contacts, members or 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.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_format_address($object, $withcountry=0, $sep="\n", $outputlangs='', $mode=0, $extralangcode='')
Return a formated address (part address/zip/town/state) according to country rules.
img_action($titlealt, $numaction, $picto='', $moreatt='')
Show logo action.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0)
Format phone numbers according to country.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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.
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:123