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