1 package C4::Members::AttributeTypes;
3 # Copyright (C) 2008 LibLime
5 # This file is part of Koha.
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
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.
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
23 use vars qw($VERSION);
26 # set the version for version checking
32 C4::Members::AttributeTypes - mananage extended patron attribute types
38 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
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);
52 my $attr_type = C4::Members::AttributeTypes->fetch($code);
53 $attr_type = C4::Members::AttributeTypes->delete($code);
59 =head2 GetAttributeTypes
63 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
67 Returns an array of hashrefs of each attribute type defined
68 in the database. The array is sorted by code. Each hashref contains
76 sub GetAttributeTypes {
77 my $dbh = C4::Context->dbh;
78 my $sth = $dbh->prepare('SELECT code, description FROM borrower_attribute_types ORDER by code');
81 while (my $row = $sth->fetchrow_hashref) {
91 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
95 Create a new attribute type.
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'} = '';
120 my $attr_type = C4::Members::AttributeTypes->fetch($code);
124 Fetches an attribute type from the database. If no
125 type with the given C<$code> exists, returns undef.
133 my $dbh = C4::Context->dbh();
135 my $sth = $dbh->prepare_cached("SELECT * FROM borrower_attribute_types WHERE code = ?");
136 $sth->execute($code);
137 my $row = $sth->fetchrow_hashref;
139 return undef unless defined $row;
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'};
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.
171 my $dbh = C4::Context->dbh;
173 my $existing = __PACKAGE__->fetch($self->{'code'});
174 if (defined $existing) {
175 $sth = $dbh->prepare_cached("UPDATE borrower_attribute_types
180 password_allowed = ?,
181 staff_searchable = ?,
182 authorised_value_category = ?
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 (?, ?, ?, ?, ?,
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'});
207 my $code = $attr_type->code();
208 $attr_type->code($code);
212 Accessor. Note that the code is immutable once
213 a type is created or fetched from the database.
219 return $self->{'code'};
226 my $description = $attr_type->description();
227 $attr_type->description($description);
237 @_ ? $self->{'description'} = shift : $self->{'description'};
244 my $repeatable = $attr_type->repeatable();
245 $attr_type->repeatable($repeatable);
249 Accessor. The C<$repeatable> argument
250 is interpreted as a Perl boolean.
256 @_ ? $self->{'repeatable'} = ((shift) ? 1 : 0) : $self->{'repeatable'};
263 my $unique_id = $attr_type->unique_id();
264 $attr_type->unique_id($unique_id);
268 Accessor. The C<$unique_id> argument
269 is interpreted as a Perl boolean.
275 @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'};
281 my $opac_display = $attr_type->opac_display();
282 $attr_type->opac_display($opac_display);
286 Accessor. The C<$opac_display> argument
287 is interpreted as a Perl boolean.
293 @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'};
295 =head2 password_allowed
299 my $password_allowed = $attr_type->password_allowed();
300 $attr_type->password_allowed($password_allowed);
304 Accessor. The C<$password_allowed> argument
305 is interpreted as a Perl boolean.
309 sub password_allowed {
311 @_ ? $self->{'password_allowed'} = ((shift) ? 1 : 0) : $self->{'password_allowed'};
313 =head2 staff_searchable
317 my $staff_searchable = $attr_type->staff_searchable();
318 $attr_type->staff_searchable($staff_searchable);
322 Accessor. The C<$staff_searchable> argument
323 is interpreted as a Perl boolean.
327 sub staff_searchable {
329 @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'};
332 =head2 authorised_value_category
336 my $authorised_value_category = $attr_type->authorised_value_category();
337 $attr_type->authorised_value_category($authorised_value_category);
345 sub authorised_value_category {
347 @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'};
354 $attr_type->delete();
355 C4::Members::AttributeTypes->delete($code);
359 Delete an attribute type from the database. The attribute
360 type may be specified either by an object or by a code.
367 if (ref($arg) eq __PACKAGE__) {
368 $code = $arg->{'code'};
373 my $dbh = C4::Context->dbh;
374 my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?");
375 $sth->execute($code);
382 my $count = $attr_type->num_patrons();
386 Returns the number of patron records that use
394 my $dbh = C4::Context->dbh;
395 my $sth = $dbh->prepare_cached("SELECT COUNT(DISTINCT borrowernumber)
396 FROM borrower_attributes
398 $sth->execute($self->{code});
399 my ($count) = $sth->fetchrow_array;
408 my @borrowernumbers = $attr_type->get_patrons($attribute);
412 Returns the borrowernumber of the patron records that
413 have an attribute with the specifie value.
421 my $dbh = C4::Context->dbh;
422 my $sth = $dbh->prepare_cached("SELECT DISTINCT borrowernumber
423 FROM borrower_attributes
426 $sth->execute($self->{code}, $value);
428 while (my ($borrowernumber) = $sth->fetchrow_array) {
429 push @results, $borrowernumber;
436 Koha Development Team <info@koha.org>
438 Galen Charlton <galen.charlton@liblime.com>