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.
26 use Koha::Patron::Attribute::Types;
28 use vars qw(@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS);
29 our ($csv, $AttributeTypes);
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;
49 =head2 SearchIdMatchingAttribute
51 my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
55 sub SearchIdMatchingAttribute{
57 $filter = [$filter] unless ref $filter;
59 my $dbh = C4::Context->dbh();
61 SELECT DISTINCT borrowernumber
62 FROM borrower_attributes
63 JOIN borrower_attribute_types USING (code)
64 WHERE staff_searchable = 1
65 AND (} . join (" OR ", map "attribute like ?", @$filter) .qq{)};
66 my $sth = $dbh->prepare_cached($query);
67 $sth->execute(map "%$_%", @$filter);
68 return [map $_->[0], @{ $sth->fetchall_arrayref }];
71 =head2 extended_attributes_code_value_arrayref
73 my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
74 my $aref = extended_attributes_code_value_arrayref($patron_attributes);
76 Takes a comma-delimited CSV-style string argument and returns the kind of data structure that Koha::Patron->extended_attributes wants,
77 namely a reference to array of hashrefs like:
78 [ { code => 'CODE', attribute => 'value' }, { code => 'CODE2', attribute => 'othervalue' } ... ]
80 Caches Text::CSV parser object for efficiency.
84 sub extended_attributes_code_value_arrayref {
85 my $string = shift or return;
86 $csv or $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode
87 my $ok = $csv->parse($string); # parse field again to get subfields!
88 my @list = $csv->fields();
89 # TODO: error handling (check $ok)
91 sort {&_sort_by_code($a,$b)}
92 map { map { my @arr = split /:/, $_, 2; { code => $arr[0], attribute => $arr[1] } } $_ }
95 # nested map because of split
98 =head2 extended_attributes_merge
100 my $old_attributes = extended_attributes_code_value_arrayref("homeroom:224,grade:04,deanslist:2007,deanslist:2008,somedata:xxx");
101 my $new_attributes = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2009,extradata:foobar");
102 my $merged = extended_attributes_merge($patron_attributes, $new_attributes, 1);
104 # assuming deanslist is a repeatable code, value same as:
105 # $merged = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2007,deanslist:2008,deanslist:2009,extradata:foobar,somedata:xxx");
107 Takes three arguments. The first two are references to array of hashrefs, each like:
108 [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
110 The third option specifies whether repeatable codes are clobbered or collected. True for non-clobber.
112 Returns one reference to (merged) array of hashref.
114 Caches results of attribute types for efficiency. # FIXME That is very bad and can cause side-effects undef plack
118 sub extended_attributes_merge {
119 my $old = shift or return;
120 my $new = shift or return $old;
121 my $keep = @_ ? shift : 0;
122 $AttributeTypes or $AttributeTypes = Koha::Patron::Attribute::Types->search->unblessed;
124 foreach my $att (@$new) {
125 unless ($att->{code}) {
126 warn "Cannot merge element: no 'code' defined";
129 unless ($AttributeTypes->{$att->{code}}) {
130 warn "Cannot merge element: unrecognized code = '$att->{code}'";
133 unless ($AttributeTypes->{$att->{code}}->{repeatable} and $keep) {
134 @merged = grep {$att->{code} ne $_->{code}} @merged; # filter out any existing attributes of the same code
138 return [( sort {&_sort_by_code($a,$b)} @merged )];
143 defined ($x->{code}) or return -1;
144 defined ($y->{code}) or return 1;
145 return $x->{code} cmp $y->{code} || $x->{value} cmp $y->{value};
150 Koha Development Team <http://koha-community.org/>
152 Galen Charlton <galen.charlton@liblime.com>