Bug 14544: Get rid of GetAllShelves
[koha.git] / C4 / Utils / DataTables / VirtualShelves.pm
1 package C4::Utils::DataTables::VirtualShelves;
2
3 use Modern::Perl;
4 use C4::Branch qw/onlymine/;
5 use C4::Context;
6 use C4::Members qw/GetMemberIssuesAndFines/;
7 use C4::Utils::DataTables;
8 use C4::VirtualShelves;
9
10 sub search {
11     my ( $params ) = @_;
12     my $shelfname = $params->{shelfname};
13     my $count = $params->{count};
14     my $owner = $params->{owner};
15     my $sortby = $params->{sortby};
16     my $type = $params->{type};
17     my $dt_params = $params->{dt_params};
18
19     # public is default
20     $type = 2 if not $type or $type != 1;
21
22     # If not logged in user, be carreful and set the borrowernumber to 0
23     # to prevent private lists lack
24     my $loggedinuser = C4::Context->userenv->{'number'} || 0;
25
26     my ($iTotalRecords, $iTotalDisplayRecords);
27
28     my $dbh = C4::Context->dbh;
29
30     # FIXME refactore the following queries
31     # We should call Koha::Virtualshelves
32     my $select = q|
33         SELECT vs.shelfnumber, vs.shelfname, vs.owner, vs.category AS type,
34         vs.created_on, vs.lastmodified as modification_time,
35         bo.surname, bo.firstname, vs.sortfield as sortby,
36         count(vc.biblionumber) as count
37     |;
38
39     my $from_total = q|
40         FROM virtualshelves vs
41         LEFT JOIN borrowers bo ON vs.owner=bo.borrowernumber
42     |;
43
44     my $from = $from_total . q|
45         LEFT JOIN virtualshelfcontents vc USING( shelfnumber )
46     |;
47
48     my @args;
49     # private
50     if ( $type == 1 ) {
51         my $join_vs .= q|
52             LEFT JOIN virtualshelfshares sh ON sh.shelfnumber = vs.shelfnumber
53             AND sh.borrowernumber = ?
54         |;
55         $from .= $join_vs;
56         $from_total .= $join_vs;
57         push @args, $loggedinuser;
58
59     }
60
61     my @where_strs;
62
63     if ( defined $shelfname and $shelfname ne '' ) {
64         push @where_strs, 'shelfname LIKE ?';
65         push @args, "%$shelfname%";
66     }
67     if ( defined $owner and $owner ne '' ) {
68         push @where_strs, '( bo.firstname LIKE ? OR bo.surname LIKE ? )';
69         push @args, "%$owner%", "%$owner%";
70     }
71     if ( defined $sortby and $sortby ne '' ) {
72         push @where_strs, 'sortfield = ?';
73         push @args, $sortby;
74     }
75
76     push @where_strs, 'category = ?';
77     push @args, $type;
78
79     if ( $type == 1 ) {
80         push @where_strs, '(vs.owner = ? OR sh.borrowernumber = ?)';
81         push @args, $loggedinuser, $loggedinuser;
82     }
83
84     my $where;
85     $where = " WHERE " . join (" AND ", @where_strs) if @where_strs;
86     my $orderby = dt_build_orderby($dt_params);
87     $orderby =~ s|shelfnumber|vs.shelfnumber| if $orderby;
88
89     my $limit;
90     # If iDisplayLength == -1, we want to display all shelves
91     if ( $dt_params->{iDisplayLength} > -1 ) {
92         # In order to avoid sql injection
93         $dt_params->{iDisplayStart} =~ s/\D//g;
94         $dt_params->{iDisplayLength} =~ s/\D//g;
95         $dt_params->{iDisplayStart} //= 0;
96         $dt_params->{iDisplayLength} //= 20;
97         $limit = "LIMIT $dt_params->{iDisplayStart},$dt_params->{iDisplayLength}";
98     }
99
100     my $group_by = " GROUP BY vs.shelfnumber";
101
102     my $query = join(
103         " ",
104         $select,
105         $from,
106         ($where ? $where : ""),
107         $group_by,
108         ($orderby ? $orderby : ""),
109         ($limit ? $limit : "")
110     );
111     my $shelves = $dbh->selectall_arrayref( $query, { Slice => {} }, @args );
112
113     # Get the iTotalDisplayRecords DataTable variable
114     $query = "SELECT COUNT(vs.shelfnumber) " . $from_total . ($where ? $where : "");
115     ($iTotalDisplayRecords) = $dbh->selectrow_array( $query, undef, @args );
116
117     # Get the iTotalRecords DataTable variable
118     $query = q|SELECT COUNT(vs.shelfnumber)| . $from_total . q| WHERE category = ?|;
119     $query .= q| AND (vs.owner = ? OR sh.borrowernumber = ?)| if $type == 1;
120     @args = $type == 1 ? ( $loggedinuser, $type, $loggedinuser, $loggedinuser ) : ( $type );
121     ( $iTotalRecords ) = $dbh->selectrow_array( $query, undef, @args );
122
123     for my $shelf ( @$shelves ) {
124         $shelf->{can_manage_shelf} = C4::VirtualShelves::ShelfPossibleAction( $loggedinuser, $shelf->{shelfnumber}, 'manage' );
125         $shelf->{can_delete_shelf} = C4::VirtualShelves::ShelfPossibleAction( $loggedinuser, $shelf->{shelfnumber}, 'delete_shelf' );
126     }
127     return {
128         iTotalRecords => $iTotalRecords,
129         iTotalDisplayRecords => $iTotalDisplayRecords,
130         shelves => $shelves,
131     }
132 }
133
134 1;
135 __END__
136
137 =head1 NAME
138
139 C4::Utils::DataTables::VirtualShelves - module for using DataTables with virtual shelves
140
141 =head1 SYNOPSIS
142
143 This module provides routines used by the virtual shelves search
144
145 =head2 FUNCTIONS
146
147 =head3 search
148
149     my $dt_infos = C4::Utils::DataTables::VirtualShelves->search($params);
150
151 $params is a hashref with some keys:
152
153 =over 4
154
155 =item shelfname
156
157 =item count
158
159 =item sortby
160
161 =item type
162
163 =item dt_params
164
165 =cut
166
167 =back
168
169 =head1 LICENSE
170
171 This file is part of Koha.
172
173 Copyright 2014 BibLibre
174
175 Koha is free software; you can redistribute it and/or modify it
176 under the terms of the GNU General Public License as published by
177 the Free Software Foundation; either version 3 of the License, or
178 (at your option) any later version.
179
180 Koha is distributed in the hope that it will be useful, but
181 WITHOUT ANY WARRANTY; without even the implied warranty of
182 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
183 GNU General Public License for more details.
184
185 You should have received a copy of the GNU General Public License
186 along with Koha; if not, see <http://www.gnu.org/licenses>.