Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
[koha.git] / C4 / BookShelves.pm
1 package C4::BookShelves;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 require Exporter;
24 use DBI;
25 use C4::Context;
26 use C4::Circulation::Circ2;
27 use vars qw($VERSION @ISA @EXPORT);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::BookShelves - Functions for manipulating Koha virtual bookshelves
35
36 =head1 SYNOPSIS
37
38   use C4::BookShelves;
39
40 =head1 DESCRIPTION
41
42 This module provides functions for manipulating virtual bookshelves,
43 including creating and deleting bookshelves, and adding and removing
44 items to and from bookshelves.
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 @ISA = qw(Exporter);
53 @EXPORT = qw(&GetShelfList &GetShelfContents &AddToShelf &RemoveFromShelf &AddShelf &RemoveShelf);
54
55 my $dbh = C4::Context->dbh;
56
57 =item GetShelfList
58
59   $shelflist = &GetShelfList();
60   ($shelfnumber, $shelfhash) = each %{$shelflist};
61
62 Looks up the virtual bookshelves, and returns a summary. C<$shelflist>
63 is a reference-to-hash. The keys are the bookshelf numbers
64 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
65 themselves references-to-hash, with the following keys:
66
67 =over 4
68
69 =item C<$shelfhash-E<gt>{shelfname}>
70
71 A string. The name of the shelf.
72
73 =item C<$shelfhash-E<gt>{count}>
74
75 The number of books on that bookshelf.
76
77 =back
78
79 =cut
80 #'
81 # FIXME - Wouldn't it be more intuitive to return a list, rather than
82 # a reference-to-hash? The shelf number can be just another key in the
83 # hash.
84 sub GetShelfList {
85     # FIXME - These two database queries can be combined into one:
86     #   SELECT          bookshelf.shelfnumber, bookshelf.shelfname,
87     #                   count(shelfcontents.itemnumber)
88     #   FROM            bookshelf
89     #   LEFT JOIN       shelfcontents
90     #   ON              bookshelf.shelfnumber = shelfcontents.shelfnumber
91     #   GROUP BY        bookshelf.shelfnumber
92     my $sth=$dbh->prepare("select shelfnumber,shelfname from bookshelf");
93     $sth->execute;
94     my %shelflist;
95     while (my ($shelfnumber, $shelfname) = $sth->fetchrow) {
96         my $sti=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=$shelfnumber");
97                 # FIXME - Should there be an "order by" in here somewhere?
98         $sti->execute;
99         my ($count) = $sti->fetchrow;
100         $shelflist{$shelfnumber}->{'shelfname'}=$shelfname;
101         $shelflist{$shelfnumber}->{'count'}=$count;
102     }
103     return(\%shelflist);
104 }
105
106 =item GetShelfContents
107
108   $itemlist = &GetShelfContents($env, $shelfnumber);
109
110 Looks up information about the contents of virtual bookshelf number
111 C<$shelfnumber>.
112
113 Returns a reference-to-array, whose elements are references-to-hash,
114 as returned by C<&getiteminformation>.
115
116 I don't know what C<$env> is.
117
118 =cut
119 #'
120 sub GetShelfContents {
121     my ($env, $shelfnumber) = @_;
122     my @itemlist;
123     my $sth=$dbh->prepare("select itemnumber from shelfcontents where shelfnumber=$shelfnumber order by itemnumber");
124     $sth->execute;
125     while (my ($itemnumber) = $sth->fetchrow) {
126         my ($item) = getiteminformation($env, $itemnumber, 0);
127         push (@itemlist, $item);
128     }
129     return (\@itemlist);
130                 # FIXME - Wouldn't it be more intuitive to return a list,
131                 # rather than a reference-to-list?
132 }
133
134 =item AddToShelf
135
136   &AddToShelf($env, $itemnumber, $shelfnumber);
137
138 Adds item number C<$itemnumber> to virtual bookshelf number
139 C<$shelfnumber>, unless that item is already on that shelf.
140
141 C<$env> is ignored.
142
143 =cut
144 #'
145 sub AddToShelf {
146     my ($env, $itemnumber, $shelfnumber) = @_;
147     my $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=$shelfnumber and itemnumber=$itemnumber");
148     $sth->execute;
149     if ($sth->rows) {
150 # already on shelf
151     } else {
152         $sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values ($shelfnumber, $itemnumber, 0)");
153                         # FIXME - The default for 'flags' is NULL.
154                         # Why set it to 0?
155         $sth->execute;
156     }
157 }
158
159 =item RemoveFromShelf
160
161   &RemoveFromShelf($env, $itemnumber, $shelfnumber);
162
163 Removes item number C<$itemnumber> from virtual bookshelf number
164 C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
165 nothing happens.
166
167 C<$env> is ignored.
168
169 =cut
170 #'
171 sub RemoveFromShelf {
172     my ($env, $itemnumber, $shelfnumber) = @_;
173     my $sth=$dbh->prepare("delete from shelfcontents where shelfnumber=$shelfnumber and itemnumber=$itemnumber");
174     $sth->execute;
175 }
176
177 =item AddShelf
178
179   ($status, $msg) = &AddShelf($env, $shelfname);
180
181 Creates a new virtual bookshelf with name C<$shelfname>.
182
183 Returns a two-element array, where C<$status> is 0 if the operation
184 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
185 success, or an error message giving the reason for failure.
186
187 C<$env> is ignored.
188
189 =cut
190 #'
191 # FIXME - Perhaps this could/should return the number of the new bookshelf
192 # as well?
193 sub AddShelf {
194     my ($env, $shelfname) = @_;
195     my $q_shelfname=$dbh->quote($shelfname);
196     my $sth=$dbh->prepare("select * from bookshelf where shelfname=$q_shelfname");
197     $sth->execute;
198     if ($sth->rows) {
199         return(1, "Shelf \"$shelfname\" already exists");
200     } else {
201         $sth=$dbh->prepare("insert into bookshelf (shelfname) values ($q_shelfname)");
202         $sth->execute;
203         return (0, "Done");
204     }
205 }
206
207 =item RemoveShelf
208
209   ($status, $msg) = &RemoveShelf($env, $shelfnumber);
210
211 Deletes virtual bookshelf number C<$shelfnumber>. The bookshelf must
212 be empty.
213
214 Returns a two-element array, where C<$status> is 0 if the operation
215 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
216 success, or an error message giving the reason for failure.
217
218 C<$env> is ignored.
219
220 =cut
221 #'
222 sub RemoveShelf {
223     my ($env, $shelfnumber) = @_;
224     my $sth=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=$shelfnumber");
225     $sth->execute;
226     my ($count)=$sth->fetchrow;
227     if ($count) {
228         return (1, "Shelf has $count items on it.  Please remove all items before deleting this shelf.");
229     } else {
230         $sth=$dbh->prepare("delete from bookshelf where shelfnumber=$shelfnumber");
231         $sth->execute;
232         return (0, "Done");
233     }
234 }
235
236 END { }       # module clean-up code here (global destructor)
237
238 1;
239
240 #
241 # $Log$
242 # Revision 1.9  2002/10/13 08:29:18  arensb
243 # Deleted unused variables.
244 # Removed trailing whitespace.
245 #
246 # Revision 1.8  2002/10/10 04:32:44  arensb
247 # Simplified references.
248 #
249 # Revision 1.7  2002/10/05 09:50:10  arensb
250 # Merged with arensb-context branch: use C4::Context->dbh instead of
251 # &C4Connect, and generally prefer C4::Context over C4::Database.
252 #
253 # Revision 1.6.2.1  2002/10/04 02:24:43  arensb
254 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
255 # C4Connect.
256 #
257 # Revision 1.6  2002/09/23 13:50:30  arensb
258 # Fixed missing bit in POD.
259 #
260 # Revision 1.5  2002/09/22 17:29:17  arensb
261 # Added POD.
262 # Added some FIXME comments.
263 # Removed useless trailing whitespace.
264 #
265 # Revision 1.4  2002/08/14 18:12:51  tonnesen
266 # Added copyright statement to all .pl and .pm files
267 #
268 # Revision 1.3  2002/07/02 17:48:06  tonnesen
269 # Merged in updates from rel-1-2
270 #
271 # Revision 1.2.2.1  2002/06/26 20:46:48  tonnesen
272 # Inserting some changes I made locally a while ago.
273 #
274 #
275
276 __END__
277
278 =back
279
280 =head1 AUTHOR
281
282 Koha Developement team <info@koha.org>
283
284 =head1 SEE ALSO
285
286 C4::Circulation::Circ2(3)
287
288 =cut