Code to allow the associated borrowers to work
[koha.git] / opac / opac-userupdate.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5 use Mail::Sendmail;
6
7 use C4::Auth;         # checkauth, getborrowernumber.
8 use C4::Context;
9 use C4::Koha;
10 use C4::Circulation::Circ2;
11 use C4::Interface::CGI::Output;
12 use HTML::Template;
13 use C4::Date;
14
15 my $query = new CGI;
16
17 my ($template, $borrowernumber, $cookie) 
18     = get_template_and_user({template_name => "opac-userupdate.tmpl",
19                              query => $query,
20                              type => "opac",
21                              authnotrequired => 0,
22                              flagsrequired => {borrow => 1},
23                              debug => 1,
24                              });
25
26 # get borrower information ....
27 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
28
29
30 # handle the new information....
31 # collect the form values and send an email.
32 my @fields = ('title', 'surname', 'firstname', 'phone', 'faxnumber', 'streetaddress', 'emailaddress', 'city');
33 my $update;
34 my $updateemailaddress= C4::Context->preference('KohaAdminEmailAddress');
35 if ($updateemailaddress eq '') {
36     warn "KohaAdminEmailAddress system preference not set.  Couldn't send patron update information for $borr->{'firstname'} $borr->{'surname'} (#$borrowernumber)\n";
37     my($template) = get_template_and_user({template_name => "kohaerror.tmpl",
38                              query => $query,
39                              type => "opac",
40                              authnotrequired => 1,
41                              flagsrequired => {borrow => 1},
42                              debug => 1,
43                              });
44
45     $template->param(errormessage => 'KohaAdminEmailAddress system preference
46     is not set.  Please visit the library to update your user record');
47
48     output_html_with_http_headers $query, $cookie, $template->output;
49     exit;
50 }
51
52 if ($query->{'title'}) {
53     # get all the fields:
54     my $message = <<"EOF";
55 Borrower $borr->{'cardnumber'}
56
57 has requested to change her/his personal details.
58 Please check these new details and make the changes:
59 EOF
60     foreach my $field (@fields){
61         my $newfield = $query->param($field);
62         $message .= "$field : $borr->{$field}  -->  $newfield\n";
63     }
64     $message .= "\n\nThanks,\nKoha\n\n";
65     my %mail = ( To      => $updateemailaddress ,
66                  From    => $updateemailaddress ,
67                  Subject => "User Request for update of Record.",
68                  Message => $message );
69     if (sendmail %mail) {
70 # do something if it works....
71         warn "Mail sent ok\n";
72         print $query->redirect('/cgi-bin/koha/opac-user.pl');
73         exit;
74     } else {
75 # do something if it doesnt work....
76         warn "Error sending mail: $Mail::Sendmail::error \n";
77     }
78 }
79
80
81 $borr->{'dateenrolled'} = format_date($borr->{'dateenrolled'});
82 $borr->{'expiry'}       = format_date($borr->{'expiry'});
83 $borr->{'dateofbirth'}  = format_date($borr->{'dateofbirth'});
84 $borr->{'ethnicity'}    = fixEthnicity($borr->{'ethnicity'});
85
86
87 my @bordat;
88 $bordat[0] = $borr;
89
90 $template->param(BORROWER_INFO => \@bordat,
91 );
92
93 output_html_with_http_headers $query, $cookie, $template->output;