453a062766c913e6e001cf5743d7b03a454f538f
[koha-equinox.git] / cataloguing / isbnsearch.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22
23 use C4::Auth;
24 use C4::Biblio;
25 use C4::Search;
26 use C4::Output;
27 use C4::Interface::CGI::Output;
28 use C4::Breeding;
29 use C4::Koha;
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 my %search;
41 my $toggle;
42 my $marc_p = C4::Context->boolean_preference("marc");
43 my $SQLorZEBRA=C4::Context->preference("SQLorZEBRA");
44 if ( !$isbn && !$title ) {
45     print $input->redirect('addbooks.pl');
46 }
47 else {
48     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49         {
50             template_name   => "cataloguing/isbnsearch.tmpl",
51             query           => $input,
52             type            => "intranet",
53             authnotrequired => 0,
54             flagsrequired   => { editcatalogue => 1 },
55             debug           => 1,
56         }
57     );
58
59     # fill with books in ACTIVE DB (biblio)
60     if ( !$offset ) {
61         $offset     = 0;
62         $showoffset = 1;
63     }
64     if ( !$num ) { $num = 10 }
65 my @kohafield;
66 my @value;
67 my @relation;
68 my @and_or;
69 my $order="title";
70 if ($isbn){
71 $search{'isbn'}=$isbn;
72 push @kohafield, "isbn";
73 push @value,$isbn;
74 }else{
75 $search{'title'}=$title;
76 push @kohafield, "title";
77 push @value,$title;
78 push @relation, "\@attr 5=1 \@attr 6=3 \@attr 4=1 \@attr 3=1 ";
79   }
80 $search{avoidquerylog}=1;
81 if ($SQLorZEBRA eq "sql"){
82 ($count, @results) =cataloguing_search(\%search,$num,$offset);
83 }else{
84 ($count,@results) =ZEBRAsearch_kohafields(\@kohafield,\@value, \@relation,$order, \@and_or, 1,"",$offset, $num,"intranet");
85
86 }
87 my $grandtotal=$count;
88     if ( $count < ( $offset + $num ) ) {
89         $total = $count;
90     }
91     else {
92         $total = $offset + $num;
93     }    # else
94
95     my @loop_data;
96  
97  @loop_data=@results if $count >0;;
98     $template->param( startfrom => $offset + 1 );
99     ( $offset + $num <= $count )
100       ? ( $template->param( endat => $offset + $num ) )
101       : ( $template->param( endat => $count ) );
102     $template->param( numrecords => $count );
103     my $nextstartfrom = ( $offset + $num < $count ) ? ( $offset + $num ) : (-1);
104     my $prevstartfrom = ( $offset - $num >= 0 ) ? ( $offset - $num ) : (-1);
105     $template->param( nextstartfrom => $nextstartfrom );
106     my $displaynext = 1;
107     my $displayprev = 0;
108     ( $nextstartfrom == -1 ) ? ( $displaynext = 0 ) : ( $displaynext = 1 );
109     ( $prevstartfrom == -1 ) ? ( $displayprev = 0 ) : ( $displayprev = 1 );
110     $template->param( displaynext => $displaynext );
111     $template->param( displayprev => $displayprev );
112     my @numbers = ();
113     my $term;
114     my $value;
115
116     if ($isbn) {
117         $term  = "isbn";
118         $value = $isbn;
119     }
120     else {
121         $term  = "title";
122         $value = $title;
123     }
124     if ( $count > 10 ) {
125         for ( my $i = 1 ; $i < $count / 10 + 1 ; $i++ ) {
126             if ( $i < 16 ) {
127                 my $highlight = 0;
128                 ( $offset == ( $i - 1 ) * 10 ) && ( $highlight = 1 );
129                 push @numbers,
130                   {
131                     number    => $i,
132                     highlight => $highlight,
133                     term      => $term,
134                     value     => $value,
135                     startfrom => ( $i - 1 ) * 10
136                 };
137             }
138         }
139     }
140
141     # fill with books in breeding farm
142     ( $count, @results ) = BreedingSearch( $title, $isbn );
143     my @breeding_loop = ();
144     for ( my $i = 0 ; $i <= $#results ; $i++ ) {
145         my %row_data;
146         if ( $i % 2 ) {
147             $toggle = "#ffffcc";
148         }
149         else {
150             $toggle = "white";
151         }
152         $row_data{toggle} = $toggle;
153         $row_data{id}     = $results[$i]->{'id'};
154         $row_data{isbn}   = $results[$i]->{'isbn'};
155         $row_data{file}   = $results[$i]->{'file'};
156         $row_data{title}  = $results[$i]->{'title'};
157         $row_data{author} = $results[$i]->{'author'};
158         $row_data{classification} = $results[$i]->{'classification'};
159         $row_data{subclass} = $results[$i]->{'subclass'};
160           push ( @breeding_loop, \%row_data );
161     }
162 # get framework list
163         my $frameworks = getframeworks;
164         my @frameworkcodeloop;
165         foreach my $thisframeworkcode (keys %$frameworks) {
166                 my %row =(value => $thisframeworkcode,
167                                         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
168                                 );
169                 push @frameworkcodeloop, \%row;
170         }
171
172
173     $template->param(
174         isbn          => $isbn,
175         title         => $title,
176         showoffset    => $showoffset,
177         total         => $total,
178         grandtotal         => $grandtotal,
179         offset        => $offset,
180         results_loop          => \@loop_data,
181         breeding_loop => \@breeding_loop,
182         numbers       => \@numbers,
183         term          => $term,
184         value         => $value,
185         frameworkcodeloop => \@frameworkcodeloop
186     );
187
188   output_html_with_http_headers $input, $cookie, $template->output;
189 }    # else