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
21 use Carp qw(carp croak);
25 # helper function for validating \%opts
28 for (qw(branchcode categorycode item_type)) {
29 exists($opts->{$_}) || croak("'$_' is a required parameter.");
38 C4::ItemCirculationAlertPreference - manage preferences for sending alerts
44 use C4::ItemCirculationAlertPreference;
46 # a short-cut to reduce typing the long package name over and over again
47 my $preferences = 'C4::ItemCirculationAlertPreference';
51 my $pref = $preferences->create({
59 $preferences->delete({
67 This class is used to manage the preferences for when an alert may be sent. By
68 default, item circulation alerts are enabled for every B<branch>, B<patron
69 category> and B<item type>.
71 However, if you would like to prevent item circulation alerts from being sent
72 for any combination of these 3 variables, a preference can be inserted into the
73 C<item_circulation_alert_preferences> table to make that a policy.
81 =head3 C4::ItemCirculationAlertPreference->new(\%opts)
83 This is a constructor for an in-memory C4::ItemCirculationAlertPreference
84 object. The database is not affected by this method.
89 my ($class, $opts) = @_;
90 bless $opts => $class;
96 =head3 C4::ItemCirculationAlertPreference->create(\%opts)
98 This will find or create an item circulation alert preference. You must pass
99 it a B<branchcode>, B<categorycode>, and B<item_type>.
104 my ($class, $opts) = @_;
106 my $dbh = C4::Context->dbh;
107 my $prefs = $dbh->selectall_arrayref(
108 "SELECT id, branchcode, categorycode, item_type
109 FROM item_circulation_alert_preferences
115 $opts->{categorycode},
119 return $class->new($prefs->[0]);
121 my $success = $dbh->do(
122 "INSERT INTO item_circulation_alert_preferences
123 (branchcode, categorycode, item_type) VALUES (?, ?, ?)",
126 $opts->{categorycode},
131 id => $dbh->last_insert_id(undef, undef, undef, undef),
132 branchcode => $opts->{branchcode},
133 categorycode => $opts->{categorycode},
134 item_type => $opts->{item_type},
136 return $class->new($self);
147 =head3 C4::ItemCirculationAlertPreference->delete(\%opts)
149 Delete an item circulation alert preference. You can delete by either passing
150 in an B<id> or passing in a B<branchcode>, B<categorycode>, B<item_type>
156 my ($class, $opts) = @_;
157 my $dbh = C4::Context->dbh;
160 "DELETE FROM item_circulation_alert_preferences WHERE id = ?",
167 "DELETE FROM item_circulation_alert_preferences
173 $opts->{categorycode},
182 =head3 C4::ItemCirculationAlertPreference->is_enabled_for(\%opts)
184 Based on the existing preferences in the C<item_circulation_alert_preferences>
185 table, can an alert be sent for the given B<branchcode>, B<categorycode>, and
190 my $alert = 'C4::ItemCirculationAlertPreference';
193 categorycode => 'IL',
197 if ($alert->is_enabled_for($conditions)) {
204 my ($class, $opts) = @_;
206 my $dbh = C4::Context->dbh;
208 # Does a preference exist to block this alert?
211 FROM item_circulation_alert_preferences
212 WHERE (branchcode = ? OR branchcode = '*')
213 AND (categorycode = ? OR categorycode = '*')
214 AND (item_type = ? OR item_type = '*')
217 my $preferences = $dbh->selectall_arrayref(
221 $opts->{categorycode},
225 # If any preferences showed up, we are NOT enabled.
236 =head3 C4::ItemCirculationAlertPreference->find({ branchcode => $bc })
238 This method returns all the item circulation alert preferences for a given
243 my @branch_prefs = C4::ItemCirculationAlertPreference->find({
250 my ($class, $where) = @_;
251 my $dbh = C4::Context->dbh;
253 SELECT id, branchcode, categorycode, item_type
254 FROM item_circulation_alert_preferences
256 ORDER BY categorycode, item_type
258 return map { $class->new($_) } @{$dbh->selectall_arrayref(
268 =head2 Object Methods
270 These are read-only accessors for the various attributes of a preference.
274 =head3 $pref->branchcode
276 =head3 $pref->categorycode
278 =head3 $pref->item_type
284 my $attr = $AUTOLOAD;
286 if (exists $self->{$attr}) {
287 return $self->{$attr};
298 L<C4::Circulation>, C<admin/item_circulation_alerts.pl>
302 John Beppu <john.beppu@liblime.com>
308 # Local Variables: ***
310 # indent-tabs-mode: nil ***
311 # cperl-close-paren-offset: -4 ***
312 # cperl-continued-statement-offset: 4 ***
313 # cperl-indent-level: 4 ***
314 # cperl-indent-parens-as-block: t ***
315 # cperl-tab-always-indent: nil ***
317 # vim:tabstop=8 softtabstop=4 shiftwidth=4 shiftround expandtab