Passed
Pull Request — dev (#8)
by Rafael
58:47
created

Contact::initAsSpecimen()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 43
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 32
nc 3
nop 0
dl 0
loc 43
rs 9.408
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2002-2004  Rodolphe Quiedeville        <[email protected]>
4
 * Copyright (C) 2004       Benoit Mortier              <[email protected]>
5
 * Copyright (C) 2004-2013  Laurent Destailleur         <[email protected]>
6
 * Copyright (C) 2005-2012  Regis Houssin               <[email protected]>
7
 * Copyright (C) 2007       Franky Van Liedekerke       <[email protected]>
8
 * Copyright (C) 2008       Raphael Bertrand (Resultic) <[email protected]>
9
 * Copyright (C) 2013       Florian Henry		  	    <[email protected]>
10
 * Copyright (C) 2013       Alexandre Spangaro 	        <[email protected]>
11
 * Copyright (C) 2013       Juanjo Menent	 	        <[email protected]>
12
 * Copyright (C) 2015       Marcos García               <[email protected]>
13
 * Copyright (C) 2019       Nicolas ZABOURI 	        <[email protected]>
14
 * Copyright (C) 2020       Open-Dsi  	                <[email protected]>
15
 * Copyright (C) 2024       Frédéric France             <[email protected]>
16
 * Copyright (C) 2024		MDW							<[email protected]>
17
 * Copyright (C) 2024       Rafael San José             <[email protected]>
18
 *
19
 * This program is free software; you can redistribute it and/or modify
20
 * it under the terms of the GNU General Public License as published by
21
 * the Free Software Foundation; either version 3 of the License, or
22
 * (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31
 */
32
33
namespace Dolibarr\Code\Contact\Classes;
34
35
use Dolibarr\Code\Categories\Classes\Categorie;
36
use Dolibarr\Code\Core\Classes\Form;
37
use Dolibarr\Code\Core\Traits\CommonPeople;
38
use Dolibarr\Code\Core\Traits\CommonSocialNetworks;
39
use Dolibarr\Code\User\Classes\User;
40
use Dolibarr\Core\Base\CommonObject;
41
use DoliDB;
42
use Exception;
43
44
/**
45
 *  \file       htdocs/contact/class/contact.class.php
46
 *  \ingroup    societe
47
 *  \brief      File of contacts class
48
 */
49
50
/**
51
 *  Class to manage contact/addresses
52
 */
53
class Contact extends CommonObject
54
{
55
    use CommonSocialNetworks;
56
    use CommonPeople;
0 ignored issues
show
introduced by
The trait Dolibarr\Code\Core\Traits\CommonPeople requires some properties which are not provided by Dolibarr\Code\Contact\Classes\Contact: $MAIN_SHOW_REGION_IN_STATE_SELECT, $user_mobile, $attributes, $company, $global, $nom, $use_javascript_ajax, $societe, $phone
Loading history...
57
58
    /**
59
     * @var string ID to identify managed object
60
     */
61
    public $element = 'contact';
62
63
    /**
64
     * @var string Name of table without prefix where object is stored
65
     */
66
    public $table_element = 'socpeople';
67
68
    /**
69
     * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
70
     */
71
    public $picto = 'contact';
72
73
    /**
74
     *  'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
75
     *         Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
76
     *  'label' the translation key.
77
     *  'enabled' is a condition when the field must be managed.
78
     *  'position' is the sort order of field.
79
     *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
80
     *  'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
81
     *  'noteditable' says if field is not editable (1 or 0)
82
     *  'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
83
     *  'index' if we want an index in database.
84
     *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommended to name the field fk_...).
85
     *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.
86
     *  'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
87
     *  'css' is the CSS style to use on field. For example: 'maxwidth200'
88
     *  'help' is a string visible as a tooltip on field
89
     *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record
90
     *  'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
91
     *  'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
92
     *  'comment' is not used. You can store here any text of your choice. It is not used by application.
93
     *
94
     *  Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
95
     */
96
97
    // BEGIN MODULEBUILDER PROPERTIES
98
    /**
99
     * @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}>  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,array{type:...ring>,comment?:string}> at position 16 could not be parsed: Expected '}' at position 16, but found 'int'.
Loading history...
100
     */
101
    public $fields = array(
102
        'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'),
103
        'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 30, 'index' => 1),
104
        'ref_ext' => array('type' => 'varchar(255)', 'label' => 'Ref ext', 'enabled' => 1, 'visible' => 3, 'position' => 35),
105
        'civility' => array('type' => 'varchar(6)', 'label' => 'Civility', 'enabled' => 1, 'visible' => 3, 'position' => 40),
106
        'lastname' => array('type' => 'varchar(50)', 'label' => 'Lastname', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'showoncombobox' => 1, 'searchall' => 1),
107
        'name_alias' => array('type' => 'varchar(255)', 'label' => 'Name alias', 'enabled' => 1, 'visible' => -1, 'position' => 46, 'searchall' => 1),
108
        'firstname' => array('type' => 'varchar(50)', 'label' => 'Firstname', 'enabled' => 1, 'visible' => 1, 'position' => 50, 'showoncombobox' => 1, 'searchall' => 1),
109
        'poste' => array('type' => 'varchar(80)', 'label' => 'PostOrFunction', 'enabled' => 1, 'visible' => -1, 'position' => 52),
110
        'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 55),
111
        'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 60),
112
        'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 65),
113
        'fk_departement' => array('type' => 'integer', 'label' => 'Fk departement', 'enabled' => 1, 'visible' => 3, 'position' => 70),
114
        'fk_pays' => array('type' => 'integer', 'label' => 'Fk pays', 'enabled' => 1, 'visible' => 3, 'position' => 75),
115
        'fk_soc' => array('type' => 'integer', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => 1, 'position' => 77, 'searchall' => 1),
116
        'birthday' => array('type' => 'date', 'label' => 'Birthday', 'enabled' => 1, 'visible' => -1, 'position' => 80),
117
        'phone' => array('type' => 'varchar(30)', 'label' => 'Phone', 'enabled' => 1, 'visible' => 1, 'position' => 90, 'searchall' => 1),
118
        'phone_perso' => array('type' => 'varchar(30)', 'label' => 'PhonePerso', 'enabled' => 1, 'visible' => -1, 'position' => 95, 'searchall' => 1),
119
        'phone_mobile' => array('type' => 'varchar(30)', 'label' => 'PhoneMobile', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'searchall' => 1),
120
        'fax' => array('type' => 'varchar(30)', 'label' => 'Fax', 'enabled' => 1, 'visible' => -1, 'position' => 105, 'searchall' => 1),
121
        'email' => array('type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'visible' => 1, 'position' => 110, 'searchall' => 1),
122
        'socialnetworks' => array('type' => 'text', 'label' => 'SocialNetworks', 'enabled' => 1, 'visible' => 3, 'position' => 115),
123
        'photo' => array('type' => 'varchar(255)', 'label' => 'Photo', 'enabled' => 1, 'visible' => 3, 'position' => 170),
124
        'priv' => array('type' => 'smallint(6)', 'label' => 'ContactVisibility', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 175),
125
        'fk_stcommcontact' => array('type' => 'integer', 'label' => 'ProspectStatus', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 220),
126
        'fk_prospectcontactlevel' => array('type' => 'varchar(12)', 'label' => 'ProspectLevel', 'enabled' => 1, 'visible' => -1, 'position' => 255),
127
        //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),
128
        'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 3, 'position' => 195, 'searchall' => 1),
129
        'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 3, 'position' => 200, 'searchall' => 1),
130
        'default_lang' => array('type' => 'varchar(6)', 'label' => 'Default lang', 'enabled' => 1, 'visible' => 3, 'position' => 205),
131
        'canvas' => array('type' => 'varchar(32)', 'label' => 'Canvas', 'enabled' => 1, 'visible' => 3, 'position' => 210),
132
        'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 300),
133
        'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305),
134
        'fk_user_creat' => array('type' => 'integer', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 3, 'position' => 310),
135
        'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 3, 'position' => 315),
136
        'statut' => array('type' => 'tinyint(4)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500),
137
        'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000),
138
    );
139
140
    public $civility_id; // In fact we store civility_code
141
    public $civility_code;
142
    public $civility;
143
144
    /**
145
     * @var string gender
146
     */
147
    public $gender;
148
149
    /**
150
     * @var int egroupware_id
151
     */
152
    //private $egroupware_id;
153
154
    /**
155
     * @var int birthday_alert
156
     */
157
    public $birthday_alert;
158
159
    /**
160
     * @var string The civilite code, not an integer
161
     * @deprecated
162
     * @see $civility_code
163
     */
164
    public $civilite;
165
166
    /**
167
     * @var string fullname
168
     */
169
    public $fullname;
170
171
    /**
172
     * @var string Name alias
173
     */
174
    public $name_alias;
175
176
    /**
177
     * @var string Address
178
     */
179
    public $address;
180
181
    /**
182
     * @var string zip code
183
     */
184
    public $zip;
185
186
    /**
187
     * @var string Town
188
     */
189
    public $town;
190
191
    /**
192
     * @var int  Id of department
193
     */
194
    public $state_id;
195
196
    /**
197
     * @var string  Code of department
198
     */
199
    public $state_code;
200
201
    /**
202
     * @var string  Label of department
203
     */
204
    public $state;
205
206
    /**
207
     * @var string  Job Position
208
     */
209
    public $poste;
210
211
    /**
212
     * @var int Thirdparty ID
213
     */
214
    public $socid;      // both socid and fk_soc are used
215
    public $fk_soc;     // both socid and fk_soc are used
216
217
    /**
218
     * @var string Thirdparty name
219
     */
220
    public $socname;
221
222
    /**
223
     * @var int  Status 0=inactive, 1=active
224
     */
225
    public $statut;
226
227
    public $code;
228
229
    /**
230
     * Email
231
     * @var string
232
     */
233
    public $email;
234
235
    /**
236
     * Email
237
     * @var string
238
     * @deprecated
239
     * @see $email
240
     */
241
    public $mail;
242
243
    /**
244
     * URL
245
     * @var string
246
     */
247
    public $url;
248
249
    /**
250
     * Unsubscribe all : 1 = contact has globally unsubscribed of all mass emailing
251
     * @var int
252
     * @deprecated Has been replaced by a search into llx_mailing_unsubscribe
253
     */
254
    public $no_email;
255
256
    /**
257
     * Array of social-networks
258
     * @var array
259
     */
260
    public $socialnetworks;
261
262
    /**
263
     * @var string filename for photo
264
     */
265
    public $photo;
266
267
    /**
268
     * @var string phone pro (professional/business)
269
     */
270
    public $phone_pro;
271
272
    /**
273
     * @var string phone perso (personal/private)
274
     */
275
    public $phone_perso;
276
277
    /**
278
     * @var string phone mobile
279
     */
280
    public $phone_mobile;
281
282
    /**
283
     * @var string fax
284
     */
285
    public $fax;
286
287
    /**
288
     * Private or public
289
     * @var int
290
     */
291
    public $priv;
292
293
    /**
294
     * @var int|string Date
295
     */
296
    public $birthday;
297
298
    /**
299
     * @var string language for contact communication  -- only with multilanguage enabled
300
     */
301
    public $default_lang;
302
303
    /**
304
     * @var int Number of invoices for which he is contact
305
     */
306
    public $ref_facturation;
307
308
    /**
309
     * @var int  Number of contracts for which he is contact
310
     */
311
    public $ref_contrat;
312
313
    /**
314
     * @var int Number of orders for which he is contact
315
     */
316
    public $ref_commande;
317
318
    /**
319
     * @var int Number of proposals for which he is contact
320
     */
321
    public $ref_propal;
322
323
    /**
324
     * @var int user ID
325
     */
326
    public $user_id;
327
328
    /**
329
     * @var string user login
330
     */
331
    public $user_login;
332
333
    // END MODULEBUILDER PROPERTIES
334
335
336
    /**
337
     * Old copy
338
     * @var static
339
     */
340
    public $oldcopy; // To contains a clone of this when we need to save old properties of object
341
342
    /**
343
     * @var array roles
344
     */
345
    public $roles;
346
347
    public $cacheprospectstatus = array();
348
349
    /**
350
     * @var string  Prospect level. ie: 'PL_LOW', 'PL...'
351
     */
352
    public $fk_prospectlevel;
353
354
    public $stcomm_id;
355
356
    public $statut_commercial;
357
358
    /**
359
     * @var string picto
360
     */
361
    public $stcomm_picto;
362
363
364
    /**
365
     *  Constructor
366
     *
367
     *  @param      DoliDB      $db      Database handler
368
     */
369
    public function __construct($db)
370
    {
371
        $this->db = $db;
372
        $this->statut = 1; // By default, status is enabled
373
        $this->ismultientitymanaged = 1;
374
        $this->isextrafieldmanaged = 1;
375
376
        if (!isModEnabled('mailing')) {
377
            $this->fields['no_email']['enabled'] = 0;
378
        }
379
        // typical ['s.nom'] is used for third-parties
380
        if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) {
381
            $this->fields['fk_soc']['enabled'] = 0;
382
            $this->fields['fk_soc']['searchall'] = 0;
383
        }
384
385
        // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty
386
        if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) {  // Default behaviour
387
            $this->fields['fk_stcommcontact']['enabled'] = 0;
388
            $this->fields['fk_prospectcontactlevel']['enabled'] = 0;
389
        }
390
391
        // Unset fields that are disabled
392
        foreach ($this->fields as $key => $val) {
393
            if (isset($val['enabled']) && empty($val['enabled'])) {
394
                unset($this->fields[$key]);
395
            }
396
        }
397
398
        // Translate some data of arrayofkeyval
399
        /*if (is_object($langs))
400
        {
401
            foreach($this->fields as $key => $val)
402
            {
403
                if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
404
                {
405
                    foreach($val['arrayofkeyval'] as $key2 => $val2)
406
                    {
407
                        $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2);
408
                    }
409
                }
410
            }
411
        }*/
412
    }
413
414
    /**
415
     *  Load indicators into this->nb for board
416
     *
417
     *  @return     int         Return integer <0 if KO, >0 if OK
418
     */
419
    public function loadStateBoard()
420
    {
421
        global $user, $hookmanager;
422
423
        $this->nb = array();
424
        $clause = "WHERE";
425
426
        $sql = "SELECT count(sp.rowid) as nb";
427
        $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as sp";
428
        if (!$user->hasRight('societe', 'client', 'voir')) {
429
            $sql .= ", " . MAIN_DB_PREFIX . "societe as s";
430
            $sql .= ", " . MAIN_DB_PREFIX . "societe_commerciaux as sc";
431
            $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = " . ((int) $user->id);
432
            $clause = "AND";
433
        }
434
        $sql .= " " . $clause . " sp.entity IN (" . getEntity($this->element) . ")";
435
        $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = " . ((int) $user->id) . "))";
436
        if ($user->socid > 0) {
437
            $sql .= " AND sp.fk_soc = " . ((int) $user->socid);
438
        }
439
        // Add where from hooks
440
        if (is_object($hookmanager)) {
441
            $parameters = array();
442
            $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook
443
            $sql .= $hookmanager->resPrint;
444
        }
445
446
        $resql = $this->db->query($sql);
447
        if ($resql) {
448
            while ($obj = $this->db->fetch_object($resql)) {
449
                $this->nb["contacts"] = $obj->nb;
450
            }
451
            $this->db->free($resql);
452
            return 1;
453
        } else {
454
            dol_print_error($this->db);
455
            $this->error = $this->db->lasterror();
456
            return -1;
457
        }
458
    }
