Fix so that if you edit a borrower from circ, or change a password from circ you...
[koha.git] / members / member-password.pl
1 #!/usr/bin/perl
2 #script to set the password, and optionally a userid, for a borrower
3 #written 2/5/00
4 #by chris@katipo.co.nz
5 #converted to using templates 3/16/03 by mwhansen@hmc.edu
6
7 use strict;
8 use C4::Auth;
9 use C4::Output;
10 use C4::Context;
11 use C4::Members;
12 use C4::Circulation;
13 use CGI;
14
15 use Digest::MD5 qw(md5_base64);
16
17 my $input = new CGI;
18
19 my $theme = $input->param('theme') || "default";
20                         # only used if allowthemeoverride is set
21
22 my ($template, $loggedinuser, $cookie)
23     = get_template_and_user({template_name => "members/member-password.tmpl",
24                              query => $input,
25                              type => "intranet",
26                              authnotrequired => 0,
27                              flagsrequired => {borrowers => 1},
28                              debug => 1,
29                              });
30
31 my $flagsrequired;
32 $flagsrequired->{borrowers}=1;
33 my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired);
34
35 my $member=$input->param('member');
36 my $cardnumber = $input->param('cardnumber');
37 my $destination = $input->param('destination');
38
39 my %member2;
40 $member2{'borrowernumber'}=$member;
41 # my $issues=GetBorrowerIssues(\%member2);
42 # my $i=0;
43 # foreach (sort keys %$issues) {
44 #     $i++;
45 # }
46
47 my ($bor,$flags)=GetMemberDetails( $member,'');
48 my $newpassword = $input->param('newpassword');
49
50 if ( $newpassword ) {
51     my $digest=md5_base64($input->param('newpassword'));
52     my $uid = $input->param('newuserid');
53     my $dbh=C4::Context->dbh;
54     warn $destination;
55     if (changepassword($uid,$member,$digest)) {
56         $template->param(newpassword => $newpassword);
57         if ($destination eq 'circ') {
58             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");               
59         } 
60         else {
61             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
62         }
63     } 
64     else {
65         $template->param(othernames => $bor->{'othernames'},
66                                                 surname     => $bor->{'surname'},
67                                                 firstname   => $bor->{'firstname'},
68                                                 userid      => $bor->{'userid'},
69                                                 defaultnewpassword => $newpassword );
70     }
71 } else {
72     my $userid = $bor->{'userid'};
73
74     my $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
75     my $length=int(rand(2))+4;
76     my $defaultnewpassword='';
77     for (my $i=0; $i<$length; $i++) {
78         $defaultnewpassword.=substr($chars, int(rand(length($chars))),1);
79     }
80     $template->param( othernames => $bor->{'othernames'},
81             surname     => $bor->{'surname'},
82             firstname   => $bor->{'firstname'},
83             borrowernumber => $bor->{'borrowernumber'},
84             cardnumber => $bor->{'cardnumber'},
85             categorycode => $bor->{'categorycode'},
86             category_type => $bor->{'category_type'},
87             category_description => $bor->{'description'},
88             address => $bor->{'address'},
89             address2 => $bor->{'address2'},
90             city => $bor->{'city'},
91             zipcode => $bor->{'zipcode'},
92             phone => $bor->{'phone'},
93             email => $bor->{'email'},
94             branchcode => $bor->{'branchcode'},
95             userid      => $bor->{'userid'},
96             destination => $destination,
97             defaultnewpassword => $defaultnewpassword 
98         );
99
100
101 }
102
103 $template->param( member => $member);
104
105 output_html_with_http_headers $input, $cookie, $template->output;