Bug 10363: There is no package for authorised values.
[koha.git] / opac / opac-memberentry.pl
blob9a18afa47a3f8e8bc6b513facef1aac22ffc8488
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use CGI qw ( -utf8 );
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 C4::Form::MessagingPreferences;
28 use Koha::Borrower::Modifications;
29 use C4::Branch qw(GetBranchesLoop);
30 use C4::Scrubber;
31 use Email::Valid;
33 my $cgi = new CGI;
34 my $dbh = C4::Context->dbh;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 template_name => "opac-memberentry.tt",
39 type => "opac",
40 query => $cgi,
41 authnotrequired => 1,
45 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
47 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
48 exit;
51 my $action = $cgi->param('action') || q{};
52 if ( $action eq q{} ) {
53 if ($borrowernumber) {
54 $action = 'edit';
56 else {
57 $action = 'new';
61 my $mandatory = GetMandatoryFields($action);
62 my $hidden = GetHiddenFields($mandatory);
64 $template->param(
65 action => $action,
66 hidden => $hidden,
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) {
83 $template->param(
84 empty_mandatory_fields => \@empty_mandatory_fields,
85 invalid_form_fields => $invalidformfields,
86 borrower => \%borrower
89 elsif (
90 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
92 $template->param(
93 failed_captcha => 1,
94 borrower => \%borrower
97 else {
98 if (
99 C4::Context->boolean_preference(
100 'PatronSelfRegistrationVerifyByEmail')
103 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
105 template_name => "opac-registration-email-sent.tt",
106 type => "opac",
107 query => $cgi,
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(
122 module => 'members',
123 letter_code => 'OPAC_REG_VERIFY',
124 tables => {
125 borrower_modifications => $verification_token,
129 C4::Letters::EnqueueLetter(
131 letter => $letter,
132 message_transport_type => 'email',
133 to_address => $borrower{'email'},
134 from_address =>
135 C4::Context->preference('KohaAdminEmailAddress'),
139 else {
140 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
142 template_name => "opac-registration-confirmation.tt",
143 type => "opac",
144 query => $cgi,
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 );
156 $template->param(
157 borrower => GetMember( borrowernumber => $borrowernumber ) );
158 $template->param(
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) {
176 $template->param(
177 empty_mandatory_fields => \@empty_mandatory_fields,
178 invalid_form_fields => $invalidformfields,
179 borrower => \%borrower
182 $template->param( action => 'edit' );
184 else {
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",
190 type => "opac",
191 query => $cgi,
192 authnotrequired => 1,
196 my $m =
197 Koha::Borrower::Modifications->new(
198 borrowernumber => $borrowernumber );
200 $m->DelModifications;
201 $m->AddModifications(\%borrower_changes);
202 $template->param(
203 borrower => GetMember( borrowernumber => $borrowernumber ),
206 else {
207 $template->param(
208 action => 'edit',
209 nochanges => 1,
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;
226 $template->param(
227 borrower => $borrower, );
229 if (C4::Context->preference('OPACpatronimages')) {
230 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
231 if ($image) {
232 $template->param(
233 display_patron_image => 1
240 my $captcha = random_string("CCCCC");
242 $template->param(
243 captcha => $captcha,
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) = @_;
251 my %hidden_fields;
253 my $BorrowerUnwantedField =
254 C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
256 my @fields = split( /\|/, $BorrowerUnwantedField );
257 foreach (@fields) {
258 next unless m/\w/o;
259 #Don't hide mandatory fields
260 next if $mandatory->{$_};
261 $hidden_fields{$_} = 1;
264 return \%hidden_fields;
267 sub GetMandatoryFields {
268 my ($action) = @_;
270 my %mandatory_fields;
272 my $BorrowerMandatoryField =
273 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
275 my @fields = split( /\|/, $BorrowerMandatoryField );
277 foreach (@fields) {
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;
308 my @invalidFields;
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 {
322 my ($cgi) = @_;
324 my $scrubber = C4::Scrubber->new();
325 my %borrower;
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'} ) );
338 return %borrower;
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};
352 return %new_data;
355 sub DelEmptyFields {
356 my (%borrower) = @_;
358 foreach my $key ( keys %borrower ) {
359 delete $borrower{$key} unless $borrower{$key};
362 return %borrower;