Incorporates the facets from dev_week. This one is table read so its both UNIMARC...
[koha.git] / catalogue / catalogue-search.pl
1 #!/usr/bin/perl
2 use strict;
3
4 use CGI;
5 use C4::Search;
6 use C4::Auth;
7 use C4::Interface::CGI::Output;
8 use C4::Biblio;
9 use C4::Koha;
10 use POSIX qw(ceil floor);
11
12 my $query = new CGI;
13 my $dbh = C4::Context->dbh;
14
15 my $op = $query->param('op'); #show the search form or execute the search
16
17 my $format=$query->param('MARC');
18 my ($template, $borrowernumber, $cookie);
19
20 # get all the common search variables, 
21 my @value=$query->param('value');
22 my @kohafield=$query->param('kohafield');
23 my @and_or=$query->param('and_or');
24 my @relation=$query->param('relation');
25 my $order=$query->param('order');
26 my $reorder=$query->param('reorder');
27 my $number_of_results=$query->param('number_of_results');
28 my $zoom=$query->param('zoom');
29 my $ascend=$query->param('asc');
30 my $searchtype=$query->param('searchtype'); ## this is actual query type
31
32 my @marclist = $query->param('marclist');
33 # collect all the fields ...
34 my %search;
35 my @forminputs;         #this is for the links to navigate among the results
36 my (@searchdesc, %hashdesc,$facetsdesc);        #this is to show the description of the current search
37 my @fields = ('value', 'kohafield', 'and_or', 'relation','order','barcode','biblionumber','itemnumber','asc','from','searchtype');
38
39 ###Collect all the marclist values coming from old Koha MARCdetails
40 ## Although we can not search on all marc fields- if any is matched in Zebra we can use it 
41 my $sth=$dbh->prepare("Select kohafield from koha_attr where tagfield=? and tagsubfield=? and intrashow=1");
42 foreach my $marc (@marclist) {
43                 if ($marc) {
44                 $sth->execute(substr($marc,0,3),substr($marc,3,1));
45                         if ((my $kohafield)=$sth->fetchrow){
46                         push @kohafield,$kohafield;
47                         push @and_or,"\@or";
48                         push @value,@value[0] if @kohafield>1;
49                         push @relation ,"\@attr 5=1";
50                         }
51                 }
52 }
53 #### Now   normal search routine
54 foreach my $field (@fields) {
55         my @fieldvalue = $query->param($field);
56         foreach my $fvalue (@fieldvalue) {
57                 push @forminputs, { field=>$field ,value=> $fvalue} unless ($field eq 'reorder');
58                 $facetsdesc.="&".$field."=".$fvalue;
59           }
60 }
61
62
63 $hashdesc{'query'} = join " , ", @value;
64 push @searchdesc,\%hashdesc;
65
66
67 ############################################################################
68 if ($op eq "do_search"){
69  
70 #this fields is just to allow the user come back to the search form with all the values  previously entered
71 $search{'search_type'} = $query->param('search_type');# this is the panel type
72 push @forminputs, {field => 'search_type', value => $search{'search_type'}};
73
74
75         ($template, $borrowernumber, $cookie)
76                 = get_template_and_user({template_name => "catalogue/catalogue_searchresults.tmpl",
77                                          query => $query,
78                                          type => "intranet",
79                                          authnotrequired => 1,
80         });
81
82         $search{'from'} = 'intranet';
83         $search{'borrowernumber'} = $borrowernumber;
84         $search{'remote_IP'} = $query->remote_addr();
85         $search{'remote_URL'} = $query->url(-query=>1);
86         $search{'searchdesc'} = \@searchdesc;
87         $template->param(FORMINPUTS => \@forminputs);
88         $template->param(reorder => $query->param('reorder'));
89         $template->param(facetsdesc=>$facetsdesc);
90         # do the searchs ....
91          $number_of_results = 10 unless $number_of_results;
92         my $startfrom=$query->param('startfrom');
93         ($startfrom) || ($startfrom=0);
94 my ($count,@results,$facets);
95 if (!$zoom){
96 ## using sql search for barcode,biblionumber or itemnumber only useful for libraian interface
97         ($count, @results) =sqlsearch($dbh,\%search);
98 }else{
99 my $sortorder=$order.",".$ascend if $order;
100  ($count,$facets,@results) =ZEBRAsearch_kohafields(\@kohafield,\@value, \@relation,$sortorder, \@and_or, 1,$reorder,$startfrom, $number_of_results,"intranet",$searchtype);
101 }
102         if ( $count eq "error"){
103         $template->param(error =>1);
104         goto "show";
105         }
106         my $num = scalar(@results) - 1;
107 if ( $count == 1){
108     # if its a barcode search by definition we will only have one result.
109     # And if we have a result
110     # lets jump straight to the detail.pl page
111         if ($format eq '1') {
112     print $query->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
113         }else{
114     print $query->redirect("/cgi-bin/koha/catalogue/detail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
115         }
116 }
117         # sorting out which results to display.
118         # the result number to star to show
119         $template->param(startfrom => $startfrom);
120         $template->param(beginning => $startfrom+1);
121         # the result number to end to show
122         ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num+1)) : ($template->param(endat => $count));
123         # the total results searched
124         $template->param(numrecords => $count);
125         $template->param(FORMINPUTS => \@forminputs );
126         $template->param(searchdesc => \@searchdesc );
127         $template->param(SEARCH_RESULTS => \@results,
128                         facets_loop => $facets,
129                         );
130
131         #this is to show the images numbers to navigate among the results, if it has to show the number highlighted or not
132         my $numbers;
133         @$numbers = ();
134         my $pg = 1;
135         if (defined($query->param('pg'))) {
136                 $pg = $query->param('pg');
137         }
138         my $start = 0;
139         
140         $start = ($pg - 1) * $number_of_results;
141         my $pages = ceil($count / $number_of_results);
142         my $total_pages = ceil($count / $number_of_results);
143
144         if ($pg > 1) {
145                 my $url = $pg - 1;
146                 push @$numbers, { number => "&lt;&lt;", 
147                                               highlight => 0 , 
148                                               startfrom => 0, 
149                                               pg => '1' };
150                 push @$numbers, { number => "&lt;", 
151                                                   highlight => 0 , forminputs=>\@forminputs,
152                                                   startfrom => ($url-1)*$number_of_results+1, 
153                                                   pg => $url };
154         }
155         my $current_ten = $pg / 10;
156         if ($current_ten == 0) {
157                  $current_ten = 0.1;           # In case it´s in ten = 0
158         } 
159         my $from = $current_ten * 10; # Calculate the initial page
160         my $end_ten = $from + 9;
161         my $to;
162         if ($pages > $end_ten) {
163                 $to = $end_ten;
164         } else {
165                 $to = $pages;
166         }
167         for (my $i = $from; $i <= $to ; $i++) {
168                 if ($i == $pg) {   
169                         if ($count > $number_of_results) {
170                                 push @$numbers, { number => $i, 
171                                                                   highlight => 1 , forminputs=>\@forminputs,
172                                                                   startfrom => ($i-1)*$number_of_results , 
173                                                                   pg => $i };
174                         }
175                 } else {
176                         push @$numbers, { number => $i, 
177                                                           highlight => 0 , forminputs=>\@forminputs,
178                                                           startfrom => ($i-1)*$number_of_results , 
179                                                           pg => $i };
180                 }
181         }                                                       
182         if ($pg < $pages) {
183                 my $url = $pg + 1;
184                 push @$numbers, { number => "&gt;", 
185                                                   highlight => 0 , forminputs=>\@forminputs,
186                                                   startfrom => ($url-1)*$number_of_results, 
187                                                   pg => $url };
188                 push @$numbers, { number => "&gt;&gt;", 
189                                                   highlight => 0 , forminputs=>\@forminputs,
190                                                   start => ($total_pages-1)*$number_of_results, 
191                                                   pg => $total_pages};
192         }
193 #       push @$numbers,{forminputs=>@forminputs};
194         $template->param(numbers =>$numbers,
195                         );
196         #show the virtual shelves
197         #my $results = &GetShelfList($borrowernumber);
198         #$template->param(shelvescount => scalar(@{$results}));
199         #$template->param(shelves => $results);
200
201 ########
202 if ($format eq '1') {
203         $template->param(script => "catalogue/MARCdetail.pl");
204 }else{
205         $template->param(script => "catalogue/detail.pl");
206 }
207
208 }else{ ## No search yet
209 ($template, $borrowernumber, $cookie)
210                 = get_template_and_user({template_name => "catalogue/catalogue_search.tmpl",
211                                         query => $query,
212                                         type => "intranet",
213                                         authnotrequired => 1,
214                                 });
215 #show kohafields
216         my $kohafield = $query->param('kohafield');
217         my ($fieldcount,@kohafields)=getkohafields();
218         foreach my $row (@kohafields) {
219                 if ($kohafield eq $row->{'kohafield'}) {
220                         $row->{'sel'} = 1;
221                 }
222         }
223         $template->param(kohafields => \@kohafields);
224 ##show sorting fields
225 my @sorts;
226  $order=$query->param('order');
227         foreach my $sort (@kohafields) {
228             if ($sort->{sorts}){
229                 push @sorts,$sort;
230                 if ($order eq $sort->{'attr'}) {
231                         $sort->{'sel'} = 1;
232                 }
233            }
234         }
235         $template->param(sorts => \@sorts);
236 # load the branches
237 my @branches = GetallBranches();
238 $template->param(branchloop => \@branches,);
239
240 # load the itemtypes 
241 my $itemtypes=GetItemTypes();
242 my (@item_type_loop);
243 foreach my $thisitemtype (sort keys %$itemtypes) {
244     my %row =(value => $thisitemtype,
245                  description => $itemtypes->{$thisitemtype}->{'description'},
246             );
247     push @item_type_loop, \%row;
248 }
249
250 $template->param(itemtypeloop=>\@item_type_loop,);
251 my $search_type = $query->param('search_type');
252         if ((!$search_type) || ($search_type eq 'zoom'))  {
253                 $template->param(zoom_search => 1);
254         } else{
255                 $template->param(sql_search => 1);
256         } 
257 }
258
259 show:
260 output_html_with_http_headers $query, $cookie, $template->output();
261