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__
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;
<li id="additemsc"><a id="additems" href="#">Add item(s) to batch</a></li>
<li><a id="deletebatch" href="/cgi-bin/koha/labels/label-manage.pl?op=delete&label_element=batch&element_id=<!-- TMPL_VAR NAME="batch_id" -->">Delete current batch</a></li>
<!-- FIXME: should use POST to change server state, not GET -->
- <li><a id="dedup" href="/cgi-bin/koha/labels/label-manager.pl?op=deduplicate&batch_id=<!-- TMPL_VAR NAME="batch_id" -->&type=<!-- TMPL_VAR NAME="batch_type" -->">Remove duplicates</a></li>
+ <li><a id="dedup" href="/cgi-bin/koha/labels/label-edit-batch.pl?op=de_duplicate&batch_id=<!-- TMPL_VAR NAME="batch_id" -->">Remove duplicate items</a></li>
<li><a id="generate" href="/cgi-bin/koha/labels/label-print-<!-- TMPL_VAR NAME="outputformat" -->.pl?batch_id=<!-- TMPL_VAR NAME="batch_id" -->&type=<!-- TMPL_VAR NAME="batch_type" -->">Generate labels for Batch</a></li>
</ul>
</div>
</div>
<!-- TMPL_IF NAME="err" -->
<div class="yui-u">
- <div class="dialog alert">
+ <div class="alert">
<strong>WARNING: An error was encountered and <!-- TMPL_VAR NAME="errstr" --> Please have your system administrator check the syslog for details.</strong>
</div>
</div>
<!-- /TMPL_IF -->
+ <!-- TMPL_IF NAME="duplicate_message" -->
+ <div class="yui-u">
+ <div class="dialog">
+ <strong><!-- TMPL_VAR NAME="duplicate_count" --> duplicate item(s) found and removed from batch <!-- TMPL_VAR NAME="batch_id" -->.</strong>
+ </div>
+ </div>
+ <!-- /TMPL_IF -->
</div>
</div>
</div>
);
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}},
$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);
}
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;