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