new C4 modules for patron attributes
[koha.git] / C4 / Members / Attributes.pm
1 package C4::Members::Attributes;
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Context;
22 use C4::Members::AttributeTypes;
23
24 use vars qw($VERSION);
25
26 BEGIN {
27     # set the version for version checking
28     $VERSION = 3.00;
29 }
30
31 =head1 NAME
32
33 C4::Members::Attribute - manage extend patron attributes
34
35 =head1 SYNOPSIS
36
37 =over 4
38
39 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
40
41 =back
42
43 =head1 FUNCTIONS
44
45 =head2 GetBorrowerAttributes
46
47 =over 4
48
49 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber[, $opac_only]);
50
51 =back
52
53 Retrieve an arrayref of extended attributes associated with the
54 patron specified by C<$borrowernumber>.  Each entry in the arrayref
55 is a hashref containing the following keys:
56
57 code (attribute type code)
58 description (attribute type description)
59 value (attribute value)
60 value_description (attribute value description (if associated with an authorised value))
61 password (password, if any, associated with attribute
62
63 If the C<$opac_only> parameter is present and has a true value, only the attributes
64 marked for OPAC display are returned.
65
66 =cut
67
68 sub GetBorrowerAttributes {
69     my $borrowernumber = shift;
70     my $opac_only = @_ ? shift : 0;
71
72     my $dbh = C4::Context->dbh();
73     my $query = "SELECT code, description, attribute, lib, password
74                  FROM borrower_attributes
75                  JOIN borrower_attribute_types USING (code)
76                  LEFT JOIN authorised_values ON (category = authorised_value_category AND attribute = authorised_value)
77                  WHERE borrowernumber = ?";
78     $query .= "\nAND opac_display = 1" if $opac_only;
79     $query .= "\nORDER BY code, attribute";
80     my $sth = $dbh->prepare_cached($query);
81     $sth->execute($borrowernumber);
82     my @results = ();
83     while (my $row = $sth->fetchrow_hashref()) {
84         push @results, {
85             code              => $row->{'code'},
86             description       => $row->{'description'},
87             value             => $row->{'attribute'},  
88             value_description => $row->{'lib'},  
89             password          => $row->{'password'},
90         }
91     }
92     return \@results;
93 }
94
95 =head2 CheckUniqueness
96
97 =over 4
98
99 my $ok = CheckUniqueness($code, $value[, $borrowernumber]);
100
101 =back
102
103 Given an attribute type and value, verify if would violate
104 a unique_id restriction if added to the patron.  The
105 optional C<$borrowernumber> is the patron that the attribute
106 value would be added to, if known.
107
108 Returns false if the C<$code> is not valid or the
109 value would violate the uniqueness constraint.
110
111 =cut
112
113 sub CheckUniqueness {
114     my $code = shift;
115     my $value = shift;
116     my $borrowernumber = @_ ? shift : undef;
117
118     my $attr_type = C4::Members::AttributeTypes->fetch($code);
119
120     return 0 unless defined $attr_type;
121     return 1 unless $attr_type->unique_id();
122
123     my $dbh = C4::Context->dbh;
124     my $sth;
125     if (defined($borrowernumber)) {
126         $sth = $dbh->prepare("SELECT COUNT(*) 
127                               FROM borrower_attributes 
128                               WHERE code = ? 
129                               AND attribute = ?
130                               AND borrowernumber <> ?");
131         $sth->execute($code, $value, $borrowernumber);
132     } else {
133         $sth = $dbh->prepare("SELECT COUNT(*) 
134                               FROM borrower_attributes 
135                               WHERE code = ? 
136                               AND attribute = ?");
137         $sth->execute($code, $value);
138     }
139     my ($count) = $sth->fetchrow_array;
140     $sth->finish();
141     return ($count == 0);
142 }
143
144 =head2 SetBorrowerAttributes 
145
146 =over 4
147
148 SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value', password => 'password' }, ... ] );
149
150 =back
151
152 Set patron attributes for the patron identified by C<$borrowernumber>,
153 replacing any that existed previously.
154
155 =cut
156
157 sub SetBorrowerAttributes {
158     my $borrowernumber = shift;
159     my $attr_list = shift;
160
161     my $dbh = C4::Context->dbh;
162     my $delsth = $dbh->prepare("DELETE FROM borrower_attributes WHERE borrowernumber = ?");
163     $delsth->execute($borrowernumber);
164
165     my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute, password)
166                              VALUES (?, ?, ?, ?)");
167     foreach my $attr (@$attr_list) {
168         $attr->{password} = undef unless exists $attr->{password};
169         $sth->execute($borrowernumber, $attr->{code}, $attr->{value}, $attr->{password});
170     }
171 }
172
173 =head1 AUTHOR
174
175 Koha Development Team <info@koha.org>
176
177 Galen Charlton <galen.charlton@liblime.com>
178
179 =cut
180
181 1;