Bug 15635: Koha::Patron::Images - Remove GetPatronImage
[koha.git] / opac / opac-memberentry.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
23
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;
35
36 my $cgi = new CGI;
37 my $dbh = C4::Context->dbh;
38
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40     {
41         template_name   => "opac-memberentry.tt",
42         type            => "opac",
43         query           => $cgi,
44         authnotrequired => 1,
45     }
46 );
47
48 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
49 {
50     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
51     exit;
52 }
53
54 my $action = $cgi->param('action') || q{};
55 if ( $action eq q{} ) {
56     if ($borrowernumber) {
57         $action = 'edit';
58     }
59     else {
60         $action = 'new';
61     }
62 }
63
64 my $mandatory = GetMandatoryFields($action);
65
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'),
73 );
74
75 if ( $action eq 'create' ) {
76
77     my %borrower = ParseCgiForBorrower($cgi);
78
79     %borrower = DelEmptyFields(%borrower);
80
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} );
89     }
90
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 );
96         }
97
98         $template->param(
99             empty_mandatory_fields => \@empty_mandatory_fields,
100             invalid_form_fields    => $invalidformfields,
101             borrower               => \%borrower
102         );
103     }
104     elsif (
105         md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
106     {
107         $template->param(
108             failed_captcha => 1,
109             borrower       => \%borrower
110         );
111     }
112     else {
113         if (
114             C4::Context->boolean_preference(
115                 'PatronSelfRegistrationVerifyByEmail')
116           )
117         {
118             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
119                 {
120                     template_name   => "opac-registration-email-sent.tt",
121                     type            => "opac",
122                     query           => $cgi,
123                     authnotrequired => 1,
124                 }
125             );
126             $template->param( 'email' => $borrower{'email'} );
127
128             my $verification_token = md5_hex( \%borrower );
129             $borrower{'password'} = random_string("..........");
130
131             Koha::Patron::Modifications->new(
132                 verification_token => $verification_token )
133               ->AddModifications(\%borrower);
134
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,
141                 },
142             );
143
144             C4::Letters::EnqueueLetter(
145                 {
146                     letter                 => $letter,
147                     message_transport_type => 'email',
148                     to_address             => $borrower{'email'},
149                     from_address =>
150                       C4::Context->preference('KohaAdminEmailAddress'),
151                 }
152             );
153         }
154         else {
155             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
156                 {
157                     template_name   => "opac-registration-confirmation.tt",
158                     type            => "opac",
159                     query           => $cgi,
160                     authnotrequired => 1,
161                 }
162             );
163
164             $template->param( OpacPasswordChange =>
165                   C4::Context->preference('OpacPasswordChange') );
166
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');
169
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')
177             );
178         }
179     }
180 }
181 elsif ( $action eq 'update' ) {
182
183     my %borrower = ParseCgiForBorrower($cgi);
184
185     my %borrower_changes = DelEmptyFields(%borrower);
186     my @empty_mandatory_fields =
187       CheckMandatoryFields( \%borrower_changes, $action );
188     my $invalidformfields = CheckForInvalidFields(\%borrower);
189
190     if (@empty_mandatory_fields || @$invalidformfields) {
191         $template->param(
192             empty_mandatory_fields => \@empty_mandatory_fields,
193             invalid_form_fields    => $invalidformfields,
194             borrower               => \%borrower
195         );
196
197         $template->param( action => 'edit' );
198     }
199     else {
200         my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
201         if (%borrower_changes) {
202             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
203                 {
204                     template_name   => "opac-memberentry-update-submitted.tt",
205                     type            => "opac",
206                     query           => $cgi,
207                     authnotrequired => 1,
208                 }
209             );
210
211             my $m =
212               Koha::Patron::Modifications->new(
213                 borrowernumber => $borrowernumber );
214
215             $m->DelModifications;
216             $m->AddModifications(\%borrower_changes);
217             $template->param(
218                 borrower => GetMember( borrowernumber => $borrowernumber ),
219             );
220         }
221         else {
222             $template->param(
223                 action => 'edit',
224                 nochanges => 1,
225                 borrower => GetMember( borrowernumber => $borrowernumber ),
226             );
227         }
228     }
229 }
230 elsif ( $action eq 'edit' ) {    #Display logged in borrower's data
231     my $borrower = GetMember( borrowernumber => $borrowernumber );
232
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;
238         }
239     }
240
241     $template->param(
242         borrower  => $borrower,
243         guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
244         hidden => GetHiddenFields( $mandatory, 'modification' ),
245     );
246
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;
250     }
251
252 }
253
254 my $captcha = random_string("CCCCC");
255
256 $template->param(
257     captcha        => $captcha,
258     captcha_digest => md5_base64($captcha)
259 );
260
261 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
262
263 sub GetHiddenFields {
264     my ( $mandatory, $action ) = @_;
265     my %hidden_fields;
266
267     my $BorrowerUnwantedField = $action eq 'modification' ?
268       C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
269       C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
270
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;
277     }
278
279     return \%hidden_fields;
280 }
281
282 sub GetMandatoryFields {
283     my ($action) = @_;
284
285     my %mandatory_fields;
286
287     my $BorrowerMandatoryField =
288       C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
289
290     my @fields = split( /\|/, $BorrowerMandatoryField );
291
292     foreach (@fields) {
293         $mandatory_fields{$_} = 1;
294     }
295
296     if ( $action eq 'create' || $action eq 'new' ) {
297         $mandatory_fields{'email'} = 1
298           if C4::Context->boolean_preference(
299             'PatronSelfRegistrationVerifyByEmail');
300     }
301
302     return \%mandatory_fields;
303 }
304
305 sub CheckMandatoryFields {
306     my ( $borrower, $action ) = @_;
307
308     my @empty_mandatory_fields;
309
310     my $mandatory_fields = GetMandatoryFields($action);
311     delete $mandatory_fields->{'cardnumber'};
312
313     foreach my $key ( keys %$mandatory_fields ) {
314         push( @empty_mandatory_fields, $key )
315           unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
316     }
317
318     return @empty_mandatory_fields;
319 }
320
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'}));
327     }
328     if ($borrower->{'emailpro'}) {
329         push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
330     }
331     if ($borrower->{'B_email'}) {
332         push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
333     }
334     if ( $borrower->{'password'} ne $borrower->{'password2'} ){
335         push(@invalidFields, "password_match");
336     }
337     if ( $borrower->{'password'}  && $minpw && (length($borrower->{'password'}) < $minpw) ) {
338        push(@invalidFields, "password_invalid");
339     }
340     if ( $borrower->{'password'} ) {
341        push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
342     }
343
344     return \@invalidFields;
345 }
346
347 sub ParseCgiForBorrower {
348     my ($cgi) = @_;
349
350     my $scrubber = C4::Scrubber->new();
351     my %borrower;
352
353     foreach ( $cgi->param ) {
354         if ( $_ =~ '^borrower_' ) {
355             my ($key) = substr( $_, 9 );
356             $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
357         }
358     }
359
360     my $dob_dt;
361     $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
362         if ( $borrower{'dateofbirth'} );
363
364     if ( $dob_dt ) {
365         $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
366     }
367     else {
368         # Trigger validation
369         $borrower{'dateofbirth'} = undef;
370     }
371
372     return %borrower;
373 }
374
375 sub DelUnchangedFields {
376     my ( $borrowernumber, %new_data ) = @_;
377
378     my $current_data = GetMember( borrowernumber => $borrowernumber );
379
380     foreach my $key ( keys %new_data ) {
381         if ( $current_data->{$key} eq $new_data{$key} ) {
382             delete $new_data{$key};
383         }
384     }
385
386     return %new_data;
387 }
388
389 sub DelEmptyFields {
390     my (%borrower) = @_;
391
392     foreach my $key ( keys %borrower ) {
393         delete $borrower{$key} unless $borrower{$key};
394     }
395
396     return %borrower;
397 }