2 # Please use 8-character tabs for this file (indents are every 4 characters)
4 package C4::VirtualShelves;
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA 02111-1307 USA
27 use vars qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
35 &GetShelves &GetShelfContents &GetShelf
37 &AddToShelf &AddToShelfFromBiblio &AddShelf
41 &DelFromShelf &DelShelf
45 my $dbh = C4::Context->dbh;
49 C4::VirtualShelves - Functions for manipulating Koha virtual virtualshelves
53 use C4::VirtualShelves;
57 This module provides functions for manipulating virtual virtualshelves,
58 including creating and deleting virtualshelves, and adding and removing
59 items to and from virtualshelves.
67 $shelflist = &GetShelves($owner, $mincategory);
68 ($shelfnumber, $shelfhash) = each %{$shelflist};
70 Looks up the virtual virtualshelves, and returns a summary. C<$shelflist>
71 is a reference-to-hash. The keys are the virtualshelves numbers
72 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
73 themselves references-to-hash, with the following keys:
75 C<mincategory> : 2 if the list is for "look". 3 if the list is for "Select virtualshelves for adding a virtual".
76 virtualshelves of the owner are always selected, whatever the category
80 =item C<$shelfhash-E<gt>{shelfname}>
82 A string. The name of the shelf.
84 =item C<$shelfhash-E<gt>{count}>
86 The number of virtuals on that virtualshelves.
93 # FIXME - Wouldn't it be more intuitive to return a list, rather than
94 # a reference-to-hash? The shelf number can be just another key in the
98 my ( $owner, $mincategory ) = @_;
101 SELECT virtualshelves.shelfnumber, virtualshelves.shelfname,owner,surname,firstname,virtualshelves.category,virtualshelves.sortfield,
102 count(virtualshelfcontents.biblionumber) as count
104 LEFT JOIN virtualshelfcontents ON virtualshelves.shelfnumber = virtualshelfcontents.shelfnumber
105 LEFT JOIN borrowers ON virtualshelves.owner = borrowers.borrowernumber
106 WHERE owner=? OR category>=?
107 GROUP BY virtualshelves.shelfnumber
108 ORDER BY virtualshelves.category, virtualshelves.shelfname, borrowers.firstname, borrowers.surname
110 my $sth = $dbh->prepare($query);
111 $sth->execute( $owner, $mincategory );
115 $shelfnumber, $shelfname, $owner, $surname,
116 $firstname, $category, $sortfield, $count
121 $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
122 $shelflist{$shelfnumber}->{'count'} = $count;
123 $shelflist{$shelfnumber}->{'sortfield'} = $sortfield;
124 $shelflist{$shelfnumber}->{'category'} = $category;
125 $shelflist{$shelfnumber}->{'owner'} = $owner;
126 $shelflist{$shelfnumber}->{'surname'} = $surname;
127 $shelflist{$shelfnumber}->{'firstname'} = $firstname;
129 return ( \%shelflist );
134 (shelfnumber,shelfname,owner,category) = &GetShelf($shelfnumber);
136 Looks up information about the contents of virtual virtualshelves number
139 Returns the database's information on 'virtualshelves' table.
144 my ($shelfnumber) = @_;
146 SELECT shelfnumber,shelfname,owner,category,sortfield
150 my $sth = $dbh->prepare($query);
151 $sth->execute($shelfnumber);
152 return $sth->fetchrow;
155 =item GetShelfContents
157 $itemlist = &GetShelfContents($shelfnumber);
159 Looks up information about the contents of virtual virtualshelves number
160 C<$shelfnumber>. Sorted by a field in the biblio table. copyrightdate
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 ,$sortfield) = @_;
171 my $dbh=C4::Context->dbh();
173 my $sthsort = $dbh->prepare('select sortfield from virtualshelves where shelfnumber=?');
174 $sthsort->execute($shelfnumber);
175 ($sortfield) = $sthsort->fetchrow_array;
179 " SELECT vc.biblionumber,vc.shelfnumber,biblio.*
180 FROM virtualshelfcontents vc LEFT JOIN biblio on vc.biblionumber=biblio.biblionumber
181 WHERE vc.shelfnumber=? ";
182 my @bind = ($shelfnumber);
184 #$sortfield = $dbh->quote($sortfield);
185 $query .= " ORDER BY `$sortfield` ";
186 $query .= " DESC " if ($sortfield eq 'copyrightdate');
188 my $sth = $dbh->prepare($query);
189 $sth->execute(@bind);
190 while ( my $item = $sth->fetchrow_hashref ) {
191 push( @itemlist, $item );
193 return ( \@itemlist );
198 $shelfnumber = &AddShelf( $shelfname, $owner, $category);
200 Creates a new virtual virtualshelves with name C<$shelfname>, owner C<$owner> and category
203 Returns a code to know what's happen.
204 * -1 : if this virtualshelves already exist.
205 * $shelfnumber : if success.
210 my ( $shelfname, $owner, $category ) = @_;
214 WHERE shelfname=? AND owner=?
216 my $sth = $dbh->prepare($query);
217 $sth->execute($shelfname,$owner);
218 ( $sth->rows ) and return (-1);
220 INSERT INTO virtualshelves
221 (shelfname,owner,category)
224 $sth = $dbh->prepare($query);
225 $sth->execute( $shelfname, $owner, $category );
226 my $shelfnumber = $dbh->{'mysql_insertid'};
227 return ($shelfnumber);
232 &AddToShelf($biblionumber, $shelfnumber);
234 Adds item number C<$biblionumber> to virtual virtualshelves number
235 C<$shelfnumber>, unless that item is already on that shelf.
241 my ( $biblionumber, $shelfnumber ) = @_;
242 return unless $biblionumber;
245 FROM virtualshelfcontents
246 WHERE shelfnumber=? AND biblionumber=?
248 my $sth = $dbh->prepare($query);
250 $sth->execute( $shelfnumber, $biblionumber );
251 unless ( $sth->rows ) {
254 INSERT INTO virtualshelfcontents
255 (shelfnumber, biblionumber, flags)
259 $sth = $dbh->prepare($query);
260 $sth->execute( $shelfnumber, $biblionumber );
264 =item AddToShelfFromBiblio
266 &AddToShelfFromBiblio($biblionumber, $shelfnumber)
268 this function allow to add a virtual into the shelf number $shelfnumber
273 sub AddToShelfFromBiblio {
274 my ( $biblionumber, $shelfnumber ) = @_;
275 return unless $biblionumber;
278 FROM virtualshelfcontents
279 WHERE shelfnumber=? AND biblionumber=?
281 my $sth = $dbh->prepare($query);
282 $sth->execute( $shelfnumber, $biblionumber );
283 unless ( $sth->rows ) {
285 INSERT INTO virtualshelfcontents
286 (shelfnumber, biblionumber, flags)
290 $sth = $dbh->prepare($query);
291 $sth->execute( $shelfnumber, $biblionumber );
297 ModShelf($shelfnumber, $shelfname, $owner, $category )
299 Modify the value into virtualshelves table with values given on input arg.
304 my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) = @_;
306 UPDATE virtualshelves
307 SET shelfname=?,owner=?,category=?,sortfield=?
310 my $sth = $dbh->prepare($query);
311 $sth->execute( $shelfname, $owner, $category, $sortfield, $shelfnumber );
316 ($status) = &DelShelf($shelfnumber);
318 Deletes virtual virtualshelves number C<$shelfnumber>. The virtualshelves must
321 Returns a two-element array, where C<$status> is 0 if the operation
322 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
323 success, or an error message giving the reason for failure.
328 =item ShelfPossibleAction
330 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
332 C<$loggedinuser,$shelfnumber,$action>
334 $action can be "view" or "manage".
336 Returns 1 if the user can do the $action in the $shelfnumber shelf.
341 sub ShelfPossibleAction {
342 my ( $user, $shelfnumber, $action ) = @_;
344 SELECT owner,category
348 my $sth = $dbh->prepare($query);
349 $sth->execute($shelfnumber);
350 my ( $owner, $category ) = $sth->fetchrow;
351 return 1 if (($category >= 3 or $owner eq $user) && $action eq 'manage' );
352 return 1 if (($category >= 2 or $owner eq $user) && $action eq 'view' );
358 &DelFromShelf( $biblionumber, $shelfnumber);
360 Removes item number C<$biblionumber> from virtual virtualshelves number
361 C<$shelfnumber>. If the item wasn't on that virtualshelves to begin with,
368 my ( $biblionumber, $shelfnumber ) = @_;
370 DELETE FROM virtualshelfcontents
371 WHERE shelfnumber=? AND biblionumber=?
373 my $sth = $dbh->prepare($query);
374 $sth->execute( $shelfnumber, $biblionumber );
379 $Number = DelShelf($shelfnumber);
381 this function delete the shelf number, and all of it's content
387 my ( $shelfnumber ) = @_;
388 my $sth = $dbh->prepare("DELETE FROM virtualshelves WHERE shelfnumber=?");
389 $sth->execute($shelfnumber);
393 END { } # module clean-up code here (global destructor)
403 Koha Developement team <info@koha.org>
407 C4::Circulation::Circ2(3)