e85f9204857c95df3c4040c8cb238840779c4503
[koha.git] / C4 / VirtualShelves.pm
1 package C4::VirtualShelves;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 use Carp;
24 use C4::Context;
25 use C4::Debug;
26 use C4::Members;
27
28 use constant SHELVES_MASTHEAD_MAX => 10; #number under Lists button in masthead
29 use constant SHELVES_COMBO_MAX => 10; #add to combo in search
30 use constant SHELVES_MGRPAGE_MAX => 20; #managing page
31 use constant SHELVES_POPUP_MAX => 40; #addbybiblio popup
32
33 use constant SHARE_INVITATION_EXPIRY_DAYS => 14; #two weeks to accept
34
35 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
36
37 BEGIN {
38     # set the version for version checking
39     $VERSION = 3.07.00.049;
40     require Exporter;
41     @ISA    = qw(Exporter);
42     @EXPORT = qw(
43     );
44         @EXPORT_OK = qw(
45             &ShelvesMax
46         );
47 }
48
49
50 =head1 NAME
51
52 C4::VirtualShelves - Functions for manipulating Koha virtual shelves
53
54 =head1 SYNOPSIS
55
56   use C4::VirtualShelves;
57
58 =head1 DESCRIPTION
59
60 This module provides functions for manipulating virtual shelves,
61 including creating and deleting virtual shelves, and adding and removing
62 bibs to and from virtual shelves.
63
64 =head1 FUNCTIONS
65
66 =head2 GetSomeShelfNames
67
68 Returns shelf names and numbers for Add to combo of search results and Lists button of OPAC header.
69
70 =cut
71
72 sub GetSomeShelfNames {
73     my ($owner, $purpose, $adding_allowed)= @_;
74     my ($bar, $pub, @params);
75     my $dbh = C4::Context->dbh;
76
77     my $bquery = 'SELECT vs.shelfnumber, vs.shelfname FROM virtualshelves vs ';
78     my $limit= ShelvesMax($purpose);
79
80     my $qry1= $bquery."WHERE vs.category=2 ";
81     $qry1.= "AND (allow_add=1 OR owner=?) " if $adding_allowed;
82     push @params, $owner||0 if $adding_allowed;
83     $qry1.= "ORDER BY vs.lastmodified DESC LIMIT $limit";
84
85     unless($adding_allowed && (!defined($owner) || $owner<=0)) {
86         #if adding items, user should be known
87         $pub= $dbh->selectall_arrayref($qry1,{Slice=>{}},@params);
88     }
89
90     if($owner) {
91         my $qry2= $bquery. qq{
92             LEFT JOIN virtualshelfshares sh ON sh.shelfnumber=vs.shelfnumber AND sh.borrowernumber=?
93             WHERE vs.category=1 AND (vs.owner=? OR sh.borrowernumber=?) };
94         @params=($owner,$owner,$owner);
95         $qry2.= "AND (allow_add=1 OR owner=?) " if $adding_allowed;
96         push @params, $owner if $adding_allowed;
97         $qry2.= "ORDER BY vs.lastmodified DESC ";
98         $qry2.= "LIMIT $limit";
99         $bar= $dbh->selectall_arrayref($qry2,{Slice=>{}},@params);
100     }
101
102     return ( { bartotal => $bar? scalar @$bar: 0, pubtotal => $pub? scalar @$pub: 0}, $pub, $bar);
103 }
104
105 =head2 ShelvesMax
106
107     $howmany= ShelvesMax($context);
108
109 Tells how much shelves are shown in which context.
110 POPUP refers to addbybiblionumber popup, MGRPAGE is managing page (in opac or
111 staff), COMBO refers to the Add to-combo of search results. MASTHEAD is the
112 main Koha toolbar with Lists button.
113
114 =cut
115
116 sub ShelvesMax {
117     my $which= shift;
118     return SHELVES_POPUP_MAX if $which eq 'POPUP';
119     return SHELVES_MGRPAGE_MAX if $which eq 'MGRPAGE';
120     return SHELVES_COMBO_MAX if $which eq 'COMBO';
121     return SHELVES_MASTHEAD_MAX if $which eq 'MASTHEAD';
122     return SHELVES_MASTHEAD_MAX;
123 }
124
125 =head2 HandleDelBorrower
126
127      HandleDelBorrower($borrower);
128
129 When a member is deleted (DelMember in Members.pm), you should call me first.
130 This routine deletes/moves lists and entries for the deleted member/borrower.
131 Lists owned by the borrower are deleted, but entries from the borrower to
132 other lists are kept.
133
134 =cut
135
136 sub HandleDelBorrower {
137     my ($borrower)= @_;
138     my $query;
139     my $dbh = C4::Context->dbh;
140
141     #Delete all lists and all shares of this borrower
142     #Consistent with the approach Koha uses on deleting individual lists
143     #Note that entries in virtualshelfcontents added by this borrower to
144     #lists of others will be handled by a table constraint: the borrower
145     #is set to NULL in those entries.
146     $query="DELETE FROM virtualshelves WHERE owner=?";
147     $dbh->do($query,undef,($borrower));
148
149     #NOTE:
150     #We could handle the above deletes via a constraint too.
151     #But a new BZ report 11889 has been opened to discuss another approach.
152     #Instead of deleting we could also disown lists (based on a pref).
153     #In that way we could save shared and public lists.
154     #The current table constraints support that idea now.
155     #This pref should then govern the results of other routines/methods such as
156     #Koha::Virtualshelf->new->delete too.
157 }
158
159 sub GetShelfCount {
160     my ($owner, $category) = @_;
161     my @params;
162     # Find out how many shelves total meet the submitted criteria...
163
164     my $dbh = C4::Context->dbh;
165     my $query = "SELECT count(*) FROM virtualshelves vs ";
166     if($category==1) {
167         $query.= qq{
168             LEFT JOIN virtualshelfshares sh ON sh.shelfnumber=vs.shelfnumber
169             AND sh.borrowernumber=?
170         WHERE category=1 AND (vs.owner=? OR sh.borrowernumber=?) };
171         @params= ($owner, $owner, $owner);
172     }
173     else {
174         $query.='WHERE category=2';
175         @params= ();
176     }
177     my $sth = $dbh->prepare($query);
178     $sth->execute(@params);
179     my ($total)= $sth->fetchrow;
180     return $total;
181 }
182
183 1;
184
185 __END__
186
187 =head1 AUTHOR
188
189 Koha Development Team <http://koha-community.org/>
190
191 =head1 SEE ALSO
192
193 C4::Circulation::Circ2(3)
194
195 =cut