Passing "adultborrower" variable to the template so that "Add Child" button can be...
[wip/koha-chris_n.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
34 #my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired);
35
36 my $member=$input->param('member');
37 my $cardnumber = $input->param('cardnumber');
38 my $destination = $input->param('destination');
39
40 my $errormsg;
41 my ($bor,$flags)=GetMemberDetails( $member,'');
42 if(( $member ne $loggedinuser ) && ($bor->{'category_type'} eq 'S' || $bor->{'authflags'}->{'catalogue'}) ) {
43         my $luser = GetMemberDetails($loggedinuser);
44         $errormsg = 'NOPERMISSION' unless($luser->{'authflags'}->{'staffaccess'} );
45 }
46 my $newpassword = $input->param('newpassword');
47 my $minpw = C4::Context->preference('minPasswordLength');
48 $errormsg = 'SHORTPASSWORD' if( $newpassword && $minpw & (length($newpassword) < $minpw ) );
49
50 if ( $newpassword  && ! $errormsg ) {
51     my $digest=md5_base64($input->param('newpassword'));
52     my $uid = $input->param('newuserid');
53     my $dbh=C4::Context->dbh;
54     if (changepassword($uid,$member,$digest)) {
55                 $template->param(newpassword => $newpassword);
56                 if ($destination eq 'circ') {
57                     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");               
58                 } else {
59                     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
60                 }
61     } else {
62                         $errormsg = 'BADUSERID';
63             $template->param(othernames => $bor->{'othernames'},
64                                                 surname     => $bor->{'surname'},
65                                                 firstname   => $bor->{'firstname'},
66                                                 userid      => $bor->{'userid'},
67                                                 defaultnewpassword => $newpassword 
68                                                 );
69     }
70 } else {
71     my $userid = $bor->{'userid'};
72
73     my $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
74     my $length=int(rand(2))+4;
75     my $defaultnewpassword='';
76     for (my $i=0; $i<$length; $i++) {
77         $defaultnewpassword.=substr($chars, int(rand(length($chars))),1);
78     }
79         
80         my $borrowercategory = GetBorrowercategory( $bor->{'categorycode'} );
81 my $category_type = $borrowercategory->{'category_type'};
82 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
83         
84     $template->param( othernames => $bor->{'othernames'},
85             surname     => $bor->{'surname'},
86             firstname   => $bor->{'firstname'},
87             borrowernumber => $bor->{'borrowernumber'},
88             cardnumber => $bor->{'cardnumber'},
89             categorycode => $bor->{'categorycode'},
90             category_type => $bor->{'category_type'},
91             category_description => $bor->{'description'},
92             address => $bor->{'address'},
93             address2 => $bor->{'address2'},
94             city => $bor->{'city'},
95             zipcode => $bor->{'zipcode'},
96             phone => $bor->{'phone'},
97             email => $bor->{'email'},
98             branchcode => $bor->{'branchcode'},
99             userid      => $bor->{'userid'},
100             destination => $destination,
101             defaultnewpassword => $defaultnewpassword 
102         );
103
104
105 }
106
107 $template->param( member => $member,
108                                         errormsg => $errormsg,
109                                         $errormsg => 1 ,
110                                         minPasswordLength => $minpw );
111
112 output_html_with_http_headers $input, $cookie, $template->output;