459
460
    /**
461
     *  Add a contact into database
462
     *
463
     *  @param      User    $user           Object user that create
464
     *  @param      int     $notrigger      1=Does not execute triggers, 0= execute triggers
465
     *  @return     int                     Return integer <0 if KO, >0 if OK
466
     */
467
    public function create($user, $notrigger = 0)
468
    {
469
        global $conf;
470
471
        $error = 0;
472
        $now = dol_now();
473
474
        $this->db->begin();
475
476
        // Clean parameters
477
        $this->name_alias = trim($this->name_alias);
478
        $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name);
479
        $this->firstname = trim($this->firstname);
480
        $this->setUpperOrLowerCase();
481
        if (empty($this->socid)) {
482
            $this->socid = 0;
483
        }
484
        if (empty($this->priv)) {
485
            $this->priv = 0;
486
        }
487
        if (empty($this->statut)) {
488
            $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request
489
        }
490
491
        $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
492
493
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "socpeople (";
494
        $sql .= " datec";
495
        $sql .= ", fk_soc";
496
        $sql .= ", name_alias";
497
        $sql .= ", lastname";
498
        $sql .= ", firstname";
499
        $sql .= ", fk_user_creat";
500
        $sql .= ", priv";
501
        $sql .= ", fk_stcommcontact";
502
        $sql .= ", statut";
503
        $sql .= ", canvas";
504
        $sql .= ", entity";
505
        $sql .= ", ref_ext";
506
        $sql .= ", import_key";
507
        $sql .= ") VALUES (";
508
        $sql .= "'" . $this->db->idate($now) . "',";
509
        if ($this->socid > 0) {
510
            $sql .= " " . ((int) $this->socid) . ",";
511
        } else {
512
            $sql .= "null,";
513
        }
514
        $sql .= "'" . $this->db->escape($this->name_alias) . "',";
515
        $sql .= "'" . $this->db->escape($this->lastname) . "',";
516
        $sql .= "'" . $this->db->escape($this->firstname) . "',";
517
        $sql .= " " . ($user->id > 0 ? ((int) $user->id) : "null") . ",";
518
        $sql .= " " . ((int) $this->priv) . ",";
519
        $sql .= " 0,";
520
        $sql .= " " . ((int) $this->statut) . ",";
521
        $sql .= " " . (!empty($this->canvas) ? "'" . $this->db->escape($this->canvas) . "'" : "null") . ",";
522
        $sql .= " " . ((int) $this->entity) . ",";
523
        $sql .= "'" . $this->db->escape($this->ref_ext) . "',";
524
        $sql .= " " . (!empty($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null");
525
        $sql .= ")";
526
527
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
528
        $resql = $this->db->query($sql);
529
        if ($resql) {
530
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "socpeople");
531
532
            if (!$error) {
533
                $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...
534
                if ($result < 0) {
535
                    $error++;
536
                    $this->error = $this->db->lasterror();
537
                }
538
            }
539
540
            if (!$error) {
541
                $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update
542
                if ($result < 0) {
543
                    $error++;
544
                    $this->error = $this->db->lasterror();
545
                }
546
            }
547
548
            if (!$error && !$notrigger) {
549
                // Call trigger
550
                $result = $this->call_trigger('CONTACT_CREATE', $user);
551
                if ($result < 0) {
552
                    $error++;
553
                }
554
                // End call triggers
555
            }
556
557
            if (!$error) {
558
                $this->db->commit();
559
                return $this->id;
560
            } else {
561
                $this->db->rollback();
562
                dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
563
                return -2;
564
            }
565
        } else {
566
            $this->error = $this->db->lasterror();
567
568
            $this->db->rollback();
569
            dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
570
            return -1;
571
        }
572
    }
573
574
    /**
575
     *      Update information into database
576
     *
577
     *      @param      int     $id             Id of contact/address to update
578
     *      @param      User    $user           Object user making change
579
     *      @param      int     $notrigger      0=no, 1=yes
580
     *      @param      string  $action         Current action for hookmanager
581
     *      @param      int     $nosyncuser     No sync linked user (external users and contacts are linked)
582
     *      @return     int                     Return integer <0 if KO, >0 if OK
583
     */
584
    public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0)
585
    {
586
        global $conf;
587
588
        $error = 0;
589
590
        $this->id = $id;
591
592
        $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
593
594
        // Clean parameters
595
        $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext));
596
        $this->name_alias = trim($this->name_alias);
597
        $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
598
        $this->firstname = trim($this->firstname);
599
        $this->email = trim($this->email);
600
        $this->phone_pro = trim($this->phone_pro);
601
        $this->phone_perso = trim($this->phone_perso);
602
        $this->phone_mobile = trim($this->phone_mobile);
603
        $this->photo = trim($this->photo);
604
        $this->fax = trim($this->fax);
605
        $this->zip = (empty($this->zip) ? '' : trim($this->zip));
606
        $this->town = (empty($this->town) ? '' : trim($this->town));
607
        $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id;
608
        if (empty($this->statut)) {
609
            $this->statut = 0;
610
        }
