Bug 13669: Re-adds error handling to load_sql
[koha.git] / opac / opac-memberentry.pl
blob75ecdc33f9f889adcd825556d65cf3c7ef0f28e4
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 );
24 use C4::Auth;
25 use C4::Output;
26 use C4::Members;
27 use C4::Form::MessagingPreferences;
28 use Koha::Patrons;
29 use Koha::Patron::Modifications;
30 use C4::Branch qw(GetBranchesLoop);
31 use C4::Scrubber;
32 use Email::Valid;
33 use Koha::DateUtils;
34 use Koha::Patron::Images;
36 my $cgi = new CGI;
37 my $dbh = C4::Context->dbh;
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41 template_name => "opac-memberentry.tt",
42 type => "opac",
43 query => $cgi,
44 authnotrequired => 1,
48 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
50 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
51 exit;
54 my $action = $cgi->param('action') || q{};
55 if ( $action eq q{} ) {
56 if ($borrowernumber) {
57 $action = 'edit';
59 else {
60 $action = 'new';
64 my $mandatory = GetMandatoryFields($action);
66 $template->param(
67 action => $action,
68 hidden => GetHiddenFields( $mandatory, 'registration' ),
69 mandatory => $mandatory,
70 member_titles => GetTitles() || undef,
71 branches => GetBranchesLoop(),
72 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
75 if ( $action eq 'create' ) {
77 my %borrower = ParseCgiForBorrower($cgi);
79 %borrower = DelEmptyFields(%borrower);
81 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
82 my $invalidformfields = CheckForInvalidFields(\%borrower);
83 delete $borrower{'password2'};
84 my $cardnumber_error_code;
85 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
86 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
87 # spurious length warning.
88 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
91 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
92 if ( $cardnumber_error_code == 1 ) {
93 $template->param( cardnumber_already_exists => 1 );
94 } elsif ( $cardnumber_error_code == 2 ) {
95 $template->param( cardnumber_wrong_length => 1 );
98 $template->param(
99 empty_mandatory_fields => \@empty_mandatory_fields,
100 invalid_form_fields => $invalidformfields,
101 borrower => \%borrower
104 elsif (
105 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
107 $template->param(
108 failed_captcha => 1,
109 borrower => \%borrower
112 else {
113 if (
114 C4::Context->boolean_preference(
115 'PatronSelfRegistrationVerifyByEmail')
118 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
120 template_name => "opac-registration-email-sent.tt",
121 type => "opac",
122 query => $cgi,
123 authnotrequired => 1,
126 $template->param( 'email' => $borrower{'email'} );
128 my $verification_token = md5_hex( \%borrower );
129 $borrower{'password'} = random_string("..........");
131 Koha::Patron::Modifications->new(
132 verification_token => $verification_token )
133 ->AddModifications(\%borrower);
135 #Send verification email
136 my $letter = C4::Letters::GetPreparedLetter(
137 module => 'members',
138 letter_code => 'OPAC_REG_VERIFY',
139 tables => {
140 borrower_modifications => $verification_token,
144 C4::Letters::EnqueueLetter(
146 letter => $letter,
147 message_transport_type => 'email',
148 to_address => $borrower{'email'},
149 from_address =>
150 C4::Context->preference('KohaAdminEmailAddress'),
154 else {
155 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
157 template_name => "opac-registration-confirmation.tt",
158 type => "opac",
159 query => $cgi,
160 authnotrequired => 1,
164 $template->param( OpacPasswordChange =>
165 C4::Context->preference('OpacPasswordChange') );
167 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
168 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
170 $template->param( password_cleartext => $password );
171 $template->param(
172 borrower => GetMember( borrowernumber => $borrowernumber ) );
173 $template->param(
174 PatronSelfRegistrationAdditionalInstructions =>
175 C4::Context->preference(
176 'PatronSelfRegistrationAdditionalInstructions')
181 elsif ( $action eq 'update' ) {
183 my %borrower = ParseCgiForBorrower($cgi);
185 my %borrower_changes = DelEmptyFields(%borrower);
186 my @empty_mandatory_fields =
187 CheckMandatoryFields( \%borrower_changes, $action );
188 my $invalidformfields = CheckForInvalidFields(\%borrower);
190 if (@empty_mandatory_fields || @$invalidformfields) {
191 $template->param(
192 empty_mandatory_fields => \@empty_mandatory_fields,
193 invalid_form_fields => $invalidformfields,
194 borrower => \%borrower
197 $template->param( action => 'edit' );
199 else {
200 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
201 if (%borrower_changes) {
202 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
204 template_name => "opac-memberentry-update-submitted.tt",
205 type => "opac",
206 query => $cgi,
207 authnotrequired => 1,
211 my $m =
212 Koha::Patron::Modifications->new(
213 borrowernumber => $borrowernumber );
215 $m->DelModifications;
216 $m->AddModifications(\%borrower_changes);
217 $template->param(
218 borrower => GetMember( borrowernumber => $borrowernumber ),
221 else {
222 $template->param(
223 action => 'edit',
224 nochanges => 1,
225 borrower => GetMember( borrowernumber => $borrowernumber ),
230 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
231 my $borrower = GetMember( borrowernumber => $borrowernumber );
233 if (C4::Context->preference('ExtendedPatronAttributes')) {
234 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
235 if (scalar(@$attributes) > 0) {
236 $borrower->{ExtendedPatronAttributes} = 1;
237 $borrower->{patron_attributes} = $attributes;
241 $template->param(
242 borrower => $borrower,
243 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
244 hidden => GetHiddenFields( $mandatory, 'modification' ),
247 if (C4::Context->preference('OPACpatronimages')) {
248 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
249 $template->param( display_patron_image => 1 ) if $patron_image;
254 my $captcha = random_string("CCCCC");
256 $template->param(
257 captcha => $captcha,
258 captcha_digest => md5_base64($captcha)
261 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
263 sub GetHiddenFields {
264 my ( $mandatory, $action ) = @_;
265 my %hidden_fields;
267 my $BorrowerUnwantedField = $action eq 'modification' ?
268 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
269 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
271 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
272 foreach (@fields) {
273 next unless m/\w/o;
274 #Don't hide mandatory fields
275 next if $mandatory->{$_};
276 $hidden_fields{$_} = 1;
279 return \%hidden_fields;
282 sub GetMandatoryFields {
283 my ($action) = @_;
285 my %mandatory_fields;
287 my $BorrowerMandatoryField =
288 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
290 my @fields = split( /\|/, $BorrowerMandatoryField );
292 foreach (@fields) {
293 $mandatory_fields{$_} = 1;
296 if ( $action eq 'create' || $action eq 'new' ) {
297 $mandatory_fields{'email'} = 1
298 if C4::Context->boolean_preference(
299 'PatronSelfRegistrationVerifyByEmail');
302 return \%mandatory_fields;
305 sub CheckMandatoryFields {
306 my ( $borrower, $action ) = @_;
308 my @empty_mandatory_fields;
310 my $mandatory_fields = GetMandatoryFields($action);
311 delete $mandatory_fields->{'cardnumber'};
313 foreach my $key ( keys %$mandatory_fields ) {
314 push( @empty_mandatory_fields, $key )
315 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
318 return @empty_mandatory_fields;
321 sub CheckForInvalidFields {
322 my $minpw = C4::Context->preference('minPasswordLength');
323 my $borrower = shift;
324 my @invalidFields;
325 if ($borrower->{'email'}) {
326 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
328 if ($borrower->{'emailpro'}) {
329 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
331 if ($borrower->{'B_email'}) {
332 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
334 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
335 push(@invalidFields, "password_match");
337 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
338 push(@invalidFields, "password_invalid");
340 if ( $borrower->{'password'} ) {
341 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
344 return \@invalidFields;
347 sub ParseCgiForBorrower {
348 my ($cgi) = @_;
350 my $scrubber = C4::Scrubber->new();
351 my %borrower;
353 foreach ( $cgi->param ) {
354 if ( $_ =~ '^borrower_' ) {
355 my ($key) = substr( $_, 9 );
356 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
360 my $dob_dt;
361 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
362 if ( $borrower{'dateofbirth'} );
364 if ( $dob_dt ) {
365 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
367 else {
368 # Trigger validation
369 $borrower{'dateofbirth'} = undef;
372 return %borrower;
375 sub DelUnchangedFields {
376 my ( $borrowernumber, %new_data ) = @_;
378 my $current_data = GetMember( borrowernumber => $borrowernumber );
380 foreach my $key ( keys %new_data ) {
381 if ( $current_data->{$key} eq $new_data{$key} ) {
382 delete $new_data{$key};
386 return %new_data;
389 sub DelEmptyFields {
390 my (%borrower) = @_;
392 foreach my $key ( keys %borrower ) {
393 delete $borrower{$key} unless $borrower{$key};
396 return %borrower;