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 @EXPORT_OK);
31 # set the version for version checking
36 &GetShelves &GetShelfContents &GetShelf
38 &AddToShelf &AddToShelfFromBiblio &AddShelf
42 &DelFromShelf &DelShelf
44 @EXPORT_OK = qw(&GetShelvesSummary);
47 my $dbh = C4::Context->dbh;
51 C4::VirtualShelves - Functions for manipulating Koha virtual virtualshelves
55 use C4::VirtualShelves;
59 This module provides functions for manipulating virtual virtualshelves,
60 including creating and deleting virtualshelves, and adding and removing
61 items to and from virtualshelves.
69 $shelflist = &GetShelves($owner);
70 $shelflist = &GetShelves($owner, $mincategory);
71 $shelflist = &GetShelves($owner, $mincategory, $limit);
72 ($shelfnumber, $shelfhash) = each %{$shelflist};
74 Looks up the virtual virtualshelves, and returns a summary. C<$shelflist>
75 is a reference-to-hash. The keys are the virtualshelves numbers
76 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
77 themselves references-to-hash, with the following keys:
79 C<mincategory> : 2 if the list is for "Public", 3 for "Open".
80 virtualshelves of the owner are always selected, whatever the category
84 =item C<$shelfhash-E<gt>{shelfname}>
86 A string. The name of the shelf.
88 =item C<$shelfhash-E<gt>{count}>
90 The number of virtuals on that virtualshelves.
97 my ($owner, $mincategory, $limit) = @_;
98 ($mincategory and $mincategory =~ /^\d+$/) or $mincategory = 2;
99 ( $limit and $limit =~ /^\d+$/) or $limit = undef;
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 $limit and $query .= " LIMIT $limit ";
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 );
133 sub GetShelvesSummary {
134 my ($owner, $mincategory, $limit) = @_;
135 ($mincategory and $mincategory =~ /^\d+$/) or $mincategory = 2;
136 ( $limit and $limit =~ /^\d+$/) or $limit = 10;
139 virtualshelves.shelfnumber,
140 virtualshelves.shelfname,
142 CONCAT(firstname, ' ', surname) AS name,
143 virtualshelves.category,
144 count(virtualshelfcontents.biblionumber) AS count
146 LEFT JOIN virtualshelfcontents ON virtualshelves.shelfnumber = virtualshelfcontents.shelfnumber
147 LEFT JOIN borrowers ON virtualshelves.owner = borrowers.borrowernumber
148 WHERE owner=? OR category>=?
149 GROUP BY virtualshelves.shelfnumber
150 ORDER BY virtualshelves.category, borrowers.surname, borrowers.firstname, virtualshelves.shelfname
153 my $sth = $dbh->prepare($query);
154 $sth->execute($owner,$mincategory,$limit);
155 return $sth->fetchall_arrayref({});
156 # Probably NOT the final implementation since it is still bulky (repeated hash keys).
157 # might like an array of rows of delimited values:
158 # 1|2||0|blacklist|112
159 # 2|6|Josh Ferraro|51|en_fuego|106
164 (shelfnumber,shelfname,owner,category) = &GetShelf($shelfnumber);
166 Looks up information about the contents of virtual virtualshelves number
169 Returns the database's information on 'virtualshelves' table.
174 my ($shelfnumber) = @_;
176 SELECT shelfnumber,shelfname,owner,category,sortfield
180 my $sth = $dbh->prepare($query);
181 $sth->execute($shelfnumber);
182 return $sth->fetchrow;
185 =item GetShelfContents
187 $itemlist = &GetShelfContents($shelfnumber);
189 Looks up information about the contents of virtual virtualshelves number
190 C<$shelfnumber>. Sorted by a field in the biblio table. copyrightdate
193 Returns a reference-to-array, whose elements are references-to-hash,
194 as returned by C<C4::Biblio::GetBiblioFromItemNumber>.
196 Note: the notforloan status comes from the itemtype, and where it equals 0
197 it does not ensure that related items.notforloan status is likewise 0. The
198 caller has to check any items on their own, possibly with CanBookBeIssued
199 from C4::Circulation.
203 sub GetShelfContents {
204 my ( $shelfnumber ,$sortfield) = @_;
205 my $dbh=C4::Context->dbh();
207 my $sthsort = $dbh->prepare('select sortfield from virtualshelves where shelfnumber=?');
208 $sthsort->execute($shelfnumber);
209 ($sortfield) = $sthsort->fetchrow_array;
212 " SELECT vc.biblionumber, vc.shelfnumber,
213 biblio.*, biblioitems.itemtype, itemtypes.*
214 FROM virtualshelfcontents vc
215 LEFT JOIN biblio ON vc.biblionumber = biblio.biblionumber
216 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
217 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
218 WHERE vc.shelfnumber=? ";
220 $query .= " ORDER BY `$sortfield` ";
221 $query .= " DESC " if ($sortfield eq 'copyrightdate');
223 my $sth = $dbh->prepare($query);
224 $sth->execute($shelfnumber);
225 return $sth->fetchall_arrayref({});
226 # Like the perldoc says,
227 # returns reference-to-array, where each element is reference-to-hash of the row:
228 # like [ $sth->fetchrow_hashref(), $sth->fetchrow_hashref() ... ]
229 # Suitable for use in TMPL_LOOP.
230 # See http://search.cpan.org/~timb/DBI-1.601/DBI.pm#fetchall_arrayref
231 # or newer, for your version of DBI.
236 $shelfnumber = &AddShelf( $shelfname, $owner, $category);
238 Creates a new virtual virtualshelves with name C<$shelfname>, owner C<$owner> and category
241 Returns a code to know what's happen.
242 * -1 : if this virtualshelves already exist.
243 * $shelfnumber : if success.
248 my ( $shelfname, $owner, $category ) = @_;
252 WHERE shelfname=? AND owner=?
254 my $sth = $dbh->prepare($query);
255 $sth->execute($shelfname,$owner);
256 ( $sth->rows ) and return (-1);
258 INSERT INTO virtualshelves
259 (shelfname,owner,category)
262 $sth = $dbh->prepare($query);
263 $sth->execute( $shelfname, $owner, $category );
264 my $shelfnumber = $dbh->{'mysql_insertid'};
265 return ($shelfnumber);
270 &AddToShelf($biblionumber, $shelfnumber);
272 Adds item number C<$biblionumber> to virtual virtualshelves number
273 C<$shelfnumber>, unless that item is already on that shelf.
279 my ( $biblionumber, $shelfnumber ) = @_;
280 return unless $biblionumber;
283 FROM virtualshelfcontents
284 WHERE shelfnumber=? AND biblionumber=?
286 my $sth = $dbh->prepare($query);
288 $sth->execute( $shelfnumber, $biblionumber );
289 ($sth->rows) and return undef; # already on shelf
291 INSERT INTO virtualshelfcontents
292 (shelfnumber, biblionumber, flags)
296 $sth = $dbh->prepare($query);
297 $sth->execute( $shelfnumber, $biblionumber );
300 =item AddToShelfFromBiblio
302 &AddToShelfFromBiblio($biblionumber, $shelfnumber)
304 this function allow to add a virtual into the shelf number $shelfnumber
309 sub AddToShelfFromBiblio {
310 my ( $biblionumber, $shelfnumber ) = @_;
311 return unless $biblionumber;
314 FROM virtualshelfcontents
315 WHERE shelfnumber=? AND biblionumber=?
317 my $sth = $dbh->prepare($query);
318 $sth->execute( $shelfnumber, $biblionumber );
319 unless ( $sth->rows ) {
321 INSERT INTO virtualshelfcontents
322 (shelfnumber, biblionumber, flags)
326 $sth = $dbh->prepare($query);
327 $sth->execute( $shelfnumber, $biblionumber );
333 ModShelf($shelfnumber, $shelfname, $owner, $category )
335 Modify the value into virtualshelves table with values given on input arg.
340 my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) = @_;
342 UPDATE virtualshelves
343 SET shelfname=?,owner=?,category=?,sortfield=?
346 my $sth = $dbh->prepare($query);
347 $sth->execute( $shelfname, $owner, $category, $sortfield, $shelfnumber );
350 =item ShelfPossibleAction
352 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
354 C<$loggedinuser,$shelfnumber,$action>
356 $action can be "view" or "manage".
358 Returns 1 if the user can do the $action in the $shelfnumber shelf.
363 sub ShelfPossibleAction {
364 my ( $user, $shelfnumber, $action ) = @_;
366 SELECT owner,category
370 my $sth = $dbh->prepare($query);
371 $sth->execute($shelfnumber);
372 my ( $owner, $category ) = $sth->fetchrow;
373 return 1 if ($owner eq $user);
374 return 1 if ( $category >= 3);
375 return 1 if (($category >= 2) && $action eq 'view' );
381 &DelFromShelf( $biblionumber, $shelfnumber);
383 Removes item number C<$biblionumber> from virtual virtualshelves number
384 C<$shelfnumber>. If the item wasn't on that virtualshelves to begin with,
391 my ( $biblionumber, $shelfnumber ) = @_;
393 DELETE FROM virtualshelfcontents
394 WHERE shelfnumber=? AND biblionumber=?
396 my $sth = $dbh->prepare($query);
397 $sth->execute( $shelfnumber, $biblionumber );
400 =item DelShelf (old version)
402 ($status, $msg) = &DelShelf($shelfnumber);
404 Deletes virtual virtualshelves number C<$shelfnumber>. The virtualshelves must
407 Returns a two-element array, where C<$status> is 0 if the operation
408 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
409 success, or an error message giving the reason for failure.
411 =item DelShelf (current version)
413 $Number = DelShelf($shelfnumber);
415 This function deletes the shelf number, and all of it's content.
421 carp "DelShelf called without valid argument (shelfnumber)";
424 my $sth = $dbh->prepare("DELETE FROM virtualshelves WHERE shelfnumber=?");
425 return $sth->execute(shift);
436 Koha Developement team <info@koha.org>
440 C4::Circulation::Circ2(3)