| File: | C4/Members/AttributeTypes.pm |
| Coverage: | 8.0% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package C4::Members::AttributeTypes; | |||||
| 2 | ||||||
| 3 | # Copyright (C) 2008 LibLime | |||||
| 4 | # | |||||
| 5 | # This file is part of Koha. | |||||
| 6 | # | |||||
| 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 | |||||
| 10 | # version. | |||||
| 11 | # | |||||
| 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. | |||||
| 15 | # | |||||
| 16 | # You should have received a copy of the GNU General Public License along | |||||
| 17 | # with Koha; if not, write to the Free Software Foundation, Inc., | |||||
| 18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |||||
| 19 | ||||||
| 20 | 28 28 28 | 43537 300 1027 | use strict; | |||
| 21 | #use warnings; FIXME - Bug 2505 | |||||
| 22 | 28 28 28 | 746 268 513 | use C4::Context; | |||
| 23 | ||||||
| 24 | 28 28 28 | 324 210 1547 | use vars qw($VERSION); | |||
| 25 | ||||||
| 26 | BEGIN { | |||||
| 27 | # set the version for version checking | |||||
| 28 | 28 | 40951 | $VERSION = 3.00; | |||
| 29 | } | |||||
| 30 | ||||||
| 31 - 69 | =head1 NAME C4::Members::AttributeTypes - mananage extended patron attribute types =head1 SYNOPSIS my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes(); my $attr_type = C4::Members::AttributeTypes->new($code, $description); $attr_type->code($code); $attr_type->description($description); $attr_type->repeatable($repeatable); $attr_type->unique_id($unique_id); $attr_type->opac_display($opac_display); $attr_type->password_allowed($password_allowed); $attr_type->staff_searchable($staff_searchable); $attr_type->authorised_value_category($authorised_value_category); $attr_type->store(); $attr_type->delete(); my $attr_type = C4::Members::AttributeTypes->fetch($code); $attr_type = C4::Members::AttributeTypes->delete($code); =head1 FUNCTIONS =head2 GetAttributeTypes my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes($all_fields); Returns an array of hashrefs of each attribute type defined in the database. The array is sorted by code. Each hashref contains at least the following fields: - code - description If $all_fields is true, then each hashref also contains the other fields from borrower_attribute_types. =cut | |||||
| 70 | ||||||
| 71 | sub GetAttributeTypes { | |||||
| 72 | 0 | my ($all) = @_; | ||||
| 73 | 0 | my $select = $all ? '*' : 'code, description'; | ||||
| 74 | 0 | my $dbh = C4::Context->dbh; | ||||
| 75 | 0 | my $sth = $dbh->prepare("SELECT $select FROM borrower_attribute_types ORDER by code"); | ||||
| 76 | 0 | $sth->execute(); | ||||
| 77 | 0 | my $results = $sth->fetchall_arrayref({}); | ||||
| 78 | 0 | return @$results; | ||||
| 79 | } | |||||
| 80 | ||||||
| 81 | sub GetAttributeTypes_hashref { | |||||
| 82 | 0 0 | my %hash = map {$_->{code} => $_} GetAttributeTypes(@_); | ||||
| 83 | 0 | return \%hash; | ||||
| 84 | } | |||||
| 85 | ||||||
| 86 - 93 | =head2 AttributeTypeExists my $have_attr_xyz = C4::Members::AttributeTypes::AttributeTypeExists($code) Returns true if we have attribute type C<$code> in the database. =cut | |||||
| 94 | ||||||
| 95 | sub AttributeTypeExists { | |||||
| 96 | 0 | my ($code) = @_; | ||||
| 97 | 0 | my $dbh = C4::Context->dbh; | ||||
| 98 | 0 | my $exists = $dbh->selectrow_array("SELECT code FROM borrower_attribute_types WHERE code = ?", undef, $code); | ||||
| 99 | 0 | return $exists; | ||||
| 100 | } | |||||
| 101 | ||||||
| 102 - 108 | =head1 METHODS my $attr_type = C4::Members::AttributeTypes->new($code, $description); Create a new attribute type. =cut | |||||
| 109 | ||||||
| 110 | sub new { | |||||
| 111 | 0 | my $class = shift; | ||||
| 112 | 0 | my $self = {}; | ||||
| 113 | ||||||
| 114 | 0 | $self->{'code'} = shift; | ||||
| 115 | 0 | $self->{'description'} = shift; | ||||
| 116 | 0 | $self->{'repeatable'} = 0; | ||||
| 117 | 0 | $self->{'unique_id'} = 0; | ||||
| 118 | 0 | $self->{'opac_display'} = 0; | ||||
| 119 | 0 | $self->{'password_allowed'} = 0; | ||||
| 120 | 0 | $self->{'staff_searchable'} = 0; | ||||
| 121 | 0 | $self->{'display_checkout'} = 0; | ||||
| 122 | 0 | $self->{'authorised_value_category'} = ''; | ||||
| 123 | ||||||
| 124 | 0 | bless $self, $class; | ||||
| 125 | 0 | return $self; | ||||
| 126 | } | |||||
| 127 | ||||||
| 128 - 135 | =head2 fetch my $attr_type = C4::Members::AttributeTypes->fetch($code); Fetches an attribute type from the database. If no type with the given C<$code> exists, returns undef. =cut | |||||
| 136 | ||||||
| 137 | sub fetch { | |||||
| 138 | 0 | my $class = shift; | ||||
| 139 | 0 | my $code = shift; | ||||
| 140 | 0 | my $self = {}; | ||||
| 141 | 0 | my $dbh = C4::Context->dbh(); | ||||
| 142 | ||||||
| 143 | 0 | my $sth = $dbh->prepare_cached("SELECT * FROM borrower_attribute_types WHERE code = ?"); | ||||
| 144 | 0 | $sth->execute($code); | ||||
| 145 | 0 | my $row = $sth->fetchrow_hashref; | ||||
| 146 | 0 | $sth->finish(); | ||||
| 147 | 0 | return undef unless defined $row; | ||||
| 148 | ||||||
| 149 | 0 | $self->{'code'} = $row->{'code'}; | ||||
| 150 | 0 | $self->{'description'} = $row->{'description'}; | ||||
| 151 | 0 | $self->{'repeatable'} = $row->{'repeatable'}; | ||||
| 152 | 0 | $self->{'unique_id'} = $row->{'unique_id'}; | ||||
| 153 | 0 | $self->{'opac_display'} = $row->{'opac_display'}; | ||||
| 154 | 0 | $self->{'password_allowed'} = $row->{'password_allowed'}; | ||||
| 155 | 0 | $self->{'staff_searchable'} = $row->{'staff_searchable'}; | ||||
| 156 | 0 | $self->{'display_checkout'} = $row->{'display_checkout'}; | ||||
| 157 | 0 | $self->{'authorised_value_category'} = $row->{'authorised_value_category'}; | ||||
| 158 | ||||||
| 159 | 0 | bless $self, $class; | ||||
| 160 | 0 | return $self; | ||||
| 161 | } | |||||
| 162 | ||||||
| 163 - 171 | =head2 store $attr_type->store(); Stores attribute type in the database. If the type previously retrieved from the database via the fetch() method, the DB representation of the type is replaced. =cut | |||||
| 172 | ||||||
| 173 | sub store { | |||||
| 174 | 0 | my $self = shift; | ||||
| 175 | ||||||
| 176 | 0 | my $dbh = C4::Context->dbh; | ||||
| 177 | 0 | my $sth; | ||||
| 178 | 0 | my $existing = __PACKAGE__->fetch($self->{'code'}); | ||||
| 179 | 0 | if (defined $existing) { | ||||
| 180 | 0 | $sth = $dbh->prepare_cached("UPDATE borrower_attribute_types | ||||
| 181 | SET description = ?, | |||||
| 182 | repeatable = ?, | |||||
| 183 | unique_id = ?, | |||||
| 184 | opac_display = ?, | |||||
| 185 | password_allowed = ?, | |||||
| 186 | staff_searchable = ?, | |||||
| 187 | authorised_value_category = ?, | |||||
| 188 | display_checkout = ? | |||||
| 189 | WHERE code = ?"); | |||||
| 190 | } else { | |||||
| 191 | 0 | $sth = $dbh->prepare_cached("INSERT INTO borrower_attribute_types | ||||
| 192 | (description, repeatable, unique_id, opac_display, password_allowed, | |||||
| 193 | staff_searchable, authorised_value_category, display_checkout, code) | |||||
| 194 | VALUES (?, ?, ?, ?, ?, | |||||
| 195 | ?, ?, ?, ?)"); | |||||
| 196 | } | |||||
| 197 | 0 | $sth->bind_param(1, $self->{'description'}); | ||||
| 198 | 0 | $sth->bind_param(2, $self->{'repeatable'}); | ||||
| 199 | 0 | $sth->bind_param(3, $self->{'unique_id'}); | ||||
| 200 | 0 | $sth->bind_param(4, $self->{'opac_display'}); | ||||
| 201 | 0 | $sth->bind_param(5, $self->{'password_allowed'}); | ||||
| 202 | 0 | $sth->bind_param(6, $self->{'staff_searchable'}); | ||||
| 203 | 0 | $sth->bind_param(7, $self->{'authorised_value_category'}); | ||||
| 204 | 0 | $sth->bind_param(8, $self->{'display_checkout'}); | ||||
| 205 | 0 | $sth->bind_param(9, $self->{'code'}); | ||||
| 206 | 0 | $sth->execute; | ||||
| 207 | ||||||
| 208 | } | |||||
| 209 | ||||||
| 210 - 218 | =head2 code my $code = $attr_type->code(); $attr_type->code($code); Accessor. Note that the code is immutable once a type is created or fetched from the database. =cut | |||||
| 219 | ||||||
| 220 | sub code { | |||||
| 221 | 0 | my $self = shift; | ||||
| 222 | 0 | return $self->{'code'}; | ||||
| 223 | } | |||||
| 224 | ||||||
| 225 - 232 | =head2 description my $description = $attr_type->description(); $attr_type->description($description); Accessor. =cut | |||||
| 233 | ||||||
| 234 | sub description { | |||||
| 235 | 0 | my $self = shift; | ||||
| 236 | 0 | @_ ? $self->{'description'} = shift : $self->{'description'}; | ||||
| 237 | } | |||||
| 238 | ||||||
| 239 - 247 | =head2 repeatable my $repeatable = $attr_type->repeatable(); $attr_type->repeatable($repeatable); Accessor. The C<$repeatable> argument is interpreted as a Perl boolean. =cut | |||||
| 248 | ||||||
| 249 | sub repeatable { | |||||
| 250 | 0 | my $self = shift; | ||||
| 251 | 0 | @_ ? $self->{'repeatable'} = ((shift) ? 1 : 0) : $self->{'repeatable'}; | ||||
| 252 | } | |||||
| 253 | ||||||
| 254 - 262 | =head2 unique_id my $unique_id = $attr_type->unique_id(); $attr_type->unique_id($unique_id); Accessor. The C<$unique_id> argument is interpreted as a Perl boolean. =cut | |||||
| 263 | ||||||
| 264 | sub unique_id { | |||||
| 265 | 0 | my $self = shift; | ||||
| 266 | 0 | @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'}; | ||||
| 267 | } | |||||
| 268 - 276 | =head2 opac_display my $opac_display = $attr_type->opac_display(); $attr_type->opac_display($opac_display); Accessor. The C<$opac_display> argument is interpreted as a Perl boolean. =cut | |||||
| 277 | ||||||
| 278 | sub opac_display { | |||||
| 279 | 0 | my $self = shift; | ||||
| 280 | 0 | @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'}; | ||||
| 281 | } | |||||
| 282 - 290 | =head2 password_allowed my $password_allowed = $attr_type->password_allowed(); $attr_type->password_allowed($password_allowed); Accessor. The C<$password_allowed> argument is interpreted as a Perl boolean. =cut | |||||
| 291 | ||||||
| 292 | sub password_allowed { | |||||
| 293 | 0 | my $self = shift; | ||||
| 294 | 0 | @_ ? $self->{'password_allowed'} = ((shift) ? 1 : 0) : $self->{'password_allowed'}; | ||||
| 295 | } | |||||
| 296 - 304 | =head2 staff_searchable my $staff_searchable = $attr_type->staff_searchable(); $attr_type->staff_searchable($staff_searchable); Accessor. The C<$staff_searchable> argument is interpreted as a Perl boolean. =cut | |||||
| 305 | ||||||
| 306 | sub staff_searchable { | |||||
| 307 | 0 | my $self = shift; | ||||
| 308 | 0 | @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'}; | ||||
| 309 | } | |||||
| 310 | ||||||
| 311 - 323 | =head2 display_checkout =over 4 my $display_checkout = $attr_type->display_checkout(); $attr_type->display_checkout($display_checkout); =back Accessor. The C<$display_checkout> argument is interpreted as a Perl boolean. =cut | |||||
| 324 | ||||||
| 325 | sub display_checkout { | |||||
| 326 | 0 | my $self = shift; | ||||
| 327 | 0 | @_ ? $self->{'display_checkout'} = ((shift) ? 1 : 0) : $self->{'display_checkout'}; | ||||
| 328 | } | |||||
| 329 | ||||||
| 330 - 337 | =head2 authorised_value_category my $authorised_value_category = $attr_type->authorised_value_category(); $attr_type->authorised_value_category($authorised_value_category); Accessor. =cut | |||||
| 338 | ||||||
| 339 | sub authorised_value_category { | |||||
| 340 | 0 | my $self = shift; | ||||
| 341 | 0 | @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'}; | ||||
| 342 | } | |||||
| 343 | ||||||
| 344 - 352 | =head2 delete $attr_type->delete(); C4::Members::AttributeTypes->delete($code); Delete an attribute type from the database. The attribute type may be specified either by an object or by a code. =cut | |||||
| 353 | ||||||
| 354 | sub delete { | |||||
| 355 | 0 | my $arg = shift; | ||||
| 356 | 0 | my $code; | ||||
| 357 | 0 | if (ref($arg) eq __PACKAGE__) { | ||||
| 358 | 0 | $code = $arg->{'code'}; | ||||
| 359 | } else { | |||||
| 360 | 0 | $code = shift; | ||||
| 361 | } | |||||
| 362 | ||||||
| 363 | 0 | my $dbh = C4::Context->dbh; | ||||
| 364 | 0 | my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?"); | ||||
| 365 | 0 | $sth->execute($code); | ||||
| 366 | } | |||||
| 367 | ||||||
| 368 - 375 | =head2 num_patrons my $count = $attr_type->num_patrons(); Returns the number of patron records that use this attribute type. =cut | |||||
| 376 | ||||||
| 377 | sub num_patrons { | |||||
| 378 | 0 | my $self = shift; | ||||
| 379 | ||||||
| 380 | 0 | my $dbh = C4::Context->dbh; | ||||
| 381 | 0 | my $sth = $dbh->prepare_cached("SELECT COUNT(DISTINCT borrowernumber) | ||||
| 382 | FROM borrower_attributes | |||||
| 383 | WHERE code = ?"); | |||||
| 384 | 0 | $sth->execute($self->{code}); | ||||
| 385 | 0 | my ($count) = $sth->fetchrow_array; | ||||
| 386 | 0 | $sth->finish; | ||||
| 387 | 0 | return $count; | ||||
| 388 | } | |||||
| 389 | ||||||
| 390 - 397 | =head2 get_patrons my @borrowernumbers = $attr_type->get_patrons($attribute); Returns the borrowernumber of the patron records that have an attribute with the specifie value. =cut | |||||
| 398 | ||||||
| 399 | sub get_patrons { | |||||
| 400 | 0 | my $self = shift; | ||||
| 401 | 0 | my $value = shift; | ||||
| 402 | ||||||
| 403 | 0 | my $dbh = C4::Context->dbh; | ||||
| 404 | 0 | my $sth = $dbh->prepare_cached("SELECT DISTINCT borrowernumber | ||||
| 405 | FROM borrower_attributes | |||||
| 406 | WHERE code = ? | |||||
| 407 | AND attribute = ?"); | |||||
| 408 | 0 | $sth->execute($self->{code}, $value); | ||||
| 409 | 0 | my @results; | ||||
| 410 | 0 | while (my ($borrowernumber) = $sth->fetchrow_array) { | ||||
| 411 | 0 | push @results, $borrowernumber; | ||||
| 412 | } | |||||
| 413 | 0 | return @results; | ||||
| 414 | } | |||||
| 415 | ||||||
| 416 - 422 | =head1 AUTHOR Koha Development Team <http://koha-community.org/> Galen Charlton <galen.charlton@liblime.com> =cut | |||||
| 423 | ||||||
| 424 | 1; | |||||