[SIGNED-OFF]Bug 2780 - Capitalize strings consistently (tools)
[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 strict;
27 #use warnings; FIXME - Bug 2505
28 use C4::Auth;
29 use C4::Output;
30 use CGI;
31 use C4::Members;
32 use C4::Branch;
33 use C4::Category;
34 use File::Basename;
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 $patron = $input->Vars;
52 foreach (keys %$patron){
53         delete $$patron{$_} unless($$patron{$_});
54 }
55 my @categories=C4::Category->all;
56
57 my $branches = GetBranches;
58 my @branchloop;
59
60 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
61   my $selected = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
62   my %row = ( value => $_,
63         selected => $selected,
64         branchname => $branches->{$_}->{branchname},
65       );
66   push @branchloop, \%row;
67 }
68
69 my %categories_dislay;
70
71 foreach my $category (@categories){
72         my $hash={
73                         category_description=>$$category{description},
74                         category_type=>$$category{category_type}
75                          };
76         $categories_dislay{$$category{categorycode}} = $hash;
77 }
78 my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
79 $template->param( 
80         "AddPatronLists_$AddPatronLists" => "1",
81             );
82 if ($AddPatronLists=~/code/){
83     $categories[0]->{'first'}=1;
84 }  
85
86 my $member=$input->param('member');
87 my $orderbyparams=$input->param('orderby');
88 my @orderby;
89 if ($orderbyparams){
90         my @orderbyelt=split(/,/,$orderbyparams);
91         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
92 }
93 else {
94         @orderby = ({surname=>0},{firstname=>0});
95 }
96
97 my $searchfields = $input->param('searchfields');
98 my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
99
100 $member =~ s/,//g;   #remove any commas from search string
101 $member =~ s/\*/%/g;
102
103 my $from = ( $startfrom - 1 ) * $resultsperpage;
104 my $to   = $from + $resultsperpage;
105
106 my ($count,$results);
107 if ($member || keys %$patron) {
108     #($results)=Search($member || $patron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  );
109     my $search_scope = ( $quicksearch ? "field_start_with" : "start_with" );
110     ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
111 }
112
113 if ($results) {
114         for my $field ('categorycode','branchcode'){
115                 next unless ($patron->{$field});
116                 @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
117         }
118     $count = scalar(@$results);
119 }
120
121 if($count == 1){
122     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
123     exit;
124 }
125
126 my @resultsdata;
127 $to=($count>$to?$to:$count);
128 my $index=$from;
129 foreach my $borrower(@$results[$from..$to-1]){
130   #find out stats
131   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
132
133   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
134
135   my %row = (
136     count => $index++,
137         %$borrower,
138         %{$categories_dislay{$$borrower{categorycode}}},
139     overdues => $od,
140     issues => $issue,
141     odissue => "$od/$issue",
142     fines =>  sprintf("%.2f",$fines),
143     );
144   push(@resultsdata, \%row);
145 }
146
147 if ($$patron{categorycode}){
148         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
149                 $$category{selected}=1;
150         }
151 }
152 my %parameters=
153         (  %$patron
154                 , 'orderby'                     => $orderbyparams 
155                 , 'resultsperpage'      => $resultsperpage 
156         , 'type'=> 'intranet'); 
157 my $base_url =
158     'member.pl?&'
159   . join(
160     '&',
161     map { "$_=$parameters{$_}" } (keys %parameters)
162   );
163
164 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
165
166 $template->param(
167     letters => \@letters,
168     paginationbar => pagination_bar(
169         $base_url,
170         int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
171         $startfrom, 'startfrom'
172     ),
173     startfrom => $startfrom,
174     from      => ($startfrom-1)*$resultsperpage+1,  
175     to        => $to,
176     multipage => ($count != $to || $startfrom!=1),
177     advsearch => ($$patron{categorycode} || $$patron{branchcode}),
178     branchloop=>\@branchloop,
179     categories=>\@categories,
180     searching       => "1",
181                 actionname              =>basename($0),
182                 %$patron,
183         numresults      => $count,
184         resultsloop     => \@resultsdata,
185             );
186
187 output_html_with_http_headers $input, $cookie, $template->output;