Bug - 5511: Added new system preference: SessionRestrictionByIP
[koha.git] / opac / opac-memberentry.pl
blobe63f183aaa8d4d80749d111e8fce0eaaf7315f4e
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 Koha::Borrower::Modifications;
28 use C4::Branch qw(GetBranchesLoop);
29 use C4::Scrubber;
31 my $cgi = new CGI;
32 my $dbh = C4::Context->dbh;
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36 template_name => "opac-memberentry.tt",
37 type => "opac",
38 query => $cgi,
39 authnotrequired => 1,
43 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
45 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
46 exit;
49 my $action = $cgi->param('action') || q{};
50 if ( $action eq q{} ) {
51 if ($borrowernumber) {
52 $action = 'edit';
54 else {
55 $action = 'new';
59 my $mandatory = GetMandatoryFields($action);
60 my $hidden = GetHiddenFields($mandatory);
62 $template->param(
63 action => $action,
64 hidden => $hidden,
65 mandatory => $mandatory,
66 member_titles => GetTitles() || undef,
67 branches => GetBranchesLoop(),
68 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
71 if ( $action eq 'create' ) {
73 my %borrower = ParseCgiForBorrower($cgi);
75 %borrower = DelEmptyFields(%borrower);
77 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
79 if (@empty_mandatory_fields) {
80 $template->param(
81 empty_mandatory_fields => \@empty_mandatory_fields,
82 borrower => \%borrower
85 elsif (
86 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
88 $template->param(
89 failed_captcha => 1,
90 borrower => \%borrower
93 else {
94 if (
95 C4::Context->boolean_preference(
96 'PatronSelfRegistrationVerifyByEmail')
99 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
101 template_name => "opac-registration-email-sent.tt",
102 type => "opac",
103 query => $cgi,
104 authnotrequired => 1,
107 $template->param( 'email' => $borrower{'email'} );
109 my $verification_token = md5_hex( \%borrower );
110 $borrower{'password'} = random_string("..........");
112 Koha::Borrower::Modifications->new(
113 verification_token => $verification_token )
114 ->AddModifications(\%borrower);
116 #Send verification email
117 my $letter = C4::Letters::GetPreparedLetter(
118 module => 'members',
119 letter_code => 'OPAC_REG_VERIFY',
120 tables => {
121 borrower_modifications => $verification_token,
125 C4::Letters::EnqueueLetter(
127 letter => $letter,
128 message_transport_type => 'email',
129 to_address => $borrower{'email'},
130 from_address =>
131 C4::Context->preference('KohaAdminEmailAddress'),
135 else {
136 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
138 template_name => "opac-registration-confirmation.tt",
139 type => "opac",
140 query => $cgi,
141 authnotrequired => 1,
145 $template->param( OpacPasswordChange =>
146 C4::Context->preference('OpacPasswordChange') );
148 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
150 $template->param( password_cleartext => $password );
151 $template->param(
152 borrower => GetMember( borrowernumber => $borrowernumber ) );
153 $template->param(
154 PatronSelfRegistrationAdditionalInstructions =>
155 C4::Context->preference(
156 'PatronSelfRegistrationAdditionalInstructions')
161 elsif ( $action eq 'update' ) {
163 my %borrower = ParseCgiForBorrower($cgi);
165 my %borrower_changes = DelEmptyFields(%borrower);
166 my @empty_mandatory_fields =
167 CheckMandatoryFields( \%borrower_changes, $action );
169 if (@empty_mandatory_fields) {
170 $template->param(
171 empty_mandatory_fields => \@empty_mandatory_fields,
172 borrower => \%borrower
175 $template->param( action => 'edit' );
177 else {
178 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
180 template_name => "opac-memberentry-update-submitted.tt",
181 type => "opac",
182 query => $cgi,
183 authnotrequired => 1,
187 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
189 my $m =
190 Koha::Borrower::Modifications->new(
191 borrowernumber => $borrowernumber );
193 $m->DelModifications;
194 $m->AddModifications(\%borrower_changes);
195 $template->param(
196 borrower => GetMember( borrowernumber => $borrowernumber ),
200 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
201 my $borrower = GetMember( borrowernumber => $borrowernumber );
203 if (C4::Context->preference('ExtendedPatronAttributes')) {
204 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
205 if (scalar(@$attributes) > 0) {
206 $borrower->{ExtendedPatronAttributes} = 1;
207 $borrower->{patron_attributes} = $attributes;
211 $template->param(
212 borrower => $borrower, );
214 if (C4::Context->preference('OPACpatronimages')) {
215 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
216 if ($image) {
217 $template->param(
218 display_patron_image => 1
225 my $captcha = random_string("CCCCC");
227 $template->param(
228 captcha => $captcha,
229 captcha_digest => md5_base64($captcha)
232 output_html_with_http_headers $cgi, $cookie, $template->output;
234 sub GetHiddenFields {
235 my ($mandatory) = @_;
236 my %hidden_fields;
238 my $BorrowerUnwantedField =
239 C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
241 my @fields = split( /\|/, $BorrowerUnwantedField );
242 foreach (@fields) {
243 next unless m/\w/o;
244 #Don't hide mandatory fields
245 next if $mandatory->{$_};
246 $hidden_fields{$_} = 1;
249 return \%hidden_fields;
252 sub GetMandatoryFields {
253 my ($action) = @_;
255 my %mandatory_fields;
257 my $BorrowerMandatoryField =
258 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
260 my @fields = split( /\|/, $BorrowerMandatoryField );
262 foreach (@fields) {
263 $mandatory_fields{$_} = 1;
266 if ( $action eq 'create' || $action eq 'new' ) {
267 $mandatory_fields{'email'} = 1
268 if C4::Context->boolean_preference(
269 'PatronSelfRegistrationVerifyByEmail');
272 return \%mandatory_fields;
275 sub CheckMandatoryFields {
276 my ( $borrower, $action ) = @_;
278 my @empty_mandatory_fields;
280 my $mandatory_fields = GetMandatoryFields($action);
281 delete $mandatory_fields->{'cardnumber'};
283 foreach my $key ( keys %$mandatory_fields ) {
284 push( @empty_mandatory_fields, $key )
285 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
288 return @empty_mandatory_fields;
291 sub ParseCgiForBorrower {
292 my ($cgi) = @_;
294 my $scrubber = C4::Scrubber->new();
295 my %borrower;
297 foreach ( $cgi->param ) {
298 if ( $_ =~ '^borrower_' ) {
299 my ($key) = substr( $_, 9 );
300 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
304 $borrower{'dateofbirth'} =
305 C4::Dates->new( $borrower{'dateofbirth'} )->output("iso")
306 if ( defined( $borrower{'dateofbirth'} ) );
308 return %borrower;
311 sub DelUnchangedFields {
312 my ( $borrowernumber, %new_data ) = @_;
314 my $current_data = GetMember( borrowernumber => $borrowernumber );
316 foreach my $key ( keys %new_data ) {
317 if ( $current_data->{$key} eq $new_data{$key} ) {
318 delete $new_data{$key};
322 return %new_data;
325 sub DelEmptyFields {
326 my (%borrower) = @_;
328 foreach my $key ( keys %borrower ) {
329 delete $borrower{$key} unless $borrower{$key};
332 return %borrower;