Bug 7276 : Follow up, adding a sub to clear the cache
[koha.git] / opac / opac-userupdate.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use Mail::Sendmail;
25
26 use C4::Auth;    # checkauth, getborrowernumber.
27 use C4::Context;
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Output;
31 use C4::Dates qw/format_date/;
32 use C4::Members;
33 use C4::Members::Attributes;
34 use C4::Branch;
35
36 my $query = new CGI;
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39     {
40         template_name   => "opac-userupdate.tmpl",
41         query           => $query,
42         type            => "opac",
43         authnotrequired => 0,
44         flagsrequired   => { borrow => 1 },
45         debug           => 1,
46     }
47 );
48
49 # get borrower information ....
50 my ( $borr ) = GetMemberDetails( $borrowernumber );
51 my ( $patronemail ) = GetFirstValidEmailAddress($borrowernumber);
52 my $lib = GetBranchDetail($borr->{'branchcode'});
53
54 # handle the new information....
55 # collect the form values and send an email.
56 my @fields = (
57     'surname','firstname','othernames','streetnumber','address','address2','city','state','zipcode','country','phone','mobile','fax','phonepro', 'emailaddress','emailpro','B_streetnumber','B_address','B_address2','B_city','B_state','B_zipcode','B_country','B_phone','B_email','dateofbirth','sex'
58 );
59 my $update;
60 my $updateemailaddress = $lib->{'branchemail'};
61 $updateemailaddress = C4::Context->preference('KohaAdminEmailAddress') unless( $updateemailaddress =~ /\w+@\w+/);
62 if ( !$updateemailaddress || $updateemailaddress eq '' ) {
63     warn
64 "KohaAdminEmailAddress system preference not set.  Couldn't send patron update information for $borr->{'firstname'} $borr->{'surname'} (#$borrowernumber)\n";
65     my ($template) = get_template_and_user(
66         {
67             template_name   => "kohaerror.tmpl",
68             query           => $query,
69             type            => "opac",
70             authnotrequired => 1,
71             flagsrequired   => { borrow => 1 },
72             debug           => 1,
73         }
74     );
75
76     $template->param(
77         noadminemail => 1,
78     );
79
80     output_html_with_http_headers $query, $cookie, $template->output;
81     exit;
82 }
83
84 if ( !$patronemail || $patronemail eq '' ) {
85         $patronemail = $updateemailaddress;
86 };
87
88 if ( $query->param('modify') ) {
89
90     # get all the fields:
91     my $message = <<"EOF";
92 Patron $borr->{'cardnumber'}
93
94 has requested to change her/his personal details.
95 Please check these new details and make the changes:
96 EOF
97
98     my $streetnumber = $borr->{'streetnumber'} || '';
99     my $address = $borr->{'address'} || '';
100     my $address2 = $borr->{'address2'} || '';
101     my $B_streetnumber = $borr->{'B_streetnumber'} || '';
102     my $B_address = $borr->{'B_address'} || '';
103     my $B_address2 = $borr->{'B_address2'} || '';
104
105     foreach my $field (@fields) {
106         my $newfield = $query->param($field) || '';
107         my $borrowerfield = '';
108         if($borr->{$field}) {
109             $borrowerfield = $borr->{$field};
110         }
111         
112         if($field eq "dateofbirth") {
113            $borrowerfield  = format_date( $borr->{'dateofbirth'} ) || '';
114         }
115
116         if($borrowerfield eq $newfield) {
117             $message .= "$field : $borrowerfield  -->  $newfield\n";
118         } else {
119             $message .= uc($field) . " : $borrowerfield  -->  $newfield\n";
120         }
121     }
122     $message .= "\n\nThanks,\nKoha\n\n";
123     my %mail = (
124         To      => $updateemailaddress,
125         From    => $patronemail,
126         Subject => "User Request for update of Record.",
127         Message => $message,
128         'Content-Type' => 'text/plain; charset="utf8"',
129     );
130
131     if ( sendmail %mail ) {
132
133         # do something if it works....
134         print $query->redirect('/cgi-bin/koha/opac-user.pl?patronupdate=sent');
135         exit;
136     }
137     else {
138
139         # do something if it doesnt work....
140         warn "Error sending mail: $Mail::Sendmail::error \n";
141     }
142 }
143
144 $borr->{'dateenrolled'} = format_date( $borr->{'dateenrolled'} );
145 $borr->{'dateexpiry'}   = format_date( $borr->{'dateexpiry'} );
146 $borr->{'dateofbirth'}  = format_date( $borr->{'dateofbirth'} );
147 $borr->{'ethnicity'}    = fixEthnicity( $borr->{'ethnicity'} );
148 $borr->{'branchname'}   = GetBranchName($borr->{'branchcode'});
149
150 if (C4::Context->preference('ExtendedPatronAttributes')) {
151     my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
152     if (scalar(@$attributes) > 0) {
153         $borr->{ExtendedPatronAttributes} = 1;
154         $borr->{patron_attributes} = $attributes;
155     }
156 }
157
158 my $checkin_prefs  = C4::Members::Messaging::GetMessagingPreferences({
159     borrowernumber => $borrowernumber,
160     message_name   => 'Item Checkout'
161 });
162 for (@{ $checkin_prefs->{transports} }) {
163     $borr->{"items_returned_$_"} = 1;
164 }
165 my $checkout_prefs = C4::Members::Messaging::GetMessagingPreferences({
166     borrowernumber => $borrowernumber,
167     message_name   => 'Item Check-in'
168 });
169 for (@{ $checkout_prefs->{transports} }) {
170     $borr->{"items_borrowed_$_"} = 1;
171 }
172
173 my @bordat;
174 $bordat[0] = $borr;
175
176 $template->param( 
177     BORROWER_INFO => \@bordat,
178     userupdateview => 1,
179 );
180
181 output_html_with_http_headers $query, $cookie, $template->output;