fixing bug when a search gets more than 10 biblios : now, user can reach biblio 10...
[koha.git] / acqui.simple / isbnsearch.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Catalogue;
25 use C4::Biblio;
26 use C4::Search;
27 use C4::Output;
28 use C4::Interface::CGI::Output;
29 use HTML::Template;
30
31 my $input      = new CGI;
32 my $isbn       = $input->param('isbn');
33 my $title               = $input->param('title');
34 my $offset     = $input->param('offset');
35 my $num        = $input->param('num');
36 my $showoffset = $offset + 1;
37 my $total;
38 my $count;
39 my @results;
40 if (! $isbn && !$title) {
41         print $input->redirect('addbooks.pl');
42 } else {
43         my ($template, $loggedinuser, $cookie)
44         = get_template_and_user({template_name => "acqui.simple/isbnsearch.tmpl",
45                                         query => $input,
46                                         type => "intranet",
47                                         authnotrequired => 0,
48                                         flagsrequired => {catalogue => 1},
49                                         debug => 1,
50                                         });
51         # fill with books in ACTIVE DB (biblio)
52         if (! $offset) {
53                 $offset     = 0;
54                 $showoffset = 1;
55         };
56         if (! $num) { $num = 10 };
57         ($count, @results) = isbnsearch($isbn,$title);
58
59         if ($count < ($offset + $num)) {
60                 $total = $count;
61         } else {
62                 $total = $offset + $num;
63         } # else
64
65         my @loop_data = ();
66         my $toggle;
67         for (my $i = $offset; $i < $total; $i++) {
68                 if ($i % 2) {
69                         $toggle="#ffffcc";
70                 } else {
71                         $toggle="white";
72                 }
73                 my %row_data;  # get a fresh hash for the row data
74                 $row_data{toggle} = $toggle;
75                 $row_data{biblionumber} =$results[$i]->{'biblionumber'};
76                 $row_data{title} = $results[$i]->{'title'};
77                 $row_data{author} = $results[$i]->{'author'};
78                 $row_data{copyrightdate} = $results[$i]->{'copyrightdate'};
79                 push(@loop_data, \%row_data);
80         }
81         $template->param(startfrom => $offset+1);
82         ($offset+$num<=$count) ? ($template->param(endat => $offset+$num)) : ($template->param(endat => $count));
83         $template->param(numrecords => $count);
84         my $nextstartfrom=($offset+$num<$count) ? ($offset+$num) : (-1);
85         my $prevstartfrom=($offset-$num>=0) ? ($offset-$num) : (-1);
86         $template->param(nextstartfrom => $nextstartfrom);
87         my $displaynext=1;
88         my $displayprev=0;
89         ($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
90         ($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
91         $template->param(displaynext => $displaynext);
92         $template->param(displayprev => $displayprev);
93         my @numbers = ();
94         my $term;
95         my $value;
96         if ($isbn) {
97                 $term = "isbn";
98                 $value=$isbn;
99         } else {
100                 $term ="title";
101                 $value=$title;
102         }
103         if ($count>10) {
104                 for (my $i=1; $i<$count/10+1; $i++) {
105                         if ($i<16) {
106                                 my $highlight=0;
107                                 ($offset==($i-1)*10) && ($highlight=1);
108                                 push @numbers, { number => $i, highlight => $highlight , term => $term, value => $value, startfrom => ($i-1)*10};
109                         }
110                 }
111         }
112         # fill with books in breeding farm
113         ($count, @results) = breedingsearch($title,$isbn);
114         my @breeding_loop = ();
115         for (my $i=0; $i <= $#results; $i++) {
116                 my %row_data;
117                 if ($i % 2) {
118                         $toggle="#ffffcc";
119                 } else {
120                         $toggle="white";
121                 }
122                 $row_data{toggle} = $toggle;
123                 $row_data{id} = $results[$i]->{'id'};
124                 $row_data{isbn} = $results[$i]->{'isbn'};
125                 $row_data{file} = $results[$i]->{'file'};
126                 $row_data{title} = $results[$i]->{'title'};
127                 $row_data{author} = $results[$i]->{'author'};
128                 push (@breeding_loop, \%row_data);
129         }
130         $template->param(isbn => $isbn,
131                                                         title => $title,
132                                                         showoffset => $showoffset,
133                                                         total => $total,
134                                                         offset => $offset,
135                                                         loop => \@loop_data,
136                                                         breeding_loop => \@breeding_loop,
137                                                         numbers => \@numbers,
138                                                         term => $term,
139                                                         value => $value,
140                                                         );
141
142         print $input->header(
143             -type => guesstype($template->output),
144             -cookie => $cookie
145         ),$template->output;
146 } # else