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
36 %EXPORT_TAGS = ( all => \@EXPORT_OK );
41 C4::Members::Attributes - manage extend patron attributes
45 use C4::Members::Attributes;
49 =head2 extended_attributes_code_value_arrayref
51 my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
52 my $aref = extended_attributes_code_value_arrayref($patron_attributes);
54 Takes a comma-delimited CSV-style string argument and returns the kind of data structure that Koha::Patron->extended_attributes wants,
55 namely a reference to array of hashrefs like:
56 [ { code => 'CODE', attribute => 'value' }, { code => 'CODE2', attribute => 'othervalue' } ... ]
58 Caches Text::CSV parser object for efficiency.
62 sub extended_attributes_code_value_arrayref {
63 my $string = shift or return;
64 use Data::Printer colored => 1; warn p $string;
65 $csv or $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode
66 my $ok = $csv->parse($string); # parse field again to get subfields!
67 my @list = $csv->fields();
68 # TODO: error handling (check $ok)
70 sort {&_sort_by_code($a,$b)}
71 map { map { my @arr = split /:/, $_, 2; { code => $arr[0], attribute => $arr[1] } } $_ }
74 # nested map because of split
79 defined ($x->{code}) or return -1;
80 defined ($y->{code}) or return 1;
81 return $x->{code} cmp $y->{code} || $x->{value} cmp $y->{value};
86 Koha Development Team <http://koha-community.org/>
88 Galen Charlton <galen.charlton@liblime.com>