Handle the iso8859-1 charset somewhat, so that when the po file is in
[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 &RemoveFromShelf &AddShelf &RemoveShelf);
57
58 my $dbh = C4::Context->dbh;
59
60 =item GetShelfList
61
62   $shelflist = &GetShelfList();
63   ($shelfnumber, $shelfhash) = each %{$shelflist};
64
65 Looks up the virtual bookshelves, and returns a summary. C<$shelflist>
66 is a reference-to-hash. The keys are the bookshelf numbers
67 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
68 themselves references-to-hash, with the following keys:
69
70 =over 4
71
72 =item C<$shelfhash-E<gt>{shelfname}>
73
74 A string. The name of the shelf.
75
76 =item C<$shelfhash-E<gt>{count}>
77
78 The number of books on that bookshelf.
79
80 =back
81
82 =cut
83 #'
84 # FIXME - Wouldn't it be more intuitive to return a list, rather than
85 # a reference-to-hash? The shelf number can be just another key in the
86 # hash.
87 sub GetShelfList {
88     # FIXME - These two database queries can be combined into one:
89     #   SELECT          bookshelf.shelfnumber, bookshelf.shelfname,
90     #                   count(shelfcontents.itemnumber)
91     #   FROM            bookshelf
92     #   LEFT JOIN       shelfcontents
93     #   ON              bookshelf.shelfnumber = shelfcontents.shelfnumber
94     #   GROUP BY        bookshelf.shelfnumber
95     my $sth=$dbh->prepare("select shelfnumber,shelfname from bookshelf");
96     $sth->execute;
97     my %shelflist;
98     while (my ($shelfnumber, $shelfname) = $sth->fetchrow) {
99         my $sti=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=?");
100                 # FIXME - Should there be an "order by" in here somewhere?
101         $sti->execute($shelfnumber);
102         my ($count) = $sti->fetchrow;
103         $shelflist{$shelfnumber}->{'shelfname'}=$shelfname;
104         $shelflist{$shelfnumber}->{'count'}=$count;
105     }
106     return(\%shelflist);
107 }
108
109 =item GetShelfContents
110
111   $itemlist = &GetShelfContents($env, $shelfnumber);
112
113 Looks up information about the contents of virtual bookshelf number
114 C<$shelfnumber>.
115
116 Returns a reference-to-array, whose elements are references-to-hash,
117 as returned by C<&getiteminformation>.
118
119 I don't know what C<$env> is.
120
121 =cut
122 #'
123 sub GetShelfContents {
124     my ($env, $shelfnumber) = @_;
125     my @itemlist;
126     my $sth=$dbh->prepare("select itemnumber from shelfcontents where shelfnumber=? order by itemnumber");
127     $sth->execute($shelfnumber);
128     while (my ($itemnumber) = $sth->fetchrow) {
129         my ($item) = getiteminformation($env, $itemnumber, 0);
130         push (@itemlist, $item);
131     }
132     return (\@itemlist);
133 }
134
135 =item AddToShelf
136
137   &AddToShelf($env, $itemnumber, $shelfnumber);
138
139 Adds item number C<$itemnumber> to virtual bookshelf number
140 C<$shelfnumber>, unless that item is already on that shelf.
141
142 C<$env> is ignored.
143
144 =cut
145 #'
146 sub AddToShelf {
147         my ($env, $itemnumber, $shelfnumber) = @_;
148         return unless $itemnumber;
149         my $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=? and itemnumber=?");
150
151         $sth->execute($shelfnumber, $itemnumber);
152         if ($sth->rows) {
153 # already on shelf
154         } else {
155                 $sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values (?, ?, 0)");
156                 $sth->execute($shelfnumber, $itemnumber);
157         }
158 }
159
160 =item RemoveFromShelf
161
162   &RemoveFromShelf($env, $itemnumber, $shelfnumber);
163
164 Removes item number C<$itemnumber> from virtual bookshelf number
165 C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
166 nothing happens.
167
168 C<$env> is ignored.
169
170 =cut
171 #'
172 sub RemoveFromShelf {
173     my ($env, $itemnumber, $shelfnumber) = @_;
174     my $sth=$dbh->prepare("delete from shelfcontents where shelfnumber=? and itemnumber=?");
175     $sth->execute($shelfnumber,$itemnumber);
176 }
177
178 =item AddShelf
179
180   ($status, $msg) = &AddShelf($env, $shelfname);
181
182 Creates a new virtual bookshelf with name C<$shelfname>.
183
184 Returns a two-element array, where C<$status> is 0 if the operation
185 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
186 success, or an error message giving the reason for failure.
187
188 C<$env> is ignored.
189
190 =cut
191 #'
192 # FIXME - Perhaps this could/should return the number of the new bookshelf
193 # as well?
194 sub AddShelf {
195     my ($env, $shelfname) = @_;
196     my $sth=$dbh->prepare("select * from bookshelf where shelfname=?");
197         $sth->execute($shelfname);
198     if ($sth->rows) {
199         return(1, "Shelf \"$shelfname\" already exists");
200     } else {
201         $sth=$dbh->prepare("insert into bookshelf (shelfname) values (?)");
202         $sth->execute($shelfname);
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=?");
225         $sth->execute($shelfnumber);
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=?");
231         $sth->execute($shelfnumber);
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.12  2004/02/11 08:40:09  tipaul
243 # synch'ing 2.0.0 branch and head
244 #
245 # Revision 1.11.2.1  2004/02/06 14:16:55  tipaul
246 # fixing bugs in bookshelves management.
247 #
248 # Revision 1.11  2003/12/15 10:57:08  slef
249 # DBI call fix for bug 662
250 #
251 # Revision 1.10  2003/02/05 10:05:02  acli
252 # Converted a few SQL statements to use ? to fix a few strange SQL errors
253 # Noted correct tab size
254 #
255 # Revision 1.9  2002/10/13 08:29:18  arensb
256 # Deleted unused variables.
257 # Removed trailing whitespace.
258 #
259 # Revision 1.8  2002/10/10 04:32:44  arensb
260 # Simplified references.
261 #
262 # Revision 1.7  2002/10/05 09:50:10  arensb
263 # Merged with arensb-context branch: use C4::Context->dbh instead of
264 # &C4Connect, and generally prefer C4::Context over C4::Database.
265 #
266 # Revision 1.6.2.1  2002/10/04 02:24:43  arensb
267 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
268 # C4Connect.
269 #
270 # Revision 1.6  2002/09/23 13:50:30  arensb
271 # Fixed missing bit in POD.
272 #
273 # Revision 1.5  2002/09/22 17:29:17  arensb
274 # Added POD.
275 # Added some FIXME comments.
276 # Removed useless trailing whitespace.
277 #
278 # Revision 1.4  2002/08/14 18:12:51  tonnesen
279 # Added copyright statement to all .pl and .pm files
280 #
281 # Revision 1.3  2002/07/02 17:48:06  tonnesen
282 # Merged in updates from rel-1-2
283 #
284 # Revision 1.2.2.1  2002/06/26 20:46:48  tonnesen
285 # Inserting some changes I made locally a while ago.
286 #
287 #
288
289 __END__
290
291 =back
292
293 =head1 AUTHOR
294
295 Koha Developement team <info@koha.org>
296
297 =head1 SEE ALSO
298
299 C4::Circulation::Circ2(3)
300
301 =cut