Bug 30477: Add new UNIMARC installer translation files
[koha.git] / tools / batch_records_ajax.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 ByWater Solutions
4 # Based on circ/ysearch.pl: Copyright 2007 Tamil s.a.r.l.
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 NAME
22
23 batch_records_ajax.pl - A script for searching batch imported records via ajax
24
25 =head1 SYNOPSIS
26
27 This script is to be used as a data source for DataTables that load and display
28 the records from an import batch.
29
30 =cut
31
32 use Modern::Perl;
33
34 use CGI qw ( -utf8 );
35 use JSON qw( to_json );
36
37 use C4::Context;
38 use C4::Auth qw( check_cookie_auth );
39 use C4::ImportBatch qw( GetImportBatch GetImportRecordsRange GetImportRecordMatches );
40
41 my $input = CGI->new;
42
43 my @sort_columns =
44   qw/import_record_id title status overlay_status overlay_status/;
45
46 my $import_batch_id   = $input->param('import_batch_id');
47 my $offset            = $input->param('iDisplayStart');
48 my $results_per_page  = $input->param('iDisplayLength');
49 my $sorting_column    = $sort_columns[ $input->param('iSortCol_0') // 0 ];
50 my $sorting_direction = $input->param('sSortDir_0');
51
52 $results_per_page = undef if $results_per_page && $results_per_page == -1;
53
54 binmode STDOUT, ":encoding(UTF-8)";
55 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
56
57 my ( $auth_status ) =
58   check_cookie_auth( $input->cookie('CGISESSID'), { tools => 'manage_staged_marc' } );
59 if ( $auth_status ne "ok" ) {
60     exit 0;
61 }
62
63 my $batch = GetImportBatch($import_batch_id);
64 my $records =
65   GetImportRecordsRange( $import_batch_id, $offset, $results_per_page, undef,
66     { order_by => $sorting_column, order_by_direction => $sorting_direction } );
67 my @list = ();
68 foreach my $record (@$records) {
69     my $citation = $record->{'title'} || $record->{'authorized_heading'};
70
71     my $match = GetImportRecordMatches( $record->{'import_record_id'}, 1 );
72     my $match_citation = '';
73     my $match_id;
74     if ( $#$match > -1 ) {
75         if ( $match->[0]->{'record_type'} eq 'biblio' ) {
76             $match_citation .= $match->[0]->{'title'}
77               if defined( $match->[0]->{'title'} );
78             $match_citation .= ' ' . $match->[0]->{'author'}
79               if defined( $match->[0]->{'author'} );
80             $match_id = $match->[0]->{'biblionumber'};
81         }
82         elsif ( $match->[0]->{'record_type'} eq 'auth' ) {
83             if ( defined( $match->[0]->{'authorized_heading'} ) ) {
84                 $match_citation .= $match->[0]->{'authorized_heading'};
85                 $match_id = $match->[0]->{'candidate_match_id'};
86             }
87         }
88     }
89
90     push @list,
91       {
92         DT_RowId        => $record->{'import_record_id'},
93         import_record_id => $record->{'import_record_id'},
94         citation        => $citation,
95         author          => $record->{'author'},
96         issn            => $record->{'issn'},
97         isbn            => $record->{'isbn'},
98         status          => $record->{'status'},
99         overlay_status  => $record->{'overlay_status'},
100         match_citation  => $match_citation,
101         matched         => $record->{'matched_biblionumber'}
102           || $record->{'matched_authid'}
103           || q{},
104         score => $#$match > -1 ? $match->[0]->{'score'} : 0,
105         match_id => $match_id,
106         diff_url => $match_id ? "/cgi-bin/koha/tools/showdiffmarc.pl?batchid=$import_batch_id&importid=$record->{import_record_id}&id=$match_id&type=$record->{record_type}" : undef
107       };
108 }
109
110 my $data;
111 $data->{'iTotalRecords'}        = $batch->{'num_records'};
112 $data->{'iTotalDisplayRecords'} = $batch->{'num_records'};
113 $data->{'sEcho'}                = $input->param('sEcho') || undef;
114 $data->{'aaData'}               = \@list;
115
116 print to_json($data);