(bug #3584) detect ccl queries
[koha.git] / C4 / ItemCirculationAlertPreference.pm
1 package C4::ItemCirculationAlertPreference;
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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
17
18 use strict;
19 use warnings;
20 use C4::Context;
21 use Carp qw(carp croak);
22
23 our $AUTOLOAD;
24
25 # helper function for validating \%opts
26 our $valid = sub {
27     my $opts = shift;
28     for (qw(branchcode categorycode item_type)) {
29         exists($opts->{$_}) || croak("'$_' is a required parameter.");
30     }
31 };
32
33
34
35
36 =head1 NAME
37
38 C4::ItemCirculationAlertPreference - manage preferences for sending alerts
39
40 =head1 SYNOPSIS
41
42 Basics:
43
44     use C4::ItemCirculationAlertPreference;
45
46     # a short-cut to reduce typing the long package name over and over again
47     my $preferences = 'C4::ItemCirculationAlertPreference';
48
49 Creating Rules:
50
51     my $pref = $preferences->create({
52         branchcode   => 'CPL',
53         categorycode => 'YA',
54         item_type    => 'BK',
55     });
56
57 Deleting Rules:
58
59     $preferences->delete({
60         branchcode   => 'CPL',
61         categorycode => 'YA',
62         item_type    => 'BK',
63     });
64
65 =head1 DESCRIPTION
66
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>.
70
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.
74
75 =head1 API
76
77 =head2 Class Methods
78
79 =cut
80
81 =head3 C4::ItemCirculationAlertPreference->new(\%opts)
82
83 This is a constructor for an in-memory C4::ItemCirculationAlertPreference
84 object.  The database is not affected by this method.
85
86 =cut
87
88 sub new {
89     my ($class, $opts) = @_;
90     bless $opts => $class;
91 }
92
93
94
95
96 =head3 C4::ItemCirculationAlertPreference->create(\%opts)
97
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>.
100
101 =cut
102
103 sub create {
104     my ($class, $opts) = @_;
105     $valid->($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
110         WHERE branchcode   = ?
111         AND   categorycode = ?
112         AND   item_type    = ?",
113         { Slice => {} },
114         $opts->{branchcode},
115         $opts->{categorycode},
116         $opts->{item_type},
117     );
118     if (@$prefs) {
119         return $class->new($prefs->[0]);
120     } else {
121         my $success = $dbh->do(
122             "INSERT INTO item_circulation_alert_preferences
123             (branchcode, categorycode, item_type) VALUES (?, ?, ?)",
124             {},
125             $opts->{branchcode},
126             $opts->{categorycode},
127             $opts->{item_type},
128         );
129         if ($success) {
130             my $self = {
131                 id           => $dbh->last_insert_id(undef, undef, undef, undef),
132                 branchcode   => $opts->{branchcode},
133                 categorycode => $opts->{categorycode},
134                 item_type    => $opts->{item_type},
135             };
136             return $class->new($self);
137         } else {
138             carp $dbh->errstr;
139             return undef;
140         }
141     }
142 }
143
144
145
146
147 =head3 C4::ItemCirculationAlertPreference->delete(\%opts)
148
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>
151 triplet.
152
153 =cut
154
155 sub delete {
156     my ($class, $opts) = @_;
157     my $dbh = C4::Context->dbh;
158     if ($opts->{id}) {
159         $dbh->do(
160             "DELETE FROM item_circulation_alert_preferences WHERE id = ?",
161             {},
162             $opts->{id}
163         );
164     } else {
165         $valid->($opts);
166         $dbh->do(
167             "DELETE FROM item_circulation_alert_preferences
168             WHERE branchcode   = ?
169             AND   categorycode = ?
170             AND   item_type    = ?",
171             {},
172             $opts->{branchcode},
173             $opts->{categorycode},
174             $opts->{item_type}
175         );
176     }
177 }
178
179
180
181
182 =head3 C4::ItemCirculationAlertPreference->is_enabled_for(\%opts)
183
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
186 B<itemtype>?
187
188 B<Example>:
189
190     my $alert = 'C4::ItemCirculationAlertPreference';
191     my $conditions = {
192         branchcode   => 'CPL',
193         categorycode => 'IL',
194         item_type    => 'MU',
195     };
196
197     if ($alert->is_enabled_for($conditions)) {
198         # ...
199     }
200
201 =cut
202
203 sub is_enabled_for {
204     my ($class, $opts) = @_;
205     $valid->($opts);
206     my $dbh = C4::Context->dbh;
207
208     # Does a preference exist to block this alert?
209     my $query = qq{
210         SELECT id
211           FROM item_circulation_alert_preferences
212          WHERE (branchcode   = ? OR branchcode   = '*')
213            AND (categorycode = ? OR categorycode = '*')
214            AND (item_type    = ? OR item_type    = '*')
215     };
216
217     my $preferences = $dbh->selectall_arrayref(
218         $query,
219         { },
220         $opts->{branchcode},
221         $opts->{categorycode},
222         $opts->{item_type},
223     );
224
225     # If any preferences showed up, we are NOT enabled.
226     if (@$preferences) {
227         return undef;
228     } else {
229         return 1;
230     }
231 }
232
233
234
235
236 =head3 C4::ItemCirculationAlertPreference->find({ branchcode => $bc })
237
238 This method returns all the item circulation alert preferences for a given
239 branch.
240
241 B<Example>:
242
243     my @branch_prefs = C4::ItemCirculationAlertPreference->find({
244         branchcode => 'CPL',
245     });
246
247 =cut
248
249 sub find {
250     my ($class, $where) = @_;
251     my $dbh = C4::Context->dbh;
252     my $query = qq{
253         SELECT id, branchcode, categorycode, item_type
254           FROM item_circulation_alert_preferences
255          WHERE branchcode = ?
256          ORDER BY categorycode, item_type
257     };
258     return    map { $class->new($_) }    @{$dbh->selectall_arrayref(
259         $query,
260         { Slice => {} },
261         $where->{branchcode}
262     )};
263 }
264
265
266
267
268 =head2 Object Methods
269
270 These are read-only accessors for the various attributes of a preference.
271
272 =head3 $pref->id
273
274 =head3 $pref->branchcode
275
276 =head3 $pref->categorycode
277
278 =head3 $pref->item_type
279
280 =cut
281
282 sub AUTOLOAD {
283     my $self = shift;
284     my $attr = $AUTOLOAD;
285     $attr =~ s/.*://;
286     if (exists $self->{$attr}) {
287         return $self->{$attr};
288     } else {
289         return undef;
290     }
291 }
292
293
294
295
296 =head1 SEE ALSO
297
298 L<C4::Circulation>, C<admin/item_circulation_alerts.pl>
299
300 =head1 AUTHOR
301
302 John Beppu <john.beppu@liblime.com>
303
304 =cut
305
306 1;
307
308 # Local Variables: ***
309 # mode: cperl ***
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 ***
316 # End: ***
317 # vim:tabstop=8 softtabstop=4 shiftwidth=4 shiftround expandtab