611
        if (empty($this->civility_code) && !is_numeric($this->civility_id)) {
612
            $this->civility_code = $this->civility_id; // For backward compatibility
613
        }
614
        $this->setUpperOrLowerCase();
615
616
        $this->db->begin();
617
618
        $sql = "UPDATE " . MAIN_DB_PREFIX . "socpeople SET";
619
        if ($this->socid > 0) {
620
            $sql .= " fk_soc = " . ((int) $this->socid) . ",";
621
        } elseif ($this->socid == -1) {
622
            $sql .= " fk_soc = NULL,";
623
        }
624
        $sql .= " civility='" . $this->db->escape($this->civility_code) . "'";
625
        $sql .= ", name_alias='" . $this->db->escape($this->name_alias) . "'";
626
        $sql .= ", lastname='" . $this->db->escape($this->lastname) . "'";
627
        $sql .= ", firstname='" . $this->db->escape($this->firstname) . "'";
628
        $sql .= ", address='" . $this->db->escape($this->address) . "'";
629
        $sql .= ", zip='" . $this->db->escape($this->zip) . "'";
630
        $sql .= ", town='" . $this->db->escape($this->town) . "'";
631
        $sql .= ", ref_ext = " . (!empty($this->ref_ext) ? "'" . $this->db->escape($this->ref_ext) . "'" : "NULL");
632
        $sql .= ", fk_pays=" . ($this->country_id > 0 ? $this->country_id : 'NULL');
633
        $sql .= ", fk_departement=" . ($this->state_id > 0 ? $this->state_id : 'NULL');
634
        $sql .= ", poste='" . $this->db->escape($this->poste) . "'";
635
        $sql .= ", fax='" . $this->db->escape($this->fax) . "'";
636
        $sql .= ", email='" . $this->db->escape($this->email) . "'";
637
        $sql .= ", socialnetworks = '" . $this->db->escape(json_encode($this->socialnetworks)) . "'";
638
        $sql .= ", photo='" . $this->db->escape($this->photo) . "'";
639
        $sql .= ", birthday=" . ($this->birthday ? "'" . $this->db->idate($this->birthday) . "'" : "null");
640
        $sql .= ", note_private = " . (isset($this->note_private) ? "'" . $this->db->escape($this->note_private) . "'" : "NULL");
641
        $sql .= ", note_public = " . (isset($this->note_public) ? "'" . $this->db->escape($this->note_public) . "'" : "NULL");
642
        $sql .= ", phone = " . (isset($this->phone_pro) ? "'" . $this->db->escape($this->phone_pro) . "'" : "NULL");
643
        $sql .= ", phone_perso = " . (isset($this->phone_perso) ? "'" . $this->db->escape($this->phone_perso) . "'" : "NULL");
644
        $sql .= ", phone_mobile = " . (isset($this->phone_mobile) ? "'" . $this->db->escape($this->phone_mobile) . "'" : "NULL");
645
        $sql .= ", priv = '" . $this->db->escape($this->priv) . "'";
646
        $sql .= ", fk_prospectlevel = '" . $this->db->escape($this->fk_prospectlevel) . "'";
647
        if (isset($this->stcomm_id)) {
648
            $sql .= ", fk_stcommcontact = " . ($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");
649
        }
650
        $sql .= ", statut = " . ((int) $this->statut);
651
        $sql .= ", fk_user_modif=" . ($user->id > 0 ? "'" . $this->db->escape($user->id) . "'" : "NULL");
652
        $sql .= ", default_lang=" . ($this->default_lang ? "'" . $this->db->escape($this->default_lang) . "'" : "NULL");
653
        $sql .= ", entity = " . ((int) $this->entity);
654
        $sql .= " WHERE rowid = " . ((int) $id);
655
656
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
657
        $result = $this->db->query($sql);
658
        if ($result) {
659
            unset($this->country_code);
660
            unset($this->country);
661
            unset($this->state_code);
662
            unset($this->state);
663
664
            $action = 'update';
665
666
            // Actions on extra fields
667
            if (!$error) {
668
                $result = $this->insertExtraFields();
669
                if ($result < 0) {
670
                    $error++;
671
                }
672
            }
673
674
            if (!$error) {
675
                $result = $this->updateRoles();
676
                if ($result < 0) {
677
                    $error++;
678
                }
679
            }
680
681
            if (!$error && $this->user_id > 0) {
682
                // If contact is linked to a user
683
                $tmpobj = new User($this->db);
684
                $tmpobj->fetch($this->user_id);
685
                $usermustbemodified = 0;
686
                if ($tmpobj->office_phone != $this->phone_pro) {
687
                    $tmpobj->office_phone = $this->phone_pro;
688
                    $usermustbemodified++;
689
                }
690
                if ($tmpobj->office_fax != $this->fax) {
691
                    $tmpobj->office_fax = $this->fax;
692
                    $usermustbemodified++;
693
                }
694
                if ($tmpobj->address != $this->address) {
695
                    $tmpobj->address = $this->address;
696
                    $usermustbemodified++;
697
                }
698
                if ($tmpobj->town != $this->town) {
699
                    $tmpobj->town = $this->town;
700
                    $usermustbemodified++;
701
                }
702
                if ($tmpobj->zip != $this->zip) {
703
                    $tmpobj->zip = $this->zip;
704
                    $usermustbemodified++;
705
                }
706
                if ($tmpobj->zip != $this->zip) {
707
                    $tmpobj->state_id = $this->state_id;
708
                    $usermustbemodified++;
709
                }
710
                if ($tmpobj->country_id != $this->country_id) {
711
                    $tmpobj->country_id = $this->country_id;
712
                    $usermustbemodified++;
713
                }
714
                if ($tmpobj->email != $this->email) {
715
                    $tmpobj->email = $this->email;
716
                    $usermustbemodified++;
717
                }
718
                if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) {
719
                    $tmpobj->socialnetworks = $this->socialnetworks;
720
                    $usermustbemodified++;
721
                }
722
                if ($usermustbemodified) {
723
                    $result = $tmpobj->update($user, 0, 1, 1, 1);
724
                    if ($result < 0) {
725
                        $error++;
726
                    }
727
                }
728
            }
729
730
            if (!$error && !$notrigger) {
731
                // Call trigger
732
                $result = $this->call_trigger('CONTACT_MODIFY', $user);
733
                if ($result < 0) {
734
                    $error++;
735
                }
736
                // End call triggers
737
            }
738
739
            if (!$error) {
740
                $this->db->commit();
741
                return 1;
742
            } else {
743
                dol_syslog(get_class($this) . "::update Error " . $this->error, LOG_ERR);
744
                $this->db->rollback();
745
                return -$error;
746
            }
747
        } else {
748
            $this->error = $this->db->lasterror() . ' sql=' . $sql;
749
            $this->db->rollback();
750
            return -1;
751
        }
752
    }
753
754
755
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
756
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
757
    /**
758
     *  Return DN string complete in the LDAP directory for the object
759
     *
760
     *  @param      array   $info       Info string loaded by _load_ldap_info
761
     *  @param      int     $mode       0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
762
     *                                  1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
763
     *                                  2=Return key only (uid=qqq)
764
     *  @return     string              DN
765
     */
766
    public function _load_ldap_dn($info, $mode = 0)
767
    {
768
		// phpcs:enable
769
        global $conf;
770
        $dn = '';
771
        if ($mode == 0) {
772
            $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=" . $info[getDolGlobalString('LDAP_KEY_CONTACTS')] . "," . getDolGlobalString('LDAP_CONTACT_DN');
773
        } elseif ($mode == 1) {
774
            $dn = getDolGlobalString('LDAP_CONTACT_DN');
775
        } elseif ($mode == 2) {
776
            $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=" . $info[getDolGlobalString('LDAP_KEY_CONTACTS')];
777
        }
778
        return $dn;
779
    }
780
781
782
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
783
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
784
    /**
785
     *  Initialize info table (LDAP attributes table)
786
     *
787
     *  @return     array       Attributes info table
788
     */
789
    public function _load_ldap_info()
790
    {
791
		// phpcs:enable
792
        global $conf, $langs;
793
794
        $info = array();
795
796
        // Object classes
797
        $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS'));
798
799
        $this->fullname = $this->getFullName($langs);
800
801
        // Fields
802
        if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) {
803
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname;
804
        }
805
        if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) {
806
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname;
807
        }
808
        if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) {
809
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname;
810
        }
811
812
        if ($this->poste) {
813
            $info["title"] = $this->poste;
814
        }
815
        if ($this->socid > 0) {
816
            $soc = new Societe($this->db);
817
            $soc->fetch($this->socid);
818
819
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name;
820
            if ($soc->client == 1) {
821
                $info["businessCategory"] = "Customers";
822
            }
823
            if ($soc->client == 2) {
824
                $info["businessCategory"] = "Prospects";
825
            }
826
            if ($soc->fournisseur == 1) {
827
                $info["businessCategory"] = "Suppliers";
828
            }
829
        }
830
        if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) {
831
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address;
832
        }
833
        if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) {
834
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip;
835
        }
836
        if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) {
837
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town;
838
        }
839
        if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) {
840
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code;
841
        }
842
        if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) {
843
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro;
844
        }
845
        if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) {
846
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso;
847
        }
848
        if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) {
849
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile;
850
        }
851
        if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) {
852
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax;
853
        }
854
        if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) {
855
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2);
856
        }
857
        if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) {
858
            $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email;
859
        }
860
861
        if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') {
862
            $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware
863
864
            $info['uidnumber'] = $this->id;
865
866
            $info['phpgwTz'] = 0;
867
            $info['phpgwMailType'] = 'INTERNET';
868
            $info['phpgwMailHomeType'] = 'INTERNET';
869
870
            $info["phpgwContactTypeId"] = 'n';
871
            $info["phpgwContactCatId"] = 0;
872
            $info["phpgwContactAccess"] = "public";
873
874
            /*
875
            if (dol_strlen($this->egroupware_id) == 0) {
876
                $this->egroupware_id = 1;
877
            }
878
            $info["phpgwContactOwner"] = $this->egroupware_id;
879
            */
880
            $info["phpgwContactOwner"] = 1;
881
882
            if ($this->email) {
883
                $info["rfc822Mailbox"] = $this->email;
884
            }
885
            if ($this->phone_mobile) {
886
                $info["phpgwCellTelephoneNumber"] = $this->phone_mobile;
887
            }
888
        }
