3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use Digest
::MD5
qw( md5_base64 md5_hex );
22 use String
::Random
qw( random_string );
27 use Koha
::Borrower
::Modifications
;
28 use C4
::Branch
qw(GetBranchesLoop);
32 my $dbh = C4
::Context
->dbh;
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
36 template_name
=> "opac-memberentry.tmpl",
43 unless ( C4
::Context
->preference('PatronSelfRegistration') || $borrowernumber )
45 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
49 my $action = $cgi->param('action') || q{};
50 if ( $action eq q{} ) {
51 if ($borrowernumber) {
61 hidden
=> GetHiddenFields
(),
62 mandatory
=> GetMandatoryFields
($action),
63 member_titles
=> GetTitles
() || undef,
64 branches
=> GetBranchesLoop
(),
65 OPACPatronDetails
=> C4
::Context
->preference('OPACPatronDetails'),
68 if ( $action eq 'create' ) {
70 my %borrower = ParseCgiForBorrower
($cgi);
72 %borrower = DelEmptyFields
(%borrower);
74 my @empty_mandatory_fields = CheckMandatoryFields
( \
%borrower, $action );
76 if (@empty_mandatory_fields) {
78 empty_mandatory_fields
=> \
@empty_mandatory_fields,
79 borrower
=> \
%borrower
83 md5_base64
( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
87 borrower
=> \
%borrower
92 C4
::Context
->boolean_preference(
93 'PatronSelfRegistrationVerifyByEmail')
96 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
98 template_name
=> "opac-registration-email-sent.tmpl",
101 authnotrequired
=> 1,
104 $template->param( 'email' => $borrower{'email'} );
106 my $verification_token = md5_hex
( \
%borrower );
107 $borrower{'password'} = random_string
("..........");
109 Koha
::Borrower
::Modifications
->new(
110 verification_token
=> $verification_token )
111 ->AddModifications(\
%borrower);
113 #Send verification email
114 my $letter = C4
::Letters
::GetPreparedLetter
(
116 letter_code
=> 'OPAC_REG_VERIFY',
118 borrower_modifications
=>
119 [ $verification_token, $verification_token ],
123 C4
::Letters
::EnqueueLetter
(
126 message_transport_type
=> 'email',
127 to_address
=> $borrower{'email'},
129 C4
::Context
->preference('KohaAdminEmailAddress'),
134 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
136 template_name
=> "opac-registration-confirmation.tmpl",
139 authnotrequired
=> 1,
143 $template->param( OpacPasswordChange
=>
144 C4
::Context
->preference('OpacPasswordChange') );
146 my ( $borrowernumber, $password ) = AddMember_Opac
(%borrower);
148 $template->param( password_cleartext
=> $password );
150 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ) );
152 PatronSelfRegistrationAdditionalInstructions
=>
153 C4
::Context
->preference(
154 'PatronSelfRegistrationAdditionalInstructions')
159 elsif ( $action eq 'update' ) {
161 my %borrower = ParseCgiForBorrower
($cgi);
163 my %borrower_changes = DelEmptyFields
(%borrower);
164 my @empty_mandatory_fields =
165 CheckMandatoryFields
( \
%borrower_changes, $action );
167 if (@empty_mandatory_fields) {
169 empty_mandatory_fields
=> \
@empty_mandatory_fields,
170 borrower
=> \
%borrower
173 $template->param( action
=> 'edit' );
176 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
178 template_name
=> "opac-memberentry-update-submitted.tmpl",
181 authnotrequired
=> 1,
185 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
188 Koha
::Borrower
::Modifications
->new(
189 borrowernumber
=> $borrowernumber );
191 $m->DelModifications;
192 $m->AddModifications(\
%borrower_changes);
194 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
198 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
199 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
201 borrower
=> $borrower, );
203 if (C4
::Context
->preference('OPACpatronimages')) {
204 my ($image, $dberror) = GetPatronImage
($borrower->{borrowernumber
});
207 display_patron_image
=> 1
214 my $captcha = random_string
("CCCCC");
218 captcha_digest
=> md5_base64
($captcha)
221 output_html_with_http_headers
$cgi, $cookie, $template->output;
223 sub GetHiddenFields
{
226 my $BorrowerUnwantedField =
227 C4
::Context
->preference("PatronSelfRegistrationBorrowerUnwantedField");
229 my @fields = split( /\|/, $BorrowerUnwantedField );
232 $hidden_fields{$_} = 1;
235 return \
%hidden_fields;
238 sub GetMandatoryFields
{
241 my %mandatory_fields;
243 my $BorrowerMandatoryField =
244 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
246 my @fields = split( /\|/, $BorrowerMandatoryField );
249 $mandatory_fields{$_} = 1;
252 if ( $action eq 'create' || $action eq 'new' ) {
253 $mandatory_fields{'email'} = 1
254 if C4
::Context
->boolean_preference(
255 'PatronSelfRegistrationVerifyByEmail');
258 return \
%mandatory_fields;
261 sub CheckMandatoryFields
{
262 my ( $borrower, $action ) = @_;
264 my @empty_mandatory_fields;
266 my $mandatory_fields = GetMandatoryFields
($action);
267 delete $mandatory_fields->{'cardnumber'};
269 foreach my $key ( keys %$mandatory_fields ) {
270 push( @empty_mandatory_fields, $key )
271 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
274 return @empty_mandatory_fields;
277 sub ParseCgiForBorrower
{
280 my $scrubber = C4
::Scrubber
->new();
283 foreach ( $cgi->param ) {
284 if ( $_ =~ '^borrower_' ) {
285 my ($key) = substr( $_, 9 );
286 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
290 $borrower{'dateofbirth'} =
291 C4
::Dates
->new( $borrower{'dateofbirth'} )->output("iso")
292 if ( defined( $borrower{'dateofbirth'} ) );
297 sub DelUnchangedFields
{
298 my ( $borrowernumber, %new_data ) = @_;
300 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
302 foreach my $key ( keys %new_data ) {
303 if ( $current_data->{$key} eq $new_data{$key} ) {
304 delete $new_data{$key};
314 foreach my $key ( keys %borrower ) {
315 delete $borrower{$key} unless $borrower{$key};