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