Bug 7067 - OPAC Borrower Self Registration
[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 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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use CGI;
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 Koha::Borrower::Modifications;
28 use C4::Branch qw(GetBranchesLoop);
29
30 my $cgi = new CGI;
31 my $dbh = C4::Context->dbh;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34     {
35         template_name   => "opac-memberentry.tmpl",
36         type            => "opac",
37         query           => $cgi,
38         authnotrequired => 1,
39     }
40 );
41
42 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
43 {
44     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
45     exit;
46 }
47
48 my $action = $cgi->param('action') || q{};
49 if ( $action eq q{} ) {
50     if ($borrowernumber) {
51         $action = 'edit';
52     }
53     else {
54         $action = 'new';
55     }
56 }
57
58 $template->param(
59     action            => $action,
60     hidden            => GetHiddenFields(),
61     mandatory         => GetMandatoryFields($action),
62     member_titles     => GetTitles(),
63     branches          => GetBranchesLoop(),
64     OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
65 );
66
67 if ( $action eq 'create' ) {
68
69     my %borrower = ParseCgiForBorrower($cgi);
70
71     %borrower = DelEmptyFields(%borrower);
72
73     my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
74
75     if (@empty_mandatory_fields) {
76         $template->param(
77             empty_mandatory_fields => \@empty_mandatory_fields,
78             borrower               => \%borrower
79         );
80     }
81     elsif (
82         md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
83     {
84         $template->param(
85             failed_captcha => 1,
86             borrower       => \%borrower
87         );
88     }
89     else {
90         if (
91             C4::Context->boolean_preference(
92                 'PatronSelfRegistrationVerifyByEmail')
93           )
94         {
95             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
96                 {
97                     template_name   => "opac-registration-email-sent.tmpl",
98                     type            => "opac",
99                     query           => $cgi,
100                     authnotrequired => 1,
101                 }
102             );
103             $template->param( 'email' => $borrower{'email'} );
104
105             my $verification_token = md5_hex( \%borrower );
106             $borrower{'password'} = random_string("..........");
107
108             Koha::Borrower::Modifications->new(
109                 verification_token => $verification_token )
110               ->AddModifications(%borrower);
111
112             #Send verification email
113             my $letter = C4::Letters::GetPreparedLetter(
114                 module      => 'members',
115                 letter_code => 'OPAC_REG_VERIFY',
116                 tables      => {
117                     borrower_modifications =>
118                       [ $verification_token, $verification_token ],
119                 },
120             );
121
122             C4::Letters::EnqueueLetter(
123                 {
124                     letter                 => $letter,
125                     message_transport_type => 'email',
126                     to_address             => $borrower{'email'},
127                     from_address =>
128                       C4::Context->preference('KohaAdminEmailAddress'),
129                 }
130             );
131         }
132         else {
133             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
134                 {
135                     template_name   => "opac-registration-confirmation.tmpl",
136                     type            => "opac",
137                     query           => $cgi,
138                     authnotrequired => 1,
139                 }
140             );
141
142             $template->param( OpacPasswordChange =>
143                   C4::Context->preference('OpacPasswordChange') );
144
145             my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
146
147             $template->param( password_cleartext => $password );
148             $template->param(
149                 borrower => GetMember( borrowernumber => $borrowernumber ) );
150             $template->param(
151                 PatronSelfRegistrationAdditionalInstructions =>
152                   C4::Context->preference(
153                     'PatronSelfRegistrationAdditionalInstructions')
154             );
155         }
156     }
157 }
158 elsif ( $action eq 'update' ) {
159
160     my %borrower = ParseCgiForBorrower($cgi);
161
162     my %borrower_changes = DelEmptyFields(%borrower);
163     my @empty_mandatory_fields =
164       CheckMandatoryFields( \%borrower_changes, $action );
165
166     if (@empty_mandatory_fields) {
167         $template->param(
168             empty_mandatory_fields => \@empty_mandatory_fields,
169             borrower               => \%borrower
170         );
171
172         $template->param( action => 'edit' );
173     }
174     else {
175         ( $template, $borrowernumber, $cookie ) = get_template_and_user(
176             {
177                 template_name   => "opac-memberentry-update-submitted.tmpl",
178                 type            => "opac",
179                 query           => $cgi,
180                 authnotrequired => 1,
181             }
182         );
183
184         my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
185
186         my $m =
187           Koha::Borrower::Modifications->new(
188             borrowernumber => $borrowernumber );
189
190         $m->DelModifications;
191         $m->AddModifications(%borrower_changes);
192     }
193 }
194 elsif ( $action eq 'edit' ) {    #Display logged in borrower's data
195     $template->param(
196         borrower => GetMember( borrowernumber => $borrowernumber ), );
197 }
198
199 my $captcha = random_string("CCCCC");
200
201 $template->param(
202     captcha        => $captcha,
203     captcha_digest => md5_base64($captcha)
204 );
205
206 output_html_with_http_headers $cgi, $cookie, $template->output;
207
208 sub GetHiddenFields {
209     my %hidden_fields;
210
211     my $BorrowerUnwantedField =
212       C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
213
214     my @fields = split( /\|/, $BorrowerUnwantedField );
215     foreach (@fields) {
216         next unless m/\w/o;
217         $hidden_fields{$_} = 1;
218     }
219
220     return \%hidden_fields;
221 }
222
223 sub GetMandatoryFields {
224     my ($action) = @_;
225
226     my %mandatory_fields;
227
228     my $BorrowerMandatoryField =
229       C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
230
231     my @fields = split( /\|/, $BorrowerMandatoryField );
232
233     foreach (@fields) {
234         $mandatory_fields{$_} = 1;
235     }
236
237     if ( $action eq 'create' || $action eq 'new' ) {
238         $mandatory_fields{'email'} = 1
239           if C4::Context->boolean_preference(
240             'PatronSelfRegistrationVerifyByEmail');
241     }
242
243     return \%mandatory_fields;
244 }
245
246 sub CheckMandatoryFields {
247     my ( $borrower, $action ) = @_;
248
249     my @empty_mandatory_fields;
250
251     my $mandatory_fields = GetMandatoryFields($action);
252     delete $mandatory_fields->{'cardnumber'};
253
254     foreach my $key ( keys %$mandatory_fields ) {
255         push( @empty_mandatory_fields, $key )
256           unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
257     }
258
259     return @empty_mandatory_fields;
260 }
261
262 sub ParseCgiForBorrower {
263     my ($cgi) = @_;
264
265     my %borrower;
266
267     foreach ( $cgi->param ) {
268         if ( $_ =~ '^borrower_' ) {
269             my ($key) = substr( $_, 9 );
270             $borrower{$key} = $cgi->param($_);
271         }
272     }
273
274     $borrower{'dateofbirth'} =
275       C4::Dates->new( $borrower{'dateofbirth'} )->output("iso")
276       if ( defined( $borrower{'dateofbirth'} ) );
277
278     return %borrower;
279 }
280
281 sub DelUnchangedFields {
282     my ( $borrowernumber, %new_data ) = @_;
283
284     my $current_data = GetMember( borrowernumber => $borrowernumber );
285
286     foreach my $key ( keys %new_data ) {
287         if ( $current_data->{$key} eq $new_data{$key} ) {
288             delete $new_data{$key};
289         }
290     }
291
292     return %new_data;
293 }
294
295 sub DelEmptyFields {
296     my (%borrower) = @_;
297
298     foreach my $key ( keys %borrower ) {
299         delete $borrower{$key} unless $borrower{$key};
300     }
301
302     return %borrower;
303 }