Lines 1-330
Link Here
|
1 |
package Koha::Signs; |
1 |
package Koha::Signs; |
2 |
|
2 |
|
3 |
use C4::Context; |
3 |
# Copyright 2016 Aleisha Amohia <aleisha@catalyst.net.nz> |
4 |
use C4::Biblio; |
4 |
# |
5 |
use C4::Koha; |
5 |
# This file is part of Koha. |
6 |
use C4::Items; |
6 |
# |
7 |
use Modern::Perl; |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
|
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
use base qw( Exporter ); |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
|
10 |
# version. |
11 |
# set the version for version checking |
11 |
# |
12 |
our @EXPORT = qw( |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
|
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
AddSignStream |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
ModSignStream |
15 |
# |
16 |
GetSignStream |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
GetSignStreams |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
DelSignStream |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
|
|
20 |
GetSignStreamRecords |
21 |
|
22 |
AddSign |
23 |
ModSign |
24 |
GetSign |
25 |
GetSigns |
26 |
DelSign |
27 |
|
28 |
AttachSignStreamToSign |
29 |
DetachSignStreamFromSign |
30 |
GetSignStreamsAttachedToSign |
31 |
GetSignStreamsAttachedToSignWithRecords |
32 |
|
33 |
ModSignStreamParams |
34 |
GetSignStreamParams |
35 |
); |
36 |
|
37 |
my $dbh = C4::Context->dbh; |
38 |
|
39 |
# Streams |
40 |
|
41 |
# Returns id of new sign_stream |
42 |
sub AddSignStream { |
43 |
|
44 |
my ( $name, $report ) = @_; |
45 |
|
46 |
my $sth=$dbh->prepare("INSERT INTO sign_streams SET name = ?, saved_sql_id = ?"); |
47 |
$sth->execute( $name, $report ); |
48 |
return $dbh->last_insert_id( undef, undef, 'sign_streams', 'sign_stream_id' ); |
49 |
|
50 |
} |
51 |
|
52 |
sub ModSignStream { |
53 |
|
54 |
my ( $name, $report, $sign_stream_id ) = @_; |
55 |
|
56 |
my $sth = $dbh->prepare("UPDATE sign_streams SET name = ?, saved_sql_id = ? WHERE sign_stream_id = ?"); |
57 |
return $sth->execute( $name, $report, $sign_stream_id ); |
58 |
|
59 |
} |
60 |
|
61 |
sub GetSignStream { |
62 |
|
63 |
my ( $sign_id ) = @_; |
64 |
|
65 |
return unless $sign_id; |
66 |
|
67 |
my $query = "SELECT s.*, sq.report_name, sq.savedsql |
68 |
FROM sign_streams AS s, saved_sql AS sq |
69 |
WHERE s.saved_sql_id = sq.id |
70 |
AND s.sign_stream_id = ?"; |
71 |
my $sth = $dbh->prepare($query); |
72 |
$sth->execute($sign_id); |
73 |
return $sth->fetchrow_hashref(); |
74 |
|
75 |
} |
76 |
|
77 |
sub GetSignStreams { |
78 |
|
79 |
my $query = "SELECT s.*, sq.report_name, sq.savedsql |
80 |
FROM sign_streams AS s, saved_sql AS sq |
81 |
WHERE s.saved_sql_id = sq.id |
82 |
ORDER BY s.name"; |
83 |
my $sth = $dbh->prepare($query); |
84 |
$sth->execute(); |
85 |
return $sth->fetchall_arrayref({}); |
86 |
|
87 |
} |
88 |
|
89 |
sub DelSignStream { |
90 |
|
91 |
my ( $sign_stream_id ) = @_; |
92 |
|
93 |
return unless $sign_stream_id; |
94 |
|
95 |
my $sth = $dbh->prepare('DELETE FROM sign_streams WHERE sign_stream_id = ?'); |
96 |
return $sth->execute($sign_stream_id); |
97 |
|
98 |
} |
99 |
|
100 |
sub GetSignStreamRecords { |
101 |
my ( $stream, $sign_to_stream_id) = @_; |
102 |
my $sql = $stream->{'savedsql'}; |
103 |
|
104 |
if ( defined $sign_to_stream_id ) { |
105 |
my $params = GetSignStreamParams( $sign_to_stream_id ); |
106 |
$sql = ReplaceParamsInSQL( $sql, $params ); |
107 |
return undef if ( $sql =~ m/<</ ); |
108 |
} |
109 |
|
110 |
return {result => RunSQL( $sql ), sql => $sql }; |
111 |
} |
112 |
|
113 |
# Signs |
114 |
|
115 |
# Returns id of new sign |
116 |
sub AddSign { |
117 |
|
118 |
my ( $name, $webapp, $swatch, $transition, $idleafter, $pagedelay, $reloadafter ) = @_; |
119 |
|
120 |
my $sth=$dbh->prepare("INSERT INTO signs SET name = ?, webapp = ?, swatch = ?, transition = ?, idleafter = ?, pagedelay = ?, reloadafter = ?"); |
121 |
$sth->execute( $name, $webapp, $swatch, $transition, $idleafter, $pagedelay, $reloadafter ); |
122 |
return $dbh->last_insert_id( undef, undef, 'signs', 'sign_id' ); |
123 |
|
124 |
} |
125 |
|
126 |
sub ModSign { |
127 |
|
128 |
my ( $name, $webapp, $swatch, $transition, $idleafter, $pagedelay, $reloadafter, $sign_id ) = @_; |
129 |
|
130 |
my $sth = $dbh->prepare("UPDATE signs SET name = ?, webapp = ?, swatch = ?, transition = ?, idleafter = ?, pagedelay = ?, reloadafter = ? WHERE sign_id = ?"); |
131 |
return $sth->execute( $name, $webapp, $swatch, $transition, $idleafter, $pagedelay, $reloadafter, $sign_id ); |
132 |
|
133 |
} |
134 |
|
135 |
sub GetSign { |
136 |
|
137 |
my ( $sign_id ) = @_; |
138 |
|
139 |
return unless $sign_id; |
140 |
|
141 |
my $query = "SELECT * |
142 |
FROM signs |
143 |
WHERE sign_id = ?"; |
144 |
my $sth = $dbh->prepare($query); |
145 |
$sth->execute($sign_id); |
146 |
return $sth->fetchrow_hashref(); |
147 |
|
19 |
|
148 |
} |
20 |
use Modern::Perl; |
149 |
|
|
|
150 |
sub GetSigns { |
151 |
|
152 |
my $query = "SELECT * |
153 |
FROM signs |
154 |
ORDER BY name"; |
155 |
my $sth = $dbh->prepare($query); |
156 |
$sth->execute(); |
157 |
return $sth->fetchall_arrayref({}); |
158 |
|
159 |
} |
160 |
|
161 |
sub DelSign { |
162 |
|
163 |
my ( $sign_id ) = @_; |
164 |
|
165 |
return unless $sign_id; |
166 |
|
167 |
my $sth = $dbh->prepare('DELETE FROM signs WHERE sign_id = ?'); |
168 |
return $sth->execute($sign_id); |
169 |
|
170 |
} |
171 |
|
172 |
# Streams attached to signs |
173 |
|
174 |
# Returns the ID of the connection between sign and stream |
175 |
sub AttachSignStreamToSign { |
176 |
|
177 |
my ( $sign_stream_id, $sign_id ) = @_; |
178 |
|
179 |
return unless $sign_stream_id || $sign_id; |
180 |
|
181 |
my $sth = $dbh->prepare( 'INSERT INTO signs_to_streams SET sign_stream_id = ?, sign_id = ?' ); |
182 |
$sth->execute( $sign_stream_id, $sign_id ); |
183 |
return $dbh->last_insert_id( undef, undef, 'signs_to_streams', 'sign_to_stream_id' ); |
184 |
|
185 |
} |
186 |
|
187 |
sub ModSignStreamParams { |
188 |
|
189 |
my ( $sign_to_stream_id, $params ) = @_; |
190 |
return unless $sign_to_stream_id; |
191 |
my $sth = $dbh->prepare( 'UPDATE signs_to_streams SET params = ? WHERE sign_to_stream_id = ?' ); |
192 |
return $sth->execute( $params, $sign_to_stream_id ); |
193 |
|
194 |
} |
195 |
|
196 |
# Returns the string of params for a given stream-attached-to-sign |
197 |
sub GetSignStreamParams { |
198 |
|
199 |
my ( $sign_to_stream_id ) = @_; |
200 |
|
201 |
return unless $sign_to_stream_id; |
202 |
|
203 |
my $query = 'SELECT params FROM signs_to_streams WHERE sign_to_stream_id = ?'; |
204 |
my $sth = $dbh->prepare( $query ); |
205 |
$sth->execute( $sign_to_stream_id ); |
206 |
return $sth->fetchrow_array(); |
207 |
|
208 |
} |
209 |
|
210 |
sub ReplaceParamsInSQL { |
211 |
|
212 |
my ( $sql, $params ) = @_; |
213 |
|
214 |
return unless $sql || $params; |
215 |
return $sql unless $sql =~ m/<</; |
216 |
|
217 |
foreach my $param ( split /&/, $params ) { |
218 |
my ( $key, $value ) = split /=/, $param; |
219 |
# FIXME Handle spaces |
220 |
$sql =~ s/<<$key>>/$value/g; |
221 |
} |
222 |
return $sql; |
223 |
|
224 |
} |
225 |
|
226 |
sub GetSignStreamsAttachedToSign { |
227 |
|
228 |
my ( $sign_id ) = @_; |
229 |
|
230 |
return unless $sign_id; |
231 |
|
232 |
my $query = 'SELECT s.*, sts.sign_to_stream_id, sts.params, sq.report_name, sq.savedsql |
233 |
FROM sign_streams AS s, signs_to_streams AS sts, saved_sql AS sq |
234 |
WHERE s.sign_stream_id = sts.sign_stream_id |
235 |
AND s.saved_sql_id = sq.id |
236 |
AND sts.sign_id = ? |
237 |
ORDER BY s.name'; |
238 |
my $sth = $dbh->prepare( $query ); |
239 |
$sth->execute( $sign_id ); |
240 |
return $sth->fetchall_arrayref({}); |
241 |
|
242 |
} |
243 |
|
244 |
sub GetSignStreamsAttachedToSignWithRecords { |
245 |
|
246 |
my ( $sign_id, $include_marc ) = @_; |
247 |
|
248 |
return unless $sign_id; |
249 |
|
250 |
my $streams = GetSignStreamsAttachedToSign( $sign_id ); |
251 |
my @changedstreams; |
252 |
my %record_cache = (); |
253 |
|
254 |
# Add records to the streams |
255 |
foreach my $stream ( @{$streams} ) { |
256 |
|
257 |
my $records = RunSQL( ReplaceParamsInSQL( $stream->{'savedsql'}, $stream->{'params'} ) ); |
258 |
|
259 |
if ( $include_marc ) { |
260 |
|
261 |
my @processed_records; |
262 |
foreach my $rec ( @{$records} ) { |
263 |
unless(defined $record_cache{$rec->{'biblionumber'}}) { |
264 |
my $marc = GetMarcBiblio( $rec->{'biblionumber'} ); |
265 |
if ( ! $marc ) { |
266 |
next; |
267 |
} |
268 |
|
269 |
my $isbn = GetNormalizedISBN( undef, $marc, C4::Context->preference("marcflavor") ) || ''; |
270 |
$rec->{'isbn'} = $isbn; |
271 |
|
21 |
|
272 |
my $dat = GetBiblioData($rec->{'biblionumber'}); |
22 |
use Koha::Database; |
273 |
my @items = GetItemsInfo($rec->{'biblionumber'}); |
23 |
use Koha::Sign; |
274 |
foreach my $item ( @items ) { |
|
|
275 |
my $shelflocations = GetKohaAuthorisedValues('items.location',$dat->{'frameworkcode'}, 'opac'); |
276 |
if ( defined $item->{'location'} ) { |
277 |
$item->{'location_description'} = $shelflocations->{ $item->{'location'} }; |
278 |
} |
279 |
} |
280 |
$rec->{'items'} = \@items; |
281 |
foreach ( keys %{$dat} ) { |
282 |
$rec->{"$_"} = defined $dat->{$_} ? $dat->{$_} : ''; |
283 |
} |
284 |
|
24 |
|
285 |
$rec->{'marc'} = $marc; |
25 |
use base qw( Koha::Objects ); |
286 |
|
26 |
|
287 |
$record_cache{$rec->{'biblionumber'}} = $rec; |
27 |
=head1 NAME |
288 |
} |
|
|
289 |
push @processed_records, $record_cache{$rec->{'biblionumber'}}; |
290 |
} |
291 |
$stream->{'records'} = \@processed_records; |
292 |
|
28 |
|
293 |
} else { |
29 |
Koha::Signs - Koha Sign object set class |
294 |
|
30 |
|
295 |
$stream->{'records'} = $records; |
31 |
=head1 API |
296 |
|
32 |
|
297 |
} |
33 |
=head2 Class Methods |
298 |
|
34 |
|
299 |
push @changedstreams, $stream; |
35 |
=cut |
300 |
|
36 |
|
301 |
} |
37 |
=head3 type |
302 |
|
38 |
|
303 |
return \@changedstreams; |
39 |
=cut |
304 |
|
40 |
|
|
|
41 |
sub _type { |
42 |
return 'Sign'; |
305 |
} |
43 |
} |
306 |
|
44 |
|
307 |
sub DetachSignStreamFromSign { |
45 |
=head3 object_class |
308 |
|
|
|
309 |
my ( $sign_to_stream_id ) = @_; |
310 |
|
311 |
return unless $sign_to_stream_id; |
312 |
|
46 |
|
313 |
my $sth = $dbh->prepare( 'DELETE FROM signs_to_streams WHERE sign_to_stream_id = ?' ); |
47 |
=cut |
314 |
return $sth->execute( $sign_to_stream_id ); |
|
|
315 |
|
48 |
|
|
|
49 |
sub object_class { |
50 |
return 'Koha::Sign'; |
316 |
} |
51 |
} |
317 |
|
52 |
|
318 |
sub RunSQL { |
53 |
=head1 AUTHOR |
319 |
|
54 |
|
320 |
my ( $query ) = @_; |
55 |
Aleisha Amohia <aleisha@catalyst.net.nz> |
321 |
|
56 |
|
322 |
return unless $query; |
57 |
=cut |
323 |
|
|
|
324 |
my $sth = $dbh->prepare($query); |
325 |
$sth->execute(); |
326 |
return $sth->fetchall_arrayref({}); |
327 |
|
328 |
} |
329 |
|
58 |
|
330 |
1; |
59 |
1; |