1 package C4
::ItemCirculationAlertPreference
;
3 # Copyright Liblime 2009
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 use Carp
qw(carp croak);
29 # helper function for validating \%opts
32 for (qw(branchcode categorycode item_type notification)) {
33 exists($opts->{$_}) || croak
("'$_' is a required parameter.");
42 C4::ItemCirculationAlertPreference - manage preferences for sending alerts
48 use C4::ItemCirculationAlertPreference;
50 # a short-cut to reduce typing the long package name over and over again
51 my $preferences = 'C4::ItemCirculationAlertPreference';
53 Creating a restriction on sending messages:
55 my $pref = $preferences->create({
59 notification => 'CHECKOUT',
62 Removing a restriction on sending messages:
64 $preferences->delete({
68 notification => 'CHECKOUT',
73 This class is used to manage the preferences for when an alert may be sent. By
74 default, item circulation alerts are enabled for every B<branch>, B<patron
75 category> and B<item type>.
77 However, if you would like to prevent item circulation alerts from being sent
78 for any combination of these 3 variables, a preference can be inserted into the
79 C<item_circulation_alert_preferences> table to make that a policy.
87 =head3 C4::ItemCirculationAlertPreference->new(\%opts)
89 This is a constructor for an in-memory C4::ItemCirculationAlertPreference
90 object. The database is not affected by this method.
95 my ($class, $opts) = @_;
96 bless $opts => $class;
102 =head3 C4::ItemCirculationAlertPreference->create(\%opts)
104 This will find or create an item circulation alert preference. You must pass
105 it a B<branchcode>, B<categorycode>, B<item_type>, and B<notification>. Valid
106 values for these attributes are as follows:
116 category.categorycode
124 This can be "CHECKIN" or "CHECKOUT"
131 my ($class, $opts) = @_;
133 my $dbh = C4
::Context
->dbh;
134 my $prefs = $dbh->selectall_arrayref(
135 "SELECT id, branchcode, categorycode, item_type
136 FROM item_circulation_alert_preferences
140 AND notification = ?",
143 $opts->{categorycode
},
145 $opts->{notification
},
148 return $class->new($prefs->[0]);
150 my $success = $dbh->do(
151 "INSERT INTO item_circulation_alert_preferences
152 (branchcode, categorycode, item_type, notification) VALUES (?, ?, ?, ?)",
155 $opts->{categorycode
},
157 $opts->{notification
},
161 id
=> $dbh->last_insert_id(undef, undef, undef, undef),
162 branchcode
=> $opts->{branchcode
},
163 categorycode
=> $opts->{categorycode
},
164 item_type
=> $opts->{item_type
},
165 notification
=> $opts->{notification
},
167 return $class->new($self);
178 =head3 C4::ItemCirculationAlertPreference->delete(\%opts)
180 Delete an item circulation alert preference. You can delete by either passing
181 in an B<id> or passing in a B<branchcode>, B<categorycode>, B<item_type>
187 my ($class, $opts) = @_;
188 my $dbh = C4
::Context
->dbh;
191 "DELETE FROM item_circulation_alert_preferences WHERE id = ?",
198 "DELETE FROM item_circulation_alert_preferences
202 AND notification = ?";
207 $opts->{categorycode
},
209 $opts->{notification
},
217 =head3 C4::ItemCirculationAlertPreference->is_enabled_for(\%opts)
219 Based on the existing preferences in the C<item_circulation_alert_preferences>
220 table, can an alert be sent for the given B<branchcode>, B<categorycode>, and
225 my $alert = 'C4::ItemCirculationAlertPreference';
228 categorycode => 'IL',
232 if ($alert->is_enabled_for($conditions)) {
238 sub is_disabled_for
{
239 my ($class, $opts) = @_;
241 my $dbh = C4
::Context
->dbh;
243 # Does a preference exist to block this alert?
245 SELECT id
, branchcode
, categorycode
, item_type
, notification
246 FROM item_circulation_alert_preferences
247 WHERE
(branchcode
= ? OR branchcode
= '*')
248 AND
(categorycode
= ? OR categorycode
= '*')
249 AND
(item_type
= ? OR item_type
= '*')
250 AND
(notification
= ? OR notification
= '*')
253 my $preferences = $dbh->selectall_arrayref(
257 $opts->{categorycode
},
259 $opts->{notification
},
262 # If any preferences showed up, we are NOT enabled.
263 return @
$preferences;
267 my ($class, $opts) = @_;
268 return not $class->is_disabled_for($opts);
274 =head3 C4::ItemCirculationAlertPreference->find({ branchcode => $bc, notification => $type })
276 This method returns all the item circulation alert preferences for a given
277 branch and notification.
281 my @branch_prefs = C4::ItemCirculationAlertPreference->find({
283 notification => 'CHECKIN',
289 my ($class, $where) = @_;
290 my $dbh = C4
::Context
->dbh;
292 SELECT id
, branchcode
, categorycode
, item_type
, notification
293 FROM item_circulation_alert_preferences
294 WHERE branchcode
= ? AND notification
= ?
295 ORDER BY categorycode
, item_type
297 return map { $class->new($_) } @
{$dbh->selectall_arrayref(
300 $where->{branchcode
},
301 $where->{notification
},
308 =head3 C4::ItemCirculationAlertPreference->grid({ branchcode => $c, notification => $type })
310 Return a 2D arrayref for the grid view in F<admin/item_circulation_alert_preferences.pl>.
311 Each row represents a category (like 'Patron' or 'Young Adult') and
312 each column represents an itemtype (like 'Book' or 'Music').
314 Each cell contains...
319 my $grid = C4::ItemCirculationAlertPreference->grid({
321 notification => 'CHECKOUT',
325 See F<admin/item_circulation_alerts.pl> to see how this method is used.
330 my ($class, $where) = @_;
331 my @branch_prefs = $class->find($where);
332 my @default_prefs = $class->find({ branchcode
=> '*', notification
=> $where->{notification
} });
333 my @cc = C4
::Category
->all;
334 my @it = C4
::ItemType
->all;
335 my $notification = $where->{notification
};
337 my $key = $_->categorycode . "-" . $_->item_type . "-" . $notification;
342 my $key = $_->categorycode . "-" . $_->item_type . "-" . $notification;
348 my $row = { description
=> $c->description, items
=> [] };
351 my $key = $c->categorycode . "-" . $i->itemtype . "-" . $notification;
355 if ($disabled{$key}) {
356 push @classes, 'disabled';
357 $text = "Disabled for $where->{branchcode}";
359 if ($default{$key}) {
360 push @classes, 'default';
361 $text = "Disabled for all";
363 push @
{$row->{items
}}, {
364 class => join(' ', @classes),
376 =head2 Object Methods
378 These are read-only accessors for the various attributes of a preference.
384 =head3 $pref->branchcode
388 =head3 $pref->categorycode
392 =head3 $pref->item_type
396 =head3 $pref->notification
402 my $attr = $AUTOLOAD;
404 if (exists $self->{$attr}) {
405 return $self->{$attr};
417 L<C4::Circulation>, F<admin/item_circulation_alerts.pl>
421 John Beppu <john.beppu@liblime.com>