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 vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
32 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
36 C4::BookShelves - Functions for manipulating Koha virtual bookshelves
44 This module provides functions for manipulating virtual bookshelves,
45 including creating and deleting bookshelves, and adding and removing
46 items to and from bookshelves.
56 &GetShelves &GetShelfContents &GetShelf
58 &AddToShelf &AddToShelfFromBiblio &AddShelf
62 &DelFromShelf &DelShelf
65 my $dbh = C4::Context->dbh;
69 $shelflist = &GetShelves($owner, $mincategory);
70 ($shelfnumber, $shelfhash) = each %{$shelflist};
72 Looks up the virtual bookshelves, and returns a summary. C<$shelflist>
73 is a reference-to-hash. The keys are the bookshelf numbers
74 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
75 themselves references-to-hash, with the following keys:
77 C<mincategory> : 2 if the list is for "look". 3 if the list is for "Select bookshelf for adding a book".
78 bookshelves of the owner are always selected, whatever the category
82 =item C<$shelfhash-E<gt>{shelfname}>
84 A string. The name of the shelf.
86 =item C<$shelfhash-E<gt>{count}>
88 The number of books on that bookshelf.
95 # FIXME - Wouldn't it be more intuitive to return a list, rather than
96 # a reference-to-hash? The shelf number can be just another key in the
100 my ( $owner, $mincategory ) = @_;
103 SELECT bookshelf.shelfnumber, bookshelf.shelfname,owner,surname,firstname,bookshelf.category,
104 count(shelfcontents.itemnumber) as count
106 LEFT JOIN shelfcontents ON bookshelf.shelfnumber = shelfcontents.shelfnumber
107 LEFT JOIN borrowers ON bookshelf.owner = borrowers.borrowernumber
108 WHERE owner=? OR category>=?
109 GROUP BY bookshelf.shelfnumber
110 ORDER BY bookshelf.category, bookshelf.shelfname, borrowers.firstname, borrowers.surname
112 my $sth = $dbh->prepare($query);
113 $sth->execute( $owner, $mincategory );
117 $shelfnumber, $shelfname, $owner, $surname,
118 $firstname, $category, $count
123 $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
124 $shelflist{$shelfnumber}->{'count'} = $count;
125 $shelflist{$shelfnumber}->{'category'} = $category;
126 $shelflist{$shelfnumber}->{'owner'} = $owner;
127 $shelflist{$shelfnumber}->{'surname'} = $surname;
128 $shelflist{$shelfnumber}->{'firstname'} = $firstname;
130 return ( \%shelflist );
135 (shelfnumber,shelfname,owner,category) = &GetShelf($shelfnumber);
137 Looks up information about the contents of virtual bookshelf number
140 Returns the database's information on 'bookshelf' table.
145 my ($shelfnumber) = @_;
147 SELECT shelfnumber,shelfname,owner,category
151 my $sth = $dbh->prepare($query);
152 $sth->execute($shelfnumber);
153 return $sth->fetchrow;
156 =item GetShelfContents
158 $itemlist = &GetShelfContents($shelfnumber);
160 Looks up information about the contents of virtual bookshelf number
163 Returns a reference-to-array, whose elements are references-to-hash,
164 as returned by C<C4::Biblio::GetBiblioFromItemNumber>.
169 sub GetShelfContents {
170 my ( $shelfnumber ) = @_;
178 my $sth = $dbh->prepare($query);
179 $sth->execute($shelfnumber);
180 my $sth2 = $dbh->prepare("
181 SELECT biblio.*,biblioitems.* FROM items
182 LEFT JOIN biblio on items.biblionumber=biblio.biblionumber
183 LEFT JOIN biblioitems on items.biblionumber=biblioitems.biblionumber
184 WHERE items.itemnumber=?"
186 while ( my ($itemnumber) = $sth->fetchrow ) {
187 $sth2->execute($itemnumber);
188 my $item = $sth2->fetchrow_hashref;
189 $item->{'itemnumber'}=$itemnumber;
190 push( @itemlist, $item );
192 return ( \@itemlist );
197 $shelfnumber = &AddShelf( $shelfname, $owner, $category);
199 Creates a new virtual bookshelf with name C<$shelfname>, owner C<$owner> and category
202 Returns a code to know what's happen.
203 * -1 : if this bookshelf already exist.
204 * $shelfnumber : if success.
209 my ( $shelfname, $owner, $category ) = @_;
213 WHERE shelfname=? AND owner=?
215 my $sth = $dbh->prepare($query);
216 $sth->execute($shelfname,$owner);
222 INSERT INTO bookshelf
223 (shelfname,owner,category)
226 $sth = $dbh->prepare($query);
227 $sth->execute( $shelfname, $owner, $category );
228 my $shelfnumber = $dbh->{'mysql_insertid'};
229 return ($shelfnumber);
235 &AddToShelf($itemnumber, $shelfnumber);
237 Adds item number C<$itemnumber> to virtual bookshelf number
238 C<$shelfnumber>, unless that item is already on that shelf.
244 my ( $itemnumber, $shelfnumber ) = @_;
245 return unless $itemnumber;
249 WHERE shelfnumber=? AND itemnumber=?
251 my $sth = $dbh->prepare($query);
253 $sth->execute( $shelfnumber, $itemnumber );
254 unless ( $sth->rows ) {
257 INSERT INTO shelfcontents
258 (shelfnumber, itemnumber, flags)
262 $sth = $dbh->prepare($query);
263 $sth->execute( $shelfnumber, $itemnumber );
267 =item AddToShelfFromBiblio
269 &AddToShelfFromBiblio($biblionumber, $shelfnumber)
271 this function allow to add a book into the shelf number $shelfnumber
276 sub AddToShelfFromBiblio {
277 my ( $biblionumber, $shelfnumber ) = @_;
278 return unless $biblionumber;
284 my $sth = $dbh->prepare($query);
285 $sth->execute($biblionumber);
286 my ($itemnumber) = $sth->fetchrow;
290 WHERE shelfnumber=? AND itemnumber=?
292 $sth = $dbh->prepare($query);
293 $sth->execute( $shelfnumber, $itemnumber );
294 unless ( $sth->rows ) {
295 # "already on shelf";
297 INSERT INTO shelfcontents
298 (shelfnumber, itemnumber, flags)
302 $sth = $dbh->prepare($query);
303 $sth->execute( $shelfnumber, $itemnumber );
309 ModShelf($shelfnumber, $shelfname, $owner, $category )
311 Modify the value into bookshelf table with values given on input arg.
316 my ( $shelfnumber, $shelfname, $owner, $category ) = @_;
319 SET shelfname=?,owner=?,category=?
322 my $sth = $dbh->prepare($query);
323 $sth->execute( $shelfname, $owner, $category, $shelfnumber );
328 ($status) = &DelShelf($shelfnumber);
330 Deletes virtual bookshelf number C<$shelfnumber>. The bookshelf must
333 Returns a two-element array, where C<$status> is 0 if the operation
334 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
335 success, or an error message giving the reason for failure.
340 =item ShelfPossibleAction
342 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
344 C<$loggedinuser,$shelfnumber,$action>
346 $action can be "view" or "manage".
348 Returns 1 if the user can do the $action in the $shelfnumber shelf.
353 sub ShelfPossibleAction {
354 my ( $user, $shelfnumber, $action ) = @_;
356 SELECT owner,category
360 my $sth = $dbh->prepare($query);
361 $sth->execute($shelfnumber);
362 my ( $owner, $category ) = $sth->fetchrow;
363 return 1 if (($category >= 3 or $owner eq $user) && $action eq 'manage' );
364 return 1 if (($category >= 2 or $owner eq $user) && $action eq 'view' );
370 &DelFromShelf( $itemnumber, $shelfnumber);
372 Removes item number C<$itemnumber> from virtual bookshelf number
373 C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
380 my ( $itemnumber, $shelfnumber ) = @_;
382 DELETE FROM shelfcontents
383 WHERE shelfnumber=? AND itemnumber=?
385 my $sth = $dbh->prepare($query);
386 $sth->execute( $shelfnumber, $itemnumber );
391 $Number = DelShelf($shelfnumber);
393 this function delete the shelf number, and all of it's content
399 my ( $shelfnumber ) = @_;
400 my $sth = $dbh->prepare("DELETE FROM bookshelf WHERE shelfnumber=?");
401 $sth->execute($shelfnumber);
405 END { } # module clean-up code here (global destructor)
415 Koha Developement team <info@koha.org>
419 C4::Circulation::Circ2(3)
425 # Revision 1.21 2007/04/04 16:46:22 tipaul
426 # HUGE COMMIT : code cleaning circulation.
428 # some stuff to do, i'll write a mail on koha-devel NOW !
430 # Revision 1.20 2007/03/09 14:31:47 tipaul
431 # rel_3_0 moved to HEAD
433 # Revision 1.15.8.10 2007/01/25 13:18:15 tipaul
434 # checking that a bookshelf with the same name AND OWNER does not exist before creating it
436 # Revision 1.15.8.9 2006/12/15 17:37:52 toins
437 # removing a function used only once.
439 # Revision 1.15.8.8 2006/12/14 17:22:55 toins
440 # bookshelves work perfectly with mod_perl and are cleaned.
442 # Revision 1.15.8.7 2006/12/13 19:46:41 hdl
445 # Revision 1.15.8.6 2006/12/11 17:10:06 toins
446 # fixing some bugs on bookshelves.
448 # Revision 1.15.8.5 2006/12/07 16:45:43 toins
449 # removing warn compilation. (perl -wc)
451 # Revision 1.15.8.4 2006/11/23 09:05:01 tipaul
452 # enable removal of a bookshelf even if there are items inside
454 # Revision 1.15.8.3 2006/10/30 09:50:20 tipaul
455 # removing getiteminformations (using direct SQL, as we are in a .pm, so it's "legal")
457 # Revision 1.15.8.2 2006/08/31 16:03:52 toins
458 # Add Pod to DelShelf
460 # Revision 1.15.8.1 2006/08/30 15:59:14 toins
461 # Code cleaned according to coding guide lines.
463 # Revision 1.15 2004/12/16 11:30:58 tipaul
464 # adding bookshelf features :
465 # * create bookshelf on the fly
466 # * modify a bookshelf name & status
468 # Revision 1.14 2004/12/15 17:28:23 tipaul
469 # adding bookshelf features :
470 # * create bookshelf on the fly
471 # * modify a bookshelf (this being not finished, will commit the rest soon)