889
890
        return $info;
891
    }
892
893
894
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
895
    /**
896
     *  Update field alert birthday
897
     *
898
     *  @param      int         $id         Id of contact
899
     *  @param      User        $user       User asking to change alert or birthday
900
     *  @param      int         $notrigger  0=no, 1=yes
901
     *  @return     int                     Return integer <0 if KO, >=0 if OK
902
     */
903
    public function update_perso($id, $user = null, $notrigger = 0)
904
    {
905
		// phpcs:enable
906
        $error = 0;
907
        $result = false;
908
909
        $this->db->begin();
910
911
        // Update the contact
912
        $sql = "UPDATE " . MAIN_DB_PREFIX . "socpeople SET";
913
        $sql .= " birthday = " . ($this->birthday ? "'" . $this->db->idate($this->birthday) . "'" : "null");
914
        $sql .= ", photo = " . ($this->photo ? "'" . $this->db->escape($this->photo) . "'" : "null");
915
        if ($user) {
916
            $sql .= ", fk_user_modif = " . ((int) $user->id);
917
        }
918
        $sql .= " WHERE rowid = " . ((int) $id);
919
920
        dol_syslog(get_class($this) . "::update_perso this->birthday=" . $this->birthday . " -", LOG_DEBUG);
921
        $resql = $this->db->query($sql);
922
        if (!$resql) {
923
            $error++;
924
            $this->error = $this->db->lasterror();
925
        }
926
927
        if ($user) {
928
            // Update birthday alert
929
            if (!empty($this->birthday_alert)) {
930
                //check existing
931
                $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id);
932
                $result_check = $this->db->query($sql_check);
933
                if (!$result_check || ($this->db->num_rows($result_check) < 1)) {
934
                    //insert
935
                    $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) ";
936
                    $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")";
937
                    $result = $this->db->query($sql);
938
                    if (!$result) {
939
                        $error++;
940
                        $this->error = $this->db->lasterror();
941
                    }
942
                } else {
943
                    $result = true;
944
                }
945
            } else {
946
                $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert ";
947
                $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id);
948
                $result = $this->db->query($sql);
949
                if (!$result) {
950
                    $error++;
951
                    $this->error = $this->db->lasterror();
952
                }
953
            }
954
        }
955
956
        if (!$error && !$notrigger) {
957
            // Call trigger
958
            $result = $this->call_trigger('CONTACT_MODIFY', $user);
959
            if ($result < 0) {
960
                $error++;
961
            }
962
            // End call triggers
963
        }
964
965
        if (!$error) {
966
            $this->db->commit();
967
            return 1;
968
        } else {
969
            dol_syslog(get_class($this) . "::update Error " . $this->error, LOG_ERR);
970
            $this->db->rollback();
971
            return -$error;
972
        }
973
    }
974
975
976
    /**
977
     *  Load object contact.
978
     *
979
     *  @param      int     $id             Id of contact
980
     *  @param      ?User   $user           Load also alerts of this user (subscribing to alerts) that want alerts about this contact
981
     *  @param      string  $ref_ext        External reference, not given by Dolibarr
982
     *  @param      string  $email          Email
983
     *  @param      int     $loadalsoroles  Load also roles. Try to always 0 here and load roles with a separate call of fetchRoles().
984
     *  @return     int                     >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.
985
     */
986
    public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
987
    {
988
        global $langs;
989
990
        dol_syslog(get_class($this) . "::fetch id=" . $id . " ref_ext=" . $ref_ext . " email=" . $email, LOG_DEBUG);
991
992
        if (empty($id) && empty($ref_ext) && empty($email)) {
993
            $this->error = 'BadParameter';
994
            return -1;
995
        }
996
997
        $langs->loadLangs(array("dict", "companies"));
998
999
        $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,";
1000
        $sql .= " c.address, c.statut, c.zip, c.town,";
1001
        $sql .= " c.fk_pays as country_id,";
1002
        $sql .= " c.fk_departement as state_id,";
1003
        $sql .= " c.birthday,";
1004
        $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
1005
        $sql .= " c.socialnetworks,";
1006
        $sql .= " c.photo,";
1007
        $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";
1008
        $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";
1009
        $sql .= " c.import_key,";
1010
        $sql .= " c.datec as date_creation, c.tms as date_modification,";
1011
        $sql .= " co.label as country, co.code as country_code,";
1012
        $sql .= " d.nom as state, d.code_departement as state_code,";
1013
        $sql .= " u.rowid as user_id, u.login as user_login,";
1014
        $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
1015
        $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as c";
1016
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_country as co ON c.fk_pays = co.rowid";
1017
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_departements as d ON c.fk_departement = d.rowid";
1018
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u ON c.rowid = u.fk_socpeople";
1019
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON c.fk_soc = s.rowid";
1020
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_stcommcontact as st ON c.fk_stcommcontact = st.id';
1021
        if ($id) {
1022
            $sql .= " WHERE c.rowid = " . ((int) $id);
1023
        } else {
1024
            $sql .= " WHERE c.entity IN (" . getEntity($this->element) . ")";
1025
            if ($ref_ext) {
1026
                $sql .= " AND c.ref_ext = '" . $this->db->escape($ref_ext) . "'";
1027
            }
1028
            if ($email) {
1029
                $sql .= " AND c.email = '" . $this->db->escape($email) . "'";
1030
            }
1031
        }
1032
1033
        $resql = $this->db->query($sql);
1034
        if ($resql) {
1035
            $num = $this->db->num_rows($resql);
1036
            if ($num > 1) {
1037
                $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.';
1038
                dol_syslog($this->error, LOG_ERR);
1039
1040
                return 2;
1041
            } elseif ($num) {   // $num = 1
1042
                $obj = $this->db->fetch_object($resql);
1043
1044
                $this->id       = $obj->rowid;
1045
                $this->entity   = $obj->entity;
1046
                $this->ref      = $obj->rowid;
1047
                $this->ref_ext  = $obj->ref_ext;
1048
1049
                $this->civility_code    = $obj->civility_code;
1050
                $this->civility         = $obj->civility_code ? ($langs->trans("Civility" . $obj->civility_code) != "Civility" . $obj->civility_code ? $langs->trans("Civility" . $obj->civility_code) : $obj->civility_code) : '';
1051
1052
                $this->name_alias   = $obj->name_alias;
1053
                $this->lastname     = $obj->lastname;
1054
                $this->firstname    = $obj->firstname;
1055
                $this->address      = $obj->address;
1056
                $this->zip          = $obj->zip;
1057
                $this->town         = $obj->town;
1058
1059
                $this->date_creation     = $this->db->jdate($obj->date_creation);
1060
                $this->date_modification = $this->db->jdate($obj->date_modification);
1061
1062
                $this->state_id     = $obj->state_id;
1063
                $this->state_code   = $obj->state_code;
1064
                $this->state        = $obj->state;
1065
1066
                $this->country_id   = $obj->country_id;
1067
                $this->country_code = $obj->country_id ? $obj->country_code : '';
1068
                $this->country      = $obj->country_id ? ($langs->trans('Country' . $obj->country_code) != 'Country' . $obj->country_code ? $langs->transnoentities('Country' . $obj->country_code) : $obj->country) : '';
1069
1070
                $this->fk_soc       = $obj->fk_soc;     // Both fk_soc and socid are used
1071
                $this->socid        = $obj->fk_soc;     // Both fk_soc and socid are used
1072
                $this->socname      = $obj->socname;
1073
                $this->poste        = $obj->poste;
1074
                $this->statut       = $obj->statut;
1075
1076
                $this->fk_prospectlevel = $obj->fk_prospectlevel;
1077
1078
                $transcode = $langs->trans('StatusProspect' . $obj->fk_stcommcontact);
1079
                $libelle = ($transcode != 'StatusProspect' . $obj->fk_stcommcontact ? $transcode : $obj->stcomm);
1080
                $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial
1081
                $this->statut_commercial = $libelle; // libelle statut commercial
1082
                $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial
1083
1084
                $this->phone_pro    = trim($obj->phone);
1085
                $this->fax          = trim($obj->fax);
1086
                $this->phone_perso  = trim($obj->phone_perso);
1087
                $this->phone_mobile = trim($obj->phone_mobile);
1088
1089
                $this->email            = $obj->email;
1090
                $this->socialnetworks   = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
1091
                $this->photo            = $obj->photo;
1092
                $this->priv             = $obj->priv;
1093
                $this->mail             = $obj->email;
1094
1095
                $this->birthday     = $this->db->jdate($obj->birthday);
1096
                $this->note         = $obj->note_private; // deprecated
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$note has been deprecated: Use $note_private instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1096
                /** @scrutinizer ignore-deprecated */ $this->note         = $obj->note_private; // deprecated

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
1097
                $this->note_private = $obj->note_private;
1098
                $this->note_public  = $obj->note_public;
1099
                $this->default_lang = $obj->default_lang;
1100
                $this->user_id      = $obj->user_id;
1101
                $this->user_login   = $obj->user_login;
1102
                $this->canvas       = $obj->canvas;
1103
1104
                $this->import_key = $obj->import_key;
1105
1106
                // Define gender according to civility
1107
                $this->setGenderFromCivility();
1108
1109
                // Search Dolibarr user linked to this contact
1110
                $sql = "SELECT u.rowid ";
1111
                $sql .= " FROM " . MAIN_DB_PREFIX . "user as u";
1112
                $sql .= " WHERE u.fk_socpeople = " . ((int) $this->id);
1113
1114
                $resql = $this->db->query($sql);
1115
                if ($resql) {
1116
                    if ($this->db->num_rows($resql)) {
1117
                        $uobj = $this->db->fetch_object($resql);
1118
1119
                        $this->user_id = $uobj->rowid;
1120
                    }
1121
                    $this->db->free($resql);
1122
                } else {
1123
                    $this->error = $this->db->error();
1124
                    return -1;
1125
                }
1126
1127
                // Retrieve all extrafield
1128
                // fetch optionals attributes and labels
1129
                $this->fetch_optionals();
1130
1131
                // Load also alerts of this user
1132
                if ($user) {
1133
                    $sql = "SELECT fk_user";
1134
                    $sql .= " FROM " . MAIN_DB_PREFIX . "user_alert";
1135
                    $sql .= " WHERE fk_user = " . ((int) $user->id) . " AND fk_contact = " . ((int) $id);
1136
1137
                    $resql = $this->db->query($sql);
1138
                    if ($resql) {
1139
                        if ($this->db->num_rows($resql)) {
1140
                            $obj = $this->db->fetch_object($resql);
1141
1142
                            $this->birthday_alert = 1;
1143
                        }
1144
                        $this->db->free($resql);
1145
                    } else {
1146
                        $this->error = $this->db->error();
1147
                        return -1;
1148
                    }
1149
                }
1150
1151
                // Load also roles of this address
1152
                if ($loadalsoroles) {
1153
                    $resultRole = $this->fetchRoles();
1154
                    if ($resultRole < 0) {
1155
                        return $resultRole;
1156
                    }
1157
                }
1158
1159
                return 1;
1160
            } else {
1161
                $this->error = $langs->trans("RecordNotFound");
1162
                return 0;
1163
            }
1164
        } else {
1165
            $this->error = $this->db->error();
1166
            return -1;
1167
        }
1168
    }
