use C4::Auth;
use C4::Output;
use C4::Context;
-use C4::Branch qw/GetBranchesLoop GetBranchName/;
use C4::Members;
use C4::Members::Attributes qw(:all);
use C4::Members::AttributeTypes;
use C4::Reports::Guided;
use C4::Templates;
use Koha::Patron::Debarments;
+use Koha::Patrons;
use Koha::DateUtils;
+use Koha::Token;
+use Koha::Libraries;
+use Koha::Patron::Categories;
use Text::CSV;
# Text::CSV::Unicode, even in binary mode, fails to parse lines with these diacriticals:
# č
use CGI qw ( -utf8 );
-# use encoding 'utf8'; # don't do this
+use Digest::MD5 qw(md5_base64);
+use Encode qw( encode );
my (@errors, @feedback);
my $extended = C4::Context->preference('ExtendedPatronAttributes');
my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences');
-my @columnkeys = C4::Members::columns();
+my @columnkeys = Koha::Patrons->columns();
@columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } @columnkeys;
if ($extended) {
push @columnkeys, 'patron_attributes';
debug => 1,
});
-# get the branches and pass them to the template
-my $branches = GetBranchesLoop();
-$template->param( branches => $branches ) if ( $branches );
# get the patron categories and pass them to the template
-my $categories = GetBorrowercategoryList();
-$template->param( categories => $categories ) if ( $categories );
+my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+$template->param( categories => \@patron_categories );
my $columns = C4::Templates::GetColumnDefs( $input )->{borrowers};
$columns = [ grep { $_->{field} ne 'borrowernumber' ? $_ : () } @$columns ];
$template->param( borrower_fields => $columns );
$template->param( SCRIPT_NAME => '/cgi-bin/koha/tools/import_borrowers.pl' );
if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
+ die "Wrong CSRF token"
+ unless Koha::Token->new->check_csrf({
+ id => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
+ secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
+ token => scalar $input->param('csrf_token'),
+ });
+
push @feedback, {feedback=>1, name=>'filename', value=>$uploadborrowers, filename=>$uploadborrowers};
my $handle = $input->upload('uploadborrowers');
my $uploadinfo = $input->uploadInfo($uploadborrowers);
}
push @feedback, {feedback=>1, name=>'headerrow', value=>join(', ', @csvcolumns)};
- my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
+ my $today = output_pref;
my @criticals = qw(surname branchcode categorycode); # there probably should be others
my @bad_dates; # I've had a few.
LINE: while ( my $borrowerline = <$handle> ) {
#warn join(':',%borrower);
if ($borrower{categorycode}) {
push @missing_criticals, {key=>'categorycode', line=>$. , lineraw=>$borrowerline, value=>$borrower{categorycode}, category_map=>1}
- unless GetBorrowercategory($borrower{categorycode});
+ unless Koha::Patron::Categories->find($borrower{categorycode});
} else {
push @missing_criticals, {key=>'categorycode', line=>$. , lineraw=>$borrowerline};
}
if ($borrower{branchcode}) {
push @missing_criticals, {key=>'branchcode', line=>$. , lineraw=>$borrowerline, value=>$borrower{branchcode}, branch_map=>1}
- unless GetBranchName($borrower{branchcode});
+ unless Koha::Libraries->find($borrower{branchcode});
} else {
push @missing_criticals, {key=>'branchcode', line=>$. , lineraw=>$borrowerline};
}
push @missing_criticals, {key=>$_, line=>$. , lineraw=>$borrowerline, bad_date=>1};
}
}
- $borrower{dateenrolled} = $today_iso unless $borrower{dateenrolled};
- $borrower{dateexpiry} = GetExpiryDate($borrower{categorycode},$borrower{dateenrolled}) unless $borrower{dateexpiry};
+ $borrower{dateenrolled} ||= $today;
+ $borrower{dateexpiry} ||= Koha::Patron::Categories->find( $borrower{categorycode} )->get_expiry_date( $borrower{dateenrolled} );
my $borrowernumber;
my $member;
if ( ($matchpoint eq 'cardnumber') && ($borrower{'cardnumber'}) ) {
next;
}
-
if ($borrowernumber) {
# borrower exists
unless ($overwrite_cardnumber) {
$borrower{$col} = $member->{$col} if($member->{$col}) ;
}
}
+
+ # Check if the userid provided does not exist yet
+ if ( exists $borrower{userid}
+ and $borrower{userid}
+ and not Check_Userid( $borrower{userid}, $borrower{borrowernumber} ) ) {
+ push @errors, { duplicate_userid => 1, userid => $borrower{userid} };
+ $invalid++;
+ next LINE;
+ }
+
unless (ModMember(%borrower)) {
$invalid++;
# until we have better error trapping, we have no way of knowing why ModMember errored out...
}
$template->param(matchpoints => \@matchpoints);
}
+
+ $template->param(
+ csrf_token => Koha::Token->new->generate_csrf(
+ { id => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
+ secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
+ }
+ ),
+ );
+
}
output_html_with_http_headers $input, $cookie, $template->output;