Completed
Branch develop (59ab9a)
by
unknown
27:43
created

AdherentType::update()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 29
nc 12
nop 1
dl 0
loc 49
rs 6.7272
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
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 *	\file       htdocs/adherents/class/adherent_type.class.php
23
 *	\ingroup    member
24
 *	\brief      File of class to manage members types
25
 *	\author     Rodolphe Quiedeville
26
 */
27
28
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29
30
31
/**
32
 *	Class to manage members type
33
 */
34
class AdherentType extends CommonObject
35
{
36
	public $table_element = 'adherent_type';
37
	public $element = 'adherent_type';
38
	public $picto = 'group';
39
40
	/**
41
	 * @var string
42
	 * @deprecated Use label
43
	 * @see label
44
	 */
45
	public $libelle;
46
	/** @var string Label */
47
	public $label;
48
	/**
49
	 * @var bool
50
	 * @deprecated Use subscription
51
	 * @see subscription
52
	 */
53
	public $cotisation;
54
	/**
55
	 * @var int Subsription required (0 or 1)
56
	 * @since 5.0
57
	 */
58
	public $subscription;
59
	/** @var string Public note */
60
	public $note;
61
	/** @var bool Can vote*/
62
	public $vote;
63
	/** @var bool Email sent during validation */
64
	public $mail_valid;
65
66
67
    /**
68
	 *	Constructor
69
	 *
70
	 *	@param 		DoliDB		$db		Database handler
71
     */
72
    function __construct($db)
73
    {
74
        $this->db = $db;
75
        $this->statut = 1;
76
    }
77
78
79
    /**
80
     *  Fonction qui permet de creer le status de l'adherent
81
     *
82
     *  @param      User		$user		User making creation
83
     *  @return     int						>0 if OK, < 0 if KO
84
     */
85
    function create($user)
86
    {
87
        global $conf;
88
89
        $this->statut=(int) $this->statut;
90
        $this->label=(!empty($this->libelle)?trim($this->libelle):trim($this->label));
91
92
        $sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type (";
93
        $sql.= "libelle";
94
        $sql.= ", entity";
95
        $sql.= ") VALUES (";
96
        $sql.= "'".$this->db->escape($this->label)."'";
97
        $sql.= ", ".$conf->entity;
98
        $sql.= ")";
99
100
        dol_syslog("Adherent_type::create", LOG_DEBUG);
101
        $result = $this->db->query($sql);
102
        if ($result)
103
        {
104
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent_type");
105
            return $this->update($user);
106
        }
107
        else
108
        {
109
            $this->error=$this->db->error().' sql='.$sql;
110
            return -1;
111
        }
112
    }
113
114
115
    /**
116
     *  Met a jour en base donnees du type
117
     *
118
     *	@param		User	$user	Object user making change
119
     *  @return		int				>0 if OK, < 0 if KO
120
     */
121
    function update($user)
122
    {
123
    	global $hookmanager,$conf;
124
125
    	$error=0;
126
127
    	$this->label=(!empty($this->libelle)?trim($this->libelle):trim($this->label));
128
129
        $sql = "UPDATE ".MAIN_DB_PREFIX."adherent_type ";
130
        $sql.= "SET ";
131
        $sql.= "statut = ".$this->statut.",";
132
        $sql.= "libelle = '".$this->db->escape($this->label) ."',";
133
        $sql.= "subscription = '".$this->db->escape($this->subscription)."',";
134
        $sql.= "note = '".$this->db->escape($this->note)."',";
135
        $sql.= "vote = '".$this->db->escape($this->vote)."',";
136
        $sql.= "mail_valid = '".$this->db->escape($this->mail_valid)."'";
137
        $sql .= " WHERE rowid =".$this->id;
138
139
        $result = $this->db->query($sql);
140
        if ($result)
141
        {
142
        	$action='update';
143
144
        	// Actions on extra fields (by external module or standard code)
145
        	$hookmanager->initHooks(array('membertypedao'));
146
        	$parameters=array('membertype'=>$this->id);
147
        	$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
148
        	if (empty($reshook))
149
        	{
150
        		if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
151
        		{
152
        			$result=$this->insertExtraFields();
153
        			if ($result < 0)
154
        			{
155
        				$error++;
156
        			}
157
        		}
158
        	}
159
        	else if ($reshook < 0) $error++;
160
161
162
            return 1;
163
        }
164
        else
165
        {
166
            $this->error=$this->db->error().' sql='.$sql;
167
            return -1;
168
        }
169
    }
170
171
    /**
172
     *	Fonction qui permet de supprimer le status de l'adherent
173
     *
174
     *	@param      int		$rowid		Id of member type to delete
175
     *  @return		int					>0 if OK, 0 if not found, < 0 if KO
176
     */
177
    function delete($rowid='')
178
    {
179
    	if (empty($rowid)) $rowid=$this->id;
180
181
        $sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_type WHERE rowid = ".$rowid;
182
183
        $resql=$this->db->query($sql);
184
        if ($resql)
185
        {
186
            if ($this->db->affected_rows($resql))
187
            {
188
                return 1;
189
            }
190
            else
191
            {
192
                return 0;
193
            }
194
        }
195
        else
196
        {
197
            print "Err : ".$this->db->error();
198
            return 0;
199
        }
200
    }
201
202
    /**
203
     *  Fonction qui permet de recuperer le status de l'adherent
204
     *
205
     *  @param 		int		$rowid		Id of member type to load
206
     *  @return		int					<0 if KO, >0 if OK
207
     */
208
    function fetch($rowid)
209
    {
210
        $sql = "SELECT d.rowid, d.libelle as label, d.statut, d.subscription, d.mail_valid, d.note, d.vote";
211
        $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as d";
212
        $sql .= " WHERE d.rowid = ".$rowid;
213
214
        dol_syslog("Adherent_type::fetch", LOG_DEBUG);
215
216
        $resql=$this->db->query($sql);
217
        if ($resql)
218
        {
219
            if ($this->db->num_rows($resql))
220
            {
221
                $obj = $this->db->fetch_object($resql);
222
223
                $this->id             = $obj->rowid;
224
                $this->ref            = $obj->rowid;
225
                $this->label          = $obj->label;
226
                $this->libelle        = $obj->label;	// For backward compatibility
227
                $this->statut         = $obj->statut;
228
                $this->subscription   = $obj->subscription;
229
                $this->mail_valid     = $obj->mail_valid;
230
                $this->note           = $obj->note;
231
                $this->vote           = $obj->vote;
232
            }
233
            return 1;
234
        }
235
        else
236
        {
237
            $this->error=$this->db->lasterror();
238
            return -1;
239
        }
240
    }
241
242
    /**
243
     *  Return list of members' type
244
     *
245
     *  @return 	array	List of types of members
246
     */
247
    function liste_array()
248
    {
249
        global $conf,$langs;
250
251
        $adherenttypes = array();
252
253
        $sql = "SELECT rowid, libelle";
254
        $sql.= " FROM ".MAIN_DB_PREFIX."adherent_type";
255
        $sql.= " WHERE entity = ".$conf->entity;
256
257
        $resql=$this->db->query($sql);
258
        if ($resql)
259
        {
260
            $nump = $this->db->num_rows($resql);
261
262
            if ($nump)
263
            {
264
                $i = 0;
265
                while ($i < $nump)
266
                {
267
                    $obj = $this->db->fetch_object($resql);
268
269
                    $adherenttypes[$obj->rowid] = $langs->trans($obj->libelle);
270
                    $i++;
271
                }
272
            }
273
        }
274
        else
275
        {
276
            print $this->db->error();
277
        }
278
        return $adherenttypes;
279
    }
280
281
282
    /**
283
     *    	Return clicable name (with picto eventually)
284
     *
285
     *		@param		int		$withpicto		0=No picto, 1=Include picto into link, 2=Only picto
286
     *		@param		int		$maxlen			length max libelle
287
     *		@return		string					String with URL
288
     */
289
    function getNomUrl($withpicto=0,$maxlen=0)
290
    {
291
        global $langs;
292
293
        $result='';
294
        $label=$langs->trans("ShowTypeCard",$this->libelle);
295
296
        $link = '<a href="'.DOL_URL_ROOT.'/adherents/type.php?rowid='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
297
        $linkend='</a>';
298
299
        $picto='group';
300
301
        if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
302
        if ($withpicto && $withpicto != 2) $result.=' ';
303
        $result.=$link.($maxlen?dol_trunc($this->libelle,$maxlen):$this->libelle).$linkend;
304
        return $result;
305
    }
306
307
308
    /**
309
     *     getLibStatut
310
     *
311
     *     @return string     Return status of a type of member
312
     */
313
    function getLibStatut()
314
    {
315
    	return '';
316
    }
317
318
    /**
319
     *     getMailOnValid
320
     *
321
     *     @return string     Return mail model
322
     */
323
    function getMailOnValid()
324
    {
325
        global $conf;
326
327
        if (! empty($this->mail_valid) && trim(dol_htmlentitiesbr_decode($this->mail_valid)))
328
        {
329
            return $this->mail_valid;
330
        }
331
        else
332
        {
333
            return $conf->global->ADHERENT_MAIL_VALID;
334
        }
335
    }
336
337
    /**
338
     *     getMailOnSubscription
339
     *
340
     *     @return string     Return mail model
341
     */
342
    function getMailOnSubscription()
343
    {
344
        global $conf;
345
	// mail_subscription not  defined so never used
346
        if (! empty($this->mail_subscription) && trim(dol_htmlentitiesbr_decode($this->mail_subscription)))  // Property not yet defined
347
        {
348
            return $this->mail_subscription;
349
        }
350
        else
351
        {
352
            return $conf->global->ADHERENT_MAIL_COTIS;
353
        }
354
    }
355
356
    /**
357
     *     getMailOnResiliate
358
     *
359
     *     @return string     Return mail model
360
     */
361
    function getMailOnResiliate()
362
    {
363
        global $conf;
364
	// NOTE mail_resiliate not defined so never used
365
        if (! empty($this->mail_resiliate) && trim(dol_htmlentitiesbr_decode($this->mail_resiliate)))  // Property not yet defined
366
        {
367
            return $this->mail_resiliate;
368
        }
369
        else
370
        {
371
            return $conf->global->ADHERENT_MAIL_RESIL;
372
        }
373
    }
374
}
375