New XML API
[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
31 my @marclist = $query->param('marclist');
32 # collect all the fields ...
33 my %search;
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');
37
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) {
42                 if ($marc) {
43                 $sth->execute(substr($marc,0,3),substr($marc,3,1));
44                         if ((my $kohafield)=$sth->fetchrow){
45                         push @kohafield,$kohafield;
46                         push @and_or,"\@or";
47                         push @value,@value[0] if @kohafield>1;
48                         push @relation ,"\@attr 5=1";
49                         }
50                 }
51 }
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');
57                 }
58 }
59
60
61 $hashdesc{'query'} = join " , ", @value;
62 push @searchdesc,\%hashdesc;
63
64
65 ############################################################################
66 if ($op eq "do_search"){
67  
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'}};
71
72
73         ($template, $borrowernumber, $cookie)
74                 = get_template_and_user({template_name => "catalogue/catalogue_searchresults.tmpl",
75                                          query => $query,
76                                          type => "intranet",
77                                          authnotrequired => 1,
78         });
79
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'));
87
88         # do the searchs ....
89          $number_of_results = 10 unless $number_of_results;
90         my $startfrom=$query->param('startfrom');
91         ($startfrom) || ($startfrom=0);
92 my ($count,@results);
93 if (!$zoom){
94 ## using sql search for barcode,biblionumber or itemnumber only useful for libraian interface
95         ($count, @results) =sqlsearch($dbh,\%search);
96 }else{
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");
99 }
100         if ( $count eq "error"){
101         $template->param(error =>1);
102         goto "show";
103         }
104         my $num = scalar(@results) - 1;
105 if ( $count == 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'}");
111         }else{
112     print $query->redirect("/cgi-bin/koha/catalogue/detail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
113         }
114 }
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);
124
125         $template->param(searchdesc => \@searchdesc );
126         $template->param(SEARCH_RESULTS => \@results);
127
128         #this is to show the images numbers to navigate among the results, if it has to show the number highlighted or not
129         my $numbers;
130         @$numbers = ();
131         my $pg = 1;
132         if (defined($query->param('pg'))) {
133                 $pg = $query->param('pg');
134         }
135         my $start = 0;
136         
137         $start = ($pg - 1) * $number_of_results;
138         my $pages = ceil($count / $number_of_results);
139         my $total_pages = ceil($count / $number_of_results);
140
141         if ($pg > 1) {
142                 my $url = $pg - 1;
143                 push @$numbers, { number => "&lt;&lt;", 
144                                               highlight => 0 , 
145                                               startfrom => 0, 
146                                               pg => '1' };
147                 push @$numbers, { number => "&lt;", 
148                                                   highlight => 0 , forminputs=>\@forminputs,
149                                                   startfrom => ($url-1)*$number_of_results+1, 
150                                                   pg => $url };
151         }
152         my $current_ten = $pg / 10;
153         if ($current_ten == 0) {
154                  $current_ten = 0.1;           # In case it´s in ten = 0
155         } 
156         my $from = $current_ten * 10; # Calculate the initial page
157         my $end_ten = $from + 9;
158         my $to;
159         if ($pages > $end_ten) {
160                 $to = $end_ten;
161         } else {
162                 $to = $pages;
163         }
164         for (my $i = $from; $i <= $to ; $i++) {
165                 if ($i == $pg) {   
166                         if ($count > $number_of_results) {
167                                 push @$numbers, { number => $i, 
168                                                                   highlight => 1 , forminputs=>\@forminputs,
169                                                                   startfrom => ($i-1)*$number_of_results , 
170                                                                   pg => $i };
171                         }
172                 } else {
173                         push @$numbers, { number => $i, 
174                                                           highlight => 0 , forminputs=>\@forminputs,
175                                                           startfrom => ($i-1)*$number_of_results , 
176                                                           pg => $i };
177                 }
178         }                                                       
179         if ($pg < $pages) {
180                 my $url = $pg + 1;
181                 push @$numbers, { number => "&gt;", 
182                                                   highlight => 0 , forminputs=>\@forminputs,
183                                                   startfrom => ($url-1)*$number_of_results, 
184                                                   pg => $url };
185                 push @$numbers, { number => "&gt;&gt;", 
186                                                   highlight => 0 , forminputs=>\@forminputs,
187                                                   startfrom => ($total_pages-1)*$number_of_results, 
188                                                   pg => $total_pages};
189         }
190 #       push @$numbers,{forminputs=>@forminputs};
191         $template->param(numbers =>$numbers);
192
193         #show the virtual shelves
194         #my $results = &GetShelfList($borrowernumber);
195         #$template->param(shelvescount => scalar(@{$results}));
196         #$template->param(shelves => $results);
197
198 ########
199 if ($format eq '1') {
200         $template->param(script => "catalogue/MARCdetail.pl");
201 }else{
202         $template->param(script => "catalogue/detail.pl");
203 }
204
205 }else{ ## No search yet
206 ($template, $borrowernumber, $cookie)
207                 = get_template_and_user({template_name => "catalogue/catalogue_search.tmpl",
208                                         query => $query,
209                                         type => "intranet",
210                                         authnotrequired => 1,
211                                 });
212 #show kohafields
213         my $kohafield = $query->param('kohafield');
214         my ($fieldcount,@kohafields)=getkohafields();
215         foreach my $row (@kohafields) {
216                 if ($kohafield eq $row->{'marctokoha'}) {
217                         $row->{'sel'} = 1;
218                 }
219         }
220         $template->param(kohafields => \@kohafields);
221 ##show sorting fields
222 my @sorts;
223  $order=$query->param('order');
224         foreach my $sort (@kohafields) {
225             if ($sort->{sorts}){
226                 push @sorts,$sort;
227                 if ($order eq $sort->{'marctokoha'}) {
228                         $sort->{'sel'} = 1;
229                 }
230            }
231         }
232         $template->param(sorts => \@sorts);
233
234 my $search_type = $query->param('search_type');
235         if ((!$search_type) || ($search_type eq 'zoom'))  {
236                 $template->param(zoom_search => 1);
237         } else{
238                 $template->param(sql_search => 1);
239         } 
240 }
241
242 show:
243 output_html_with_http_headers $query, $cookie, $template->output();
244