1 package C4::Members::Attributes;
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 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
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.
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.
23 use Text::CSV; # Don't be tempted to use Text::CSV::Unicode -- even in binary mode it fails.
25 use C4::Members::AttributeTypes;
27 use vars qw($VERSION @ISA @EXPORT_OK @EXPORT %EXPORT_TAGS);
28 our ($csv, $AttributeTypes);
31 # set the version for version checking
34 @EXPORT_OK = qw(GetBorrowerAttributes GetBorrowerAttributeValue CheckUniqueness SetBorrowerAttributes
35 extended_attributes_code_value_arrayref extended_attributes_merge
36 SearchIdMatchingAttribute);
37 %EXPORT_TAGS = ( all => \@EXPORT_OK );
42 C4::Members::Attributes - manage extend patron attributes
46 use C4::Members::Attributes;
47 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
51 =head2 GetBorrowerAttributes
53 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber[, $opac_only]);
55 Retrieve an arrayref of extended attributes associated with the
56 patron specified by C<$borrowernumber>. Each entry in the arrayref
57 is a hashref containing the following keys:
59 code (attribute type code)
60 description (attribute type description)
61 value (attribute value)
62 value_description (attribute value description (if associated with an authorised value))
63 password (password, if any, associated with attribute
65 If the C<$opac_only> parameter is present and has a true value, only the attributes
66 marked for OPAC display are returned.
70 sub GetBorrowerAttributes {
71 my $borrowernumber = shift;
72 my $opac_only = @_ ? shift : 0;
74 my $dbh = C4::Context->dbh();
75 my $query = "SELECT code, description, attribute, lib, password, display_checkout
76 FROM borrower_attributes
77 JOIN borrower_attribute_types USING (code)
78 LEFT JOIN authorised_values ON (category = authorised_value_category AND attribute = authorised_value)
79 WHERE borrowernumber = ?";
80 $query .= "\nAND opac_display = 1" if $opac_only;
81 $query .= "\nORDER BY code, attribute";
82 my $sth = $dbh->prepare_cached($query);
83 $sth->execute($borrowernumber);
85 while (my $row = $sth->fetchrow_hashref()) {
87 code => $row->{'code'},
88 description => $row->{'description'},
89 value => $row->{'attribute'},
90 value_description => $row->{'lib'},
91 password => $row->{'password'},
92 display_checkout => $row->{'display_checkout'},
98 =head2 GetBorrowerAttributeValue
100 my $value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attribute_code);
102 Retrieve the value of an extended attribute C<$attribute_code> associated with the
103 patron specified by C<$borrowernumber>.
107 sub GetBorrowerAttributeValue {
108 my $borrowernumber = shift;
111 my $dbh = C4::Context->dbh();
112 my $query = "SELECT attribute
113 FROM borrower_attributes
114 WHERE borrowernumber = ?
116 my $value = $dbh->selectrow_array($query, undef, $borrowernumber, $code);
120 =head2 SearchIdMatchingAttribute
122 my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
126 sub SearchIdMatchingAttribute{
128 $filter = [$filter] unless ref $filter;
130 my $dbh = C4::Context->dbh();
132 SELECT DISTINCT borrowernumber
133 FROM borrower_attributes
134 JOIN borrower_attribute_types USING (code)
135 WHERE staff_searchable = 1
136 AND (} . join (" OR ", map "attribute like ?", @$filter) .qq{)};
137 my $sth = $dbh->prepare_cached($query);
138 $sth->execute(map "%$_%", @$filter);
139 return [map $_->[0], @{ $sth->fetchall_arrayref }];
142 =head2 CheckUniqueness
144 my $ok = CheckUniqueness($code, $value[, $borrowernumber]);
146 Given an attribute type and value, verify if would violate
147 a unique_id restriction if added to the patron. The
148 optional C<$borrowernumber> is the patron that the attribute
149 value would be added to, if known.
151 Returns false if the C<$code> is not valid or the
152 value would violate the uniqueness constraint.
156 sub CheckUniqueness {
159 my $borrowernumber = @_ ? shift : undef;
161 my $attr_type = C4::Members::AttributeTypes->fetch($code);
163 return 0 unless defined $attr_type;
164 return 1 unless $attr_type->unique_id();
166 my $dbh = C4::Context->dbh;
168 if (defined($borrowernumber)) {
169 $sth = $dbh->prepare("SELECT COUNT(*)
170 FROM borrower_attributes
173 AND borrowernumber <> ?");
174 $sth->execute($code, $value, $borrowernumber);
176 $sth = $dbh->prepare("SELECT COUNT(*)
177 FROM borrower_attributes
180 $sth->execute($code, $value);
182 my ($count) = $sth->fetchrow_array;
183 return ($count == 0);
186 =head2 SetBorrowerAttributes
188 SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value', password => 'password' }, ... ] );
190 Set patron attributes for the patron identified by C<$borrowernumber>,
191 replacing any that existed previously.
195 sub SetBorrowerAttributes {
196 my $borrowernumber = shift;
197 my $attr_list = shift;
199 my $dbh = C4::Context->dbh;
200 my $delsth = $dbh->prepare("DELETE FROM borrower_attributes WHERE borrowernumber = ?");
201 $delsth->execute($borrowernumber);
203 my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute, password)
204 VALUES (?, ?, ?, ?)");
205 foreach my $attr (@$attr_list) {
206 $attr->{password} = undef unless exists $attr->{password};
207 $sth->execute($borrowernumber, $attr->{code}, $attr->{value}, $attr->{password});
209 warn sprintf('Database returned the following error: %s', $sth->errstr);
210 return; # bail immediately on errors
213 return 1; # borower attributes successfully set
216 =head2 extended_attributes_code_value_arrayref
218 my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
219 my $aref = extended_attributes_code_value_arrayref($patron_attributes);
221 Takes a comma-delimited CSV-style string argument and returns the kind of data structure that SetBorrowerAttributes wants,
222 namely a reference to array of hashrefs like:
223 [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
225 Caches Text::CSV parser object for efficiency.
229 sub extended_attributes_code_value_arrayref {
230 my $string = shift or return;
231 $csv or $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode
232 my $ok = $csv->parse($string); # parse field again to get subfields!
233 my @list = $csv->fields();
234 # TODO: error handling (check $ok)
236 sort {&_sort_by_code($a,$b)}
237 map { map { my @arr = split /:/, $_, 2; { code => $arr[0], value => $arr[1] } } $_ }
240 # nested map because of split
243 =head2 extended_attributes_merge
245 my $old_attributes = extended_attributes_code_value_arrayref("homeroom:224,grade:04,deanslist:2007,deanslist:2008,somedata:xxx");
246 my $new_attributes = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2009,extradata:foobar");
247 my $merged = extended_attributes_merge($patron_attributes, $new_attributes, 1);
249 # assuming deanslist is a repeatable code, value same as:
250 # $merged = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2007,deanslist:2008,deanslist:2009,extradata:foobar,somedata:xxx");
252 Takes three arguments. The first two are references to array of hashrefs, each like:
253 [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
255 The third option specifies whether repeatable codes are clobbered or collected. True for non-clobber.
257 Returns one reference to (merged) array of hashref.
259 Caches results of C4::Members::AttributeTypes::GetAttributeTypes_hashref(1) for efficiency.
263 sub extended_attributes_merge {
264 my $old = shift or return;
265 my $new = shift or return $old;
266 my $keep = @_ ? shift : 0;
267 $AttributeTypes or $AttributeTypes = C4::Members::AttributeTypes::GetAttributeTypes_hashref(1);
269 foreach my $att (@$new) {
270 unless ($att->{code}) {
271 warn "Cannot merge element: no 'code' defined";
274 unless ($AttributeTypes->{$att->{code}}) {
275 warn "Cannot merge element: unrecognized code = '$att->{code}'";
278 unless ($AttributeTypes->{$att->{code}}->{repeatable} and $keep) {
279 @merged = grep {$att->{code} ne $_->{code}} @merged; # filter out any existing attributes of the same code
283 return [( sort {&_sort_by_code($a,$b)} @merged )];
288 defined ($x->{code}) or return -1;
289 defined ($y->{code}) or return 1;
290 return $x->{code} cmp $y->{code} || $x->{value} cmp $y->{value};
295 Koha Development Team <http://koha-community.org/>
297 Galen Charlton <galen.charlton@liblime.com>