7 use C4::Interface::CGI::Output;
10 use POSIX qw(ceil floor);
13 my $dbh = C4::Context->dbh;
15 my $op = $query->param('op'); #show the search form or execute the search
17 my $format=$query->param('MARC');
18 my ($template, $borrowernumber, $cookie);
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
32 my @marclist = $query->param('marclist');
33 # collect all the fields ...
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');
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) {
44 $sth->execute(substr($marc,0,3),substr($marc,3,1));
45 if ((my $kohafield)=$sth->fetchrow){
46 push @kohafield,$kohafield;
48 push @value,@value[0] if @kohafield>1;
49 push @relation ,"\@attr 5=1";
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;
63 $hashdesc{'query'} = join " , ", @value;
64 push @searchdesc,\%hashdesc;
67 ############################################################################
68 if ($op eq "do_search"){
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'}};
75 ($template, $borrowernumber, $cookie)
76 = get_template_and_user({template_name => "catalogue/catalogue_searchresults.tmpl",
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);
91 $number_of_results = 10 unless $number_of_results;
92 my $startfrom=$query->param('startfrom');
93 ($startfrom) || ($startfrom=0);
94 my ($count,@results,$facets);
96 ## using sql search for barcode,biblionumber or itemnumber only useful for libraian interface
97 ($count, @results) =sqlsearch($dbh,\%search);
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);
102 if ( $count eq "error"){
103 $template->param(error =>1);
106 my $num = scalar(@results) - 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'}");
114 print $query->redirect("/cgi-bin/koha/catalogue/detail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
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,
131 #this is to show the images numbers to navigate among the results, if it has to show the number highlighted or not
135 if (defined($query->param('pg'))) {
136 $pg = $query->param('pg');
140 $start = ($pg - 1) * $number_of_results;
141 my $pages = ceil($count / $number_of_results);
142 my $total_pages = ceil($count / $number_of_results);
146 push @$numbers, { number => "<<",
150 push @$numbers, { number => "<",
151 highlight => 0 , forminputs=>\@forminputs,
152 startfrom => ($url-1)*$number_of_results+1,
155 my $current_ten = $pg / 10;
156 if ($current_ten == 0) {
157 $current_ten = 0.1; # In case it´s in ten = 0
159 my $from = $current_ten * 10; # Calculate the initial page
160 my $end_ten = $from + 9;
162 if ($pages > $end_ten) {
167 for (my $i = $from; $i <= $to ; $i++) {
169 if ($count > $number_of_results) {
170 push @$numbers, { number => $i,
171 highlight => 1 , forminputs=>\@forminputs,
172 startfrom => ($i-1)*$number_of_results ,
176 push @$numbers, { number => $i,
177 highlight => 0 , forminputs=>\@forminputs,
178 startfrom => ($i-1)*$number_of_results ,
184 push @$numbers, { number => ">",
185 highlight => 0 , forminputs=>\@forminputs,
186 startfrom => ($url-1)*$number_of_results,
188 push @$numbers, { number => ">>",
189 highlight => 0 , forminputs=>\@forminputs,
190 start => ($total_pages-1)*$number_of_results,
193 # push @$numbers,{forminputs=>@forminputs};
194 $template->param(numbers =>$numbers,
196 #show the virtual shelves
197 #my $results = &GetShelfList($borrowernumber);
198 #$template->param(shelvescount => scalar(@{$results}));
199 #$template->param(shelves => $results);
202 if ($format eq '1') {
203 $template->param(script => "catalogue/MARCdetail.pl");
205 $template->param(script => "catalogue/detail.pl");
208 }else{ ## No search yet
209 ($template, $borrowernumber, $cookie)
210 = get_template_and_user({template_name => "catalogue/catalogue_search.tmpl",
213 authnotrequired => 1,
216 my $kohafield = $query->param('kohafield');
217 my ($fieldcount,@kohafields)=getkohafields();
218 foreach my $row (@kohafields) {
219 if ($kohafield eq $row->{'kohafield'}) {
223 $template->param(kohafields => \@kohafields);
224 ##show sorting fields
226 $order=$query->param('order');
227 foreach my $sort (@kohafields) {
230 if ($order eq $sort->{'attr'}) {
235 $template->param(sorts => \@sorts);
237 my @branches = GetallBranches();
238 $template->param(branchloop => \@branches,);
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'},
247 push @item_type_loop, \%row;
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);
255 $template->param(sql_search => 1);
260 output_html_with_http_headers $query, $cookie, $template->output();