/
adherent_type.class.php
558 lines (482 loc) · 13.8 KB
/
adherent_type.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
/* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2009-2017 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/adherents/class/adherent_type.class.php
* \ingroup member
* \brief File of class to manage members types
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
/**
* Class to manage members type
*/
class AdherentType extends CommonObject
{
public $table_element = 'adherent_type';
public $element = 'adherent_type';
public $picto = 'group';
public $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
/**
* @var string
* @deprecated Use label
* @see label
*/
public $libelle;
/** @var string Label */
public $label;
/**
* @var int Subsription required (0 or 1)
* @since 5.0
*/
public $subscription;
/** @var string Public note */
public $note;
/** @var integer Can vote */
public $vote;
/** @var string Email sent during validation */
public $mail_valid;
/** @var array Array of members */
public $members=array();
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
$this->db = $db;
$this->statut = 1;
}
/**
* Fonction qui permet de creer le status de l'adherent
*
* @param User $user User making creation
* @param int $notrigger 1=do not execute triggers, 0 otherwise
* @return int >0 if OK, < 0 if KO
*/
function create($user,$notrigger=0)
{
global $conf;
$error=0;
$this->statut=(int) $this->statut;
$this->label=trim($this->label);
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type (";
$sql.= "libelle";
$sql.= ", entity";
$sql.= ") VALUES (";
$sql.= "'".$this->db->escape($this->label)."'";
$sql.= ", ".$conf->entity;
$sql.= ")";
dol_syslog("Adherent_type::create", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent_type");
$result = $this->update($user,1);
if ($result < 0)
{
$this->db->rollback();
return -3;
}
if (! $notrigger)
{
// Call trigger
$result=$this->call_trigger('MEMBER_TYPE_CREATE',$user);
if ($result < 0) { $error++; }
// End call triggers
}
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->lasterror();
$this->db->rollback();
return -1;
}
}
/**
* Met a jour en base donnees du type
*
* @param User $user Object user making change
* @param int $notrigger 1=do not execute triggers, 0 otherwise
* @return int >0 if OK, < 0 if KO
*/
function update($user,$notrigger=0)
{
global $conf, $hookmanager;
$error=0;
$this->label=trim($this->label);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."adherent_type ";
$sql.= "SET ";
$sql.= "statut = ".$this->statut.",";
$sql.= "libelle = '".$this->db->escape($this->label) ."',";
$sql.= "subscription = '".$this->db->escape($this->subscription)."',";
$sql.= "note = '".$this->db->escape($this->note)."',";
$sql.= "vote = ".(integer) $this->db->escape($this->vote).",";
$sql.= "mail_valid = '".$this->db->escape($this->mail_valid)."'";
$sql.= " WHERE rowid =".$this->id;
$result = $this->db->query($sql);
if ($result)
{
$action='update';
// Actions on extra fields (by external module or standard code)
$hookmanager->initHooks(array('membertypedao'));
$parameters=array('membertype'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
}
else if ($reshook < 0) $error++;
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('MEMBER_TYPE_MODIFY',$user);
if ($result < 0) { $error++; }
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
return -$error;
}
}
else
{
$this->error=$this->db->lasterror();
$this->db->rollback();
return -1;
}
}
/**
* Fonction qui permet de supprimer le status de l'adherent
*
* @return int >0 if OK, 0 if not found, < 0 if KO
*/
function delete()
{
global $user;
$error = 0;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_type";
$sql.= " WHERE rowid = ".$this->id;
$resql=$this->db->query($sql);
if ($resql)
{
// Call trigger
$result=$this->call_trigger('MEMBER_TYPE_DELETE',$user);
if ($result < 0) { $error++; $this->db->rollback(); return -2; }
// End call triggers
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Fonction qui permet de recuperer le status de l'adherent
*
* @param int $rowid Id of member type to load
* @return int <0 if KO, >0 if OK
*/
function fetch($rowid)
{
$sql = "SELECT d.rowid, d.libelle as label, d.statut, d.subscription, d.mail_valid, d.note, d.vote";
$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as d";
$sql .= " WHERE d.rowid = ".(int) $rowid;
dol_syslog("Adherent_type::fetch", LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
$this->label = $obj->label;
$this->statut = $obj->statut;
$this->subscription = $obj->subscription;
$this->mail_valid = $obj->mail_valid;
$this->note = $obj->note;
$this->vote = $obj->vote;
}
return 1;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Return list of members' type
*
* @return array List of types of members
*/
function liste_array()
{
global $conf,$langs;
$adherenttypes = array();
$sql = "SELECT rowid, libelle as label";
$sql.= " FROM ".MAIN_DB_PREFIX."adherent_type";
$sql.= " WHERE entity IN (".getEntity('member_type').")";
$resql=$this->db->query($sql);
if ($resql)
{
$nump = $this->db->num_rows($resql);
if ($nump)
{
$i = 0;
while ($i < $nump)
{
$obj = $this->db->fetch_object($resql);
$adherenttypes[$obj->rowid] = $langs->trans($obj->label);
$i++;
}
}
}
else
{
print $this->db->error();
}
return $adherenttypes;
}
/**
* Return array of Member objects for member type this->id (or all if this->id not defined)
*
* @param string $excludefilter Filter to exclude
* @param int $mode 0=Return array of member instance
* 1=Return array of member instance without extra data
* 2=Return array of members id only
* @return mixed Array of members or -1 on error
*/
function listMembersForMemberType($excludefilter='', $mode=0)
{
global $conf, $user;
$ret=array();
$sql = "SELECT a.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
$sql.= " WHERE a.entity IN (".getEntity('member').")";
$sql.= " AND a.fk_adherent_type = ".$this->id;
if (! empty($excludefilter)) $sql.=' AND ('.$excludefilter.')';
dol_syslog(get_class($this)."::listUsersForGroup", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
while ($obj = $this->db->fetch_object($resql))
{
if (! array_key_exists($obj->rowid, $ret))
{
if ($mode < 2)
{
$memberstatic=new Adherent($this->db);
if ($mode == 1) {
$memberstatic->fetch($obj->rowid,'','','',false, false);
} else {
$memberstatic->fetch($obj->rowid);
}
$ret[$obj->rowid]=$memberstatic;
}
else $ret[$obj->rowid]=$obj->rowid;
}
}
$this->db->free($resql);
$this->members=$ret;
return $ret;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Return clicable name (with picto eventually)
*
* @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
* @param int $maxlen length max label
* @param int $notooltip 1=Disable tooltip
* @return string String with URL
*/
function getNomUrl($withpicto=0, $maxlen=0, $notooltip=0)
{
global $langs;
$result='';
$label=$langs->trans("ShowTypeCard",$this->label);
$linkstart = '<a href="'.DOL_URL_ROOT.'/adherents/type.php?rowid='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
$linkend='</a>';
$result .= $linkstart;
if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->label,$maxlen):$this->label);
$result .= $linkend;
return $result;
}
/**
* getLibStatut
*
* @return string Return status of a type of member
*/
function getLibStatut()
{
return '';
}
/**
* Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
*
* @param array $info Info array loaded by _load_ldap_info
* @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
* 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
* 2=Return key only (uid=qqq)
* @return string DN
*/
function _load_ldap_dn($info,$mode=0)
{
global $conf;
$dn='';
if ($mode==0) $dn=$conf->global->LDAP_KEY_MEMBERS_TYPES."=".$info[$conf->global->LDAP_KEY_MEMBERS_TYPES].",".$conf->global->LDAP_MEMBER_TYPE_DN;
if ($mode==1) $dn=$conf->global->LDAP_MEMBER_TYPE_DN;
if ($mode==2) $dn=$conf->global->LDAP_KEY_MEMBERS_TYPES."=".$info[$conf->global->LDAP_KEY_MEMBERS_TYPES];
return $dn;
}
/**
* Initialize the info array (array of LDAP values) that will be used to call LDAP functions
*
* @return array Tableau info des attributs
*/
function _load_ldap_info()
{
global $conf,$langs;
$info=array();
// Object classes
$info["objectclass"]=explode(',',$conf->global->LDAP_MEMBER_TYPE_OBJECT_CLASS);
// Champs
if ($this->label && ! empty($conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME)) $info[$conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME] = $this->label;
if ($this->note && ! empty($conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION)) $info[$conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION] = $this->note;
if (! empty($conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS))
{
$valueofldapfield=array();
foreach($this->members as $key=>$val) // This is array of users for group into dolibarr database.
{
$member=new Adherent($this->db);
$member->fetch($val->id,'','','',false,false);
$info2 = $member->_load_ldap_info();
$valueofldapfield[] = $member->_load_ldap_dn($info2);
}
$info[$conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS] = (!empty($valueofldapfield)?$valueofldapfield:'');
}
return $info;
}
/**
* Initialise an instance with random values.
* Used to build previews or test instances.
* id must be 0 if object instance is a specimen.
*
* @return void
*/
function initAsSpecimen()
{
global $conf, $user, $langs;
// Initialise parametres
$this->id = 0;
$this->ref = 'MTSPEC';
$this->specimen=1;
$this->label='MEMBERS TYPE SPECIMEN';
$this->note='This is a note';
$this->mail_valid='This is welcome email';
$this->subscription=1;
$this->vote=0;
$this->statut=1;
// Members of this member type is just me
$this->members=array(
$user->id => $user
);
}
/**
* getMailOnValid
*
* @return string Return mail content of type or empty
*/
function getMailOnValid()
{
global $conf;
if (! empty($this->mail_valid) && trim(dol_htmlentitiesbr_decode($this->mail_valid)))
{
return $this->mail_valid;
}
return '';
}
/**
* getMailOnSubscription
*
* @return string Return mail content of type or empty
*/
function getMailOnSubscription()
{
global $conf;
// mail_subscription not defined so never used
if (! empty($this->mail_subscription) && trim(dol_htmlentitiesbr_decode($this->mail_subscription))) // Property not yet defined
{
return $this->mail_subscription;
}
return '';
}
/**
* getMailOnResiliate
*
* @return string Return mail model content of type or empty
*/
function getMailOnResiliate()
{
global $conf;
// NOTE mail_resiliate not defined so never used
if (! empty($this->mail_resiliate) && trim(dol_htmlentitiesbr_decode($this->mail_resiliate))) // Property not yet defined
{
return $this->mail_resiliate;
}
return '';
}
}