fixed a couple of bugs. Note this version of opac-reserve.pl is designed to work...
[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                              flagsrequired => {borrow => 1},
18                          });
19
20
21 my $subject=$query->param('subject');
22
23
24
25 if ($subject) {
26     $template->param(subjectsearch => $subject);
27 }
28
29 # get all the search variables
30 # we assume that C4::Search will validate these values for us
31 my @fields = ('keyword', 'subject', 'author', 'illustrator', 'itemnumber', 'isbn', 'date-before', 'date-after', 'class', 'dewey', 'branch', 'title', 'abstract', 'publisher');
32
33
34
35 # collect all the fields ...
36 my %search;
37
38 my $forminputs;
39 my $searchdesc = '';
40 foreach my $field (@fields) {
41     $search{$field} = $query->param($field);
42     if ($field eq 'keyword'){
43         $search{$field} = $query->param('words') unless $search{$field};
44     }
45     if ($search{$field}) {
46         push @$forminputs, {field => $field, value => $search{$field}};
47         $searchdesc .= "$field = $search{$field}, ";
48     }
49 }
50
51 $search{'ttype'} = $query->param('ttype');
52 push @$forminputs, {field => 'ttype', value => $search{'ttype'}};
53
54 if (my $subjectitems=$query->param('subjectitems')){
55     $search{'subject'} = $subjectitems;
56     $searchdesc.="subject = $subjectitems, ";
57 }
58
59 @$forminputs=() unless $forminputs;
60 $template->param(FORMINPUTS => $forminputs);
61
62 # do the searchs ....
63 my $env;
64 $env->{itemcount}=1;
65 my $number_of_results = 20;
66 my @results;
67 my $count;
68 my $startfrom = $query->param('startfrom');
69 my $subjectitems=$query->param('subjectitems');
70 if ($subjectitems) {
71     #@results = subsearch($env,$subjectitems, $number_of_results, $startfrom);
72     @results = subsearch($env, $subjectitems);
73     $count = $#results+1;
74 } else {
75     ($count, @results) = catalogsearch($env,'',\%search,$number_of_results,$startfrom);
76 }
77
78 my $num = 1;
79 foreach my $res (@results) {
80     my @items = ItemInfo(undef, $res->{'biblionumber'}, "intra");
81     my $norequests = 1;
82     foreach my $itm (@items) {
83         $norequests = 0 unless $itm->{'notforloan'};
84     }
85     $res->{'norequests'} = $norequests;
86     # set up the even odd elements....
87     $res->{'even'} = 1 if $num % 2 == 0;
88     $res->{'odd'} = 1 if $num % 2 == 1;
89     $num++;
90 }
91
92
93 my $startfrom=$query->param('startfrom');
94 ($startfrom) || ($startfrom=0);
95
96 my $resultsarray=\@results;
97 ($resultsarray) || (@$resultsarray=());
98
99
100 # sorting out which results to display.
101 $template->param(startfrom => $startfrom+1);
102 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
103 $template->param(numrecords => $count);
104 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
105 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
106 $template->param(nextstartfrom => $nextstartfrom);
107 my $displaynext=($nextstartfrom==-1) ? 0 : 1;
108 my $displayprev=($prevstartfrom==-1) ? 0 : 1;
109 $template->param(displaynext => $displaynext);
110 $template->param(displayprev => $displayprev);
111 $template->param(prevstartfrom => $prevstartfrom);
112
113 $template->param(searchdesc => $searchdesc);
114 $template->param(SEARCH_RESULTS => $resultsarray);
115
116 my $numbers;
117 @$numbers = ();
118 if ($count>10) {
119     for (my $i=1; $i<$count/10+1; $i++) {
120         my $highlight=0;
121         my $themelang = $template->param('themelang');
122         ($startfrom==($i-1)*10) && ($highlight=1);
123         push @$numbers, { number => $i, highlight => $highlight , startfrom => ($i-1)*10 };
124     }
125 }
126
127 $template->param(numbers => $numbers);
128
129 output_html_with_http_headers $query, $cookie, $template->output;