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
;
29 use Koha
::Borrower
::Modifications
;
30 use C4
::Branch
qw(GetBranchesLoop);
36 my $dbh = C4
::Context
->dbh;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
40 template_name
=> "opac-memberentry.tt",
47 unless ( C4
::Context
->preference('PatronSelfRegistration') || $borrowernumber )
49 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
53 my $action = $cgi->param('action') || q{};
54 if ( $action eq q{} ) {
55 if ($borrowernumber) {
63 my $mandatory = GetMandatoryFields
($action);
67 hidden
=> GetHiddenFields
( $mandatory, 'registration' ),
68 mandatory
=> $mandatory,
69 member_titles
=> GetTitles
() || undef,
70 branches
=> GetBranchesLoop
(),
71 OPACPatronDetails
=> C4
::Context
->preference('OPACPatronDetails'),
74 if ( $action eq 'create' ) {
76 my %borrower = ParseCgiForBorrower
($cgi);
78 %borrower = DelEmptyFields
(%borrower);
80 my @empty_mandatory_fields = CheckMandatoryFields
( \
%borrower, $action );
81 my $invalidformfields = CheckForInvalidFields
(\
%borrower);
82 delete $borrower{'password2'};
84 if (@empty_mandatory_fields || @
$invalidformfields) {
86 empty_mandatory_fields
=> \
@empty_mandatory_fields,
87 invalid_form_fields
=> $invalidformfields,
88 borrower
=> \
%borrower
92 md5_base64
( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
96 borrower
=> \
%borrower
101 C4
::Context
->boolean_preference(
102 'PatronSelfRegistrationVerifyByEmail')
105 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
107 template_name
=> "opac-registration-email-sent.tt",
110 authnotrequired
=> 1,
113 $template->param( 'email' => $borrower{'email'} );
115 my $verification_token = md5_hex
( \
%borrower );
116 $borrower{'password'} = random_string
("..........");
117 Koha
::Borrower
::Modifications
->new(
118 verification_token
=> $verification_token )
119 ->AddModifications(\
%borrower);
121 #Send verification email
122 my $letter = C4
::Letters
::GetPreparedLetter
(
124 letter_code
=> 'OPAC_REG_VERIFY',
126 borrower_modifications
=> $verification_token,
130 C4
::Letters
::EnqueueLetter
(
133 message_transport_type
=> 'email',
134 to_address
=> $borrower{'email'},
136 C4
::Context
->preference('KohaAdminEmailAddress'),
141 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
143 template_name
=> "opac-registration-confirmation.tt",
146 authnotrequired
=> 1,
150 $template->param( OpacPasswordChange
=>
151 C4
::Context
->preference('OpacPasswordChange') );
153 my ( $borrowernumber, $password ) = AddMember_Opac
(%borrower);
154 C4
::Form
::MessagingPreferences
::handle_form_action
($cgi, { borrowernumber
=> $borrowernumber }, $template, 1, C4
::Context
->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4
::Context
->preference('EnhancedMessagingPreferences');
156 $template->param( password_cleartext
=> $password );
158 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ) );
160 PatronSelfRegistrationAdditionalInstructions
=>
161 C4
::Context
->preference(
162 'PatronSelfRegistrationAdditionalInstructions')
167 elsif ( $action eq 'update' ) {
169 my %borrower = ParseCgiForBorrower
($cgi);
171 my %borrower_changes = DelEmptyFields
(%borrower);
172 my @empty_mandatory_fields =
173 CheckMandatoryFields
( \
%borrower_changes, $action );
174 my $invalidformfields = CheckForInvalidFields
(\
%borrower);
176 if (@empty_mandatory_fields || @
$invalidformfields) {
178 empty_mandatory_fields
=> \
@empty_mandatory_fields,
179 invalid_form_fields
=> $invalidformfields,
180 borrower
=> \
%borrower
183 $template->param( action
=> 'edit' );
186 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
187 if (%borrower_changes) {
188 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
190 template_name
=> "opac-memberentry-update-submitted.tt",
193 authnotrequired
=> 1,
198 Koha
::Borrower
::Modifications
->new(
199 borrowernumber
=> $borrowernumber );
201 $m->DelModifications;
202 $m->AddModifications(\
%borrower_changes);
204 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
211 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
216 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
217 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
219 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
220 my $attributes = C4
::Members
::Attributes
::GetBorrowerAttributes
($borrowernumber, 'opac');
221 if (scalar(@
$attributes) > 0) {
222 $borrower->{ExtendedPatronAttributes
} = 1;
223 $borrower->{patron_attributes
} = $attributes;
228 borrower
=> $borrower,
229 guarantor
=> scalar Koha
::Borrowers
->find($borrowernumber)->guarantor(),
230 hidden
=> GetHiddenFields
( $mandatory, 'modification' ),
233 if (C4
::Context
->preference('OPACpatronimages')) {
234 my ($image, $dberror) = GetPatronImage
($borrower->{borrowernumber
});
237 display_patron_image
=> 1
244 my $captcha = random_string
("CCCCC");
248 captcha_digest
=> md5_base64
($captcha)
251 output_html_with_http_headers
$cgi, $cookie, $template->output, undef, { force_no_caching
=> 1 };
253 sub GetHiddenFields
{
254 my ( $mandatory, $action ) = @_;
257 my $BorrowerUnwantedField = $action eq 'modification' ?
258 C4
::Context
->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
259 C4
::Context
->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
261 my @fields = split( /\|/, $BorrowerUnwantedField || q
|| );
264 #Don't hide mandatory fields
265 next if $mandatory->{$_};
266 $hidden_fields{$_} = 1;
269 return \
%hidden_fields;
272 sub GetMandatoryFields
{
275 my %mandatory_fields;
277 my $BorrowerMandatoryField =
278 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
280 my @fields = split( /\|/, $BorrowerMandatoryField );
283 $mandatory_fields{$_} = 1;
286 if ( $action eq 'create' || $action eq 'new' ) {
287 $mandatory_fields{'email'} = 1
288 if C4
::Context
->boolean_preference(
289 'PatronSelfRegistrationVerifyByEmail');
292 return \
%mandatory_fields;
295 sub CheckMandatoryFields
{
296 my ( $borrower, $action ) = @_;
298 my @empty_mandatory_fields;
300 my $mandatory_fields = GetMandatoryFields
($action);
301 delete $mandatory_fields->{'cardnumber'};
303 foreach my $key ( keys %$mandatory_fields ) {
304 push( @empty_mandatory_fields, $key )
305 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
308 return @empty_mandatory_fields;
311 sub CheckForInvalidFields
{
312 my $minpw = C4
::Context
->preference('minPasswordLength');
313 my $borrower = shift;
315 if ($borrower->{'email'}) {
316 push(@invalidFields, "email") if (!Email
::Valid
->address($borrower->{'email'}));
318 if ($borrower->{'emailpro'}) {
319 push(@invalidFields, "emailpro") if (!Email
::Valid
->address($borrower->{'emailpro'}));
321 if ($borrower->{'B_email'}) {
322 push(@invalidFields, "B_email") if (!Email
::Valid
->address($borrower->{'B_email'}));
324 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
325 push(@invalidFields, "password_match");
327 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
328 push(@invalidFields, "password_invalid");
330 if ( $borrower->{'password'} ) {
331 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
334 return \
@invalidFields;
337 sub ParseCgiForBorrower
{
340 my $scrubber = C4
::Scrubber
->new();
343 foreach ( $cgi->param ) {
344 if ( $_ =~ '^borrower_' ) {
345 my ($key) = substr( $_, 9 );
346 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
351 $dob_dt = eval { dt_from_string
( $borrower{'dateofbirth'} ); }
352 if ( $borrower{'dateofbirth'} );
355 $borrower{'dateofbirth'} = output_pref
( { dt
=> $dob_dt, dateonly
=> 1, dateformat
=> 'iso' } );
359 $borrower{'dateofbirth'} = undef;
365 sub DelUnchangedFields
{
366 my ( $borrowernumber, %new_data ) = @_;
368 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
370 foreach my $key ( keys %new_data ) {
371 if ( $current_data->{$key} eq $new_data{$key} ) {
372 delete $new_data{$key};
382 foreach my $key ( keys %borrower ) {
383 delete $borrower{$key} unless $borrower{$key};