Adding verbose parameter to allow for more detailed view in opac basket (requires...
[koha.git] / opac / opac-search.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4
5 use C4::Auth;
6 use C4::Interface::CGI::Output;
7 use C4::Context;
8 use CGI;
9 use C4::Database;
10 use HTML::Template;
11 use C4::SearchMarc;
12 use C4::Acquisition;
13 use C4::Biblio;
14 # use C4::Search;
15
16 my $classlist='';
17
18 my $dbh=C4::Context->dbh;
19 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
20 $sth->execute;
21 while (my ($description,$itemtype) = $sth->fetchrow) {
22     $classlist.="<option value=\"$itemtype\">$description</option>\n";
23 }
24
25
26 my $query = new CGI;
27 my $op = $query->param("op");
28 my $type=$query->param('type');
29
30 my $startfrom=$query->param('startfrom');
31 $startfrom=0 if(!defined $startfrom);
32 my ($template, $loggedinuser, $cookie);
33 my $resultsperpage;
34 my $searchdesc;
35
36 if ($op eq "do_search") {
37         my @marclist = $query->param('marclist');
38         my @and_or = $query->param('and_or');
39         my @excluding = $query->param('excluding');
40         my @operator = $query->param('operator');
41         my @value = $query->param('value');
42
43         for (my $i=0;$i<=$#marclist;$i++) {
44                 if ($searchdesc) { # don't put the and_or on the 1st search term
45                         $searchdesc .= $and_or[$i]." ".$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
46                 } else {
47                         $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
48                 }
49         }
50         $resultsperpage= $query->param('resultsperpage');
51         $resultsperpage = 19 if(!defined $resultsperpage);
52         my $orderby = $query->param('orderby');
53
54         # builds tag and subfield arrays
55         my @tags;
56
57         foreach my $marc (@marclist) {
58                 if ($marc) {
59                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
60                         if ($tag) {
61                                 push @tags,$dbh->quote("$tag$subfield");
62                         } else {
63                                 push @tags, $dbh->quote(substr($marc,0,4));
64                         }
65                 } else {
66                         push @tags, "";
67                 }
68         }
69         findseealso($dbh,\@tags);
70         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
71                                                                                 \@excluding, \@operator, \@value,
72                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby);
73
74         ($template, $loggedinuser, $cookie)
75                 = get_template_and_user({template_name => "opac-searchresults.tmpl",
76                                 query => $query,
77                                 type => 'opac',
78                                 authnotrequired => 1,
79                                 debug => 1,
80                                 });
81
82         # multi page display gestion
83         my $displaynext=0;
84         my $displayprev=$startfrom;
85         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
86                 $displaynext = 1;
87         }
88
89         my @field_data = ();
90
91
92         for(my $i = 0 ; $i <= $#marclist ; $i++)
93         {
94                 push @field_data, { term => "marclist", val=>$marclist[$i] };
95                 push @field_data, { term => "and_or", val=>$and_or[$i] };
96                 push @field_data, { term => "excluding", val=>$excluding[$i] };
97                 push @field_data, { term => "operator", val=>$operator[$i] };
98                 push @field_data, { term => "value", val=>$value[$i] };
99         }
100
101         my @numbers = ();
102
103         if ($total>$resultsperpage)
104         {
105                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
106                 {
107                         if ($i<16)
108                         {
109                         my $highlight=0;
110                         ($startfrom==($i-1)) && ($highlight=1);
111                         push @numbers, { number => $i,
112                                         highlight => $highlight ,
113                                         searchdata=> \@field_data,
114                                         startfrom => ($i-1)};
115                         }
116         }
117         }
118
119         my $from = $startfrom*$resultsperpage+1;
120         my $to;
121
122         if($total < (($startfrom+1)*$resultsperpage))
123         {
124                 $to = $total;
125         } else {
126                 $to = (($startfrom+1)*$resultsperpage);
127         }
128         my $defaultview = 'BiblioDefaultView'.C4::Context->preference('BiblioDefaultView');
129         $template->param(results => $results,
130                                                         startfrom=> $startfrom,
131                                                         displaynext=> $displaynext,
132                                                         displayprev=> $displayprev,
133                                                         resultsperpage => $resultsperpage,
134                                                         orderby => $orderby,
135                                                         startfromnext => $startfrom+1,
136                                                         startfromprev => $startfrom-1,
137                                                         searchdata=>\@field_data,
138                                                         total=>$total,
139                                                         from=>$from,
140                                                         to=>$to,
141                                                         numbers=>\@numbers,
142                                                         searchdesc=> $searchdesc,
143                                                         $defaultview => 1,
144                                                         );
145
146 } else {
147         ($template, $loggedinuser, $cookie)
148                 = get_template_and_user({template_name => "opac-search.tmpl",
149                                         query => $query,
150                                         type => "opac",
151                                         authnotrequired => 1,
152                                 });
153         
154         
155         $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
156         $sth->execute;
157         my  @itemtype;
158         my %itemtypes;
159         push @itemtype, "";
160         $itemtypes{''} = "";
161         while (my ($value,$lib) = $sth->fetchrow_array) {
162                 push @itemtype, $value;
163                 $itemtypes{$value}=$lib;
164         }
165         
166         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
167                                 -values   => \@itemtype,
168                                 -labels   => \%itemtypes,
169                                 -size     => 1,
170                                 -multiple => 0 );
171         $sth->finish;
172         
173         my @branches;
174         my @select_branch;
175         my %select_branches;
176         my ($count2,@branches)=branches();
177         push @select_branch, "";
178         $select_branches{''} = "";
179         for (my $i=0;$i<$count2;$i++){
180                 push @select_branch, $branches[$i]->{'branchcode'};#
181                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
182         }
183         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
184                                 -values   => \@select_branch,
185                                 -labels   => \%select_branches,
186                                 -size     => 1,
187                                 -multiple => 0 );
188         $sth->finish;
189     
190         $template->param(classlist => $classlist,
191                                         CGIitemtype => $CGIitemtype,
192                                         CGIbranch => $CGIbranch,
193         );
194 }
195
196 output_html_with_http_headers $query, $cookie, $template->output;