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 DeleteBorrowerAttribute UpdateBorrowerAttribute
36 extended_attributes_code_value_arrayref extended_attributes_merge
37 SearchIdMatchingAttribute);
38 %EXPORT_TAGS = ( all => \@EXPORT_OK );
43 C4::Members::Attributes - manage extend patron attributes
47 use C4::Members::Attributes;
48 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
52 =head2 GetBorrowerAttributes
54 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber[, $opac_only]);
56 Retrieve an arrayref of extended attributes associated with the
57 patron specified by C<$borrowernumber>. Each entry in the arrayref
58 is a hashref containing the following keys:
60 code (attribute type code)
61 description (attribute type description)
62 value (attribute value)
63 value_description (attribute value description (if associated with an authorised value))
64 password (password, if any, associated with attribute
66 If the C<$opac_only> parameter is present and has a true value, only the attributes
67 marked for OPAC display are returned.
71 sub GetBorrowerAttributes {
72 my $borrowernumber = shift;
73 my $opac_only = @_ ? shift : 0;
75 my $dbh = C4::Context->dbh();
76 my $query = "SELECT code, description, attribute, lib, password, display_checkout, category_code, class
77 FROM borrower_attributes
78 JOIN borrower_attribute_types USING (code)
79 LEFT JOIN authorised_values ON (category = authorised_value_category AND attribute = authorised_value)
81 $query .= "\nAND borrowernumber = ?" if $borrowernumber;
82 $query .= "\nAND opac_display = 1" if $opac_only;
83 $query .= "\nORDER BY code, attribute";
84 my $sth = $dbh->prepare_cached($query);
85 $sth->execute($borrowernumber ? $borrowernumber : ());
87 while (my $row = $sth->fetchrow_hashref()) {
89 code => $row->{'code'},
90 description => $row->{'description'},
91 value => $row->{'attribute'},
92 value_description => $row->{'lib'},
93 password => $row->{'password'},
94 display_checkout => $row->{'display_checkout'},
95 category_code => $row->{'category_code'},
96 class => $row->{'class'},
104 my $attributes = C4::Members::Attributes::GetAttributes([$opac_only]);
106 Retrieve an arrayref of extended attribute codes
111 my ($opac_only) = @_;
113 my $dbh = C4::Context->dbh();
114 my $query = "SELECT code FROM borrower_attribute_types";
115 $query .= "\nWHERE opac_display = 1" if $opac_only;
116 $query .= "\nORDER BY code";
117 return $dbh->selectcol_arrayref($query);
120 =head2 GetBorrowerAttributeValue
122 my $value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attribute_code);
124 Retrieve the value of an extended attribute C<$attribute_code> associated with the
125 patron specified by C<$borrowernumber>.
129 sub GetBorrowerAttributeValue {
130 my $borrowernumber = shift;
133 my $dbh = C4::Context->dbh();
134 my $query = "SELECT attribute
135 FROM borrower_attributes
136 WHERE borrowernumber = ?
138 my $value = $dbh->selectrow_array($query, undef, $borrowernumber, $code);
142 =head2 SearchIdMatchingAttribute
144 my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
148 sub SearchIdMatchingAttribute{
150 $filter = [$filter] unless ref $filter;
152 my $dbh = C4::Context->dbh();
154 SELECT DISTINCT borrowernumber
155 FROM borrower_attributes
156 JOIN borrower_attribute_types USING (code)
157 WHERE staff_searchable = 1
158 AND (} . join (" OR ", map "attribute like ?", @$filter) .qq{)};
159 my $sth = $dbh->prepare_cached($query);
160 $sth->execute(map "%$_%", @$filter);
161 return [map $_->[0], @{ $sth->fetchall_arrayref }];
164 =head2 CheckUniqueness
166 my $ok = CheckUniqueness($code, $value[, $borrowernumber]);
168 Given an attribute type and value, verify if would violate
169 a unique_id restriction if added to the patron. The
170 optional C<$borrowernumber> is the patron that the attribute
171 value would be added to, if known.
173 Returns false if the C<$code> is not valid or the
174 value would violate the uniqueness constraint.
178 sub CheckUniqueness {
181 my $borrowernumber = @_ ? shift : undef;
183 my $attr_type = C4::Members::AttributeTypes->fetch($code);
185 return 0 unless defined $attr_type;
186 return 1 unless $attr_type->unique_id();
188 my $dbh = C4::Context->dbh;
190 if (defined($borrowernumber)) {
191 $sth = $dbh->prepare("SELECT COUNT(*)
192 FROM borrower_attributes
195 AND borrowernumber <> ?");
196 $sth->execute($code, $value, $borrowernumber);
198 $sth = $dbh->prepare("SELECT COUNT(*)
199 FROM borrower_attributes
202 $sth->execute($code, $value);
204 my ($count) = $sth->fetchrow_array;
205 return ($count == 0);
208 =head2 SetBorrowerAttributes
210 SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value', password => 'password' }, ... ] );
212 Set patron attributes for the patron identified by C<$borrowernumber>,
213 replacing any that existed previously.
217 sub SetBorrowerAttributes {
218 my $borrowernumber = shift;
219 my $attr_list = shift;
221 my $dbh = C4::Context->dbh;
222 my $delsth = $dbh->prepare("DELETE FROM borrower_attributes WHERE borrowernumber = ?");
223 $delsth->execute($borrowernumber);
225 my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute, password)
226 VALUES (?, ?, ?, ?)");
227 foreach my $attr (@$attr_list) {
228 $attr->{password} = undef unless exists $attr->{password};
229 $sth->execute($borrowernumber, $attr->{code}, $attr->{value}, $attr->{password});
231 warn sprintf('Database returned the following error: %s', $sth->errstr);
232 return; # bail immediately on errors
235 return 1; # borower attributes successfully set
238 =head2 DeleteBorrowerAttribute
240 DeleteBorrowerAttribute($borrowernumber, $attribute);
242 Delete a borrower attribute for the patron identified by C<$borrowernumber> and the attribute code of C<$attribute>
245 sub DeleteBorrowerAttribute {
246 my ( $borrowernumber, $attribute ) = @_;
248 my $dbh = C4::Context->dbh;
249 my $sth = $dbh->prepare(qq{
250 DELETE FROM borrower_attributes
251 WHERE borrowernumber = ?
254 $sth->execute( $borrowernumber, $attribute->{code} );
257 =head2 UpdateBorrowerAttribute
259 UpdateBorrowerAttribute($borrowernumber, $attribute );
261 Update a borrower attribute C<$attribute> for the patron identified by C<$borrowernumber>,
264 sub UpdateBorrowerAttribute {
265 my ( $borrowernumber, $attribute ) = @_;
267 DeleteBorrowerAttribute $borrowernumber, $attribute;
269 my $dbh = C4::Context->dbh;
270 my $query = "INSERT INTO borrower_attributes SET attribute = ?, code = ?, borrowernumber = ?";
271 my @params = ( $attribute->{attribute}, $attribute->{code}, $borrowernumber );
272 if ( defined $attribute->{password} ) {
273 $query .= ", password = ?";
274 push @params, $attribute->{password};
276 my $sth = $dbh->prepare( $query );
278 $sth->execute( @params );
282 =head2 extended_attributes_code_value_arrayref
284 my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
285 my $aref = extended_attributes_code_value_arrayref($patron_attributes);
287 Takes a comma-delimited CSV-style string argument and returns the kind of data structure that SetBorrowerAttributes wants,
288 namely a reference to array of hashrefs like:
289 [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
291 Caches Text::CSV parser object for efficiency.
295 sub extended_attributes_code_value_arrayref {
296 my $string = shift or return;
297 $csv or $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode
298 my $ok = $csv->parse($string); # parse field again to get subfields!
299 my @list = $csv->fields();
300 # TODO: error handling (check $ok)
302 sort {&_sort_by_code($a,$b)}
303 map { map { my @arr = split /:/, $_, 2; { code => $arr[0], value => $arr[1] } } $_ }
306 # nested map because of split
309 =head2 extended_attributes_merge
311 my $old_attributes = extended_attributes_code_value_arrayref("homeroom:224,grade:04,deanslist:2007,deanslist:2008,somedata:xxx");
312 my $new_attributes = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2009,extradata:foobar");
313 my $merged = extended_attributes_merge($patron_attributes, $new_attributes, 1);
315 # assuming deanslist is a repeatable code, value same as:
316 # $merged = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2007,deanslist:2008,deanslist:2009,extradata:foobar,somedata:xxx");
318 Takes three arguments. The first two are references to array of hashrefs, each like:
319 [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
321 The third option specifies whether repeatable codes are clobbered or collected. True for non-clobber.
323 Returns one reference to (merged) array of hashref.
325 Caches results of C4::Members::AttributeTypes::GetAttributeTypes_hashref(1) for efficiency.
329 sub extended_attributes_merge {
330 my $old = shift or return;
331 my $new = shift or return $old;
332 my $keep = @_ ? shift : 0;
333 $AttributeTypes or $AttributeTypes = C4::Members::AttributeTypes::GetAttributeTypes_hashref(1);
335 foreach my $att (@$new) {
336 unless ($att->{code}) {
337 warn "Cannot merge element: no 'code' defined";
340 unless ($AttributeTypes->{$att->{code}}) {
341 warn "Cannot merge element: unrecognized code = '$att->{code}'";
344 unless ($AttributeTypes->{$att->{code}}->{repeatable} and $keep) {
345 @merged = grep {$att->{code} ne $_->{code}} @merged; # filter out any existing attributes of the same code
349 return [( sort {&_sort_by_code($a,$b)} @merged )];
354 defined ($x->{code}) or return -1;
355 defined ($y->{code}) or return 1;
356 return $x->{code} cmp $y->{code} || $x->{value} cmp $y->{value};
361 Koha Development Team <http://koha-community.org/>
363 Galen Charlton <galen.charlton@liblime.com>