Bug 6322 - It's possible to view lists/virtualshelves even when virtualshelves is off
[koha.git] / opac / opac-memberentry.pl
blob667f2016e707b4a8f8d60b6f0426000310b163a0
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::Borrowers;
29 use Koha::Borrower::Modifications;
30 use C4::Branch qw(GetBranchesLoop);
31 use C4::Scrubber;
32 use Email::Valid;
33 use Koha::DateUtils;
35 my $cgi = new CGI;
36 my $dbh = C4::Context->dbh;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 template_name => "opac-memberentry.tt",
41 type => "opac",
42 query => $cgi,
43 authnotrequired => 1,
47 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
49 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
50 exit;
53 my $action = $cgi->param('action') || q{};
54 if ( $action eq q{} ) {
55 if ($borrowernumber) {
56 $action = 'edit';
58 else {
59 $action = 'new';
63 my $mandatory = GetMandatoryFields($action);
64 my $hidden = GetHiddenFields($mandatory);
66 $template->param(
67 action => $action,
68 hidden => $hidden,
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);
84 if (@empty_mandatory_fields || @$invalidformfields) {
85 $template->param(
86 empty_mandatory_fields => \@empty_mandatory_fields,
87 invalid_form_fields => $invalidformfields,
88 borrower => \%borrower
91 elsif (
92 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
94 $template->param(
95 failed_captcha => 1,
96 borrower => \%borrower
99 else {
100 if (
101 C4::Context->boolean_preference(
102 'PatronSelfRegistrationVerifyByEmail')
105 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
107 template_name => "opac-registration-email-sent.tt",
108 type => "opac",
109 query => $cgi,
110 authnotrequired => 1,
113 $template->param( 'email' => $borrower{'email'} );
115 my $verification_token = md5_hex( \%borrower );
116 $borrower{'password'} = random_string("..........");
118 Koha::Borrower::Modifications->new(
119 verification_token => $verification_token )
120 ->AddModifications(\%borrower);
122 #Send verification email
123 my $letter = C4::Letters::GetPreparedLetter(
124 module => 'members',
125 letter_code => 'OPAC_REG_VERIFY',
126 tables => {
127 borrower_modifications => $verification_token,
131 C4::Letters::EnqueueLetter(
133 letter => $letter,
134 message_transport_type => 'email',
135 to_address => $borrower{'email'},
136 from_address =>
137 C4::Context->preference('KohaAdminEmailAddress'),
141 else {
142 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
144 template_name => "opac-registration-confirmation.tt",
145 type => "opac",
146 query => $cgi,
147 authnotrequired => 1,
151 $template->param( OpacPasswordChange =>
152 C4::Context->preference('OpacPasswordChange') );
154 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
155 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
157 $template->param( password_cleartext => $password );
158 $template->param(
159 borrower => GetMember( borrowernumber => $borrowernumber ) );
160 $template->param(
161 PatronSelfRegistrationAdditionalInstructions =>
162 C4::Context->preference(
163 'PatronSelfRegistrationAdditionalInstructions')
168 elsif ( $action eq 'update' ) {
170 my %borrower = ParseCgiForBorrower($cgi);
172 my %borrower_changes = DelEmptyFields(%borrower);
173 my @empty_mandatory_fields =
174 CheckMandatoryFields( \%borrower_changes, $action );
175 my $invalidformfields = CheckForInvalidFields(\%borrower);
177 if (@empty_mandatory_fields || @$invalidformfields) {
178 $template->param(
179 empty_mandatory_fields => \@empty_mandatory_fields,
180 invalid_form_fields => $invalidformfields,
181 borrower => \%borrower
184 $template->param( action => 'edit' );
186 else {
187 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
188 if (%borrower_changes) {
189 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
191 template_name => "opac-memberentry-update-submitted.tt",
192 type => "opac",
193 query => $cgi,
194 authnotrequired => 1,
198 my $m =
199 Koha::Borrower::Modifications->new(
200 borrowernumber => $borrowernumber );
202 $m->DelModifications;
203 $m->AddModifications(\%borrower_changes);
204 $template->param(
205 borrower => GetMember( borrowernumber => $borrowernumber ),
208 else {
209 $template->param(
210 action => 'edit',
211 nochanges => 1,
212 borrower => GetMember( borrowernumber => $borrowernumber ),
217 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
218 my $borrower = GetMember( borrowernumber => $borrowernumber );
220 if (C4::Context->preference('ExtendedPatronAttributes')) {
221 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
222 if (scalar(@$attributes) > 0) {
223 $borrower->{ExtendedPatronAttributes} = 1;
224 $borrower->{patron_attributes} = $attributes;
228 $template->param(
229 borrower => $borrower,
230 guarantor => Koha::Borrowers->find($borrowernumber)->guarantor(),
233 if (C4::Context->preference('OPACpatronimages')) {
234 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
235 if ($image) {
236 $template->param(
237 display_patron_image => 1
244 my $captcha = random_string("CCCCC");
246 $template->param(
247 captcha => $captcha,
248 captcha_digest => md5_base64($captcha)
251 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
253 sub GetHiddenFields {
254 my ($mandatory) = @_;
255 my %hidden_fields;
257 my $BorrowerUnwantedField =
258 C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
260 my @fields = split( /\|/, $BorrowerUnwantedField );
261 foreach (@fields) {
262 next unless m/\w/o;
263 #Don't hide mandatory fields
264 next if $mandatory->{$_};
265 $hidden_fields{$_} = 1;
268 return \%hidden_fields;
271 sub GetMandatoryFields {
272 my ($action) = @_;
274 my %mandatory_fields;
276 my $BorrowerMandatoryField =
277 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
279 my @fields = split( /\|/, $BorrowerMandatoryField );
281 foreach (@fields) {
282 $mandatory_fields{$_} = 1;
285 if ( $action eq 'create' || $action eq 'new' ) {
286 $mandatory_fields{'email'} = 1
287 if C4::Context->boolean_preference(
288 'PatronSelfRegistrationVerifyByEmail');
291 return \%mandatory_fields;
294 sub CheckMandatoryFields {
295 my ( $borrower, $action ) = @_;
297 my @empty_mandatory_fields;
299 my $mandatory_fields = GetMandatoryFields($action);
300 delete $mandatory_fields->{'cardnumber'};
302 foreach my $key ( keys %$mandatory_fields ) {
303 push( @empty_mandatory_fields, $key )
304 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
307 return @empty_mandatory_fields;
310 sub CheckForInvalidFields {
311 my $borrower = shift;
312 my @invalidFields;
313 if ($borrower->{'email'}) {
314 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
316 if ($borrower->{'emailpro'}) {
317 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
319 if ($borrower->{'B_email'}) {
320 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
322 return \@invalidFields;
325 sub ParseCgiForBorrower {
326 my ($cgi) = @_;
328 my $scrubber = C4::Scrubber->new();
329 my %borrower;
331 foreach ( $cgi->param ) {
332 if ( $_ =~ '^borrower_' ) {
333 my ($key) = substr( $_, 9 );
334 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
338 my $dob_dt;
339 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
340 if ( $borrower{'dateofbirth'} );
342 if ( $dob_dt ) {
343 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
345 else {
346 # Trigger validation
347 $borrower{'dateofbirth'} = undef;
350 return %borrower;
353 sub DelUnchangedFields {
354 my ( $borrowernumber, %new_data ) = @_;
356 my $current_data = GetMember( borrowernumber => $borrowernumber );
358 foreach my $key ( keys %new_data ) {
359 if ( $current_data->{$key} eq $new_data{$key} ) {
360 delete $new_data{$key};
364 return %new_data;
367 sub DelEmptyFields {
368 my (%borrower) = @_;
370 foreach my $key ( keys %borrower ) {
371 delete $borrower{$key} unless $borrower{$key};
374 return %borrower;