Adding userid to search in member.pl
[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 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use C4::Auth;
27 use C4::Output;
28 use CGI;
29 use C4::Members;
30 use C4::Branch;
31 use C4::Category;
32 use File::Basename;
33
34 my $input = new CGI;
35 my $quicksearch = $input->param('quicksearch');
36 my $startfrom = $input->param('startfrom')||1;
37 my $resultsperpage = $input->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
38
39 my ($template, $loggedinuser, $cookie);
40 if($quicksearch){
41     ($template, $loggedinuser, $cookie)
42     = get_template_and_user({template_name => "members/member-quicksearch-results.tmpl",
43                  query => $input,
44                  type => "intranet",
45                  authnotrequired => 0,
46                  flagsrequired => {borrowers => 1},
47                  });
48 } else {
49     ($template, $loggedinuser, $cookie)
50     = get_template_and_user({template_name => "members/member.tmpl",
51                  query => $input,
52                  type => "intranet",
53                  authnotrequired => 0,
54                  flagsrequired => {borrowers => 1},
55                  });
56 }
57 my $theme = $input->param('theme') || "default";
58
59 my $patron = $input->Vars;
60 foreach (keys %$patron){
61         delete $$patron{$_} unless($$patron{$_}); 
62 }
63
64 my @categories=C4::Category->all;
65 my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
66
67 my %categories_dislay;
68
69 foreach my $category (@categories){
70         my $hash={
71                         category_description=>$$category{description},
72                         category_type=>$$category{category_type}
73                          };
74         $categories_dislay{$$category{categorycode}} = $hash;
75 }
76 $template->param( 
77         "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
78             );
79 if (C4::Context->preference("AddPatronLists")=~/code/){
80     $categories[0]->{'first'}=1;
81 }  
82
83 my $member=$input->param('member');
84 my $orderby=$input->param('orderby');
85 $orderby = "surname,firstname" unless $orderby;
86 $member =~ s/,//g;   #remove any commas from search string
87 $member =~ s/\*/%/g;
88
89 my ($count,$results);
90
91 $$patron{firstname}.="\%" if ($$patron{firstname});
92 $$patron{surname}.="\%" if ($$patron{surname});
93
94 my @searchpatron;
95 push @searchpatron, $member if ($member);
96 push @searchpatron, $patron if (keys %$patron);
97 my $from= ($startfrom-1)*$resultsperpage;
98 my $to=$from+$resultsperpage;
99  #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
100  ($results)=Search(\@searchpatron,{surname=>1,firstname=>1},undef,undef,["firstname","surname","email","othernames","cardnumber","userid"],"start_with"  ) if (@searchpatron);
101 if ($results){
102         $count =scalar(@$results);
103 }
104 my @resultsdata;
105 my $to=($count>$to?$to:$count);
106 my $index=$from;
107 foreach my $borrower(@$results[$from..$to-1]){
108   #find out stats
109   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
110
111   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
112
113   my %row = (
114     count => $index++,
115         %$borrower,
116         %{$categories_dislay{$$borrower{categorycode}}},
117     overdues => $od,
118     issues => $issue,
119     odissue => "$od/$issue",
120     fines =>  sprintf("%.2f",$fines),
121     );
122   push(@resultsdata, \%row);
123 }
124
125 if ($$patron{branchcode}){
126         foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
127                 $$branch{selected}=1;
128         }
129 }
130 if ($$patron{categorycode}){
131         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
132                 $$category{selected}=1;
133         }
134 }
135 my %parameters=
136         (  %$patron
137                 , 'orderby'                     => $orderby 
138                 , 'resultsperpage'      => $resultsperpage 
139         , 'type'=> 'intranet'); 
140 my $base_url =
141     'member.pl?&'
142   . join(
143     '&',
144     map { "$_=$parameters{$_}" } (keys %parameters)
145   );
146
147 $template->param(
148     paginationbar => pagination_bar(
149         $base_url,  int( $count / $resultsperpage ) + 1,
150         $startfrom, 'startfrom'
151     ),
152     startfrom => $startfrom,
153     from      => ($startfrom-1)*$resultsperpage+1,  
154     to        => $to,
155     multipage => ($count != $to+1 || $startfrom!=1),
156 );
157 $template->param(
158     branchloop=>$branches,
159         categoryloop=>\@categories,
160 );
161
162
163 $template->param( 
164         searching       => "1",
165                 actionname              =>basename($0),
166                 %$patron,
167         numresults      => $count,
168         resultsloop     => \@resultsdata,
169             );
170
171 output_html_with_http_headers $input, $cookie, $template->output;