Bug 10052 Followup for edithelp.tt and help-top.inc
[koha.git] / opac / opac-memberentry.pl
blob112d405fb2959c432762baa76b8c98413870aa40
1 #!/usr/bin/perl
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
8 # version.
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.
18 use Modern::Perl;
20 use CGI;
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
24 use C4::Auth;
25 use C4::Output;
26 use C4::Members;
27 use Koha::Borrower::Modifications;
28 use C4::Branch qw(GetBranchesLoop);
30 my $cgi = new CGI;
31 my $dbh = C4::Context->dbh;
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35 template_name => "opac-memberentry.tmpl",
36 type => "opac",
37 query => $cgi,
38 authnotrequired => 1,
42 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
44 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
45 exit;
48 my $action = $cgi->param('action') || q{};
49 if ( $action eq q{} ) {
50 if ($borrowernumber) {
51 $action = 'edit';
53 else {
54 $action = 'new';
58 $template->param(
59 action => $action,
60 hidden => GetHiddenFields(),
61 mandatory => GetMandatoryFields($action),
62 member_titles => GetTitles(),
63 branches => GetBranchesLoop(),
64 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
67 if ( $action eq 'create' ) {
69 my %borrower = ParseCgiForBorrower($cgi);
71 %borrower = DelEmptyFields(%borrower);
73 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
75 if (@empty_mandatory_fields) {
76 $template->param(
77 empty_mandatory_fields => \@empty_mandatory_fields,
78 borrower => \%borrower
81 elsif (
82 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
84 $template->param(
85 failed_captcha => 1,
86 borrower => \%borrower
89 else {
90 if (
91 C4::Context->boolean_preference(
92 'PatronSelfRegistrationVerifyByEmail')
95 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
97 template_name => "opac-registration-email-sent.tmpl",
98 type => "opac",
99 query => $cgi,
100 authnotrequired => 1,
103 $template->param( 'email' => $borrower{'email'} );
105 my $verification_token = md5_hex( \%borrower );
106 $borrower{'password'} = random_string("..........");
108 Koha::Borrower::Modifications->new(
109 verification_token => $verification_token )
110 ->AddModifications(\%borrower);
112 #Send verification email
113 my $letter = C4::Letters::GetPreparedLetter(
114 module => 'members',
115 letter_code => 'OPAC_REG_VERIFY',
116 tables => {
117 borrower_modifications =>
118 [ $verification_token, $verification_token ],
122 C4::Letters::EnqueueLetter(
124 letter => $letter,
125 message_transport_type => 'email',
126 to_address => $borrower{'email'},
127 from_address =>
128 C4::Context->preference('KohaAdminEmailAddress'),
132 else {
133 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
135 template_name => "opac-registration-confirmation.tmpl",
136 type => "opac",
137 query => $cgi,
138 authnotrequired => 1,
142 $template->param( OpacPasswordChange =>
143 C4::Context->preference('OpacPasswordChange') );
145 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
147 $template->param( password_cleartext => $password );
148 $template->param(
149 borrower => GetMember( borrowernumber => $borrowernumber ) );
150 $template->param(
151 PatronSelfRegistrationAdditionalInstructions =>
152 C4::Context->preference(
153 'PatronSelfRegistrationAdditionalInstructions')
158 elsif ( $action eq 'update' ) {
160 my %borrower = ParseCgiForBorrower($cgi);
162 my %borrower_changes = DelEmptyFields(%borrower);
163 my @empty_mandatory_fields =
164 CheckMandatoryFields( \%borrower_changes, $action );
166 if (@empty_mandatory_fields) {
167 $template->param(
168 empty_mandatory_fields => \@empty_mandatory_fields,
169 borrower => \%borrower
172 $template->param( action => 'edit' );
174 else {
175 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
177 template_name => "opac-memberentry-update-submitted.tmpl",
178 type => "opac",
179 query => $cgi,
180 authnotrequired => 1,
184 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
186 my $m =
187 Koha::Borrower::Modifications->new(
188 borrowernumber => $borrowernumber );
190 $m->DelModifications;
191 $m->AddModifications(\%borrower_changes);
192 $template->param(
193 borrower => GetMember( borrowernumber => $borrowernumber ),
197 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
198 my $borrower = GetMember( borrowernumber => $borrowernumber );
199 $template->param(
200 borrower => $borrower, );
202 if (C4::Context->preference('OPACpatronimages')) {
203 my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
204 if ($image) {
205 $template->param(
206 display_patron_image => 1
213 my $captcha = random_string("CCCCC");
215 $template->param(
216 captcha => $captcha,
217 captcha_digest => md5_base64($captcha)
220 output_html_with_http_headers $cgi, $cookie, $template->output;
222 sub GetHiddenFields {
223 my %hidden_fields;
225 my $BorrowerUnwantedField =
226 C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
228 my @fields = split( /\|/, $BorrowerUnwantedField );
229 foreach (@fields) {
230 next unless m/\w/o;
231 $hidden_fields{$_} = 1;
234 return \%hidden_fields;
237 sub GetMandatoryFields {
238 my ($action) = @_;
240 my %mandatory_fields;
242 my $BorrowerMandatoryField =
243 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
245 my @fields = split( /\|/, $BorrowerMandatoryField );
247 foreach (@fields) {
248 $mandatory_fields{$_} = 1;
251 if ( $action eq 'create' || $action eq 'new' ) {
252 $mandatory_fields{'email'} = 1
253 if C4::Context->boolean_preference(
254 'PatronSelfRegistrationVerifyByEmail');
257 return \%mandatory_fields;
260 sub CheckMandatoryFields {
261 my ( $borrower, $action ) = @_;
263 my @empty_mandatory_fields;
265 my $mandatory_fields = GetMandatoryFields($action);
266 delete $mandatory_fields->{'cardnumber'};
268 foreach my $key ( keys %$mandatory_fields ) {
269 push( @empty_mandatory_fields, $key )
270 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
273 return @empty_mandatory_fields;
276 sub ParseCgiForBorrower {
277 my ($cgi) = @_;
279 my %borrower;
281 foreach ( $cgi->param ) {
282 if ( $_ =~ '^borrower_' ) {
283 my ($key) = substr( $_, 9 );
284 $borrower{$key} = $cgi->param($_);
288 $borrower{'dateofbirth'} =
289 C4::Dates->new( $borrower{'dateofbirth'} )->output("iso")
290 if ( defined( $borrower{'dateofbirth'} ) );
292 return %borrower;
295 sub DelUnchangedFields {
296 my ( $borrowernumber, %new_data ) = @_;
298 my $current_data = GetMember( borrowernumber => $borrowernumber );
300 foreach my $key ( keys %new_data ) {
301 if ( $current_data->{$key} eq $new_data{$key} ) {
302 delete $new_data{$key};
306 return %new_data;
309 sub DelEmptyFields {
310 my (%borrower) = @_;
312 foreach my $key ( keys %borrower ) {
313 delete $borrower{$key} unless $borrower{$key};
316 return %borrower;