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
::Patron
::Modifications
;
30 use C4
::Branch
qw(GetBranchesLoop);
34 use Koha
::Patron
::Images
;
37 my $dbh = C4
::Context
->dbh;
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
41 template_name
=> "opac-memberentry.tt",
48 unless ( C4
::Context
->preference('PatronSelfRegistration') || $borrowernumber )
50 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
54 my $action = $cgi->param('action') || q{};
55 if ( $action eq q{} ) {
56 if ($borrowernumber) {
64 my $mandatory = GetMandatoryFields
($action);
68 hidden
=> GetHiddenFields
( $mandatory, 'registration' ),
69 mandatory
=> $mandatory,
70 member_titles
=> GetTitles
() || undef,
71 branches
=> GetBranchesLoop
(),
72 OPACPatronDetails
=> C4
::Context
->preference('OPACPatronDetails'),
75 if ( $action eq 'create' ) {
77 my %borrower = ParseCgiForBorrower
($cgi);
79 %borrower = DelEmptyFields
(%borrower);
81 my @empty_mandatory_fields = CheckMandatoryFields
( \
%borrower, $action );
82 my $invalidformfields = CheckForInvalidFields
(\
%borrower);
83 delete $borrower{'password2'};
84 my $cardnumber_error_code;
85 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
86 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
87 # spurious length warning.
88 $cardnumber_error_code = checkcardnumber
( $borrower{cardnumber
}, $borrower{borrowernumber
} );
91 if ( @empty_mandatory_fields || @
$invalidformfields || $cardnumber_error_code ) {
92 if ( $cardnumber_error_code == 1 ) {
93 $template->param( cardnumber_already_exists
=> 1 );
94 } elsif ( $cardnumber_error_code == 2 ) {
95 $template->param( cardnumber_wrong_length
=> 1 );
99 empty_mandatory_fields
=> \
@empty_mandatory_fields,
100 invalid_form_fields
=> $invalidformfields,
101 borrower
=> \
%borrower
105 md5_base64
( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
109 borrower
=> \
%borrower
114 C4
::Context
->boolean_preference(
115 'PatronSelfRegistrationVerifyByEmail')
118 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
120 template_name
=> "opac-registration-email-sent.tt",
123 authnotrequired
=> 1,
126 $template->param( 'email' => $borrower{'email'} );
128 my $verification_token = md5_hex
( \
%borrower );
129 $borrower{'password'} = random_string
("..........");
131 Koha
::Patron
::Modifications
->new(
132 verification_token
=> $verification_token )
133 ->AddModifications(\
%borrower);
135 #Send verification email
136 my $letter = C4
::Letters
::GetPreparedLetter
(
138 letter_code
=> 'OPAC_REG_VERIFY',
140 borrower_modifications
=> $verification_token,
144 C4
::Letters
::EnqueueLetter
(
147 message_transport_type
=> 'email',
148 to_address
=> $borrower{'email'},
150 C4
::Context
->preference('KohaAdminEmailAddress'),
155 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
157 template_name
=> "opac-registration-confirmation.tt",
160 authnotrequired
=> 1,
164 $template->param( OpacPasswordChange
=>
165 C4
::Context
->preference('OpacPasswordChange') );
167 my ( $borrowernumber, $password ) = AddMember_Opac
(%borrower);
168 C4
::Form
::MessagingPreferences
::handle_form_action
($cgi, { borrowernumber
=> $borrowernumber }, $template, 1, C4
::Context
->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4
::Context
->preference('EnhancedMessagingPreferences');
170 $template->param( password_cleartext
=> $password );
172 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ) );
174 PatronSelfRegistrationAdditionalInstructions
=>
175 C4
::Context
->preference(
176 'PatronSelfRegistrationAdditionalInstructions')
181 elsif ( $action eq 'update' ) {
183 my %borrower = ParseCgiForBorrower
($cgi);
185 my %borrower_changes = DelEmptyFields
(%borrower);
186 my @empty_mandatory_fields =
187 CheckMandatoryFields
( \
%borrower_changes, $action );
188 my $invalidformfields = CheckForInvalidFields
(\
%borrower);
190 if (@empty_mandatory_fields || @
$invalidformfields) {
192 empty_mandatory_fields
=> \
@empty_mandatory_fields,
193 invalid_form_fields
=> $invalidformfields,
194 borrower
=> \
%borrower
197 $template->param( action
=> 'edit' );
200 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
201 if (%borrower_changes) {
202 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
204 template_name
=> "opac-memberentry-update-submitted.tt",
207 authnotrequired
=> 1,
212 Koha
::Patron
::Modifications
->new(
213 borrowernumber
=> $borrowernumber );
215 $m->DelModifications;
216 $m->AddModifications(\
%borrower_changes);
218 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
225 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
230 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
231 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
233 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
234 my $attributes = C4
::Members
::Attributes
::GetBorrowerAttributes
($borrowernumber, 'opac');
235 if (scalar(@
$attributes) > 0) {
236 $borrower->{ExtendedPatronAttributes
} = 1;
237 $borrower->{patron_attributes
} = $attributes;
242 borrower
=> $borrower,
243 guarantor
=> scalar Koha
::Patrons
->find($borrowernumber)->guarantor(),
244 hidden
=> GetHiddenFields
( $mandatory, 'modification' ),
247 if (C4
::Context
->preference('OPACpatronimages')) {
248 my $patron_image = Koha
::Patron
::Images
->find($borrower->{borrowernumber
});
249 $template->param( display_patron_image
=> 1 ) if $patron_image;
254 my $captcha = random_string
("CCCCC");
258 captcha_digest
=> md5_base64
($captcha)
261 output_html_with_http_headers
$cgi, $cookie, $template->output, undef, { force_no_caching
=> 1 };
263 sub GetHiddenFields
{
264 my ( $mandatory, $action ) = @_;
267 my $BorrowerUnwantedField = $action eq 'modification' ?
268 C4
::Context
->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
269 C4
::Context
->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
271 my @fields = split( /\|/, $BorrowerUnwantedField || q
|| );
274 #Don't hide mandatory fields
275 next if $mandatory->{$_};
276 $hidden_fields{$_} = 1;
279 return \
%hidden_fields;
282 sub GetMandatoryFields
{
285 my %mandatory_fields;
287 my $BorrowerMandatoryField =
288 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
290 my @fields = split( /\|/, $BorrowerMandatoryField );
293 $mandatory_fields{$_} = 1;
296 if ( $action eq 'create' || $action eq 'new' ) {
297 $mandatory_fields{'email'} = 1
298 if C4
::Context
->boolean_preference(
299 'PatronSelfRegistrationVerifyByEmail');
302 return \
%mandatory_fields;
305 sub CheckMandatoryFields
{
306 my ( $borrower, $action ) = @_;
308 my @empty_mandatory_fields;
310 my $mandatory_fields = GetMandatoryFields
($action);
311 delete $mandatory_fields->{'cardnumber'};
313 foreach my $key ( keys %$mandatory_fields ) {
314 push( @empty_mandatory_fields, $key )
315 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
318 return @empty_mandatory_fields;
321 sub CheckForInvalidFields
{
322 my $minpw = C4
::Context
->preference('minPasswordLength');
323 my $borrower = shift;
325 if ($borrower->{'email'}) {
326 push(@invalidFields, "email") if (!Email
::Valid
->address($borrower->{'email'}));
328 if ($borrower->{'emailpro'}) {
329 push(@invalidFields, "emailpro") if (!Email
::Valid
->address($borrower->{'emailpro'}));
331 if ($borrower->{'B_email'}) {
332 push(@invalidFields, "B_email") if (!Email
::Valid
->address($borrower->{'B_email'}));
334 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
335 push(@invalidFields, "password_match");
337 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
338 push(@invalidFields, "password_invalid");
340 if ( $borrower->{'password'} ) {
341 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
344 return \
@invalidFields;
347 sub ParseCgiForBorrower
{
350 my $scrubber = C4
::Scrubber
->new();
353 foreach ( $cgi->param ) {
354 if ( $_ =~ '^borrower_' ) {
355 my ($key) = substr( $_, 9 );
356 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
361 $dob_dt = eval { dt_from_string
( $borrower{'dateofbirth'} ); }
362 if ( $borrower{'dateofbirth'} );
365 $borrower{'dateofbirth'} = output_pref
( { dt
=> $dob_dt, dateonly
=> 1, dateformat
=> 'iso' } );
369 $borrower{'dateofbirth'} = undef;
375 sub DelUnchangedFields
{
376 my ( $borrowernumber, %new_data ) = @_;
378 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
380 foreach my $key ( keys %new_data ) {
381 if ( $current_data->{$key} eq $new_data{$key} ) {
382 delete $new_data{$key};
392 foreach my $key ( keys %borrower ) {
393 delete $borrower{$key} unless $borrower{$key};