Added some more functionality to Shelf.pm (including the ability to actually
authortonnesen <tonnesen>
Wed, 23 Oct 2002 23:32:47 +0000 (23:32 +0000)
committertonnesen <tonnesen>
Wed, 23 Oct 2002 23:32:47 +0000 (23:32 +0000)
create a new shelf) and started a test script (Shelf.t)

C4/Shelf.pm
t/Shelf.t [new file with mode: 0644]

index a2f8cd2..2f1bd6b 100644 (file)
@@ -78,6 +78,9 @@ Base constructor for the class.
   my $shelf=Shelf->new('Favourite Books', 'sjohnson');
   my $shelf=Shelf->new(-name => 'Favourite Books', -owner => 'sjohnson');
       will load sjohnson's "Favourite Books" bookshelf
+  
+  Any of the last four invocations will create a new shelf with the name and
+  owner given if one doesn't already exist.
 
 
 =cut
@@ -94,11 +97,11 @@ sub new {
     $self->{CACHE}=new Cache::FileCache( { 'namespace' => 'KohaShelves' } );
 
     if (@_) {
+       my $dbh=C4::Context->dbh();
        shift;
        if ($#_ == 0) {
            $self->{ID}=shift;
            # load attributes of shelf #ID
-           my $dbh=C4::Context->dbh();
            my $sth;
            $sth=$dbh->prepare("select bookshelfname,bookshelfowner from bookshelves where bookshelfid=?");
            $sth->execute($self->{ID});
@@ -108,7 +111,7 @@ sub new {
            while (my ($attribute,$value) = $sth->fetchrow) {
                $self->{ATTRIBUTES}->{$attribute}=$value;
            }
-       } else {
+       } elsif ($#_) {
            my ($name,$owner,$attributes);
            if ($_[0] =~/^-/) {
                my %params=@_;
@@ -120,6 +123,26 @@ sub new {
                $owner=shift;
                $attributes=shift;
            }
+           my $sth=$dbh->prepare("select bookshelfid from bookshelves where bookshelfname=? and bookshelfowner=?");
+           $sth->execute($name, $owner);
+           if ($sth->rows) {
+               ($self->{ID})=$sth->fetchrow;
+               $sth=$dbh->prepare("select attribute,value from bookshelfattributes where bookshelfid=?");
+               $sth->execute($self->{ID});
+               while (my ($attribute,$value) = $sth->fetchrow) {
+                   $self->{ATTRIBUTES}->{$attribute}=$value;
+               }
+           } else {
+               $sth=$dbh->prepare("insert into bookshelves (bookshelfname, bookshelfowner) values (?, ?)");
+               $sth->execute($name,$owner);
+               $sth=$dbh->prepare("select bookshelfid from bookshelves where bookshelfname=? and bookshelfowner=?");
+               $sth->execute($name,$owner);
+               ($self->{ID})=$sth->fetchrow();
+               foreach my $attribute (keys %$attributes) {
+                   my $value=$attributes->{$attribute};
+                   $self->attribute($attribute,$value);
+               }
+           }
        }
     }
     bless($self);
@@ -228,7 +251,26 @@ sub shelfcontents {
     my $self=shift;
 }
 
-sub clearshelf {
+
+=head2 C<clearcontents()>
+
+Removes all contents from the shelf.
+
+    $shelf->clearcontents();
+
+=cut
+
+sub clearcontents {
+    my $self=shift;
+    my $dbh=C4::Context->dbh();
+    my $sth=$dbh->prepare("delete from bookshelfcontents where bookshelfid=?");
+    $sth->execute($self->{ID});
+    foreach my $level ('ITEM', 'BIBLIOITEM', 'BIBLIO') {
+       delete $self->{$level."CONTENTS"};
+       $self->{$level."CONTENTS"}={};
+    }
+    $self->clearcache();
+
 }
 
 
@@ -277,15 +319,32 @@ sub removefromshelf {
 
 =head2 C<attribute()>
 
-Returns the value of a given attribute for the shelf.
+Returns or sets the value of a given attribute for the shelf.
 
   my $loanlength=$shelf->attribute('loanlength');
+  $shelf->attribute('loanlength', '21 days');
+
 
 =cut
 
 sub attribute {
     my $self=shift;
-    my $attribute=shift;
+    my ($attribute, $value);
+    $attribute=shift;
+    $value=shift;
+    if ($value) {
+       $self->{ATTRIBUTES}->{$attribute}=$value;
+       my $dbh=C4::Context->dbh();
+       my $sth=$dbh->prepare("select value from bookshelfattributes where bookshelfid=? and attribute=?");
+       $sth->execute($self->{ID}, $attribute);
+       if ($sth->rows) {
+           my $sti=$dbh->prepare("update bookshelfattributes set value=? where bookshelfid=? and attribute=?");
+           $sti->execute($value, $self->{ID}, $attribute);
+       } else {
+           my $sti=$dbh->prepare("inesrt into bookshelfattributes (bookshelfid, attribute, value) values (?, ?, ?)");
+           $sti->execute($self->{ID}, $attribute, $value);
+       }
+    }
     return $self->{ATTRIBUTES}->{$attribute};
 }
 
diff --git a/t/Shelf.t b/t/Shelf.t
new file mode 100644 (file)
index 0000000..8a14b9b
--- /dev/null
+++ b/t/Shelf.t
@@ -0,0 +1,47 @@
+BEGIN { $| = 1; print "1..25\n"; }
+END {print "not ok 1\n" unless $loaded;}
+use C4::Shelf;
+use C4::Context;
+$loaded = 1;
+print "ok 1\n";
+
+
+
+# Load some sample data from the items table
+my $itemdata;
+my $dbh=C4::Context->dbh();
+my $sth=$dbh->prepare("select biblionumber,biblioitemnumber,itemnumber from items limit 30");
+$sth->execute;
+while (my ($biblionumber, $biblioitemnumber, $itemnumber) = $sth->fetchrow) {
+    push @$itemdata, { biblionumber=>$biblionumber, biblioitemnumber=>$biblioitemnumber, itemnumber=>$itemnumber };
+}
+
+if ($itemdata=~/^ARRAY/) {
+    print "ok 2\n";
+} else {
+    print "not ok 2\n";
+}
+
+
+
+# Create a couple of new shelves
+
+my $shelf=Shelf->new('Shelf1', -1);
+my $shelf2=Shelf->new('Shelf2', -1);
+
+if (defined $shelf) {
+    print "ok 3\n";
+} else {
+    print "not ok 3\n";
+}
+
+
+# Add an item to a shelf
+
+
+for ($i=1; $i<20; $i++) {
+    $shelf->addtoshelf( -add => [[ $$itemdata[$i]->{biblionumber},
+                                  $$itemdata[$i]->{biblioitemnumber},
+                                  $$itemdata[$i]->{itemnumber} ]]);
+}
+