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