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