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($all_fields);
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 at least the following fields:
74 If $all_fields is true, then each hashref also contains the other fields from borrower_attribute_types.
78 sub GetAttributeTypes {
79 my $all = @_ ? shift : 0;
80 my $select = $all ? '*' : 'code, description';
81 my $dbh = C4::Context->dbh;
82 my $sth = $dbh->prepare("SELECT $select FROM borrower_attribute_types ORDER by code");
84 my $results = $sth->fetchall_arrayref({});
88 sub GetAttributeTypes_hashref {
89 my %hash = map {$_->{code} => $_} GetAttributeTypes(@_);
97 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
101 Create a new attribute type.
109 $self->{'code'} = shift;
110 $self->{'description'} = shift;
111 $self->{'repeatable'} = 0;
112 $self->{'unique_id'} = 0;
113 $self->{'opac_display'} = 0;
114 $self->{'password_allowed'} = 0;
115 $self->{'staff_searchable'} = 0;
116 $self->{'authorised_value_category'} = '';
126 my $attr_type = C4::Members::AttributeTypes->fetch($code);
130 Fetches an attribute type from the database. If no
131 type with the given C<$code> exists, returns undef.
139 my $dbh = C4::Context->dbh();
141 my $sth = $dbh->prepare_cached("SELECT * FROM borrower_attribute_types WHERE code = ?");
142 $sth->execute($code);
143 my $row = $sth->fetchrow_hashref;
145 return undef unless defined $row;
147 $self->{'code'} = $row->{'code'};
148 $self->{'description'} = $row->{'description'};
149 $self->{'repeatable'} = $row->{'repeatable'};
150 $self->{'unique_id'} = $row->{'unique_id'};
151 $self->{'opac_display'} = $row->{'opac_display'};
152 $self->{'password_allowed'} = $row->{'password_allowed'};
153 $self->{'staff_searchable'} = $row->{'staff_searchable'};
154 $self->{'authorised_value_category'} = $row->{'authorised_value_category'};
168 Stores attribute type in the database. If the type
169 previously retrieved from the database via the fetch()
170 method, the DB representation of the type is replaced.
177 my $dbh = C4::Context->dbh;
179 my $existing = __PACKAGE__->fetch($self->{'code'});
180 if (defined $existing) {
181 $sth = $dbh->prepare_cached("UPDATE borrower_attribute_types
186 password_allowed = ?,
187 staff_searchable = ?,
188 authorised_value_category = ?
191 $sth = $dbh->prepare_cached("INSERT INTO borrower_attribute_types
192 (description, repeatable, unique_id, opac_display, password_allowed,
193 staff_searchable, authorised_value_category, code)
194 VALUES (?, ?, ?, ?, ?,
197 $sth->bind_param(1, $self->{'description'});
198 $sth->bind_param(2, $self->{'repeatable'});
199 $sth->bind_param(3, $self->{'unique_id'});
200 $sth->bind_param(4, $self->{'opac_display'});
201 $sth->bind_param(5, $self->{'password_allowed'});
202 $sth->bind_param(6, $self->{'staff_searchable'});
203 $sth->bind_param(7, $self->{'authorised_value_category'});
204 $sth->bind_param(8, $self->{'code'});
213 my $code = $attr_type->code();
214 $attr_type->code($code);
218 Accessor. Note that the code is immutable once
219 a type is created or fetched from the database.
225 return $self->{'code'};
232 my $description = $attr_type->description();
233 $attr_type->description($description);
243 @_ ? $self->{'description'} = shift : $self->{'description'};
250 my $repeatable = $attr_type->repeatable();
251 $attr_type->repeatable($repeatable);
255 Accessor. The C<$repeatable> argument
256 is interpreted as a Perl boolean.
262 @_ ? $self->{'repeatable'} = ((shift) ? 1 : 0) : $self->{'repeatable'};
269 my $unique_id = $attr_type->unique_id();
270 $attr_type->unique_id($unique_id);
274 Accessor. The C<$unique_id> argument
275 is interpreted as a Perl boolean.
281 @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'};
287 my $opac_display = $attr_type->opac_display();
288 $attr_type->opac_display($opac_display);
292 Accessor. The C<$opac_display> argument
293 is interpreted as a Perl boolean.
299 @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'};
301 =head2 password_allowed
305 my $password_allowed = $attr_type->password_allowed();
306 $attr_type->password_allowed($password_allowed);
310 Accessor. The C<$password_allowed> argument
311 is interpreted as a Perl boolean.
315 sub password_allowed {
317 @_ ? $self->{'password_allowed'} = ((shift) ? 1 : 0) : $self->{'password_allowed'};
319 =head2 staff_searchable
323 my $staff_searchable = $attr_type->staff_searchable();
324 $attr_type->staff_searchable($staff_searchable);
328 Accessor. The C<$staff_searchable> argument
329 is interpreted as a Perl boolean.
333 sub staff_searchable {
335 @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'};
338 =head2 authorised_value_category
342 my $authorised_value_category = $attr_type->authorised_value_category();
343 $attr_type->authorised_value_category($authorised_value_category);
351 sub authorised_value_category {
353 @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'};
360 $attr_type->delete();
361 C4::Members::AttributeTypes->delete($code);
365 Delete an attribute type from the database. The attribute
366 type may be specified either by an object or by a code.
373 if (ref($arg) eq __PACKAGE__) {
374 $code = $arg->{'code'};
379 my $dbh = C4::Context->dbh;
380 my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?");
381 $sth->execute($code);
388 my $count = $attr_type->num_patrons();
392 Returns the number of patron records that use
400 my $dbh = C4::Context->dbh;
401 my $sth = $dbh->prepare_cached("SELECT COUNT(DISTINCT borrowernumber)
402 FROM borrower_attributes
404 $sth->execute($self->{code});
405 my ($count) = $sth->fetchrow_array;
414 my @borrowernumbers = $attr_type->get_patrons($attribute);
418 Returns the borrowernumber of the patron records that
419 have an attribute with the specifie value.
427 my $dbh = C4::Context->dbh;
428 my $sth = $dbh->prepare_cached("SELECT DISTINCT borrowernumber
429 FROM borrower_attributes
432 $sth->execute($self->{code}, $value);
434 while (my ($borrowernumber) = $sth->fetchrow_array) {
435 push @results, $borrowernumber;
442 Koha Development Team <info@koha.org>
444 Galen Charlton <galen.charlton@liblime.com>