[35/40] Work on C4::Labels tests and various bugfixs resulting
[srvgit] / C4 / Labels / Batch.pm
index da84552..d1bc677 100644 (file)
@@ -1,22 +1,5 @@
 package C4::Labels::Batch;
 
-# Copyright 2009 Foundations Bible College.
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
-#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
-
 use strict;
 use warnings;
 
@@ -24,7 +7,6 @@ use Sys::Syslog qw(syslog);
 
 use C4::Context;
 use C4::Debug;
-use Data::Dumper;
 
 BEGIN {
     use version; our $VERSION = qv('1.0.0_1');
@@ -57,32 +39,6 @@ sub _check_params {
     return $exit_code;
 }
 
-=head1 NAME
-
-C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
-
-=cut
-
-=head1 METHODS
-
-=head2 C4::Labels::Batch->new()
-
-    Invoking the I<new> method constructs a new batch object with no items. It is possible to pre-populate the batch with items and a branch code by passing them
-    as in the second example below.
-
-    B<NOTE:> The items list must be an arrayref pointing to an array of hashes containing a key/data pair after this fashion: {item_number => item_number}. The order of
-    the array elements determines the order of the items in the batch.
-
-    example:
-        C<my $batch = C4::Labels::Batch->new(); # Creates and returns a new batch object>
-
-        C<my $batch = C4::Labels::Batch->new(items => $arrayref, branch_code => branch_code) #    Creates and returns a new batch object containing the items passed in
-                                                                                                with the branch code passed in.>
-
-    B<NOTE:> This batch is I<not> written to the database until C<$batch->save()> is invoked. You have been warned!
-
-=cut
-
 sub new {
     my ($invocant) = shift;
     my $type = ref($invocant) || $invocant;
@@ -101,15 +57,6 @@ sub new {
     return $self;
 }
 
-=head2 $batch->add_item(item_number => $item_number, branch_code => $branch_code)
-
-    Invoking the I<add_item> method will add the supplied item to the batch object.
-
-    example:
-        $batch->add_item(item_number => $item_number, branch_code => $branch_code);
-
-=cut
-
 sub add_item {
     my $self = shift;
     my $item_number = shift;
@@ -126,33 +73,15 @@ sub add_item {
     $sth1->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'});
     my $label_id = $sth1->fetchrow_array;
     push (@{$self->{'items'}}, {item_number => $item_number, label_id => $label_id});
-    $self->{'batch_stat'} = 0;
+    $self->{'batch_stat'} = 1;
     return 0;
 }
 
-=head2 $batch->get_attr()
-
-    Invoking the I<get_attr> method will return the requested attribute.
-
-    example:
-        my @items = $batch->get_attr($attr);
-
-=cut
-
 sub get_attr {
     my $self = shift;
     return $self->{$_[0]};
 }
 
-=head2 $batch->remove_item()
-
-    Invoking the I<remove_item> method will remove the supplied item from the batch object.
-
-    example:
-        $batch->remove_item();
-
-=cut
-
 sub remove_item {
     my $self = shift;
     my $label_id = shift;
@@ -169,42 +98,35 @@ sub remove_item {
     return 0;
 }
 
-=head2 $batch->save()
-
-    Invoking the I<save> method attempts to insert the batch into the database. The method returns
-    the new record batch_id upon success and -1 upon failure (This avoids conflicting with a record
-    batch_id of 1). Errors are logged to the syslog.
-
-    example:
-        my $exitstat = $batch->save(); # to save the record behind the $batch object
-
-=cut
-
-sub save {
-    my $self = shift;
-    foreach my $item_number (@{$self->{'items'}}) {
-        my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
-        my $sth1 = C4::Context->dbh->prepare($query);
-        $sth1->execute($self->{'batch_id'}, $item_number->{'item_number'}, $self->{'branch_code'});
-        if ($sth1->err) {
-            syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth1->errstr);
-            return -1;
-        }
-        $self->{'batch_stat'} = 1;
-        return $self->{'batch_id'};
-    }
-}
-
-=head2 C4::Labels::Batch->retrieve(batch_id)
-
-    Invoking the I<retrieve> method constructs a new batch object containing the current values for batch_id. The method returns
-    a new object upon success and 1 upon failure. Errors are logged to the syslog.
-
-    examples:
-
-        my $batch = C4::Labels::Batch->retrieve(batch_id => 1); # Retrieves batch record 1 and returns an object containing the record
-
-=cut
+# FIXME: This method is effectively useless the way the current add_item method is written. Ideally, the items should be added to the object
+#       and then the save method called. This does not work well in practice due to the inability to pass objects accross cgi script calls.
+#       I'm leaving it here because it should be here and for consistency's sake. -cnighswonger
+#
+#=head2 $batch->save()
+#
+#    Invoking the I<save> method attempts to insert the batch into the database. The method returns
+#    the new record batch_id upon success and -1 upon failure (This avoids conflicting with a record
+#    batch_id of 1). Errors are logged to the syslog.
+#
+#    example:
+#        my $exitstat = $batch->save(); # to save the record behind the $batch object
+#
+#=cut
+#
+#sub save {
+#    my $self = shift;
+#    foreach my $item_number (@{$self->{'items'}}) {
+#        my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);";
+#        my $sth1 = C4::Context->dbh->prepare($query);
+#        $sth1->execute($self->{'batch_id'}, $item_number->{'item_number'}, $self->{'branch_code'});
+#        if ($sth1->err) {
+#            syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth1->errstr);
+#            return -1;
+#        }
+#        $self->{'batch_stat'} = 1;
+#        return $self->{'batch_id'};
+#    }
+#}
 
 sub retrieve {
     my $invocant = shift;
@@ -234,17 +156,6 @@ sub retrieve {
     return $self;
 }
 
-=head2 C4::Labels::Batch->delete(batch_id => batch_id) | $batch->delete()
-
-    Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
-    and 1 upon failure. Errors are logged to the syslog.
-
-    examples:
-        my $exitstat = $batch->delete(); # to delete the record behind the $batch object
-        my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
-
-=cut
-
 sub delete {
     my $self = {};
     my %opts = ();
@@ -275,16 +186,6 @@ 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=();
@@ -306,9 +207,103 @@ sub remove_duplicates {
 1;
 __END__
 
+=head1 NAME
+
+C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
+
+=head1 ABSTRACT
+
+This module provides methods for creating, and otherwise manipulating batch objects used by Koha to create and export labels.
+
+=head1 METHODS
+
+=head2 new()
+
+    Invoking the I<new> method constructs a new batch object with no items. It is possible to pre-populate the batch with items and a branch code by passing them
+    as in the second example below.
+
+    B<NOTE:> The items list must be an arrayref pointing to an array of hashes containing a key/data pair after this fashion: {item_number => item_number}. The order of
+    the array elements determines the order of the items in the batch.
+
+    example:
+        C<my $batch = C4::Labels::Batch->new(); # Creates and returns a new batch object>
+
+        C<my $batch = C4::Labels::Batch->new(items => $arrayref, branch_code => branch_code) #    Creates and returns a new batch object containing the items passed in
+            with the branch code passed in.>
+
+    B<NOTE:> This batch is I<not> written to the database until C<$batch->save()> is invoked. You have been warned!
+
+=head2 $batch->add_item(item_number => $item_number, branch_code => $branch_code)
+
+    Invoking the I<add_item> method will add the supplied item to the batch object.
+
+    example:
+        $batch->add_item(item_number => $item_number, branch_code => $branch_code);
+
+=head2 $batch->get_attr($attribute)
+
+    Invoking the I<get_attr> method will return the requested attribute.
+
+    example:
+        my @items = $batch->get_attr('items');
+
+=head2 $batch->remove_item($item_number)
+
+    Invoking the I<remove_item> method will remove the supplied item number from the batch object.
+
+    example:
+        $batch->remove_item($item_number);
+
+=head2 C4::Labels::Batch->retrieve(batch_id => $batch_id)
+
+    Invoking the I<retrieve> method constructs a new batch object containing the current values for batch_id. The method returns a new object upon success and 1 upon failure.
+    Errors are logged to the syslog.
+
+    examples:
+
+        my $batch = C4::Labels::Batch->retrieve(batch_id => 1); # Retrieves batch 1 and returns an object containing the record
+
+=head2 delete()
+
+    Invoking the delete method attempts to delete the template from the database. The method returns -1 upon failure. Errors are logged to the syslog.
+    NOTE: This method may also be called as a function and passed a key/value pair simply deleteing that batch from the database. See the example below.
+
+    examples:
+        my $exitstat = $batch->delete(); # to delete the record behind the $batch object
+        my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch 1
+
+=head2 remove_duplicates()
+
+    Invoking the remove_duplicates method attempts to remove duplicate items in 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.
+    NOTE: This method may also be called as a function and passed a key/value pair removing duplicates in the batch passed in. See the example below.
+
+    examples:
+        my $remove_count = $batch->remove_duplicates(); # to remove duplicates the record behind the $batch object
+        my $remove_count = C4::Labels::Batch->remove_duplicates(batch_id => 1); # to remove duplicates in batch 1
+
 =head1 AUTHOR
 
 Chris Nighswonger <cnighswonger AT foundations DOT edu>
 
+=head1 COPYRIGHT
+
+Copyright 2009 Foundations Bible College.
+
+=head1 LICENSE
+
+This file is part of Koha.
+       
+Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
+Foundation; either version 2 of the License, or (at your option) any later version.
+
+You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+Suite 330, Boston, MA  02111-1307 USA
+
+=head1 DISCLAIMER OF WARRANTY
+
+Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
 =cut