dolibarr 25.0.0-alpha
contact.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
4 * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerker@telenet.be>
7 * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
8 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
9 * Copyright (C) 2013 Alexandre Spangaro <aspangaro@open-dsi.fr>
10 * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
11 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
12 * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
13 * Copyright (C) 2020 Open-Dsi <support@open-dsi.fr>
14 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
15 * Copyright (C) 2024-2026 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
53 public $TRIGGER_PREFIX = 'CONTACT';
54
62 const MERGE_FIELDS_FILL_IF_EMPTY = array(
63 'civility_code', 'lastname', 'firstname', 'name_alias', 'address', 'zip', 'town',
64 'state_id', 'country_id', 'poste', 'phone_pro', 'phone_perso', 'phone_mobile', 'fax',
65 'email', 'socialnetworks', 'birthday', 'default_lang', 'ref_ext',
66 'fk_prospectlevel', 'stcomm_id', 'socid'
67 );
68
72 const MERGE_FIELDS_CONCAT = array('note_public', 'note_private');
73
78 const MERGE_MAX_PARENT_DEPTH = 100;
79
83 public $element = 'contact';
84
88 public $table_element = 'socpeople';
89
93 public $picto = 'contact';
94
119 // BEGIN MODULEBUILDER PROPERTIES
123 public $fields = array(
124 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'),
125 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 30, 'index' => 1),
126 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 35),
127 'civility' => array('type' => 'varchar(6)', 'label' => 'Civility', 'enabled' => 1, 'visible' => 3, 'position' => 40),
128 'lastname' => array('type' => 'varchar(50)', 'label' => 'Lastname', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'showoncombobox' => 1, 'searchall' => 1),
129 //'name_alias' => array('type' => 'varchar(255)', 'label' => 'Name alias', 'enabled' => 1, 'visible' => -1, 'position' => 46, 'searchall' => 1),
130 'firstname' => array('type' => 'varchar(50)', 'label' => 'Firstname', 'enabled' => 1, 'visible' => 1, 'position' => 50, 'showoncombobox' => 1, 'searchall' => 1),
131 'poste' => array('type' => 'varchar(80)', 'label' => 'PostOrFunction', 'enabled' => 1, 'visible' => -1, 'position' => 52),
132 'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 55),
133 'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 60),
134 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 65),
135 'fk_departement' => array('type' => 'integer', 'label' => 'State', 'enabled' => 1, 'visible' => 3, 'position' => 70),
136 'fk_pays' => array('type' => 'integer', 'label' => 'Country', 'enabled' => 1, 'visible' => 3, 'position' => 75),
137 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => 1, 'position' => 77, 'searchall' => 1),
138 'birthday' => array('type' => 'date', 'label' => 'Birthday', 'enabled' => 1, 'visible' => -1, 'position' => 80),
139 'phone' => array('type' => 'varchar(30)', 'label' => 'Phone', 'enabled' => 1, 'visible' => 1, 'position' => 90, 'searchall' => 1),
140 'phone_perso' => array('type' => 'varchar(30)', 'label' => 'PhonePerso', 'enabled' => 1, 'visible' => -1, 'position' => 95, 'searchall' => 1),
141 'phone_mobile' => array('type' => 'varchar(30)', 'label' => 'PhoneMobile', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'searchall' => 1),
142 'fax' => array('type' => 'varchar(30)', 'label' => 'Fax', 'enabled' => 1, 'visible' => -1, 'position' => 105, 'searchall' => 1),
143 'email' => array('type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'visible' => 1, 'position' => 110, 'searchall' => 1),
144 'socialnetworks' => array('type' => 'text', 'label' => 'SocialNetworks', 'enabled' => 1, 'visible' => 3, 'position' => 115),
145 'photo' => array('type' => 'varchar(255)', 'label' => 'Photo', 'enabled' => 1, 'visible' => 3, 'position' => 170),
146 'priv' => array('type' => 'smallint(6)', 'label' => 'ContactVisibility', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 175),
147 'fk_stcommcontact' => array('type' => 'integer', 'label' => 'ProspectStatus', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 220),
148 'fk_prospectcontactlevel' => array('type' => 'varchar(12)', 'label' => 'ProspectLevel', 'enabled' => 1, 'visible' => -1, 'position' => 255),
149 //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),
150 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 195, 'searchall' => 1),
151 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 200, 'searchall' => 1),
152 'default_lang' => array('type' => 'varchar(6)', 'label' => 'Default lang', 'enabled' => 1, 'visible' => 3, 'position' => 205),
153 'canvas' => array('type' => 'varchar(32)', 'label' => 'Canvas', 'enabled' => 1, 'visible' => 3, 'position' => 210),
154 'ip' => array('type' => 'ip', 'label' => 'IPAddress', 'enabled' => '1', 'position' => 700, 'notnull' => 0, 'visible' => '-2', 'comment' => 'ip used to create record (for public submission page)'),
155 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 300),
156 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305),
157 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 3, 'position' => 310),
158 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 3, 'position' => 315),
159 'statut' => array('type' => 'tinyint(4)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500),
160 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000),
161 );
162
166 public $civility_id; // In fact we store civility_code
170 public $civility_code;
174 public $civility;
175
179 public $gender;
180
184 public $birthday_alert;
185
191 public $civilite;
192
196 public $fullname;
197
201 public $name_alias;
202
206 public $address;
207
211 public $zip;
212
216 public $town;
217
221 public $state_id;
222
226 public $state_code;
227
231 public $state;
232
236 public $poste;
237
241 public $socid; // both socid and fk_soc are used
245 public $fk_soc; // both socid and fk_soc are used
246
250 public $socname;
251
256 public $statut;
257
261 public $code;
262
267 public $email;
268
275 public $mail;
276
281 public $url;
282
288 public $no_email;
289
294 public $socialnetworks;
295
299 public $photo;
300
304 public $phone_pro;
305
309 public $phone_perso;
310
314 public $phone_mobile;
315
319 public $fax;
320
325 public $priv;
326
330 public $birthday;
331
335 public $default_lang;
336
340 public $ref_facturation;
341
345 public $ref_contrat;
346
350 public $ref_commande;
351
355 public $ref_propal;
356
360 public $user_id;
361
365 public $user_login;
366
370 public $ip;
371 // END MODULEBUILDER PROPERTIES
372
376 public $roles;
377
381 public $cacheprospectstatus = array();
382
386 public $fk_prospectlevel;
387
391 public $stcomm_id;
392
396 public $statut_commercial;
397
401 public $stcomm_picto;
402
403
409 public function __construct($db)
410 {
411 $this->db = $db;
412 $this->statut = 1; // By default, status is enabled
413 $this->status = 1; // By default, status is enabled
414 $this->ismultientitymanaged = 1;
415 $this->isextrafieldmanaged = 1;
416
417 $this->fields['ref_ext']['visible'] = getDolGlobalInt('MAIN_LIST_SHOW_REF_EXT');
418
419 if (!isModEnabled('mailing')) {
420 $this->fields['no_email']['enabled'] = 0;
421 }
422 // typical ['s.nom'] is used for third-parties
423 if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) {
424 $this->fields['fk_soc']['enabled'] = 0;
425 $this->fields['fk_soc']['searchall'] = 0;
426 }
427
428 // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
429 if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) { // Default behaviour
430 $this->fields['fk_stcommcontact']['enabled'] = 0;
431 $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
432 }
433
434 // Unset fields that are disabled
435 foreach ($this->fields as $key => $val) {
436 if (isset($val['enabled']) && empty($val['enabled'])) {
437 unset($this->fields[$key]);
438 }
439 }
440
441 // Translate some data of arrayofkeyval
442 /*if (is_object($langs)) {
443 foreach($this->fields as $key => $val) {
444 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
445 foreach($val['arrayofkeyval'] as $key2 => $val2) {
446 $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
447 }
448 }
449 }
450 }*/
451 }
452
458 public function loadStateBoard()
459 {
460 global $user, $hookmanager;
461
462 $this->nb = array();
463 $clause = "WHERE";
464
465 $sql = "SELECT count(sp.rowid) as nb";
466 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
467 if (!$user->hasRight('societe', 'client', 'voir')) {
468 $sql .= ", ".MAIN_DB_PREFIX."societe as s";
469 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
470 $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
471 $clause = "AND";
472 }
473 $sql .= " ".$clause." sp.entity IN (".getEntity($this->element).")";
474 $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = ".((int) $user->id)."))";
475 if ($user->socid > 0) {
476 $sql .= " AND sp.fk_soc = ".((int) $user->socid);
477 }
478 // Add where from hooks
479 if (is_object($hookmanager)) {
480 $parameters = array();
481 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
482 $sql .= $hookmanager->resPrint;
483 }
484
485 $resql = $this->db->query($sql);
486 if ($resql) {
487 while ($obj = $this->db->fetch_object($resql)) {
488 $this->nb["contacts"] = $obj->nb;
489 }
490 $this->db->free($resql);
491 return 1;
492 } else {
493 dol_print_error($this->db);
494 $this->error = $this->db->lasterror();
495 return -1;
496 }
497 }
498
506 public function create($user, $notrigger = 0)
507 {
508 $error = 0;
509 $now = dol_now();
510
511 if (empty($this->date_creation)) {
512 $this->date_creation = $now;
513 }
514
515 $this->db->begin();
516
517 // Clean parameters
518 $this->name_alias = trim((string) $this->name_alias);
519 $this->lastname = $this->lastname ? trim((string) $this->lastname) : trim((string) $this->name);
520 $this->firstname = trim((string) $this->firstname);
521 $this->setUpperOrLowerCase();
522 if (empty($this->socid)) {
523 $this->socid = 0;
524 }
525 if (empty($this->priv)) {
526 $this->priv = 0;
527 }
528 if (!empty($this->statut) && empty($this->status)) {
529 $this->status = 1;
530 }
531 if (empty($this->status)) {
532 $this->status = 0; // This is to convert '' into '0' to avoid bad sql request
533 $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
534 }
535
536 // setEntity will set entity with the right value if empty or change it for the right value if multicompany module is active
537 $this->entity = setEntity($this);
538
539 $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";
540 $sql .= " datec";
541 $sql .= ", fk_soc";
542 $sql .= ", name_alias";
543 $sql .= ", lastname";
544 $sql .= ", firstname";
545 $sql .= ", fk_user_creat";
546 $sql .= ", priv";
547 $sql .= ", fk_stcommcontact";
548 $sql .= ", statut";
549 $sql .= ", canvas";
550 $sql .= ", entity";
551 $sql .= ", ref_ext";
552 $sql .= ", import_key";
553 $sql .= ", ip";
554 $sql .= ") VALUES (";
555 $sql .= "'".$this->db->idate($now)."',";
556 if ($this->socid > 0) {
557 $sql .= " ".((int) $this->socid).",";
558 } else {
559 $sql .= "null,";
560 }
561 $sql .= "'".$this->db->escape($this->name_alias)."',";
562 $sql .= "'".$this->db->escape($this->lastname)."',";
563 $sql .= "'".$this->db->escape($this->firstname)."',";
564 $sql .= " ".($user->id > 0 ? ((int) $user->id) : "null").",";
565 $sql .= " ".((int) $this->priv).",";
566 $sql .= " 0,";
567 $sql .= " ".((int) $this->status).",";
568 $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";
569 $sql .= " ".((int) $this->entity).",";
570 $sql .= "'".$this->db->escape((string) $this->ref_ext)."',";
571 $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").",";
572 $sql .= " ".(!empty($this->ip) ? "'".$this->db->escape($this->ip)."'" : "null");
573 $sql .= ")";
574
575 dol_syslog(get_class($this)."::create", LOG_DEBUG);
576 $resql = $this->db->query($sql);
577 if ($resql) {
578 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople");
579
580 $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
581 if ($result < 0) {
582 $error++;
583 $this->error = $this->db->lasterror();
584 }
585
586 if (!$error) {
587 $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
588 if ($result < 0) {
589 $error++;
590 $this->error = $this->db->lasterror();
591 }
592 }
593
594 if (!$error && !$notrigger) {
595 // Call trigger
596 $result = $this->call_trigger('CONTACT_CREATE', $user);
597 if ($result < 0) {
598 $error++;
599 }
600 // End call triggers
601 }
602
603 if (!$error) {
604 $this->db->commit();
605 return $this->id;
606 } else {
607 $this->db->rollback();
608 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
609 return -2;
610 }
611 } else {
612 $this->error = $this->db->lasterror();
613
614 $this->db->rollback();
615 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
616 return -1;
617 }
618 }
619
630 public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
631 {
632 global $conf;
633
634 if (empty($this->country_id) && !empty($this->country_code)) {
635 $country_id = getCountry($this->country_code, '3');
636 $this->country_id = is_int($country_id) ? $country_id : 0;
637 }
638
639 $error = 0;
640
641 $this->id = $id;
642
643 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
644
645 // Clean parameters
646 $this->ref_ext = (empty($this->ref_ext) ? '' : trim((string) $this->ref_ext));
647 $this->name_alias = trim((string) $this->name_alias);
648 $this->lastname = $this->lastname ? trim((string) $this->lastname) : trim((string) $this->name);
649 $this->firstname = trim((string) $this->firstname);
650 $this->email = trim($this->email ?? '');
651 $this->phone_pro = trim((string) $this->phone_pro);
652 $this->phone_perso = trim((string) $this->phone_perso);
653 $this->phone_mobile = trim((string) $this->phone_mobile);
654 $this->photo = trim((string) $this->photo);
655 $this->fax = trim((string) $this->fax);
656 $this->zip = (empty($this->zip) ? '' : trim((string) $this->zip));
657 $this->town = (empty($this->town) ? '' : trim((string) $this->town));
658 $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
659 if (!empty($this->statut) && empty($this->status)) {
660 $this->status = 1;
661 }
662 if (empty($this->status)) {
663 $this->status = 0;
664 $this->statut = 0;
665 }
666 if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
667 $this->civility_code = $this->civility_id; // For backward compatibility
668 }
669 $this->setUpperOrLowerCase();
670
671 $this->db->begin();
672
673 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
674 if ($this->socid > 0) {
675 $sql .= " fk_soc = ".((int) $this->socid).",";
676 } elseif ($this->socid == -1) {
677 $sql .= " fk_soc = NULL,";
678 }
679 $sql .= " civility='".$this->db->escape($this->civility_code)."'";
680 $sql .= ", name_alias='".$this->db->escape($this->name_alias)."'";
681 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
682 $sql .= ", firstname='".$this->db->escape($this->firstname)."'";
683 $sql .= ", address='".$this->db->escape((string) $this->address)."'";
684 $sql .= ", zip='".$this->db->escape($this->zip)."'";
685 $sql .= ", town='".$this->db->escape($this->town)."'";
686 $sql .= ", ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "NULL");
687 $sql .= ", fk_pays=".($this->country_id > 0 ? ((int) $this->country_id) : 'NULL');
688 $sql .= ", fk_departement=".($this->state_id > 0 ? ((int) $this->state_id) : 'NULL');
689 $sql .= ", poste='".$this->db->escape($this->poste)."'";
690 $sql .= ", fax='".$this->db->escape($this->fax)."'";
691 $sql .= ", email='".$this->db->escape($this->email)."'";
692 $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
693 $sql .= ", photo='".$this->db->escape($this->photo)."'";
694 $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
695 $sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "NULL");
696 $sql .= ", note_public = ".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "NULL");
697 $sql .= ", phone = ".(isset($this->phone_pro) ? "'".$this->db->escape($this->phone_pro)."'" : "NULL");
698 $sql .= ", phone_perso = ".(isset($this->phone_perso) ? "'".$this->db->escape($this->phone_perso)."'" : "NULL");
699 $sql .= ", phone_mobile = ".(isset($this->phone_mobile) ? "'".$this->db->escape($this->phone_mobile)."'" : "NULL");
700 $sql .= ", priv = ".((int) $this->priv);
701 $sql .= ", fk_prospectlevel = '".$this->db->escape($this->fk_prospectlevel)."'";
702 if (isset($this->stcomm_id)) {
703 $sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? ((int) $this->stcomm_id) : "0");
704 }
705 $sql .= ", statut = ".((int) $this->status);
706 $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape((string) $user->id)."'" : "NULL");
707 $sql .= ", default_lang=".($this->default_lang ? "'".$this->db->escape($this->default_lang)."'" : "NULL");
708 $sql .= ", entity = ".((int) $this->entity);
709 $sql .= " WHERE rowid = ".((int) $id);
710
711 dol_syslog(get_class($this)."::update", LOG_DEBUG);
712 $result = $this->db->query($sql);
713 if ($result) {
714 unset($this->country_code);
715 unset($this->country);
716 unset($this->state_code);
717 unset($this->state);
718
719 $action = 'update';
720
721 // Actions on extra fields
722 $result = $this->insertExtraFields();
723 if ($result < 0) {
724 $error++;
725 }
726
727 if (!$error) {
728 $result = $this->updateRoles();
729 if ($result < 0) {
730 $error++;
731 }
732 }
733
734 if (!$error && $this->user_id > 0 && !$nosyncuser) {
735 // If contact is linked to a user
736 $tmpobj = new User($this->db);
737 $tmpobj->fetch($this->user_id);
738 $usermustbemodified = 0;
739 if ($tmpobj->office_phone != $this->phone_pro) {
740 $tmpobj->office_phone = $this->phone_pro;
741 $usermustbemodified++;
742 }
743 if ($tmpobj->office_fax != $this->fax) {
744 $tmpobj->office_fax = $this->fax;
745 $usermustbemodified++;
746 }
747 if ($tmpobj->address != $this->address) {
748 $tmpobj->address = $this->address;
749 $usermustbemodified++;
750 }
751 if ($tmpobj->town != $this->town) {
752 $tmpobj->town = $this->town;
753 $usermustbemodified++;
754 }
755 if ($tmpobj->zip != $this->zip) {
756 $tmpobj->zip = $this->zip;
757 $usermustbemodified++;
758 }
759 if ($tmpobj->state_id != $this->state_id) {
760 $tmpobj->state_id = $this->state_id;
761 $usermustbemodified++;
762 }
763 if ($tmpobj->country_id != $this->country_id) {
764 $tmpobj->country_id = $this->country_id;
765 $usermustbemodified++;
766 }
767 if ($tmpobj->email != $this->email) {
768 $tmpobj->email = $this->email;
769 $usermustbemodified++;
770 }
771 $usersocialnetworks = (is_array($tmpobj->socialnetworks) ? $tmpobj->socialnetworks : array());
772 $contactsocialnetworks = (is_array($this->socialnetworks) ? $this->socialnetworks : array());
773 // Compare in both directions, so a network added on the contact is also seen as a difference
774 if (!empty(array_diff_assoc($usersocialnetworks, $contactsocialnetworks)) || !empty(array_diff_assoc($contactsocialnetworks, $usersocialnetworks))) {
775 $tmpobj->socialnetworks = $this->socialnetworks;
776 $usermustbemodified++;
777 }
778 if ($usermustbemodified) {
779 $result = $tmpobj->update($user, 0, 1, 1, 1);
780 if ($result < 0) {
781 $error++;
782 }
783 }
784 }
785
786 if (!$error && !$notrigger) {
787 // Call trigger
788 $result = $this->call_trigger('CONTACT_MODIFY', $user);
789 if ($result < 0) {
790 $error++;
791 }
792 // End call triggers
793 }
794
795 if (!$error) {
796 $this->db->commit();
797 return 1;
798 } else {
799 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
800 $this->db->rollback();
801 return -$error;
802 }
803 } else {
804 $this->error = $this->db->lasterror().' sql='.$sql;
805 $this->db->rollback();
806 return -1;
807 }
808 }
809
810
811 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
812 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
822 public function _load_ldap_dn($info, $mode = 0)
823 {
824 // phpcs:enable
825 global $conf;
826 $dn = '';
827 if ($mode == 0) {
828 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')]."," . getDolGlobalString('LDAP_CONTACT_DN');
829 } elseif ($mode == 1) {
830 $dn = getDolGlobalString('LDAP_CONTACT_DN');
831 } elseif ($mode == 2) {
832 $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=".$info[getDolGlobalString('LDAP_KEY_CONTACTS')];
833 }
834 return $dn;
835 }
836
837
838 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
839 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
845 public function _load_ldap_info()
846 {
847 // phpcs:enable
848 global $conf, $langs;
849
850 $info = array();
851
852 // Object classes
853 $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
854
855 $this->fullname = $this->getFullName($langs);
856
857 // Fields
858 if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
859 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
860 }
861 if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
862 $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
863 }
864 if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
865 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
866 }
867
868 if ($this->poste) {
869 $info["title"] = $this->poste;
870 }
871 if ($this->socid > 0) {
872 $soc = new Societe($this->db);
873 $soc->fetch($this->socid);
874
875 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
876 if ($soc->client == 1) {
877 $info["businessCategory"] = "Customers";
878 }
879 if ($soc->client == 2) {
880 $info["businessCategory"] = "Prospects";
881 }
882 if ($soc->fournisseur == 1) {
883 $info["businessCategory"] = "Suppliers";
884 }
885 }
886 if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
887 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
888 }
889 if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
890 $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
891 }
892 if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
893 $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
894 }
895 if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
896 $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
897 }
898 if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
899 $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
900 }
901 if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
902 $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
903 }
904 if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
905 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
906 }
907 if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
908 $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
909 }
910 if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
911 $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
912 }
913 if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
914 $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
915 }
916
917 if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
918 $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
919
920 $info['uidnumber'] = $this->id;
921
922 $info['phpgwTz'] = 0;
923 $info['phpgwMailType'] = 'INTERNET';
924 $info['phpgwMailHomeType'] = 'INTERNET';
925
926 $info["phpgwContactTypeId"] = 'n';
927 $info["phpgwContactCatId"] = 0;
928 $info["phpgwContactAccess"] = "public";
929
930 $info["phpgwContactOwner"] = 1;
931
932 if ($this->email) {
933 $info["rfc822Mailbox"] = $this->email;
934 }
935 if ($this->phone_mobile) {
936 $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
937 }
938 }
939
940 return $info;
941 }
942
943
944 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
953 public function update_perso($id, $user = null, $notrigger = 0)
954 {
955 // phpcs:enable
956 $error = 0;
957 $result = false;
958
959 $this->db->begin();
960
961 // Update the contact
962 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";
963 $sql .= " birthday = ".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");
964 $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
965 if ($user) {
966 $sql .= ", fk_user_modif = ".((int) $user->id);
967 }
968 $sql .= " WHERE rowid = ".((int) $id);
969
970 dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG);
971 $resql = $this->db->query($sql);
972 if (!$resql) {
973 $error++;
974 $this->error = $this->db->lasterror();
975 }
976
977 if ($user) {
978 // Update birthday alert
979 if (!empty($this->birthday_alert)) {
980 //check existing
981 $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
982 $result_check = $this->db->query($sql_check);
983 if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
984 //insert
985 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
986 $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
987 $result = $this->db->query($sql);
988 if (!$result) {
989 $error++;
990 $this->error = $this->db->lasterror();
991 }
992 } else {
993 $result = true;
994 }
995 } else {
996 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
997 $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
998 $result = $this->db->query($sql);
999 if (!$result) {
1000 $error++;
1001 $this->error = $this->db->lasterror();
1002 }
1003 }
1004
1005 if (!$error && !$notrigger) {
1006 // Call trigger
1007 $result = $this->call_trigger('CONTACT_MODIFY', $user);
1008 if ($result < 0) {
1009 $error++;
1010 }
1011 // End call triggers
1012 }
1013 }
1014
1015 if (!$error) {
1016 $this->db->commit();
1017 return 1;
1018 } else {
1019 dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);
1020 $this->db->rollback();
1021 return -$error;
1022 }
1023 }
1024
1025
1037 public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
1038 {
1039 global $langs;
1040
1041 dol_syslog(get_class($this)."::fetch id=".$id." ref_ext=".$ref_ext." email=".$email, LOG_DEBUG);
1042
1043 if (empty($id) && empty($ref_ext) && empty($email)) {
1044 $this->error = 'BadParameter';
1045 return -1;
1046 }
1047
1048 // $langs may be null when called from a page with NOREQUIRETRAN (like viewimage.php)
1049 if (is_object($langs)) {
1050 $langs->loadLangs(array("dict", "companies"));
1051 }
1052
1053 $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1054 $sql .= " c.address, c.statut as status, c.zip, c.town,";
1055 $sql .= " c.fk_pays as country_id,";
1056 $sql .= " c.fk_departement as state_id,";
1057 $sql .= " c.birthday,";
1058 $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1059 $sql .= " c.socialnetworks,";
1060 $sql .= " c.photo,";
1061 $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1062 $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1063 $sql .= " c.import_key,";
1064 $sql .= " c.datec as date_creation, GREATEST(c.tms, COALESCE(cef.tms, c.tms)) as date_modification, c.fk_user_creat, c.fk_user_modif,";
1065 $sql .= " co.label as country, co.code as country_code,";
1066 $sql .= " d.nom as state, d.code_departement as state_code,";
1067 $sql .= " u.rowid as user_id, u.login as user_login,";
1068 $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1069 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1070 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as cef ON cef.fk_object=c.rowid";
1071 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
1072 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
1073 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
1074 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
1075 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1076 if ($id) {
1077 $sql .= " WHERE c.rowid = ".((int) $id);
1078 } else {
1079 $sql .= " WHERE c.entity IN (".getEntity($this->element).")";
1080 if ($ref_ext) {
1081 $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";
1082 }
1083 if ($email) {
1084 $sql .= " AND c.email = '".$this->db->escape($email)."'";
1085 }
1086 if ($socid) {
1087 $sql .= " AND c.fk_soc = ".((int) $socid);
1088 }
1089 }
1090
1091 $resql = $this->db->query($sql);
1092 if ($resql) {
1093 $num = $this->db->num_rows($resql);
1094 if ($num > 1) {
1095 $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1096 dol_syslog($this->error, LOG_ERR);
1097
1098 return 2;
1099 } elseif ($num) { // $num = 1
1100 $obj = $this->db->fetch_object($resql);
1101
1102 $this->id = $obj->rowid;
1103 $this->entity = $obj->entity;
1104 $this->ref = $obj->rowid;
1105 $this->ref_ext = $obj->ref_ext;
1106
1107 $this->civility_code = $obj->civility_code;
1108 $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != "Civility".$obj->civility_code ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
1109
1110 $this->name_alias = $obj->name_alias;
1111 $this->lastname = $obj->lastname;
1112 $this->firstname = $obj->firstname;
1113 $this->address = $obj->address;
1114 $this->zip = $obj->zip;
1115 $this->town = $obj->town;
1116
1117 $this->date_creation = $this->db->jdate($obj->date_creation);
1118 $this->date_modification = $this->db->jdate($obj->date_modification);
1119 $this->user_creation_id = $obj->fk_user_creat;
1120 $this->user_modification_id = $obj->fk_user_modif;
1121
1122 $this->state_id = $obj->state_id;
1123 $this->state_code = $obj->state_code;
1124 $this->state = $obj->state;
1125
1126 $this->country_id = $obj->country_id;
1127 $this->country_code = $obj->country_id ? $obj->country_code : '';
1128 $this->country = $obj->country_id ? ($langs->trans('Country'.$obj->country_code) != 'Country'.$obj->country_code ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
1129
1130 $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used
1131 $this->socid = $obj->fk_soc; // Both fk_soc and socid are used
1132 $this->socname = $obj->socname;
1133 $this->poste = $obj->poste;
1134 $this->status = $obj->status;
1135 $this->statut = $obj->status; // deprecated
1136
1137 $this->fk_prospectlevel = $obj->fk_prospectlevel;
1138
1139 $transcode = (is_object($langs) ? $langs->trans('StatusProspect'.$obj->fk_stcommcontact) : 'StatusProspect'.$obj->fk_stcommcontact);
1140 $label_sale_status = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1141 $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1142 $this->statut_commercial = $label_sale_status; // libelle statut commercial
1143 $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1144
1145 $this->phone_pro = trim((string) $obj->phone);
1146 $this->fax = trim((string) $obj->fax);
1147 $this->phone_perso = trim((string) $obj->phone_perso);
1148 $this->phone_mobile = trim((string) $obj->phone_mobile);
1149
1150 $this->email = $obj->email;
1151 $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1152 $this->photo = $obj->photo;
1153 $this->priv = $obj->priv;
1154 $this->mail = $obj->email;
1155
1156 $this->birthday = $this->db->jdate($obj->birthday);
1157 $this->note = $obj->note_private; // deprecated
1158 $this->note_private = $obj->note_private;
1159 $this->note_public = $obj->note_public;
1160 $this->default_lang = $obj->default_lang;
1161 $this->user_id = $obj->user_id;
1162 $this->user_login = $obj->user_login;
1163 $this->canvas = $obj->canvas;
1164
1165 $this->import_key = $obj->import_key;
1166
1167 // Define gender according to civility
1168 $this->setGenderFromCivility();
1169
1170 // Search Dolibarr user linked to this contact
1171 $sql = "SELECT u.rowid ";
1172 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1173 $sql .= " WHERE u.fk_socpeople = ".((int) $this->id);
1174
1175 $resql = $this->db->query($sql);
1176 if ($resql) {
1177 if ($this->db->num_rows($resql)) {
1178 $uobj = $this->db->fetch_object($resql);
1179
1180 $this->user_id = $uobj->rowid;
1181 }
1182 $this->db->free($resql);
1183 } else {
1184 $this->error = $this->db->error();
1185 return -1;
1186 }
1187
1188 // Retrieve all extrafield
1189 // fetch optionals attributes and labels
1190 $this->fetch_optionals();
1191
1192 // Load also alerts of this user
1193 if ($user) {
1194 $sql = "SELECT fk_user";
1195 $sql .= " FROM ".MAIN_DB_PREFIX."user_alert";
1196 $sql .= " WHERE fk_user = ".((int) $user->id)." AND fk_contact = ".((int) $id);
1197
1198 $resql = $this->db->query($sql);
1199 if ($resql) {
1200 if ($this->db->num_rows($resql)) {
1201 $obj = $this->db->fetch_object($resql);
1202
1203 $this->birthday_alert = 1;
1204 }
1205 $this->db->free($resql);
1206 } else {
1207 $this->error = $this->db->error();
1208 return -1;
1209 }
1210 }
1211
1212 // Load also roles of this address
1213 if ($loadalsoroles) {
1214 $resultRole = $this->fetchRoles();
1215 if ($resultRole < 0) {
1216 return $resultRole;
1217 }
1218 }
1219
1220 return $this->id;
1221 } else {
1222 $langs->load('errors');
1223 $this->error = $langs->trans("ErrorRecordNotFound");
1224 return 0;
1225 }
1226 } else {
1227 $this->error = $this->db->error();
1228 return -1;
1229 }
1230 }
1231
1245 public function findNearest($id = 0, $lastname = '', $firstname = '', $ref_ext = '', $email = '', $ref_alias = '', $socid = 0)
1246 {
1247 // A rowid is known, it is a unique key so we found it
1248 if ($id) {
1249 return $id;
1250 }
1251
1252 // We try to find the contact with exact matching on all fields
1253 // TODO Replace this with step by step search
1254 // Then search on email
1255 // Then search on lastname + firstname
1256 // Then search ref_ext or alias with a OR
1257 $tmpcontact = new Contact($this->db);
1258 $result = $tmpcontact->fetch($id, null, $ref_ext, $email, 0, $socid);
1259
1260 return $result;
1261 }
1262
1263
1264
1271 public function setGenderFromCivility()
1272 {
1273 unset($this->gender);
1274
1275 if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1276 $this->gender = 'man';
1277 } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1278 $this->gender = 'woman';
1279 }
1280 }
1281
1282 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1292 public function load_ref_elements()
1293 {
1294 // phpcs:enable
1295 // Count the elements for which it is contact
1296 $sql = "SELECT tc.element, count(ec.rowid) as nb";
1297 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
1298 $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1299 $sql .= " AND fk_socpeople = ".((int) $this->id);
1300 $sql .= " AND tc.source = 'external'";
1301 $sql .= " GROUP BY tc.element";
1302
1303 dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG);
1304
1305 $resql = $this->db->query($sql);
1306 if ($resql) {
1307 while ($obj = $this->db->fetch_object($resql)) {
1308 if ($obj->nb) {
1309 if ($obj->element == 'facture') {
1310 $this->ref_facturation = $obj->nb;
1311 } elseif ($obj->element == 'contrat') {
1312 $this->ref_contrat = $obj->nb;
1313 } elseif ($obj->element == 'commande') {
1314 $this->ref_commande = $obj->nb;
1315 } elseif ($obj->element == 'propal') {
1316 $this->ref_propal = $obj->nb;
1317 }
1318 }
1319 }
1320 $this->db->free($resql);
1321 return 0;
1322 } else {
1323 $this->error = $this->db->lasterror();
1324 return -1;
1325 }
1326 }
1327
1336 public function delete($user, $notrigger = 0, $nodeletefiles = 0)
1337 {
1338 global $conf;
1339
1340 $error = 0;
1341
1342 $this->db->begin();
1343
1344 if (!$notrigger) {
1345 // Call trigger
1346 $result = $this->call_trigger('CONTACT_DELETE', $user);
1347 if ($result < 0) {
1348 $error++;
1349 }
1350 // End call triggers
1351 }
1352
1353 if (!$error) {
1354 // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1355 $sql = "SELECT ec.rowid";
1356 $sql .= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
1357 $sql .= " ".MAIN_DB_PREFIX."c_type_contact tc";
1358 $sql .= " WHERE ec.fk_socpeople=".((int) $this->id);
1359 $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1360 $sql .= " AND tc.source='external'";
1361 dol_syslog(__METHOD__, LOG_DEBUG);
1362 $resql = $this->db->query($sql);
1363 if ($resql) {
1364 $num = $this->db->num_rows($resql);
1365
1366 $i = 0;
1367 while ($i < $num && !$error) {
1368 $obj = $this->db->fetch_object($resql);
1369
1370 $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
1371 $sqldel .= " WHERE rowid = ".((int) $obj->rowid);
1372 dol_syslog(__METHOD__, LOG_DEBUG);
1373 $result = $this->db->query($sqldel);
1374 if (!$result) {
1375 $error++;
1376 $this->error = $this->db->error().' sql='.$sqldel;
1377 }
1378
1379 $i++;
1380 }
1381 } else {
1382 $error++;
1383 $this->error = $this->db->error().' sql='.$sql;
1384 }
1385 }
1386
1387 if (!$error) {
1388 // Remove Roles
1389 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".((int) $this->id);
1390 dol_syslog(__METHOD__, LOG_DEBUG);
1391 $resql = $this->db->query($sql);
1392 if (!$resql) {
1393 $error++;
1394 $this->error .= $this->db->lasterror();
1395 $errorflag = -1;
1396 }
1397 }
1398
1399 if (!$error) {
1400 // Remove Notifications
1401 $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def WHERE fk_contact = ".((int) $this->id);
1402 dol_syslog(__METHOD__, LOG_DEBUG);
1403 $resql = $this->db->query($sql);
1404 if (!$resql) {
1405 $error++;
1406 $this->error .= $this->db->lasterror();
1407 $errorflag = -1;
1408 }
1409 }
1410
1411 if (!$error) {
1412 // Remove category
1413 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".((int) $this->id);
1414 dol_syslog(__METHOD__, LOG_DEBUG);
1415 $resql = $this->db->query($sql);
1416 if (!$resql) {
1417 $error++;
1418 $this->error .= $this->db->lasterror();
1419 $errorflag = -1;
1420 }
1421 }
1422
1423 if (!$error) {
1424 // Remove the birthday alerts set by users on this contact
1425 $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_alert WHERE fk_contact = ".((int) $this->id);
1426 dol_syslog(__METHOD__, LOG_DEBUG);
1427 $resql = $this->db->query($sql);
1428 if (!$resql) {
1429 $error++;
1430 $this->error .= $this->db->lasterror();
1431 }
1432 }
1433
1434 if (!$error) {
1435 // Remove the link of the user created from this contact (the user is kept)
1436 $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_socpeople = NULL WHERE fk_socpeople = ".((int) $this->id);
1437 dol_syslog(__METHOD__, LOG_DEBUG);
1438 $resql = $this->db->query($sql);
1439 if (!$resql) {
1440 $error++;
1441 $this->error .= $this->db->lasterror();
1442 }
1443 }
1444
1445 if (!$error) {
1446 // Remove the links with other objects
1447 $result = $this->deleteObjectLinked();
1448 if ($result < 0) {
1449 $error++;
1450 }
1451 }
1452
1453 // Remove the index of the documents of the contact (the files are removed after the commit).
1454 // Nothing is done when $nodeletefiles is set: the caller moves the documents elsewhere.
1455 $dirofdocuments = '';
1456 if ($this->id > 0 && !empty($conf->societe->multidir_output[$this->entity])) {
1457 $dirofdocuments = $conf->societe->multidir_output[$this->entity].'/contact/'.dol_sanitizeFileName((string) $this->id);
1458 }
1459 if (!$error && $dirofdocuments) {
1460 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1461 if (deleteFilesIntoDatabaseIndex($dirofdocuments, '', '') < 0 || !$this->deleteEcmFiles(1)) {
1462 $error++;
1463 $this->error .= $this->db->lasterror();
1464 }
1465 }
1466
1467 // Remove extrafields
1468 if (!$error) {
1469 // For avoid conflicts if trigger used
1470 $result = $this->deleteExtraFields();
1471 if ($result < 0) {
1472 $error++;
1473 }
1474 }
1475
1476 if (!$error) {
1477 $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
1478 $sql .= " WHERE rowid = ".((int) $this->id);
1479 dol_syslog(__METHOD__, LOG_DEBUG);
1480 $result = $this->db->query($sql);
1481 if (!$result) {
1482 $error++;
1483 $this->error = $this->db->error().' sql='.$sql;
1484 }
1485 }
1486
1487 if (!$error) {
1488 $this->db->commit();
1489
1490 // Delete the directory of the documents of the contact
1491 if (empty($nodeletefiles) && $dirofdocuments) {
1492 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1493 if (dol_is_dir($dirofdocuments)) {
1494 dol_delete_dir_recursive($dirofdocuments);
1495 }
1496 }
1497
1498 return 1;
1499 } else {
1500 $this->db->rollback();
1501 dol_syslog("Error ".$this->error, LOG_ERR);
1502 return -1;
1503 }
1504 }
1505
1506
1513 public function info($id)
1514 {
1515 $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1516 $sql .= " GREATEST(c.tms, COALESCE(cef.tms, c.tms)) as tms, c.fk_user_modif";
1517 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
1518 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as cef ON cef.fk_object=c.rowid";
1519 $sql .= " WHERE c.rowid = ".((int) $id);
1520
1521 $resql = $this->db->query($sql);
1522 if ($resql) {
1523 if ($this->db->num_rows($resql)) {
1524 $obj = $this->db->fetch_object($resql);
1525
1526 $this->id = $obj->rowid;
1527
1528 $this->user_creation_id = $obj->fk_user_creat;
1529 $this->user_modification_id = $obj->fk_user_modif;
1530 $this->date_creation = $this->db->jdate($obj->datec);
1531 $this->date_modification = $this->db->jdate($obj->tms);
1532 }
1533
1534 $this->db->free($resql);
1535 } else {
1536 print $this->db->error();
1537 }
1538 }
1539
1545 public function getNbOfEMailings()
1546 {
1547 $sql = "SELECT count(mc.email) as nb";
1548 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
1549 $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
1550 $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1551
1552 $resql = $this->db->query($sql);
1553 if ($resql) {
1554 $obj = $this->db->fetch_object($resql);
1555 $nb = (int) $obj->nb;
1556
1557 $this->db->free($resql);
1558 return $nb;
1559 } else {
1560 $this->error = $this->db->error();
1561 return -1;
1562 }
1563 }
1564
1571 public function getTooltipContentArray($params)
1572 {
1573 global $conf, $langs, $user;
1574
1575 $datas = [];
1576
1577 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1578 return ['optimize' => $langs->trans("ShowContact")];
1579 }
1580 if (!empty($this->photo) && class_exists('Form')) {
1581 $photo = '<div class="photointooltip floatright">';
1582 $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.
1583 $photo .= '</div>';
1584 $datas['photo'] = $photo;
1585 }
1586
1587 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u> ' . $this->getLibStatut(4);
1588 $datas['name'] = '<br><b>'.$langs->trans("Name").':</b> '.$this->getFullName($langs);
1589 // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code
1590 if (!empty($this->poste)) {
1591 $datas['job'] = '<br><b>'.$langs->trans("Poste").':</b> '.$this->poste;
1592 }
1593 $datas['email'] = '<br><b>'.$langs->trans("EMail").':</b> '.$this->email;
1594 $phonelist = array();
1595 $country_code = empty($this->country_code) ? '' : $this->country_code;
1596 if ($this->phone_pro) {
1597 $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1598 }
1599 if ($this->phone_mobile) {
1600 $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1601 }
1602 if ($this->phone_perso) {
1603 $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1604 }
1605 $datas['phonelist'] = '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $phonelist);
1606 $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs);
1607
1608 return $datas;
1609 }
1610
1624 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $moreparam = '', $save_lastsearch_value = -1, $maxlen = 0, $morecss = 'valignmiddle')
1625 {
1626 global $conf, $langs, $hookmanager;
1627
1628 if (!empty($conf->dol_no_mouse_hover)) {
1629 $notooltip = 1; // Force disable tooltips
1630 }
1631
1632 $result = '';
1633 $params = [
1634 'id' => $this->id,
1635 'objecttype' => $this->element,
1636 'option' => $option,
1637 ];
1638 $classfortooltip = 'classfortooltip';
1639 $dataparams = '';
1640 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1641 $classfortooltip = 'classforajaxtooltip';
1642 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1643 $label = '';
1644 } else {
1645 $label = implode($this->getTooltipContentArray($params));
1646 }
1647
1648 $baseurl = DOL_URL_ROOT . '/contact/card.php';
1649 $query = ['id' => $this->id];
1650 if ($option !== 'nolink') {
1651 // Add param to save lastsearch_values or not
1652 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1653 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1654 $add_save_lastsearch_values = 1;
1655 }
1656 if ($add_save_lastsearch_values) {
1657 $query = array_merge($query, ['save_lastsearch_values' => 1]);
1658 }
1659 }
1660 $url = dolBuildUrl($baseurl, $query);
1661
1662 $url .= $moreparam;
1663
1664 $linkclose = '';
1665 if (empty($notooltip)) {
1666 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1667 $label = $langs->trans("ShowContact");
1668 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1669 }
1670 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1671 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1672 } else {
1673 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1674 }
1675
1676 if ($option == 'nolink') {
1677 $linkstart = '<span';
1678 } else {
1679 $linkstart = '<a href="'.$url.'"';
1680 }
1681 $linkstart .= $linkclose.'>';
1682 if ($option == 'nolink') {
1683 $linkend = '</span>';
1684 } else {
1685 $linkend = '</a>';
1686 }
1687
1688 $result .= $linkstart;
1689
1690 if ($withpicto) {
1691 if ($withpicto < 0) {
1692 $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>';
1693 if ($withpicto != 2 && $withpicto != -2) {
1694 $result .= ' ';
1695 }
1696 } else {
1697 $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);
1698 }
1699 }
1700 if ($withpicto != 2 && $withpicto != -2) {
1701 $result .= '<span class="valignmiddle">'.($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)).'</span>';
1702 }
1703
1704 $result .= $linkend;
1705
1706 global $action;
1707 $hookmanager->initHooks(array('contactdao'));
1708 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1709 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1710 if ($reshook > 0) {
1711 $result = $hookmanager->resPrint;
1712 } else {
1713 $result .= $hookmanager->resPrint;
1714 }
1715
1716 return $result;
1717 }
1718
1724 public function getCivilityLabel()
1725 {
1726 global $langs;
1727
1728 $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1729 if (empty($code)) {
1730 return '';
1731 }
1732
1733 $langs->load("dict");
1734 return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
1735 }
1736
1743 public function getLibStatut($mode)
1744 {
1745 return $this->LibStatut($this->status, $mode);
1746 }
1747
1748 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1756 public function LibStatut($status, $mode)
1757 {
1758 // phpcs:enable
1759 global $langs;
1760
1761 $labelStatus = array(
1762 0 => 'ActivityCeased',
1763 1 => 'InActivity',
1764 4 => 'InActivity',
1765 5 => 'ActivityCeased',
1766 );
1767 $labelStatusShort = array(
1768 0 => 'ActivityCeased',
1769 1 => 'InActivity',
1770 4 => 'InActivity',
1771 5 => 'ActivityCeased',
1772 );
1773
1774 $statusType = 'status4';
1775 if ($status == 0 || $status == 5) {
1776 $statusType = 'status5';
1777 }
1778
1779 $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1780 $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1781
1782 return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1783 }
1784
1785
1786 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1794 public function LibPubPriv($status, $decorate = 0)
1795 {
1796 // phpcs:enable
1797 global $langs;
1798 if ($status == '1') {
1799 $s = ($decorate == 2 ? '' : $langs->trans('ContactPrivate'));
1800 if ($decorate) {
1801 $s = '<span title="'.$langs->trans('ContactPrivateDesc').'">'.img_picto('', 'private', 'class="paddingrightonly"').$s.'</span>';
1802 }
1803 return $s;
1804 } else {
1805 $s = ($decorate == 2 ? '' : $langs->trans('ContactPublic'));
1806 if ($decorate) {
1807 $s = '<span title="'.$langs->trans('ContactPublicDesc').'">'.img_picto('', 'public', 'class="paddingrightonly"').$s.'</span>';
1808 }
1809 return $s;
1810 }
1811 }
1812
1813
1821 public function initAsSpecimen()
1822 {
1823 // Get first id of existing company and save it into $socid
1824 $socid = 0;
1825 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1";
1826 $resql = $this->db->query($sql);
1827 if ($resql) {
1828 $obj = $this->db->fetch_object($resql);
1829 if ($obj) {
1830 $socid = $obj->rowid;
1831 }
1832 }
1833
1834 // Initialise parameters
1835 $this->id = 0;
1836 $this->entity = 1;
1837 $this->specimen = 1;
1838 $this->lastname = 'DOLIBARR';
1839 $this->firstname = 'SPECIMEN';
1840 $this->address = '21 jump street';
1841 $this->zip = '99999';
1842 $this->town = 'MyTown';
1843 $this->country_id = 1;
1844 $this->country_code = 'FR';
1845 $this->country = 'France';
1846 $this->email = 'specimen@specimen.com';
1847 $this->socialnetworks = array(
1848 'skype' => 'tom.hanson',
1849 'twitter' => 'tomhanson',
1850 'linkedin' => 'tomhanson',
1851 );
1852 $this->phone_pro = '0909090901';
1853 $this->phone_perso = '0909090902';
1854 $this->phone_mobile = '0909090903';
1855 $this->fax = '0909090909';
1856
1857 $this->note_public = 'This is a comment (public)';
1858 $this->note_private = 'This is a comment (private)';
1859
1860 $this->socid = $socid;
1861 $this->status = 1;
1862
1863 return 1;
1864 }
1865
1872 public function setstatus($status)
1873 {
1874 global $conf, $langs, $user;
1875
1876 $error = 0;
1877
1878 // Check parameters
1879 if (!empty($this->statut) && empty($this->status)) {
1880 $this->status = 1;
1881 }
1882 if ($this->status == $status) {
1883 return 0;
1884 } else {
1885 $this->status = $status;
1886 $this->statut = $status;
1887 }
1888
1889 $this->db->begin();
1890
1891 // User disable
1892 $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople";
1893 $sql .= " SET statut = ".((int) $this->status);
1894 $sql .= ", fk_user_modif = ".((int) $user->id);
1895 $sql .= " WHERE rowid = ".((int) $this->id);
1896 $result = $this->db->query($sql);
1897
1898 dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);
1899 if ($result) {
1900 // Call trigger
1901 $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1902 if ($result < 0) {
1903 $error++;
1904 }
1905 // End call triggers
1906 }
1907
1908 if ($error) {
1909 $this->db->rollback();
1910 return -$error;
1911 } else {
1912 $this->db->commit();
1913 return 1;
1914 }
1915 }
1916
1928 public function setCategories($categories, $remove_existing = true)
1929 {
1930 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1931 return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT, $remove_existing);
1932 }
1933
1942 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1943 {
1944 $tables = array(
1945 'socpeople', 'societe_contacts'
1946 );
1947
1948 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1949 }
1950
1963 public static function replaceContact(DoliDB $dbs, $origin_id, $dest_id)
1964 {
1965 // llx_societe_contacts: UNIQUE(entity, fk_soc, fk_c_type_contact, fk_socpeople).
1966 // Delete the roles the target contact already has, then move the remaining ones.
1967 $sql = 'DELETE FROM '.$dbs->prefix().'societe_contacts WHERE rowid IN (';
1968 $sql .= ' SELECT x.rowid FROM (';
1969 $sql .= ' SELECT origin.rowid FROM '.$dbs->prefix().'societe_contacts as origin';
1970 $sql .= ' INNER JOIN '.$dbs->prefix().'societe_contacts as dest ON dest.entity = origin.entity';
1971 $sql .= ' AND dest.fk_soc = origin.fk_soc AND dest.fk_c_type_contact = origin.fk_c_type_contact';
1972 $sql .= ' WHERE origin.fk_socpeople = '.((int) $origin_id).' AND dest.fk_socpeople = '.((int) $dest_id);
1973 $sql .= ' ) as x)';
1974 if (!$dbs->query($sql)) {
1975 return false;
1976 }
1977
1978 if (!CommonObject::commonReplaceContact($dbs, $origin_id, $dest_id, array('societe_contacts'))) {
1979 return false;
1980 }
1981
1982 // llx_element_contact.fk_socpeople points to llx_socpeople only when c_type_contact.source is
1983 // 'external'. It points to llx_user when source is 'internal', so both queries below MUST filter
1984 // on it, otherwise internal (user) assignments would be moved to the merged contact.
1985
1986
1987 // llx_element_contact: UNIQUE(element_id, fk_c_type_contact, fk_socpeople)
1988 $sql = 'DELETE FROM '.$dbs->prefix().'element_contact WHERE rowid IN (';
1989 $sql .= ' SELECT x.rowid FROM (';
1990 $sql .= ' SELECT origin.rowid FROM '.$dbs->prefix().'element_contact as origin';
1991 $sql .= ' INNER JOIN '.$dbs->prefix().'element_contact as dest ON dest.element_id = origin.element_id';
1992 $sql .= ' AND dest.fk_c_type_contact = origin.fk_c_type_contact';
1993 $sql .= ' WHERE origin.fk_socpeople = '.((int) $origin_id).' AND dest.fk_socpeople = '.((int) $dest_id);
1994 $sql .= " AND origin.fk_c_type_contact IN (SELECT rowid FROM ".$dbs->prefix()."c_type_contact WHERE source = 'external')";
1995 $sql .= ' ) as x)';
1996 if (!$dbs->query($sql)) {
1997 return false;
1998 }
1999
2000 $sql = 'UPDATE '.$dbs->prefix().'element_contact SET fk_socpeople = '.((int) $dest_id);
2001 $sql .= ' WHERE fk_socpeople = '.((int) $origin_id);
2002 $sql .= " AND fk_c_type_contact IN (SELECT rowid FROM ".$dbs->prefix()."c_type_contact WHERE source = 'external')";
2003 if (!$dbs->query($sql)) {
2004 return false;
2005 }
2006
2007 // References to a contact stored as a (type, id) couple. All the names below are literals, so
2008 // they are safe to concatenate. 'unique' lists the other columns of the unique index of the
2009 // table, if any, so the rows the target contact already has can be dropped before the update.
2010 $polymorphic = array(
2011 array('table' => 'object_lang', 'id' => 'fk_object', 'type' => 'type_object',
2012 'values' => array('contact', 'socpeople'), 'unique' => array('property', 'lang')),
2013 array('table' => 'links', 'id' => 'objectid', 'type' => 'objecttype',
2014 'values' => array('contact'), 'unique' => array('label')),
2015 array('table' => 'element_element', 'id' => 'fk_source', 'type' => 'sourcetype',
2016 'values' => array('contact'), 'unique' => array('fk_target', 'targettype')),
2017 array('table' => 'element_element', 'id' => 'fk_target', 'type' => 'targettype',
2018 'values' => array('contact'), 'unique' => array('fk_source', 'sourcetype')),
2019 // An event can be linked to a contact as its related object
2020 array('table' => 'actioncomm', 'id' => 'fk_element', 'type' => 'elementtype',
2021 'values' => array('contact'), 'unique' => array()),
2022 // dol_move() updates the path of the indexed files but never their source object, so the
2023 // index rows have to be moved here or they would point to the deleted contact. The unique
2024 // index of the table is on (filepath, filename, entity), which is left untouched.
2025 array('table' => 'ecm_files', 'id' => 'src_object_id', 'type' => 'src_object_type',
2026 'values' => array('contact', 'socpeople'), 'unique' => array()),
2027 array('table' => 'quickmemo_memo', 'id' => 'fk_element', 'type' => 'element_type',
2028 'values' => array('contact'), 'unique' => array()),
2029 array('table' => 'comment', 'id' => 'fk_element', 'type' => 'element_type',
2030 'values' => array('contact'), 'unique' => array()),
2031 );
2032
2033 foreach ($polymorphic as $ref) {
2034 // Some of these tables are provided by modules that may not be installed
2035 $sanitizedtable = $dbs->sanitize($ref['table']);
2036 $sanitizedidcol = $dbs->sanitize($ref['id']);
2037 $sanitizedtypecol = $dbs->sanitize($ref['type']);
2038 if (!$dbs->DDLListTables((string) $dbs->database_name, $dbs->prefix().$sanitizedtable)) {
2039 continue;
2040 }
2041 // Each value is escaped on its own: sanitize() removes the quotes inside the string it is
2042 // given, so sanitizing an already assembled list would collapse it into a single value
2043 $quotedvalues = array();
2044 foreach ($ref['values'] as $refvalue) {
2045 $quotedvalues[] = "'".$dbs->escape($refvalue)."'";
2046 }
2047 $sanitizedvalues = implode(', ', $quotedvalues); // @phan-suppress-current-line SqlInjection
2048 $sanitizedtypefilter = $sanitizedtypecol." IN (".$sanitizedvalues.")";
2049
2050 if (!empty($ref['unique'])) {
2051 $sql = "DELETE FROM ".$dbs->prefix().$sanitizedtable." WHERE rowid IN (";
2052 $sql .= " SELECT x.rowid FROM (";
2053 $sql .= " SELECT origin.rowid FROM ".$dbs->prefix().$sanitizedtable." as origin";
2054 $sql .= " INNER JOIN ".$dbs->prefix().$sanitizedtable." as dest";
2055 $sql .= " ON dest.".$sanitizedtypecol." = origin.".$sanitizedtypecol;
2056 foreach ($ref['unique'] as $uniquecol) {
2057 $sanitizeduniquecol = $dbs->sanitize($uniquecol);
2058 $sql .= " AND dest.".$sanitizeduniquecol." = origin.".$sanitizeduniquecol;
2059 }
2060 $sql .= " WHERE origin.".$sanitizedidcol." = ".((int) $origin_id);
2061 $sql .= " AND dest.".$sanitizedidcol." = ".((int) $dest_id);
2062 $sql .= " AND origin.".$sanitizedtypefilter;
2063 $sql .= " ) as x)";
2064 if (!$dbs->query($sql)) {
2065 return false;
2066 }
2067 }
2068
2069 $sql = "UPDATE ".$dbs->prefix().$sanitizedtable." SET ".$sanitizedidcol." = ".((int) $dest_id);
2070 $sql .= " WHERE ".$sanitizedtypefilter;
2071 $sql .= " AND ".$sanitizedidcol." = ".((int) $origin_id);
2072 if (!$dbs->query($sql)) {
2073 return false;
2074 }
2075 }
2076
2077 // A link between the two contacts became a link of the target contact to itself, which the
2078 // linked objects box would then display. There is no unique index violation, so nothing failed.
2079 $sql = "DELETE FROM ".$dbs->prefix()."element_element WHERE fk_source = fk_target";
2080 $sql .= " AND sourcetype = targettype AND fk_source = ".((int) $dest_id);
2081 $sql .= " AND sourcetype = 'contact'";
2082 if (!$dbs->query($sql)) {
2083 return false;
2084 }
2085
2086 return true;
2087 }
2088
2100 public function mergeContact($contact_origin_id)
2101 {
2102 global $langs, $hookmanager, $user, $action;
2103
2104 $error = 0;
2105 $langs->loadLangs(array('errors', 'companies'));
2106
2107 // The target contact must have been loaded: update() would silently update no row and the
2108 // satellite data would then be moved to the contact id 0.
2109 if (!($this->id > 0) || empty($this->entity)) {
2110 $this->error = $langs->trans('ErrorBadParameters');
2111 dol_syslog(__METHOD__.' Called on a contact that was not loaded', LOG_ERR);
2112 return -1;
2113 }
2114 if ($contact_origin_id <= 0 || $contact_origin_id == $this->id) {
2115 $this->error = $langs->trans('ErrorBadParameters');
2116 return -1;
2117 }
2118 // A merge deletes a contact, so it requires the permission to delete one, whatever the caller
2119 if (!$user->hasRight('societe', 'contact', 'creer') || !$user->hasRight('societe', 'contact', 'supprimer')) {
2120 $this->error = $langs->trans('ErrorForbidden');
2121 return -1;
2122 }
2123 // An external user never merges anything, as on the third party card
2124 if ($user->socid > 0) {
2125 $this->error = $langs->trans('ErrorForbidden');
2126 return -1;
2127 }
2128
2129 $contact_origin = new Contact($this->db); // The contact that we will delete
2130 $resultfetch = $contact_origin->fetch($contact_origin_id);
2131 // fetch() returns the id when found, 2 when several records were found, 0 when not found and -1 on error
2132 if ($resultfetch != $contact_origin_id) {
2133 $this->error = $langs->trans('ErrorRecordNotFound');
2134 dol_syslog(__METHOD__.' Cannot fetch contact id='.$contact_origin_id.', result='.$resultfetch, LOG_ERR);
2135 return -1;
2136 }
2137
2138 // Access guards. fetch() by rowid applies neither an entity filter nor a permission filter, so
2139 // the two contacts are revalidated here, including the current one: an id coming from a POST is
2140 // not to be trusted, and the checks must also protect the callers that are not the contact card.
2141 $entities = explode(',', getEntity('contact'));
2142 foreach (array($this, $contact_origin) as $tmpcontact) {
2143 if (!in_array($tmpcontact->entity, $entities) || $tmpcontact->entity != $this->entity) {
2144 $this->error = $langs->trans('ErrorContactsMergeDifferentEntity');
2145 return -1;
2146 }
2147 if (!empty($tmpcontact->priv) && $tmpcontact->user_creation_id != $user->id) {
2148 $this->error = $langs->trans('ErrorContactsMergePrivate');
2149 return -1;
2150 }
2151 // A contact without a third party is shared, so the perimeter of the sales representatives
2152 // does not apply to it, as in restrictedArea()
2153 if ($tmpcontact->socid > 0 && !$user->hasRight('societe', 'client', 'voir')
2154 && !$this->isSalesRepresentativeOf($tmpcontact->socid)) {
2155 $this->error = $langs->trans('ErrorForbidden');
2156 return -1;
2157 }
2158 }
2159 // Absorbing a shared contact into a private one would hide its data from everybody else,
2160 // including the administrators, and the merged contact is deleted so it is not reversible
2161 if (!empty($this->priv) && empty($contact_origin->priv)) {
2162 $this->error = $langs->trans('ErrorContactsMergeIntoPrivate');
2163 return -1;
2164 }
2165 $originlinked = $this->isLinkedToUser($contact_origin->id);
2166 $destlinked = $this->isLinkedToUser($this->id);
2167 if ($originlinked < 0 || $destlinked < 0) {
2168 // The guard below is a security one, so it must refuse and not let the merge through
2169 $this->error = $langs->trans('ErrorContactsMerge');
2170 return -1;
2171 }
2172 // Moving llx_user.fk_socpeople would give the contact of a user account to another contact,
2173 // and Contact::update() then propagates the email of that contact to the user, which is a way
2174 // to take over the account. Changing the contact of a user requires the permission to do so.
2175 if (($originlinked > 0 || $destlinked > 0) && !$user->hasRight('user', 'user', 'creer')) {
2176 $this->error = $langs->trans('ErrorContactsMergeLinkedToUser');
2177 return -1;
2178 }
2179 // llx_user.fk_socpeople has a unique key: refuse rather than silently break a user link
2180 if ($originlinked > 0 && $destlinked > 0) {
2181 $this->error = $langs->trans('ErrorContactsMergeBothLinkedToUser');
2182 return -1;
2183 }
2184
2185 dol_syslog(__METHOD__.' merge contact id='.$contact_origin->id.' (will be deleted) into the contact id='.$this->id);
2186
2187 $this->db->begin();
2188
2189 // Recopy some data
2190 foreach (self::MERGE_FIELDS_FILL_IF_EMPTY as $property) {
2191 if (empty($this->$property) && !empty($contact_origin->$property)) {
2192 $this->$property = $contact_origin->$property;
2193 }
2194 }
2195
2196 // Concat some data, with a dated mention so a targeted erasure stays possible later
2197 $mention = '['.$langs->transnoentitiesnoconv('MergedFromContact', dol_print_date(dol_now(), 'day'), (string) $contact_origin->id).']';
2198 foreach (self::MERGE_FIELDS_CONCAT as $property) {
2199 if (!empty($contact_origin->$property)) {
2200 $this->$property = dol_concatdesc($this->$property, $mention."\n".$contact_origin->$property);
2201 }
2202 }
2203
2204 // A merge must never make the data of a private contact visible to everybody
2205 if (!empty($contact_origin->priv)) {
2206 $this->priv = 1;
2207 }
2208
2209 // If alias name is not defined on target contact, we can store in it the old name of the contact
2210 if (empty($this->name_alias) && $this->getFullName($langs) != $contact_origin->getFullName($langs)) {
2211 $this->name_alias = $contact_origin->getFullName($langs);
2212 }
2213
2214 // Merge extrafields. They are saved by the update() below.
2215 if (is_array($contact_origin->array_options)) {
2216 foreach ($contact_origin->array_options as $key => $val) {
2217 if (empty($this->array_options[$key])) {
2218 $this->array_options[$key] = $val;
2219 }
2220 }
2221 }
2222
2223 // updateRoles(), called by update(), deletes then reinserts every societe_contacts row of the
2224 // contact from $this->roles, which would wipe the roles we are about to move. It is a no-op
2225 // when roles is not set. Set it to null instead of using unset(): roles is a declared property
2226 // and unset() would make any later access emit an "Undefined property" warning.
2227 $this->roles = null;
2228
2229 // Update. The trigger is called once at the end of the merge, hence $notrigger = 1.
2230 if ($this->update($this->id, $user, 1) <= 0) {
2231 $error++;
2232 dol_syslog(__METHOD__.' Failed to update the target contact: '.$this->errorsToString(), LOG_ERR);
2233 }
2234
2235 // Merge categories, before the deletion below: llx_categorie_contact has a foreign key on
2236 // llx_socpeople without ON DELETE.
2237 if (!$error) {
2238 include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
2239 $static_cat = new Categorie($this->db);
2240 $cats_origin = $static_cat->containing($contact_origin->id, 'contact', 'id');
2241 $cats_dest = $static_cat->containing($this->id, 'contact', 'id');
2242 // containing() returns the int -1 on SQL error. Reading it as an empty list would replace
2243 // the categories of the target contact by the ones of the merged contact only.
2244 if (!is_array($cats_origin) || !is_array($cats_dest)) {
2245 $this->error = $static_cat->error;
2246 dol_syslog(__METHOD__.' Cannot read the categories of the contacts: '.$this->error, LOG_ERR);
2247 $error++;
2248 } else {
2249 // array_merge() must be used here: the + operator on arrays is a union on keys, it
2250 // would silently drop categories.
2251 $cats = array_merge($cats_origin, $cats_dest);
2252 if ($this->setCategories(array_values(array_unique($cats))) < 0) {
2253 $error++;
2254 }
2255 }
2256 }
2257
2258 // Children contacts
2259 if (!$error) {
2260 $error += $this->mergeContactChildren($contact_origin);
2261 }
2262
2263 // Move links
2264 if (!$error) {
2265 $objects = array(
2266 'ActionComm' => '/comm/action/class/actioncomm.class.php',
2267 'Contact' => '/contact/class/contact.class.php',
2268 'User' => '/user/class/user.class.php',
2269 );
2270 foreach ($objects as $object_name => $object_file) {
2271 require_once DOL_DOCUMENT_ROOT.$object_file;
2272
2273 if (!$object_name::replaceContact($this->db, $contact_origin->id, $this->id)) {
2274 $error++;
2275 $this->error = $this->db->lasterror();
2276 dol_syslog(__METHOD__.' '.$object_name.'::replaceContact failed: '.$this->error, LOG_ERR);
2277 break;
2278 }
2279 }
2280 }
2281
2282 // Tables of the optional modules
2283 if (!$error) {
2284 $error += $this->mergeContactOptionalTables($contact_origin);
2285 }
2286
2287 // External modules should update their ones too
2288 if (!$error) {
2289 $parameters = array('contact_origin' => $contact_origin->id, 'contact_dest' => $this->id);
2290 $reshook = $hookmanager->executeHooks('replaceContact', $parameters, $this, $action);
2291
2292 if ($reshook < 0) {
2293 $this->error = $hookmanager->error;
2294 $this->errors = $hookmanager->errors;
2295 $error++;
2296 }
2297 }
2298
2299 if (!$error) {
2300 $this->context = array(
2301 'merge' => 1,
2302 'mergefromid' => $contact_origin->id,
2303 'mergefromname' => $contact_origin->getFullName($langs)
2304 );
2305
2306 // Call trigger
2307 $result = $this->call_trigger('CONTACT_MODIFY', $user);
2308 if ($result < 0) {
2309 $error++;
2310 }
2311 // End call triggers
2312 }
2313
2314 if (!$error) {
2315 // We finally remove the old contact. Its documents are kept ($nodeletefiles): they are moved
2316 // to the target contact once the transaction is committed, after this deletion.
2317 if ($contact_origin->delete($user, 0, 1) <= 0) {
2318 $this->error = $contact_origin->error;
2319 $this->errors = $contact_origin->errors;
2320 $error++;
2321 }
2322 }
2323
2324 if (!$error) {
2325 // Files are moved once the transaction is committed: dol_move() is not transactional, and
2326 // the old contact was deleted with $nodeletefiles = 1 so its documents are still there.
2327 $this->mergeContactFiles($contact_origin->id);
2328 }
2329
2330 if (!$error) {
2331 $this->db->commit();
2332 return 0;
2333 } else {
2334 $langs->load("errors");
2335 $this->error = $langs->trans('ErrorContactsMerge').' '.$this->error;
2336 $this->db->rollback();
2337 // The object still holds the merged values in memory, reload it so the caller does not
2338 // display data that was rolled back
2339 $this->fetch($this->id);
2340 return -1;
2341 }
2342 }
2343
2352 private function isLinkedToUser($contactid)
2353 {
2354 $sql = "SELECT rowid FROM ".$this->db->prefix()."user WHERE fk_socpeople = ".((int) $contactid);
2355
2356 $resql = $this->db->query($sql);
2357 if (!$resql) {
2358 $this->error = $this->db->lasterror();
2359 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2360 return -1;
2361 }
2362 $found = ($this->db->num_rows($resql) > 0 ? 1 : 0);
2363 $this->db->free($resql);
2364
2365 return $found;
2366 }
2367
2375 private function isSalesRepresentativeOf($socid)
2376 {
2377 global $user;
2378
2379 if (empty($socid)) {
2380 return false; // A shared contact with no third party is out of any portfolio
2381 }
2382
2383 $sql = "SELECT fk_soc FROM ".$this->db->prefix()."societe_commerciaux";
2384 $sql .= " WHERE fk_soc = ".((int) $socid)." AND fk_user = ".((int) $user->id);
2385
2386 $resql = $this->db->query($sql);
2387 if (!$resql) {
2388 $this->error = $this->db->lasterror();
2389 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2390 return false;
2391 }
2392 $found = ($this->db->num_rows($resql) > 0);
2393 $this->db->free($resql);
2394
2395 return $found;
2396 }
2397
2408 private function mergeContactChildren($contact_origin)
2409 {
2410 $error = 0;
2411
2412 // fk_parent is not loaded by fetch()
2413 $parentofdest = $this->getParentId($this->id);
2414
2415 // The target contact is a child of the merged one: its parent is about to be deleted
2416 if ($parentofdest == $contact_origin->id) {
2417 $newparent = $this->getParentId($contact_origin->id);
2418 // A dedicated query is used rather than setValueFrom(): the 'int' format of the latter
2419 // casts null to 0, while fk_parent is nullable, and its trigger key would fetch the
2420 // record again and overwrite the values merged into memory.
2421 $sql = 'UPDATE '.$this->db->prefix().'socpeople';
2422 $sql .= ' SET fk_parent = '.($newparent > 0 ? ((int) $newparent) : 'NULL');
2423 $sql .= ' WHERE rowid = '.((int) $this->id);
2424 if (!$this->db->query($sql)) {
2425 $this->error = $this->db->lasterror();
2426 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2427 return 1;
2428 }
2429 }
2430
2431 // Collect the ancestors of the target contact, they must not become its children
2432 $ancestors = array();
2433 $currentid = $this->getParentId($this->id);
2434 $depth = 0;
2435 while ($currentid > 0 && $depth < self::MERGE_MAX_PARENT_DEPTH) {
2436 if (in_array($currentid, $ancestors)) {
2437 break; // The hierarchy already contains a cycle, stop walking it
2438 }
2439 $ancestors[] = (int) $currentid;
2440 $currentid = $this->getParentId($currentid);
2441 $depth++;
2442 }
2443
2444 $sql = 'UPDATE '.$this->db->prefix().'socpeople SET fk_parent = '.((int) $this->id);
2445 $sql .= ' WHERE fk_parent = '.((int) $contact_origin->id);
2446 $sql .= ' AND rowid <> '.((int) $this->id);
2447 if (!empty($ancestors)) {
2448 // $ancestors only contains ids already cast to int
2449 $sanitizedancestors = implode(',', $ancestors); // @phan-suppress-current-line SqlInjection
2450 $sql .= " AND rowid NOT IN (".$sanitizedancestors.")";
2451 }
2452 if (!$this->db->query($sql)) {
2453 $this->error = $this->db->lasterror();
2454 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2455 $error++;
2456 }
2457
2458 // The children excluded above, being ancestors of the target contact, still point to the
2459 // contact about to be deleted. Detach them rather than leave a dangling parent.
2460 if (!$error) {
2461 $sql = 'UPDATE '.$this->db->prefix().'socpeople SET fk_parent = NULL';
2462 $sql .= ' WHERE fk_parent = '.((int) $contact_origin->id);
2463 if (!$this->db->query($sql)) {
2464 $this->error = $this->db->lasterror();
2465 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2466 $error++;
2467 }
2468 }
2469
2470 return $error;
2471 }
2472
2480 private function getParentId($contactid)
2481 {
2482 $sql = "SELECT fk_parent FROM ".$this->db->prefix()."socpeople WHERE rowid = ".((int) $contactid);
2483
2484 $resql = $this->db->query($sql);
2485 if (!$resql) {
2486 $this->error = $this->db->lasterror();
2487 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2488 return 0;
2489 }
2490 $parentid = 0;
2491 if ($obj = $this->db->fetch_object($resql)) {
2492 $parentid = (empty($obj->fk_parent) ? 0 : (int) $obj->fk_parent);
2493 }
2494 $this->db->free($resql);
2495
2496 return $parentid;
2497 }
2498
2507 private function mergeContactOptionalTables($contact_origin)
2508 {
2509 $error = 0;
2510
2511 // Notifications. llx_notify_def has no unique key, but a duplicated row means the same
2512 // notification sent twice, so it must be deduplicated as well.
2513 $sql = 'DELETE FROM '.$this->db->prefix().'notify_def WHERE rowid IN (';
2514 $sql .= ' SELECT x.rowid FROM (';
2515 $sql .= ' SELECT origin.rowid FROM '.$this->db->prefix().'notify_def as origin';
2516 $sql .= ' INNER JOIN '.$this->db->prefix().'notify_def as dest ON dest.fk_action = origin.fk_action';
2517 // A row is a duplicate only if the whole definition matches, recipient included: fk_soc,
2518 // entity, type, threshold, context, fk_user and email are nullable, hence the NULL safe
2519 // comparisons, the MySQL <=> operator not being portable to PostgreSQL.
2520 $sql .= ' AND (dest.fk_soc = origin.fk_soc OR (dest.fk_soc IS NULL AND origin.fk_soc IS NULL))';
2521 $sql .= ' AND (dest.entity = origin.entity OR (dest.entity IS NULL AND origin.entity IS NULL))';
2522 $sql .= ' AND (dest.type = origin.type OR (dest.type IS NULL AND origin.type IS NULL))';
2523 $sql .= ' AND (dest.threshold = origin.threshold OR (dest.threshold IS NULL AND origin.threshold IS NULL))';
2524 $sql .= ' AND (dest.context = origin.context OR (dest.context IS NULL AND origin.context IS NULL))';
2525 $sql .= ' AND (dest.fk_user = origin.fk_user OR (dest.fk_user IS NULL AND origin.fk_user IS NULL))';
2526 $sql .= ' AND (dest.email = origin.email OR (dest.email IS NULL AND origin.email IS NULL))';
2527 $sql .= ' WHERE origin.fk_contact = '.((int) $contact_origin->id).' AND dest.fk_contact = '.((int) $this->id);
2528 $sql .= ' ) as x)';
2529 if (!$this->db->query($sql)) {
2530 $this->error = $this->db->lasterror();
2531 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2532 return 1;
2533 }
2534
2535 if (!CommonObject::commonReplaceContact($this->db, $contact_origin->id, $this->id, array('notify', 'notify_def'), 'fk_contact')) {
2536 dol_syslog(__METHOD__.' Failed to move the notifications: '.$this->db->lasterror(), LOG_ERR);
2537 return 1;
2538 }
2539
2540 // Mass emailing targets
2541 if ($this->db->DDLListTables((string) $this->db->database_name, $this->db->prefix().'mailing_cibles')) {
2542 // No deduplication here: uk_mailing_cibles is (fk_mailing, email), it does not contain
2543 // fk_contact, so moving fk_contact cannot violate it, and the rows hold the send history.
2544 if (!CommonObject::commonReplaceContact($this->db, $contact_origin->id, $this->id, array('mailing_cibles'), 'fk_contact')) {
2545 dol_syslog(__METHOD__.' Failed to move the mass emailing targets: '.$this->db->lasterror(), LOG_ERR);
2546 return 1;
2547 }
2548
2549 // The source of a target is also stored as a (source_type, source_id) couple
2550 $sql = 'UPDATE '.$this->db->prefix().'mailing_cibles SET source_id = '.((int) $this->id);
2551 $sql .= " WHERE source_type = 'contact' AND source_id = ".((int) $contact_origin->id);
2552 if (!$this->db->query($sql)) {
2553 $this->error = $this->db->lasterror();
2554 dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
2555 return 1;
2556 }
2557 }
2558
2559 return $error;
2560 }
2561
2570 private function mergeContactFiles($contact_origin_id)
2571 {
2572 global $conf, $langs;
2573
2574 if (empty($conf->societe->multidir_output[$this->entity])) {
2575 return;
2576 }
2577
2578 // files.lib.php is not loaded when the merge is called outside of a web context
2579 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2580
2581 // The id is used and not $this->ref, which is null unless the contact was loaded by fetch()
2582 $srcdir = $conf->societe->multidir_output[$this->entity].'/contact/'.((int) $contact_origin_id);
2583 $destdir = $conf->societe->multidir_output[$this->entity].'/contact/'.((int) $this->id);
2584
2585 if (!dol_is_dir($srcdir)) {
2586 return;
2587 }
2588
2589 $failed = array();
2590 $dirlist = dol_dir_list($srcdir, 'files', 1);
2591 foreach ($dirlist as $filetomove) {
2592 $destfile = $destdir.'/'.$filetomove['relativename'];
2593 // dol_move() is called below with $overwriteifexists = 0, so a file already existing on
2594 // the target contact is renamed rather than lost
2595 if (dol_is_file($destfile)) {
2596 $info = pathinfo($filetomove['relativename']);
2597 $suffix = (empty($info['extension']) ? '' : '.'.$info['extension']);
2598 $destfile = $destdir.'/'.(empty($info['dirname']) || $info['dirname'] == '.' ? '' : $info['dirname'].'/');
2599 $destfile .= $info['filename'].'-'.((int) $contact_origin_id).$suffix;
2600 }
2601 // dol_move() does not create the target directory, and the target contact usually has
2602 // none yet, so it has to be created for every level of the source tree
2603 dol_mkdir(dirname($destfile));
2604 if (!dol_move($filetomove['fullname'], $destfile, '0', 0, 0, 1)) {
2605 $failed[] = $filetomove['relativename'];
2606 }
2607 }
2608
2609 if (!empty($failed)) {
2610 dol_syslog(__METHOD__.' Failed to move '.count($failed).' file(s) from '.$srcdir, LOG_ERR);
2611 // The merge itself is committed, so this is reported as a warning and not as a failure
2612 $this->warnings[] = $langs->trans('WarningMergeFilesNotMoved', implode(', ', $failed));
2613 }
2614 }
2615
2623 public function fetchRoles()
2624 {
2625 global $langs;
2626 $error = 0;
2627 $num = 0;
2628
2629 $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
2630 $sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";
2631 $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
2632 $sql .= " AND tc.source = 'external' AND tc.active=1";
2633 $sql .= " AND sc.fk_socpeople = ".((int) $this->id);
2634 $sql .= " AND sc.entity IN (".getEntity('societe').')';
2635
2636 $resql = $this->db->query($sql);
2637 if ($resql) {
2638 $this->roles = array();
2639
2640 $num = $this->db->num_rows($resql);
2641 if ($num > 0) {
2642 while ($obj = $this->db->fetch_object($resql)) {
2643 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
2644 $label_element = $langs->trans('ContactDefault_'.$obj->element);
2645 $this->roles[$obj->contactroleid] = array('id' => $obj->rowid, 'socid' => $obj->socid, 'element' => $obj->element, 'source' => $obj->source, 'code' => $obj->code, 'label' => $label_element.' - '.($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->label));
2646 }
2647 }
2648 } else {
2649 $error++;
2650 $this->error = $this->db->lasterror();
2651 $this->errors[] = $this->db->lasterror();
2652 }
2653
2654 if (empty($error)) {
2655 return $num;
2656 } else {
2657 return $error * -1;
2658 }
2659 }
2660
2668 public function getContactRoles($element = '')
2669 {
2670 $tab = array();
2671
2672 if ($element == 'action') {
2673 $element = 'agenda';
2674 }
2675
2676 $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
2677 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
2678 $sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";
2679 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."socpeople sp";
2680 $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
2681 $sql .= " WHERE sc.fk_soc = ".((int) $this->socid);
2682 $sql .= " AND sc.fk_c_type_contact=tc.rowid";
2683 $sql .= " AND tc.element = '".$this->db->escape($element)."'";
2684 $sql .= " AND sp.entity IN (".getEntity('contact').")";
2685 $sql .= " AND tc.active = 1";
2686
2687 $resql = $this->db->query($sql);
2688 if ($resql) {
2689 $num = $this->db->num_rows($resql);
2690 $i = 0;
2691 while ($i < $num) {
2692 $obj = $this->db->fetch_object($resql);
2693 $tab[$obj->id] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
2694
2695 $i++;
2696 }
2697
2698 return $tab;
2699 } else {
2700 $this->error = $this->db->error();
2701 dol_print_error($this->db);
2702 return -1;
2703 }
2704 }
2705
2713 public function updateRoles()
2714 {
2715 global $conf;
2716
2717 $error = 0;
2718
2719 if (!isset($this->roles)) {
2720 return 0; // Avoid to loose roles when property not set
2721 }
2722
2723 $this->db->begin();
2724
2725 $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("contact").")";
2726
2727 $result = $this->db->query($sql);
2728 if (!$result) {
2729 $this->errors[] = $this->db->lasterror().' sql='.$sql;
2730 $error++;
2731 } else {
2732 if (count($this->roles) > 0) {
2733 foreach ($this->roles as $keyRoles => $valRoles) {
2734 if (empty($valRoles)) {
2735 continue;
2736 }
2737 $idrole = 0;
2738 if (is_array($valRoles)) {
2739 $idrole = $valRoles['id'];
2740 } else {
2741 $idrole = $valRoles;
2742 }
2743
2744 $socid = 0;
2745 if (is_array($valRoles)) {
2746 $socid = $valRoles['socid'];
2747 } else {
2748 $socid = $this->socid;
2749 }
2750
2751 if ($socid > 0) {
2752 $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
2753 $sql .= " (entity,";
2754 $sql .= "date_creation,";
2755 $sql .= "fk_soc,";
2756 $sql .= "fk_c_type_contact,";
2757 $sql .= "fk_socpeople) ";
2758 $sql .= " VALUES (".((int) $conf->entity).",";
2759 $sql .= "'".$this->db->idate(dol_now())."',";
2760 $sql .= ((int) $socid).", ";
2761 $sql .= ((int) $idrole)." , ";
2762 $sql .= ((int) $this->id);
2763 $sql .= ")";
2764
2765 $result = $this->db->query($sql);
2766 if (!$result) {
2767 $this->errors[] = $this->db->lasterror().' sql='.$sql;
2768 $error++;
2769 }
2770 }
2771 }
2772 }
2773 }
2774 if (empty($error)) {
2775 $this->db->commit();
2776 return 1;
2777 } else {
2778 $this->error = implode(' ', $this->errors);
2779 $this->db->rollback();
2780 return $error * -1;
2781 }
2782 }
2783
2790 public function loadCacheOfProspStatus($active = 1)
2791 {
2792 global $langs;
2793
2794 $sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";
2795 if ($active >= 0) {
2796 $sql .= " WHERE active = ".((int) $active);
2797 }
2798 $resql = $this->db->query($sql);
2799 $num = $this->db->num_rows($resql);
2800 $i = 0;
2801 while ($i < $num) {
2802 $obj = $this->db->fetch_object($resql);
2803 $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);
2804 $i++;
2805 }
2806 return 1;
2807 }
2808
2814 public function getLibProspLevel()
2815 {
2816 return $this->libProspLevel($this->fk_prospectlevel);
2817 }
2818
2825 public function libProspLevel($fk_prospectlevel)
2826 {
2827 global $langs;
2828
2829 $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
2830 // If lib not found in language file, we get label from cache/database
2831 if ($lib == "ProspectLevel".$fk_prospectlevel) {
2832 $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2833 }
2834 return $lib;
2835 }
2836
2837
2845 public function setProspectLevel(User $user)
2846 {
2847 return $this->update($this->id, $user);
2848 }
2849
2857 public function getLibProspCommStatut($mode = 0, $label = '')
2858 {
2859 return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2860 }
2861
2875 public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2876 {
2877 global $langs;
2878 $langs->load('customers');
2879
2880 if ($mode == 2) {
2881 if ($statut == '-1' || $statut == 'ST_NO') {
2882 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2883 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2884 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2885 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2886 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2887 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2888 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2889 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2890 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2891 } else {
2892 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);
2893 }
2894 }
2895 if ($mode == 3) {
2896 if ($statut == '-1' || $statut == 'ST_NO') {
2897 return img_action($langs->trans("StatusProspect-1"), '-1', $picto);
2898 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2899 return img_action($langs->trans("StatusProspect0"), '0', $picto);
2900 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2901 return img_action($langs->trans("StatusProspect1"), '1', $picto);
2902 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2903 return img_action($langs->trans("StatusProspect2"), '2', $picto);
2904 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2905 return img_action($langs->trans("StatusProspect3"), '3', $picto);
2906 } else {
2907 return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, '0', $picto);
2908 }
2909 }
2910 if ($mode == 4) {
2911 if ($statut == '-1' || $statut == 'ST_NO') {
2912 return img_action($langs->trans("StatusProspect-1"), '-1', $picto).' '.$langs->trans("StatusProspect-1");
2913 } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2914 return img_action($langs->trans("StatusProspect0"), '0', $picto).' '.$langs->trans("StatusProspect0");
2915 } elseif ($statut == '1' || $statut == 'ST_TODO') {
2916 return img_action($langs->trans("StatusProspect1"), '1', $picto).' '.$langs->trans("StatusProspect1");
2917 } elseif ($statut == '2' || $statut == 'ST_PEND') {
2918 return img_action($langs->trans("StatusProspect2"), '2', $picto).' '.$langs->trans("StatusProspect2");
2919 } elseif ($statut == '3' || $statut == 'ST_DONE') {
2920 return img_action($langs->trans("StatusProspect3"), '3', $picto).' '.$langs->trans("StatusProspect3");
2921 } else {
2922 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);
2923 }
2924 }
2925
2926 return "Error, mode/status not found";
2927 }
2928
2929
2936 public function setNoEmail($no_email)
2937 {
2938 $error = 0;
2939
2940 // Update mass emailing flag into table mailing_unsubscribe
2941 if ($this->email) {
2942 $this->db->begin();
2943
2944 if ($no_email) {
2945 $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)."'";
2946 $resql = $this->db->query($sql);
2947 if ($resql) {
2948 $obj = $this->db->fetch_object($resql);
2949 $noemail = $obj->nb;
2950 if (empty($noemail)) {
2951 $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())."')";
2952 $resql = $this->db->query($sql);
2953 if (!$resql) {
2954 $error++;
2955 $this->error = $this->db->lasterror();
2956 $this->errors[] = $this->error;
2957 }
2958 }
2959 } else {
2960 $error++;
2961 $this->error = $this->db->lasterror();
2962 $this->errors[] = $this->error;
2963 }
2964 } else {
2965 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
2966 $resql = $this->db->query($sql);
2967 if (!$resql) {
2968 $error++;
2969 $this->error = $this->db->lasterror();
2970 $this->errors[] = $this->error;
2971 }
2972 }
2973
2974 if (empty($error)) {
2975 $this->no_email = $no_email;
2976 $this->db->commit();
2977 return 1;
2978 } else {
2979 $this->db->rollback();
2980 return $error * -1;
2981 }
2982 }
2983
2984 return 0;
2985 }
2986
2993 public function getNoEmail()
2994 {
2995 if ($this->email) {
2996 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
2997 $resql = $this->db->query($sql);
2998 if ($resql) {
2999 $obj = $this->db->fetch_object($resql);
3000 $this->no_email = $obj->nb;
3001 return 1;
3002 } else {
3003 $this->error = $this->db->lasterror();
3004 $this->errors[] = $this->error;
3005 return -1;
3006 }
3007 }
3008 return 0;
3009 }
3010
3011
3019 public function getKanbanView($option = '', $arraydata = null)
3020 {
3021 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
3022
3023 $return = '<div class="box-flex-item box-flex-grow-zero">';
3024 $return .= '<div class="info-box info-box-sm">';
3025 $return .= '<span class="info-box-icon bg-infobox-action">';
3026 if (!is_null($this->photo)) {
3027 $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
3028 } else {
3029 $return .= img_picto('', $this->picto);
3030 }
3031 $return .= '</span>';
3032 $return .= '<div class="info-box-content">';
3033 $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->getNomUrl(0) . '</div>';
3034 if ($selected >= 0) {
3035 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
3036 }
3037 if (is_object($this->thirdparty)) {
3038 $return .= '<div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
3039 }
3040 /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
3041 $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
3042 $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
3043 }*/
3044 /*if (method_exists($this, 'LibPubPriv')) {
3045 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
3046 $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
3047 }*/
3048 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
3049 $return .= '</div>';
3050 $return .= '</div>';
3051 $return .= '</div>';
3052 return $return;
3053 }
3054}
$object ref
Definition info.php:90
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage categories.
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...
deleteEcmFiles($mode=0)
Delete related files of object in database.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
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.
static commonReplaceContact(DoliDB $dbs, $origin_id, $dest_id, array $tables, $fieldname='fk_socpeople', $ignoreerrors=0)
Function used to replace a contact id with another one.
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.
getParentId($contactid)
Return the id of the parent contact of a contact, 0 if none.
$fields
'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter...
create($user, $notrigger=0)
Add a contact into database.
loadStateBoard()
Load indicators into this->nb for board.
static replaceContact(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a contact id with another one when merging two contacts.
getNoEmail()
get "blacklist" mailing status set no_email attribute to 1 or 0
getNomUrl($withpicto=0, $option='', $notooltip=0, $moreparam='', $save_lastsearch_value=-1, $maxlen=0, $morecss='valignmiddle')
Return name of contact with link (and eventually picto) Use $this->id, $this->lastname,...
getNbOfEMailings()
Return number of mass Emailing received by these contacts with its email.
isLinkedToUser($contactid)
Tell whether a user account is linked to the given contact.
libProspCommStatut($statut, $mode=0, $label='', $picto='')
Return label of a given status.
getTooltipContentArray($params)
getTooltipContentArray
mergeContactOptionalTables($contact_origin)
Move the data stored by the optional modules and by the notification system.
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.
isSalesRepresentativeOf($socid)
Tell whether the current user is a sales representative of the given third party.
setCategories($categories, $remove_existing=true)
Sets object to supplied categories.
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.
LibPubPriv($status, $decorate=0)
Return translated label of Public or Private.
setstatus($status)
Change status of a user.
loadCacheOfProspStatus($active=1)
Load array of prospect status.
mergeContact($contact_origin_id)
Merge a contact with the current one, deleting the given contact $contact_origin_id.
_load_ldap_info()
Initialize info table (LDAP attributes table)
mergeContactFiles($contact_origin_id)
Move the files of the merged contact into the directory of the target contact.
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.
mergeContactChildren($contact_origin)
Move the children of the merged contact to the target contact.
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.
prefix()
Return the DB prefix found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=....
sanitize($stringtosanitize, $allowsimplequote=0, $allowsequals=0, $allowsspace=0, $allowschars=1)
Sanitize a string for SQL forging.
static showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='', $addlinktofullsize=1, $cache=0, $forcecapture='', $noexternsourceoverwrite=0, $usesharelinkifavailable=0)
Return HTML code to output a photo.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
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.
getCountry($searchkey, $withcode='', $dbtouse=null, $outputlangs=null, $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array(), $entity=null)
Move a file into another name.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_is_file($pathoffile)
Return if path is a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:66
dol_is_dir($folder)
Test if filename is a directory.
deleteFilesIntoDatabaseIndex($dir, $file, $mode='uploaded', $object=null)
Delete files into database index using search criteria.
dol_now($mode='gmt')
Return date for now.
setEntity($currentobject)
Set entity id to use when to create an object.
dol_print_phone($phone, $countrycode='', $contactid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
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.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
img_action($titlealt, $numaction, $picto='', $moreatt='')
Show logo action.
DDLListTables($database, $table='')
List tables into a database.
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:165
div refaddress div address