Added Paul's LibraryName variable to template
[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 # collect all the fields ...
34 my %search;
35
36 my $forminputs;
37 my $searchdesc = '';
38 foreach my $field (@fields) {
39     $search{$field} = $query->param($field);
40     if ($field eq 'keyword'){
41         $search{$field} = $query->param('words') unless $search{$field};
42     }
43     if ($search{$field}) {
44         push @$forminputs, {field => $field, value => $search{$field}};
45         $searchdesc .= "$field = $search{$field}, ";
46     }
47 }
48
49 $search{'ttype'} = $query->param('ttype');
50 push @$forminputs, {field => 'ttype', value => $search{'ttype'}};
51
52 if (my $subjectitems=$query->param('subjectitems')){
53     $search{'subject'} = $subjectitems;
54     $searchdesc.="subject = $subjectitems, ";
55 }
56
57 @$forminputs=() unless $forminputs;
58 $template->param(FORMINPUTS => $forminputs);
59
60 # do the searchs ....
61 my $env;
62 $env->{itemcount}=1;
63 my $number_of_results = 20;
64 my @results;
65 my $count;
66 my $startfrom = $query->param('startfrom');
67 my $subjectitems=$query->param('subjectitems');
68 if ($subjectitems) {
69     #@results = subsearch($env,$subjectitems, $number_of_results, $startfrom);
70     @results = subsearch($env, $subjectitems);
71     $count = $#results+1;
72 } else {
73     ($count, @results) = catalogsearch($env,'',\%search,$number_of_results,$startfrom);
74 }
75
76 my $num = 1;
77 foreach my $res (@results) {
78     my @items = ItemInfo(undef, $res->{'biblionumber'}, "intra");
79     my $norequests = 1;
80     foreach my $itm (@items) {
81         $norequests = 0 unless $itm->{'notforloan'};
82     }
83     $res->{'norequests'} = $norequests;
84     # set up the even odd elements....
85     $res->{'even'} = 1 if $num % 2 == 0;
86     $res->{'odd'} = 1 if $num % 2 == 1;
87     $num++;
88 }
89
90
91 my $startfrom=$query->param('startfrom');
92 ($startfrom) || ($startfrom=0);
93
94 my $resultsarray=\@results;
95 ($resultsarray) || (@$resultsarray=());
96
97
98 # sorting out which results to display.
99 $template->param(startfrom => $startfrom+1);
100 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
101 $template->param(numrecords => $count);
102 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
103 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$number_of_results) : (-1);
104 $template->param(nextstartfrom => $nextstartfrom);
105 my $displaynext=($nextstartfrom==-1) ? 0 : 1;
106 my $displayprev=($prevstartfrom==-1) ? 0 : 1;
107 $template->param(displaynext => $displaynext);
108 $template->param(displayprev => $displayprev);
109 $template->param(prevstartfrom => $prevstartfrom);
110
111 $template->param(searchdesc => $searchdesc);
112 $template->param(SEARCH_RESULTS => $resultsarray);
113
114 my $numbers;
115 @$numbers = ();
116 if ($count>$number_of_results) {
117     for (my $i=1; $i<$count/$number_of_results+1; $i++) {
118         my $highlight=0;
119         my $themelang = $template->param('themelang');
120         ($startfrom==($i-1)*$number_of_results+1) && ($highlight=1);
121         push @$numbers, { number => $i, highlight => $highlight , startfrom => ($i-1)*$number_of_results+1 };
122     }
123 }
124
125 $template->param(numbers => $numbers,
126                              LibraryName => C4::Context->preference("LibraryName"),
127 );
128
129 output_html_with_http_headers $query, $cookie, $template->output;