Add the faceted search facility
[koha.git] / serials / subscription-bib-search.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22
23 use CGI;
24 use C4::Koha;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Interface::CGI::Output;
30 use C4::Biblio;
31 use C4::Acquisition;
32 use C4::Koha; # XXX subfield_is_koha_internal_p
33
34
35 # Creates a scrolling list with the associated default value.
36 # Using more than one scrolling list in a CGI assigns the same default value to all the
37 # scrolling lists on the page !?!? That's why this function was written.
38
39 my $query=new CGI;
40 my $type=$query->param('type');
41 my $op = $query->param('op');
42 my $dbh = C4::Context->dbh;
43
44 my $startfrom=$query->param('startfrom');
45 $startfrom=0 if(!defined $startfrom);
46 my ($template, $loggedinuser, $cookie);
47 my $resultsperpage;
48
49 if ($op eq "do_search") {
50         my @kohafield = $query->param('kohafield');
51         my @and_or = $query->param('and_or');
52         my @relation = $query->param('relation');
53         my @value = $query->param('value');
54         my $order=$query->param('order');
55         $resultsperpage= $query->param('resultsperpage');
56         $resultsperpage = 9 if(!defined $resultsperpage);
57         # builds tag and subfield arrays
58         
59         my ($total,$facets,@results) = ZEBRAsearch_kohafields(\@kohafield,\@value,\@relation,$order,\@and_or,1,"",$startfrom,$resultsperpage,"intranet");
60                                                                                 
61         ($template, $loggedinuser, $cookie)
62                 = get_template_and_user({template_name => "serials/result.tmpl",
63                                 query => $query,
64                                 type => "intranet",
65                                 authnotrequired => 0,
66                                 flagsrequired => {borrowers => 1},
67                                 flagsrequired => {catalogue => 1},
68                                 debug => 1,
69                                 });
70
71         # multi page display gestion
72         my $displaynext=0;
73         my $displayprev=$startfrom;
74         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
75                 $displaynext = 1;
76         }
77
78         my @field_data = ();
79
80
81         for(my $i = 0 ; $i <= $#value ; $i++)
82         {
83                 push @field_data, { term => "kohafield", val=>$kohafield[$i] };
84                 push @field_data, { term => "and_or", val=>$and_or[$i] };
85                 push @field_data, { term => "relation", val=>$relation[$i] };
86                 push @field_data, { term => "value", val=>$value[$i] };
87         }
88
89         my @numbers = ();
90
91         if ($total>$resultsperpage)
92         {
93                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
94                 {
95                         if ($i<16)
96                         {
97                         my $highlight=0;
98                         ($startfrom==($i-1)) && ($highlight=1);
99                         push @numbers, { number => $i,
100                                         highlight => $highlight ,
101                                         searchdata=> \@field_data,
102                                         startfrom => ($i-1)};
103                         }
104         }
105         }
106
107         my $from = $startfrom*$resultsperpage+1;
108         my $to;
109
110         if($total < (($startfrom+1)*$resultsperpage))
111         {
112                 $to = $total;
113         } else {
114                 $to = (($startfrom+1)*$resultsperpage);
115         }
116         $template->param(result => \@results,
117                                                         startfrom=> $startfrom,
118                                                         displaynext=> $displaynext,
119                                                         displayprev=> $displayprev,
120                                                         resultsperpage => $resultsperpage,
121                                                         startfromnext => $startfrom+1,
122                                                         startfromprev => $startfrom-1,
123                                                         searchdata=>\@field_data,
124                                                         total=>$total,
125                                                         from=>$from,
126                                                         to=>$to,
127                                                         numbers=>\@numbers,
128                                                         );
129 } else {
130         ($template, $loggedinuser, $cookie)
131                 = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
132                                 query => $query,
133                                 type => "intranet",
134                                 authnotrequired => 0,
135                                 flagsrequired => {catalogue => 1},
136                                 debug => 1,
137                                 });
138         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
139         $sth->execute;
140         my  @itemtype;
141         my %itemtypes;
142         push @itemtype, "";
143         $itemtypes{''} = "";
144         while (my ($value,$lib) = $sth->fetchrow_array) {
145                 push @itemtype, $value;
146                 $itemtypes{$value}=$lib;
147         }
148
149         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
150                                 -values   => \@itemtype,
151                                 -labels   => \%itemtypes,
152                                 -size     => 1,
153                                 -tabindex=>'',
154                                 -multiple => 0 );
155         $sth->finish;
156
157         $template->param(
158                         CGIitemtype => $CGIitemtype,
159                         );
160 }
161
162
163 # Print the page
164 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
165                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
166                 IntranetNav => C4::Context->preference("IntranetNav"),
167                 );
168 output_html_with_http_headers $query, $cookie, $template->output;
169
170 # Local Variables:
171 # tab-width: 4
172 # End: