Bug 16220 - The view tabs on opac-detail.pl are not responsive
[koha.git] / opac / opac-memberentry.pl
blobddaf914110d99c79fa8ef296ad4bfd280a8166ef
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::Borrower::Modifications;
29 use C4::Branch qw(GetBranchesLoop);
30 use C4::Scrubber;
31 use Email::Valid;
32 use Koha::DateUtils;
34 my $cgi = new CGI;
35 my $dbh = C4::Context->dbh;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39 template_name => "opac-memberentry.tt",
40 type => "opac",
41 query => $cgi,
42 authnotrequired => 1,
46 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
48 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
49 exit;
52 my $action = $cgi->param('action') || q{};
53 if ( $action eq q{} ) {
54 if ($borrowernumber) {
55 $action = 'edit';
57 else {
58 $action = 'new';
62 my $mandatory = GetMandatoryFields($action);
63 my $hidden = GetHiddenFields($mandatory);
65 $template->param(
66 action => $action,
67 hidden => $hidden,
68 mandatory => $mandatory,
69 member_titles => GetTitles() || undef,
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);
83 if (@empty_mandatory_fields || @$invalidformfields) {
84 $template->param(
85 empty_mandatory_fields => \@empty_mandatory_fields,
86 invalid_form_fields => $invalidformfields,
87 borrower => \%borrower
90 elsif (
91 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
93 $template->param(
94 failed_captcha => 1,
95 borrower => \%borrower
98 else {
99 if (
100 C4::Context->boolean_preference(
101 'PatronSelfRegistrationVerifyByEmail')
104 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
106 template_name => "opac-registration-email-sent.tt",
107 type => "opac",
108 query => $cgi,
109 authnotrequired => 1,
112 $template->param( 'email' => $borrower{'email'} );
114 my $verification_token = md5_hex( \%borrower );
115 $borrower{'password'} = random_string("..........");
117 Koha::Borrower::Modifications->new(
118 verification_token => $verification_token )
119 ->AddModifications(\%borrower);
121 #Send verification email
122 my $letter = C4::Letters::GetPreparedLetter(
123 module => 'members',
124 letter_code => 'OPAC_REG_VERIFY',
125 tables => {
126 borrower_modifications => $verification_token,
130 C4::Letters::EnqueueLetter(
132 letter => $letter,
133 message_transport_type => 'email',
134 to_address => $borrower{'email'},
135 from_address =>
136 C4::Context->preference('KohaAdminEmailAddress'),
140 else {
141 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
143 template_name => "opac-registration-confirmation.tt",
144 type => "opac",
145 query => $cgi,
146 authnotrequired => 1,
150 $template->param( OpacPasswordChange =>
151 C4::Context->preference('OpacPasswordChange') );
153 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
154 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
156 $template->param( password_cleartext => $password );
157 $template->param(
158 borrower => GetMember( borrowernumber => $borrowernumber ) );
159 $template->param(
160 PatronSelfRegistrationAdditionalInstructions =>
161 C4::Context->preference(
162 'PatronSelfRegistrationAdditionalInstructions')
167 elsif ( $action eq 'update' ) {
169 my %borrower = ParseCgiForBorrower($cgi);
171 my %borrower_changes = DelEmptyFields(%borrower);
172 my @empty_mandatory_fields =
173 CheckMandatoryFields( \%borrower_changes, $action );
174 my $invalidformfields = CheckForInvalidFields(\%borrower);
176 if (@empty_mandatory_fields || @$invalidformfields) {
177 $template->param(
178 empty_mandatory_fields => \@empty_mandatory_fields,
179 invalid_form_fields => $invalidformfields,
180 borrower => \%borrower
183 $template->param( action => 'edit' );
185 else {
186 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
187 if (%borrower_changes) {
188 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
190 template_name => "opac-memberentry-update-submitted.tt",
191 type => "opac",
192 query => $cgi,
193 authnotrequired => 1,
197 my $m =
198 Koha::Borrower::Modifications->new(
199 borrowernumber => $borrowernumber );
201 $m->DelModifications;
202 $m->AddModifications(\%borrower_changes);
203 $template->param(
204 borrower => GetMember( borrowernumber => $borrowernumber ),
207 else {
208 $template->param(
209 action => 'edit',
210 nochanges => 1,
211 borrower => GetMember( borrowernumber => $borrowernumber ),
216 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
217 my $borrower = GetMember( borrowernumber => $borrowernumber );
219 if (C4::Context->preference('ExtendedPatronAttributes')) {
220 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
221 if (scalar(@$attributes) > 0) {
222 $borrower->{ExtendedPatronAttributes} = 1;
223 $borrower->{patron_attributes} = $attributes;
227 $template->param(
228 borrower => $borrower, );
230 if (C4::Context->preference('OPACpatronimages')) {
231 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
232 if ($image) {
233 $template->param(
234 display_patron_image => 1
241 my $captcha = random_string("CCCCC");
243 $template->param(
244 captcha => $captcha,
245 captcha_digest => md5_base64($captcha)
248 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
250 sub GetHiddenFields {
251 my ($mandatory) = @_;
252 my %hidden_fields;
254 my $BorrowerUnwantedField =
255 C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
257 my @fields = split( /\|/, $BorrowerUnwantedField );
258 foreach (@fields) {
259 next unless m/\w/o;
260 #Don't hide mandatory fields
261 next if $mandatory->{$_};
262 $hidden_fields{$_} = 1;
265 return \%hidden_fields;
268 sub GetMandatoryFields {
269 my ($action) = @_;
271 my %mandatory_fields;
273 my $BorrowerMandatoryField =
274 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
276 my @fields = split( /\|/, $BorrowerMandatoryField );
278 foreach (@fields) {
279 $mandatory_fields{$_} = 1;
282 if ( $action eq 'create' || $action eq 'new' ) {
283 $mandatory_fields{'email'} = 1
284 if C4::Context->boolean_preference(
285 'PatronSelfRegistrationVerifyByEmail');
288 return \%mandatory_fields;
291 sub CheckMandatoryFields {
292 my ( $borrower, $action ) = @_;
294 my @empty_mandatory_fields;
296 my $mandatory_fields = GetMandatoryFields($action);
297 delete $mandatory_fields->{'cardnumber'};
299 foreach my $key ( keys %$mandatory_fields ) {
300 push( @empty_mandatory_fields, $key )
301 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
304 return @empty_mandatory_fields;
307 sub CheckForInvalidFields {
308 my $borrower = shift;
309 my @invalidFields;
310 if ($borrower->{'email'}) {
311 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
313 if ($borrower->{'emailpro'}) {
314 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
316 if ($borrower->{'B_email'}) {
317 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
319 return \@invalidFields;
322 sub ParseCgiForBorrower {
323 my ($cgi) = @_;
325 my $scrubber = C4::Scrubber->new();
326 my %borrower;
328 foreach ( $cgi->param ) {
329 if ( $_ =~ '^borrower_' ) {
330 my ($key) = substr( $_, 9 );
331 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
335 my $dob_dt;
336 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
337 if ( $borrower{'dateofbirth'} );
339 if ( $dob_dt ) {
340 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
342 else {
343 # Trigger validation
344 $borrower{'dateofbirth'} = undef;
347 return %borrower;
350 sub DelUnchangedFields {
351 my ( $borrowernumber, %new_data ) = @_;
353 my $current_data = GetMember( borrowernumber => $borrowernumber );
355 foreach my $key ( keys %new_data ) {
356 if ( $current_data->{$key} eq $new_data{$key} ) {
357 delete $new_data{$key};
361 return %new_data;
364 sub DelEmptyFields {
365 my (%borrower) = @_;
367 foreach my $key ( keys %borrower ) {
368 delete $borrower{$key} unless $borrower{$key};
371 return %borrower;