adding bookshelf features :
[koha.git] / C4 / BookShelves.pm
1 # -*- tab-width: 8 -*-
2 # Please use 8-character tabs for this file (indents are every 4 characters)
3
4 package C4::BookShelves;
5
6 # $Id$
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
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
15 # version.
16 #
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.
20 #
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
24
25 use strict;
26 require Exporter;
27 use DBI;
28 use C4::Context;
29 use C4::Circulation::Circ2;
30 use vars qw($VERSION @ISA @EXPORT);
31
32 # set the version for version checking
33 $VERSION = 0.01;
34
35 =head1 NAME
36
37 C4::BookShelves - Functions for manipulating Koha virtual bookshelves
38
39 =head1 SYNOPSIS
40
41   use C4::BookShelves;
42
43 =head1 DESCRIPTION
44
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.
48
49 =head1 FUNCTIONS
50
51 =over 2
52
53 =cut
54
55 @ISA = qw(Exporter);
56 @EXPORT = qw(&GetShelfList &GetShelfContents &GetShelf
57                                 &AddToShelf &AddToShelfFromBiblio
58                                 &RemoveFromShelf &AddShelf &RemoveShelf
59                                 &ShelfPossibleAction);
60
61 my $dbh = C4::Context->dbh;
62
63 =item ShelfPossibleAction
64
65 =over 4
66
67 =item C<$loggedinuser,$shelfnumber,$action>
68
69 $action can be "view" or "manage".
70
71 Returns 1 if the user can do the $action in the $shelfnumber shelf.
72 Returns 0 otherwise.
73
74 =back
75
76 =cut
77 sub ShelfPossibleAction {
78         my ($loggedinuser,$shelfnumber,$action)= @_;
79         my $sth = $dbh->prepare("select owner,category from bookshelf where shelfnumber=?");
80         $sth->execute($shelfnumber);
81         my ($owner,$category) = $sth->fetchrow;
82         return 1 if (($category>=3 or $owner eq $loggedinuser) && $action eq 'manage');
83         return 1 if (($category>= 2 or $owner eq $loggedinuser) && $action eq 'view');
84         return 0;
85 }
86
87 =item GetShelfList
88
89   $shelflist = &GetShelfList();
90   ($shelfnumber, $shelfhash) = each %{$shelflist};
91
92 Looks up the virtual bookshelves, and returns a summary. C<$shelflist>
93 is a reference-to-hash. The keys are the bookshelf numbers
94 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
95 themselves references-to-hash, with the following keys:
96
97 =over 4
98
99 =item C<$shelfhash-E<gt>{shelfname}>
100
101 A string. The name of the shelf.
102
103 =item C<$shelfhash-E<gt>{count}>
104
105 The number of books on that bookshelf.
106
107 =back
108
109 =cut
110 #'
111 # FIXME - Wouldn't it be more intuitive to return a list, rather than
112 # a reference-to-hash? The shelf number can be just another key in the
113 # hash.
114 sub GetShelfList {
115         my ($owner,$mincategory) = @_;
116         # mincategory : 2 if the list is for "look". 3 if the list is for "Select bookshelf for adding a book".
117         # bookshelves of the owner are always selected, whatever the category
118         my $sth=$dbh->prepare("SELECT           bookshelf.shelfnumber, bookshelf.shelfname,owner,surname,firstname,
119                                                         count(shelfcontents.itemnumber) as count
120                                                                 FROM            bookshelf
121                                                                 LEFT JOIN       shelfcontents
122                                                                 ON              bookshelf.shelfnumber = shelfcontents.shelfnumber
123                                                                 left join borrowers on bookshelf.owner = borrowers.borrowernumber
124                                                                 where owner=? or category>=?
125                                                                 GROUP BY        bookshelf.shelfnumber order by shelfname");
126     $sth->execute($owner,$mincategory);
127     my %shelflist;
128     while (my ($shelfnumber, $shelfname,$owner,$surname,$firstname,$count) = $sth->fetchrow) {
129         $shelflist{$shelfnumber}->{'shelfname'}=$shelfname;
130         $shelflist{$shelfnumber}->{'count'}=$count;
131         $shelflist{$shelfnumber}->{'owner'}=$owner;
132         $shelflist{$shelfnumber}->{surname} = $surname;
133         $shelflist{$shelfnumber}->{firstname} = $firstname;
134     }
135     return(\%shelflist);
136 }
137
138 sub GetShelf {
139         my ($shelfnumber) = @_;
140         my $sth=$dbh->prepare("select shelfnumber,shelfname,owner,category from bookshelf where shelfnumber=?");
141         $sth->execute($shelfnumber);
142         return $sth->fetchrow;
143 }
144 =item GetShelfContents
145
146   $itemlist = &GetShelfContents($env, $shelfnumber);
147
148 Looks up information about the contents of virtual bookshelf number
149 C<$shelfnumber>.
150
151 Returns a reference-to-array, whose elements are references-to-hash,
152 as returned by C<&getiteminformation>.
153
154 I don't know what C<$env> is.
155
156 =cut
157 #'
158 sub GetShelfContents {
159     my ($env, $shelfnumber) = @_;
160     my @itemlist;
161     my $sth=$dbh->prepare("select itemnumber from shelfcontents where shelfnumber=? order by itemnumber");
162     $sth->execute($shelfnumber);
163     while (my ($itemnumber) = $sth->fetchrow) {
164         my ($item) = getiteminformation($env, $itemnumber, 0);
165         push (@itemlist, $item);
166     }
167     return (\@itemlist);
168 }
169
170 =item AddToShelf
171
172   &AddToShelf($env, $itemnumber, $shelfnumber);
173
174 Adds item number C<$itemnumber> to virtual bookshelf number
175 C<$shelfnumber>, unless that item is already on that shelf.
176
177 C<$env> is ignored.
178
179 =cut
180 #'
181 sub AddToShelf {
182         my ($env, $itemnumber, $shelfnumber) = @_;
183         return unless $itemnumber;
184         my $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=? and itemnumber=?");
185
186         $sth->execute($shelfnumber, $itemnumber);
187         if ($sth->rows) {
188 # already on shelf
189         } else {
190                 $sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values (?, ?, 0)");
191                 $sth->execute($shelfnumber, $itemnumber);
192         }
193 }
194 sub AddToShelfFromBiblio {
195         my ($env, $biblionumber, $shelfnumber) = @_;
196         return unless $biblionumber;
197         my $sth = $dbh->prepare("select itemnumber from items where biblionumber=?");
198         $sth->execute($biblionumber);
199         my ($itemnumber) = $sth->fetchrow;
200         $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=? and itemnumber=?");
201         $sth->execute($shelfnumber, $itemnumber);
202         if ($sth->rows) {
203 # already on shelf
204         } else {
205                 $sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values (?, ?, 0)");
206                 $sth->execute($shelfnumber, $itemnumber);
207         }
208 }
209
210 =item RemoveFromShelf
211
212   &RemoveFromShelf($env, $itemnumber, $shelfnumber);
213
214 Removes item number C<$itemnumber> from virtual bookshelf number
215 C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
216 nothing happens.
217
218 C<$env> is ignored.
219
220 =cut
221 #'
222 sub RemoveFromShelf {
223     my ($env, $itemnumber, $shelfnumber) = @_;
224     my $sth=$dbh->prepare("delete from shelfcontents where shelfnumber=? and itemnumber=?");
225     $sth->execute($shelfnumber,$itemnumber);
226 }
227
228 =item AddShelf
229
230   ($status, $msg) = &AddShelf($env, $shelfname);
231
232 Creates a new virtual bookshelf with name C<$shelfname>.
233
234 Returns a two-element array, where C<$status> is 0 if the operation
235 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
236 success, or an error message giving the reason for failure.
237
238 C<$env> is ignored.
239
240 =cut
241 #'
242 # FIXME - Perhaps this could/should return the number of the new bookshelf
243 # as well?
244 sub AddShelf {
245     my ($env, $shelfname,$owner,$category) = @_;
246     my $sth=$dbh->prepare("select * from bookshelf where shelfname=?");
247         $sth->execute($shelfname);
248     if ($sth->rows) {
249                 return(1, "Shelf \"$shelfname\" already exists");
250     } else {
251                 $sth=$dbh->prepare("insert into bookshelf (shelfname,owner,category) values (?,?,?)");
252                 $sth->execute($shelfname,$owner,$category);
253                 my $shelfnumber = $dbh->{'mysql_insertid'};
254                 return (0, "Done",$shelfnumber);
255     }
256 }
257
258 =item RemoveShelf
259
260   ($status, $msg) = &RemoveShelf($env, $shelfnumber);
261
262 Deletes virtual bookshelf number C<$shelfnumber>. The bookshelf must
263 be empty.
264
265 Returns a two-element array, where C<$status> is 0 if the operation
266 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
267 success, or an error message giving the reason for failure.
268
269 C<$env> is ignored.
270
271 =cut
272 #'
273 sub RemoveShelf {
274     my ($env, $shelfnumber) = @_;
275     my $sth=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=?");
276         $sth->execute($shelfnumber);
277     my ($count)=$sth->fetchrow;
278     if ($count) {
279         return (1, "Shelf has $count items on it.  Please remove all items before deleting this shelf.");
280     } else {
281         $sth=$dbh->prepare("delete from bookshelf where shelfnumber=?");
282         $sth->execute($shelfnumber);
283         return (0, "Done");
284     }
285 }
286
287 END { }       # module clean-up code here (global destructor)
288
289 1;
290
291 #
292 # $Log$
293 # Revision 1.14  2004/12/15 17:28:23  tipaul
294 # adding bookshelf features :
295 # * create bookshelf on the fly
296 # * modify a bookshelf (this being not finished, will commit the rest soon)
297 #
298 # Revision 1.13  2004/03/11 16:06:20  tipaul
299 # *** empty log message ***
300 #
301 # Revision 1.11.2.2  2004/02/19 10:15:41  tipaul
302 # new feature : adding book to bookshelf from biblio detail screen.
303 #
304 # Revision 1.11.2.1  2004/02/06 14:16:55  tipaul
305 # fixing bugs in bookshelves management.
306 #
307 # Revision 1.11  2003/12/15 10:57:08  slef
308 # DBI call fix for bug 662
309 #
310 # Revision 1.10  2003/02/05 10:05:02  acli
311 # Converted a few SQL statements to use ? to fix a few strange SQL errors
312 # Noted correct tab size
313 #
314 # Revision 1.9  2002/10/13 08:29:18  arensb
315 # Deleted unused variables.
316 # Removed trailing whitespace.
317 #
318 # Revision 1.8  2002/10/10 04:32:44  arensb
319 # Simplified references.
320 #
321 # Revision 1.7  2002/10/05 09:50:10  arensb
322 # Merged with arensb-context branch: use C4::Context->dbh instead of
323 # &C4Connect, and generally prefer C4::Context over C4::Database.
324 #
325 # Revision 1.6.2.1  2002/10/04 02:24:43  arensb
326 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
327 # C4Connect.
328 #
329 # Revision 1.6  2002/09/23 13:50:30  arensb
330 # Fixed missing bit in POD.
331 #
332 # Revision 1.5  2002/09/22 17:29:17  arensb
333 # Added POD.
334 # Added some FIXME comments.
335 # Removed useless trailing whitespace.
336 #
337 # Revision 1.4  2002/08/14 18:12:51  tonnesen
338 # Added copyright statement to all .pl and .pm files
339 #
340 # Revision 1.3  2002/07/02 17:48:06  tonnesen
341 # Merged in updates from rel-1-2
342 #
343 # Revision 1.2.2.1  2002/06/26 20:46:48  tonnesen
344 # Inserting some changes I made locally a while ago.
345 #
346 #
347
348 __END__
349
350 =back
351
352 =head1 AUTHOR
353
354 Koha Developement team <info@koha.org>
355
356 =head1 SEE ALSO
357
358 C4::Circulation::Circ2(3)
359
360 =cut