Bug 22417: Adapt the batch_record_modification tool

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Cook <dcook@prosentient.com.au>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2019-02-25 21:46:01 -03:00
parent 8c47e0c2a6
commit 63cf7a0307
3 changed files with 38 additions and 160 deletions

View File

@ -76,6 +76,21 @@
Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% m.biblionumber | uri %]">[% m.biblionumber | html %]</a> has not been modified. An error occurred on modifying it.
[% CASE 'biblio_modified' %]
Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% m.biblionumber | uri %]">[% m.biblionumber | html %]</a> has successfully been modified.
<h3>Next steps</h3>
<ul>
<li><a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
[% IF lists.count %]
<li>
<label for="add_bibs_to_list">Add modified records to the following list: </label>
<select name="add_bibs_to_list" id="add_bibs_to_list">
<option value="">Select a list</option>
[% FOREACH list IN lists %]
<option class="shelf" value="[% list.shelfnumber | html %]">[% list.shelfname | html %]</option>
[% END %]
</select>
</li>
[% END %]
</ul>
[% END %]
</div>
[% END %]

View File

@ -42,12 +42,8 @@
Bibliographic record [% message.biblionumber | html %] does not exist in the database.
[% ELSIF message.code == 'authority_not_exists' %]
Authority record [% message.authid | html %] does not exist in the database.
[% ELSIF message.code == 'biblio_not_modified' %]
Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% message.biblionumber | uri %]">[% message.biblionumber | html %]</a> has not been modified. An error occurred on modifying it.
[% ELSIF message.code == 'authority_not_modified' %]
Authority record <a href="/cgi-bin/koha/authorities/detail.pl?authid=[% message.authid | uri %]">[% message.authid | html %]</a> has not been modified. An error occurred on modifying it.
[% ELSIF message.code == 'biblio_modified' %]
Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% message.biblionumber | uri %]">[% message.biblionumber | html %]</a> has successfully been modified.
[% ELSIF message.code == 'authority_modified' %]
Bibliographic record <a href="/cgi-bin/koha/authorities/detail.pl?authid=[% message.authid | uri %]">[% message.authid | html %]</a> has successfully been modified.
[% END %]
@ -147,7 +143,7 @@
</form> <!-- /#record_ids_selection -->
[% ELSIF view == 'list' %]
[% IF records %]
<form action="/cgi-bin/koha/tools/batch_record_modification.pl" method="post" id="process">
<form action="/cgi-bin/koha/tools/batch_record_modification.pl" method="post">
<label for="marc_modification_template_id" class="required">Modify record using the following template: </label>
<select name="marc_modification_template_id" id="marc_modification_template_id" required="required">
<option value="">Select a template</option>
@ -233,49 +229,14 @@
<fieldset class="action">
<input type="hidden" name="op" value="modify" />
<input type="hidden" name="recordtype" value="[% recordtype | html %]" />
<input type="button" id="mainformsubmit" value="Modify selected records" class="button" />
<input type="hidden" name="runinbackground" id="runinbackground" value="" />
<input type="hidden" name="completedJobID" id="completedJobID" value="" />
<a class="cancel" href="/cgi-bin/koha/tools/batch_record_modification.pl">Cancel</a>
<input type="submit" id="mainformsubmit" value="Modify selected records" class="button" />
</fieldset>
<div id="jobpanel">
<div id="jobstatus" class="progress_panel">Job progress: <div id="jobprogress"></div> <span id="jobprogresspercent">0</span>%</div>
<div id="jobfailed"></div>
</div>
</form> <!-- /#process -->
[% ELSE %]
<div class="dialog message">
No records were modified. [% IF recordtype != 'biblio' %]<a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>[% END %]
</div>
[% END %]
[% ELSIF view == 'report' %]
[% IF report.total_records == report.total_success %]
<div class="dialog message">
All records have successfully been modified! [% IF recordtype != 'biblio' %]<a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>[% END %]
</div>
[% ELSE %]
<div class="dialog message">
[% report.total_success | html %] / [% report.total_records | html %] records have successfully been modified. Some errors occurred. <a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
</div>
[% END %]
[% IF recordtype == 'biblio' %]
<h3>Next steps</h3>
<ul>
<li><a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
[% IF lists.count %]
<li>
<label for="add_bibs_to_list">Add modified records to the following list: </label>
<select name="add_bibs_to_list" id="add_bibs_to_list">
<option value="">Select a list</option>
[% FOREACH list IN lists %]
<option class="shelf" value="[% list.shelfnumber | html %]">[% list.shelfname | html %]</option>
[% END %]
</select>
</li>
[% END %]
</ul>
[% END %]
[% ELSIF view == 'errors' %]
[% FOR error IN errors %]
[% IF error == 'no_template_defined' %]
@ -284,6 +245,12 @@
</div>
[% END %]
[% END %]
[% ELSIF view == 'enqueued' %]
<div class="dialog message">
The job has been enqueued! It will be processed as soon as possible (FIXME - well, it could depend on a config?)
<a href="/cgi-bin/koha/admin/background_jobs.pl?op=view&id=[% job_id | uri %]" title="View detail of the enqueued job">View detail of the enqueued job</a>
| <a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
</div>
[% ELSE %]
<div class="dialog alert">
No action defined for the template.
@ -291,7 +258,6 @@
[% END %]
</main>
</div> <!-- /.col-sm-10.col-sm-push-2 -->
<div class="col-sm-2 col-sm-pull-10">
<aside>
[% INCLUDE 'tools-menu.inc' %]
@ -362,7 +328,7 @@
alert(_("Please select a modification template."));
return false;
}
return submitBackgroundJob(document.getElementById("process"));
return true;
});
$("#record_ids_selection").on("submit", function(e) {

View File

@ -22,15 +22,17 @@ use Modern::Perl;
use CGI;
use List::MoreUtils qw( uniq );
use JSON qw( encode_json );
use Net::RabbitFoot;
use C4::Auth qw( get_template_and_user );
use C4::Output qw( output_html_with_http_headers );
use C4::AuthoritiesMarc qw( BuildSummary ModAuthority );
use C4::BackgroundJob;
use C4::Biblio qw( GetMarcBiblio ModBiblio );
use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModificationTemplates ModifyRecordWithTemplate );
use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModificationTemplates );
use Koha::Biblios;
use Koha::BackgroundJob::BatchUpdateBiblio;
use Koha::MetadataRecord::Authority;
use Koha::Virtualshelves;
@ -53,23 +55,6 @@ $template->param( lists => scalar Koha::Virtualshelves->search([{ category => 1,
my $sessionID = $input->cookie("CGISESSID");
my $runinbackground = $input->param('runinbackground');
my $completedJobID = $input->param('completedJobID');
if ( $completedJobID ) {
my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
my $report = $job->get('report');
my $messages = $job->get('messages');
$template->param(
report => $report,
messages => $messages,
view => 'report',
recordtype => $recordtype,
);
output_html_with_http_headers $input, $cookie, $template->output;
$job->clear();
exit;
}
my @templates = GetModificationTemplates( $mmtid );
unless ( @templates ) {
$op = 'error';
@ -165,105 +150,17 @@ if ( $op eq 'form' ) {
# We want to modify selected records!
my @record_ids = $input->multi_param('record_id');
my ( $job );
if ( $runinbackground ) {
my $job_size = scalar( @record_ids );
$job = C4::BackgroundJob->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
my $job_id = $job->id;
if (my $pid = fork) {
$dbh->{InactiveDestroy} = 1;
my $reply = CGI->new("");
print $reply->header(-type => 'text/html');
print '{"jobID":"' . $job_id . '"}';
exit 0;
} elsif (defined $pid) {
close STDOUT;
} else {
warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
exit 0;
my $job_id = Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue(
{
mmtid => $mmtid,
record_type => $recordtype,
record_ids => \@record_ids,
}
}
my $report = {
total_records => 0,
total_success => 0,
};
my $progress = 0;
$dbh->{RaiseError} = 1;
RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
$report->{total_records}++;
next unless $record_id;
if ( $recordtype eq 'biblio' ) {
# Biblios
my $biblionumber = $record_id;
# Finally, modify the biblio
my $error = eval {
my $record = GetMarcBiblio({ biblionumber => $biblionumber });
ModifyRecordWithTemplate( $mmtid, $record );
my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
ModBiblio( $record, $biblionumber, $frameworkcode );
};
if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
push @messages, {
type => 'error',
code => 'biblio_not_modified',
biblionumber => $biblionumber,
error => ($@ ? $@ : $error),
};
} else {
push @messages, {
type => 'success',
code => 'biblio_modified',
biblionumber => $biblionumber,
};
$report->{total_success}++;
}
} else {
# Authorities
my $authid = $record_id;
my $error = eval {
my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
my $record = $authority->record;
ModifyRecordWithTemplate( $mmtid, $record );
ModAuthority( $authid, $record, $authority->authtypecode );
};
if ( $error and $error != $authid or $@ ) {
push @messages, {
type => 'error',
code => 'authority_not_modified',
authid => $authid,
error => ($@ ? $@ : 0),
};
} else {
push @messages, {
type => 'success',
code => 'authority_modified',
authid => $authid,
};
$report->{total_success}++;
}
}
$job->set({
view => 'report',
report => $report,
messages => \@messages,
});
$job->progress( ++$progress ) if $runinbackground;
}
if ($runinbackground) {
$job->finish if defined $job;
} else {
$template->param(
view => 'report',
report => $report,
messages => \@messages,
);
}
);
$template->param(
view => 'enqueued',
job_id => $job_id,
);
}
$template->param(