Bug 23355: (RM follow-up) Make DB update idempotent
[koha.git] / cataloguing / addbooks.pl
blob9f6fec7f05ceb7886a37c5bb913bc8f8c9faa711
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
21 =head1 cataloguing:addbooks.pl
23 TODO
25 =cut
27 use Modern::Perl;
29 use CGI qw ( -utf8 );
30 use URI::Escape;
31 use C4::Auth;
32 use C4::Biblio;
33 use C4::Breeding;
34 use C4::Output;
35 use C4::Koha;
36 use C4::Languages qw(getlanguage);
37 use C4::Search;
39 use Koha::BiblioFrameworks;
40 use Koha::SearchEngine::Search;
41 use Koha::SearchEngine::QueryBuilder;
42 use Koha::Z3950Servers;
44 my $input = new CGI;
46 my $success = $input->param('biblioitem');
47 my $query = $input->param('q');
48 my @value = $input->multi_param('value');
49 my $page = $input->param('page') || 1;
50 my $results_per_page = 20;
51 my $lang = C4::Languages::getlanguage($input);
54 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
56 template_name => "cataloguing/addbooks.tt",
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => { editcatalogue => '*' },
61 debug => 1,
65 # Searching the catalog.
66 if ($query) {
68 # build query
69 my @operands = $query;
71 my $builtquery;
72 my $query_cgi;
73 my $builder = Koha::SearchEngine::QueryBuilder->new(
74 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
75 my $searcher = Koha::SearchEngine::Search->new(
76 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
77 ( undef, $builtquery, undef, $query_cgi, undef, undef, undef, undef, undef, undef ) =
78 $builder->build_query_compat( undef, \@operands, undef, undef, undef, 0, $lang );
80 $template->param( search_query => $builtquery ) if C4::Context->preference('DumpSearchQueryTemplate');
82 # find results
83 my ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($builtquery, $results_per_page * ($page - 1), $results_per_page);
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;
92 # format output
93 # SimpleSearch() give the results per page we want, so 0 offet here
94 my $total = @{$marcresults};
95 my @newresults = searchResults( {'interface' => 'intranet'}, $query, $total, $results_per_page, 0, 0, $marcresults );
96 foreach my $line (@newresults) {
97 if ( not exists $line->{'size'} ) { $line->{'size'} = "" }
99 $template->param(
100 total => $total_hits,
101 query => $query,
102 resultsloop => \@newresults,
103 pagination_bar => pagination_bar( "/cgi-bin/koha/cataloguing/addbooks.pl?$query_cgi&", getnbpages( $total_hits, $results_per_page ), $page, 'page' ),
107 # fill with books in breeding farm
109 my $countbr = 0;
110 my @resultsbr;
111 if ($query) {
112 # fill isbn or title, depending on what has been entered
113 #u must do check on isbn because u can find number in beginning of title
114 #check is on isbn legnth 13 for new isbn and 10 for old isbn
115 my ( $title, $isbn );
116 if ($query=~/\d/) {
117 my $clean_query = $query;
118 $clean_query =~ s/-//g; # remove hyphens
119 my $querylength = length $clean_query;
120 if ( $querylength == 13 || $querylength == 10 ) {
121 $isbn = $query;
124 if (!$isbn) {
125 $title = $query;
127 ( $countbr, @resultsbr ) = BreedingSearch( $title, $isbn );
129 my $breeding_loop = [];
130 for my $resultsbr (@resultsbr) {
131 push @{$breeding_loop}, {
132 id => $resultsbr->{import_record_id},
133 isbn => $resultsbr->{isbn},
134 copyrightdate => $resultsbr->{copyrightdate},
135 editionstatement => $resultsbr->{editionstatement},
136 file => $resultsbr->{file_name},
137 title => $resultsbr->{title},
138 author => $resultsbr->{author},
142 my $servers = Koha::Z3950Servers->search(
144 recordtype => 'biblio',
145 servertype => ['zed','sru'],
149 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
150 $template->param(
151 servers => $servers,
152 frameworks => $frameworks,
153 breeding_count => $countbr,
154 breeding_loop => $breeding_loop,
155 z3950_search_params => C4::Search::z3950_search_args($query),
158 output_html_with_http_headers $input, $cookie, $template->output;