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.tt",
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) {
59 my $mandatory = GetMandatoryFields
($action);
60 my $hidden = GetHiddenFields
($mandatory);
65 mandatory
=> $mandatory,
66 member_titles
=> GetTitles
() || undef,
67 branches
=> GetBranchesLoop
(),
68 OPACPatronDetails
=> C4
::Context
->preference('OPACPatronDetails'),
71 if ( $action eq 'create' ) {
73 my %borrower = ParseCgiForBorrower
($cgi);
75 %borrower = DelEmptyFields
(%borrower);
77 my @empty_mandatory_fields = CheckMandatoryFields
( \
%borrower, $action );
79 if (@empty_mandatory_fields) {
81 empty_mandatory_fields
=> \
@empty_mandatory_fields,
82 borrower
=> \
%borrower
86 md5_base64
( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
90 borrower
=> \
%borrower
95 C4
::Context
->boolean_preference(
96 'PatronSelfRegistrationVerifyByEmail')
99 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
101 template_name
=> "opac-registration-email-sent.tt",
104 authnotrequired
=> 1,
107 $template->param( 'email' => $borrower{'email'} );
109 my $verification_token = md5_hex
( \
%borrower );
110 $borrower{'password'} = random_string
("..........");
112 Koha
::Borrower
::Modifications
->new(
113 verification_token
=> $verification_token )
114 ->AddModifications(\
%borrower);
116 #Send verification email
117 my $letter = C4
::Letters
::GetPreparedLetter
(
119 letter_code
=> 'OPAC_REG_VERIFY',
121 borrower_modifications
=> $verification_token,
125 C4
::Letters
::EnqueueLetter
(
128 message_transport_type
=> 'email',
129 to_address
=> $borrower{'email'},
131 C4
::Context
->preference('KohaAdminEmailAddress'),
136 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
138 template_name
=> "opac-registration-confirmation.tt",
141 authnotrequired
=> 1,
145 $template->param( OpacPasswordChange
=>
146 C4
::Context
->preference('OpacPasswordChange') );
148 my ( $borrowernumber, $password ) = AddMember_Opac
(%borrower);
150 $template->param( password_cleartext
=> $password );
152 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ) );
154 PatronSelfRegistrationAdditionalInstructions
=>
155 C4
::Context
->preference(
156 'PatronSelfRegistrationAdditionalInstructions')
161 elsif ( $action eq 'update' ) {
163 my %borrower = ParseCgiForBorrower
($cgi);
165 my %borrower_changes = DelEmptyFields
(%borrower);
166 my @empty_mandatory_fields =
167 CheckMandatoryFields
( \
%borrower_changes, $action );
169 if (@empty_mandatory_fields) {
171 empty_mandatory_fields
=> \
@empty_mandatory_fields,
172 borrower
=> \
%borrower
175 $template->param( action
=> 'edit' );
178 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
180 template_name
=> "opac-memberentry-update-submitted.tt",
183 authnotrequired
=> 1,
187 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
190 Koha
::Borrower
::Modifications
->new(
191 borrowernumber
=> $borrowernumber );
193 $m->DelModifications;
194 $m->AddModifications(\
%borrower_changes);
196 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
200 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
201 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
203 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
204 my $attributes = C4
::Members
::Attributes
::GetBorrowerAttributes
($borrowernumber, 'opac');
205 if (scalar(@
$attributes) > 0) {
206 $borrower->{ExtendedPatronAttributes
} = 1;
207 $borrower->{patron_attributes
} = $attributes;
212 borrower
=> $borrower, );
214 if (C4
::Context
->preference('OPACpatronimages')) {
215 my ($image, $dberror) = GetPatronImage
($borrower->{borrowernumber
});
218 display_patron_image
=> 1
225 my $captcha = random_string
("CCCCC");
229 captcha_digest
=> md5_base64
($captcha)
232 output_html_with_http_headers
$cgi, $cookie, $template->output;
234 sub GetHiddenFields
{
235 my ($mandatory) = @_;
238 my $BorrowerUnwantedField =
239 C4
::Context
->preference("PatronSelfRegistrationBorrowerUnwantedField");
241 my @fields = split( /\|/, $BorrowerUnwantedField );
244 #Don't hide mandatory fields
245 next if $mandatory->{$_};
246 $hidden_fields{$_} = 1;
249 return \
%hidden_fields;
252 sub GetMandatoryFields
{
255 my %mandatory_fields;
257 my $BorrowerMandatoryField =
258 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
260 my @fields = split( /\|/, $BorrowerMandatoryField );
263 $mandatory_fields{$_} = 1;
266 if ( $action eq 'create' || $action eq 'new' ) {
267 $mandatory_fields{'email'} = 1
268 if C4
::Context
->boolean_preference(
269 'PatronSelfRegistrationVerifyByEmail');
272 return \
%mandatory_fields;
275 sub CheckMandatoryFields
{
276 my ( $borrower, $action ) = @_;
278 my @empty_mandatory_fields;
280 my $mandatory_fields = GetMandatoryFields
($action);
281 delete $mandatory_fields->{'cardnumber'};
283 foreach my $key ( keys %$mandatory_fields ) {
284 push( @empty_mandatory_fields, $key )
285 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
288 return @empty_mandatory_fields;
291 sub ParseCgiForBorrower
{
294 my $scrubber = C4
::Scrubber
->new();
297 foreach ( $cgi->param ) {
298 if ( $_ =~ '^borrower_' ) {
299 my ($key) = substr( $_, 9 );
300 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
304 $borrower{'dateofbirth'} =
305 C4
::Dates
->new( $borrower{'dateofbirth'} )->output("iso")
306 if ( defined( $borrower{'dateofbirth'} ) );
311 sub DelUnchangedFields
{
312 my ( $borrowernumber, %new_data ) = @_;
314 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
316 foreach my $key ( keys %new_data ) {
317 if ( $current_data->{$key} eq $new_data{$key} ) {
318 delete $new_data{$key};
328 foreach my $key ( keys %borrower ) {
329 delete $borrower{$key} unless $borrower{$key};