3 # Copyright 2011 BibLibre
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>.
20 # Script to export borrowers
24 use Getopt
::Long
qw(:config no_ignore_case);
30 binmode STDOUT
, ":encoding(UTF-8)";
33 ( my $basename = $0 ) =~ s
|.*/||;
37 Export patron informations in CSV format.
38 It prints to standard output. Use redirection to save CSV in a file.
41 $0 [--field=FIELD [--field=FIELD [...]]] [--separator=CHAR] [--show-header] [--where=CONDITION]
44 -f, --field=FIELD Field to export. It is repeatable and has to match
45 column names of the borrower table (also as 'description' and 'category_type'
46 If no field is specified, then all fields will be
48 -s, --separator=CHAR This character will be used to separate fields.
49 Some characters like | or ; will need to be escaped
50 in the parameter setting, like -s=\\| or -s=\\;
51 If no separator is specified, the delimiter pref
52 will be used (or a comma, if the pref is empty)
53 -H, --show-header Print field names on first row
54 -w, --where=CONDITION Condition to filter borrowers to export
56 CONDITION must be enclosed by double quotes
57 You can use single quotes around a field value
58 within the condition like:
59 --where "surname='De Lattre'"
60 -h, --help Show this help
73 'field|f=s' => \
@fields,
74 'separator|s=s' => \
$separator,
75 'show-header|H' => \
$show_header,
76 'where|w=s' => \
$where,
78 ) or print_usage
, exit 1;
86 my $dbh = C4
::Context
->dbh;
87 my $query = "SELECT borrowernumber FROM borrowers";
88 $query .= " WHERE $where" if ($where);
89 $query .= " ORDER BY borrowernumber";
90 my $sth = $dbh->prepare($query);
93 unless ( $separator ) {
94 $separator = C4
::Context
->preference('delimiter') || ',';
95 $separator = "\t" if ($separator eq 'tabulation');
98 my $csv = Text
::CSV
->new( { sep_char
=> $separator, binary
=> 1 } );
100 # If the user did not specify any field to export, we assume they want them all
101 # We retrieve the first borrower informations to get field names
102 my ($borrowernumber) = $sth->fetchrow_array or die "No borrower to export";
103 my $patron = Koha
::Patrons
->find( $borrowernumber ); # FIXME Now is_expired is no longer available
104 # We will have to use Koha::Patron and allow method calls
105 my $category = $patron->category;
106 my $member = $patron->unblessed;
107 $member->{description
} = $category->description;
108 $member->{category_type
} = $category->category_type;
110 @fields = keys %$member unless (@fields);
113 $csv->combine(@fields);
114 print $csv->string . "\n";
119 ( defined $member->{$_} and !ref $member->{$_} )
124 die "Invalid character at borrower $borrowernumber: ["
125 . $csv->error_input . "]\n"
126 if ( !defined( $csv->string ) );
127 print $csv->string . "\n";
129 while ( my $borrowernumber = $sth->fetchrow_array ) {
130 my $patron = Koha
::Patrons
->find( $borrowernumber );
131 my $category = $patron->category;
132 my $member = $patron->unblessed;
133 $member->{description
} = $category->description;
134 $member->{category_type
} = $category->category_type;
137 ( defined $member->{$_} and !ref $member->{$_} )
142 die "Invalid character at borrower $borrowernumber: ["
143 . $csv->error_input . "]\n"
144 if ( !defined( $csv->string ) );
145 print $csv->string . "\n";