Release notes 3.20.8
[koha.git] / opac / opac-memberentry.pl
bloba6e39b775235c3c4bf4b08e37d6aab1476c9e596
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;
32 my $cgi = new CGI;
33 my $dbh = C4::Context->dbh;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37 template_name => "opac-memberentry.tt",
38 type => "opac",
39 query => $cgi,
40 authnotrequired => 1,
44 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
46 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
47 exit;
50 my $action = $cgi->param('action') || q{};
51 if ( $action eq q{} ) {
52 if ($borrowernumber) {
53 $action = 'edit';
55 else {
56 $action = 'new';
60 my $mandatory = GetMandatoryFields($action);
61 my $hidden = GetHiddenFields($mandatory);
63 $template->param(
64 action => $action,
65 hidden => $hidden,
66 mandatory => $mandatory,
67 member_titles => GetTitles() || undef,
68 branches => GetBranchesLoop(),
69 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
72 if ( $action eq 'create' ) {
74 my %borrower = ParseCgiForBorrower($cgi);
76 %borrower = DelEmptyFields(%borrower);
78 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
80 if (@empty_mandatory_fields) {
81 $template->param(
82 empty_mandatory_fields => \@empty_mandatory_fields,
83 borrower => \%borrower
86 elsif (
87 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
89 $template->param(
90 failed_captcha => 1,
91 borrower => \%borrower
94 else {
95 if (
96 C4::Context->boolean_preference(
97 'PatronSelfRegistrationVerifyByEmail')
100 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
102 template_name => "opac-registration-email-sent.tt",
103 type => "opac",
104 query => $cgi,
105 authnotrequired => 1,
108 $template->param( 'email' => $borrower{'email'} );
110 my $verification_token = md5_hex( \%borrower );
111 $borrower{'password'} = random_string("..........");
113 Koha::Borrower::Modifications->new(
114 verification_token => $verification_token )
115 ->AddModifications(\%borrower);
117 #Send verification email
118 my $letter = C4::Letters::GetPreparedLetter(
119 module => 'members',
120 letter_code => 'OPAC_REG_VERIFY',
121 tables => {
122 borrower_modifications => $verification_token,
126 C4::Letters::EnqueueLetter(
128 letter => $letter,
129 message_transport_type => 'email',
130 to_address => $borrower{'email'},
131 from_address =>
132 C4::Context->preference('KohaAdminEmailAddress'),
136 else {
137 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
139 template_name => "opac-registration-confirmation.tt",
140 type => "opac",
141 query => $cgi,
142 authnotrequired => 1,
146 $template->param( OpacPasswordChange =>
147 C4::Context->preference('OpacPasswordChange') );
149 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
150 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
152 $template->param( password_cleartext => $password );
153 $template->param(
154 borrower => GetMember( borrowernumber => $borrowernumber ) );
155 $template->param(
156 PatronSelfRegistrationAdditionalInstructions =>
157 C4::Context->preference(
158 'PatronSelfRegistrationAdditionalInstructions')
163 elsif ( $action eq 'update' ) {
165 my %borrower = ParseCgiForBorrower($cgi);
167 my %borrower_changes = DelEmptyFields(%borrower);
168 my @empty_mandatory_fields =
169 CheckMandatoryFields( \%borrower_changes, $action );
171 if (@empty_mandatory_fields) {
172 $template->param(
173 empty_mandatory_fields => \@empty_mandatory_fields,
174 borrower => \%borrower
177 $template->param( action => 'edit' );
179 else {
180 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
182 template_name => "opac-memberentry-update-submitted.tt",
183 type => "opac",
184 query => $cgi,
185 authnotrequired => 1,
189 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
191 my $m =
192 Koha::Borrower::Modifications->new(
193 borrowernumber => $borrowernumber );
195 $m->DelModifications;
196 $m->AddModifications(\%borrower_changes);
197 $template->param(
198 borrower => GetMember( borrowernumber => $borrowernumber ),
202 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
203 my $borrower = GetMember( borrowernumber => $borrowernumber );
205 if (C4::Context->preference('ExtendedPatronAttributes')) {
206 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
207 if (scalar(@$attributes) > 0) {
208 $borrower->{ExtendedPatronAttributes} = 1;
209 $borrower->{patron_attributes} = $attributes;
213 $template->param(
214 borrower => $borrower, );
216 if (C4::Context->preference('OPACpatronimages')) {
217 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
218 if ($image) {
219 $template->param(
220 display_patron_image => 1
227 my $captcha = random_string("CCCCC");
229 $template->param(
230 captcha => $captcha,
231 captcha_digest => md5_base64($captcha)
234 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
236 sub GetHiddenFields {
237 my ($mandatory) = @_;
238 my %hidden_fields;
240 my $BorrowerUnwantedField =
241 C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
243 my @fields = split( /\|/, $BorrowerUnwantedField );
244 foreach (@fields) {
245 next unless m/\w/o;
246 #Don't hide mandatory fields
247 next if $mandatory->{$_};
248 $hidden_fields{$_} = 1;
251 return \%hidden_fields;
254 sub GetMandatoryFields {
255 my ($action) = @_;
257 my %mandatory_fields;
259 my $BorrowerMandatoryField =
260 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
262 my @fields = split( /\|/, $BorrowerMandatoryField );
264 foreach (@fields) {
265 $mandatory_fields{$_} = 1;
268 if ( $action eq 'create' || $action eq 'new' ) {
269 $mandatory_fields{'email'} = 1
270 if C4::Context->boolean_preference(
271 'PatronSelfRegistrationVerifyByEmail');
274 return \%mandatory_fields;
277 sub CheckMandatoryFields {
278 my ( $borrower, $action ) = @_;
280 my @empty_mandatory_fields;
282 my $mandatory_fields = GetMandatoryFields($action);
283 delete $mandatory_fields->{'cardnumber'};
285 foreach my $key ( keys %$mandatory_fields ) {
286 push( @empty_mandatory_fields, $key )
287 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
290 return @empty_mandatory_fields;
293 sub ParseCgiForBorrower {
294 my ($cgi) = @_;
296 my $scrubber = C4::Scrubber->new();
297 my %borrower;
299 foreach ( $cgi->param ) {
300 if ( $_ =~ '^borrower_' ) {
301 my ($key) = substr( $_, 9 );
302 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
306 $borrower{'dateofbirth'} =
307 C4::Dates->new( $borrower{'dateofbirth'} )->output("iso")
308 if ( defined( $borrower{'dateofbirth'} ) );
310 return %borrower;
313 sub DelUnchangedFields {
314 my ( $borrowernumber, %new_data ) = @_;
316 my $current_data = GetMember( borrowernumber => $borrowernumber );
318 foreach my $key ( keys %new_data ) {
319 if ( $current_data->{$key} eq $new_data{$key} ) {
320 delete $new_data{$key};
324 return %new_data;
327 sub DelEmptyFields {
328 my (%borrower) = @_;
330 foreach my $key ( keys %borrower ) {
331 delete $borrower{$key} unless $borrower{$key};
334 return %borrower;