-
Notifications
You must be signed in to change notification settings - Fork 257
/
ItemCirculationAlertPreference.pm
426 lines (315 loc) · 10.1 KB
/
ItemCirculationAlertPreference.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package C4::ItemCirculationAlertPreference;
# Copyright Liblime 2009
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use strict;
use warnings;
use C4::Context;
use Carp qw( carp croak );
use Koha::ItemTypes;
use Koha::Patron::Categories;
our $AUTOLOAD;
# helper function for validating \%opts
our $valid = sub {
my $opts = shift;
for (qw(branchcode categorycode item_type notification)) {
exists($opts->{$_}) || croak("'$_' is a required parameter.");
}
};
=head1 NAME
C4::ItemCirculationAlertPreference - manage preferences for sending alerts
=head1 SYNOPSIS
Basics:
use C4::ItemCirculationAlertPreference;
# a short-cut to reduce typing the long package name over and over again
my $preferences = 'C4::ItemCirculationAlertPreference';
Creating a restriction on sending messages:
my $pref = $preferences->create({
branchcode => 'CPL',
categorycode => 'YA',
item_type => 'BK',
notification => 'CHECKOUT',
});
Removing a restriction on sending messages:
$preferences->delete({
branchcode => 'CPL',
categorycode => 'YA',
item_type => 'BK',
notification => 'CHECKOUT',
});
=head1 DESCRIPTION
This class is used to manage the preferences for when an alert may be sent. By
default, item circulation alerts are enabled for every B<branch>, B<patron
category> and B<item type>.
However, if you would like to prevent item circulation alerts from being sent
for any combination of these 3 variables, a preference can be inserted into the
C<item_circulation_alert_preferences> table to make that a policy.
=head1 API
=head2 Class Methods
=cut
=head3 C4::ItemCirculationAlertPreference->new(\%opts)
This is a constructor for an in-memory C4::ItemCirculationAlertPreference
object. The database is not affected by this method.
=cut
sub new {
my ($class, $opts) = @_;
bless $opts => $class;
}
=head3 C4::ItemCirculationAlertPreference->create(\%opts)
This will find or create an item circulation alert preference. You must pass
it a B<branchcode>, B<categorycode>, B<item_type>, and B<notification>. Valid
values for these attributes are as follows:
=over 4
=item branchcode
branches.branchcode
=item categorycode
category.categorycode
=item item_type
itemtypes.itemtype
=item notification
This can be "CHECKIN" or "CHECKOUT"
=back
=cut
sub create {
my ($class, $opts) = @_;
$valid->($opts);
my $dbh = C4::Context->dbh;
my $prefs = $dbh->selectall_arrayref(
"SELECT id, branchcode, categorycode, item_type
FROM item_circulation_alert_preferences
WHERE branchcode = ?
AND categorycode = ?
AND item_type = ?
AND notification = ?",
{ Slice => {} },
$opts->{branchcode},
$opts->{categorycode},
$opts->{item_type},
$opts->{notification},
);
if (@$prefs) {
return $class->new($prefs->[0]);
} else {
my $success = $dbh->do(
"INSERT INTO item_circulation_alert_preferences
(branchcode, categorycode, item_type, notification) VALUES (?, ?, ?, ?)",
{},
$opts->{branchcode},
$opts->{categorycode},
$opts->{item_type},
$opts->{notification},
);
if ($success) {
my $self = {
id => $dbh->last_insert_id(undef, undef, undef, undef),
branchcode => $opts->{branchcode},
categorycode => $opts->{categorycode},
item_type => $opts->{item_type},
notification => $opts->{notification},
};
return $class->new($self);
} else {
carp $dbh->errstr;
return;
}
}
}
=head3 C4::ItemCirculationAlertPreference->delete(\%opts)
Delete an item circulation alert preference. You can delete by either passing
in an B<id> or passing in a B<branchcode>, B<categorycode>, B<item_type>
triplet.
=cut
sub delete {
my ($class, $opts) = @_;
my $dbh = C4::Context->dbh;
if ($opts->{id}) {
$dbh->do(
"DELETE FROM item_circulation_alert_preferences WHERE id = ?",
{},
$opts->{id}
);
} else {
$valid->($opts);
my $sql =
"DELETE FROM item_circulation_alert_preferences
WHERE branchcode = ?
AND categorycode = ?
AND item_type = ?
AND notification = ?";
$dbh->do(
$sql,
{},
$opts->{branchcode},
$opts->{categorycode},
$opts->{item_type},
$opts->{notification},
);
}
}
=head3 C4::ItemCirculationAlertPreference->is_enabled_for(\%opts)
Based on the existing preferences in the C<item_circulation_alert_preferences>
table, can an alert be sent for the given B<branchcode>, B<categorycode>, and
B<itemtype>?
B<Example>:
my $alert = 'C4::ItemCirculationAlertPreference';
my $conditions = {
branchcode => 'CPL',
categorycode => 'IL',
item_type => 'MU',
};
if ($alert->is_enabled_for($conditions)) {
# ...
}
=cut
sub is_disabled_for {
my ($class, $opts) = @_;
$valid->($opts);
my $dbh = C4::Context->dbh;
# Does a preference exist to block this alert?
my $query = qq{
SELECT id, branchcode, categorycode, item_type, notification
FROM item_circulation_alert_preferences
WHERE (branchcode = ? OR branchcode = '*')
AND (categorycode = ? OR categorycode = '*')
AND (item_type = ? OR item_type = '*')
AND (notification = ? OR notification = '*')
};
my $preferences = $dbh->selectall_arrayref(
$query,
{ Slice => {} },
$opts->{branchcode},
$opts->{categorycode},
$opts->{item_type},
$opts->{notification},
);
# If any preferences showed up, we are NOT enabled.
return @$preferences;
}
sub is_enabled_for {
my ($class, $opts) = @_;
return not $class->is_disabled_for($opts);
}
=head3 C4::ItemCirculationAlertPreference->find({ branchcode => $bc, notification => $type })
This method returns all the item circulation alert preferences for a given
branch and notification.
B<Example>:
my @branch_prefs = C4::ItemCirculationAlertPreference->find({
branchcode => 'CPL',
notification => 'CHECKIN',
});
=cut
sub find {
my ($class, $where) = @_;
my $dbh = C4::Context->dbh;
my $query = qq{
SELECT id, branchcode, categorycode, item_type, notification
FROM item_circulation_alert_preferences
WHERE branchcode = ? AND notification = ?
ORDER BY categorycode, item_type
};
return map { $class->new($_) } @{$dbh->selectall_arrayref(
$query,
{ Slice => {} },
$where->{branchcode},
$where->{notification},
)};
}
=head3 C4::ItemCirculationAlertPreference->grid({ branchcode => $c, notification => $type })
Return a 2D arrayref for the grid view in F<admin/item_circulation_alert_preferences.pl>.
Each row represents a category (like 'Patron' or 'Young Adult') and
each column represents an itemtype (like 'Book' or 'Music').
Each cell contains...
B<Example>:
use Data::Dump 'pp';
my $grid = C4::ItemCirculationAlertPreference->grid({
branchcode => 'CPL',
notification => 'CHECKOUT',
});
warn pp($grid);
See F<admin/item_circulation_alerts.pl> to see how this method is used.
=cut
sub grid {
my ($class, $where) = @_;
my @branch_prefs = $class->find($where);
my @default_prefs = $class->find({ branchcode => '*', notification => $where->{notification} });
my @cc = Koha::Patron::Categories->search_with_library_limits->as_list;
my @it = Koha::ItemTypes->search->as_list;
my $notification = $where->{notification};
my %disabled = map {
my $key = $_->categorycode . "-" . $_->item_type . "-" . $notification;
$key =~ s/\*/_/g;
($key => 1);
} @branch_prefs;
my %default = map {
my $key = $_->categorycode . "-" . $_->item_type . "-" . $notification;
$key =~ s/\*/_/g;
($key => 1);
} @default_prefs;
my @grid;
for my $c (@cc) {
my $row = { description => $c->description, items => [] };
push @grid, $row;
for my $i (@it) {
my $key = $c->categorycode . "-" . $i->itemtype . "-" . $notification;
$key =~ s/\*/_/g;
my @classes;
my $text = " ";
if ($disabled{$key}) {
push @classes, 'disabled';
$text = "Disabled for $where->{branchcode}";
}
if ($default{$key}) {
push @classes, 'default';
$text = "Disabled for all";
}
push @{$row->{items}}, {
class => join(' ', @classes),
id => $key,
text => $text,
};
}
}
return \@grid;
}
=head2 Object Methods
These are read-only accessors for the various attributes of a preference.
=head3 $pref->id
=cut
=head3 $pref->branchcode
=cut
=head3 $pref->categorycode
=cut
=head3 $pref->item_type
=cut
=head3 $pref->notification
=cut
sub AUTOLOAD {
my $self = shift;
my $attr = $AUTOLOAD;
$attr =~ s/.*://;
if (exists $self->{$attr}) {
return $self->{$attr};
} else {
return;
}
}
sub DESTROY { }
=head1 SEE ALSO
L<C4::Circulation>, F<admin/item_circulation_alerts.pl>
=head1 AUTHOR
John Beppu <[email protected]>
=cut
1;