1169
1170
1171
1172
    /**
1173
     * Set the property "gender" of this class, based on the property "civility_id"
1174
     * or use property "civility_code" as fallback, when "civility_id" is not available.
1175
     *
1176
     * @return void
1177
     */
1178
    public function setGenderFromCivility()
1179
    {
1180
        unset($this->gender);
1181
1182
        if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) {
1183
            $this->gender = 'man';
1184
        } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) {
1185
            $this->gender = 'woman';
1186
        }
1187
    }
1188
1189
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1190
    /**
1191
     *  Load number of elements the contact is used as a link for
1192
     *  ref_facturation
1193
     *  ref_contrat
1194
     *  ref_commande (for order and/or shipments)
1195
     *  ref_propale
1196
     *
1197
     *  @return     int                                 Return integer <0 if KO, >=0 if OK
1198
     */
1199
    public function load_ref_elements()
1200
    {
1201
		// phpcs:enable
1202
        // Count the elements for which it is contact
1203
        $sql = "SELECT tc.element, count(ec.rowid) as nb";
1204
        $sql .= " FROM " . MAIN_DB_PREFIX . "element_contact as ec, " . MAIN_DB_PREFIX . "c_type_contact as tc";
1205
        $sql .= " WHERE ec.fk_c_type_contact = tc.rowid";
1206
        $sql .= " AND fk_socpeople = " . ((int) $this->id);
1207
        $sql .= " AND tc.source = 'external'";
1208
        $sql .= " GROUP BY tc.element";
1209
1210
        dol_syslog(get_class($this) . "::load_ref_elements", LOG_DEBUG);
1211
1212
        $resql = $this->db->query($sql);
1213
        if ($resql) {
1214
            while ($obj = $this->db->fetch_object($resql)) {
1215
                if ($obj->nb) {
1216
                    if ($obj->element == 'facture') {
1217
                        $this->ref_facturation = $obj->nb;
1218
                    } elseif ($obj->element == 'contrat') {
1219
                        $this->ref_contrat = $obj->nb;
1220
                    } elseif ($obj->element == 'commande') {
1221
                        $this->ref_commande = $obj->nb;
1222
                    } elseif ($obj->element == 'propal') {
1223
                        $this->ref_propal = $obj->nb;
1224
                    }
1225
                }
1226
            }
1227
            $this->db->free($resql);
1228
            return 0;
1229
        } else {
1230
            $this->error = $this->db->lasterror();
1231
            return -1;
1232
        }
1233
    }
1234
1235
    /**
1236
     *  Delete a contact from database
1237
     *
1238
     *  @param      User    $user           User making the delete
1239
     *  @param      int     $notrigger      Disable all trigger
1240
     *  @return     int                     Return integer <0 if KO, >0 if OK
1241
     */
1242
    public function delete($user, $notrigger = 0)
1243
    {
1244
        $error = 0;
1245
1246
        $this->db->begin();
1247
1248
        if (!$error && !$notrigger) {
1249
            // Call trigger
1250
            $result = $this->call_trigger('CONTACT_DELETE', $user);
1251
            if ($result < 0) {
1252
                $error++;
1253
            }
1254
            // End call triggers
1255
        }
1256
1257
        if (!$error) {
1258
            // Get all rowid of element_contact linked to a type that is link to llx_socpeople
1259
            $sql = "SELECT ec.rowid";
1260
            $sql .= " FROM " . MAIN_DB_PREFIX . "element_contact ec,";
1261
            $sql .= " " . MAIN_DB_PREFIX . "c_type_contact tc";
1262
            $sql .= " WHERE ec.fk_socpeople=" . ((int) $this->id);
1263
            $sql .= " AND ec.fk_c_type_contact=tc.rowid";
1264
            $sql .= " AND tc.source='external'";
1265
            dol_syslog(__METHOD__, LOG_DEBUG);
1266
            $resql = $this->db->query($sql);
1267
            if ($resql) {
1268
                $num = $this->db->num_rows($resql);
1269
1270
                $i = 0;
1271
                while ($i < $num && !$error) {
1272
                    $obj = $this->db->fetch_object($resql);
1273
1274
                    $sqldel = "DELETE FROM " . MAIN_DB_PREFIX . "element_contact";
1275
                    $sqldel .= " WHERE rowid = " . ((int) $obj->rowid);
1276
                    dol_syslog(__METHOD__, LOG_DEBUG);
1277
                    $result = $this->db->query($sqldel);
1278
                    if (!$result) {
1279
                        $error++;
1280
                        $this->error = $this->db->error() . ' sql=' . $sqldel;
1281
                    }
1282
1283
                    $i++;
1284
                }
1285
            } else {
1286
                $error++;
1287
                $this->error = $this->db->error() . ' sql=' . $sql;
1288
            }
1289
        }
1290
1291
        if (!$error) {
1292
            // Remove Roles
1293
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "societe_contacts WHERE fk_socpeople = " . ((int) $this->id);
1294
            dol_syslog(__METHOD__, LOG_DEBUG);
1295
            $resql = $this->db->query($sql);
1296
            if (!$resql) {
1297
                $error++;
1298
                $this->error .= $this->db->lasterror();
1299
                $errorflag = -1;
1300
            }
1301
        }
1302
1303
        if (!$error) {
1304
            // Remove Notifications
1305
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "notify_def WHERE fk_contact = " . ((int) $this->id);
1306
            dol_syslog(__METHOD__, LOG_DEBUG);
1307
            $resql = $this->db->query($sql);
1308
            if (!$resql) {
1309
                $error++;
1310
                $this->error .= $this->db->lasterror();
1311
                $errorflag = -1;
1312
            }
1313
        }
1314
1315
        if (!$error) {
1316
            // Remove category
1317
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "categorie_contact WHERE fk_socpeople = " . ((int) $this->id);
1318
            dol_syslog(__METHOD__, LOG_DEBUG);
1319
            $resql = $this->db->query($sql);
1320
            if (!$resql) {
1321
                $error++;
1322
                $this->error .= $this->db->lasterror();
1323
                $errorflag = -1;
1324
            }
1325
        }
1326
1327
        if (!$error) {
1328
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "socpeople";
1329
            $sql .= " WHERE rowid = " . ((int) $this->id);
1330
            dol_syslog(__METHOD__, LOG_DEBUG);
1331
            $result = $this->db->query($sql);
1332
            if (!$result) {
1333
                $error++;
1334
                $this->error = $this->db->error() . ' sql=' . $sql;
1335
            }
1336
        }
1337
1338
        // Remove extrafields
1339
        if (!$error) {
1340
            // For avoid conflicts if trigger used
1341
            $result = $this->deleteExtraFields();
1342
            if ($result < 0) {
1343
                $error++;
1344
            }
1345
        }
1346
1347
        if (!$error) {
1348
            $this->db->commit();
1349
            return 1;
1350
        } else {
1351
            $this->db->rollback();
1352
            dol_syslog("Error " . $this->error, LOG_ERR);
1353
            return -1;
1354
        }
1355
    }
1356
1357
1358
    /**
1359
     *  Load contact information from the database
1360
     *
1361
     *  @param      int     $id      Id of the contact to load
1362
     *  @return     void
1363
     */
1364
    public function info($id)
1365
    {
1366
        $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,";
1367
        $sql .= " c.tms as tms, c.fk_user_modif";
1368
        $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as c";
1369
        $sql .= " WHERE c.rowid = " . ((int) $id);
1370
1371
        $resql = $this->db->query($sql);
1372
        if ($resql) {
1373
            if ($this->db->num_rows($resql)) {
1374
                $obj = $this->db->fetch_object($resql);
1375
1376
                $this->id = $obj->rowid;
1377
1378
                $this->user_creation_id = $obj->fk_user_creat;
1379
                $this->user_modification_id = $obj->fk_user_modif;
1380
                $this->date_creation     = $this->db->jdate($obj->datec);
1381
                $this->date_modification = $this->db->jdate($obj->tms);
1382
            }
1383
1384
            $this->db->free($resql);
1385
        } else {
1386
            print $this->db->error();
1387
        }
1388
    }
1389
1390
    /**
1391
     *  Return number of mass Emailing received by these contacts with its email
1392
     *
1393
     *  @return       int     Number of EMailings
1394
     */
1395
    public function getNbOfEMailings()
1396
    {
1397
        $sql = "SELECT count(mc.email) as nb";
1398
        $sql .= " FROM " . MAIN_DB_PREFIX . "mailing_cibles as mc, " . MAIN_DB_PREFIX . "mailing as m";
1399
        $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '" . $this->db->escape($this->email) . "' ";
1400
        $sql .= " AND m.entity IN (" . getEntity($this->element) . ") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
1401
1402
        $resql = $this->db->query($sql);
1403
        if ($resql) {
1404
            $obj = $this->db->fetch_object($resql);
1405
            $nb = $obj->nb;
1406
1407
            $this->db->free($resql);
1408
            return $nb;
1409
        } else {
1410
            $this->error = $this->db->error();
1411
            return -1;
1412
        }
1413
    }
1414
1415
    /**
1416
     * getTooltipContentArray
1417
     * @param array $params params to construct tooltip data
1418
     * @since v18
1419
     * @return array
1420
     */
1421
    public function getTooltipContentArray($params)
1422
    {
1423
        global $conf, $langs, $user;
1424
1425
        $datas = [];
1426
1427
        if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1428
            return ['optimize' => $langs->trans("ShowContact")];
1429
        }
