Bug 10558 - Convert records table in manage-marc-import.pl to Ajax DataTable
[koha.git] / tools / batch_records_ajax.pl
1 #!/usr/bin/perl
2
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
4
5 # Copyright 2007 Tamil s.a.r.l.
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 =head1 ysearch.pl
23
24
25 =cut
26
27 use Modern::Perl;
28
29 use CGI;
30 use JSON qw/ to_json /;
31
32 use C4::Context;
33 use C4::Charset;
34 use C4::Auth qw/check_cookie_auth/;
35 use C4::ImportBatch;
36
37 my $input = new CGI;
38
39 my @sort_columns =
40   qw/import_record_id title status overlay_status overlay_status/;
41
42 my $import_batch_id   = $input->param('import_batch_id');
43 my $offset            = $input->param('iDisplayStart');
44 my $results_per_page  = $input->param('iDisplayLength');
45 my $sorting_column    = $sort_columns[ $input->param('iSortCol_0') ];
46 my $sorting_direction = $input->param('sSortDir_0');
47
48 binmode STDOUT, ":encoding(UTF-8)";
49 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
50
51 my ( $auth_status, $sessionID ) =
52   check_cookie_auth( $input->cookie('CGISESSID'), { editcatalogue => '*' } );
53 if ( $auth_status ne "ok" ) {
54     exit 0;
55 }
56
57 my $batch = GetImportBatch($import_batch_id);
58 my $records =
59   GetImportRecordsRange( $import_batch_id, $offset, $results_per_page, undef,
60     { order_by => $sorting_column, order_by_direction => $sorting_direction } );
61 my @list = ();
62 foreach my $record (@$records) {
63     my $citation = $record->{'title'} || $record->{'authorized_heading'};
64     $citation .= " $record->{'author'}" if $record->{'author'};
65     $citation .= " (" if $record->{'issn'} or $record->{'isbn'};
66     $citation .= $record->{'isbn'} if $record->{'isbn'};
67     $citation .= ", " if $record->{'issn'} and $record->{'isbn'};
68     $citation .= $record->{'issn'} if $record->{'issn'};
69     $citation .= ")" if $record->{'issn'} or $record->{'isbn'};
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         status          => $record->{'status'},
96         overlay_status  => $record->{'overlay_status'},
97         match_citation  => $match_citation,
98         matched         => $record->{'matched_biblionumber'}
99           || $record->{'matched_authid'}
100           || q{},
101         score => $#$match > -1 ? $match->[0]->{'score'} : 0,
102         match_id => $match_id,
103       };
104 }
105
106 my $data;
107 $data->{'iTotalRecords'}        = $batch->{'num_records'};
108 $data->{'iTotalDisplayRecords'} = $batch->{'num_records'};
109 $data->{'sEcho'}                = $input->param('sEcho') || undef;
110 $data->{'aaData'}               = \@list;
111
112 print to_json($data);