[21/40] Adding de-duplicating method and associated label edit batch code.
authorChris Nighswonger <cnighswonger@foundations.edu>
Tue, 1 Sep 2009 18:05:52 +0000 (14:05 -0400)
committerChris Nighswonger <cnighswonger@foundations.edu>
Tue, 1 Sep 2009 19:51:26 +0000 (15:51 -0400)
C4/Labels/Batch.pm
koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css
koha-tmpl/intranet-tmpl/prog/en/includes/labels-batches-toolbar.inc
koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tmpl
labels/label-edit-batch.pl

index d7d483a..05a6a2a 100644 (file)
@@ -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__
index 145c2bf..5324c70 100644 (file)
@@ -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;
index 38d92e3..48c317a 100644 (file)
@@ -30,7 +30,7 @@
         <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&amp;label_element=batch&amp;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&amp;batch_id=<!-- TMPL_VAR NAME="batch_id" -->&amp;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&amp;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" -->&amp;type=<!-- TMPL_VAR NAME="batch_type" -->">Generate labels for Batch</a></li>
     </ul>
 </div>
index 514176c..2859c4a 100644 (file)
                         </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>
index 0a9877b..1eae99b 100755 (executable)
@@ -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;