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