Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2011 BibLibre |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
# Script to export borrowers |
21 |
|
22 |
use Modern::Perl; |
23 |
use Text::CSV; |
24 |
use Getopt::Long qw(:config no_ignore_case); |
25 |
|
26 |
use C4::Context; |
27 |
use C4::Members; |
28 |
|
29 |
use encoding 'utf8'; |
30 |
|
31 |
sub print_usage { |
32 |
( my $basename = $0 ) =~ s|.*/||; |
33 |
print <<USAGE; |
34 |
|
35 |
$basename |
36 |
Export patron informations in CSV format. |
37 |
It prints to standard output. Use redirection to save CSV in a file. |
38 |
|
39 |
Usage: |
40 |
$0 [--field=FIELD [--field=FIELD [...]]] [--show-header] |
41 |
$0 -h |
42 |
|
43 |
-f, --field=FIELD Field to export. It is repeatable and has to match |
44 |
keys returned by the GetMemberDetails function. |
45 |
If no field is specified, then all fields will be |
46 |
exported. |
47 |
-H, --show-header Print field names on first row |
48 |
-h, --help Show this help |
49 |
|
50 |
USAGE |
51 |
} |
52 |
|
53 |
# Getting parameters |
54 |
my @fields; |
55 |
my $show_header; |
56 |
my $help; |
57 |
|
58 |
GetOptions( |
59 |
'field|f=s' => \@fields, |
60 |
'show-header|H' => \$show_header, |
61 |
'help|h' => \$help |
62 |
) or print_usage, exit 1; |
63 |
|
64 |
if ($help) { |
65 |
print_usage; |
66 |
exit; |
67 |
} |
68 |
|
69 |
# Getting borrowers |
70 |
my $dbh = C4::Context->dbh; |
71 |
my $query = "SELECT borrowernumber FROM borrowers ORDER BY borrowernumber"; |
72 |
my $sth = $dbh->prepare($query); |
73 |
$sth->execute; |
74 |
|
75 |
my $csv = Text::CSV->new( { binary => 1 } ); |
76 |
|
77 |
# If the user did not specify any field to export, we assume he wants them all |
78 |
# We retrieve the first borrower informations to get field names |
79 |
my ($borrowernumber) = $sth->fetchrow_array; |
80 |
my $member = GetMemberDetails($borrowernumber); |
81 |
@fields = keys %$member unless (@fields); |
82 |
|
83 |
if ($show_header) { |
84 |
$csv->combine(@fields); |
85 |
print $csv->string . "\n"; |
86 |
} |
87 |
|
88 |
$csv->combine( |
89 |
map { |
90 |
( defined $member->{$_} and !ref $member->{$_} ) |
91 |
? $member->{$_} |
92 |
: '' |
93 |
} @fields |
94 |
); |
95 |
die "Invalid character at borrower $borrowernumber: [" |
96 |
. $csv->error_input . "]\n" |
97 |
if ( !defined( $csv->string ) ); |
98 |
print $csv->string . "\n"; |
99 |
|
100 |
while ( my $borrowernumber = $sth->fetchrow_array ) { |
101 |
$member = GetMemberDetails($borrowernumber); |
102 |
$csv->combine( |
103 |
map { |
104 |
( defined $member->{$_} and !ref $member->{$_} ) |
105 |
? $member->{$_} |
106 |
: '' |
107 |
} @fields |
108 |
); |
109 |
die "Invalid character at borrower $borrowernumber: [" |
110 |
. $csv->error_input . "]\n" |
111 |
if ( !defined( $csv->string ) ); |
112 |
print $csv->string . "\n"; |
113 |
} |
114 |
|