From 4b4b161d50e039833610e5d8c4e6521199c6bd76 Mon Sep 17 00:00:00 2001 From: Chris Nighswonger Date: Tue, 1 Sep 2009 14:05:52 -0400 Subject: [PATCH] [21/40] Adding de-duplicating method and associated label edit batch code. --- C4/Labels/Batch.pm | 27 +++++++++++++++++++ .../prog/en/css/staff-global.css | 5 ++++ .../en/includes/labels-batches-toolbar.inc | 2 +- .../en/modules/labels/label-edit-batch.tmpl | 9 ++++++- labels/label-edit-batch.pl | 23 +++++++++++++--- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/C4/Labels/Batch.pm b/C4/Labels/Batch.pm index d7d483a274..05a6a2a67f 100644 --- a/C4/Labels/Batch.pm +++ b/C4/Labels/Batch.pm @@ -269,6 +269,33 @@ sub delete { return 0; } +=head2 C4::Labels::Batch->remove_duplicates(batch_id => batch_id) | $batch->remove_duplicates() + + Invoking the remove_duplicates method attempts to remove duplicates the batch from the database. The method returns the count of duplicate + records removed upon success and -1 upon failure. Errors are logged to the syslog. + + examples: + my $remove_count = $batch->remove_duplicates(); # to remove duplicates the record behind the $batch object + +=cut + +sub remove_duplicates { + my $self = shift; + my %seen=(); + my $query = "DELETE FROM labels_batches WHERE label_id = ?;"; # ORDER BY timestamp ASC LIMIT ?;"; + my $sth = C4::Context->dbh->prepare($query); + my @duplicate_items = grep{$seen{$_->{'item_number'}}++} @{$self->{'items'}}; + foreach my $item (@duplicate_items) { + $sth->execute($item->{'label_id'}); + if ($sth->err) { + syslog("LOG_ERR", "C4::Labels::Batch->remove_duplicates() : Database returned the following error on attempted DELETE for label_id %s: %s", $item->{'label_id'}, $sth->errstr); + return -1; + } + $sth->finish(); # Per DBI.pm docs: "If execute() is called on a statement handle that's still active ($sth->{Active} is true) then it should effectively call finish() to tidy up the previous execution results before starting this new execution." + @{$self->{'items'}} = grep{$_->{'label_id'} != $item->{'label_id'}} @{$self->{'items'}}; # the correct label/item must be removed from the current batch object as well; this should be done *after* each sql DELETE in case the DELETE fails + } + return scalar(@duplicate_items); +} 1; __END__ diff --git a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css index 145c2bf697..5324c70ae1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css +++ b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css @@ -1107,6 +1107,11 @@ div.alert strong { color : #900; } +div.dialog { + background : #FFC url(../../img/dialog-bg.gif) repeat-x left 0; + text-align : center; +} + div.message { background : white url("../../img/message-bg.gif") repeat-x left 0; border : 1px solid #bcbcbc; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/labels-batches-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/labels-batches-toolbar.inc index 38d92e322f..48c317a48b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/labels-batches-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/labels-batches-toolbar.inc @@ -30,7 +30,7 @@
  • Add item(s) to batch
  • ">Delete current batch
  • -
  • &type=">Remove duplicates
  • +
  • ">Remove duplicate items
  • .pl?batch_id=&type=">Generate labels for Batch
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tmpl index 514176c467..2859c4ac53 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tmpl @@ -126,11 +126,18 @@
    -
    +
    WARNING: An error was encountered and Please have your system administrator check the syslog for details.
    + +
    +
    + duplicate item(s) found and removed from batch . +
    +
    +
    diff --git a/labels/label-edit-batch.pl b/labels/label-edit-batch.pl index 0a9877b44b..1eae99b826 100755 --- a/labels/label-edit-batch.pl +++ b/labels/label-edit-batch.pl @@ -50,6 +50,8 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( ); my $err = 0; my $errstr = undef; +my $duplicate_count = undef; +my $duplicate_message = undef; my $db_rows = {}; my $batch = undef; my $display_columns = [ {_label_number => {label => 'Label Number', link_field => 0}}, @@ -90,7 +92,13 @@ elsif ($op eq 'new') { $batch = C4::Labels::Batch->new(branch_code => $branch_code); $batch_id = $batch->get_attr('batch_id'); } -else { # display batch +elsif ($op eq 'de_duplicate') { + $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id); + $duplicate_count = $batch->remove_duplicates(); + $duplicate_message = 1 if $duplicate_count != -1; + $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1; +} +else { # edit $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id); } @@ -99,13 +107,20 @@ $db_rows = get_label_summary(items => $items, batch_id => $batch_id); my $table = html_table($display_columns, $db_rows); -$template->param( err => $err, - errstr => $errstr, +$template->param( + duplicate_message => $duplicate_message, + duplicate_count => $duplicate_count, + ); + +$template->param( + err => $err, + errstr => $errstr, ) if ($err ne 0); + $template->param( op => $op, batch_id => $batch_id, table_loop => $table, -); + ); output_html_with_http_headers $cgi, $cookie, $template->output; -- 2.20.1