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>.
27 C4::Members::AttributeTypes - mananage extended patron attribute types
31 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
32 $attr_type->code($code);
33 $attr_type->description($description);
34 $attr_type->repeatable($repeatable);
35 $attr_type->unique_id($unique_id);
36 $attr_type->opac_display($opac_display);
37 $attr_type->opac_editable($opac_editable);
38 $attr_type->staff_searchable($staff_searchable);
39 $attr_type->authorised_value_category($authorised_value_category);
43 my $attr_type = C4::Members::AttributeTypes->fetch($code);
44 $attr_type = C4::Members::AttributeTypes->delete($code);
50 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
52 Create a new attribute type.
60 $self->{'code'} = shift;
61 $self->{'description'} = shift;
62 $self->{'repeatable'} = 0;
63 $self->{'unique_id'} = 0;
64 $self->{'opac_display'} = 0;
65 $self->{'opac_editable'} = 0;
66 $self->{'staff_searchable'} = 0;
67 $self->{'display_checkout'} = 0;
68 $self->{'authorised_value_category'} = '';
69 $self->{'category_code'} = '';
70 $self->{'category_description'} = '';
71 $self->{'class'} = '';
79 my $attr_type = C4::Members::AttributeTypes->fetch($code);
81 Fetches an attribute type from the database. If no
82 type with the given C<$code> exists, returns undef.
90 my $dbh = C4::Context->dbh();
92 my $sth = $dbh->prepare_cached("
93 SELECT borrower_attribute_types.*, categories.description AS category_description
94 FROM borrower_attribute_types
95 LEFT JOIN categories ON borrower_attribute_types.category_code=categories.categorycode
98 my $row = $sth->fetchrow_hashref;
100 return unless defined $row;
102 $self->{'code'} = $row->{'code'};
103 $self->{'description'} = $row->{'description'};
104 $self->{'repeatable'} = $row->{'repeatable'};
105 $self->{'unique_id'} = $row->{'unique_id'};
106 $self->{'opac_display'} = $row->{'opac_display'};
107 $self->{'opac_editable'} = $row->{'opac_editable'};
108 $self->{'staff_searchable'} = $row->{'staff_searchable'};
109 $self->{'display_checkout'} = $row->{'display_checkout'};
110 $self->{'authorised_value_category'} = $row->{'authorised_value_category'};
111 $self->{'category_code'} = $row->{'category_code'};
112 $self->{'category_description'} = $row->{'category_description'};
113 $self->{'class'} = $row->{'class'};
115 $sth = $dbh->prepare("SELECT branchcode, branchname FROM borrower_attribute_types_branches, branches WHERE b_branchcode = branchcode AND bat_code = ?;");
116 $sth->execute( $code );
117 while ( my $data = $sth->fetchrow_hashref ) {
118 push @{ $self->{branches} }, $data;
130 Stores attribute type in the database. If the type
131 previously retrieved from the database via the fetch()
132 method, the DB representation of the type is replaced.
139 my $dbh = C4::Context->dbh;
141 my $existing = __PACKAGE__->fetch($self->{'code'});
142 if (defined $existing) {
143 $sth = $dbh->prepare_cached("UPDATE borrower_attribute_types
149 staff_searchable = ?,
150 authorised_value_category = ?,
151 display_checkout = ?,
156 $sth = $dbh->prepare_cached("INSERT INTO borrower_attribute_types
163 authorised_value_category,
169 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
173 $self->{'description'},
174 $self->{'repeatable'},
175 $self->{'unique_id'},
176 $self->{'opac_display'},
177 $self->{'opac_editable'},
178 $self->{'staff_searchable'} || 0,
179 $self->{'authorised_value_category'},
180 $self->{'display_checkout'},
181 $self->{'category_code'} || undef,
186 if ( defined $$self{branches} ) {
187 $sth = $dbh->prepare("DELETE FROM borrower_attribute_types_branches WHERE bat_code = ?");
188 $sth->execute( $$self{code} );
189 $sth = $dbh->prepare(
190 "INSERT INTO borrower_attribute_types_branches
191 ( bat_code, b_branchcode )
194 for my $branchcode ( @{$$self{branches}} ) {
195 next if not $branchcode;
196 $sth->bind_param( 1, $$self{code} );
197 $sth->bind_param( 2, $branchcode );
206 my $code = $attr_type->code();
207 $attr_type->code($code);
209 Accessor. Note that the code is immutable once
210 a type is created or fetched from the database.
216 return $self->{'code'};
221 my $description = $attr_type->description();
222 $attr_type->description($description);
230 @_ ? $self->{'description'} = shift : $self->{'description'};
235 my $branches = $attr_type->branches();
236 $attr_type->branches($branches);
244 @_ ? $self->{branches} = shift : $self->{branches};
249 my $repeatable = $attr_type->repeatable();
250 $attr_type->repeatable($repeatable);
252 Accessor. The C<$repeatable> argument
253 is interpreted as a Perl boolean.
259 @_ ? $self->{'repeatable'} = ((shift) ? 1 : 0) : $self->{'repeatable'};
264 my $unique_id = $attr_type->unique_id();
265 $attr_type->unique_id($unique_id);
267 Accessor. The C<$unique_id> argument
268 is interpreted as a Perl boolean.
274 @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'};
279 my $opac_display = $attr_type->opac_display();
280 $attr_type->opac_display($opac_display);
282 Accessor. The C<$opac_display> argument
283 is interpreted as a Perl boolean.
289 @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'};
294 my $opac_editable = $attr_type->opac_editable();
295 $attr_type->opac_editable($opac_editable);
297 Accessor. The C<$opac_editable> argument
298 is interpreted as a Perl boolean.
304 @_ ? $self->{'opac_editable'} = ((shift) ? 1 : 0) : $self->{'opac_editable'};
307 =head2 staff_searchable
309 my $staff_searchable = $attr_type->staff_searchable();
310 $attr_type->staff_searchable($staff_searchable);
312 Accessor. The C<$staff_searchable> argument
313 is interpreted as a Perl boolean.
317 sub staff_searchable {
319 @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'};
322 =head2 display_checkout
324 my $display_checkout = $attr_type->display_checkout();
325 $attr_type->display_checkout($display_checkout);
327 Accessor. The C<$display_checkout> argument
328 is interpreted as a Perl boolean.
332 sub display_checkout {
334 @_ ? $self->{'display_checkout'} = ((shift) ? 1 : 0) : $self->{'display_checkout'};
337 =head2 authorised_value_category
339 my $authorised_value_category = $attr_type->authorised_value_category();
340 $attr_type->authorised_value_category($authorised_value_category);
346 sub authorised_value_category {
348 @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'};
353 my $category_code = $attr_type->category_code();
354 $attr_type->category_code($category_code);
362 @_ ? $self->{'category_code'} = shift : $self->{'category_code'};
365 =head2 category_description
367 my $category_description = $attr_type->category_description();
368 $attr_type->category_description($category_description);
374 sub category_description {
376 @_ ? $self->{'category_description'} = shift : $self->{'category_description'};
381 my $class = $attr_type->class();
382 $attr_type->class($class);
390 @_ ? $self->{'class'} = shift : $self->{'class'};
396 $attr_type->delete();
397 C4::Members::AttributeTypes->delete($code);
399 Delete an attribute type from the database. The attribute
400 type may be specified either by an object or by a code.
407 if (ref($arg) eq __PACKAGE__) {
408 $code = $arg->{'code'};
413 my $dbh = C4::Context->dbh;
414 my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?");
415 $sth->execute($code);
421 my $count = $attr_type->num_patrons();
423 Returns the number of patron records that use
431 my $dbh = C4::Context->dbh;
432 my $sth = $dbh->prepare_cached("SELECT COUNT(DISTINCT borrowernumber)
433 FROM borrower_attributes
435 $sth->execute($self->{code});
436 my ($count) = $sth->fetchrow_array;
443 my @borrowernumbers = $attr_type->get_patrons($attribute);
445 Returns the borrowernumber of the patron records that
446 have an attribute with the specifie value.
454 my $dbh = C4::Context->dbh;
455 my $sth = $dbh->prepare_cached("SELECT DISTINCT borrowernumber
456 FROM borrower_attributes
459 $sth->execute($self->{code}, $value);
461 while (my ($borrowernumber) = $sth->fetchrow_array) {
462 push @results, $borrowernumber;
469 Koha Development Team <http://koha-community.org/>
471 Galen Charlton <galen.charlton@liblime.com>