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
$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});
while (my ($attribute,$value) = $sth->fetchrow) {
$self->{ATTRIBUTES}->{$attribute}=$value;
}
- } else {
+ } elsif ($#_) {
my ($name,$owner,$attributes);
if ($_[0] =~/^-/) {
my %params=@_;
$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);
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();
+
}
=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};
}
--- /dev/null
+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} ]]);
+}
+