Bug 17175: Typo in patron card images error message
[koha.git] / opac / opac-memberentry.pl
blob1a5bac49ef61c0a1d70301092543740a101777c1
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use CGI qw ( -utf8 );
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
23 use HTML::Entities;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Form::MessagingPreferences;
29 use Koha::Patrons;
30 use Koha::Patron::Modification;
31 use Koha::Patron::Modifications;
32 use C4::Branch qw(GetBranchesLoop);
33 use C4::Scrubber;
34 use Email::Valid;
35 use Koha::DateUtils;
36 use Koha::Patron::Images;
37 use Koha::Token;
39 my $cgi = new CGI;
40 my $dbh = C4::Context->dbh;
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 template_name => "opac-memberentry.tt",
45 type => "opac",
46 query => $cgi,
47 authnotrequired => 1,
51 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
53 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
54 exit;
57 my $action = $cgi->param('action') || q{};
58 if ( $action eq q{} ) {
59 if ($borrowernumber) {
60 $action = 'edit';
62 else {
63 $action = 'new';
67 my $mandatory = GetMandatoryFields($action);
69 $template->param(
70 action => $action,
71 hidden => GetHiddenFields( $mandatory, 'registration' ),
72 mandatory => $mandatory,
73 branches => GetBranchesLoop(),
74 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
77 if ( $action eq 'create' ) {
79 my %borrower = ParseCgiForBorrower($cgi);
81 %borrower = DelEmptyFields(%borrower);
83 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
84 my $invalidformfields = CheckForInvalidFields(\%borrower);
85 delete $borrower{'password2'};
86 my $cardnumber_error_code;
87 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
88 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
89 # spurious length warning.
90 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
93 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
94 if ( $cardnumber_error_code == 1 ) {
95 $template->param( cardnumber_already_exists => 1 );
96 } elsif ( $cardnumber_error_code == 2 ) {
97 $template->param( cardnumber_wrong_length => 1 );
100 $template->param(
101 empty_mandatory_fields => \@empty_mandatory_fields,
102 invalid_form_fields => $invalidformfields,
103 borrower => \%borrower
106 elsif (
107 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
109 $template->param(
110 failed_captcha => 1,
111 borrower => \%borrower
114 else {
115 if (
116 C4::Context->boolean_preference(
117 'PatronSelfRegistrationVerifyByEmail')
120 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
122 template_name => "opac-registration-email-sent.tt",
123 type => "opac",
124 query => $cgi,
125 authnotrequired => 1,
128 $template->param( 'email' => $borrower{'email'} );
130 my $verification_token = md5_hex( \%borrower );
132 $borrower{password} = random_string("..........");
133 $borrower{verification_token} = $verification_token;
135 Koha::Patron::Modification->new( \%borrower )->store();
137 #Send verification email
138 my $letter = C4::Letters::GetPreparedLetter(
139 module => 'members',
140 letter_code => 'OPAC_REG_VERIFY',
141 tables => {
142 borrower_modifications => $verification_token,
146 C4::Letters::EnqueueLetter(
148 letter => $letter,
149 message_transport_type => 'email',
150 to_address => $borrower{'email'},
151 from_address =>
152 C4::Context->preference('KohaAdminEmailAddress'),
156 else {
157 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
159 template_name => "opac-registration-confirmation.tt",
160 type => "opac",
161 query => $cgi,
162 authnotrequired => 1,
166 $template->param( OpacPasswordChange =>
167 C4::Context->preference('OpacPasswordChange') );
169 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
170 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
172 $template->param( password_cleartext => $password );
173 $template->param(
174 borrower => GetMember( borrowernumber => $borrowernumber ) );
175 $template->param(
176 PatronSelfRegistrationAdditionalInstructions =>
177 C4::Context->preference(
178 'PatronSelfRegistrationAdditionalInstructions')
183 elsif ( $action eq 'update' ) {
185 my $borrower = GetMember( borrowernumber => $borrowernumber );
186 die "Wrong CSRF token"
187 unless Koha::Token->new->check_csrf({
188 id => $borrower->{userid},
189 secret => md5_base64( C4::Context->config('pass') ),
190 token => scalar $cgi->param('csrf_token'),
193 my %borrower = ParseCgiForBorrower($cgi);
195 my %borrower_changes = DelEmptyFields(%borrower);
196 my @empty_mandatory_fields =
197 CheckMandatoryFields( \%borrower_changes, $action );
198 my $invalidformfields = CheckForInvalidFields(\%borrower);
200 # Send back the data to the template
201 %borrower = ( %$borrower, %borrower );
203 if (@empty_mandatory_fields || @$invalidformfields) {
204 $template->param(
205 empty_mandatory_fields => \@empty_mandatory_fields,
206 invalid_form_fields => $invalidformfields,
207 borrower => \%borrower,
208 csrf_token => Koha::Token->new->generate_csrf({
209 id => $borrower->{userid},
210 secret => md5_base64( C4::Context->config('pass') ),
214 $template->param( action => 'edit' );
216 else {
217 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
218 if (%borrower_changes) {
219 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
221 template_name => "opac-memberentry-update-submitted.tt",
222 type => "opac",
223 query => $cgi,
224 authnotrequired => 1,
228 $borrower_changes{borrowernumber} = $borrowernumber;
230 # FIXME update the following with
231 # Koha::Patron::Modifications->search({ borrowernumber => $borrowernumber })->delete;
232 # when bug 17091 will be pushed
233 my $patron_modifications = Koha::Patron::Modifications->search({ borrowernumber => $borrowernumber });
234 while ( my $patron_modification = $patron_modifications->next ) {
235 $patron_modification->delete;
238 my $m = Koha::Patron::Modification->new( \%borrower_changes )->store();
240 $template->param(
241 borrower => GetMember( borrowernumber => $borrowernumber ),
244 else {
245 $template->param(
246 action => 'edit',
247 nochanges => 1,
248 borrower => GetMember( borrowernumber => $borrowernumber ),
249 csrf_token => Koha::Token->new->generate_csrf({
250 id => $borrower->{userid},
251 secret => md5_base64( C4::Context->config('pass') ),
257 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
258 my $borrower = GetMember( borrowernumber => $borrowernumber );
260 if (C4::Context->preference('ExtendedPatronAttributes')) {
261 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
262 if (scalar(@$attributes) > 0) {
263 $borrower->{ExtendedPatronAttributes} = 1;
264 $borrower->{patron_attributes} = $attributes;
268 $template->param(
269 borrower => $borrower,
270 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
271 hidden => GetHiddenFields( $mandatory, 'modification' ),
272 csrf_token => Koha::Token->new->generate_csrf({
273 id => $borrower->{userid},
274 secret => md5_base64( C4::Context->config('pass') ),
278 if (C4::Context->preference('OPACpatronimages')) {
279 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
280 $template->param( display_patron_image => 1 ) if $patron_image;
285 my $captcha = random_string("CCCCC");
287 $template->param(
288 captcha => $captcha,
289 captcha_digest => md5_base64($captcha)
292 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
294 sub GetHiddenFields {
295 my ( $mandatory, $action ) = @_;
296 my %hidden_fields;
298 my $BorrowerUnwantedField = $action eq 'modification' ?
299 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
300 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
302 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
303 foreach (@fields) {
304 next unless m/\w/o;
305 #Don't hide mandatory fields
306 next if $mandatory->{$_};
307 $hidden_fields{$_} = 1;
310 return \%hidden_fields;
313 sub GetMandatoryFields {
314 my ($action) = @_;
316 my %mandatory_fields;
318 my $BorrowerMandatoryField =
319 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
321 my @fields = split( /\|/, $BorrowerMandatoryField );
323 foreach (@fields) {
324 $mandatory_fields{$_} = 1;
327 if ( $action eq 'create' || $action eq 'new' ) {
328 $mandatory_fields{'email'} = 1
329 if C4::Context->boolean_preference(
330 'PatronSelfRegistrationVerifyByEmail');
333 return \%mandatory_fields;
336 sub CheckMandatoryFields {
337 my ( $borrower, $action ) = @_;
339 my @empty_mandatory_fields;
341 my $mandatory_fields = GetMandatoryFields($action);
342 delete $mandatory_fields->{'cardnumber'};
344 foreach my $key ( keys %$mandatory_fields ) {
345 push( @empty_mandatory_fields, $key )
346 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
349 return @empty_mandatory_fields;
352 sub CheckForInvalidFields {
353 my $minpw = C4::Context->preference('minPasswordLength');
354 my $borrower = shift;
355 my @invalidFields;
356 if ($borrower->{'email'}) {
357 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
359 if ($borrower->{'emailpro'}) {
360 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
362 if ($borrower->{'B_email'}) {
363 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
365 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
366 push(@invalidFields, "password_match");
368 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
369 push(@invalidFields, "password_invalid");
371 if ( $borrower->{'password'} ) {
372 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
375 return \@invalidFields;
378 sub ParseCgiForBorrower {
379 my ($cgi) = @_;
381 my $scrubber = C4::Scrubber->new();
382 my %borrower;
384 foreach ( $cgi->param ) {
385 if ( $_ =~ '^borrower_' ) {
386 my ($key) = substr( $_, 9 );
387 $borrower{$key} = HTML::Entities::encode( $scrubber->scrub( scalar $cgi->param($_) ) );
391 my $dob_dt;
392 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
393 if ( $borrower{'dateofbirth'} );
395 if ( $dob_dt ) {
396 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
398 else {
399 # Trigger validation
400 $borrower{'dateofbirth'} = undef;
403 return %borrower;
406 sub DelUnchangedFields {
407 my ( $borrowernumber, %new_data ) = @_;
409 my $current_data = GetMember( borrowernumber => $borrowernumber );
411 foreach my $key ( keys %new_data ) {
412 if ( $current_data->{$key} eq $new_data{$key} ) {
413 delete $new_data{$key};
417 return %new_data;
420 sub DelEmptyFields {
421 my (%borrower) = @_;
423 foreach my $key ( keys %borrower ) {
424 delete $borrower{$key} unless $borrower{$key};
427 return %borrower;