Bug 11715: require authentication for various staff scripts
[koha.git] / members / member.pl
1 #!/usr/bin/perl
2
3
4 #script to do a borrower enquiry/bring up borrower details etc
5 #written 20/12/99 by chris@katipo.co.nz
6
7
8 # Copyright 2000-2002 Katipo Communications
9 # Copyright 2010 BibLibre
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along
23 # with Koha; if not, write to the Free Software Foundation, Inc.,
24 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 use Modern::Perl;
27 use C4::Auth;
28 use C4::Output;
29 use CGI;
30 use C4::Members;
31 use C4::Branch;
32 use C4::Category;
33 use Koha::DateUtils;
34 use Koha::List::Patron;
35
36 my $input = new CGI;
37 my $quicksearch = $input->param('quicksearch') || '';
38 my $startfrom = $input->param('startfrom') || 1;
39 my $resultsperpage = $input->param('resultsperpage') || C4::Context->preference("PatronsPerPage") || 20;
40
41 my ($template, $loggedinuser, $cookie)
42     = get_template_and_user({template_name => "members/member.tmpl",
43                  query => $input,
44                  type => "intranet",
45                  authnotrequired => 0,
46                  flagsrequired => {borrowers => 1},
47                  });
48
49 my $theme = $input->param('theme') || "default";
50
51 my $add_to_patron_list       = $input->param('add_to_patron_list');
52 my $add_to_patron_list_which = $input->param('add_to_patron_list_which');
53 my $new_patron_list          = $input->param('new_patron_list');
54 my @borrowernumbers          = $input->param('borrowernumber');
55 $input->delete(
56     'add_to_patron_list', 'add_to_patron_list_which',
57     'new_patron_list',    'borrowernumber',
58 );
59
60 my $patron = $input->Vars;
61 foreach (keys %$patron){
62         delete $$patron{$_} unless($$patron{$_});
63 }
64 my @categories=C4::Category->all;
65
66 my $branches = GetBranches;
67 my @branchloop;
68
69 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
70   my $selected;
71   $selected = 1 if $patron->{branchcode} && $branches->{$_}->{branchcode} eq $patron->{branchcode};
72   my %row = ( value => $_,
73         selected => $selected,
74         branchname => $branches->{$_}->{branchname},
75       );
76   push @branchloop, \%row;
77 }
78
79 my %categories_dislay;
80
81 foreach my $category (@categories){
82         my $hash={
83                         category_description=>$$category{description},
84                         category_type=>$$category{category_type}
85                          };
86         $categories_dislay{$$category{categorycode}} = $hash;
87 }
88 my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
89 $template->param( 
90         "AddPatronLists_$AddPatronLists" => "1",
91             );
92 if ($AddPatronLists=~/code/){
93     $categories[0]->{'first'}=1;
94 }  
95
96 my $member=$input->param('member') || '';
97 my $orderbyparams=$input->param('orderby') || '';
98 my @orderby;
99 if ($orderbyparams){
100         my @orderbyelt=split(/,/,$orderbyparams);
101         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
102 }
103 else {
104         @orderby = ({surname=>0},{firstname=>0});
105 }
106
107 $member =~ s/,//g;   #remove any commas from search string
108 $member =~ s/\*/%/g;
109
110 my $from = ( $startfrom - 1 ) * $resultsperpage;
111 my $to   = $from + $resultsperpage;
112
113 my ($count,$results);
114 if ($member || keys %$patron) {
115     my $searchfields = $input->param('searchfields') || '';
116     my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
117
118     if ( $searchfields eq "dateofbirth" ) {
119         $member = output_pref({dt => dt_from_string($member), dateformat => 'iso', dateonly => 1});
120     }
121
122     my $searchtype = $input->param('searchtype');
123     my $search_scope =
124         $quicksearch ? "field_start_with"
125       : $searchtype  ? $searchtype
126       :                "start_with";
127
128     ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
129 }
130
131 if ($add_to_patron_list) {
132     my $patron_list;
133
134     if ( $add_to_patron_list eq 'new' ) {
135         $patron_list = AddPatronList( { name => $new_patron_list } );
136     }
137     else {
138         $patron_list =
139           [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0];
140     }
141
142     if ( $add_to_patron_list_which eq 'all' ) {
143         @borrowernumbers = map { $_->{borrowernumber} } @$results;
144     }
145
146     my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
147
148     $template->param(
149         patron_list           => $patron_list,
150         patrons_added_to_list => \@patrons_added_to_list,
151       )
152 }
153
154 if ($results) {
155         for my $field ('categorycode','branchcode'){
156                 next unless ($patron->{$field});
157                 @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
158         }
159     $count = scalar(@$results);
160 } else {
161     $count = 0;
162 }
163
164 if($count == 1){
165     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
166     exit;
167 }
168
169 my @resultsdata;
170 $to=($count>$to?$to:$count);
171 my $index=$from;
172 foreach my $borrower(@$results[$from..$to-1]){
173   #find out stats
174   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
175   $fines ||= 0;
176   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
177
178   my %row = (
179     count => $index++,
180     %$borrower,
181     (defined $categories_dislay{ $borrower->{categorycode} }?   %{ $categories_dislay{ $borrower->{categorycode} } }:()),
182     overdues => $od,
183     issues => $issue,
184     odissue => "$od/$issue",
185     fines =>  sprintf("%.2f",$fines),
186     branchname => $branches->{$borrower->{branchcode}}->{branchname},
187     );
188   push(@resultsdata, \%row);
189 }
190
191 if ($$patron{categorycode}){
192         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
193                 $$category{selected}=1;
194         }
195 }
196 my %parameters=
197         (  %$patron
198                 , 'orderby'                     => $orderbyparams 
199                 , 'resultsperpage'      => $resultsperpage 
200         , 'type'=> 'intranet'); 
201 my $base_url =
202     'member.pl?&'
203   . join(
204     '&',
205     map { "$_=$parameters{$_}" } (keys %parameters)
206   );
207
208 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
209
210 $template->param(
211     %$patron,
212     letters       => \@letters,
213     paginationbar => pagination_bar(
214         $base_url,
215         int( $count / $resultsperpage ) + ( $count % $resultsperpage ? 1 : 0 ),
216         $startfrom,
217         'startfrom'
218     ),
219     startfrom    => $startfrom,
220     from         => ( $startfrom - 1 ) * $resultsperpage + 1,
221     to           => $to,
222     multipage    => ( $count != $to || $startfrom != 1 ),
223     advsearch    => ( $$patron{categorycode} || $$patron{branchcode} ),
224     branchloop   => \@branchloop,
225     categories   => \@categories,
226     searching    => "1",
227     numresults   => $count,
228     resultsloop  => \@resultsdata,
229     results_per_page => $resultsperpage,
230     member => $member,
231     search_parameters => \%parameters,
232     patron_lists => [ GetPatronLists() ],
233 );
234
235 output_html_with_http_headers $input, $cookie, $template->output;