Sync to same file in 1.2 branch
[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 HTML::Template;
12
13
14 my $query = new CGI;
15
16 my ($template, $borrowernumber, $cookie) 
17     = get_template_and_user({template_name => "opac-userupdate.tmpl",
18                              query => $query,
19                              type => "opac",
20                              authnotrequired => 0,
21                              flagsrequired => {borrow => 1},
22                              debug => 1,
23                              });
24
25 # get borrower information ....
26 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
27
28
29 # handle the new information....
30 # collect the form values and send an email.
31 my @fields = ('title', 'surname', 'firstname', 'phone', 'faxnumber', 'streetaddress', 'emailaddress', 'city');
32 my $update;
33 my $updateemailaddress= C4::Context->preference('KohaAdminEmailAddress');
34 if ($updateemailaddress eq '') {
35     warn "KohaAdminEmailAddress system preference not set.  Couldn't send patron update information for $borr->{'firstname'} $borr->{'surname'} (#$borrowernumber)\n";
36     my $template = gettemplate("kohaerror.tmpl", "opac");
37
38     $template->param(errormessage => 'KohaAdminEmailAddress system preference
39     is not set.  Please visit the library to update your user record');
40
41     print $query->header(), $template->output;
42     exit;
43 }
44
45 if ($query->{'title'}) {
46     # get all the fields:
47     my $message = <<"EOF";
48 Borrower $borr->{'cardnumber'} http://intradev.katipo.co.nz/cgi-bin/koha/moremember.pl?bornum=$borrowernumber
49
50 has requested to change their personal details. Please check these new details and make the changes:
51 EOF
52     foreach my $field (@fields){
53         my $newfield = $query->param($field);
54         $message .= "$field : $borr->{$field}  -->  $newfield\n";
55     }
56     $message .= "\n\nThanks,\nKoha\n\n";
57     my %mail = ( To      => $updateemailaddress ,
58                  From    => $updateemailaddress ,
59                  Subject => "User Request for update of Record.",
60                  Message => $message );
61     if (sendmail %mail) {
62 # do something if it works....
63         warn "Mail sent ok\n";
64         print $query->redirect('/cgi-bin/koha/opac-user.pl');
65         exit;
66     } else {
67 # do something if it doesnt work....
68         warn "Error sending mail: $Mail::Sendmail::error \n";
69     }
70 }
71
72
73 $borr->{'dateenrolled'} = slashifyDate($borr->{'dateenrolled'});
74 $borr->{'expiry'}       = slashifyDate($borr->{'expiry'});
75 $borr->{'dateofbirth'}  = slashifyDate($borr->{'dateofbirth'});
76 $borr->{'ethnicity'}    = fixEthnicity($borr->{'ethnicity'});
77
78
79 my @bordat;
80 $bordat[0] = $borr;
81
82 $template->param(BORROWER_INFO => \@bordat);
83
84 output_html_with_http_headers $query, $cookie, $template->output;