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');
31 my @marclist = $query->param('marclist');
32 # collect all the fields ...
34 my @forminputs; #this is for the links to navigate among the results
35 my (@searchdesc, %hashdesc); #this is to show the description of the current search
36 my @fields = ('value', 'kohafield', 'and_or', 'relation','order','barcode','biblionumber','itemnumber','asc','from');
38 ###Collect all the marclist values coming from old Koha MARCdetails
39 ## Although we can not search on all marc fields- if any is matched in Zebra we can use it it
40 my $sth=$dbh->prepare("Select marctokoha from koha_attr where tagfield=? and tagsubfield=? and intrashow=1");
41 foreach my $marc (@marclist) {
43 $sth->execute(substr($marc,0,3),substr($marc,3,1));
44 if ((my $kohafield)=$sth->fetchrow){
45 push @kohafield,$kohafield;
47 push @value,@value[0] if @kohafield>1;
48 push @relation ,"\@attr 5=1";
52 #### Now normal search routine
53 foreach my $field (@fields) {
54 $search{$field} = $query->param($field);
55 if ($search{$field}) {
56 push @forminputs, { field=>$field ,value=> $search{$field}} unless ($field eq 'reorder');
61 $hashdesc{'query'} = join " , ", @value;
62 push @searchdesc,\%hashdesc;
65 ############################################################################
66 if ($op eq "do_search"){
68 #this fields is just to allow the user come back to the search form with all the values previously entered
69 $search{'search_type'} = $query->param('search_type');
70 push @forminputs, {field => 'search_type', value => $search{'search_type'}};
73 ($template, $borrowernumber, $cookie)
74 = get_template_and_user({template_name => "catalogue/catalogue_searchresults.tmpl",
80 $search{'from'} = 'intranet';
81 $search{'borrowernumber'} = $borrowernumber;
82 $search{'remote_IP'} = $query->remote_addr();
83 $search{'remote_URL'} = $query->url(-query=>1);
84 $search{'searchdesc'} = \@searchdesc;
85 $template->param(FORMINPUTS => \@forminputs);
86 $template->param(reorder => $query->param('reorder'));
89 $number_of_results = 10 unless $number_of_results;
90 my $startfrom=$query->param('startfrom');
91 ($startfrom) || ($startfrom=0);
94 ## using sql search for barcode,biblionumber or itemnumber only useful for libraian interface
95 ($count, @results) =sqlsearch($dbh,\%search);
97 my $sortorder=$order.",".$ascend if $order;
98 ($count,@results) =ZEBRAsearch_kohafields(\@kohafield,\@value, \@relation,$sortorder, \@and_or, 1,$reorder,$startfrom, $number_of_results,"intranet");
100 if ( $count eq "error"){
101 $template->param(error =>1);
104 my $num = scalar(@results) - 1;
106 # if its a barcode search by definition we will only have one result.
107 # And if we have a result
108 # lets jump straight to the detail.pl page
109 if ($format eq '1') {
110 print $query->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
112 print $query->redirect("/cgi-bin/koha/catalogue/detail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
115 # sorting out which results to display.
116 # the result number to star to show
117 $template->param(starting => $startfrom+$number_of_results);
118 $template->param(endinging => $startfrom+1);
119 $template->param(startfrom => $startfrom+1);
120 # the result number to end to show
121 ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num+1)) : ($template->param(endat => $count));
122 # the total results searched
123 $template->param(numrecords => $count);
125 $template->param(searchdesc => \@searchdesc );
126 $template->param(SEARCH_RESULTS => \@results);
128 #this is to show the images numbers to navigate among the results, if it has to show the number highlighted or not
132 if (defined($query->param('pg'))) {
133 $pg = $query->param('pg');
137 $start = ($pg - 1) * $number_of_results;
138 my $pages = ceil($count / $number_of_results);
139 my $total_pages = ceil($count / $number_of_results);
143 push @$numbers, { number => "<<",
147 push @$numbers, { number => "<",
148 highlight => 0 , forminputs=>\@forminputs,
149 startfrom => ($url-1)*$number_of_results+1,
152 my $current_ten = $pg / 10;
153 if ($current_ten == 0) {
154 $current_ten = 0.1; # In case it´s in ten = 0
156 my $from = $current_ten * 10; # Calculate the initial page
157 my $end_ten = $from + 9;
159 if ($pages > $end_ten) {
164 for (my $i = $from; $i <= $to ; $i++) {
166 if ($count > $number_of_results) {
167 push @$numbers, { number => $i,
168 highlight => 1 , forminputs=>\@forminputs,
169 startfrom => ($i-1)*$number_of_results ,
173 push @$numbers, { number => $i,
174 highlight => 0 , forminputs=>\@forminputs,
175 startfrom => ($i-1)*$number_of_results ,
181 push @$numbers, { number => ">",
182 highlight => 0 , forminputs=>\@forminputs,
183 startfrom => ($url-1)*$number_of_results,
185 push @$numbers, { number => ">>",
186 highlight => 0 , forminputs=>\@forminputs,
187 startfrom => ($total_pages-1)*$number_of_results,
190 # push @$numbers,{forminputs=>@forminputs};
191 $template->param(numbers =>$numbers);
193 #show the virtual shelves
194 #my $results = &GetShelfList($borrowernumber);
195 #$template->param(shelvescount => scalar(@{$results}));
196 #$template->param(shelves => $results);
199 if ($format eq '1') {
200 $template->param(script => "catalogue/MARCdetail.pl");
202 $template->param(script => "catalogue/detail.pl");
205 }else{ ## No search yet
206 ($template, $borrowernumber, $cookie)
207 = get_template_and_user({template_name => "catalogue/catalogue_search.tmpl",
210 authnotrequired => 1,
213 my $kohafield = $query->param('kohafield');
214 my ($fieldcount,@kohafields)=getkohafields();
215 foreach my $row (@kohafields) {
216 if ($kohafield eq $row->{'marctokoha'}) {
220 $template->param(kohafields => \@kohafields);
221 ##show sorting fields
223 $order=$query->param('order');
224 foreach my $sort (@kohafields) {
227 if ($order eq $sort->{'marctokoha'}) {
232 $template->param(sorts => \@sorts);
234 my $search_type = $query->param('search_type');
235 if ((!$search_type) || ($search_type eq 'zoom')) {
236 $template->param(zoom_search => 1);
238 $template->param(sql_search => 1);
243 output_html_with_http_headers $query, $cookie, $template->output();