Bug 24405: Compiled CSS
[koha.git] / tools / import_borrowers.pl
blobf2b72c247747d557d1c7dcb5d9ad0edc183e7ad1
1 #!/usr/bin/perl
3 # Copyright 2007 Liblime
4 # Parts copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 # Script to take some borrowers data in a known format and load it into Koha
23 # File format
25 # cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype,
26 # address line , address line 2, city, zipcode, contry, email, phone, mobile, fax, work email, work phone,
27 # alternate streetnumber, alternate streettype, alternate address line 1, alternate city,
28 # alternate zipcode, alternate country, alternate email, alternate phone, date of birth, branchcode,
29 # categorycode, enrollment date, expiry date, noaddress, lost, debarred, contact surname,
30 # contact firstname, contact title, borrower notes, contact relationship
31 # gender, username, opac note, contact note, password, sort one, sort two
33 # any fields except cardnumber can be blank but the number of fields must match
34 # dates should be in the format you have set up Koha to expect
35 # branchcode and categorycode need to be valid
37 use Modern::Perl;
39 use C4::Auth;
40 use C4::Output;
41 use C4::Templates;
42 use Koha::Patrons;
43 use Koha::DateUtils;
44 use Koha::Token;
45 use Koha::Libraries;
46 use Koha::Patron::Categories;
47 use Koha::Patron::Attribute::Types;
48 use Koha::List::Patron;
50 use Koha::Patrons::Import;
51 my $Import = Koha::Patrons::Import->new();
53 use Text::CSV;
55 # Text::CSV::Unicode, even in binary mode, fails to parse lines with these diacriticals:
56 # ė
57 # č
59 use CGI qw ( -utf8 );
61 my ( @errors, @feedback );
62 my $extended = C4::Context->preference('ExtendedPatronAttributes');
64 my @columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } Koha::Patrons->columns();
65 push( @columnkeys, 'patron_attributes' ) if $extended;
66 push( @columnkeys, qw( relationship guarantor_id guarantor_firstname guarantor_surname ) );
68 my $input = CGI->new();
70 #push @feedback, {feedback=>1, name=>'backend', value=>$csv->backend, backend=>$csv->backend}; #XXX
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
74 template_name => "tools/import_borrowers.tt",
75 query => $input,
76 type => "intranet",
77 authnotrequired => 0,
78 flagsrequired => { tools => 'import_patrons' },
79 debug => 1,
83 # get the patron categories and pass them to the template
84 my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
85 $template->param( categories => \@patron_categories );
86 my $columns = C4::Templates::GetColumnDefs( $input )->{borrowers};
87 $columns = [ grep { $_->{field} ne 'borrowernumber' ? $_ : () } @$columns ];
88 $template->param( borrower_fields => $columns );
90 if ( $input->param('sample') ) {
91 our $csv = Text::CSV->new( { binary => 1 } ); # binary needed for non-ASCII Unicode
92 print $input->header(
93 -type => 'application/vnd.sun.xml.calc', # 'application/vnd.ms-excel' ?
94 -attachment => 'patron_import.csv',
96 $csv->combine(@columnkeys);
97 print $csv->string, "\n";
98 exit 0;
101 my $uploadborrowers = $input->param('uploadborrowers');
102 my $matchpoint = $input->param('matchpoint');
103 if ($matchpoint) {
104 $matchpoint =~ s/^patron_attribute_//;
107 #create a patronlist
108 my $createpatronlist = $input->param('createpatronlist') || 0;
109 my $dt = dt_from_string();
110 my $timestamp = $dt->ymd('-').' '.$dt->hms(':');
111 my $patronlistname = $uploadborrowers . ' (' . $timestamp .')';
113 $template->param( SCRIPT_NAME => '/cgi-bin/koha/tools/import_borrowers.pl' );
115 if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
116 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
117 unless Koha::Token->new->check_csrf({
118 session_id => scalar $input->cookie('CGISESSID'),
119 token => scalar $input->param('csrf_token'),
122 my $handle = $input->upload('uploadborrowers');
123 my %defaults = $input->Vars;
124 my $overwrite_passwords = defined $input->param('overwrite_passwords') ? 1 : 0;
125 my $return = $Import->import_patrons(
127 file => $handle,
128 defaults => \%defaults,
129 matchpoint => $matchpoint,
130 overwrite_cardnumber => $input->param('overwrite_cardnumber'),
131 overwrite_passwords => $overwrite_passwords,
132 preserve_extended_attributes => $input->param('ext_preserve') || 0,
136 my $feedback = $return->{feedback};
137 my $errors = $return->{errors};
138 my $imported = $return->{imported};
139 my $overwritten = $return->{overwritten};
140 my $alreadyindb = $return->{already_in_db};
141 my $invalid = $return->{invalid};
142 my $imported_borrowers = $return->{imported_borrowers};
144 if ( $imported && $createpatronlist ) {
145 my $patronlist = AddPatronList({ name => $patronlistname });
146 AddPatronsToList({ list => $patronlist, borrowernumbers => $imported_borrowers });
147 $template->param('patronlistname' => $patronlistname);
150 my $uploadinfo = $input->uploadInfo($uploadborrowers);
151 foreach ( keys %$uploadinfo ) {
152 push @$feedback, { feedback => 1, name => $_, value => $uploadinfo->{$_}, $_ => $uploadinfo->{$_} };
155 push @$feedback, { feedback => 1, name => 'filename', value => $uploadborrowers, filename => $uploadborrowers };
157 $template->param(
158 uploadborrowers => 1,
159 errors => $errors,
160 feedback => $feedback,
161 imported => $imported,
162 overwritten => $overwritten,
163 alreadyindb => $alreadyindb,
164 invalid => $invalid,
165 total => $imported + $alreadyindb + $invalid + $overwritten,
169 else {
170 if ($extended) {
171 my @matchpoints = ();
172 my $attribute_types = Koha::Patron::Attribute::Types->search;
174 while ( my $attr_type = $attribute_types->next ) {
175 if ( $attr_type->unique_id() ) {
176 push @matchpoints,
177 { code => "patron_attribute_" . $attr_type->code(), description => $attr_type->description() };
180 $template->param( matchpoints => \@matchpoints );
183 $template->param(
184 csrf_token => Koha::Token->new->generate_csrf(
185 { session_id => scalar $input->cookie('CGISESSID'), }
191 output_html_with_http_headers $input, $cookie, $template->output;