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
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>.
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(@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS);
28 our ($csv, $AttributeTypes);
32 @EXPORT_OK = qw(GetBorrowerAttributes GetBorrowerAttributeValue CheckUniqueness SetBorrowerAttributes
33 DeleteBorrowerAttribute UpdateBorrowerAttribute
34 extended_attributes_code_value_arrayref extended_attributes_merge
35 SearchIdMatchingAttribute);
36 %EXPORT_TAGS = ( all
=> \
@EXPORT_OK );
41 C4::Members::Attributes - manage extend patron attributes
45 use C4::Members::Attributes;
46 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
50 =head2 GetBorrowerAttributes
52 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber[, $opac_only]);
54 Retrieve an arrayref of extended attributes associated with the
55 patron specified by C<$borrowernumber>. Each entry in the arrayref
56 is a hashref containing the following keys:
58 code (attribute type code)
59 description (attribute type description)
60 value (attribute value)
61 value_description (attribute value description (if associated with an authorised value))
63 If the C<$opac_only> parameter is present and has a true value, only the attributes
64 marked for OPAC display are returned.
68 sub GetBorrowerAttributes
{
69 my $borrowernumber = shift;
70 my $opac_only = @_ ?
shift : 0;
72 my $dbh = C4
::Context
->dbh();
73 my $query = "SELECT code, description, attribute, lib, display_checkout, category_code, class
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);
83 while (my $row = $sth->fetchrow_hashref()) {
85 code
=> $row->{'code'},
86 description
=> $row->{'description'},
87 value
=> $row->{'attribute'},
88 value_description
=> $row->{'lib'},
89 display_checkout
=> $row->{'display_checkout'},
90 category_code
=> $row->{'category_code'},
91 class => $row->{'class'},
100 my $attributes = C4::Members::Attributes::GetAttributes([$opac_only]);
102 Retrieve an arrayref of extended attribute codes
107 my ($opac_only) = @_;
109 my $dbh = C4
::Context
->dbh();
110 my $query = "SELECT code FROM borrower_attribute_types";
111 $query .= "\nWHERE opac_display = 1" if $opac_only;
112 $query .= "\nORDER BY code";
113 return $dbh->selectcol_arrayref($query);
116 =head2 GetBorrowerAttributeValue
118 my $value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attribute_code);
120 Retrieve the value of an extended attribute C<$attribute_code> associated with the
121 patron specified by C<$borrowernumber>.
125 sub GetBorrowerAttributeValue
{
126 my $borrowernumber = shift;
129 my $dbh = C4
::Context
->dbh();
130 my $query = "SELECT attribute
131 FROM borrower_attributes
132 WHERE borrowernumber = ?
134 my $value = $dbh->selectrow_array($query, undef, $borrowernumber, $code);
138 =head2 SearchIdMatchingAttribute
140 my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
144 sub SearchIdMatchingAttribute
{
146 $filter = [$filter] unless ref $filter;
148 my $dbh = C4
::Context
->dbh();
150 SELECT DISTINCT borrowernumber
151 FROM borrower_attributes
152 JOIN borrower_attribute_types USING
(code
)
153 WHERE staff_searchable
= 1
154 AND
(} . join (" OR ", map "attribute like ?", @
$filter) .qq{)};
155 my $sth = $dbh->prepare_cached($query);
156 $sth->execute(map "%$_%", @
$filter);
157 return [map $_->[0], @
{ $sth->fetchall_arrayref }];
160 =head2 CheckUniqueness
162 my $ok = CheckUniqueness($code, $value[, $borrowernumber]);
164 Given an attribute type and value, verify if would violate
165 a unique_id restriction if added to the patron. The
166 optional C<$borrowernumber> is the patron that the attribute
167 value would be added to, if known.
169 Returns false if the C<$code> is not valid or the
170 value would violate the uniqueness constraint.
174 sub CheckUniqueness
{
177 my $borrowernumber = @_ ?
shift : undef;
179 my $attr_type = C4
::Members
::AttributeTypes
->fetch($code);
181 return 0 unless defined $attr_type;
182 return 1 unless $attr_type->unique_id();
184 my $dbh = C4
::Context
->dbh;
186 if (defined($borrowernumber)) {
187 $sth = $dbh->prepare("SELECT COUNT(*)
188 FROM borrower_attributes
191 AND borrowernumber <> ?");
192 $sth->execute($code, $value, $borrowernumber);
194 $sth = $dbh->prepare("SELECT COUNT(*)
195 FROM borrower_attributes
198 $sth->execute($code, $value);
200 my ($count) = $sth->fetchrow_array;
201 return ($count == 0);
204 =head2 SetBorrowerAttributes
206 SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value' }, ... ] );
208 Set patron attributes for the patron identified by C<$borrowernumber>,
209 replacing any that existed previously.
213 sub SetBorrowerAttributes
{
214 my $borrowernumber = shift;
215 my $attr_list = shift;
216 my $no_branch_limit = shift // 0;
218 my $dbh = C4
::Context
->dbh;
220 DeleteBorrowerAttributes
( $borrowernumber, $no_branch_limit );
222 my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute)
224 foreach my $attr (@
$attr_list) {
225 $sth->execute($borrowernumber, $attr->{code
}, $attr->{value
});
227 warn sprintf('Database returned the following error: %s', $sth->errstr);
228 return; # bail immediately on errors
231 return 1; # borrower attributes successfully set
234 =head2 DeleteBorrowerAttributes
236 DeleteBorrowerAttributes($borrowernumber);
238 Delete borrower attributes for the patron identified by C<$borrowernumber>.
242 sub DeleteBorrowerAttributes
{
243 my $borrowernumber = shift;
244 my $no_branch_limit = @_ ?
shift : 0;
245 my $branch_limit = $no_branch_limit
247 : C4
::Context
->userenv ? C4
::Context
->userenv->{"branch"} : 0;
249 my $dbh = C4
::Context
->dbh;
251 DELETE borrower_attributes FROM borrower_attributes
254 $query .= $branch_limit
256 LEFT JOIN borrower_attribute_types_branches ON bat_code = code
257 WHERE ( b_branchcode = ? OR b_branchcode IS NULL )
258 AND borrowernumber = ?
261 WHERE borrowernumber = ?
264 $dbh->do( $query, undef, $branch_limit ?
$branch_limit : (), $borrowernumber );
267 =head2 DeleteBorrowerAttribute
269 DeleteBorrowerAttribute($borrowernumber, $attribute);
271 Delete a borrower attribute for the patron identified by C<$borrowernumber> and the attribute code of C<$attribute>
275 sub DeleteBorrowerAttribute
{
276 my ( $borrowernumber, $attribute ) = @_;
278 my $dbh = C4
::Context
->dbh;
279 my $sth = $dbh->prepare(qq{
280 DELETE FROM borrower_attributes
281 WHERE borrowernumber
= ?
284 $sth->execute( $borrowernumber, $attribute->{code
} );
287 =head2 UpdateBorrowerAttribute
289 UpdateBorrowerAttribute($borrowernumber, $attribute );
291 Update a borrower attribute C<$attribute> for the patron identified by C<$borrowernumber>,
295 sub UpdateBorrowerAttribute
{
296 my ( $borrowernumber, $attribute ) = @_;
298 DeleteBorrowerAttribute
$borrowernumber, $attribute;
300 my $dbh = C4
::Context
->dbh;
301 my $query = "INSERT INTO borrower_attributes SET attribute = ?, code = ?, borrowernumber = ?";
302 my @params = ( $attribute->{attribute
}, $attribute->{code
}, $borrowernumber );
303 my $sth = $dbh->prepare( $query );
305 $sth->execute( @params );
309 =head2 extended_attributes_code_value_arrayref
311 my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
312 my $aref = extended_attributes_code_value_arrayref($patron_attributes);
314 Takes a comma-delimited CSV-style string argument and returns the kind of data structure that SetBorrowerAttributes wants,
315 namely a reference to array of hashrefs like:
316 [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
318 Caches Text::CSV parser object for efficiency.
322 sub extended_attributes_code_value_arrayref
{
323 my $string = shift or return;
324 $csv or $csv = Text
::CSV
->new({binary
=> 1}); # binary needed for non-ASCII Unicode
325 my $ok = $csv->parse($string); # parse field again to get subfields!
326 my @list = $csv->fields();
327 # TODO: error handling (check $ok)
329 sort {&_sort_by_code
($a,$b)}
330 map { map { my @arr = split /:/, $_, 2; { code
=> $arr[0], value
=> $arr[1] } } $_ }
333 # nested map because of split
336 =head2 extended_attributes_merge
338 my $old_attributes = extended_attributes_code_value_arrayref("homeroom:224,grade:04,deanslist:2007,deanslist:2008,somedata:xxx");
339 my $new_attributes = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2009,extradata:foobar");
340 my $merged = extended_attributes_merge($patron_attributes, $new_attributes, 1);
342 # assuming deanslist is a repeatable code, value same as:
343 # $merged = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2007,deanslist:2008,deanslist:2009,extradata:foobar,somedata:xxx");
345 Takes three arguments. The first two are references to array of hashrefs, each like:
346 [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
348 The third option specifies whether repeatable codes are clobbered or collected. True for non-clobber.
350 Returns one reference to (merged) array of hashref.
352 Caches results of C4::Members::AttributeTypes::GetAttributeTypes_hashref(1) for efficiency.
356 sub extended_attributes_merge
{
357 my $old = shift or return;
358 my $new = shift or return $old;
359 my $keep = @_ ?
shift : 0;
360 $AttributeTypes or $AttributeTypes = C4
::Members
::AttributeTypes
::GetAttributeTypes_hashref
(1);
362 foreach my $att (@
$new) {
363 unless ($att->{code
}) {
364 warn "Cannot merge element: no 'code' defined";
367 unless ($AttributeTypes->{$att->{code
}}) {
368 warn "Cannot merge element: unrecognized code = '$att->{code}'";
371 unless ($AttributeTypes->{$att->{code
}}->{repeatable
} and $keep) {
372 @merged = grep {$att->{code
} ne $_->{code
}} @merged; # filter out any existing attributes of the same code
376 return [( sort {&_sort_by_code
($a,$b)} @merged )];
381 defined ($x->{code
}) or return -1;
382 defined ($y->{code
}) or return 1;
383 return $x->{code
} cmp $y->{code
} || $x->{value
} cmp $y->{value
};
388 Koha Development Team <http://koha-community.org/>
390 Galen Charlton <galen.charlton@liblime.com>