Branch Level Notification Config
[koha.git] / admin / item_circulation_alerts.pl
1 #!/usr/bin/perl
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
21 use CGI;
22 use File::Basename;
23 use Encode;
24 use URI::Escape 'uri_escape_utf8';
25 #use Data::Dump 'pp';
26
27 use C4::Auth;
28 use C4::Context;
29 use C4::Branch;
30 use C4::Category;
31 use C4::ItemType;
32 use C4::ItemCirculationAlertPreference;
33 use C4::Output;
34
35 # shortcut for long package name
36 my $preferences = 'C4::ItemCirculationAlertPreference';
37
38 # common redirect code
39 sub redirect {
40     my ($input) = @_;
41     my $path = defined($input->param('redirect_to'))
42         ? $input->param('redirect_to')
43         : basename($0);
44     print $input->redirect($path);
45 }
46
47 # utf8 filter
48 sub utf8 {
49     my ($data, @keys) = @_;
50     for (@keys) {
51         $data->{$_} = decode('utf8', $data->{$_});
52     }
53     $data;
54 }
55
56 # add long category and itemtype descriptions to preferences
57 sub category_and_itemtype {
58     my ($categories, $item_types, @prefs) = @_;
59     my %c = map { $_->{categorycode} => $_->{description} } @$categories;
60     my %i = map { $_->{itemtype}     => $_->{description} } @$item_types;
61     for (@prefs) {
62         $_->{category_description} = $c{$_->{categorycode}} || 'Default';
63         $_->{item_type_description} = $i{$_->{item_type}} || 'Default';
64     }
65 }
66
67 # display item circulation alerts
68 sub show {
69     my ($input) = @_;
70     my $dbh = C4::Context->dbh;
71     my ($template, $user, $cookie) = get_template_and_user(
72         {
73             template_name   => "admin/item_circulation_alerts.tmpl",
74             query           => $input,
75             type            => "intranet",
76             authnotrequired => 0,
77             flagsrequired   => { admin => 1 },
78             debug           => defined($input->param('debug')),
79         }
80     );
81
82     my $br       = GetBranches;
83     my $branch   = $input->param('branch') || '*';
84     my @branches = map { utf8($_, 'branchname') } (
85         {
86             branchcode => '*',
87             branchname => 'Default',
88         },
89         sort { $a->{branchname} cmp $b->{branchname} } values %$br,
90     );
91     for (@branches) {
92         $_->{selected} = "selected" if ($branch eq $_->{branchcode});
93     }
94     my $branch_name = exists($br->{$branch}) && $br->{$branch}->{branchname};
95
96     my @categories = map { utf8($_, 'description') }  (
97         C4::Category->new({ categorycode => '*', description => 'Default' }),
98         C4::Category->all
99     );
100     my @item_types = map { utf8($_, 'description') }  (
101         C4::ItemType->new({ itemtype => '*', description => 'Default' }),
102         C4::ItemType->all
103     );
104     my @default_prefs = $preferences->find({ branchcode => '*' });
105     my @branch_prefs;
106     my $redirect_to = "?branch=$branch";
107
108     $template->param(redirect_to        => $redirect_to);
109     $template->param(redirect_to_x      => uri_escape_utf8($redirect_to));
110     $template->param(branch             => $branch);
111     $template->param(branch_name        => $branch_name);
112     $template->param(branches           => \@branches);
113     $template->param(categories         => \@categories);
114     $template->param(item_types         => \@item_types);
115     $template->param(default_prefs      => \@default_prefs);
116     if ($branch ne '*') {
117         @branch_prefs = $preferences->find({ branchcode => $branch });
118         $template->param(branch_prefs => \@branch_prefs);
119     }
120     category_and_itemtype(\@categories, \@item_types, (@default_prefs, @branch_prefs));
121     output_html_with_http_headers $input, $cookie, $template->output;
122 }
123
124 # create item circulation alert preference and redirect
125 sub create {
126     my ($input) = @_;
127     my $branchcode   = $input->param('branchcode');
128     my $categorycode = $input->param('categorycode');
129     my $item_type    = $input->param('item_type');
130     $preferences->create({
131         branchcode   => $branchcode,
132         categorycode => $categorycode,
133         item_type    => $item_type,
134     });
135     redirect($input);
136 }
137
138 # delete preference and redirect
139 sub delete {
140     my ($input) = @_;
141     my $id = $input->param('id');
142     $preferences->delete({ id => $id });
143     redirect($input);
144 }
145
146 # dispatch to various actions based on CGI parameter 'action'
147 sub dispatch {
148     my %handler = (
149         show   => \&show,
150         create => \&create,
151         delete => \&delete,
152     );
153     my $input  = new CGI;
154     my $action = $input->param('action') || 'show';
155     if (not exists $handler{$action}) {
156         my $status = 400;
157         print $input->header(-status => $status);
158         print $input->div(
159             $input->h1($status),
160             $input->p("$action is not supported.")
161         );
162     } else {
163         $handler{$action}->($input);
164     }
165 }
166
167 # main
168 dispatch if $ENV{REQUEST_URI};
169 1;
170
171
172 =head1 NAME
173
174 admin/item_circulation_alerts.pl - per-branch configuration for messaging
175
176 =head1 SYNOPSIS
177
178 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
179
180 =head1 DESCRIPTION
181
182 This CGI script drives an interface for configuring item circulation alerts.
183 If you want to prevent alerts from going out for any combination of branch,
184 patron category, and item type, this is where that policy would be set.
185
186 =head2 URLs
187
188
189 =head3 ?action=show
190
191 Display a branches item circulation alert preferences.
192
193 Parameters:
194
195 =over 4
196
197 =item branch
198
199 What branch are we looking at.  If none is specified, the virtual default
200 branch '*' is used.
201
202 =back
203
204
205
206
207 =head3 ?action=create
208
209 Create an item circulation alert preference.
210
211 Parameters:
212
213 =over 4
214
215 =item branchcode
216
217 Branch code
218
219 =item categorycode
220
221 Patron category
222
223 =item item_type
224
225 Item type
226
227 =back
228
229
230
231
232 =head3 ?action=delete
233
234 Delete an item circulation alert preference.
235
236 Parameters:
237
238 =over 4
239
240 =item id
241
242 The id of the preference to delete.
243
244 =back
245
246
247
248
249 =cut
250
251 # Local Variables: ***
252 # mode: cperl ***
253 # indent-tabs-mode: nil ***
254 # cperl-close-paren-offset: -4 ***
255 # cperl-continued-statement-offset: 4 ***
256 # cperl-indent-level: 4 ***
257 # cperl-indent-parens-as-block: t ***
258 # cperl-tab-always-indent: nil ***
259 # End: ***
260 # vim:tabstop=8 softtabstop=4 shiftwidth=4 shiftround expandtab