#!/usr/bin/perl # Copyright 2011 BibLibre # # This file is part of Koha. # # Koha is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # Koha is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Koha; if not, see . # Script to export borrowers use Modern::Perl; use Text::CSV; use Getopt::Long qw( GetOptions ); use Koha::Script; use C4::Context; use Koha::Patrons; binmode STDOUT, ":encoding(UTF-8)"; sub print_usage { ( my $basename = $0 ) =~ s|.*/||; print < \@fields, 'separator|s=s' => \$separator, 'show-header|H' => \$show_header, 'where|w=s' => \$where, 'help|h' => \$help ) or print_usage, exit 1; if ($help) { print_usage; exit; } # Getting borrowers my $dbh = C4::Context->dbh; my $query = "SELECT borrowernumber FROM borrowers"; $query .= " WHERE $where" if ($where); $query .= " ORDER BY borrowernumber"; my $sth = $dbh->prepare($query); $sth->execute; unless ( $separator ) { $separator = C4::Context->preference('CSVDelimiter') || ','; $separator = "\t" if ($separator eq 'tabulation'); } my $csv = Text::CSV->new( { sep_char => $separator, binary => 1 } ); # If the user did not specify any field to export, we assume they want them all # We retrieve the first borrower informations to get field names my ($borrowernumber) = $sth->fetchrow_array or die "No borrower to export"; my $patron = Koha::Patrons->find( $borrowernumber ); # FIXME Now is_expired is no longer available # We will have to use Koha::Patron and allow method calls my $category = $patron->category; my $member = $patron->unblessed; $member->{description} = $category->description; $member->{category_type} = $category->category_type; @fields = keys %$member unless (@fields); if ($show_header) { $csv->combine(@fields); print $csv->string . "\n"; } $csv->combine( map { ( defined $member->{$_} and !ref $member->{$_} ) ? $member->{$_} : '' } @fields ); die "Invalid character at borrower $borrowernumber: [" . $csv->error_input . "]\n" if ( !defined( $csv->string ) ); print $csv->string . "\n"; while ( my $borrowernumber = $sth->fetchrow_array ) { my $patron = Koha::Patrons->find( $borrowernumber ); my $category = $patron->category; my $member = $patron->unblessed; $member->{description} = $category->description; $member->{category_type} = $category->category_type; $csv->combine( map { ( defined $member->{$_} and !ref $member->{$_} ) ? $member->{$_} : '' } @fields ); die "Invalid character at borrower $borrowernumber: [" . $csv->error_input . "]\n" if ( !defined( $csv->string ) ); print $csv->string . "\n"; }