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