127ad4e735136f31a34492944a361da711155034
[koha.git] / 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
35                     );
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 extended_attributes_code_value_arrayref 
50
51    my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
52    my $aref = extended_attributes_code_value_arrayref($patron_attributes);
53
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' } ... ]
57
58 Caches Text::CSV parser object for efficiency.
59
60 =cut
61
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)
69     return [
70         sort {&_sort_by_code($a,$b)}
71         map { map { my @arr = split /:/, $_, 2; { code => $arr[0], attribute => $arr[1] } } $_ }
72         @list
73     ];
74     # nested map because of split
75 }
76
77 sub _sort_by_code {
78     my ($x, $y) = @_;
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};
82 }
83
84 =head1 AUTHOR
85
86 Koha Development Team <http://koha-community.org/>
87
88 Galen Charlton <galen.charlton@liblime.com>
89
90 =cut
91
92 1;