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>.
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
27 use C4::Form::MessagingPreferences;
28 use Koha::Borrower::Modifications;
29 use C4::Branch qw(GetBranchesLoop);
34 my $dbh = C4::Context->dbh;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 template_name => "opac-memberentry.tt",
45 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
47 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
51 my $action = $cgi->param('action') || q{};
52 if ( $action eq q{} ) {
53 if ($borrowernumber) {
61 my $mandatory = GetMandatoryFields($action);
62 my $hidden = GetHiddenFields($mandatory);
67 mandatory => $mandatory,
68 member_titles => GetTitles() || undef,
69 branches => GetBranchesLoop(),
70 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
73 if ( $action eq 'create' ) {
75 my %borrower = ParseCgiForBorrower($cgi);
77 %borrower = DelEmptyFields(%borrower);
79 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
80 my $invalidformfields = CheckForInvalidFields(\%borrower);
82 if (@empty_mandatory_fields || @$invalidformfields) {
84 empty_mandatory_fields => \@empty_mandatory_fields,
85 invalid_form_fields => $invalidformfields,
86 borrower => \%borrower
90 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
94 borrower => \%borrower
99 C4::Context->boolean_preference(
100 'PatronSelfRegistrationVerifyByEmail')
103 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
105 template_name => "opac-registration-email-sent.tt",
108 authnotrequired => 1,
111 $template->param( 'email' => $borrower{'email'} );
113 my $verification_token = md5_hex( \%borrower );
114 $borrower{'password'} = random_string("..........");
116 Koha::Borrower::Modifications->new(
117 verification_token => $verification_token )
118 ->AddModifications(\%borrower);
120 #Send verification email
121 my $letter = C4::Letters::GetPreparedLetter(
123 letter_code => 'OPAC_REG_VERIFY',
125 borrower_modifications => $verification_token,
129 C4::Letters::EnqueueLetter(
132 message_transport_type => 'email',
133 to_address => $borrower{'email'},
135 C4::Context->preference('KohaAdminEmailAddress'),
140 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
142 template_name => "opac-registration-confirmation.tt",
145 authnotrequired => 1,
149 $template->param( OpacPasswordChange =>
150 C4::Context->preference('OpacPasswordChange') );
152 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
153 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
155 $template->param( password_cleartext => $password );
157 borrower => GetMember( borrowernumber => $borrowernumber ) );
159 PatronSelfRegistrationAdditionalInstructions =>
160 C4::Context->preference(
161 'PatronSelfRegistrationAdditionalInstructions')
166 elsif ( $action eq 'update' ) {
168 my %borrower = ParseCgiForBorrower($cgi);
170 my %borrower_changes = DelEmptyFields(%borrower);
171 my @empty_mandatory_fields =
172 CheckMandatoryFields( \%borrower_changes, $action );
173 my $invalidformfields = CheckForInvalidFields(\%borrower);
175 if (@empty_mandatory_fields || @$invalidformfields) {
177 empty_mandatory_fields => \@empty_mandatory_fields,
178 invalid_form_fields => $invalidformfields,
179 borrower => \%borrower
182 $template->param( action => 'edit' );
185 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
187 template_name => "opac-memberentry-update-submitted.tt",
190 authnotrequired => 1,
194 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
197 Koha::Borrower::Modifications->new(
198 borrowernumber => $borrowernumber );
200 $m->DelModifications;
201 $m->AddModifications(\%borrower_changes);
203 borrower => GetMember( borrowernumber => $borrowernumber ),
207 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
208 my $borrower = GetMember( borrowernumber => $borrowernumber );
210 if (C4::Context->preference('ExtendedPatronAttributes')) {
211 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
212 if (scalar(@$attributes) > 0) {
213 $borrower->{ExtendedPatronAttributes} = 1;
214 $borrower->{patron_attributes} = $attributes;
219 borrower => $borrower, );
221 if (C4::Context->preference('OPACpatronimages')) {
222 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
225 display_patron_image => 1
232 my $captcha = random_string("CCCCC");
236 captcha_digest => md5_base64($captcha)
239 output_html_with_http_headers $cgi, $cookie, $template->output;
241 sub GetHiddenFields {
242 my ($mandatory) = @_;
245 my $BorrowerUnwantedField =
246 C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
248 my @fields = split( /\|/, $BorrowerUnwantedField );
251 #Don't hide mandatory fields
252 next if $mandatory->{$_};
253 $hidden_fields{$_} = 1;
256 return \%hidden_fields;
259 sub GetMandatoryFields {
262 my %mandatory_fields;
264 my $BorrowerMandatoryField =
265 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
267 my @fields = split( /\|/, $BorrowerMandatoryField );
270 $mandatory_fields{$_} = 1;
273 if ( $action eq 'create' || $action eq 'new' ) {
274 $mandatory_fields{'email'} = 1
275 if C4::Context->boolean_preference(
276 'PatronSelfRegistrationVerifyByEmail');
279 return \%mandatory_fields;
282 sub CheckMandatoryFields {
283 my ( $borrower, $action ) = @_;
285 my @empty_mandatory_fields;
287 my $mandatory_fields = GetMandatoryFields($action);
288 delete $mandatory_fields->{'cardnumber'};
290 foreach my $key ( keys %$mandatory_fields ) {
291 push( @empty_mandatory_fields, $key )
292 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
295 return @empty_mandatory_fields;
298 sub CheckForInvalidFields {
299 my $borrower = shift;
301 if ($borrower->{'email'}) {
302 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
304 if ($borrower->{'emailpro'}) {
305 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
307 if ($borrower->{'B_email'}) {
308 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
310 return \@invalidFields;
313 sub ParseCgiForBorrower {
316 my $scrubber = C4::Scrubber->new();
319 foreach ( $cgi->param ) {
320 if ( $_ =~ '^borrower_' ) {
321 my ($key) = substr( $_, 9 );
322 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
326 $borrower{'dateofbirth'} =
327 C4::Dates->new( $borrower{'dateofbirth'} )->output("iso")
328 if ( defined( $borrower{'dateofbirth'} ) );
333 sub DelUnchangedFields {
334 my ( $borrowernumber, %new_data ) = @_;
336 my $current_data = GetMember( borrowernumber => $borrowernumber );
338 foreach my $key ( keys %new_data ) {
339 if ( $current_data->{$key} eq $new_data{$key} ) {
340 delete $new_data{$key};
350 foreach my $key ( keys %borrower ) {
351 delete $borrower{$key} unless $borrower{$key};