Bug 29407: Make the pickup locations dropdown JS reusable
[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 Modern::Perl;
28
29 use CGI qw ( -utf8 );
30 use C4::Auth qw( get_template_and_user );
31 use C4::Breeding qw( BreedingSearch );
32 use C4::Output qw( output_html_with_http_headers pagination_bar );
33 use C4::Koha qw( getnbpages );
34 use C4::Languages;
35 use C4::Search qw( searchResults z3950_search_args );
36
37 use Koha::BiblioFrameworks;
38 use Koha::SearchEngine::Search;
39 use Koha::SearchEngine::QueryBuilder;
40 use Koha::Z3950Servers;
41 use Business::ISBN;
42
43 my $input = CGI->new;
44
45 my $success = $input->param('biblioitem');
46 my $query   = $input->param('q');
47 my @value   = $input->multi_param('value');
48 my $page    = $input->param('page') || 1;
49 my $results_per_page = 20;
50 my $lang = C4::Languages::getlanguage($input);
51
52
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {
55         template_name   => "cataloguing/addbooks.tt",
56         query           => $input,
57         type            => "intranet",
58         flagsrequired   => { editcatalogue => '*' },
59     }
60 );
61
62 # Searching the catalog.
63 if ($query) {
64
65     # build query
66     my @operands = $query;
67
68     my $builtquery;
69     my $query_cgi;
70     my $builder = Koha::SearchEngine::QueryBuilder->new(
71         { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
72     my $searcher = Koha::SearchEngine::Search->new(
73         { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
74     ( undef, $builtquery, undef, $query_cgi, undef, undef, undef, undef, undef, undef ) =
75       $builder->build_query_compat( undef, \@operands, undef, undef, undef, 0, $lang, { weighted_fields => 1 });
76
77     $template->param( search_query => $builtquery ) if C4::Context->preference('DumpSearchQueryTemplate');
78
79     # find results
80     my ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($builtquery, $results_per_page * ($page - 1), $results_per_page);
81
82     if ( defined $error ) {
83         $template->param( error => $error );
84         warn "error: " . $error;
85         output_html_with_http_headers $input, $cookie, $template->output;
86         exit;
87     }
88
89     # format output
90     # SimpleSearch() give the results per page we want, so 0 offet here
91     my $total = @{$marcresults};
92     my @newresults = searchResults( {'interface' => 'intranet'}, $query, $total, $results_per_page, 0, 0, $marcresults );
93     foreach my $line (@newresults) {
94         if ( not exists $line->{'size'} ) { $line->{'size'} = "" }
95     }
96     $template->param(
97         total          => $total_hits,
98         query          => $query,
99         resultsloop    => \@newresults,
100         pagination_bar => pagination_bar( "/cgi-bin/koha/cataloguing/addbooks.pl?$query_cgi&", getnbpages( $total_hits, $results_per_page ), $page, 'page' ),
101     );
102 }
103
104 # fill with books in breeding farm
105
106 my $countbr = 0;
107 my @resultsbr;
108 if ($query) {
109     my ( $title, $isbn );
110     my $isbn_valid = Business::ISBN->new($query);
111     if ( $isbn_valid && $isbn_valid->is_valid() ) {
112         $isbn = $query;
113     } else {
114         $title = $query;
115     }
116     ( $countbr, @resultsbr ) = BreedingSearch( $title, $isbn );
117 }
118 my $breeding_loop = [];
119 for my $resultsbr (@resultsbr) {
120     push @{$breeding_loop}, {
121         id               => $resultsbr->{import_record_id},
122         isbn             => $resultsbr->{isbn},
123         copyrightdate    => $resultsbr->{copyrightdate},
124         editionstatement => $resultsbr->{editionstatement},
125         file             => $resultsbr->{file_name},
126         title            => $resultsbr->{title},
127         author           => $resultsbr->{author},
128     };
129 }
130
131 my $servers = Koha::Z3950Servers->search(
132     {
133         recordtype => 'biblio',
134         servertype => ['zed','sru'],
135     }
136 );
137
138 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
139 $template->param(
140     servers           => $servers,
141     frameworks        => $frameworks,
142     breeding_count    => $countbr,
143     breeding_loop     => $breeding_loop,
144     z3950_search_params => C4::Search::z3950_search_args($query),
145 );
146
147 output_html_with_http_headers $input, $cookie, $template->output;
148