Removed trailing whitespace.
[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::Output; # now contains gettemplate
8
9 my $query=new CGI;
10
11 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, 1);
12
13
14 my $template = gettemplate ("opac-searchresults.tmpl", "opac");
15
16
17
18 my $subject=$query->param('subject');
19
20
21 if ($subject) {
22     $template->param(subjectsearch => $subject);
23 }
24
25 # get all the search variables
26 # we assume that C4::Search will validate these values for us
27 my @fields = ('keyword', 'subject', 'author', 'illustrator', 'itemnumber', 'isbn', 'date-before', 'date-after', 'class', 'dewey', 'branch', 'title', 'abstract', 'publisher');
28
29
30
31 # collect all the fields ...
32 my %search;
33 my $forminputs;
34 my $searchdesc = '';
35 foreach my $field (@fields) {
36     $search{$field} = $query->param($field);
37     if ($search{$field}) {
38         push @$forminputs, {field => $field, value => $search{$field}};
39         $searchdesc .= "$field = $search{$field}, ";
40     }
41 }
42 $search{'ttype'} = $query->param('ttype');
43 push @$forminputs, {field => 'ttype', value => $search{'ttype'}};
44
45 if (my $subjectitems=$query->param('subjectitems')){
46     $search{'subject'} = $subjectitems;
47     $searchdesc.="subject = $subjectitems, ";
48 }
49
50 @$forminputs=() unless $forminputs;
51 $template->param(FORMINPUTS => $forminputs);
52
53 # do the searchs ....
54 my $env;
55 $env->{itemcount}=1;
56 my $num=10;
57 my @results;
58 my $count;
59 my $startfrom = $query->param('startfrom');
60 my $subjectitems=$query->param('subjectitems');
61 if ($subjectitems) {
62     my $blah;
63     @results = subsearch(\$blah,$subjectitems, $num, $startfrom);
64     $count = $#results+1;
65 } else {
66     ($count, @results) = catalogsearch($env,'',\%search,$num,$startfrom);
67 }
68
69 foreach my $res (@results) {
70     my @items = ItemInfo(undef, $res->{'biblionumber'}, "intra");
71     my $norequests = 1;
72     foreach my $itm (@items) {
73         $norequests = 0 unless $itm->{'notforloan'};
74     }
75     $res->{'norequests'} = $norequests;
76 }
77
78
79 my $startfrom=$query->param('startfrom');
80 ($startfrom) || ($startfrom=0);
81
82 my $resultsarray=\@results;
83 ($resultsarray) || (@$resultsarray=());
84
85
86 # sorting out which results to display.
87 $template->param(startfrom => $startfrom+1);
88 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
89 $template->param(numrecords => $count);
90 my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
91 my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
92 $template->param(nextstartfrom => $nextstartfrom);
93 my $displaynext=1;
94 my $displayprev=0;
95 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
96 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
97 $template->param(displaynext => $displaynext);
98 $template->param(displayprev => $displayprev);
99 $template->param(prevstartfrom => $prevstartfrom);
100
101 $template->param(searchdesc => $searchdesc);
102 $template->param(SEARCH_RESULTS => $resultsarray);
103 $template->param(loggedinuser => $loggedinuser);
104
105 my $numbers;
106 @$numbers = ();
107 if ($count>10) {
108     for (my $i=1; $i<$count/10+1; $i++) {
109         my $highlight=0;
110         my $themelang = $template->param('themelang');
111         ($startfrom==($i-1)*10) && ($highlight=1);
112         push @$numbers, { number => $i, highlight => $highlight , startfrom => ($i-1)*10 };
113     }
114 }
115
116 $template->param(numbers => $numbers);
117
118 $template->param(loggedinuser => $loggedinuser);
119
120 print $query->header(-cookie => $cookie), $template->output;
121