bug 5380: remove copy-and-paste from authorities/detail.pl
[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
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 my $subscriptionid = $cgi->param('subscriptionid');
47 my $searchstring   = $cgi->param('member');
48
49 my %categories_dislay;
50 my ($template, $loggedinuser, $cookie);
51     ($template, $loggedinuser, $cookie)
52     = get_template_and_user({template_name => "serials/member-search.tmpl",
53                  query => $cgi,
54                  type => "intranet",
55                  authnotrequired => 0,
56                  flagsrequired => { serials => 'routing' },
57                  });
58
59 foreach my $category (@categories){
60         my $hash={
61                         category_description=>$$category{description},
62                         category_type=>$$category{category_type}
63                          };
64         $categories_dislay{$$category{categorycode}} = $hash;
65 }
66 $template->param(
67         "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
68             );
69 if (C4::Context->preference("AddPatronLists")=~/code/){
70     $categories[0]->{'first'}=1;
71 }
72
73 my $member=$cgi->param('member');
74 my $orderby=$cgi->param('orderby');
75 $orderby = "surname,firstname" unless $orderby;
76 if (defined $member) {
77     $member =~ s/,//g;   #remove any commas from search string
78     $member =~ s/\*/%/g;
79 }
80
81 my ($count,$results);
82
83 if (C4::Context->preference("IndependantBranches")){
84    if (C4::Context->userenv && C4::Context->userenv->{flags} % 2 !=1 && C4::Context->userenv->{'branch'}){
85         $$patron{branchcode}=C4::Context->userenv->{'branch'} unless (C4::Context->userenv->{'branch'} eq "insecure");
86    }
87 }
88 $$patron{firstname}.="\%" if ($$patron{firstname});
89 $$patron{surname}.="\%" if ($$patron{surname});
90
91 my @searchpatron;
92 push @searchpatron, $member if ($member);
93 push @searchpatron, $patron if ( keys %$patron );
94 my $from = ( $startfrom - 1 ) * $resultsperpage;
95 my $to   = $from + $resultsperpage;
96 if (@searchpatron) {
97     ($results) = Search(
98         \@searchpatron,
99         [ { surname => 0 }, { firstname => 0 } ],
100         undef,
101         undef,
102         [ "firstname", "surname", "email", "othernames", "cardnumber" ],
103         "start_with"
104     );
105 }
106 if ($results) {
107     $count = scalar(@$results);
108 }
109 my @resultsdata;
110 $to=($count>$to?$to:$count);
111 my $index=$from;
112 foreach my $borrower(@$results[$from..$to-1]){
113   #find out stats
114
115   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
116
117   my %row = (
118     count => $index++,
119         %$borrower,
120         %{$categories_dislay{$$borrower{categorycode}}},
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-search.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 $cgi, $cookie, $template->output;