Bug 18433: Remember item search results selected rows in session storage
[koha.git] / C4 / Members / AttributeTypes.pm
blob948acbe3a9cd3e8a49a4e718947d63e7bb713732
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>.
20 use Modern::Perl;
21 use C4::Context;
25 =head1 NAME
27 C4::Members::AttributeTypes - mananage extended patron attribute types
29 =head1 SYNOPSIS
31 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
33 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
34 $attr_type->code($code);
35 $attr_type->description($description);
36 $attr_type->repeatable($repeatable);
37 $attr_type->unique_id($unique_id);
38 $attr_type->opac_display($opac_display);
39 $attr_type->opac_editable($opac_editable);
40 $attr_type->staff_searchable($staff_searchable);
41 $attr_type->authorised_value_category($authorised_value_category);
42 $attr_type->store();
43 $attr_type->delete();
45 my $attr_type = C4::Members::AttributeTypes->fetch($code);
46 $attr_type = C4::Members::AttributeTypes->delete($code);
48 =head1 FUNCTIONS
50 =head2 GetAttributeTypes
52 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes($all_fields);
54 Returns an array of hashrefs of each attribute type defined
55 in the database. The array is sorted by code. Each hashref contains
56 at least the following fields:
58 - code
59 - description
61 If $all_fields is true, then each hashref also contains the other fields from borrower_attribute_types.
63 =cut
65 sub GetAttributeTypes {
66 my $all = @_ ? shift : 0;
67 my $no_branch_limit = @_ ? shift : 0;
68 my $branch_limit = $no_branch_limit
69 ? 0
70 : C4::Context->userenv ? C4::Context->userenv->{"branch"} : 0;
71 my $select = $all ? '*' : 'DISTINCT(code), description, class';
73 my $dbh = C4::Context->dbh;
74 my $query = "SELECT $select FROM borrower_attribute_types";
75 $query .= qq{
76 LEFT JOIN borrower_attribute_types_branches ON bat_code = code
77 WHERE b_branchcode = ? OR b_branchcode IS NULL
78 } if $branch_limit;
79 $query .= " ORDER BY code";
80 my $sth = $dbh->prepare($query);
81 $sth->execute( $branch_limit ? $branch_limit : () );
82 my $results = $sth->fetchall_arrayref({});
83 $sth->finish;
84 return @$results;
87 sub GetAttributeTypes_hashref {
88 my %hash = map {$_->{code} => $_} GetAttributeTypes(@_);
89 return \%hash;
92 =head1 METHODS
94 my $attr_type = C4::Members::AttributeTypes->new($code, $description);
96 Create a new attribute type.
98 =cut
100 sub new {
101 my $class = shift;
102 my $self = {};
104 $self->{'code'} = shift;
105 $self->{'description'} = shift;
106 $self->{'repeatable'} = 0;
107 $self->{'unique_id'} = 0;
108 $self->{'opac_display'} = 0;
109 $self->{'opac_editable'} = 0;
110 $self->{'staff_searchable'} = 0;
111 $self->{'display_checkout'} = 0;
112 $self->{'authorised_value_category'} = '';
113 $self->{'category_code'} = '';
114 $self->{'category_description'} = '';
115 $self->{'class'} = '';
117 bless $self, $class;
118 return $self;
121 =head2 fetch
123 my $attr_type = C4::Members::AttributeTypes->fetch($code);
125 Fetches an attribute type from the database. If no
126 type with the given C<$code> exists, returns undef.
128 =cut
130 sub fetch {
131 my $class = shift;
132 my $code = shift;
133 my $self = {};
134 my $dbh = C4::Context->dbh();
136 my $sth = $dbh->prepare_cached("
137 SELECT borrower_attribute_types.*, categories.description AS category_description
138 FROM borrower_attribute_types
139 LEFT JOIN categories ON borrower_attribute_types.category_code=categories.categorycode
140 WHERE code =?");
141 $sth->execute($code);
142 my $row = $sth->fetchrow_hashref;
143 $sth->finish();
144 return unless defined $row;
146 $self->{'code'} = $row->{'code'};
147 $self->{'description'} = $row->{'description'};
148 $self->{'repeatable'} = $row->{'repeatable'};
149 $self->{'unique_id'} = $row->{'unique_id'};
150 $self->{'opac_display'} = $row->{'opac_display'};
151 $self->{'opac_editable'} = $row->{'opac_editable'};
152 $self->{'staff_searchable'} = $row->{'staff_searchable'};
153 $self->{'display_checkout'} = $row->{'display_checkout'};
154 $self->{'authorised_value_category'} = $row->{'authorised_value_category'};
155 $self->{'category_code'} = $row->{'category_code'};
156 $self->{'category_description'} = $row->{'category_description'};
157 $self->{'class'} = $row->{'class'};
159 $sth = $dbh->prepare("SELECT branchcode, branchname FROM borrower_attribute_types_branches, branches WHERE b_branchcode = branchcode AND bat_code = ?;");
160 $sth->execute( $code );
161 while ( my $data = $sth->fetchrow_hashref ) {
162 push @{ $self->{branches} }, $data;
164 $sth->finish();
166 bless $self, $class;
167 return $self;
170 =head2 store
172 $attr_type->store();
174 Stores attribute type in the database. If the type
175 previously retrieved from the database via the fetch()
176 method, the DB representation of the type is replaced.
178 =cut
180 sub store {
181 my $self = shift;
183 my $dbh = C4::Context->dbh;
184 my $sth;
185 my $existing = __PACKAGE__->fetch($self->{'code'});
186 if (defined $existing) {
187 $sth = $dbh->prepare_cached("UPDATE borrower_attribute_types
188 SET description = ?,
189 repeatable = ?,
190 unique_id = ?,
191 opac_display = ?,
192 opac_editable = ?,
193 staff_searchable = ?,
194 authorised_value_category = ?,
195 display_checkout = ?,
196 category_code = ?,
197 class = ?
198 WHERE code = ?");
199 } else {
200 $sth = $dbh->prepare_cached("INSERT INTO borrower_attribute_types
201 ( description,
202 repeatable,
203 unique_id,
204 opac_display,
205 opac_editable,
206 staff_searchable,
207 authorised_value_category,
208 display_checkout,
209 category_code,
210 class,
211 code
213 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
216 $sth->execute(
217 $self->{'description'},
218 $self->{'repeatable'},
219 $self->{'unique_id'},
220 $self->{'opac_display'},
221 $self->{'opac_editable'},
222 $self->{'staff_searchable'} || 0,
223 $self->{'authorised_value_category'},
224 $self->{'display_checkout'},
225 $self->{'category_code'} || undef,
226 $self->{'class'},
227 $self->{'code'}
230 if ( defined $$self{branches} ) {
231 $sth = $dbh->prepare("DELETE FROM borrower_attribute_types_branches WHERE bat_code = ?");
232 $sth->execute( $$self{code} );
233 $sth = $dbh->prepare(
234 "INSERT INTO borrower_attribute_types_branches
235 ( bat_code, b_branchcode )
236 VALUES ( ?, ? )"
238 for my $branchcode ( @{$$self{branches}} ) {
239 next if not $branchcode;
240 $sth->bind_param( 1, $$self{code} );
241 $sth->bind_param( 2, $branchcode );
242 $sth->execute;
245 $sth->finish;
248 =head2 code
250 my $code = $attr_type->code();
251 $attr_type->code($code);
253 Accessor. Note that the code is immutable once
254 a type is created or fetched from the database.
256 =cut
258 sub code {
259 my $self = shift;
260 return $self->{'code'};
263 =head2 description
265 my $description = $attr_type->description();
266 $attr_type->description($description);
268 Accessor.
270 =cut
272 sub description {
273 my $self = shift;
274 @_ ? $self->{'description'} = shift : $self->{'description'};
277 =head2 branches
279 my $branches = $attr_type->branches();
280 $attr_type->branches($branches);
282 Accessor.
284 =cut
286 sub branches {
287 my $self = shift;
288 @_ ? $self->{branches} = shift : $self->{branches};
291 =head2 repeatable
293 my $repeatable = $attr_type->repeatable();
294 $attr_type->repeatable($repeatable);
296 Accessor. The C<$repeatable> argument
297 is interpreted as a Perl boolean.
299 =cut
301 sub repeatable {
302 my $self = shift;
303 @_ ? $self->{'repeatable'} = ((shift) ? 1 : 0) : $self->{'repeatable'};
306 =head2 unique_id
308 my $unique_id = $attr_type->unique_id();
309 $attr_type->unique_id($unique_id);
311 Accessor. The C<$unique_id> argument
312 is interpreted as a Perl boolean.
314 =cut
316 sub unique_id {
317 my $self = shift;
318 @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'};
321 =head2 opac_display
323 my $opac_display = $attr_type->opac_display();
324 $attr_type->opac_display($opac_display);
326 Accessor. The C<$opac_display> argument
327 is interpreted as a Perl boolean.
329 =cut
331 sub opac_display {
332 my $self = shift;
333 @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'};
336 =head2 opac_editable
338 my $opac_editable = $attr_type->opac_editable();
339 $attr_type->opac_editable($opac_editable);
341 Accessor. The C<$opac_editable> argument
342 is interpreted as a Perl boolean.
344 =cut
346 sub opac_editable {
347 my $self = shift;
348 @_ ? $self->{'opac_editable'} = ((shift) ? 1 : 0) : $self->{'opac_editable'};
351 =head2 staff_searchable
353 my $staff_searchable = $attr_type->staff_searchable();
354 $attr_type->staff_searchable($staff_searchable);
356 Accessor. The C<$staff_searchable> argument
357 is interpreted as a Perl boolean.
359 =cut
361 sub staff_searchable {
362 my $self = shift;
363 @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'};
366 =head2 display_checkout
368 my $display_checkout = $attr_type->display_checkout();
369 $attr_type->display_checkout($display_checkout);
371 Accessor. The C<$display_checkout> argument
372 is interpreted as a Perl boolean.
374 =cut
376 sub display_checkout {
377 my $self = shift;
378 @_ ? $self->{'display_checkout'} = ((shift) ? 1 : 0) : $self->{'display_checkout'};
381 =head2 authorised_value_category
383 my $authorised_value_category = $attr_type->authorised_value_category();
384 $attr_type->authorised_value_category($authorised_value_category);
386 Accessor.
388 =cut
390 sub authorised_value_category {
391 my $self = shift;
392 @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'};
395 =head2 category_code
397 my $category_code = $attr_type->category_code();
398 $attr_type->category_code($category_code);
400 Accessor.
402 =cut
404 sub category_code {
405 my $self = shift;
406 @_ ? $self->{'category_code'} = shift : $self->{'category_code'};
409 =head2 category_description
411 my $category_description = $attr_type->category_description();
412 $attr_type->category_description($category_description);
414 Accessor.
416 =cut
418 sub category_description {
419 my $self = shift;
420 @_ ? $self->{'category_description'} = shift : $self->{'category_description'};
423 =head2 class
425 my $class = $attr_type->class();
426 $attr_type->class($class);
428 Accessor.
430 =cut
432 sub class {
433 my $self = shift;
434 @_ ? $self->{'class'} = shift : $self->{'class'};
438 =head2 delete
440 $attr_type->delete();
441 C4::Members::AttributeTypes->delete($code);
443 Delete an attribute type from the database. The attribute
444 type may be specified either by an object or by a code.
446 =cut
448 sub delete {
449 my $arg = shift;
450 my $code;
451 if (ref($arg) eq __PACKAGE__) {
452 $code = $arg->{'code'};
453 } else {
454 $code = shift;
457 my $dbh = C4::Context->dbh;
458 my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?");
459 $sth->execute($code);
460 $sth->finish;
463 =head2 num_patrons
465 my $count = $attr_type->num_patrons();
467 Returns the number of patron records that use
468 this attribute type.
470 =cut
472 sub num_patrons {
473 my $self = shift;
475 my $dbh = C4::Context->dbh;
476 my $sth = $dbh->prepare_cached("SELECT COUNT(DISTINCT borrowernumber)
477 FROM borrower_attributes
478 WHERE code = ?");
479 $sth->execute($self->{code});
480 my ($count) = $sth->fetchrow_array;
481 $sth->finish;
482 return $count;
485 =head2 get_patrons
487 my @borrowernumbers = $attr_type->get_patrons($attribute);
489 Returns the borrowernumber of the patron records that
490 have an attribute with the specifie value.
492 =cut
494 sub get_patrons {
495 my $self = shift;
496 my $value = shift;
498 my $dbh = C4::Context->dbh;
499 my $sth = $dbh->prepare_cached("SELECT DISTINCT borrowernumber
500 FROM borrower_attributes
501 WHERE code = ?
502 AND attribute = ?");
503 $sth->execute($self->{code}, $value);
504 my @results;
505 while (my ($borrowernumber) = $sth->fetchrow_array) {
506 push @results, $borrowernumber;
508 return @results;
511 =head1 AUTHOR
513 Koha Development Team <http://koha-community.org/>
515 Galen Charlton <galen.charlton@liblime.com>
517 =cut