Bug 22082: ambiguous column in patron stats
[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::Languages qw(getlanguage);
36 use C4::Search;
37
38 use Koha::BiblioFrameworks;
39 use Koha::SearchEngine::Search;
40 use Koha::SearchEngine::QueryBuilder;
41
42 my $input = new CGI;
43
44 my $success = $input->param('biblioitem');
45 my $query   = $input->param('q');
46 my @value   = $input->multi_param('value');
47 my $page    = $input->param('page') || 1;
48 my $results_per_page = 20;
49 my $lang = C4::Languages::getlanguage($input);
50
51
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53     {
54         template_name   => "cataloguing/addbooks.tt",
55         query           => $input,
56         type            => "intranet",
57         authnotrequired => 0,
58         flagsrequired   => { editcatalogue => '*' },
59         debug           => 1,
60     }
61 );
62
63 # Searching the catalog.
64 if ($query) {
65
66     # build query
67     my @operands = $query;
68
69     my $QParser;
70     $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
71     my $builtquery;
72     my $builder = Koha::SearchEngine::QueryBuilder->new(
73         { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
74     my $searcher = Koha::SearchEngine::Search->new(
75         { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
76     if ($QParser) {
77         $builtquery = $query;
78     } else {
79         ( undef, $builtquery, undef, undef, undef, undef, undef, undef, undef, undef ) =
80           $builder->build_query_compat( undef, \@operands, undef, undef, undef, 0, $lang );
81     }
82     # find results
83     my ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($builtquery, $results_per_page * ($page - 1), $results_per_page);
84
85     if ( defined $error ) {
86         $template->param( error => $error );
87         warn "error: " . $error;
88         output_html_with_http_headers $input, $cookie, $template->output;
89         exit;
90     }
91
92     # format output
93     # SimpleSearch() give the results per page we want, so 0 offet here
94     my $total = @{$marcresults};
95     my @newresults = searchResults( 'intranet', $query, $total, $results_per_page, 0, 0, $marcresults );
96     $template->param(
97         total          => $total_hits,
98         query          => $query,
99         resultsloop    => \@newresults,
100         pagination_bar => pagination_bar( "/cgi-bin/koha/cataloguing/addbooks.pl?q=$query&", 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 # fill isbn or title, depending on what has been entered
110 #u must do check on isbn because u can find number in beginning of title
111 #check is on isbn legnth 13 for new isbn and 10 for old isbn
112     my ( $title, $isbn );
113     if ($query=~/\d/) {
114         my $clean_query = $query;
115         $clean_query =~ s/-//g; # remove hyphens
116         my $querylength = length $clean_query;
117         if ( $querylength == 13 || $querylength == 10 ) {
118             $isbn = $query;
119         }
120     }
121     if (!$isbn) {
122         $title = $query;
123     }
124     ( $countbr, @resultsbr ) = BreedingSearch( $title, $isbn );
125 }
126 my $breeding_loop = [];
127 for my $resultsbr (@resultsbr) {
128     push @{$breeding_loop}, {
129         id               => $resultsbr->{import_record_id},
130         isbn             => $resultsbr->{isbn},
131         copyrightdate    => $resultsbr->{copyrightdate},
132         editionstatement => $resultsbr->{editionstatement},
133         file             => $resultsbr->{file_name},
134         title            => $resultsbr->{title},
135         author           => $resultsbr->{author},
136     };
137 }
138
139 my $schema = Koha::Database->new()->schema();
140 my $servers = $schema->resultset('Z3950server')->search(
141         {
142             recordtype => 'biblio',
143             servertype => ['zed', 'sru'],
144         },
145         {   result_class => 'DBIx::Class::ResultClass::HashRefInflator',
146             order_by     => ['rank', 'servername'],
147         },
148 );
149
150 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
151 $template->param(
152     servers           => $servers,
153     frameworks        => $frameworks,
154     breeding_count    => $countbr,
155     breeding_loop     => $breeding_loop,
156     z3950_search_params => C4::Search::z3950_search_args($query),
157 );
158
159 output_html_with_http_headers $input, $cookie, $template->output;
160