Passed
Branch develop (75104c)
by
unknown
85:26
created

AdherentType::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 0
dl 0
loc 24
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2002		Rodolphe Quiedeville	<[email protected]>
3
 * Copyright (C) 2004-2008	Laurent Destailleur		<[email protected]>
4
 * Copyright (C) 2009-2017	Regis Houssin			<[email protected]>
5
 * Copyright (C) 2016		Charlie Benke			<[email protected]>
6
 * Copyright (C) 2018-2019  Thibault Foucart		<[email protected]>
7
 * Copyright (C) 2021     	Waël Almoman            <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
/**
24
 *	\file       htdocs/adherents/class/adherent_type.class.php
25
 *	\ingroup    member
26
 *	\brief      File of class to manage members types
27
 */
28
29
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30
31
32
/**
33
 *	Class to manage members type
34
 */
35
class AdherentType extends CommonObject
36
{
37
	/**
38
	 * @var string Name of table without prefix where object is stored
39
	 */
40
	public $table_element = 'adherent_type';
41
42
	/**
43
	 * @var string ID to identify managed object
44
	 */
45
	public $element = 'adherent_type';
46
47
	/**
48
	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
49
	 */
50
	public $picto = 'members';
51
52
	/**
53
	 * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
54
	 * @var int
55
	 */
56
	public $ismultientitymanaged = 1;
57
58
	/**
59
	 * @var string
60
	 * @deprecated Use label
61
	 * @see $label
62
	 */
63
	public $libelle;
64
65
	/**
66
	 * @var string Adherent type label
67
	 */
68
	public $label;
69
70
	/**
71
	 * @var string Adherent type nature
72
	 */
73
	public $morphy;
74
75
	public $duration;
76
77
	/*
78
	* type expiration
79
	*/
80
	public $duration_value;
81
82
	/**
83
	 * Expiration unit
84
	 */
85
	public $duration_unit;
86
87
	/**
88
	 * @var int Subsription required (0 or 1)
89
	 */
90
	public $subscription;
91
92
	/**
93
	 * @var float|string 	Amount for subscription (null or '' means not defined)
94
	 */
95
	public $amount;
96
97
	/**
98
	 * @var int Amount can be choosen by the visitor during subscription (0 or 1)
99
	 */
100
	public $caneditamount;
101
102
	/**
103
	 * @var string 	Public note
104
	 * @deprecated
105
	 */
106
	public $note;
107
108
	/** @var string 	Public note */
109
	public $note_public;
110
111
	/** @var integer	Can vote */
112
	public $vote;
113
114
	/** @var string Email sent during validation of member */
115
	public $mail_valid;
116
117
	/** @var string Email sent after recording a new subscription */
118
	public $mail_subscription = '';
119
120
	/** @var string Email sent after resiliation */
121
	public $mail_resiliate = '';
122
123
	/** @var string Email sent after exclude */
124
	public $mail_exclude = '';
125
126
	/** @var array Array of members */
127
	public $members = array();
128
129
	/** @var string string other */
130
	public $other = array();
131
132
	public $multilangs = array();
133
134
135
	/**
136
	 *	Constructor
137
	 *
138
	 *	@param 		DoliDB		$db		Database handler
139
	 */
140
	public function __construct($db)
141
	{
142
		$this->db = $db;
143
		$this->status = 1;
144
	}
145
146
	/**
147
	 * Load array this->multilangs
148
	 *
149
	 * @return int        <0 if KO, >0 if OK
150
	 */
151
	public function getMultiLangs()
152
	{
153
		global $langs;
154
155
		$current_lang = $langs->getDefaultLang();
156
157
		$sql = "SELECT lang, label, description, email";
158
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type_lang";
159
		$sql .= " WHERE fk_type = ".((int) $this->id);
160
161
		$result = $this->db->query($sql);
162
		if ($result) {
163
			while ($obj = $this->db->fetch_object($result)) {
164
				//print 'lang='.$obj->lang.' current='.$current_lang.'<br>';
165
				if ($obj->lang == $current_lang) {  // si on a les traduct. dans la langue courante on les charge en infos principales.
166
					$this->label        = $obj->label;
167
					$this->description = $obj->description;
168
					$this->email        = $obj->email;
169
				}
170
				$this->multilangs["$obj->lang"]["label"] = $obj->label;
171
				$this->multilangs["$obj->lang"]["description"] = $obj->description;
172
				$this->multilangs["$obj->lang"]["email"] = $obj->email;
173
			}
174
			return 1;
175
		} else {
176
			$this->error = "Error: ".$this->db->lasterror()." - ".$sql;
177
			return -1;
178
		}
179
	}
180
181
	/**
182
	 * Update or add a translation for this member type
183
	 *
184
	 * @param  User $user Object user making update
185
	 * @return int        <0 if KO, >0 if OK
186
	 */
187
	public function setMultiLangs($user)
188
	{
189
		global $conf, $langs;
190
191
		$langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
192
		$current_lang = $langs->getDefaultLang();
193
194
		foreach ($langs_available as $key => $value) {
195
			if ($key == $current_lang) {
196
				$sql = "SELECT rowid";
197
				$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type_lang";
198
				$sql .= " WHERE fk_type = ".((int) $this->id);
199
				$sql .= " AND lang = '".$this->db->escape($key)."'";
200
201
				$result = $this->db->query($sql);
202
203
				if ($this->db->num_rows($result)) { // if there is already a description line for this language
204
					$sql2 = "UPDATE ".MAIN_DB_PREFIX."adherent_type_lang";
205
					$sql2 .= " SET";
206
					$sql2 .= " label = '".$this->db->escape($this->label)."',";
207
					$sql2 .= " description = '".$this->db->escape($this->description)."'";
208
					$sql2 .= " WHERE fk_type = ".((int) $this->id)." AND lang='".$this->db->escape($key)."'";
209
				} else {
210
					$sql2 = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type_lang (fk_type, lang, label, description";
211
					$sql2 .= ")";
212
					$sql2 .= " VALUES(".((int) $this->id).",'".$this->db->escape($key)."','".$this->db->escape($this->label)."',";
213
					$sql2 .= " '".$this->db->escape($this->description)."'";
214
					$sql2 .= ")";
215
				}
216
				dol_syslog(get_class($this).'::setMultiLangs key = current_lang = '.$key);
217
				if (!$this->db->query($sql2)) {
218
					$this->error = $this->db->lasterror();
219
					return -1;
220
				}
221
			} elseif (isset($this->multilangs[$key])) {
222
				$sql = "SELECT rowid";
223
				$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type_lang";
224
				$sql .= " WHERE fk_type = ".((int) $this->id);
225
				$sql .= " AND lang = '".$this->db->escape($key)."'";
226
227
				$result = $this->db->query($sql);
228
229
				if ($this->db->num_rows($result)) { // if there is already a description line for this language
230
					$sql2 = "UPDATE ".MAIN_DB_PREFIX."adherent_type_lang";
231
					$sql2 .= " SET ";
232
					$sql2 .= " label = '".$this->db->escape($this->multilangs["$key"]["label"])."',";
233
					$sql2 .= " description = '".$this->db->escape($this->multilangs["$key"]["description"])."'";
234
					$sql2 .= " WHERE fk_type = ".((int) $this->id)." AND lang='".$this->db->escape($key)."'";
235
				} else {
236
					$sql2 = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type_lang (fk_type, lang, label, description";
237
					$sql2 .= ")";
238
					$sql2 .= " VALUES(".$this->id.",'".$this->db->escape($key)."','".$this->db->escape($this->multilangs["$key"]["label"])."',";
239
					$sql2 .= " '".$this->db->escape($this->multilangs["$key"]["description"])."'";
240
					$sql2 .= ")";
241
				}
242
243
				// We do not save if main fields are empty
244
				if ($this->multilangs["$key"]["label"] || $this->multilangs["$key"]["description"]) {
245
					if (!$this->db->query($sql2)) {
246
						$this->error = $this->db->lasterror();
247
						return -1;
248
					}
249
				}
250
			} else {
251
				// language is not current language and we didn't provide a multilang description for this language
252
			}
253
		}
254
255
		// Call trigger
256
		$result = $this->call_trigger('MEMBER_TYPE_SET_MULTILANGS', $user);
257
		if ($result < 0) {
258
			$this->error = $this->db->lasterror();
259
			return -1;
260
		}
261
		// End call triggers
262
263
		return 1;
264
	}
265
266
	   /**
267
		* Delete a language for this member type
268
		*
269
		* @param string $langtodelete 	Language code to delete
270
		* @param User   $user         	Object user making delete
271
		* @return int                   <0 if KO, >0 if OK
272
		*/
273
	public function delMultiLangs($langtodelete, $user)
274
	{
275
		$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_type_lang";
276
		$sql .= " WHERE fk_type = ".((int) $this->id)." AND lang = '".$this->db->escape($langtodelete)."'";
277
278
		dol_syslog(get_class($this).'::delMultiLangs', LOG_DEBUG);
279
		$result = $this->db->query($sql);
280
		if ($result) {
281
			// Call trigger
282
			$result = $this->call_trigger('MEMBER_TYPE_DEL_MULTILANGS', $user);
283
			if ($result < 0) {
284
				$this->error = $this->db->lasterror();
285
				dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
286
				return -1;
287
			}
288
			// End call triggers
289
			return 1;
290
		} else {
291
			$this->error = $this->db->lasterror();
292
			dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
293
			return -1;
294
		}
295
	}
296
297
	/**
298
	 *  Function to create the member type
299
	 *
300
	 *  @param	User	$user			User making creation
301
	 *  @param	int		$notrigger		1=do not execute triggers, 0 otherwise
302
	 *  @return	int						>0 if OK, < 0 if KO
303
	 */
304
	public function create($user, $notrigger = 0)
305
	{
306
		global $langs, $conf;
307
308
		$error = 0;
309
310
		$this->status = (int) $this->status;
311
		$this->label = trim($this->label);
312
313
		$this->db->begin();
314
315
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type (";
316
		$sql .= " morphy";
317
		$sql .= ", libelle";
318
		$sql .= ", entity";
319
		$sql .= ") VALUES (";
320
		$sql .= "'".$this->db->escape($this->morphy)."'";
321
		$sql .= ", '".$this->db->escape($this->label)."'";
322
		$sql .= ", ".((int) $conf->entity);
323
		$sql .= ")";
324
325
		dol_syslog("Adherent_type::create", LOG_DEBUG);
326
		$result = $this->db->query($sql);
327
		if ($result) {
328
			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent_type");
329
330
			$result = $this->update($user, 1);
331
			if ($result < 0) {
332
				$this->db->rollback();
333
				return -3;
334
			}
335
336
			if (!$notrigger) {
337
				// Call trigger
338
				$result = $this->call_trigger('MEMBER_TYPE_CREATE', $user);
339
				if ($result < 0) {
340
					$error++;
341
				}
342
				// End call triggers
343
			}
344
345
			if (!$error) {
346
				$this->db->commit();
347
				return $this->id;
348
			} else {
349
				dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
350
				$this->db->rollback();
351
				return -2;
352
			}
353
		} else {
354
			$this->error = $this->db->lasterror();
355
			$this->db->rollback();
356
			return -1;
357
		}
358
	}
359
360
	/**
361
	 *  Updating the type in the database
362
	 *
363
	 *  @param	User	$user			Object user making change
364
	 *  @param	int		$notrigger		1=do not execute triggers, 0 otherwise
365
	 *  @return	int						>0 if OK, < 0 if KO
366
	 */
367
	public function update($user, $notrigger = 0)
368
	{
369
		global $langs, $conf, $hookmanager;
370
371
		$error = 0;
372
373
		$this->label = trim($this->label);
374
375
		if (empty($this->note_public) && !empty($this->note)) {		// For backward compatibility
376
			$this->note_public = $this->note;
377
		}
378
379
		$this->db->begin();
380
381
		$sql = "UPDATE ".MAIN_DB_PREFIX."adherent_type ";
382
		$sql .= "SET ";
383
		$sql .= "statut = ".((int) $this->status).",";
384
		$sql .= "libelle = '".$this->db->escape($this->label)."',";
385
		$sql .= "morphy = '".$this->db->escape($this->morphy)."',";
386
		$sql .= "subscription = '".$this->db->escape($this->subscription)."',";
387
		$sql .= "amount = ".((empty($this->amount) && $this->amount == '') ? 'null' : ((float) $this->amount)).",";
388
		$sql .= "caneditamount = ".((int) $this->caneditamount).",";
389
		$sql .= "duration = '".$this->db->escape($this->duration_value.$this->duration_unit)."',";
390
		$sql .= "note = '".$this->db->escape($this->note_public)."',";
391
		$sql .= "vote = ".(integer) $this->db->escape($this->vote).",";
392
		$sql .= "mail_valid = '".$this->db->escape($this->mail_valid)."'";
393
		$sql .= " WHERE rowid =".((int) $this->id);
394
395
		$result = $this->db->query($sql);
396
		if ($result) {
397
			$this->description = $this->db->escape($this->note_public);
398
399
			// Multilangs
400
			if (getDolGlobalInt('MAIN_MULTILANGS')) {
401
				if ($this->setMultiLangs($user) < 0) {
402
					$this->error = $langs->trans("Error")." : ".$this->db->error()." - ".$sql;
403
					return -2;
404
				}
405
			}
406
407
			$action = 'update';
408
409
			// Actions on extra fields
410
			if (!$error) {
411
				$result = $this->insertExtraFields();
412
				if ($result < 0) {
413
					$error++;
414
				}
415
			}
416
417
			if (!$error && !$notrigger) {
418
				// Call trigger
419
				$result = $this->call_trigger('MEMBER_TYPE_MODIFY', $user);
420
				if ($result < 0) {
421
					$error++;
422
				}
423
				// End call triggers
424
			}
425
426
			if (!$error) {
427
				$this->db->commit();
428
				return 1;
429
			} else {
430
				$this->db->rollback();
431
				dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
432
				return -$error;
433
			}
434
		} else {
435
			$this->error = $this->db->lasterror();
436
			$this->db->rollback();
437
			return -1;
438
		}
439
	}
440
441
	/**
442
	 *	Function to delete the member's status
443
	 *  TODO Add param "User $user"
444
	 *
445
	 *  @return		int		> 0 if OK, 0 if not found, < 0 if KO
446
	 */
447
	public function delete()
448
	{
449
		global $user;
450
451
		$error = 0;
452
453
		$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_type";
454
		$sql .= " WHERE rowid = ".((int) $this->id);
455
456
		$resql = $this->db->query($sql);
457
		if ($resql) {
458
			// Call trigger
459
			$result = $this->call_trigger('MEMBER_TYPE_DELETE', $user);
460
			if ($result < 0) {
461
				$error++; $this->db->rollback(); return -2;
462
			}
463
			// End call triggers
464
465
			$this->db->commit();
466
			return 1;
467
		} else {
468
			$this->db->rollback();
469
			$this->error = $this->db->lasterror();
470
			return -1;
471
		}
472
	}
473
474
	/**
475
	 *  Function that retrieves the properties of a membership type
476
	 *
477
	 *  @param 		int		$rowid			Id of member type to load
478
	 *  @return		int						<0 if KO, >0 if OK
479
	 */
480
	public function fetch($rowid)
481
	{
482
		global $langs, $conf;
483
484
		$sql = "SELECT d.rowid, d.libelle as label, d.morphy, d.statut as status, d.duration, d.subscription, d.amount, d.caneditamount, d.mail_valid, d.note as note_public, d.vote";
485
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as d";
486
		$sql .= " WHERE d.rowid = ".(int) $rowid;
487
488
		dol_syslog("Adherent_type::fetch", LOG_DEBUG);
489
490
		$resql = $this->db->query($sql);
491
		if ($resql) {
492
			if ($this->db->num_rows($resql)) {
493
				$obj = $this->db->fetch_object($resql);
494
495
				$this->id             = $obj->rowid;
496
				$this->ref            = $obj->rowid;
497
				$this->label          = $obj->label;
498
				$this->morphy         = $obj->morphy;
499
				$this->status         = $obj->status;
500
				$this->duration       = $obj->duration;
501
				$this->duration_value = substr($obj->duration, 0, dol_strlen($obj->duration) - 1);
502
				$this->duration_unit  = substr($obj->duration, -1);
503
				$this->subscription   = $obj->subscription;
504
				$this->amount         = $obj->amount;
505
				$this->caneditamount  = $obj->caneditamount;
506
				$this->mail_valid     = $obj->mail_valid;
507
				$this->note           = $obj->note_public;	// deprecated
508
				$this->note_public    = $obj->note_public;
509
				$this->vote           = $obj->vote;
510
511
				// multilangs
512
				if (getDolGlobalInt('MAIN_MULTILANGS')) {
513
					$this->getMultiLangs();
514
				}
515
516
				// fetch optionals attributes and labels
517
				$this->fetch_optionals();
518
			}
519
520
			return 1;
521
		} else {
522
			$this->error = $this->db->lasterror();
523
			return -1;
524
		}
525
	}
526
527
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
528
	/**
529
	 *  Return list of members' type
530
	 *
531
	 *  @param	int		$status			Filter on status of type
532
	 *  @return array					List of types of members
533
	 */
534
	public function liste_array($status = -1)
535
	{
536
		// phpcs:enable
537
		global $conf, $langs;
538
539
		$adherenttypes = array();
540
541
		$sql = "SELECT rowid, libelle as label";
542
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
543
		$sql .= " WHERE entity IN (".getEntity('member_type').")";
544
		if ($status >= 0) {
545
			$sql .= " AND statut = ".((int) $status);
546
		}
547
548
		$resql = $this->db->query($sql);
549
		if ($resql) {
550
			$nump = $this->db->num_rows($resql);
551
552
			if ($nump) {
553
				$i = 0;
554
				while ($i < $nump) {
555
					$obj = $this->db->fetch_object($resql);
556
557
					$adherenttypes[$obj->rowid] = $langs->trans($obj->label);
558
					$i++;
559
				}
560
			}
561
		} else {
562
			print $this->db->error();
563
		}
564
		return $adherenttypes;
565
	}
566
567
	/**
568
	 *  Return the array of all amounts per membership type id
569
	 *
570
	 *  @param	int		$status			Filter on status of type
571
	 *  @return array					Array of membership type
572
	 */
573
	public function amountByType($status = null)
574
	{
575
		global $conf, $langs;
576
577
		$amountbytype = array();
578
579
		$sql = "SELECT rowid, amount";
580
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
581
		$sql .= " WHERE entity IN (".getEntity('member_type').")";
582
		if ($status !== null) {
583
			$sql .= " AND statut = ".((int) $status);
584
		}
585
586
		$resql = $this->db->query($sql);
587
		if ($resql) {
588
			$nump = $this->db->num_rows($resql);
589
590
			if ($nump) {
591
				$i = 0;
592
				while ($i < $nump) {
593
					$obj = $this->db->fetch_object($resql);
594
595
					$amountbytype[$obj->rowid] = $obj->amount;
596
					$i++;
597
				}
598
			}
599
		} else {
600
			print $this->db->error();
601
		}
602
603
		return $amountbytype;
604
	}
605
606
	/**
607
	 * 	Return array of Member objects for member type this->id (or all if this->id not defined)
608
	 *
609
	 * 	@param	string	$excludefilter		Filter to exclude. This value must not come from a user input.
610
	 *  @param	int		$mode				0=Return array of member instance
611
	 *  									1=Return array of member instance without extra data
612
	 *  									2=Return array of members id only
613
	 * 	@return	mixed						Array of members or -1 on error
614
	 */
615
	public function listMembersForMemberType($excludefilter = '', $mode = 0)
616
	{
617
		global $conf, $user;
618
619
		$ret = array();
620
621
		$sql = "SELECT a.rowid";
622
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
623
		$sql .= " WHERE a.entity IN (".getEntity('member').")";
624
		$sql .= " AND a.fk_adherent_type = ".((int) $this->id);
625
		if (!empty($excludefilter)) {
626
			$sql .= ' AND ('.$excludefilter.')';
627
		}
628
629
		dol_syslog(get_class($this)."::listUsersForGroup", LOG_DEBUG);
630
		$resql = $this->db->query($sql);
631
		if ($resql) {
632
			while ($obj = $this->db->fetch_object($resql)) {
633
				if (!array_key_exists($obj->rowid, $ret)) {
634
					if ($mode < 2) {
635
						$memberstatic = new Adherent($this->db);
636
						if ($mode == 1) {
637
							$memberstatic->fetch($obj->rowid, '', '', '', false, false);
638
						} else {
639
							$memberstatic->fetch($obj->rowid);
640
						}
641
						$ret[$obj->rowid] = $memberstatic;
642
					} else {
643
						$ret[$obj->rowid] = $obj->rowid;
644
					}
645
				}
646
			}
647
648
			$this->db->free($resql);
649
650
			$this->members = $ret;
651
652
			return $ret;
653
		} else {
654
			$this->error = $this->db->lasterror();
655
			return -1;
656
		}
657
	}
658
659
	/**
660
	 *	Return translated label by the nature of a adherent (physical or moral)
661
	 *
662
	 *	@param	string		$morphy		Nature of the adherent (physical or moral)
663
	 *	@return	string					Label
664
	 */
665
	public function getmorphylib($morphy = '')
666
	{
667
		global $langs;
668
		if ($morphy == 'phy') {
669
			return $langs->trans("Physical");
670
		} elseif ($morphy == 'mor') {
671
			return $langs->trans("Moral");
672
		} else {
673
			return $langs->trans("MorAndPhy");
674
		}
675
		//return $morphy;
676
	}
677
678
	/**
679
	 *  Return clicable name (with picto eventually)
680
	 *
681
	 *  @param		int		$withpicto					0=No picto, 1=Include picto into link, 2=Only picto
682
	 *  @param		int		$maxlen						length max label
683
	 *  @param		int  	$notooltip					1=Disable tooltip
684
	 *  @param  	string  $morecss                    Add more css on link
685
	 *  @param  	int     $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
686
	 *  @return		string								String with URL
687
	 */
688
	public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
689
	{
690
		global $langs;
691
692
		$result = '';
693
694
		$label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("MemberType").'</u>';
695
		$label .= ' '.$this->getLibStatut(4);
696
		$label .= '<br>'.$langs->trans("Label").': '.$this->label;
697
		if (isset($this->subscription)) {
698
			$label .= '<br>'.$langs->trans("SubscriptionRequired").': '.yn($this->subscription);
699
		}
700
701
		$option = '';
702
703
		$url = DOL_URL_ROOT.'/adherents/type.php?rowid='.((int) $this->id);
704
705
		if ($option != 'nolink') {
706
			// Add param to save lastsearch_values or not
707
			$add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
708
			if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
709
				$add_save_lastsearch_values = 1;
710
			}
711
			if ($add_save_lastsearch_values) {
712
				$url .= '&save_lastsearch_values=1';
713
			}
714
		}
715
716
		$linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
717
		$linkend = '</a>';
718
719
		$result .= $linkstart;
720
		if ($withpicto) {
721
			$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);
722
		}
723
		if ($withpicto != 2) {
724
			$result .= ($maxlen ?dol_trunc($this->label, $maxlen) : $this->label);
725
		}
726
		$result .= $linkend;
727
728
		return $result;
729
	}
730
731
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
732
	/**
733
	 *    Return label of status (activity, closed)
734
	 *
735
	 *    @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
736
	 *    @return   string     	   		Label of status
737
	 */
738
	public function getLibStatut($mode = 0)
739
	{
740
		return $this->LibStatut($this->status, $mode);
741
	}
742
743
	/**
744
	 *  Return the label of a given status
745
	 *
746
	 *  @param	int		$status         Status id
747
	 *  @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
748
	 *  @return	string          		Status label
749
	 */
750
	public function LibStatut($status, $mode = 0)
751
	{
752
		// phpcs:enable
753
		global $langs;
754
		$langs->load('companies');
755
756
		$statusType = 'status4';
757
		if ($status == 0) {
758
			$statusType = 'status5';
759
		}
760
761
		if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
762
			$this->labelStatus[0] = $langs->transnoentitiesnoconv("ActivityCeased");
763
			$this->labelStatus[1] = $langs->transnoentitiesnoconv("InActivity");
764
			$this->labelStatusShort[0] = $langs->transnoentitiesnoconv("ActivityCeased");
765
			$this->labelStatusShort[1] = $langs->transnoentitiesnoconv("InActivity");
766
		}
767
768
		return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
769
	}
770
771
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
772
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
773
	/**
774
	 *	Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
775
	 *
776
	 *	@param		array	$info		Info array loaded by _load_ldap_info
777
	 *	@param		int		$mode		0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
778
	 *									1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
779
	 *									2=Return key only (uid=qqq)
780
	 *	@return		string				DN
781
	 */
782
	public function _load_ldap_dn($info, $mode = 0)
783
	{
784
		// phpcs:enable
785
		global $conf;
786
		$dn = '';
787
		if ($mode == 0) {
788
			$dn = $conf->global->LDAP_KEY_MEMBERS_TYPES."=".$info[$conf->global->LDAP_KEY_MEMBERS_TYPES].",".$conf->global->LDAP_MEMBER_TYPE_DN;
789
		}
790
		if ($mode == 1) {
791
			$dn = $conf->global->LDAP_MEMBER_TYPE_DN;
792
		}
793
		if ($mode == 2) {
794
			$dn = $conf->global->LDAP_KEY_MEMBERS_TYPES."=".$info[$conf->global->LDAP_KEY_MEMBERS_TYPES];
795
		}
796
		return $dn;
797
	}
798
799
800
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
801
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
802
	/**
803
	 *	Initialize the info array (array of LDAP values) that will be used to call LDAP functions
804
	 *
805
	 *	@return		array		Tableau info des attributs
806
	 */
807
	public function _load_ldap_info()
808
	{
809
		// phpcs:enable
810
		global $conf, $langs;
811
812
		$info = array();
813
814
		// Object classes
815
		$info["objectclass"] = explode(',', $conf->global->LDAP_MEMBER_TYPE_OBJECT_CLASS);
816
817
		if (empty($this->note_public) && !empty($this->note)) {		// For backward compatibility
818
			$this->note_public = $this->note;
819
		}
820
821
		// Champs
822
		if ($this->label && !empty($conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME)) {
823
			$info[$conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME] = $this->label;
824
		}
825
		if ($this->note_public && !empty($conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION)) {
826
			$info[$conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_public, 0, 'UTF-8', 1);
827
		}
828
		if (!empty($conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS)) {
829
			$valueofldapfield = array();
830
			foreach ($this->members as $key => $val) {    // This is array of users for group into dolibarr database.
831
				$member = new Adherent($this->db);
832
				$member->fetch($val->id, '', '', '', false, false);
833
				$info2 = $member->_load_ldap_info();
834
				$valueofldapfield[] = $member->_load_ldap_dn($info2);
835
			}
836
			$info[$conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS] = (!empty($valueofldapfield) ? $valueofldapfield : '');
837
		}
838
		return $info;
839
	}
840
841
	/**
842
	 *  Initialise an instance with random values.
843
	 *  Used to build previews or test instances.
844
	 *	id must be 0 if object instance is a specimen.
845
	 *
846
	 *  @return	void
847
	 */
848
	public function initAsSpecimen()
849
	{
850
		global $user;
851
852
		// Initialise parametres
853
		$this->id = 0;
854
		$this->ref = 'MTSPEC';
855
		$this->specimen = 1;
856
857
		$this->label = 'MEMBERS TYPE SPECIMEN';
858
		$this->note_public = 'This is a public note';
859
		$this->mail_valid = 'This is welcome email';
860
		$this->subscription = 1;
861
		$this->caneditamount = 0;
862
		$this->vote = 0;
863
864
		$this->status = 1;
865
866
		// Members of this member type is just me
867
		$this->members = array(
868
			$user->id => $user
869
		);
870
	}
871
872
	/**
873
	 *     getMailOnValid
874
	 *
875
	 *     @return string     Return mail content of type or empty
876
	 */
877
	public function getMailOnValid()
878
	{
879
		if (!empty($this->mail_valid) && trim(dol_htmlentitiesbr_decode($this->mail_valid))) {
880
			return $this->mail_valid;
881
		}
882
883
		return '';
884
	}
885
886
	/**
887
	 *     getMailOnSubscription
888
	 *
889
	 *     @return string     Return mail content of type or empty
890
	 */
891
	public function getMailOnSubscription()
892
	{
893
		// mail_subscription not  defined so never used
894
		if (!empty($this->mail_subscription) && trim(dol_htmlentitiesbr_decode($this->mail_subscription))) {  // Property not yet defined
895
			return $this->mail_subscription;
896
		}
897
898
		return '';
899
	}
900
901
	/**
902
	 *     getMailOnResiliate
903
	 *
904
	 *     @return string     Return mail model content of type or empty
905
	 */
906
	public function getMailOnResiliate()
907
	{
908
		// NOTE mail_resiliate not defined so never used
909
		if (!empty($this->mail_resiliate) && trim(dol_htmlentitiesbr_decode($this->mail_resiliate))) {  // Property not yet defined
910
			return $this->mail_resiliate;
911
		}
912
913
		return '';
914
	}
915
916
	/**
917
	 *     getMailOnExclude
918
	 *
919
	 *     @return string     Return mail model content of type or empty
920
	 */
921
	public function getMailOnExclude()
922
	{
923
		// NOTE mail_exclude not defined so never used
924
		if (!empty($this->mail_exclude) && trim(dol_htmlentitiesbr_decode($this->mail_exclude))) {  // Property not yet defined
925
			return $this->mail_exclude;
926
		}
927
928
		return '';
929
	}
930
931
932
	/**
933
	 *	Return clicable link of object (with eventually picto)
934
	 *
935
	 *	@param      string	    $option                 Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
936
	 *  @return 	string				HTML Code for Kanban thumb.
937
	 */
938
	public function getKanbanView($option = '')
939
	{
940
		global $langs,$user;
941
		$return = '<div class="box-flex-item box-flex-grow-zero">';
942
		$return .= '<div class="info-box info-box-sm">';
943
		$return .= '<span class="info-box-icon bg-infobox-action">';
944
		$return .= img_picto('', $this->picto);
945
		$return .= '</span>';
946
		$return .= '<div class="info-box-content">';
947
		$return .= '<span class="info-box-ref">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
948
		if ($user->rights->adherent->configurer) {
949
			$return .= '<span class="right paddingleft"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit&rowid='.$this->ref.'">'.img_edit().'</a></span>';
950
		} else {
951
			$return .= '<span class="right">&nbsp;</span>';
952
		}
953
		if (property_exists($this, 'vote')) {
954
			$return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("VoteAllowed").' : '.yn($this->vote).'</span>';
955
		}
956
		if (property_exists($this, 'amount')) {
957
			if (is_null($this->amount) || $this->amount === '') {
958
				$return .= '<br>';
959
			} else {
960
				$return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Amount").'</span>';
961
				$return .= '<span class="amount"> : '.price($this->amount).'</span>';
962
			}
963
		}
964
		if (method_exists($this, 'getLibStatut')) {
965
			$return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(5).'</div>';
966
		}
967
		$return .= '</div>';
968
		$return .= '</div>';
969
		$return .= '</div>';
970
		return $return;
971
	}
972
}
973