HTL mod for till reconciliation.
[koha.git] / opac / opac-searchresults.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5 use C4::Search;
6 use C4::Auth;
7 use C4::Interface::CGI::Output;
8 use HTML::Template;
9
10 my $query=new CGI;
11
12 my ($template, $borrowernumber, $cookie)
13     = get_template_and_user({template_name => "opac-searchresults.tmpl",
14                              query => $query,
15                              type => "opac",
16                              authnotrequired => 1,
17                          });
18
19
20 my $subject=$query->param('subject');
21
22
23
24 if ($subject) {
25     $template->param(subjectsearch => $subject);
26 }
27
28 # get all the search variables
29 # we assume that C4::Search will validate these values for us
30 my @fields = ('keyword', 'subject', 'author', 'illustrator', 'itemnumber', 'isbn', 'date-before', 'date-after', 'class', 'dewey', 'branch', 'title', 'abstract', 'publisher');
31
32 # collect all the fields ...
33 my %search;
34
35 my $forminputs;
36 my $searchdesc = '';
37 foreach my $field (@fields) {
38     $search{$field} = $query->param($field);
39     if ($field eq 'keyword'){
40         $search{$field} = $query->param('words') unless $search{$field};
41     }
42     if ($search{$field}) {
43         push @$forminputs, {field => $field, value => $search{$field}};
44         $searchdesc .= "$field = $search{$field}, ";
45     }
46 }
47
48 $search{'ttype'} = $query->param('ttype');
49 push @$forminputs, {field => 'ttype', value => $search{'ttype'}};
50
51 if (my $subjectitems=$query->param('subjectitems')){
52     $search{'subject'} = $subjectitems;
53     $searchdesc.="subject = $subjectitems, ";
54 }
55
56 @$forminputs=() unless $forminputs;
57 $template->param(FORMINPUTS => $forminputs);
58
59 # do the searchs ....
60 my $env;
61 $env->{itemcount}=1;
62 my $number_of_results = 20;
63 my @results;
64 my $count;
65 my $startfrom = $query->param('startfrom');
66 my $subjectitems=$query->param('subjectitems');
67 if ($subjectitems) {
68     #@results = subsearch($env,$subjectitems, $number_of_results, $startfrom);
69     @results = subsearch($env, $subjectitems);
70     $count = $#results+1;
71 } else {
72     ($count, @results) = catalogsearch($env,'',\%search,$number_of_results,$startfrom);
73 }
74
75 my $num = 1;
76 foreach my $res (@results) {
77     my @items = ItemInfo(undef, $res->{'biblionumber'}, "intra");
78     my $norequests = 1;
79     foreach my $itm (@items) {
80         $norequests = 0 unless $itm->{'notforloan'};
81     }
82     $res->{'norequests'} = $norequests;
83     # set up the even odd elements....
84     $res->{'even'} = 1 if $num % 2 == 0;
85     $res->{'odd'} = 1 if $num % 2 == 1;
86     $num++;
87 }
88
89
90 my $startfrom=$query->param('startfrom');
91 ($startfrom) || ($startfrom=0);
92
93 my $resultsarray=\@results;
94 ($resultsarray) || (@$resultsarray=());
95
96
97 # sorting out which results to display.
98 $template->param(startfrom => $startfrom+1);
99 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
100 $template->param(numrecords => $count);
101 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
102 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$number_of_results) : (-1);
103 $template->param(nextstartfrom => $nextstartfrom);
104 my $displaynext=($nextstartfrom==-1) ? 0 : 1;
105 my $displayprev=($prevstartfrom==-1) ? 0 : 1;
106 $template->param(displaynext => $displaynext);
107 $template->param(displayprev => $displayprev);
108 $template->param(prevstartfrom => $prevstartfrom);
109
110 $template->param(searchdesc => $searchdesc);
111 $template->param(SEARCH_RESULTS => $resultsarray);
112
113 my $numbers;
114 @$numbers = ();
115 if ($count>$number_of_results) {
116     for (my $i=1; $i<$count/$number_of_results+1; $i++) {
117         my $highlight=0;
118         my $themelang = $template->param('themelang');
119         ($startfrom==($i-1)*$number_of_results+1) && ($highlight=1);
120         push @$numbers, { number => $i, highlight => $highlight , startfrom => ($i-1)*$number_of_results+1 };
121     }
122 }
123
124 $template->param(numbers => $numbers,
125                              LibraryName => C4::Context->preference("LibraryName"),
126                                 suggestion => C4::Context->preference("suggestion"),
127                                 virtualshelves => C4::Context->preference("virtualshelves"),
128 );
129
130 output_html_with_http_headers $query, $cookie, $template->output;