adding maxItemsToDisplay
[koha.git] / C4 / VirtualShelves.pm
1 # -*- tab-width: 8 -*-
2 # Please use 8-character tabs for this file (indents are every 4 characters)
3
4 package C4::VirtualShelves;
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 require Exporter;
26 use C4::Context;
27 use C4::Circulation;
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = 3.00;
32
33 =head1 NAME
34
35 C4::VirtualShelves - Functions for manipulating Koha virtual virtualshelves
36
37 =head1 SYNOPSIS
38
39   use C4::VirtualShelves;
40
41 =head1 DESCRIPTION
42
43 This module provides functions for manipulating virtual virtualshelves,
44 including creating and deleting virtualshelves, and adding and removing
45 items to and from virtualshelves.
46
47 =head1 FUNCTIONS
48
49 =over 2
50
51 =cut
52
53 @ISA    = qw(Exporter);
54 @EXPORT = qw(
55         &GetShelves &GetShelfContents &GetShelf
56
57         &AddToShelf &AddToShelfFromBiblio &AddShelf
58
59         &ModShelf
60         &ShelfPossibleAction
61         &DelFromShelf &DelShelf
62 );
63
64 my $dbh = C4::Context->dbh;
65
66 =item GetShelves
67
68   $shelflist = &GetShelves($owner, $mincategory);
69   ($shelfnumber, $shelfhash) = each %{$shelflist};
70
71 Looks up the virtual virtualshelves, and returns a summary. C<$shelflist>
72 is a reference-to-hash. The keys are the virtualshelves numbers
73 (C<$shelfnumber>, above), and the values (C<$shelfhash>, above) are
74 themselves references-to-hash, with the following keys:
75
76 C<mincategory> : 2 if the list is for "look". 3 if the list is for "Select virtualshelves for adding a virtual".
77 virtualshelves of the owner are always selected, whatever the category
78
79 =over 4
80
81 =item C<$shelfhash-E<gt>{shelfname}>
82
83 A string. The name of the shelf.
84
85 =item C<$shelfhash-E<gt>{count}>
86
87 The number of virtuals on that virtualshelves.
88
89 =back
90
91 =cut
92
93 #'
94 # FIXME - Wouldn't it be more intuitive to return a list, rather than
95 # a reference-to-hash? The shelf number can be just another key in the
96 # hash.
97
98 sub GetShelves {
99     my ( $owner, $mincategory ) = @_;
100
101     my $query = qq(
102         SELECT virtualshelves.shelfnumber, virtualshelves.shelfname,owner,surname,firstname,virtualshelves.category,
103                count(virtualshelfcontents.biblionumber) as count
104         FROM   virtualshelves
105             LEFT JOIN   virtualshelfcontents ON virtualshelves.shelfnumber = virtualshelfcontents.shelfnumber
106             LEFT JOIN   borrowers ON virtualshelves.owner = borrowers.borrowernumber
107         WHERE  owner=? OR category>=?
108         GROUP BY virtualshelves.shelfnumber
109         ORDER BY virtualshelves.category, virtualshelves.shelfname, borrowers.firstname, borrowers.surname
110     );
111     my $sth = $dbh->prepare($query);
112     $sth->execute( $owner, $mincategory );
113     my %shelflist;
114     while (
115         my (
116             $shelfnumber, $shelfname, $owner, $surname,
117             $firstname,   $category,  $count
118         )
119         = $sth->fetchrow
120       )
121     {
122         $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
123         $shelflist{$shelfnumber}->{'count'}     = $count;
124         $shelflist{$shelfnumber}->{'category'}  = $category;
125         $shelflist{$shelfnumber}->{'owner'}     = $owner;
126         $shelflist{$shelfnumber}->{'surname'}     = $surname;
127         $shelflist{$shelfnumber}->{'firstname'}   = $firstname;
128     }
129     return ( \%shelflist );
130 }
131
132 =item GetShef
133
134   (shelfnumber,shelfname,owner,category) = &GetShelf($shelfnumber);
135
136 Looks up information about the contents of virtual virtualshelves number
137 C<$shelfnumber>
138
139 Returns the database's information on 'virtualshelves' table.
140
141 =cut
142
143 sub GetShelf {
144     my ($shelfnumber) = @_;
145     my $query = qq(
146         SELECT shelfnumber,shelfname,owner,category
147         FROM   virtualshelves
148         WHERE  shelfnumber=?
149     );
150     my $sth = $dbh->prepare($query);
151     $sth->execute($shelfnumber);
152     return $sth->fetchrow;
153 }
154
155 =item GetShelfContents
156
157   $itemlist = &GetShelfContents($shelfnumber);
158
159 Looks up information about the contents of virtual virtualshelves number
160 C<$shelfnumber>.
161
162 Returns a reference-to-array, whose elements are references-to-hash,
163 as returned by C<C4::Biblio::GetBiblioFromItemNumber>.
164
165 =cut
166
167 #'
168 sub GetShelfContents {
169     my ( $shelfnumber ) = @_;
170     my @itemlist;
171     my $query =
172        " SELECT biblionumber
173          FROM   virtualshelfcontents
174          WHERE  shelfnumber=?
175          ORDER BY biblionumber
176        ";
177     my $sth = $dbh->prepare($query);
178     $sth->execute($shelfnumber);
179     my $sth2 = $dbh->prepare("
180         SELECT biblio.*,biblioitems.* FROM biblio
181             LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber
182         WHERE biblio.biblionumber=?"
183     );
184     while ( my ($biblionumber) = $sth->fetchrow ) {
185         $sth2->execute($biblionumber);
186         my $item = $sth2->fetchrow_hashref;
187         $item->{'biblionumber'}=$biblionumber;
188         push( @itemlist, $item );
189     }
190     return ( \@itemlist );
191 }
192
193 =item AddShelf
194
195   $shelfnumber = &AddShelf( $shelfname, $owner, $category);
196
197 Creates a new virtual virtualshelves with name C<$shelfname>, owner C<$owner> and category
198 C<$category>.
199
200 Returns a code to know what's happen.
201     * -1 : if this virtualshelves already exist.
202     * $shelfnumber : if success.
203
204 =cut
205
206 sub AddShelf {
207     my ( $shelfname, $owner, $category ) = @_;
208     my $query = qq(
209         SELECT *
210         FROM   virtualshelves
211         WHERE  shelfname=? AND owner=?
212     );
213     my $sth = $dbh->prepare($query);
214     $sth->execute($shelfname,$owner);
215     if ( $sth->rows ) {
216         return (-1);
217     }
218     else {
219         my $query = qq(
220             INSERT INTO virtualshelves
221                 (shelfname,owner,category)
222             VALUES (?,?,?)
223         );
224         $sth = $dbh->prepare($query);
225         $sth->execute( $shelfname, $owner, $category );
226         my $shelfnumber = $dbh->{'mysql_insertid'};
227         return ($shelfnumber);
228     }
229 }
230
231 =item AddToShelf
232
233   &AddToShelf($biblionumber, $shelfnumber);
234
235 Adds item number C<$biblionumber> to virtual virtualshelves number
236 C<$shelfnumber>, unless that item is already on that shelf.
237
238 =cut
239
240 #'
241 sub AddToShelf {
242     my ( $biblionumber, $shelfnumber ) = @_;
243     return unless $biblionumber;
244     my $query = qq(
245         SELECT *
246         FROM   virtualshelfcontents
247         WHERE  shelfnumber=? AND biblionumber=?
248     );
249     my $sth = $dbh->prepare($query);
250
251     $sth->execute( $shelfnumber, $biblionumber );
252     unless ( $sth->rows ) {
253         # already on shelf
254         my $query = qq(
255             INSERT INTO virtualshelfcontents
256                 (shelfnumber, biblionumber, flags)
257             VALUES
258                 (?, ?, 0)
259         );
260         $sth = $dbh->prepare($query);
261         $sth->execute( $shelfnumber, $biblionumber );
262     }
263 }
264
265 =item AddToShelfFromBiblio
266  
267     &AddToShelfFromBiblio($biblionumber, $shelfnumber)
268
269     this function allow to add a virtual into the shelf number $shelfnumber
270     from biblionumber.
271
272 =cut
273
274 sub AddToShelfFromBiblio {
275     my ( $biblionumber, $shelfnumber ) = @_;
276     return unless $biblionumber;
277     my $query = qq(
278         SELECT *
279         FROM   virtualshelfcontents
280         WHERE  shelfnumber=? AND biblionumber=?
281     );
282     my $sth = $dbh->prepare($query);
283     $sth->execute( $shelfnumber, $biblionumber );
284     unless ( $sth->rows ) {
285         my $query =qq(
286             INSERT INTO virtualshelfcontents
287                 (shelfnumber, biblionumber, flags)
288             VALUES
289                 (?, ?, 0)
290         );
291         $sth = $dbh->prepare($query);
292         $sth->execute( $shelfnumber, $biblionumber );
293     }
294 }
295
296 =item ModShelf
297
298 ModShelf($shelfnumber, $shelfname, $owner, $category )
299
300 Modify the value into virtualshelves table with values given on input arg.
301
302 =cut
303
304 sub ModShelf {
305     my ( $shelfnumber, $shelfname, $owner, $category ) = @_;
306     my $query = qq(
307         UPDATE virtualshelves
308         SET    shelfname=?,owner=?,category=?
309         WHERE  shelfnumber=?
310     );
311     my $sth = $dbh->prepare($query);
312     $sth->execute( $shelfname, $owner, $category, $shelfnumber );
313 }
314
315 =item DelShelf
316
317   ($status) = &DelShelf($shelfnumber);
318
319 Deletes virtual virtualshelves number C<$shelfnumber>. The virtualshelves must
320 be empty.
321
322 Returns a two-element array, where C<$status> is 0 if the operation
323 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
324 success, or an error message giving the reason for failure.
325
326 =cut
327
328
329 =item ShelfPossibleAction
330
331 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
332
333 C<$loggedinuser,$shelfnumber,$action>
334
335 $action can be "view" or "manage".
336
337 Returns 1 if the user can do the $action in the $shelfnumber shelf.
338 Returns 0 otherwise.
339
340 =cut
341
342 sub ShelfPossibleAction {
343     my ( $user, $shelfnumber, $action ) = @_;
344     my $query = qq(
345         SELECT owner,category
346         FROM   virtualshelves
347         WHERE  shelfnumber=?
348     );
349     my $sth = $dbh->prepare($query);
350     $sth->execute($shelfnumber);
351     my ( $owner, $category ) = $sth->fetchrow;
352     return 1 if (($category >= 3 or $owner eq $user) && $action eq 'manage' );
353     return 1 if (($category >= 2 or $owner eq $user) && $action eq 'view' );
354     return 0;
355 }
356
357 =item DelFromShelf
358
359   &DelFromShelf( $biblionumber, $shelfnumber);
360
361 Removes item number C<$biblionumber> from virtual virtualshelves number
362 C<$shelfnumber>. If the item wasn't on that virtualshelves to begin with,
363 nothing happens.
364
365 =cut
366
367 #'
368 sub DelFromShelf {
369     my ( $biblionumber, $shelfnumber ) = @_;
370     my $query = qq(
371         DELETE FROM virtualshelfcontents
372         WHERE  shelfnumber=? AND biblionumber=?
373     );
374     my $sth = $dbh->prepare($query);
375     $sth->execute( $shelfnumber, $biblionumber );
376 }
377
378 =head2 DelShelf
379
380   $Number = DelShelf($shelfnumber);
381
382     this function delete the shelf number, and all of it's content
383
384 =cut
385
386 #'
387 sub DelShelf {
388     my ( $shelfnumber ) = @_;
389         my $sth = $dbh->prepare("DELETE FROM virtualshelves WHERE shelfnumber=?");
390         $sth->execute($shelfnumber);
391         return 0;
392 }
393
394 END { }    # module clean-up code here (global destructor)
395
396 1;
397
398 __END__
399
400 =back
401
402 =head1 AUTHOR
403
404 Koha Developement team <info@koha.org>
405
406 =head1 SEE ALSO
407
408 C4::Circulation::Circ2(3)
409
410 =cut