Clean up before final commits
[koha.git] / catalogue / catalogue-search.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5 #use C4::Interface::CGI::KOHACGI;
6 use C4::Search;
7 use C4::Auth;
8 use C4::Interface::CGI::Output;
9 use C4::Biblio;
10 use C4::Koha;
11 use POSIX qw(ceil floor);
12
13 my $query = new CGI;
14 my $dbh = C4::Context->dbh;
15 $query->charset('UTF8');
16 my $op = $query->param('op'); #show the search form or execute the search
17
18 my $format=$query->param('MARC');
19 my ($template, $borrowernumber, $cookie);
20
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 ...
32 my %search;
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');
36
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');
41                 }
42 }
43
44
45 $hashdesc{'query'} = join " , ", @value;
46 push @searchdesc,\%hashdesc;
47
48
49 ############################################################################
50 if ($op eq "do_search"){
51  
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'}};
55
56
57         ($template, $borrowernumber, $cookie)
58                 = get_template_and_user({template_name => "catalogue/catalogue_searchresults.tmpl",
59                                          query => $query,
60                                          type => "intranet",
61                                          authnotrequired => 1,
62         });
63
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'));
71
72         # do the searchs ....
73          $number_of_results = 10 unless $number_of_results;
74         my $startfrom=$query->param('startfrom');
75         ($startfrom) || ($startfrom=0);
76 my ($count,@results);
77 if (!$zoom){
78 ## using sql search for barcode,biblionumber or itemnumber only useful for libraian interface
79         ($count, @results) =sqlsearch($dbh,\%search);
80 }else{
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");
83 }
84         if ( $count eq "error"){
85         $template->param(error =>1);
86         goto "show";
87         }
88         my $num = scalar(@results) - 1;
89 if ( $count == 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
93         if ($format eq '1') {
94     print $query->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
95         }else{
96     print $query->redirect("/cgi-bin/koha/catalogue/detail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}");
97         }
98 }
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);
108
109         $template->param(searchdesc => \@searchdesc );
110         $template->param(SEARCH_RESULTS => \@results);
111
112         #this is to show the images numbers to navigate among the results, if it has to show the number highlighted or not
113         my $numbers;
114         @$numbers = ();
115         my $pg = 1;
116         if (defined($query->param('pg'))) {
117                 $pg = $query->param('pg');
118         }
119         my $start = 0;
120         
121         $start = ($pg - 1) * $number_of_results;
122         my $pages = ceil($count / $number_of_results);
123         my $total_pages = ceil($count / $number_of_results);
124
125         if ($pg > 1) {
126                 my $url = $pg - 1;
127                 push @$numbers, { number => "&lt;&lt;", 
128                                               highlight => 0 , 
129                                               startfrom => 0, 
130                                               pg => '1' };
131                 push @$numbers, { number => "&lt;", 
132                                                   highlight => 0 , forminputs=>\@forminputs,
133                                                   startfrom => ($url-1)*$number_of_results+1, 
134                                                   pg => $url };
135         }
136         my $current_ten = $pg / 10;
137         if ($current_ten == 0) {
138                  $current_ten = 0.1;           # In case it´s in ten = 0
139         } 
140         my $from = $current_ten * 10; # Calculate the initial page
141         my $end_ten = $from + 9;
142         my $to;
143         if ($pages > $end_ten) {
144                 $to = $end_ten;
145         } else {
146                 $to = $pages;
147         }
148         for (my $i = $from; $i <= $to ; $i++) {
149                 if ($i == $pg) {   
150                         if ($count > $number_of_results) {
151                                 push @$numbers, { number => $i, 
152                                                                   highlight => 1 , forminputs=>\@forminputs,
153                                                                   startfrom => ($i-1)*$number_of_results , 
154                                                                   pg => $i };
155                         }
156                 } else {
157                         push @$numbers, { number => $i, 
158                                                           highlight => 0 , forminputs=>\@forminputs,
159                                                           startfrom => ($i-1)*$number_of_results , 
160                                                           pg => $i };
161                 }
162         }                                                       
163         if ($pg < $pages) {
164                 my $url = $pg + 1;
165                 push @$numbers, { number => "&gt;", 
166                                                   highlight => 0 , forminputs=>\@forminputs,
167                                                   startfrom => ($url-1)*$number_of_results, 
168                                                   pg => $url };
169                 push @$numbers, { number => "&gt;&gt;", 
170                                                   highlight => 0 , forminputs=>\@forminputs,
171                                                   startfrom => ($total_pages-1)*$number_of_results, 
172                                                   pg => $total_pages};
173         }
174 #       push @$numbers,{forminputs=>@forminputs};
175         $template->param(numbers =>$numbers);
176
177         #show the virtual shelves
178         #my $results = &GetShelfList($borrowernumber);
179         #$template->param(shelvescount => scalar(@{$results}));
180         #$template->param(shelves => $results);
181
182 ########
183 if ($format eq '1') {
184         $template->param(script => "catalogue/MARCdetail.pl");
185 }else{
186         $template->param(script => "catalogue/detail.pl");
187 }
188
189 }else{ ## No search yet
190 ($template, $borrowernumber, $cookie)
191                 = get_template_and_user({template_name => "catalogue/catalogue_search.tmpl",
192                                         query => $query,
193                                         type => "intranet",
194                                         authnotrequired => 1,
195                                 });
196 #show kohafields
197         my $kohafield = $query->param('kohafield');
198         my ($fieldcount,@kohafields)=getkohafields();
199         foreach my $row (@kohafields) {
200                 if ($kohafield eq $row->{'marctokoha'}) {
201                         $row->{'sel'} = 1;
202                 }
203         }
204         $template->param(kohafields => \@kohafields);
205 ##show sorting fields
206 my @sorts;
207  $order=$query->param('order');
208         foreach my $sort (@kohafields) {
209             if ($sort->{sorts}){
210                 push @sorts,$sort;
211                 if ($order eq $sort->{'marctokoha'}) {
212                         $sort->{'sel'} = 1;
213                 }
214            }
215         }
216         $template->param(sorts => \@sorts);
217
218 my $search_type = $query->param('search_type');
219         if ((!$search_type) || ($search_type eq 'zoom'))  {
220                 $template->param(zoom_search => 1);
221         } else{
222                 $template->param(sql_search => 1);
223         } 
224 }
225
226 show:
227 output_html_with_http_headers $query, $cookie, $template->output();
228