5 #use C4::Interface::CGI::KOHACGI;
8 use C4::Interface::CGI::Output;
11 use POSIX qw(ceil floor);
14 my $dbh = C4::Context->dbh;
15 $query->charset('UTF8');
16 my $op = $query->param('op'); #show the search form or execute the search
18 my $format=$query->param('MARC');
19 my ($template, $borrowernumber, $cookie);
21 # get all the common search variables,
22 my @value=$query->param('value');
23 my @kohafield=$query->param('kohafield');
24 my @and_or=$query->param('and_or');
25 my @relation=$query->param('relation');
26 my $order=$query->param('order');
27 my $reorder=$query->param('reorder');
28 my $number_of_results=$query->param('number_of_results');
29 my $zoom=$query->param('zoom');
30 my $ascend=$query->param('asc');
31 # collect all the fields ...
33 my @forminputs; #this is for the links to navigate among the results
34 my (@searchdesc, %hashdesc); #this is to show the description of the current search
35 my @fields = ('value', 'kohafield', 'and_or', 'relation','order','barcode','biblionumber','itemnumber','asc','from');
37 foreach my $field (@fields) {
38 $search{$field} = $query->param($field);
39 if ($search{$field}) {
40 push @forminputs, { field=>$field ,value=> $search{$field}} unless ($field eq 'reorder');
45 $hashdesc{'query'} = join " , ", @value;
46 push @searchdesc,\%hashdesc;
49 ############################################################################
50 if ($op eq "do_search"){
52 #this fields is just to allow the user come back to the search form with all the values previously entered
53 $search{'search_type'} = $query->param('search_type');
54 push @forminputs, {field => 'search_type', value => $search{'search_type'}};
57 ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => "catalogue/catalogue_searchresults.tmpl",
64 $search{'from'} = 'intranet';
65 $search{'borrowernumber'} = $borrowernumber;
66 $search{'remote_IP'} = $query->remote_addr();
67 $search{'remote_URL'} = $query->url(-query=>1);
68 $search{'searchdesc'} = \@searchdesc;
69 $template->param(FORMINPUTS => \@forminputs);
70 $template->param(reorder => $query->param('reorder'));
73 $number_of_results = 10 unless $number_of_results;
74 my $startfrom=$query->param('startfrom');
75 ($startfrom) || ($startfrom=0);
78 ## using sql search for barcode,biblionumber or itemnumber only useful for libraian interface
79 ($count, @results) =sqlsearch($dbh,\%search);
81 my $sortorder=$order.",".$ascend if $order;
82 ($count,@results) =ZEBRAsearch_kohafields(\@kohafield,\@value, \@relation,$sortorder, \@and_or, 1,$reorder,$startfrom, $number_of_results,"intranet");
84 if ( $count eq "error"){
85 $template->param(error =>1);
88 my $num = scalar(@results) - 1;
90 # if its a barcode search by definition we will only have one result.
91 # And if we have a result
92 # lets jump straight to the detail.pl page
94 print $query->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
96 print $query->redirect("/cgi-bin/koha/catalogue/detail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
99 # sorting out which results to display.
100 # the result number to star to show
101 $template->param(starting => $startfrom+$number_of_results);
102 $template->param(endinging => $startfrom+1);
103 $template->param(startfrom => $startfrom+1);
104 # the result number to end to show
105 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num+1)) : ($template->param(endat => $count));
106 # the total results searched
107 $template->param(numrecords => $count);
109 $template->param(searchdesc => \@searchdesc );
110 $template->param(SEARCH_RESULTS => \@results);
112 #this is to show the images numbers to navigate among the results, if it has to show the number highlighted or not
116 if (defined($query->param('pg'))) {
117 $pg = $query->param('pg');
121 $start = ($pg - 1) * $number_of_results;
122 my $pages = ceil($count / $number_of_results);
123 my $total_pages = ceil($count / $number_of_results);
127 push @$numbers, { number => "<<",
131 push @$numbers, { number => "<",
132 highlight => 0 , forminputs=>\@forminputs,
133 startfrom => ($url-1)*$number_of_results+1,
136 my $current_ten = $pg / 10;
137 if ($current_ten == 0) {
138 $current_ten = 0.1; # In case it´s in ten = 0
140 my $from = $current_ten * 10; # Calculate the initial page
141 my $end_ten = $from + 9;
143 if ($pages > $end_ten) {
148 for (my $i = $from; $i <= $to ; $i++) {
150 if ($count > $number_of_results) {
151 push @$numbers, { number => $i,
152 highlight => 1 , forminputs=>\@forminputs,
153 startfrom => ($i-1)*$number_of_results ,
157 push @$numbers, { number => $i,
158 highlight => 0 , forminputs=>\@forminputs,
159 startfrom => ($i-1)*$number_of_results ,
165 push @$numbers, { number => ">",
166 highlight => 0 , forminputs=>\@forminputs,
167 startfrom => ($url-1)*$number_of_results,
169 push @$numbers, { number => ">>",
170 highlight => 0 , forminputs=>\@forminputs,
171 startfrom => ($total_pages-1)*$number_of_results,
174 # push @$numbers,{forminputs=>@forminputs};
175 $template->param(numbers =>$numbers);
177 #show the virtual shelves
178 #my $results = &GetShelfList($borrowernumber);
179 #$template->param(shelvescount => scalar(@{$results}));
180 #$template->param(shelves => $results);
183 if ($format eq '1') {
184 $template->param(script => "catalogue/MARCdetail.pl");
186 $template->param(script => "catalogue/detail.pl");
189 }else{ ## No search yet
190 ($template, $borrowernumber, $cookie)
191 = get_template_and_user({template_name => "catalogue/catalogue_search.tmpl",
194 authnotrequired => 1,
197 my $kohafield = $query->param('kohafield');
198 my ($fieldcount,@kohafields)=getkohafields();
199 foreach my $row (@kohafields) {
200 if ($kohafield eq $row->{'marctokoha'}) {
204 $template->param(kohafields => \@kohafields);
205 ##show sorting fields
207 $order=$query->param('order');
208 foreach my $sort (@kohafields) {
211 if ($order eq $sort->{'marctokoha'}) {
216 $template->param(sorts => \@sorts);
218 my $search_type = $query->param('search_type');
219 if ((!$search_type) || ($search_type eq 'zoom')) {
220 $template->param(zoom_search => 1);
222 $template->param(sql_search => 1);
227 output_html_with_http_headers $query, $cookie, $template->output();