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 &AddToShelfFromBiblio
57 &RemoveFromShelf &AddShelf &RemoveShelf
58 &ShelfPossibleAction);
60 my $dbh = C4::Context->dbh;
62 =item ShelfPossibleAction
66 =item C<$loggedinuser,$shelfnumber,$action>
68 $action can be "view" or "manage".
70 Returns 1 if the user can do the $action in the $shelfnumber shelf.
76 sub ShelfPossibleAction {
77 my ($loggedinuser,$shelfnumber,$action)= @_;
78 my $sth = $dbh->prepare("select owner,category from bookshelf where shelfnumber=?");
79 $sth->execute($shelfnumber);
80 my ($owner,$category) = $sth->fetchrow;
81 return 1 if (($category>=3 or $owner eq $loggedinuser) && $action eq 'manage');
82 return 1 if (($category>= 2 or $owner eq $loggedinuser) && $action eq 'view');
88 $shelflist = &GetShelfList();
89 ($shelfnumber, $shelfhash) = each %{$shelflist};
91 Looks up the virtual bookshelves, and returns a summary. C<$shelflist>
92 is a reference-to-hash. The keys are the bookshelf numbers
93 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
94 themselves references-to-hash, with the following keys:
98 =item C<$shelfhash-E<gt>{shelfname}>
100 A string. The name of the shelf.
102 =item C<$shelfhash-E<gt>{count}>
104 The number of books on that bookshelf.
110 # FIXME - Wouldn't it be more intuitive to return a list, rather than
111 # a reference-to-hash? The shelf number can be just another key in the
114 my ($owner,$mincategory) = @_;
115 # mincategory : 2 if the list is for "look". 3 if the list is for "Select bookshelf for adding a book".
116 # bookshelves of the owner are always selected, whatever the category
117 my $sth=$dbh->prepare("SELECT bookshelf.shelfnumber, bookshelf.shelfname,owner,surname,firstname,
118 count(shelfcontents.itemnumber) as count
120 LEFT JOIN shelfcontents
121 ON bookshelf.shelfnumber = shelfcontents.shelfnumber
122 left join borrowers on bookshelf.owner = borrowers.borrowernumber
123 where owner=? or category>=?
124 GROUP BY bookshelf.shelfnumber order by shelfname");
125 $sth->execute($owner,$mincategory);
127 while (my ($shelfnumber, $shelfname,$owner,$surname,$firstname,$count) = $sth->fetchrow) {
128 $shelflist{$shelfnumber}->{'shelfname'}=$shelfname;
129 $shelflist{$shelfnumber}->{'count'}=$count;
130 $shelflist{$shelfnumber}->{'owner'}=$owner;
131 $shelflist{$shelfnumber}->{surname} = $surname;
132 $shelflist{$shelfnumber}->{firstname} = $firstname;
137 =item GetShelfContents
139 $itemlist = &GetShelfContents($env, $shelfnumber);
141 Looks up information about the contents of virtual bookshelf number
144 Returns a reference-to-array, whose elements are references-to-hash,
145 as returned by C<&getiteminformation>.
147 I don't know what C<$env> is.
151 sub GetShelfContents {
152 my ($env, $shelfnumber) = @_;
154 my $sth=$dbh->prepare("select itemnumber from shelfcontents where shelfnumber=? order by itemnumber");
155 $sth->execute($shelfnumber);
156 while (my ($itemnumber) = $sth->fetchrow) {
157 my ($item) = getiteminformation($env, $itemnumber, 0);
158 push (@itemlist, $item);
165 &AddToShelf($env, $itemnumber, $shelfnumber);
167 Adds item number C<$itemnumber> to virtual bookshelf number
168 C<$shelfnumber>, unless that item is already on that shelf.
175 my ($env, $itemnumber, $shelfnumber) = @_;
176 return unless $itemnumber;
177 my $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=? and itemnumber=?");
179 $sth->execute($shelfnumber, $itemnumber);
183 $sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values (?, ?, 0)");
184 $sth->execute($shelfnumber, $itemnumber);
187 sub AddToShelfFromBiblio {
188 my ($env, $biblionumber, $shelfnumber) = @_;
189 return unless $biblionumber;
190 my $sth = $dbh->prepare("select itemnumber from items where biblionumber=?");
191 $sth->execute($biblionumber);
192 my ($itemnumber) = $sth->fetchrow;
193 $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=? and itemnumber=?");
194 $sth->execute($shelfnumber, $itemnumber);
198 $sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values (?, ?, 0)");
199 $sth->execute($shelfnumber, $itemnumber);
203 =item RemoveFromShelf
205 &RemoveFromShelf($env, $itemnumber, $shelfnumber);
207 Removes item number C<$itemnumber> from virtual bookshelf number
208 C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
215 sub RemoveFromShelf {
216 my ($env, $itemnumber, $shelfnumber) = @_;
217 my $sth=$dbh->prepare("delete from shelfcontents where shelfnumber=? and itemnumber=?");
218 $sth->execute($shelfnumber,$itemnumber);
223 ($status, $msg) = &AddShelf($env, $shelfname);
225 Creates a new virtual bookshelf with name C<$shelfname>.
227 Returns a two-element array, where C<$status> is 0 if the operation
228 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
229 success, or an error message giving the reason for failure.
235 # FIXME - Perhaps this could/should return the number of the new bookshelf
238 my ($env, $shelfname,$owner,$category) = @_;
239 my $sth=$dbh->prepare("select * from bookshelf where shelfname=?");
240 $sth->execute($shelfname);
242 return(1, "Shelf \"$shelfname\" already exists");
244 $sth=$dbh->prepare("insert into bookshelf (shelfname,owner,category) values (?,?,?)");
245 $sth->execute($shelfname,$owner,$category);
252 ($status, $msg) = &RemoveShelf($env, $shelfnumber);
254 Deletes virtual bookshelf number C<$shelfnumber>. The bookshelf must
257 Returns a two-element array, where C<$status> is 0 if the operation
258 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
259 success, or an error message giving the reason for failure.
266 my ($env, $shelfnumber) = @_;
267 my $sth=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=?");
268 $sth->execute($shelfnumber);
269 my ($count)=$sth->fetchrow;
271 return (1, "Shelf has $count items on it. Please remove all items before deleting this shelf.");
273 $sth=$dbh->prepare("delete from bookshelf where shelfnumber=?");
274 $sth->execute($shelfnumber);
279 END { } # module clean-up code here (global destructor)
285 # Revision 1.13 2004/03/11 16:06:20 tipaul
286 # *** empty log message ***
288 # Revision 1.11.2.2 2004/02/19 10:15:41 tipaul
289 # new feature : adding book to bookshelf from biblio detail screen.
291 # Revision 1.11.2.1 2004/02/06 14:16:55 tipaul
292 # fixing bugs in bookshelves management.
294 # Revision 1.11 2003/12/15 10:57:08 slef
295 # DBI call fix for bug 662
297 # Revision 1.10 2003/02/05 10:05:02 acli
298 # Converted a few SQL statements to use ? to fix a few strange SQL errors
299 # Noted correct tab size
301 # Revision 1.9 2002/10/13 08:29:18 arensb
302 # Deleted unused variables.
303 # Removed trailing whitespace.
305 # Revision 1.8 2002/10/10 04:32:44 arensb
306 # Simplified references.
308 # Revision 1.7 2002/10/05 09:50:10 arensb
309 # Merged with arensb-context branch: use C4::Context->dbh instead of
310 # &C4Connect, and generally prefer C4::Context over C4::Database.
312 # Revision 1.6.2.1 2002/10/04 02:24:43 arensb
313 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
316 # Revision 1.6 2002/09/23 13:50:30 arensb
317 # Fixed missing bit in POD.
319 # Revision 1.5 2002/09/22 17:29:17 arensb
321 # Added some FIXME comments.
322 # Removed useless trailing whitespace.
324 # Revision 1.4 2002/08/14 18:12:51 tonnesen
325 # Added copyright statement to all .pl and .pm files
327 # Revision 1.3 2002/07/02 17:48:06 tonnesen
328 # Merged in updates from rel-1-2
330 # Revision 1.2.2.1 2002/06/26 20:46:48 tonnesen
331 # Inserting some changes I made locally a while ago.
341 Koha Developement team <info@koha.org>
345 C4::Circulation::Circ2(3)