4eb69902ffbfd637990b35b4b9af9bc8d7b8e67f
[koha.git] / C4 / Members / AttributeTypes.pm
1 package C4::Members::AttributeTypes;
2
3 # Copyright (C) 2008 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Context;
22
23 use vars qw($VERSION);
24
25 BEGIN {
26     # set the version for version checking
27     $VERSION = 3.00;
28 }
29
30 =head1 NAME
31
32 C4::Members::AttributeTypes - mananage extended patron attribute types
33
34 =head1 SYNOPSIS
35
36 =over 4
37
38 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
39
40 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
41 $attr_type->code($code);
42 $attr_type->description($description);
43 $attr_type->repeatable($repeatable);
44 $attr_type->unique_id($unique_id);
45 $attr_type->opac_display($opac_display);
46 $attr_type->password_allowed($password_allowed);
47 $attr_type->staff_searchable($staff_searchable);
48 $attr_type->authorised_value_category($authorised_value_category);
49 $attr_type->store();
50 $attr_type->delete();
51
52 my $attr_type = C4::Members::AttributeTypes->fetch($code);
53 $attr_type = C4::Members::AttributeTypes->delete($code);
54
55 =back
56
57 =head1 FUNCTIONS
58
59 =head2 GetAttributeTypes
60
61 =over 4
62
63 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
64
65 =back
66
67 Returns an array of hashrefs of each attribute type defined
68 in the database.  The array is sorted by code.  Each hashref contains
69 the following fields:
70
71 code
72 description
73
74 =cut
75
76 sub GetAttributeTypes {
77     my $dbh = C4::Context->dbh;
78     my $sth = $dbh->prepare('SELECT code, description FROM borrower_attribute_types ORDER by code');
79     $sth->execute();
80     my @results = ();
81     while (my $row = $sth->fetchrow_hashref) {
82         push @results, $row;
83     }
84     return @results;
85 }
86
87 =head1 METHODS 
88
89 =over 4
90
91 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
92
93 =back
94
95 Create a new attribute type.
96
97 =cut 
98
99 sub new {
100     my $class = shift;
101     my $self = {};
102
103     $self->{'code'} = shift;
104     $self->{'description'} = shift;
105     $self->{'repeatable'} = 0;
106     $self->{'unique_id'} = 0;
107     $self->{'opac_display'} = 0;
108     $self->{'password_allowed'} = 0;
109     $self->{'staff_searchable'} = 0;
110     $self->{'authorised_value_category'} = '';
111
112     bless $self, $class;
113     return $self;
114 }
115
116 =head2 fetch
117
118 =over 4
119
120 my $attr_type = C4::Members::AttributeTypes->fetch($code);
121
122 =back
123
124 Fetches an attribute type from the database.  If no
125 type with the given C<$code> exists, returns undef.
126
127 =cut
128
129 sub fetch {
130     my $class = shift;
131     my $code = shift;
132     my $self = {};
133     my $dbh = C4::Context->dbh();
134
135     my $sth = $dbh->prepare_cached("SELECT * FROM borrower_attribute_types WHERE code = ?");
136     $sth->execute($code);
137     my $row = $sth->fetchrow_hashref;
138     $sth->finish();
139     return undef unless defined $row;    
140
141     $self->{'code'}                      = $row->{'code'};
142     $self->{'description'}               = $row->{'description'};
143     $self->{'repeatable'}                = $row->{'repeatable'};
144     $self->{'unique_id'}                 = $row->{'unique_id'};
145     $self->{'opac_display'}              = $row->{'opac_display'};
146     $self->{'password_allowed'}          = $row->{'password_allowed'};
147     $self->{'staff_searchable'}          = $row->{'staff_searchable'};
148     $self->{'authorised_value_category'} = $row->{'authorised_value_category'};
149
150     bless $self, $class;
151     return $self;
152 }
153
154 =head2 store
155
156 =over 4
157
158 $attr_type->store();
159
160 =back
161
162 Stores attribute type in the database.  If the type
163 previously retrieved from the database via the fetch()
164 method, the DB representation of the type is replaced.
165
166 =cut
167
168 sub store {
169     my $self = shift;
170
171     my $dbh = C4::Context->dbh;
172     my $sth;
173     my $existing = __PACKAGE__->fetch($self->{'code'});
174     if (defined $existing) {
175         $sth = $dbh->prepare_cached("UPDATE borrower_attribute_types
176                                      SET description = ?,
177                                          repeatable = ?,
178                                          unique_id = ?,
179                                          opac_display = ?,
180                                          password_allowed = ?,
181                                          staff_searchable = ?,
182                                          authorised_value_category = ?
183                                      WHERE code = ?");
184     } else {
185         $sth = $dbh->prepare_cached("INSERT INTO borrower_attribute_types 
186                                         (description, repeatable, unique_id, opac_display, password_allowed,
187                                          staff_searchable, authorised_value_category, code)
188                                         VALUES (?, ?, ?, ?, ?,
189                                                 ?, ?, ?)");
190     }
191     $sth->bind_param(1, $self->{'description'});
192     $sth->bind_param(2, $self->{'repeatable'});
193     $sth->bind_param(3, $self->{'unique_id'});
194     $sth->bind_param(4, $self->{'opac_display'});
195     $sth->bind_param(5, $self->{'password_allowed'});
196     $sth->bind_param(6, $self->{'staff_searchable'});
197     $sth->bind_param(7, $self->{'authorised_value_category'});
198     $sth->bind_param(8, $self->{'code'});
199     $sth->execute;
200
201 }
202
203 =head2 code
204
205 =over 4
206
207 my $code = $attr_type->code();
208 $attr_type->code($code);
209
210 =back
211
212 Accessor.  Note that the code is immutable once
213 a type is created or fetched from the database.
214
215 =cut
216
217 sub code {
218     my $self = shift;
219     return $self->{'code'};
220 }
221
222 =head2 description
223
224 =over 4
225
226 my $description = $attr_type->description();
227 $attr_type->description($description);
228
229 =back
230
231 Accessor.
232
233 =cut
234
235 sub description {
236     my $self = shift;
237     @_ ? $self->{'description'} = shift : $self->{'description'};
238 }
239
240 =head2 repeatable
241
242 =over 4
243
244 my $repeatable = $attr_type->repeatable();
245 $attr_type->repeatable($repeatable);
246
247 =back
248
249 Accessor.  The C<$repeatable> argument
250 is interpreted as a Perl boolean.
251
252 =cut
253
254 sub repeatable {
255     my $self = shift;
256     @_ ? $self->{'repeatable'} = ((shift) ? 1 : 0) : $self->{'repeatable'};
257 }
258
259 =head2 unique_id
260
261 =over 4
262
263 my $unique_id = $attr_type->unique_id();
264 $attr_type->unique_id($unique_id);
265
266 =back
267
268 Accessor.  The C<$unique_id> argument
269 is interpreted as a Perl boolean.
270
271 =cut
272
273 sub unique_id {
274     my $self = shift;
275     @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'};
276 }
277 =head2 opac_display
278
279 =over 4
280
281 my $opac_display = $attr_type->opac_display();
282 $attr_type->opac_display($opac_display);
283
284 =back
285
286 Accessor.  The C<$opac_display> argument
287 is interpreted as a Perl boolean.
288
289 =cut
290
291 sub opac_display {
292     my $self = shift;
293     @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'};
294 }
295 =head2 password_allowed
296
297 =over 4
298
299 my $password_allowed = $attr_type->password_allowed();
300 $attr_type->password_allowed($password_allowed);
301
302 =back
303
304 Accessor.  The C<$password_allowed> argument
305 is interpreted as a Perl boolean.
306
307 =cut
308
309 sub password_allowed {
310     my $self = shift;
311     @_ ? $self->{'password_allowed'} = ((shift) ? 1 : 0) : $self->{'password_allowed'};
312 }
313 =head2 staff_searchable
314
315 =over 4
316
317 my $staff_searchable = $attr_type->staff_searchable();
318 $attr_type->staff_searchable($staff_searchable);
319
320 =back
321
322 Accessor.  The C<$staff_searchable> argument
323 is interpreted as a Perl boolean.
324
325 =cut
326
327 sub staff_searchable {
328     my $self = shift;
329     @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'};
330 }
331
332 =head2 authorised_value_category
333
334 =over 4
335
336 my $authorised_value_category = $attr_type->authorised_value_category();
337 $attr_type->authorised_value_category($authorised_value_category);
338
339 =back
340
341 Accessor.
342
343 =cut
344
345 sub authorised_value_category {
346     my $self = shift;
347     @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'};
348 }
349
350 =head2 delete
351
352 =over 4
353
354 $attr_type->delete();
355 C4::Members::AttributeTypes->delete($code);
356
357 =back
358
359 Delete an attribute type from the database.  The attribute
360 type may be specified either by an object or by a code.
361
362 =cut
363
364 sub delete {
365     my $arg = shift;
366     my $code;
367     if (ref($arg) eq __PACKAGE__) {
368         $code = $arg->{'code'};
369     } else {
370         $code = shift;
371     }
372
373     my $dbh = C4::Context->dbh;
374     my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?");
375     $sth->execute($code);
376 }
377
378 =head2 num_patrons
379
380 =over 4
381
382 my $count = $attr_type->num_patrons();
383
384 =back
385
386 Returns the number of patron records that use
387 this attribute type.
388
389 =cut
390
391 sub num_patrons {
392     my $self = shift;
393
394     my $dbh = C4::Context->dbh;
395     my $sth = $dbh->prepare_cached("SELECT COUNT(DISTINCT borrowernumber)
396                                     FROM borrower_attributes
397                                     WHERE code = ?");
398     $sth->execute($self->{code});
399     my ($count) = $sth->fetchrow_array;
400     $sth->finish;
401     return $count;
402 }
403
404 =head2 get_patrons
405
406 =over 4
407
408 my @borrowernumbers = $attr_type->get_patrons($attribute);
409
410 =back
411
412 Returns the borrowernumber of the patron records that
413 have an attribute with the specifie value.
414
415 =cut
416
417 sub get_patrons {
418     my $self = shift;
419     my $value = shift;
420
421     my $dbh = C4::Context->dbh;
422     my $sth = $dbh->prepare_cached("SELECT DISTINCT borrowernumber
423                                     FROM borrower_attributes
424                                     WHERE code = ?
425                                     AND   attribute = ?");
426     $sth->execute($self->{code}, $value);
427     my @results;
428     while (my ($borrowernumber) = $sth->fetchrow_array) {
429         push @results, $borrowernumber;
430     } 
431     return @results;
432 }
433
434 =head1 AUTHOR
435
436 Koha Development Team <info@koha.org>
437
438 Galen Charlton <galen.charlton@liblime.com>
439
440 =cut
441
442 1;