Bug 12478: make things using SimpleSearch use the new version
[koha.git] / cataloguing / addbooks.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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 =head1 cataloguing:addbooks.pl
22
23         TODO
24
25 =cut
26
27 use strict;
28 use warnings;
29 use CGI qw ( -utf8 );
30 use C4::Auth;
31 use C4::Biblio;
32 use C4::Breeding;
33 use C4::Output;
34 use C4::Koha;
35 use C4::Search;
36
37 use Koha::SearchEngine::Search;
38 use Koha::SearchEngine::QueryBuilder;
39
40 my $input = new CGI;
41
42 my $success = $input->param('biblioitem');
43 my $query   = $input->param('q');
44 my @value   = $input->param('value');
45 my $page    = $input->param('page') || 1;
46 my $results_per_page = 20;
47
48
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50     {
51         template_name   => "cataloguing/addbooks.tt",
52         query           => $input,
53         type            => "intranet",
54         authnotrequired => 0,
55         flagsrequired   => { editcatalogue => '*' },
56         debug           => 1,
57     }
58 );
59
60 # get framework list
61 my $frameworks = getframeworks;
62 my @frameworkcodeloop;
63 foreach my $thisframeworkcode ( sort { uc($frameworks->{$a}->{'frameworktext'}) cmp uc($frameworks->{$b}->{'frameworktext'}) } keys %{$frameworks} ) {
64     push @frameworkcodeloop, {
65         value         => $thisframeworkcode,
66         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
67     };
68 }
69
70
71 # Searching the catalog.
72 if ($query) {
73
74     # build query
75     my @operands = $query;
76
77     my $QParser;
78     $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
79     my $builtquery;
80     my $builder  = Koha::SearchEngine::QueryBuilder->new();
81     my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
82     if ($QParser) {
83         $builtquery = $query;
84     } else {
85         ( undef,$builtquery,undef,undef,undef,undef,undef,undef,undef,undef) = $builder->build_query_compat(undef,\@operands);
86     }
87     # find results
88     my ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($builtquery, $results_per_page * ($page - 1), $results_per_page);
89
90     if ( defined $error ) {
91         $template->param( error => $error );
92         warn "error: " . $error;
93         output_html_with_http_headers $input, $cookie, $template->output;
94         exit;
95     }
96
97     # format output
98     # SimpleSearch() give the results per page we want, so 0 offet here
99     my $total = @{$marcresults};
100     my @newresults = searchResults( 'intranet', $query, $total, $results_per_page, 0, 0, $marcresults );
101     $template->param(
102         total          => $total_hits,
103         query          => $query,
104         resultsloop    => \@newresults,
105         pagination_bar => pagination_bar( "/cgi-bin/koha/cataloguing/addbooks.pl?q=$query&", getnbpages( $total_hits, $results_per_page ), $page, 'page' ),
106     );
107 }
108
109 # fill with books in breeding farm
110
111 my $countbr = 0;
112 my @resultsbr;
113 if ($query) {
114 # fill isbn or title, depending on what has been entered
115 #u must do check on isbn because u can find number in beginning of title
116 #check is on isbn legnth 13 for new isbn and 10 for old isbn
117     my ( $title, $isbn );
118     if ($query=~/\d/) {
119         my $clean_query = $query;
120         $clean_query =~ s/-//g; # remove hyphens
121         my $querylength = length $clean_query;
122         if ( $querylength == 13 || $querylength == 10 ) {
123             $isbn = $query;
124         }
125     }
126     if (!$isbn) {
127         $title = $query;
128     }
129     ( $countbr, @resultsbr ) = BreedingSearch( $title, $isbn );
130 }
131 my $breeding_loop = [];
132 for my $resultsbr (@resultsbr) {
133     push @{$breeding_loop}, {
134         id               => $resultsbr->{import_record_id},
135         isbn             => $resultsbr->{isbn},
136         copyrightdate    => $resultsbr->{copyrightdate},
137         editionstatement => $resultsbr->{editionstatement},
138         file             => $resultsbr->{file_name},
139         title            => $resultsbr->{title},
140         author           => $resultsbr->{author},
141     };
142 }
143
144 $template->param(
145     frameworkcodeloop => \@frameworkcodeloop,
146     breeding_count    => $countbr,
147     breeding_loop     => $breeding_loop,
148     z3950_search_params => C4::Search::z3950_search_args($query),
149 );
150
151 output_html_with_http_headers $input, $cookie, $template->output;
152