merged in changes from the rel-1-2 branch into the main branch. Some of the default...
[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
8 my $query=new CGI;
9
10 my ($template, $borrowernumber, $cookie) 
11     = get_template_and_user({template_name => "opac-searchresults.tmpl",
12                              query => $query,
13                              type => "opac",
14                              authnotrequired => 1,
15                              flagsrequired => {borrow => 1},
16                          });
17
18
19 my $subject=$query->param('subject');
20
21
22
23 if ($subject) {
24     $template->param(subjectsearch => $subject);
25 }
26
27 # get all the search variables
28 # we assume that C4::Search will validate these values for us
29 my @fields = ('keyword', 'subject', 'author', 'illustrator', 'itemnumber', 'isbn', 'date-before', 'date-after', 'class', 'dewey', 'branch', 'title', 'abstract', 'publisher');
30
31
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     $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-$num) : (-1);
103 $template->param(nextstartfrom => $nextstartfrom);
104 my $displaynext=1;
105 my $displayprev=0;
106 ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
107 ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
108 $template->param(displaynext => $displaynext);
109 $template->param(displayprev => $displayprev);
110 $template->param(prevstartfrom => $prevstartfrom);
111
112 $template->param(searchdesc => $searchdesc);
113 $template->param(SEARCH_RESULTS => $resultsarray);
114
115 my $numbers;
116 @$numbers = ();
117 if ($count>10) {
118     for (my $i=1; $i<$count/10+1; $i++) {
119         my $highlight=0;
120         my $themelang = $template->param('themelang');
121         ($startfrom==($i-1)*10) && ($highlight=1);
122         push @$numbers, { number => $i, highlight => $highlight , startfrom => ($i-1)*10 };
123     }
124 }
125
126 $template->param(numbers => $numbers);
127
128 print $query->header(-cookie => $cookie), $template->output;
129