Finalized XML version for intranet
[koha.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 $facets;
41 my %search;
42 my $toggle;
43 my $marc_p = C4::Context->boolean_preference("marc");
44 my $SQLorZEBRA=C4::Context->preference("SQLorZEBRA");
45 if ( !$isbn && !$title ) {
46     print $input->redirect('addbooks.pl');
47 }
48 else {
49     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50         {
51             template_name   => "cataloguing/isbnsearch.tmpl",
52             query           => $input,
53             type            => "intranet",
54             authnotrequired => 0,
55             flagsrequired   => { editcatalogue => 1 },
56             debug           => 1,
57         }
58     );
59
60     # fill with books in ACTIVE DB (biblio)
61     if ( !$offset ) {
62         $offset     = 0;
63         $showoffset = 1;
64     }
65     if ( !$num ) { $num = 10 }
66 my @kohafield;
67 my @value;
68 my @relation;
69 my @and_or;
70 my $order="title,1";
71 if ($isbn){
72 $search{'isbn'}=$isbn;
73 push @kohafield, "isbn";
74 push @value,$isbn;
75 }else{
76 $search{'title'}=$title;
77 push @kohafield, "title";
78 push @value,$title;
79 push @relation, "\@attr 5=1 \@attr 6=3 \@attr 4=1 \@attr 3=1 ";
80   }
81 $search{avoidquerylog}=1;
82 if ($SQLorZEBRA eq "sql"){
83 ($count, @results) =cataloguing_search(\%search,$num,$offset);
84 }else{
85 ($count,$facets,@results) =ZEBRAsearch_kohafields(\@kohafield,\@value, \@relation,$order, \@and_or, 1,"",$offset, $num,"intranet");
86
87 }
88 my $grandtotal=$count;
89     if ( $count < ( $offset + $num ) ) {
90         $total = $count;
91     }
92     else {
93         $total = $offset + $num;
94     }    # else
95
96     my @loop_data;
97  
98  @loop_data=@results if $count >0;;
99     $template->param( startfrom => $offset + 1 );
100     ( $offset + $num <= $count )
101       ? ( $template->param( endat => $offset + $num ) )
102       : ( $template->param( endat => $count ) );
103     $template->param( numrecords => $count );
104     my $nextstartfrom = ( $offset + $num < $count ) ? ( $offset + $num ) : (-1);
105     my $prevstartfrom = ( $offset - $num >= 0 ) ? ( $offset - $num ) : (-1);
106     $template->param( nextstartfrom => $nextstartfrom );
107     my $displaynext = 1;
108     my $displayprev = 0;
109     ( $nextstartfrom == -1 ) ? ( $displaynext = 0 ) : ( $displaynext = 1 );
110     ( $prevstartfrom == -1 ) ? ( $displayprev = 0 ) : ( $displayprev = 1 );
111     $template->param( displaynext => $displaynext );
112     $template->param( displayprev => $displayprev );
113     my @numbers = ();
114     my $term;
115     my $value;
116
117     if ($isbn) {
118         $term  = "isbn";
119         $value = $isbn;
120     }
121     else {
122         $term  = "title";
123         $value = $title;
124     }
125     if ( $count > 10 ) {
126         for ( my $i = 1 ; $i < $count / 10 + 1 ; $i++ ) {
127             if ( $i < 16 ) {
128                 my $highlight = 0;
129                 ( $offset == ( $i - 1 ) * 10 ) && ( $highlight = 1 );
130                 push @numbers,
131                   {
132                     number    => $i,
133                     highlight => $highlight,
134                     term      => $term,
135                     value     => $value,
136                     startfrom => ( $i - 1 ) * 10
137                 };
138             }
139         }
140     }
141
142     # fill with books in breeding farm
143     ( $count, @results ) = BreedingSearch( $title, $isbn );
144     my @breeding_loop = ();
145     for ( my $i = 0 ; $i <= $#results ; $i++ ) {
146         my %row_data;
147         if ( $i % 2 ) {
148             $toggle = "#ffffcc";
149         }
150         else {
151             $toggle = "white";
152         }
153         $row_data{toggle} = $toggle;
154         $row_data{id}     = $results[$i]->{'id'};
155         $row_data{isbn}   = $results[$i]->{'isbn'};
156         $row_data{file}   = $results[$i]->{'file'};
157         $row_data{title}  = $results[$i]->{'title'};
158         $row_data{author} = $results[$i]->{'author'};
159         $row_data{classification} = $results[$i]->{'classification'};
160         $row_data{subclass} = $results[$i]->{'subclass'};
161           push ( @breeding_loop, \%row_data );
162     }
163 # get framework list
164         my $frameworks = getframeworks;
165         my @frameworkcodeloop;
166         foreach my $thisframeworkcode (keys %$frameworks) {
167                 my %row =(value => $thisframeworkcode,
168                                         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
169                                 );
170                 push @frameworkcodeloop, \%row;
171         }
172
173
174     $template->param(
175         isbn          => $isbn,
176         title         => $title,
177         showoffset    => $showoffset,
178         total         => $total,
179         grandtotal         => $grandtotal,
180         offset        => $offset,
181         results_loop          => \@loop_data,
182         breeding_loop => \@breeding_loop,
183         numbers       => \@numbers,
184         term          => $term,
185         value         => $value,
186         frameworkcodeloop => \@frameworkcodeloop
187     );
188
189   output_html_with_http_headers $input, $cookie, $template->output;
190 }    # else