19b5b9e6efa68f6e8bfe83ebd3c5878d6beeff38
[srvgit] / 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
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22
23 use Text::CSV;      # Don't be tempted to use Text::CSV::Unicode -- even in binary mode it fails.
24 use C4::Context;
25
26 use Koha::Patron::Attribute::Types;
27
28 use vars qw(@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS);
29 our ($csv, $AttributeTypes);
30
31 BEGIN {
32     @ISA = qw(Exporter);
33     @EXPORT_OK = qw(
34                     extended_attributes_code_value_arrayref extended_attributes_merge
35                     SearchIdMatchingAttribute);
36     %EXPORT_TAGS = ( all => \@EXPORT_OK );
37 }
38
39 =head1 NAME
40
41 C4::Members::Attributes - manage extend patron attributes
42
43 =head1 SYNOPSIS
44
45   use C4::Members::Attributes;
46
47 =head1 FUNCTIONS
48
49 =head2 SearchIdMatchingAttribute
50
51   my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
52
53 =cut
54 use Koha::Patrons;
55 sub SearchIdMatchingAttribute{
56     my $filter = shift;
57
58     my @borrowernumbers = Koha::Patrons->filter_by_attribute_value($filter)->get_column('borrowernumber');
59     return \@borrowernumbers;
60 }
61
62 =head2 extended_attributes_code_value_arrayref 
63
64    my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
65    my $aref = extended_attributes_code_value_arrayref($patron_attributes);
66
67 Takes a comma-delimited CSV-style string argument and returns the kind of data structure that Koha::Patron->extended_attributes wants,
68 namely a reference to array of hashrefs like:
69  [ { code => 'CODE', attribute => 'value' }, { code => 'CODE2', attribute => 'othervalue' } ... ]
70
71 Caches Text::CSV parser object for efficiency.
72
73 =cut
74
75 sub extended_attributes_code_value_arrayref {
76     my $string = shift or return;
77     $csv or $csv = Text::CSV->new({binary => 1});  # binary needed for non-ASCII Unicode
78     my $ok   = $csv->parse($string);  # parse field again to get subfields!
79     my @list = $csv->fields();
80     # TODO: error handling (check $ok)
81     return [
82         sort {&_sort_by_code($a,$b)}
83         map { map { my @arr = split /:/, $_, 2; { code => $arr[0], attribute => $arr[1] } } $_ }
84         @list
85     ];
86     # nested map because of split
87 }
88
89 =head2 extended_attributes_merge
90
91   my $old_attributes = extended_attributes_code_value_arrayref("homeroom:224,grade:04,deanslist:2007,deanslist:2008,somedata:xxx");
92   my $new_attributes = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2009,extradata:foobar");
93   my $merged = extended_attributes_merge($patron_attributes, $new_attributes, 1);
94
95   # assuming deanslist is a repeatable code, value same as:
96   # $merged = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2007,deanslist:2008,deanslist:2009,extradata:foobar,somedata:xxx");
97
98 Takes three arguments.  The first two are references to array of hashrefs, each like:
99  [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
100
101 The third option specifies whether repeatable codes are clobbered or collected.  True for non-clobber.
102
103 Returns one reference to (merged) array of hashref.
104
105 Caches results of attribute types for efficiency. # FIXME That is very bad and can cause side-effects undef plack
106
107 =cut
108
109 sub extended_attributes_merge {
110     my $old = shift or return;
111     my $new = shift or return $old;
112     my $keep = @_ ? shift : 0;
113     $AttributeTypes or $AttributeTypes = Koha::Patron::Attribute::Types->search->unblessed;
114     my @merged = @$old;
115     foreach my $att (@$new) {
116         unless ($att->{code}) {
117             warn "Cannot merge element: no 'code' defined";
118             next;
119         }
120         unless ($AttributeTypes->{$att->{code}}) {
121             warn "Cannot merge element: unrecognized code = '$att->{code}'";
122             next;
123         }
124         unless ($AttributeTypes->{$att->{code}}->{repeatable} and $keep) {
125             @merged = grep {$att->{code} ne $_->{code}} @merged;    # filter out any existing attributes of the same code
126         }
127         push @merged, $att;
128     }
129     return [( sort {&_sort_by_code($a,$b)} @merged )];
130 }
131
132 sub _sort_by_code {
133     my ($x, $y) = @_;
134     defined ($x->{code}) or return -1;
135     defined ($y->{code}) or return 1;
136     return $x->{code} cmp $y->{code} || $x->{value} cmp $y->{value};
137 }
138
139 =head1 AUTHOR
140
141 Koha Development Team <http://koha-community.org/>
142
143 Galen Charlton <galen.charlton@liblime.com>
144
145 =cut
146
147 1;