Bug 12435 - Update MARC21 frameworks to Update No. 18 (April 2014)
[koha.git] / serials / member-search.pl
blobdfeb37349724cdb8a3540395272f08784a85b199
1 #!/usr/bin/perl
3 # Parts copyright Catalyst IT 2010
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 =head1 member-search.pl
22 Member Search.pl script used to search for members to add to a routing list
24 =cut
26 use strict;
27 use warnings;
28 use CGI;
29 use C4::Auth; # get_template_and_user
30 use C4::Output;
31 use C4::Members; # BornameSearch
32 use C4::Branch;
33 use C4::Category;
34 use File::Basename;
36 my $cgi = new CGI;
37 my $theme = $cgi->param('theme') || "default";
38 my $resultsperpage = $cgi->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
39 my $startfrom = $cgi->param('startfrom')||1;
41 my $patron = $cgi->Vars;
42 foreach (keys %$patron){
43 delete $$patron{$_} unless($$patron{$_});
46 my @categories=C4::Category->all;
47 my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
48 my $subscriptionid = $cgi->param('subscriptionid');
49 my $searchstring = $cgi->param('member');
51 my %categories_dislay;
52 my ($template, $loggedinuser, $cookie);
53 ($template, $loggedinuser, $cookie)
54 = get_template_and_user({template_name => "serials/member-search.tt",
55 query => $cgi,
56 type => "intranet",
57 authnotrequired => 0,
58 flagsrequired => { serials => 'routing' },
59 });
61 foreach my $category (@categories){
62 my $hash={
63 category_description=>$$category{description},
64 category_type=>$$category{category_type}
66 $categories_dislay{$$category{categorycode}} = $hash;
68 $template->param(
69 "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
71 if (C4::Context->preference("AddPatronLists")=~/code/){
72 $categories[0]->{'first'}=1;
75 my $member=$cgi->param('member');
76 my $orderby=$cgi->param('orderby');
77 $orderby = "surname,firstname" unless $orderby;
78 if (defined $member) {
79 $member =~ s/,//g; #remove any commas from search string
80 $member =~ s/\*/%/g;
83 my ($count,$results);
85 if ( C4::Context->preference("IndependentBranches") ) {
86 if ( C4::Context->userenv
87 && !C4::Context->IsSuperLibrarian()
88 && C4::Context->userenv->{'branch'} )
90 $$patron{branchcode} = C4::Context->userenv->{'branch'};
93 $$patron{firstname}.="\%" if ($$patron{firstname});
94 $$patron{surname}.="\%" if ($$patron{surname});
96 my @searchpatron;
97 push @searchpatron, $member if ($member);
98 push @searchpatron, $patron if ( keys %$patron );
99 my $from = ( $startfrom - 1 ) * $resultsperpage;
100 my $to = $from + $resultsperpage;
101 if (@searchpatron) {
102 ($results) = Search(
103 \@searchpatron,
104 [ { surname => 0 }, { firstname => 0 } ],
105 undef,
106 undef,
107 [ "firstname", "surname", "email", "othernames", "cardnumber" ],
108 "start_with"
111 if ($results) {
112 $count = scalar(@$results);
114 my @resultsdata;
115 $to=($count>$to?$to:$count);
116 my $index=$from;
117 foreach my $borrower(@$results[$from..$to-1]){
118 # find out stats
119 $borrower->{'dateexpiry'}= C4::Dates->new($borrower->{'dateexpiry'},'iso')->output('syspref');
120 if ($categories_dislay{$borrower->{'categorycode'}}){
121 my %row = (
122 count => $index++,
123 %$borrower,
124 %{$categories_dislay{$$borrower{categorycode}}},
126 push(@resultsdata, \%row);
128 else {
129 warn $borrower->{'cardnumber'} ." has a bad category code of " . $borrower->{'categorycode'} ."\n";
132 if ($$patron{branchcode}){
133 foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
134 $$branch{selected}=1;
137 if ($$patron{categorycode}){
138 foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
139 $$category{selected}=1;
142 my %parameters=
143 ( %{$patron},
144 'orderby' => $orderby,
145 'resultsperpage' => $resultsperpage,
146 'type'=> 'intranet');
147 my $base_url =
148 'member-search.pl?&'
149 . join(
150 '&',
151 map { "$_=$parameters{$_}" } (keys %parameters)
154 $template->param(
155 paginationbar => pagination_bar(
156 $base_url, int( $count / $resultsperpage ) + 1,
157 $startfrom, 'startfrom'
159 startfrom => $startfrom,
160 from => ($startfrom-1)*$resultsperpage+1,
161 to => $to,
162 multipage => ($count != $to+1 || $startfrom!=1),
164 $template->param(
165 branchloop=>$branches,
166 categoryloop=>\@categories,
170 $template->param(
171 searching => "1",
172 actionname => basename($0),
173 %$patron,
174 numresults => $count,
175 resultsloop => \@resultsdata,
178 output_html_with_http_headers $cgi, $cookie, $template->output;