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);
33 my $dbh = C4
::Context
->dbh;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
37 template_name
=> "opac-memberentry.tt",
44 unless ( C4
::Context
->preference('PatronSelfRegistration') || $borrowernumber )
46 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
50 my $action = $cgi->param('action') || q{};
51 if ( $action eq q{} ) {
52 if ($borrowernumber) {
60 my $mandatory = GetMandatoryFields
($action);
61 my $hidden = GetHiddenFields
($mandatory);
66 mandatory
=> $mandatory,
67 member_titles
=> GetTitles
() || undef,
68 branches
=> GetBranchesLoop
(),
69 OPACPatronDetails
=> C4
::Context
->preference('OPACPatronDetails'),
72 if ( $action eq 'create' ) {
74 my %borrower = ParseCgiForBorrower
($cgi);
76 %borrower = DelEmptyFields
(%borrower);
78 my @empty_mandatory_fields = CheckMandatoryFields
( \
%borrower, $action );
80 if (@empty_mandatory_fields) {
82 empty_mandatory_fields
=> \
@empty_mandatory_fields,
83 borrower
=> \
%borrower
87 md5_base64
( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
91 borrower
=> \
%borrower
96 C4
::Context
->boolean_preference(
97 'PatronSelfRegistrationVerifyByEmail')
100 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
102 template_name
=> "opac-registration-email-sent.tt",
105 authnotrequired
=> 1,
108 $template->param( 'email' => $borrower{'email'} );
110 my $verification_token = md5_hex
( \
%borrower );
111 $borrower{'password'} = random_string
("..........");
113 Koha
::Borrower
::Modifications
->new(
114 verification_token
=> $verification_token )
115 ->AddModifications(\
%borrower);
117 #Send verification email
118 my $letter = C4
::Letters
::GetPreparedLetter
(
120 letter_code
=> 'OPAC_REG_VERIFY',
122 borrower_modifications
=> $verification_token,
126 C4
::Letters
::EnqueueLetter
(
129 message_transport_type
=> 'email',
130 to_address
=> $borrower{'email'},
132 C4
::Context
->preference('KohaAdminEmailAddress'),
137 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
139 template_name
=> "opac-registration-confirmation.tt",
142 authnotrequired
=> 1,
146 $template->param( OpacPasswordChange
=>
147 C4
::Context
->preference('OpacPasswordChange') );
149 my ( $borrowernumber, $password ) = AddMember_Opac
(%borrower);
150 C4
::Form
::MessagingPreferences
::handle_form_action
($cgi, { borrowernumber
=> $borrowernumber }, $template, 1, C4
::Context
->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4
::Context
->preference('EnhancedMessagingPreferences');
152 $template->param( password_cleartext
=> $password );
154 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ) );
156 PatronSelfRegistrationAdditionalInstructions
=>
157 C4
::Context
->preference(
158 'PatronSelfRegistrationAdditionalInstructions')
163 elsif ( $action eq 'update' ) {
165 my %borrower = ParseCgiForBorrower
($cgi);
167 my %borrower_changes = DelEmptyFields
(%borrower);
168 my @empty_mandatory_fields =
169 CheckMandatoryFields
( \
%borrower_changes, $action );
171 if (@empty_mandatory_fields) {
173 empty_mandatory_fields
=> \
@empty_mandatory_fields,
174 borrower
=> \
%borrower
177 $template->param( action
=> 'edit' );
180 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
182 template_name
=> "opac-memberentry-update-submitted.tt",
185 authnotrequired
=> 1,
189 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
192 Koha
::Borrower
::Modifications
->new(
193 borrowernumber
=> $borrowernumber );
195 $m->DelModifications;
196 $m->AddModifications(\
%borrower_changes);
198 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
202 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
203 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
205 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
206 my $attributes = C4
::Members
::Attributes
::GetBorrowerAttributes
($borrowernumber, 'opac');
207 if (scalar(@
$attributes) > 0) {
208 $borrower->{ExtendedPatronAttributes
} = 1;
209 $borrower->{patron_attributes
} = $attributes;
214 borrower
=> $borrower, );
216 if (C4
::Context
->preference('OPACpatronimages')) {
217 my ($image, $dberror) = GetPatronImage
($borrower->{borrowernumber
});
220 display_patron_image
=> 1
227 my $captcha = random_string
("CCCCC");
231 captcha_digest
=> md5_base64
($captcha)
234 output_html_with_http_headers
$cgi, $cookie, $template->output, undef, { force_no_caching
=> 1 };
236 sub GetHiddenFields
{
237 my ($mandatory) = @_;
240 my $BorrowerUnwantedField =
241 C4
::Context
->preference("PatronSelfRegistrationBorrowerUnwantedField");
243 my @fields = split( /\|/, $BorrowerUnwantedField );
246 #Don't hide mandatory fields
247 next if $mandatory->{$_};
248 $hidden_fields{$_} = 1;
251 return \
%hidden_fields;
254 sub GetMandatoryFields
{
257 my %mandatory_fields;
259 my $BorrowerMandatoryField =
260 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
262 my @fields = split( /\|/, $BorrowerMandatoryField );
265 $mandatory_fields{$_} = 1;
268 if ( $action eq 'create' || $action eq 'new' ) {
269 $mandatory_fields{'email'} = 1
270 if C4
::Context
->boolean_preference(
271 'PatronSelfRegistrationVerifyByEmail');
274 return \
%mandatory_fields;
277 sub CheckMandatoryFields
{
278 my ( $borrower, $action ) = @_;
280 my @empty_mandatory_fields;
282 my $mandatory_fields = GetMandatoryFields
($action);
283 delete $mandatory_fields->{'cardnumber'};
285 foreach my $key ( keys %$mandatory_fields ) {
286 push( @empty_mandatory_fields, $key )
287 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
290 return @empty_mandatory_fields;
293 sub ParseCgiForBorrower
{
296 my $scrubber = C4
::Scrubber
->new();
299 foreach ( $cgi->param ) {
300 if ( $_ =~ '^borrower_' ) {
301 my ($key) = substr( $_, 9 );
302 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
306 $borrower{'dateofbirth'} =
307 C4
::Dates
->new( $borrower{'dateofbirth'} )->output("iso")
308 if ( defined( $borrower{'dateofbirth'} ) );
313 sub DelUnchangedFields
{
314 my ( $borrowernumber, %new_data ) = @_;
316 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
318 foreach my $key ( keys %new_data ) {
319 if ( $current_data->{$key} eq $new_data{$key} ) {
320 delete $new_data{$key};
330 foreach my $key ( keys %borrower ) {
331 delete $borrower{$key} unless $borrower{$key};