2 # Please use 8-character tabs for this file (indents are every 4 characters)
4 package C4::BookShelves;
8 # Copyright 2000-2002 Katipo Communications
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
29 use C4::Circulation::Circ2;
30 use vars qw($VERSION @ISA @EXPORT);
32 # set the version for version checking
37 C4::BookShelves - Functions for manipulating Koha virtual bookshelves
45 This module provides functions for manipulating virtual bookshelves,
46 including creating and deleting bookshelves, and adding and removing
47 items to and from bookshelves.
56 @EXPORT = qw(&GetShelfList &GetShelfContents &AddToShelf &RemoveFromShelf &AddShelf &RemoveShelf);
58 my $dbh = C4::Context->dbh;
62 $shelflist = &GetShelfList();
63 ($shelfnumber, $shelfhash) = each %{$shelflist};
65 Looks up the virtual bookshelves, and returns a summary. C<$shelflist>
66 is a reference-to-hash. The keys are the bookshelf numbers
67 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
68 themselves references-to-hash, with the following keys:
72 =item C<$shelfhash-E<gt>{shelfname}>
74 A string. The name of the shelf.
76 =item C<$shelfhash-E<gt>{count}>
78 The number of books on that bookshelf.
84 # FIXME - Wouldn't it be more intuitive to return a list, rather than
85 # a reference-to-hash? The shelf number can be just another key in the
88 # FIXME - These two database queries can be combined into one:
89 # SELECT bookshelf.shelfnumber, bookshelf.shelfname,
90 # count(shelfcontents.itemnumber)
92 # LEFT JOIN shelfcontents
93 # ON bookshelf.shelfnumber = shelfcontents.shelfnumber
94 # GROUP BY bookshelf.shelfnumber
95 my $sth=$dbh->prepare("select shelfnumber,shelfname from bookshelf");
98 while (my ($shelfnumber, $shelfname) = $sth->fetchrow) {
99 my $sti=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=?");
100 # FIXME - Should there be an "order by" in here somewhere?
101 $sti->execute($shelfnumber);
102 my ($count) = $sti->fetchrow;
103 $shelflist{$shelfnumber}->{'shelfname'}=$shelfname;
104 $shelflist{$shelfnumber}->{'count'}=$count;
109 =item GetShelfContents
111 $itemlist = &GetShelfContents($env, $shelfnumber);
113 Looks up information about the contents of virtual bookshelf number
116 Returns a reference-to-array, whose elements are references-to-hash,
117 as returned by C<&getiteminformation>.
119 I don't know what C<$env> is.
123 sub GetShelfContents {
124 my ($env, $shelfnumber) = @_;
126 my $sth=$dbh->prepare("select itemnumber from shelfcontents where shelfnumber=? order by itemnumber");
127 $sth->execute($shelfnumber);
128 while (my ($itemnumber) = $sth->fetchrow) {
129 my ($item) = getiteminformation($env, $itemnumber, 0);
130 push (@itemlist, $item);
137 &AddToShelf($env, $itemnumber, $shelfnumber);
139 Adds item number C<$itemnumber> to virtual bookshelf number
140 C<$shelfnumber>, unless that item is already on that shelf.
147 my ($env, $itemnumber, $shelfnumber) = @_;
148 my $sth=$dbh->prepare("select * from shelfcontents
149 where shelfnumber=? and itemnumber=?");
151 $sth->execute($shelfnumber, $itemnumber);
155 $sth=$dbh->prepare("insert into shelfcontents
156 (shelfnumber, itemnumber, flags) values (?, ?, 0)");
158 # FIXME - The default for 'flags' is NULL.
160 $sth->execute($shelfnumber, $itemnumber);
164 =item RemoveFromShelf
166 &RemoveFromShelf($env, $itemnumber, $shelfnumber);
168 Removes item number C<$itemnumber> from virtual bookshelf number
169 C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
176 sub RemoveFromShelf {
177 my ($env, $itemnumber, $shelfnumber) = @_;
178 my $sth=$dbh->prepare("delete from shelfcontents where shelfnumber=? and itemnumber=?");
179 $sth->execute($shelfnumber,$itemnumber);
184 ($status, $msg) = &AddShelf($env, $shelfname);
186 Creates a new virtual bookshelf with name C<$shelfname>.
188 Returns a two-element array, where C<$status> is 0 if the operation
189 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
190 success, or an error message giving the reason for failure.
196 # FIXME - Perhaps this could/should return the number of the new bookshelf
199 my ($env, $shelfname) = @_;
200 my $sth=$dbh->prepare("select * from bookshelf where shelfname=?");
201 $sth->execute($shelfname);
203 return(1, "Shelf \"$shelfname\" already exists");
205 $sth=$dbh->prepare("insert into bookshelf (shelfname) values (?)");
206 $sth->execute($shelfname);
213 ($status, $msg) = &RemoveShelf($env, $shelfnumber);
215 Deletes virtual bookshelf number C<$shelfnumber>. The bookshelf must
218 Returns a two-element array, where C<$status> is 0 if the operation
219 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
220 success, or an error message giving the reason for failure.
227 my ($env, $shelfnumber) = @_;
228 my $sth=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=?");
229 $sth->execute($shelfnumber);
230 my ($count)=$sth->fetchrow;
232 return (1, "Shelf has $count items on it. Please remove all items before deleting this shelf.");
234 $sth=$dbh->prepare("delete from bookshelf where shelfnumber=?");
235 $sth->execute($shelfnumber);
240 END { } # module clean-up code here (global destructor)
246 # Revision 1.11 2003/12/15 10:57:08 slef
247 # DBI call fix for bug 662
249 # Revision 1.10 2003/02/05 10:05:02 acli
250 # Converted a few SQL statements to use ? to fix a few strange SQL errors
251 # Noted correct tab size
253 # Revision 1.9 2002/10/13 08:29:18 arensb
254 # Deleted unused variables.
255 # Removed trailing whitespace.
257 # Revision 1.8 2002/10/10 04:32:44 arensb
258 # Simplified references.
260 # Revision 1.7 2002/10/05 09:50:10 arensb
261 # Merged with arensb-context branch: use C4::Context->dbh instead of
262 # &C4Connect, and generally prefer C4::Context over C4::Database.
264 # Revision 1.6.2.1 2002/10/04 02:24:43 arensb
265 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
268 # Revision 1.6 2002/09/23 13:50:30 arensb
269 # Fixed missing bit in POD.
271 # Revision 1.5 2002/09/22 17:29:17 arensb
273 # Added some FIXME comments.
274 # Removed useless trailing whitespace.
276 # Revision 1.4 2002/08/14 18:12:51 tonnesen
277 # Added copyright statement to all .pl and .pm files
279 # Revision 1.3 2002/07/02 17:48:06 tonnesen
280 # Merged in updates from rel-1-2
282 # Revision 1.2.2.1 2002/06/26 20:46:48 tonnesen
283 # Inserting some changes I made locally a while ago.
293 Koha Developement team <info@koha.org>
297 C4::Circulation::Circ2(3)