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
8 # 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.
12 # Koha is distributed in the hope that it will be useful, but
13 # 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.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 #use warnings; FIXME - Bug 2505
28 C4::Members::AttributeTypes - mananage extended patron attribute types
32 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
34 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
35 $attr_type->code($code);
36 $attr_type->description($description);
37 $attr_type->repeatable($repeatable);
38 $attr_type->unique_id($unique_id);
39 $attr_type->opac_display($opac_display);
40 $attr_type->opac_editable($opac_editable);
41 $attr_type->staff_searchable($staff_searchable);
42 $attr_type->authorised_value_category($authorised_value_category);
46 my $attr_type = C4::Members::AttributeTypes->fetch($code);
47 $attr_type = C4::Members::AttributeTypes->delete($code);
51 =head2 GetAttributeTypes
53 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes($all_fields);
55 Returns an array of hashrefs of each attribute type defined
56 in the database. The array is sorted by code. Each hashref contains
57 at least the following fields:
62 If $all_fields is true, then each hashref also contains the other fields from borrower_attribute_types.
66 sub GetAttributeTypes {
67 my $all = @_ ? shift : 0;
68 my $no_branch_limit = @_ ? shift : 0;
69 my $branch_limit = $no_branch_limit
71 : C4::Context->userenv ? C4::Context->userenv->{"branch"} : 0;
72 my $select = $all ? '*' : 'DISTINCT(code), description, class';
74 my $dbh = C4::Context->dbh;
75 my $query = "SELECT $select FROM borrower_attribute_types";
77 LEFT JOIN borrower_attribute_types_branches ON bat_code = code
78 WHERE b_branchcode = ? OR b_branchcode IS NULL
80 $query .= " ORDER BY code";
81 my $sth = $dbh->prepare($query);
82 $sth->execute( $branch_limit ? $branch_limit : () );
83 my $results = $sth->fetchall_arrayref({});
88 sub GetAttributeTypes_hashref {
89 my %hash = map {$_->{code} => $_} GetAttributeTypes(@_);
93 =head2 AttributeTypeExists
95 my $have_attr_xyz = C4::Members::AttributeTypes::AttributeTypeExists($code)
97 Returns true if we have attribute type C<$code>
102 sub AttributeTypeExists {
104 my $dbh = C4::Context->dbh;
105 my $exists = $dbh->selectrow_array("SELECT code FROM borrower_attribute_types WHERE code = ?", undef, $code);
111 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
113 Create a new attribute type.
121 $self->{'code'} = shift;
122 $self->{'description'} = shift;
123 $self->{'repeatable'} = 0;
124 $self->{'unique_id'} = 0;
125 $self->{'opac_display'} = 0;
126 $self->{'opac_editable'} = 0;
127 $self->{'staff_searchable'} = 0;
128 $self->{'display_checkout'} = 0;
129 $self->{'authorised_value_category'} = '';
130 $self->{'category_code'} = '';
131 $self->{'category_description'} = '';
132 $self->{'class'} = '';
140 my $attr_type = C4::Members::AttributeTypes->fetch($code);
142 Fetches an attribute type from the database. If no
143 type with the given C<$code> exists, returns undef.
151 my $dbh = C4::Context->dbh();
153 my $sth = $dbh->prepare_cached("
154 SELECT borrower_attribute_types.*, categories.description AS category_description
155 FROM borrower_attribute_types
156 LEFT JOIN categories ON borrower_attribute_types.category_code=categories.categorycode
158 $sth->execute($code);
159 my $row = $sth->fetchrow_hashref;
161 return unless defined $row;
163 $self->{'code'} = $row->{'code'};
164 $self->{'description'} = $row->{'description'};
165 $self->{'repeatable'} = $row->{'repeatable'};
166 $self->{'unique_id'} = $row->{'unique_id'};
167 $self->{'opac_display'} = $row->{'opac_display'};
168 $self->{'opac_editable'} = $row->{'opac_editable'};
169 $self->{'staff_searchable'} = $row->{'staff_searchable'};
170 $self->{'display_checkout'} = $row->{'display_checkout'};
171 $self->{'authorised_value_category'} = $row->{'authorised_value_category'};
172 $self->{'category_code'} = $row->{'category_code'};
173 $self->{'category_description'} = $row->{'category_description'};
174 $self->{'class'} = $row->{'class'};
176 $sth = $dbh->prepare("SELECT branchcode, branchname FROM borrower_attribute_types_branches, branches WHERE b_branchcode = branchcode AND bat_code = ?;");
177 $sth->execute( $code );
178 while ( my $data = $sth->fetchrow_hashref ) {
179 push @{ $self->{branches} }, $data;
191 Stores attribute type in the database. If the type
192 previously retrieved from the database via the fetch()
193 method, the DB representation of the type is replaced.
200 my $dbh = C4::Context->dbh;
202 my $existing = __PACKAGE__->fetch($self->{'code'});
203 if (defined $existing) {
204 $sth = $dbh->prepare_cached("UPDATE borrower_attribute_types
210 staff_searchable = ?,
211 authorised_value_category = ?,
212 display_checkout = ?,
217 $sth = $dbh->prepare_cached("INSERT INTO borrower_attribute_types
224 authorised_value_category,
230 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
234 $self->{'description'},
235 $self->{'repeatable'},
236 $self->{'unique_id'},
237 $self->{'opac_display'},
238 $self->{'opac_editable'},
239 $self->{'staff_searchable'} || 0,
240 $self->{'authorised_value_category'},
241 $self->{'display_checkout'},
242 $self->{'category_code'} || undef,
247 if ( defined $$self{branches} ) {
248 $sth = $dbh->prepare("DELETE FROM borrower_attribute_types_branches WHERE bat_code = ?");
249 $sth->execute( $$self{code} );
250 $sth = $dbh->prepare(
251 "INSERT INTO borrower_attribute_types_branches
252 ( bat_code, b_branchcode )
255 for my $branchcode ( @{$$self{branches}} ) {
256 next if not $branchcode;
257 $sth->bind_param( 1, $$self{code} );
258 $sth->bind_param( 2, $branchcode );
267 my $code = $attr_type->code();
268 $attr_type->code($code);
270 Accessor. Note that the code is immutable once
271 a type is created or fetched from the database.
277 return $self->{'code'};
282 my $description = $attr_type->description();
283 $attr_type->description($description);
291 @_ ? $self->{'description'} = shift : $self->{'description'};
296 my $branches = $attr_type->branches();
297 $attr_type->branches($branches);
305 @_ ? $self->{branches} = shift : $self->{branches};
310 my $repeatable = $attr_type->repeatable();
311 $attr_type->repeatable($repeatable);
313 Accessor. The C<$repeatable> argument
314 is interpreted as a Perl boolean.
320 @_ ? $self->{'repeatable'} = ((shift) ? 1 : 0) : $self->{'repeatable'};
325 my $unique_id = $attr_type->unique_id();
326 $attr_type->unique_id($unique_id);
328 Accessor. The C<$unique_id> argument
329 is interpreted as a Perl boolean.
335 @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'};
340 my $opac_display = $attr_type->opac_display();
341 $attr_type->opac_display($opac_display);
343 Accessor. The C<$opac_display> argument
344 is interpreted as a Perl boolean.
350 @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'};
355 my $opac_editable = $attr_type->opac_editable();
356 $attr_type->opac_editable($opac_editable);
358 Accessor. The C<$opac_editable> argument
359 is interpreted as a Perl boolean.
365 @_ ? $self->{'opac_editable'} = ((shift) ? 1 : 0) : $self->{'opac_editable'};
368 =head2 staff_searchable
370 my $staff_searchable = $attr_type->staff_searchable();
371 $attr_type->staff_searchable($staff_searchable);
373 Accessor. The C<$staff_searchable> argument
374 is interpreted as a Perl boolean.
378 sub staff_searchable {
380 @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'};
383 =head2 display_checkout
385 my $display_checkout = $attr_type->display_checkout();
386 $attr_type->display_checkout($display_checkout);
388 Accessor. The C<$display_checkout> argument
389 is interpreted as a Perl boolean.
393 sub display_checkout {
395 @_ ? $self->{'display_checkout'} = ((shift) ? 1 : 0) : $self->{'display_checkout'};
398 =head2 authorised_value_category
400 my $authorised_value_category = $attr_type->authorised_value_category();
401 $attr_type->authorised_value_category($authorised_value_category);
407 sub authorised_value_category {
409 @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'};
414 my $category_code = $attr_type->category_code();
415 $attr_type->category_code($category_code);
423 @_ ? $self->{'category_code'} = shift : $self->{'category_code'};
426 =head2 category_description
428 my $category_description = $attr_type->category_description();
429 $attr_type->category_description($category_description);
435 sub category_description {
437 @_ ? $self->{'category_description'} = shift : $self->{'category_description'};
442 my $class = $attr_type->class();
443 $attr_type->class($class);
451 @_ ? $self->{'class'} = shift : $self->{'class'};
457 $attr_type->delete();
458 C4::Members::AttributeTypes->delete($code);
460 Delete an attribute type from the database. The attribute
461 type may be specified either by an object or by a code.
468 if (ref($arg) eq __PACKAGE__) {
469 $code = $arg->{'code'};
474 my $dbh = C4::Context->dbh;
475 my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?");
476 $sth->execute($code);
482 my $count = $attr_type->num_patrons();
484 Returns the number of patron records that use
492 my $dbh = C4::Context->dbh;
493 my $sth = $dbh->prepare_cached("SELECT COUNT(DISTINCT borrowernumber)
494 FROM borrower_attributes
496 $sth->execute($self->{code});
497 my ($count) = $sth->fetchrow_array;
504 my @borrowernumbers = $attr_type->get_patrons($attribute);
506 Returns the borrowernumber of the patron records that
507 have an attribute with the specifie value.
515 my $dbh = C4::Context->dbh;
516 my $sth = $dbh->prepare_cached("SELECT DISTINCT borrowernumber
517 FROM borrower_attributes
520 $sth->execute($self->{code}, $value);
522 while (my ($borrowernumber) = $sth->fetchrow_array) {
523 push @results, $borrowernumber;
530 Koha Development Team <http://koha-community.org/>
532 Galen Charlton <galen.charlton@liblime.com>