1430
        if (!empty($this->photo) && class_exists('Form')) {
1431
            $photo = '<div class="photointooltip floatright">';
1432
            $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.
1433
            $photo .= '</div>';
1434
            $datas['photo'] = $photo;
1435
        }
1436
1437
        $datas['picto'] = img_picto('', $this->picto) . ' <u class="paddingrightonly">' . $langs->trans("Contact") . '</u> ' . $this->getLibStatut(4);
1438
        $datas['name'] = '<br><b>' . $langs->trans("Name") . ':</b> ' . $this->getFullName($langs);
1439
        // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id;        // TODO Translate civilty_id code
1440
        if (!empty($this->poste)) {
1441
            $datas['job'] = '<br><b>' . $langs->trans("Poste") . ':</b> ' . $this->poste;
1442
        }
1443
        $datas['email'] = '<br><b>' . $langs->trans("EMail") . ':</b> ' . $this->email;
1444
        $phonelist = array();
1445
        $country_code = empty($this->country_code) ? '' : $this->country_code;
1446
        if ($this->phone_pro) {
1447
            $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1448
        }
1449
        if ($this->phone_mobile) {
1450
            $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');
1451
        }
1452
        if ($this->phone_perso) {
1453
            $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');
1454
        }
1455
        $datas['phonelist'] = '<br><b>' . $langs->trans("Phone") . ':</b> ' . implode('&nbsp;', $phonelist);
1456
        $datas['address'] = '<br><b>' . $langs->trans("Address") . ':</b> ' . dol_format_address($this, 1, ' ', $langs);
1457
1458
        return $datas;
1459
    }
1460
1461
    /**
1462
     *  Return name of contact with link (and eventually picto)
1463
     *  Use $this->id, $this->lastname, $this->firstname, this->civility_id
1464
     *
1465
     *  @param      int         $withpicto                  Include picto with link (0=no picto, 1=picto + name, 2=picto only, -1=photo+name, -2=photo only)
1466
     *  @param      string      $option                     Where the link point to
1467
     *  @param      int         $maxlen                     Max length of
1468
     *  @param      string      $moreparam                  Add more param into URL
1469
     *  @param      int         $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
1470
     *  @param      int         $notooltip                  1=Disable tooltip
1471
     *  @param      string      $morecss                    Add more css on link
1472
     *  @return     string                                  String with URL
1473
     */
1474
    public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1475
    {
1476
        global $conf, $langs, $hookmanager;
1477
1478
        if (!empty($conf->dol_no_mouse_hover)) {
1479
            $notooltip = 1; // Force disable tooltips
1480
        }
1481
1482
        $result = '';
1483
        $params = [
1484
            'id' => $this->id,
1485
            'objecttype' => $this->element,
1486
            'option' => $option,
1487
        ];
1488
        $classfortooltip = 'classfortooltip';
1489
        $dataparams = '';
1490
        if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1491
            $classfortooltip = 'classforajaxtooltip';
1492
            $dataparams = ' data-params="' . dol_escape_htmltag(json_encode($params)) . '"';
1493
            $label = '';
1494
        } else {
1495
            $label = implode($this->getTooltipContentArray($params));
1496
        }
1497
1498
        $url = constant('BASE_URL') . '/contact/card.php?id=' . $this->id;
1499
1500
        if ($option !== 'nolink') {
1501
            // Add param to save lastsearch_values or not
1502
            $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1503
            if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1504
                $add_save_lastsearch_values = 1;
1505
            }
1506
            if ($url && $add_save_lastsearch_values) {
1507
                $url .= '&save_lastsearch_values=1';
1508
            }
1509
        }
1510
1511
        $url .= $moreparam;
1512
1513
        $linkclose = '';
1514
        if (empty($notooltip)) {
1515
            if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1516
                $label = $langs->trans("ShowContact");
1517
                $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
1518
            }
1519
            $linkclose .= ($label ? ' title="' . dol_escape_htmltag($label, 1) . '"' : ' title="tocomplete"');
1520
            $linkclose .= $dataparams . ' class="' . $classfortooltip . ($morecss ? ' ' . $morecss : '') . '"';
1521
        } else {
1522
            $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
1523
        }
1524
1525
        if ($option == 'nolink' || empty($url)) {
1526
            $linkstart = '<span';
1527
        } else {
1528
            $linkstart = '<a href="' . $url . '"';
1529
        }
1530
        $linkstart .= $linkclose . '>';
1531
        if ($option == 'nolink' || empty($url)) {
1532
            $linkend = '</span>';
1533
        } else {
1534
            $linkend = '</a>';
1535
        }
1536
1537
        $result .= $linkstart;
1538
1539
        if ($withpicto) {
1540
            if ($withpicto < 0) {
1541
                $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>';
1542
                if ($withpicto != 2 && $withpicto != -2) {
1543
                    $result .= ' ';
1544
                }
1545
            } else {
1546
                $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);
1547
            }
1548
        }
1549
        if ($withpicto != 2 && $withpicto != -2) {
1550
            $result .= '<span class="valigmiddle">' . ($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)) . '</span>';
1551
        }
1552
1553
        $result .= $linkend;
1554
1555
        global $action;
1556
        $hookmanager->initHooks(array('contactdao'));
1557
        $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1558
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1559
        if ($reshook > 0) {
1560
            $result = $hookmanager->resPrint;
1561
        } else {
1562
            $result .= $hookmanager->resPrint;
1563
        }
1564
1565
        return $result;
1566
    }
1567
1568
    /**
1569
     *    Return civility label of contact
1570
     *
1571
     *    @return   string                  Translated name of civility
1572
     */
1573
    public function getCivilityLabel()
1574
    {
1575
        global $langs;
1576
1577
        $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : '')));
1578
        if (empty($code)) {
1579
            return '';
1580
        }
1581
1582
        $langs->load("dict");
1583
        return $langs->getLabelFromKey($this->db, "Civility" . $code, "c_civility", "code", "label", $code);
1584
    }
1585
1586
    /**
1587
     *  Return the label of the status
1588
     *
1589
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
1590
     *  @return string                 Label of status
1591
     */
1592
    public function getLibStatut($mode)
1593
    {
1594
        return $this->LibStatut($this->statut, $mode);
1595
    }
1596
1597
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1598
    /**
1599
     *  Return the label of a given status
1600
     *
1601
     *  @param  int     $status        Id status
1602
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
1603
     *  @return string                 Label of status
1604
     */
1605
    public function LibStatut($status, $mode)
1606
    {
1607
		// phpcs:enable
1608
        global $langs;
1609
1610
        $labelStatus = array(
1611
            0 => 'ActivityCeased',
1612
            1 => 'InActivity',
1613
            4 => 'InActivity',
1614
            5 => 'ActivityCeased',
1615
        );
1616
        $labelStatusShort = array(
1617
            0 => 'ActivityCeased',
1618
            1 => 'InActivity',
1619
            4 => 'InActivity',
1620
            5 => 'ActivityCeased',
1621
        );
1622
1623
        $statusType = 'status4';
1624
        if ($status == 0 || $status == 5) {
1625
            $statusType = 'status5';
1626
        }
1627
1628
        $label = $langs->transnoentitiesnoconv($labelStatus[$status]);
1629
        $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]);
1630
1631
        return dolGetStatus($label, $labelshort, '', $statusType, $mode);
1632
    }
1633
1634
1635
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1636
    /**
1637
     *  Return translated label of Public or Private
1638
     *
1639
     *  @param      int         $status     Type (0 = public, 1 = private)
1640
     *  @return     string                  Label translated
1641
     */
1642
    public function LibPubPriv($status)
1643
    {
1644
		// phpcs:enable
1645
        global $langs;
1646
        if ($status == '1') {
1647
            return $langs->trans('ContactPrivate');
1648
        } else {
1649
            return $langs->trans('ContactPublic');
1650
        }
1651
    }
1652
1653
1654
    /**
1655
     *  Initialise an instance with random values.
1656
     *  Used to build previews or test instances.
1657
     *  id must be 0 if object instance is a specimen.
1658
     *
1659
     *  @return int >0 if ok
1660
     */
1661
    public function initAsSpecimen()
1662
    {
1663
        // Get first id of existing company and save it into $socid
1664
        $socid = 0;
1665
        $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "societe ORDER BY rowid LIMIT 1";
1666
        $resql = $this->db->query($sql);
1667
        if ($resql) {
1668
            $obj = $this->db->fetch_object($resql);
1669
            if ($obj) {
1670
                $socid = $obj->rowid;
1671
            }
1672
        }
1673
1674
        // Initialise parameters
1675
        $this->id = 0;
1676
        $this->entity = 1;
1677
        $this->specimen = 1;
1678
        $this->lastname = 'DOLIBARR';
1679
        $this->firstname = 'SPECIMEN';
1680
        $this->address = '21 jump street';
1681
        $this->zip = '99999';
1682
        $this->town = 'MyTown';
1683
        $this->country_id = 1;
1684
        $this->country_code = 'FR';
1685
        $this->country = 'France';
1686
        $this->email = '[email protected]';
1687
        $this->socialnetworks = array(
1688
            'skype' => 'tom.hanson',
1689
            'twitter' => 'tomhanson',
1690
            'linkedin' => 'tomhanson',
1691
        );
1692
        $this->phone_pro = '0909090901';
1693
        $this->phone_perso = '0909090902';
1694
        $this->phone_mobile = '0909090903';
1695
        $this->fax = '0909090909';
1696
1697
        $this->note_public = 'This is a comment (public)';
1698
        $this->note_private = 'This is a comment (private)';
1699
1700
        $this->socid = $socid;
1701
        $this->statut = 1;
1702
1703
        return 1;
1704
    }
1705
1706
    /**
1707
     *  Change status of a user
1708
     *
1709
     *  @param  int     $status     Status to set
1710
     *  @return int                 Return integer <0 if KO, 0 if nothing is done, >0 if OK
1711
     */
1712
    public function setstatus($status)
