Bug 27070: Add cross_fields type to our searches
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use File::Basename;
22 use Encode;
23 use JSON;
24 #use Data::Dump 'pp';
25
26 use C4::Auth;
27 use C4::Context;
28 use C4::ItemCirculationAlertPreference;
29 use C4::Output;
30
31 use Koha::ItemTypes;
32 use Koha::Patron::Categories;
33
34 # shortcut for long package name
35 our $preferences = 'C4::ItemCirculationAlertPreference';
36
37 # display item circulation alerts
38 sub show {
39     my ($input) = @_;
40     my $dbh = C4::Context->dbh;
41     my ($template, $user, $cookie) = get_template_and_user(
42         {
43             template_name   => "admin/item_circulation_alerts.tt",
44             query           => $input,
45             type            => "intranet",
46             flagsrequired   => { parameters => 'item_circ_alerts' },
47             debug           => defined($input->param('debug')),
48         }
49     );
50
51     my $branch   = $input->param('branch') || '*';
52     my @categories = Koha::Patron::Categories->search_limited;
53     my @item_types = Koha::ItemTypes->search;
54     my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
55     my $grid_checkin  = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });
56
57     $template->param(branch             => $branch);
58     $template->param(categories         => \@categories);
59     $template->param(item_types         => \@item_types);
60     $template->param(grid_checkout      => $grid_checkout);
61     $template->param(grid_checkin       => $grid_checkin);
62
63     output_html_with_http_headers $input, $cookie, $template->output;
64 }
65
66 # toggle a preference via ajax
67 sub toggle {
68     my ($input) = @_;
69     my $id = $input->param('id');
70     my $branch = $input->param('branch');
71     my ($category, $item_type, $notification) = split('-', $id);
72     $category  =~ s/_/*/;
73     $item_type =~ s/_/*/;
74
75     my $settings = {
76         branchcode   => $branch,
77         categorycode => $category,
78         item_type    => $item_type,
79         notification => $notification,
80     };
81
82     my $restrictions = $preferences;  # all the same thing...
83     my $notifications = $preferences; #
84     if ($notifications->is_enabled_for($settings)) {
85         # toggle by adding a restriction
86         $restrictions->create($settings);
87     } else {
88         # toggle by removing the restriction
89         $restrictions->delete($settings);
90     }
91
92     my $response = { success => 1 };
93     my @reasons  = $notifications->is_disabled_for($settings);
94     if (@reasons == 0) {
95         $response->{classes} = '';
96     } else {
97         my $default_exists   = grep { $_->{branchcode} eq '*' } @reasons;
98         my $non_default_also = grep { $_->{branchcode} ne '*' } @reasons;
99         my @classes;
100         push @classes, 'default'  if $default_exists;
101         push @classes, 'disabled' if $non_default_also;
102         $response->{classes} = join(' ', @classes);
103     }
104     print $input->header;
105     print encode_json($response);
106 }
107
108 # dispatch to various actions based on CGI parameter 'action'
109 sub dispatch {
110     my %handler = (
111         show   => \&show,
112         toggle => \&toggle,
113     );
114     my $input  = CGI->new;
115     my $action = $input->param('action') || 'show';
116     if (not exists $handler{$action}) {
117         my $status = 400;
118         print $input->header(-status => $status);
119         print $input->div(
120             $input->h1($status),
121             $input->p("$action is not supported.")
122         );
123     } else {
124         $handler{$action}->($input);
125     }
126 }
127
128 # main
129 dispatch if $ENV{REQUEST_URI};
130 1;
131
132
133 =head1 NAME
134
135 admin/item_circulation_alerts.pl - per-branch configuration for messaging
136
137 =head1 SYNOPSIS
138
139 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
140
141 =head1 DESCRIPTION
142
143 This CGI script drives an interface for configuring item circulation alerts.
144 If you want to prevent alerts from going out for any combination of branch,
145 patron category, and item type, this is where that policy would be set.
146
147 =head2 URLs
148
149
150 =head3 ?action=show
151
152 Display a branches item circulation alert preferences.
153
154 Parameters:
155
156 =over 2
157
158 =item branch
159
160 What branch are we looking at.  If none is specified, the virtual default
161 branch '*' is used.
162
163 =back
164
165
166
167 =head3 ?action=toggle
168
169 Toggle a preference via AJAX
170
171 Parameters:
172
173 =over 2
174
175 =item id
176
177 The id should be string that can be split on "-" which contains:
178 "$categorycode-$item_type-$notification".
179
180 =item branch
181
182 Branch code to apply this preference to
183
184 =back
185
186 =cut