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
28 use vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
36 &GetShelves &GetShelfContents &GetShelf
38 &AddToShelf &AddToShelfFromBiblio &AddShelf
42 &DelFromShelf &DelShelf
46 my $dbh = C4::Context->dbh;
50 C4::VirtualShelves - Functions for manipulating Koha virtual virtualshelves
54 use C4::VirtualShelves;
58 This module provides functions for manipulating virtual virtualshelves,
59 including creating and deleting virtualshelves, and adding and removing
60 items to and from virtualshelves.
68 $shelflist = &GetShelves($owner, $mincategory);
69 ($shelfnumber, $shelfhash) = each %{$shelflist};
71 Looks up the virtual virtualshelves, and returns a summary. C<$shelflist>
72 is a reference-to-hash. The keys are the virtualshelves numbers
73 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
74 themselves references-to-hash, with the following keys:
76 C<mincategory> : 2 if the list is for "look". 3 if the list is for "Select virtualshelves for adding a virtual".
77 virtualshelves of the owner are always selected, whatever the category
81 =item C<$shelfhash-E<gt>{shelfname}>
83 A string. The name of the shelf.
85 =item C<$shelfhash-E<gt>{count}>
87 The number of virtuals on that virtualshelves.
94 # FIXME - Wouldn't it be more intuitive to return a list, rather than
95 # a reference-to-hash? The shelf number can be just another key in the
99 my ( $owner, $mincategory ) = @_;
102 SELECT virtualshelves.shelfnumber, virtualshelves.shelfname,owner,surname,firstname,virtualshelves.category,virtualshelves.sortfield,
103 count(virtualshelfcontents.biblionumber) as count
105 LEFT JOIN virtualshelfcontents ON virtualshelves.shelfnumber = virtualshelfcontents.shelfnumber
106 LEFT JOIN borrowers ON virtualshelves.owner = borrowers.borrowernumber
107 WHERE owner=? OR category>=?
108 GROUP BY virtualshelves.shelfnumber
109 ORDER BY virtualshelves.category, virtualshelves.shelfname, borrowers.firstname, borrowers.surname
111 my $sth = $dbh->prepare($query);
112 $sth->execute( $owner, $mincategory );
116 $shelfnumber, $shelfname, $owner, $surname,
117 $firstname, $category, $sortfield, $count
122 $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
123 $shelflist{$shelfnumber}->{'count'} = $count;
124 $shelflist{$shelfnumber}->{'sortfield'} = $sortfield;
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 virtualshelves number
140 Returns the database's information on 'virtualshelves' table.
145 my ($shelfnumber) = @_;
147 SELECT shelfnumber,shelfname,owner,category,sortfield
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 virtualshelves number
161 C<$shelfnumber>. Sorted by a field in the biblio table. copyrightdate
164 Returns a reference-to-array, whose elements are references-to-hash,
165 as returned by C<C4::Biblio::GetBiblioFromItemNumber>.
167 Note: the notforloan status comes from the itemtype, and where it equals 0
168 it does not ensure that related items.notforloan status is likewise 0. The
169 caller has to check any items on their own, possibly with CanBookBeIssued
170 from C4::Circulation.
174 sub GetShelfContents {
175 my ( $shelfnumber ,$sortfield) = @_;
176 my $dbh=C4::Context->dbh();
178 my $sthsort = $dbh->prepare('select sortfield from virtualshelves where shelfnumber=?');
179 $sthsort->execute($shelfnumber);
180 ($sortfield) = $sthsort->fetchrow_array;
183 " SELECT vc.biblionumber, vc.shelfnumber,
184 biblio.*, biblioitems.itemtype, itemtypes.*
185 FROM virtualshelfcontents vc
186 LEFT JOIN biblio ON vc.biblionumber = biblio.biblionumber
187 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
188 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
189 WHERE vc.shelfnumber=? ";
191 $query .= " ORDER BY `$sortfield` ";
192 $query .= " DESC " if ($sortfield eq 'copyrightdate');
194 my $sth = $dbh->prepare($query);
195 $sth->execute($shelfnumber);
196 return $sth->fetchall_arrayref({});
197 # Like the perldoc says,
198 # returns reference-to-array, where each element is reference-to-hash of the row:
199 # like [ $sth->fetchrow_hashref(), $sth->fetchrow_hashref() ... ]
200 # Suitable for use in TMPL_LOOP.
201 # See http://search.cpan.org/~timb/DBI-1.601/DBI.pm#fetchall_arrayref
202 # or newer, for your version of DBI.
207 $shelfnumber = &AddShelf( $shelfname, $owner, $category);
209 Creates a new virtual virtualshelves with name C<$shelfname>, owner C<$owner> and category
212 Returns a code to know what's happen.
213 * -1 : if this virtualshelves already exist.
214 * $shelfnumber : if success.
219 my ( $shelfname, $owner, $category ) = @_;
223 WHERE shelfname=? AND owner=?
225 my $sth = $dbh->prepare($query);
226 $sth->execute($shelfname,$owner);
227 ( $sth->rows ) and return (-1);
229 INSERT INTO virtualshelves
230 (shelfname,owner,category)
233 $sth = $dbh->prepare($query);
234 $sth->execute( $shelfname, $owner, $category );
235 my $shelfnumber = $dbh->{'mysql_insertid'};
236 return ($shelfnumber);
241 &AddToShelf($biblionumber, $shelfnumber);
243 Adds item number C<$biblionumber> to virtual virtualshelves number
244 C<$shelfnumber>, unless that item is already on that shelf.
250 my ( $biblionumber, $shelfnumber ) = @_;
251 return unless $biblionumber;
254 FROM virtualshelfcontents
255 WHERE shelfnumber=? AND biblionumber=?
257 my $sth = $dbh->prepare($query);
259 $sth->execute( $shelfnumber, $biblionumber );
260 unless ( $sth->rows ) {
263 INSERT INTO virtualshelfcontents
264 (shelfnumber, biblionumber, flags)
268 $sth = $dbh->prepare($query);
269 $sth->execute( $shelfnumber, $biblionumber );
273 =item AddToShelfFromBiblio
275 &AddToShelfFromBiblio($biblionumber, $shelfnumber)
277 this function allow to add a virtual into the shelf number $shelfnumber
282 sub AddToShelfFromBiblio {
283 my ( $biblionumber, $shelfnumber ) = @_;
284 return unless $biblionumber;
287 FROM virtualshelfcontents
288 WHERE shelfnumber=? AND biblionumber=?
290 my $sth = $dbh->prepare($query);
291 $sth->execute( $shelfnumber, $biblionumber );
292 unless ( $sth->rows ) {
294 INSERT INTO virtualshelfcontents
295 (shelfnumber, biblionumber, flags)
299 $sth = $dbh->prepare($query);
300 $sth->execute( $shelfnumber, $biblionumber );
306 ModShelf($shelfnumber, $shelfname, $owner, $category )
308 Modify the value into virtualshelves table with values given on input arg.
313 my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) = @_;
315 UPDATE virtualshelves
316 SET shelfname=?,owner=?,category=?,sortfield=?
319 my $sth = $dbh->prepare($query);
320 $sth->execute( $shelfname, $owner, $category, $sortfield, $shelfnumber );
325 ($status) = &DelShelf($shelfnumber);
327 Deletes virtual virtualshelves number C<$shelfnumber>. The virtualshelves must
330 Returns a two-element array, where C<$status> is 0 if the operation
331 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
332 success, or an error message giving the reason for failure.
337 =item ShelfPossibleAction
339 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
341 C<$loggedinuser,$shelfnumber,$action>
343 $action can be "view" or "manage".
345 Returns 1 if the user can do the $action in the $shelfnumber shelf.
350 sub ShelfPossibleAction {
351 my ( $user, $shelfnumber, $action ) = @_;
353 SELECT owner,category
357 my $sth = $dbh->prepare($query);
358 $sth->execute($shelfnumber);
359 my ( $owner, $category ) = $sth->fetchrow;
360 return 1 if ($owner eq $user);
361 return 1 if ( $category >= 3);
362 return 1 if (($category >= 2) && $action eq 'view' );
368 &DelFromShelf( $biblionumber, $shelfnumber);
370 Removes item number C<$biblionumber> from virtual virtualshelves number
371 C<$shelfnumber>. If the item wasn't on that virtualshelves to begin with,
378 my ( $biblionumber, $shelfnumber ) = @_;
380 DELETE FROM virtualshelfcontents
381 WHERE shelfnumber=? AND biblionumber=?
383 my $sth = $dbh->prepare($query);
384 $sth->execute( $shelfnumber, $biblionumber );
389 $Number = DelShelf($shelfnumber);
391 this function delete the shelf number, and all of it's content
398 carp "DelShelf called without valid argument (shelfnumber)";
401 my $sth = $dbh->prepare("DELETE FROM virtualshelves WHERE shelfnumber=?");
402 return $sth->execute(shift);
413 Koha Developement team <info@koha.org>
417 C4::Circulation::Circ2(3)