1713
    {
1714
        global $conf, $langs, $user;
1715
1716
        $error = 0;
1717
1718
        // Check parameters
1719
        if ($this->statut == $status) {
1720
            return 0;
1721
        } else {
1722
            $this->statut = $status;
1723
        }
1724
1725
        $this->db->begin();
1726
1727
        // User disable
1728
        $sql = "UPDATE " . MAIN_DB_PREFIX . "socpeople";
1729
        $sql .= " SET statut = " . ((int) $this->statut);
1730
        $sql .= ", fk_user_modif = " . ((int) $user->id);
1731
        $sql .= " WHERE rowid = " . ((int) $this->id);
1732
        $result = $this->db->query($sql);
1733
1734
        dol_syslog(get_class($this) . "::setstatus", LOG_DEBUG);
1735
        if ($result) {
1736
            // Call trigger
1737
            $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);
1738
            if ($result < 0) {
1739
                $error++;
1740
            }
1741
            // End call triggers
1742
        }
1743
1744
        if ($error) {
1745
            $this->db->rollback();
1746
            return -$error;
1747
        } else {
1748
            $this->db->commit();
1749
            return 1;
1750
        }
1751
    }
1752
1753
    /**
1754
     * Sets object to supplied categories.
1755
     *
1756
     * Deletes object from existing categories not supplied.
1757
     * Adds it to non existing supplied categories.
1758
     * Existing categories are left untouch.
1759
     *
1760
     * @param   int[]|int   $categories     Category or categories IDs
1761
     * @return  int                         Return integer <0 if KO, >0 if OK
1762
     */
1763
    public function setCategories($categories)
1764
    {
1765
        return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT);
1766
    }
1767
1768
    /**
1769
     * Function used to replace a thirdparty id with another one.
1770
     *
1771
     * @param   DoliDB  $dbs        Database handler, because function is static we name it $dbs not $db to avoid breaking coding test
1772
     * @param   int     $origin_id  Old thirdparty id
1773
     * @param   int     $dest_id    New thirdparty id
1774
     * @return  bool
1775
     */
1776
    public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1777
    {
1778
        $tables = array(
1779
            'socpeople', 'societe_contacts'
1780
        );
1781
1782
        return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1783
    }
1784
1785
    /**
1786
     * Fetch roles (default contact of some companies) for the current contact.
1787
     * This load the array ->roles.
1788
     *
1789
     * @return  int         Return integer <0 if KO, Nb of roles found if OK
1790
     * @see updateRoles()
1791
     */
1792
    public function fetchRoles()
1793
    {
1794
        global $langs;
1795
        $error = 0;
1796
        $num = 0;
1797
1798
        $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";
1799
        $sql .= " FROM " . MAIN_DB_PREFIX . "societe_contacts as sc, " . MAIN_DB_PREFIX . "c_type_contact as tc";
1800
        $sql .= " WHERE tc.rowid = sc.fk_c_type_contact";
1801
        $sql .= " AND tc.source = 'external' AND tc.active=1";
1802
        $sql .= " AND sc.fk_socpeople = " . ((int) $this->id);
1803
        $sql .= " AND sc.entity IN (" . getEntity('societe') . ')';
1804
1805
        $resql = $this->db->query($sql);
1806
        if ($resql) {
1807
            $this->roles = array();
1808
1809
            $num = $this->db->num_rows($resql);
1810
            if ($num > 0) {
1811
                while ($obj = $this->db->fetch_object($resql)) {
1812
                    $transkey = "TypeContact_" . $obj->element . "_" . $obj->source . "_" . $obj->code;
1813
                    $libelle_element = $langs->trans('ContactDefault_' . $obj->element);
1814
                    $this->roles[$obj->contactroleid] = array('id' => $obj->rowid, 'socid' => $obj->socid, 'element' => $obj->element, 'source' => $obj->source, 'code' => $obj->code, 'label' => $libelle_element . ' - ' . ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->label));
1815
                }
1816
            }
1817
        } else {
1818
            $error++;
1819
            $this->error = $this->db->lasterror();
1820
            $this->errors[] = $this->db->lasterror();
1821
        }
1822
1823
        if (empty($error)) {
1824
            return $num;
1825
        } else {
1826
            return $error * -1;
1827
        }
1828
    }
1829
1830
    /**
1831
     * Get thirdparty contact roles of a given contact
1832
     *
1833
     * @param  string   $element    Element type
1834
     * @return array|int            Array of contact roles or -1
1835
     * @throws Exception
1836
     */
1837
    public function getContactRoles($element = '')
1838
    {
1839
        $tab = array();
1840
1841
        if ($element == 'action') {
1842
            $element = 'agenda';
1843
        }
1844
1845
        $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact";
1846
        $sql .= " FROM " . MAIN_DB_PREFIX . "c_type_contact tc";
1847
        $sql .= ", " . MAIN_DB_PREFIX . "societe_contacts sc";
1848
        $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "socpeople sp";
1849
        $sql .= " ON sc.fk_socpeople = sp.rowid AND sp.statut = 1";
1850
        $sql .= " WHERE sc.fk_soc =" . ((int) $this->socid);
1851
        $sql .= " AND sc.fk_c_type_contact=tc.rowid";
1852
        $sql .= " AND tc.element = '" . $this->db->escape($element) . "'";
1853
        $sql .= " AND sp.entity IN (" . getEntity('contact') . ")";
1854
        $sql .= " AND tc.active = 1";
1855
1856
        dol_syslog(__METHOD__, LOG_DEBUG);
1857
        $resql = $this->db->query($sql);
1858
        if ($resql) {
1859
            $num = $this->db->num_rows($resql);
1860
            $i = 0;
1861
            while ($i < $num) {
1862
                $obj = $this->db->fetch_object($resql);
1863
                $tab[] = array('fk_socpeople' => $obj->id, 'type_contact' => $obj->fk_c_type_contact);
1864
1865
                $i++;
1866
            }
1867
1868
            return $tab;
1869
        } else {
1870
            $this->error = $this->db->error();
1871
            dol_print_error($this->db);
1872
            return -1;
1873
        }
1874
    }
1875
1876
    /**
1877
     * Updates all roles (default contact for companies) according to values inside the ->roles array.
1878
     * This is called by update of contact.
1879
     *
1880
     * @return int
1881
     * @see fetchRoles()
1882
     */
1883
    public function updateRoles()
1884
    {
1885
        global $conf;
1886
1887
        $error = 0;
1888
1889
        if (!isset($this->roles)) {
1890
            return 0;   // Avoid to loose roles when property not set
1891
        }
1892
1893
        $this->db->begin();
1894
1895
        $sql = "DELETE FROM " . MAIN_DB_PREFIX . "societe_contacts WHERE fk_socpeople=" . ((int) $this->id) . " AND entity IN (" . getEntity("contact") . ")";
1896
1897
        $result = $this->db->query($sql);
1898
        if (!$result) {
1899
            $this->errors[] = $this->db->lasterror() . ' sql=' . $sql;
1900
            $error++;
1901
        } else {
1902
            if (count($this->roles) > 0) {
1903
                foreach ($this->roles as $keyRoles => $valRoles) {
1904
                    $idrole = 0;
1905
                    if (is_array($valRoles)) {
1906
                        $idrole = $valRoles['id'];
1907
                    } else {
1908
                        $idrole = $valRoles;
1909
                    }
1910
1911
                    $socid = 0;
1912
                    if (is_array($valRoles)) {
1913
                        $socid = $valRoles['socid'];
1914
                    } else {
1915
                        $socid = $this->socid;
1916
                    }
1917
1918
                    if ($socid > 0) {
1919
                        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_contacts";
1920
                        $sql .= " (entity,";
1921
                        $sql .= "date_creation,";
1922
                        $sql .= "fk_soc,";
1923
                        $sql .= "fk_c_type_contact,";
1924
                        $sql .= "fk_socpeople) ";
1925
                        $sql .= " VALUES (" . $conf->entity . ",";
1926
                        $sql .= "'" . $this->db->idate(dol_now()) . "',";
1927
                        $sql .= $socid . ", ";
1928
                        $sql .= $idrole . " , ";
1929
                        $sql .= $this->id;
1930
                        $sql .= ")";
1931
1932
                        $result = $this->db->query($sql);
1933
                        if (!$result) {
1934
                            $this->errors[] = $this->db->lasterror() . ' sql=' . $sql;
1935
                            $error++;
1936
                        }
1937
                    }
1938
                }
1939
            }
1940
        }
1941
        if (empty($error)) {
1942
            $this->db->commit();
1943
            return 1;
1944
        } else {
1945
            $this->error = implode(' ', $this->errors);
1946
            $this->db->rollback();
1947
            return $error * -1;
1948
        }
1949
    }
1950
1951
    /**
1952
     *  Load array of prospect status
1953
     *
1954
     *  @param  int     $active     1=Active only, 0=Not active only, -1=All
1955
     *  @return int                 Return integer <0 if KO, >0 if OK
1956
     */
1957
    public function loadCacheOfProspStatus($active = 1)
1958
    {
1959
        global $langs;
1960
1961
        $sql = "SELECT id, code, libelle as label, picto FROM " . MAIN_DB_PREFIX . "c_stcommcontact";
1962
        if ($active >= 0) {
1963
            $sql .= " WHERE active = " . ((int) $active);
1964
        }
1965
        $resql = $this->db->query($sql);
1966
        $num = $this->db->num_rows($resql);
1967
        $i = 0;
1968
        while ($i < $num) {
1969
            $obj = $this->db->fetch_object($resql);
1970
            $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);
1971
            $i++;
1972
        }
1973
        return 1;
1974
    }
1975
1976
    /**
1977
     *  Return prospect level
1978
     *
1979
     *  @return     string        Label
1980
     */
1981
    public function getLibProspLevel()
1982
    {
1983
        return $this->libProspLevel($this->fk_prospectlevel);
1984
    }
1985
1986
    /**
1987
     *  Return label of prospect level
1988
     *
1989
     *  @param  string  $fk_prospectlevel       Prospect level
1990
     *  @return string                          label of level
1991
     */
1992
    public function libProspLevel($fk_prospectlevel)
1993
    {
1994
        global $langs;
1995
1996
        $lib = $langs->trans("ProspectLevel" . $fk_prospectlevel);
1997
        // If lib not found in language file, we get label from cache/database
1998
        if ($lib == "ProspectLevel" . $fk_prospectlevel) {
1999
            $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
2000
        }
2001
        return $lib;
2002
    }
2003
2004
2005
    /**
2006
     *  Set prospect level
2007
     *
2008
     *  @param  User    $user       User who defines the discount
2009
     *  @return int                 Return integer <0 if KO, >0 if OK
2010
     * @deprecated Use update function instead
2011
     */
2012
    public function setProspectLevel(User $user)
