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