2nd try:Fixed buggy NoZebra cataloguing search when search term does not exist in...
[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,virtualshelves.sortfield,
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,  $sortfield, $count
118         )
119         = $sth->fetchrow
120       )
121     {
122         $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
123         $shelflist{$shelfnumber}->{'count'}     = $count;
124         $shelflist{$shelfnumber}->{'sortfield'}     = $sortfield;
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 GetShelf
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,sortfield
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>.  Sorted by a field in the biblio table.  copyrightdate 
162 gives a desc sort.
163
164 Returns a reference-to-array, whose elements are references-to-hash,
165 as returned by C<C4::Biblio::GetBiblioFromItemNumber>.
166
167 =cut
168
169 #'
170 sub GetShelfContents {
171     my ( $shelfnumber ,$sortfield) = @_;
172     my $dbh=C4::Context->dbh();
173         if(!$sortfield) {
174                 my $sthsort = $dbh->prepare('select sortfield from virtualshelves where shelfnumber=?');
175                 $sthsort->execute($shelfnumber);
176                 ($sortfield) = $sthsort->fetchrow_array;
177         }
178         my @itemlist;
179     my $query =
180        " SELECT vc.biblionumber,vc.shelfnumber,biblio.*
181          FROM   virtualshelfcontents vc LEFT JOIN biblio on vc.biblionumber=biblio.biblionumber
182          WHERE  vc.shelfnumber=? ";
183     my @bind = ($shelfnumber);
184         if($sortfield) {
185                 #$sortfield = $dbh->quote($sortfield);
186                 $query .= " ORDER BY `$sortfield`";
187                 $query .= "DESC" if($sortfield eq 'copyrightdate');
188         }
189     my $sth = $dbh->prepare($query);
190     $sth->execute(@bind);
191     while ( my $item = $sth->fetchrow_hashref ) {
192         push( @itemlist, $item );
193     }
194    return ( \@itemlist );
195 }
196
197 =item AddShelf
198
199   $shelfnumber = &AddShelf( $shelfname, $owner, $category);
200
201 Creates a new virtual virtualshelves with name C<$shelfname>, owner C<$owner> and category
202 C<$category>.
203
204 Returns a code to know what's happen.
205     * -1 : if this virtualshelves already exist.
206     * $shelfnumber : if success.
207
208 =cut
209
210 sub AddShelf {
211     my ( $shelfname, $owner, $category ) = @_;
212     my $query = qq(
213         SELECT *
214         FROM   virtualshelves
215         WHERE  shelfname=? AND owner=?
216     );
217     my $sth = $dbh->prepare($query);
218     $sth->execute($shelfname,$owner);
219     if ( $sth->rows ) {
220         return (-1);
221     }
222     else {
223         my $query = qq(
224             INSERT INTO virtualshelves
225                 (shelfname,owner,category)
226             VALUES (?,?,?)
227         );
228         $sth = $dbh->prepare($query);
229         $sth->execute( $shelfname, $owner, $category );
230         my $shelfnumber = $dbh->{'mysql_insertid'};
231         return ($shelfnumber);
232     }
233 }
234
235 =item AddToShelf
236
237   &AddToShelf($biblionumber, $shelfnumber);
238
239 Adds item number C<$biblionumber> to virtual virtualshelves number
240 C<$shelfnumber>, unless that item is already on that shelf.
241
242 =cut
243
244 #'
245 sub AddToShelf {
246     my ( $biblionumber, $shelfnumber ) = @_;
247     return unless $biblionumber;
248     my $query = qq(
249         SELECT *
250         FROM   virtualshelfcontents
251         WHERE  shelfnumber=? AND biblionumber=?
252     );
253     my $sth = $dbh->prepare($query);
254
255     $sth->execute( $shelfnumber, $biblionumber );
256     unless ( $sth->rows ) {
257         # already on shelf
258         my $query = qq(
259             INSERT INTO virtualshelfcontents
260                 (shelfnumber, biblionumber, flags)
261             VALUES
262                 (?, ?, 0)
263         );
264         $sth = $dbh->prepare($query);
265         $sth->execute( $shelfnumber, $biblionumber );
266     }
267 }
268
269 =item AddToShelfFromBiblio
270  
271     &AddToShelfFromBiblio($biblionumber, $shelfnumber)
272
273     this function allow to add a virtual into the shelf number $shelfnumber
274     from biblionumber.
275
276 =cut
277
278 sub AddToShelfFromBiblio {
279     my ( $biblionumber, $shelfnumber ) = @_;
280     return unless $biblionumber;
281     my $query = qq(
282         SELECT *
283         FROM   virtualshelfcontents
284         WHERE  shelfnumber=? AND biblionumber=?
285     );
286     my $sth = $dbh->prepare($query);
287     $sth->execute( $shelfnumber, $biblionumber );
288     unless ( $sth->rows ) {
289         my $query =qq(
290             INSERT INTO virtualshelfcontents
291                 (shelfnumber, biblionumber, flags)
292             VALUES
293                 (?, ?, 0)
294         );
295         $sth = $dbh->prepare($query);
296         $sth->execute( $shelfnumber, $biblionumber );
297     }
298 }
299
300 =item ModShelf
301
302 ModShelf($shelfnumber, $shelfname, $owner, $category )
303
304 Modify the value into virtualshelves table with values given on input arg.
305
306 =cut
307
308 sub ModShelf {
309     my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) = @_;
310     my $query = qq(
311         UPDATE virtualshelves
312         SET    shelfname=?,owner=?,category=?,sortfield=?
313         WHERE  shelfnumber=?
314     );
315         my $sth = $dbh->prepare($query);
316     $sth->execute( $shelfname, $owner, $category, $sortfield, $shelfnumber );
317 }
318
319 =item DelShelf
320
321   ($status) = &DelShelf($shelfnumber);
322
323 Deletes virtual virtualshelves number C<$shelfnumber>. The virtualshelves must
324 be empty.
325
326 Returns a two-element array, where C<$status> is 0 if the operation
327 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
328 success, or an error message giving the reason for failure.
329
330 =cut
331
332
333 =item ShelfPossibleAction
334
335 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
336
337 C<$loggedinuser,$shelfnumber,$action>
338
339 $action can be "view" or "manage".
340
341 Returns 1 if the user can do the $action in the $shelfnumber shelf.
342 Returns 0 otherwise.
343
344 =cut
345
346 sub ShelfPossibleAction {
347     my ( $user, $shelfnumber, $action ) = @_;
348     my $query = qq(
349         SELECT owner,category
350         FROM   virtualshelves
351         WHERE  shelfnumber=?
352     );
353     my $sth = $dbh->prepare($query);
354     $sth->execute($shelfnumber);
355     my ( $owner, $category ) = $sth->fetchrow;
356     return 1 if (($category >= 3 or $owner eq $user) && $action eq 'manage' );
357     return 1 if (($category >= 2 or $owner eq $user) && $action eq 'view' );
358     return 0;
359 }
360
361 =item DelFromShelf
362
363   &DelFromShelf( $biblionumber, $shelfnumber);
364
365 Removes item number C<$biblionumber> from virtual virtualshelves number
366 C<$shelfnumber>. If the item wasn't on that virtualshelves to begin with,
367 nothing happens.
368
369 =cut
370
371 #'
372 sub DelFromShelf {
373     my ( $biblionumber, $shelfnumber ) = @_;
374     my $query = qq(
375         DELETE FROM virtualshelfcontents
376         WHERE  shelfnumber=? AND biblionumber=?
377     );
378     my $sth = $dbh->prepare($query);
379     $sth->execute( $shelfnumber, $biblionumber );
380 }
381
382 =head2 DelShelf
383
384   $Number = DelShelf($shelfnumber);
385
386     this function delete the shelf number, and all of it's content
387
388 =cut
389
390 #'
391 sub DelShelf {
392     my ( $shelfnumber ) = @_;
393         my $sth = $dbh->prepare("DELETE FROM virtualshelves WHERE shelfnumber=?");
394         $sth->execute($shelfnumber);
395         return 0;
396 }
397
398 END { }    # module clean-up code here (global destructor)
399
400 1;
401
402 __END__
403
404 =back
405
406 =head1 AUTHOR
407
408 Koha Developement team <info@koha.org>
409
410 =head1 SEE ALSO
411
412 C4::Circulation::Circ2(3)
413
414 =cut