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 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
186 if (%borrower_changes) {
187 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
189 template_name
=> "opac-memberentry-update-submitted.tt",
192 authnotrequired
=> 1,
197 Koha
::Borrower
::Modifications
->new(
198 borrowernumber
=> $borrowernumber );
200 $m->DelModifications;
201 $m->AddModifications(\
%borrower_changes);
203 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
210 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
215 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
216 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
218 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
219 my $attributes = C4
::Members
::Attributes
::GetBorrowerAttributes
($borrowernumber, 'opac');
220 if (scalar(@
$attributes) > 0) {
221 $borrower->{ExtendedPatronAttributes
} = 1;
222 $borrower->{patron_attributes
} = $attributes;
227 borrower
=> $borrower, );
229 if (C4
::Context
->preference('OPACpatronimages')) {
230 my ($image, $dberror) = GetPatronImage
($borrower->{borrowernumber
});
233 display_patron_image
=> 1
240 my $captcha = random_string
("CCCCC");
244 captcha_digest
=> md5_base64
($captcha)
247 output_html_with_http_headers
$cgi, $cookie, $template->output, undef, { force_no_caching
=> 1 };
249 sub GetHiddenFields
{
250 my ($mandatory) = @_;
253 my $BorrowerUnwantedField =
254 C4
::Context
->preference("PatronSelfRegistrationBorrowerUnwantedField");
256 my @fields = split( /\|/, $BorrowerUnwantedField );
259 #Don't hide mandatory fields
260 next if $mandatory->{$_};
261 $hidden_fields{$_} = 1;
264 return \
%hidden_fields;
267 sub GetMandatoryFields
{
270 my %mandatory_fields;
272 my $BorrowerMandatoryField =
273 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
275 my @fields = split( /\|/, $BorrowerMandatoryField );
278 $mandatory_fields{$_} = 1;
281 if ( $action eq 'create' || $action eq 'new' ) {
282 $mandatory_fields{'email'} = 1
283 if C4
::Context
->boolean_preference(
284 'PatronSelfRegistrationVerifyByEmail');
287 return \
%mandatory_fields;
290 sub CheckMandatoryFields
{
291 my ( $borrower, $action ) = @_;
293 my @empty_mandatory_fields;
295 my $mandatory_fields = GetMandatoryFields
($action);
296 delete $mandatory_fields->{'cardnumber'};
298 foreach my $key ( keys %$mandatory_fields ) {
299 push( @empty_mandatory_fields, $key )
300 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
303 return @empty_mandatory_fields;
306 sub CheckForInvalidFields
{
307 my $borrower = shift;
309 if ($borrower->{'email'}) {
310 push(@invalidFields, "email") if (!Email
::Valid
->address($borrower->{'email'}));
312 if ($borrower->{'emailpro'}) {
313 push(@invalidFields, "emailpro") if (!Email
::Valid
->address($borrower->{'emailpro'}));
315 if ($borrower->{'B_email'}) {
316 push(@invalidFields, "B_email") if (!Email
::Valid
->address($borrower->{'B_email'}));
318 return \
@invalidFields;
321 sub ParseCgiForBorrower
{
324 my $scrubber = C4
::Scrubber
->new();
327 foreach ( $cgi->param ) {
328 if ( $_ =~ '^borrower_' ) {
329 my ($key) = substr( $_, 9 );
330 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
334 $borrower{'dateofbirth'} =
335 C4
::Dates
->new( $borrower{'dateofbirth'} )->output("iso")
336 if ( defined( $borrower{'dateofbirth'} ) );
341 sub DelUnchangedFields
{
342 my ( $borrowernumber, %new_data ) = @_;
344 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
346 foreach my $key ( keys %new_data ) {
347 if ( $current_data->{$key} eq $new_data{$key} ) {
348 delete $new_data{$key};
358 foreach my $key ( keys %borrower ) {
359 delete $borrower{$key} unless $borrower{$key};