1 package C4
::ItemCirculationAlertPreference
;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
23 use Carp
qw(carp croak);
27 # helper function for validating \%opts
30 for (qw(branchcode categorycode item_type notification)) {
31 exists($opts->{$_}) || croak
("'$_' is a required parameter.");
40 C4::ItemCirculationAlertPreference - manage preferences for sending alerts
46 use C4::ItemCirculationAlertPreference;
48 # a short-cut to reduce typing the long package name over and over again
49 my $preferences = 'C4::ItemCirculationAlertPreference';
51 Creating a restriction on sending messages:
53 my $pref = $preferences->create({
57 notification => 'CHECKOUT',
60 Removing a restriction on sending messages:
62 $preferences->delete({
66 notification => 'CHECKOUT',
71 This class is used to manage the preferences for when an alert may be sent. By
72 default, item circulation alerts are enabled for every B<branch>, B<patron
73 category> and B<item type>.
75 However, if you would like to prevent item circulation alerts from being sent
76 for any combination of these 3 variables, a preference can be inserted into the
77 C<item_circulation_alert_preferences> table to make that a policy.
85 =head3 C4::ItemCirculationAlertPreference->new(\%opts)
87 This is a constructor for an in-memory C4::ItemCirculationAlertPreference
88 object. The database is not affected by this method.
93 my ($class, $opts) = @_;
94 bless $opts => $class;
100 =head3 C4::ItemCirculationAlertPreference->create(\%opts)
102 This will find or create an item circulation alert preference. You must pass
103 it a B<branchcode>, B<categorycode>, B<item_type>, and B<notification>. Valid
104 values for these attributes are as follows:
114 category.categorycode
122 This can be "CHECKIN" or "CHECKOUT"
129 my ($class, $opts) = @_;
131 my $dbh = C4
::Context
->dbh;
132 my $prefs = $dbh->selectall_arrayref(
133 "SELECT id, branchcode, categorycode, item_type
134 FROM item_circulation_alert_preferences
138 AND notification = ?",
141 $opts->{categorycode
},
143 $opts->{notification
},
146 return $class->new($prefs->[0]);
148 my $success = $dbh->do(
149 "INSERT INTO item_circulation_alert_preferences
150 (branchcode, categorycode, item_type, notification) VALUES (?, ?, ?, ?)",
153 $opts->{categorycode
},
155 $opts->{notification
},
159 id
=> $dbh->last_insert_id(undef, undef, undef, undef),
160 branchcode
=> $opts->{branchcode
},
161 categorycode
=> $opts->{categorycode
},
162 item_type
=> $opts->{item_type
},
163 notification
=> $opts->{notification
},
165 return $class->new($self);
176 =head3 C4::ItemCirculationAlertPreference->delete(\%opts)
178 Delete an item circulation alert preference. You can delete by either passing
179 in an B<id> or passing in a B<branchcode>, B<categorycode>, B<item_type>
185 my ($class, $opts) = @_;
186 my $dbh = C4
::Context
->dbh;
189 "DELETE FROM item_circulation_alert_preferences WHERE id = ?",
196 "DELETE FROM item_circulation_alert_preferences
200 AND notification = ?";
205 $opts->{categorycode
},
207 $opts->{notification
},
215 =head3 C4::ItemCirculationAlertPreference->is_enabled_for(\%opts)
217 Based on the existing preferences in the C<item_circulation_alert_preferences>
218 table, can an alert be sent for the given B<branchcode>, B<categorycode>, and
223 my $alert = 'C4::ItemCirculationAlertPreference';
226 categorycode => 'IL',
230 if ($alert->is_enabled_for($conditions)) {
236 sub is_disabled_for
{
237 my ($class, $opts) = @_;
239 my $dbh = C4
::Context
->dbh;
241 # Does a preference exist to block this alert?
243 SELECT id
, branchcode
, categorycode
, item_type
, notification
244 FROM item_circulation_alert_preferences
245 WHERE
(branchcode
= ? OR branchcode
= '*')
246 AND
(categorycode
= ? OR categorycode
= '*')
247 AND
(item_type
= ? OR item_type
= '*')
248 AND
(notification
= ? OR notification
= '*')
251 my $preferences = $dbh->selectall_arrayref(
255 $opts->{categorycode
},
257 $opts->{notification
},
260 # If any preferences showed up, we are NOT enabled.
261 return @
$preferences;
265 my ($class, $opts) = @_;
266 return not $class->is_disabled_for($opts);
272 =head3 C4::ItemCirculationAlertPreference->find({ branchcode => $bc, notification => $type })
274 This method returns all the item circulation alert preferences for a given
275 branch and notification.
279 my @branch_prefs = C4::ItemCirculationAlertPreference->find({
281 notification => 'CHECKIN',
287 my ($class, $where) = @_;
288 my $dbh = C4
::Context
->dbh;
290 SELECT id
, branchcode
, categorycode
, item_type
, notification
291 FROM item_circulation_alert_preferences
292 WHERE branchcode
= ? AND notification
= ?
293 ORDER BY categorycode
, item_type
295 return map { $class->new($_) } @
{$dbh->selectall_arrayref(
298 $where->{branchcode
},
299 $where->{notification
},
306 =head3 C4::ItemCirculationAlertPreference->grid({ branchcode => $c, notification => $type })
308 Return a 2D arrayref for the grid view in F<admin/item_circulation_alert_preferences.pl>.
309 Each row represents a category (like 'Patron' or 'Young Adult') and
310 each column represents an itemtype (like 'Book' or 'Music').
312 Each cell contains...
317 my $grid = C4::ItemCirculationAlertPreference->grid({
319 notification => 'CHECKOUT',
323 See F<admin/item_circulation_alerts.pl> to see how this method is used.
328 my ($class, $where) = @_;
329 my @branch_prefs = $class->find($where);
330 my @default_prefs = $class->find({ branchcode
=> '*', notification
=> $where->{notification
} });
331 my @cc = C4
::Category
->all;
332 my @it = C4
::ItemType
->all;
333 my $notification = $where->{notification
};
335 my $key = $_->categorycode . "-" . $_->item_type . "-" . $notification;
340 my $key = $_->categorycode . "-" . $_->item_type . "-" . $notification;
346 my $row = { description
=> $c->description, items
=> [] };
349 my $key = $c->categorycode . "-" . $i->itemtype . "-" . $notification;
353 if ($disabled{$key}) {
354 push @classes, 'disabled';
355 $text = "Disabled for $where->{branchcode}";
357 if ($default{$key}) {
358 push @classes, 'default';
359 $text = "Disabled for all";
361 push @
{$row->{items
}}, {
362 class => join(' ', @classes),
374 =head2 Object Methods
376 These are read-only accessors for the various attributes of a preference.
382 =head3 $pref->branchcode
386 =head3 $pref->categorycode
390 =head3 $pref->item_type
394 =head3 $pref->notification
400 my $attr = $AUTOLOAD;
402 if (exists $self->{$attr}) {
403 return $self->{$attr};
415 L<C4::Circulation>, F<admin/item_circulation_alerts.pl>
419 John Beppu <john.beppu@liblime.com>