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