2013
    {
2014
        return $this->update($this->id, $user);
2015
    }
2016
2017
    /**
2018
     *  Return status of prospect
2019
     *
2020
     *  @param  int     $mode       0=label long, 1=label short, 2=Picto + Label short, 3=Picto, 4=Picto + Label long
2021
     *  @param  string  $label      Label to use for status for added status
2022
     *  @return string              Label
2023
     */
2024
    public function getLibProspCommStatut($mode = 0, $label = '')
2025
    {
2026
        return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
2027
    }
2028
2029
    /**
2030
     *  Return label of a given status
2031
     *
2032
     *  @param  int|string  $statut         Id or code for prospection status
2033
     *  @param  int         $mode           0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
2034
     *  @param  string      $label          Label to use for status for added status
2035
     *  @param  string      $picto          Name of image file to show ('filenew', ...)
2036
     *                                      If no extension provided, we use '.png'. Image must be stored into theme/xxx/img directory.
2037
     *                                      Example: picto.png                  if picto.png is stored into htdocs/theme/mytheme/img
2038
     *                                      Example: picto.png@mymodule         if picto.png is stored into htdocs/mymodule/img
2039
     *                                      Example: /mydir/mysubdir/picto.png  if picto.png is stored into htdocs/mydir/mysubdir (pictoisfullpath must be set to 1)
2040
     *  @return string                      Label of status
2041
     */
2042
    public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')
2043
    {
2044
        global $langs;
2045
        $langs->load('customers');
2046
2047
        if ($mode == 2) {
2048
            if ($statut == '-1' || $statut == 'ST_NO') {
2049
                return img_action($langs->trans("StatusProspect-1"), -1, $picto) . ' ' . $langs->trans("StatusProspect-1");
2050
            } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2051
                return img_action($langs->trans("StatusProspect0"), 0, $picto) . ' ' . $langs->trans("StatusProspect0");
2052
            } elseif ($statut == '1' || $statut == 'ST_TODO') {
2053
                return img_action($langs->trans("StatusProspect1"), 1, $picto) . ' ' . $langs->trans("StatusProspect1");
2054
            } elseif ($statut == '2' || $statut == 'ST_PEND') {
2055
                return img_action($langs->trans("StatusProspect2"), 2, $picto) . ' ' . $langs->trans("StatusProspect2");
2056
            } elseif ($statut == '3' || $statut == 'ST_DONE') {
2057
                return img_action($langs->trans("StatusProspect3"), 3, $picto) . ' ' . $langs->trans("StatusProspect3");
2058
            } else {
2059
                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);
2060
            }
2061
        }
2062
        if ($mode == 3) {
2063
            if ($statut == '-1' || $statut == 'ST_NO') {
2064
                return img_action($langs->trans("StatusProspect-1"), -1, $picto);
2065
            } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2066
                return img_action($langs->trans("StatusProspect0"), 0, $picto);
2067
            } elseif ($statut == '1' || $statut == 'ST_TODO') {
2068
                return img_action($langs->trans("StatusProspect1"), 1, $picto);
2069
            } elseif ($statut == '2' || $statut == 'ST_PEND') {
2070
                return img_action($langs->trans("StatusProspect2"), 2, $picto);
2071
            } elseif ($statut == '3' || $statut == 'ST_DONE') {
2072
                return img_action($langs->trans("StatusProspect3"), 3, $picto);
2073
            } else {
2074
                return img_action(($langs->trans("StatusProspect" . $statut) != "StatusProspect" . $statut) ? $langs->trans("StatusProspect" . $statut) : $label, 0, $picto);
2075
            }
2076
        }
2077
        if ($mode == 4) {
2078
            if ($statut == '-1' || $statut == 'ST_NO') {
2079
                return img_action($langs->trans("StatusProspect-1"), -1, $picto) . ' ' . $langs->trans("StatusProspect-1");
2080
            } elseif ($statut == '0' || $statut == 'ST_NEVER') {
2081
                return img_action($langs->trans("StatusProspect0"), 0, $picto) . ' ' . $langs->trans("StatusProspect0");
2082
            } elseif ($statut == '1' || $statut == 'ST_TODO') {
2083
                return img_action($langs->trans("StatusProspect1"), 1, $picto) . ' ' . $langs->trans("StatusProspect1");
2084
            } elseif ($statut == '2' || $statut == 'ST_PEND') {
2085
                return img_action($langs->trans("StatusProspect2"), 2, $picto) . ' ' . $langs->trans("StatusProspect2");
2086
            } elseif ($statut == '3' || $statut == 'ST_DONE') {
2087
                return img_action($langs->trans("StatusProspect3"), 3, $picto) . ' ' . $langs->trans("StatusProspect3");
2088
            } else {
2089
                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);
2090
            }
2091
        }
2092
2093
        return "Error, mode/status not found";
2094
    }
2095
2096
2097
    /**
2098
     *  Set "blacklist" mailing status
2099
     *
2100
     *  @param  int     $no_email   1=Do not send mailing, 0=Ok to receive mailing
2101
     *  @return int                 Return integer <0 if KO, >0 if OK
2102
     */
2103
    public function setNoEmail($no_email)
2104
    {
2105
        $error = 0;
2106
2107
        // Update mass emailing flag into table mailing_unsubscribe
2108
        if ($this->email) {
2109
            $this->db->begin();
2110
2111
            if ($no_email) {
2112
                $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) . "'";
2113
                $resql = $this->db->query($sql);
2114
                if ($resql) {
2115
                    $obj = $this->db->fetch_object($resql);
2116
                    $noemail = $obj->nb;
2117
                    if (empty($noemail)) {
2118
                        $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()) . "')";
2119
                        $resql = $this->db->query($sql);
2120
                        if (!$resql) {
2121
                            $error++;
2122
                            $this->error = $this->db->lasterror();
2123
                            $this->errors[] = $this->error;
2124
                        }
2125
                    }
2126
                } else {
2127
                    $error++;
2128
                    $this->error = $this->db->lasterror();
2129
                    $this->errors[] = $this->error;
2130
                }
2131
            } else {
2132
                $sql = "DELETE FROM " . MAIN_DB_PREFIX . "mailing_unsubscribe WHERE email = '" . $this->db->escape($this->email) . "' AND entity IN (" . getEntity('mailing', 0) . ")";
2133
                $resql = $this->db->query($sql);
2134
                if (!$resql) {
2135
                    $error++;
2136
                    $this->error = $this->db->lasterror();
2137
                    $this->errors[] = $this->error;
2138
                }
2139
            }
2140
2141
            if (empty($error)) {
2142
                $this->no_email = $no_email;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Contact\Classes\Contact::$no_email has been deprecated: Has been replaced by a search into llx_mailing_unsubscribe ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

2142
                /** @scrutinizer ignore-deprecated */ $this->no_email = $no_email;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
2143
                $this->db->commit();
2144
                return 1;
2145
            } else {
2146
                $this->db->rollback();
2147
                return $error * -1;
2148
            }
2149
        }
2150
2151
        return 0;
2152
    }
2153
2154
    /**
2155
     *  get "blacklist" mailing status
2156
     *  set no_email attribute to 1 or 0
2157
     *
2158
     *  @return int                 Return integer <0 if KO, >0 if OK
2159
     */
2160
    public function getNoEmail()
2161
    {
2162
        if ($this->email) {
2163
            $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "mailing_unsubscribe WHERE entity IN (" . getEntity('mailing') . ") AND email = '" . $this->db->escape($this->email) . "'";
2164
            $resql = $this->db->query($sql);
2165
            if ($resql) {
2166
                $obj = $this->db->fetch_object($resql);
2167
                $this->no_email = $obj->nb;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Contact\Classes\Contact::$no_email has been deprecated: Has been replaced by a search into llx_mailing_unsubscribe ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

2167
                /** @scrutinizer ignore-deprecated */ $this->no_email = $obj->nb;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
2168
                return 1;
2169
            } else {
2170
                $this->error = $this->db->lasterror();
2171
                $this->errors[] = $this->error;
2172
                return -1;
2173
            }
2174
        }
2175
        return 0;
2176
    }
2177
2178
2179
    /**
2180
     *  Return clickable link of object (with eventually picto)
2181
     *
2182
     *  @param      string      $option                 Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
2183
     *  @param      array       $arraydata              Array of data
2184
     *  @return     string                              HTML Code for Kanban thumb.
2185
     */
2186
    public function getKanbanView($option = '', $arraydata = null)
2187
    {
2188
        $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2189
2190
        $return = '<div class="box-flex-item box-flex-grow-zero">';
2191
        $return .= '<div class="info-box info-box-sm">';
2192
        $return .= '<span class="info-box-icon bg-infobox-action">';
2193
        //var_dump($this->photo);exit;
2194
        if (property_exists($this, 'photo') && !is_null($this->photo)) {
2195
            $return .= Form::showphoto('contact', $this, 0, 60, 0, 'photokanban photoref photowithmargin photologintooltip', 'small', 0, 1);
2196
        } else {
2197
            $return .= img_picto('', $this->picto);
2198
        }
2199
        $return .= '</span>';
2200
        $return .= '<div class="info-box-content">';
2201
        $return .= '<div class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . (method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref) . '</div>';
2202
        if ($selected >= 0) {
2203
            $return .= '<input id="cb' . $this->id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this->id . '"' . ($selected ? ' checked="checked"' : '') . '>';
2204
        }
2205
        if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
2206
            $return .= '<div class="info-box-ref tdoverflowmax150">' . $this->thirdparty->getNomUrl(1) . '</div>';
2207
        }
2208
        /*if (property_exists($this, 'phone_pro') && !empty($this->phone_pro)) {
2209
            $return .= '<br>'.img_picto($langs->trans("Phone"), 'phone');
2210
            $return .= ' <span class="info-box-label">'.$this->phone_pro.'</span>';
2211
        }*/
2212
        /*if (method_exists($this, 'LibPubPriv')) {
2213
            $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Visibility").'</span>';
2214
            $return .= '<span> : '.$this->LibPubPriv($this->priv).'</span>';
2215
        }*/
2216
        if (method_exists($this, 'getLibStatut')) {
2217
            $return .= '<br><div class="info-box-status">' . $this->getLibStatut(3) . '</div>';
2218
        }
2219
        $return .= '</div>';
2220
        $return .= '</div>';
2221
        $return .= '</div>';
2222
        return $return;
2223
    }
2224
}
2225