Bug 16847: Remove C4::Members::GetTitles
[koha.git] / opac / opac-memberentry.pl
blob7f109433a3d3e0bf870a4f48dc5c39bdc3f3cbef
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 branches => GetBranchesLoop(),
71 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
74 if ( $action eq 'create' ) {
76 my %borrower = ParseCgiForBorrower($cgi);
78 %borrower = DelEmptyFields(%borrower);
80 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
81 my $invalidformfields = CheckForInvalidFields(\%borrower);
82 delete $borrower{'password2'};
83 my $cardnumber_error_code;
84 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
85 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
86 # spurious length warning.
87 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
90 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
91 if ( $cardnumber_error_code == 1 ) {
92 $template->param( cardnumber_already_exists => 1 );
93 } elsif ( $cardnumber_error_code == 2 ) {
94 $template->param( cardnumber_wrong_length => 1 );
97 $template->param(
98 empty_mandatory_fields => \@empty_mandatory_fields,
99 invalid_form_fields => $invalidformfields,
100 borrower => \%borrower
103 elsif (
104 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
106 $template->param(
107 failed_captcha => 1,
108 borrower => \%borrower
111 else {
112 if (
113 C4::Context->boolean_preference(
114 'PatronSelfRegistrationVerifyByEmail')
117 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
119 template_name => "opac-registration-email-sent.tt",
120 type => "opac",
121 query => $cgi,
122 authnotrequired => 1,
125 $template->param( 'email' => $borrower{'email'} );
127 my $verification_token = md5_hex( \%borrower );
128 $borrower{'password'} = random_string("..........");
130 Koha::Patron::Modifications->new(
131 verification_token => $verification_token )
132 ->AddModifications(\%borrower);
134 #Send verification email
135 my $letter = C4::Letters::GetPreparedLetter(
136 module => 'members',
137 letter_code => 'OPAC_REG_VERIFY',
138 tables => {
139 borrower_modifications => $verification_token,
143 C4::Letters::EnqueueLetter(
145 letter => $letter,
146 message_transport_type => 'email',
147 to_address => $borrower{'email'},
148 from_address =>
149 C4::Context->preference('KohaAdminEmailAddress'),
153 else {
154 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
156 template_name => "opac-registration-confirmation.tt",
157 type => "opac",
158 query => $cgi,
159 authnotrequired => 1,
163 $template->param( OpacPasswordChange =>
164 C4::Context->preference('OpacPasswordChange') );
166 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
167 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
169 $template->param( password_cleartext => $password );
170 $template->param(
171 borrower => GetMember( borrowernumber => $borrowernumber ) );
172 $template->param(
173 PatronSelfRegistrationAdditionalInstructions =>
174 C4::Context->preference(
175 'PatronSelfRegistrationAdditionalInstructions')
180 elsif ( $action eq 'update' ) {
182 my %borrower = ParseCgiForBorrower($cgi);
184 my %borrower_changes = DelEmptyFields(%borrower);
185 my @empty_mandatory_fields =
186 CheckMandatoryFields( \%borrower_changes, $action );
187 my $invalidformfields = CheckForInvalidFields(\%borrower);
189 if (@empty_mandatory_fields || @$invalidformfields) {
190 $template->param(
191 empty_mandatory_fields => \@empty_mandatory_fields,
192 invalid_form_fields => $invalidformfields,
193 borrower => \%borrower
196 $template->param( action => 'edit' );
198 else {
199 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
200 if (%borrower_changes) {
201 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
203 template_name => "opac-memberentry-update-submitted.tt",
204 type => "opac",
205 query => $cgi,
206 authnotrequired => 1,
210 my $m =
211 Koha::Patron::Modifications->new(
212 borrowernumber => $borrowernumber );
214 $m->DelModifications;
215 $m->AddModifications(\%borrower_changes);
216 $template->param(
217 borrower => GetMember( borrowernumber => $borrowernumber ),
220 else {
221 $template->param(
222 action => 'edit',
223 nochanges => 1,
224 borrower => GetMember( borrowernumber => $borrowernumber ),
229 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
230 my $borrower = GetMember( borrowernumber => $borrowernumber );
232 if (C4::Context->preference('ExtendedPatronAttributes')) {
233 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
234 if (scalar(@$attributes) > 0) {
235 $borrower->{ExtendedPatronAttributes} = 1;
236 $borrower->{patron_attributes} = $attributes;
240 $template->param(
241 borrower => $borrower,
242 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
243 hidden => GetHiddenFields( $mandatory, 'modification' ),
246 if (C4::Context->preference('OPACpatronimages')) {
247 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
248 $template->param( display_patron_image => 1 ) if $patron_image;
253 my $captcha = random_string("CCCCC");
255 $template->param(
256 captcha => $captcha,
257 captcha_digest => md5_base64($captcha)
260 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
262 sub GetHiddenFields {
263 my ( $mandatory, $action ) = @_;
264 my %hidden_fields;
266 my $BorrowerUnwantedField = $action eq 'modification' ?
267 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
268 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
270 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
271 foreach (@fields) {
272 next unless m/\w/o;
273 #Don't hide mandatory fields
274 next if $mandatory->{$_};
275 $hidden_fields{$_} = 1;
278 return \%hidden_fields;
281 sub GetMandatoryFields {
282 my ($action) = @_;
284 my %mandatory_fields;
286 my $BorrowerMandatoryField =
287 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
289 my @fields = split( /\|/, $BorrowerMandatoryField );
291 foreach (@fields) {
292 $mandatory_fields{$_} = 1;
295 if ( $action eq 'create' || $action eq 'new' ) {
296 $mandatory_fields{'email'} = 1
297 if C4::Context->boolean_preference(
298 'PatronSelfRegistrationVerifyByEmail');
301 return \%mandatory_fields;
304 sub CheckMandatoryFields {
305 my ( $borrower, $action ) = @_;
307 my @empty_mandatory_fields;
309 my $mandatory_fields = GetMandatoryFields($action);
310 delete $mandatory_fields->{'cardnumber'};
312 foreach my $key ( keys %$mandatory_fields ) {
313 push( @empty_mandatory_fields, $key )
314 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
317 return @empty_mandatory_fields;
320 sub CheckForInvalidFields {
321 my $minpw = C4::Context->preference('minPasswordLength');
322 my $borrower = shift;
323 my @invalidFields;
324 if ($borrower->{'email'}) {
325 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
327 if ($borrower->{'emailpro'}) {
328 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
330 if ($borrower->{'B_email'}) {
331 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
333 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
334 push(@invalidFields, "password_match");
336 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
337 push(@invalidFields, "password_invalid");
339 if ( $borrower->{'password'} ) {
340 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
343 return \@invalidFields;
346 sub ParseCgiForBorrower {
347 my ($cgi) = @_;
349 my $scrubber = C4::Scrubber->new();
350 my %borrower;
352 foreach ( $cgi->param ) {
353 if ( $_ =~ '^borrower_' ) {
354 my ($key) = substr( $_, 9 );
355 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
359 my $dob_dt;
360 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
361 if ( $borrower{'dateofbirth'} );
363 if ( $dob_dt ) {
364 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
366 else {
367 # Trigger validation
368 $borrower{'dateofbirth'} = undef;
371 return %borrower;
374 sub DelUnchangedFields {
375 my ( $borrowernumber, %new_data ) = @_;
377 my $current_data = GetMember( borrowernumber => $borrowernumber );
379 foreach my $key ( keys %new_data ) {
380 if ( $current_data->{$key} eq $new_data{$key} ) {
381 delete $new_data{$key};
385 return %new_data;
388 sub DelEmptyFields {
389 my (%borrower) = @_;
391 foreach my $key ( keys %borrower ) {
392 delete $borrower{$key} unless $borrower{$key};
395 return %borrower;