Bug 17234: Need to separate KEY and FOREIGN KEY checks
[koha.git] / admin / classsources.pl
blob74e682a2001a5d2c6cd090a0a9f91056adc3c052
1 #! /usr/bin/perl
3 # Copyright 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::ClassSource;
29 use C4::ClassSortRoutine;
31 my $script_name = "/cgi-bin/koha/admin/classsources.pl";
33 my $input = new CGI;
34 my $op = $input->param('op') || '';
35 my $source_code = $input->param('class_source');
36 my $rule_code = $input->param('sort_rule');
38 my ($template, $loggedinuser, $cookie)
39 = get_template_and_user({template_name => "admin/classsources.tt",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => {parameters => 'parameters_remaining_permissions'},
44 debug => 1,
45 });
47 $template->param(script_name => $script_name);
48 $template->param($op => 1) if $op;
50 my $display_lists = 0;
51 if ($op eq "add_source") {
52 add_class_source_form($template);
53 } elsif ($op eq "add_source_confirmed") {
54 add_class_source($template,
55 $source_code,
56 $input->param('description'),
57 $input->param('used') eq "used" ? 1 : 0,
58 $rule_code);
59 $display_lists = 1;
60 } elsif ($op eq "delete_source") {
61 delete_class_source_form($template, $source_code);
62 } elsif ($op eq "delete_source_confirmed") {
63 delete_class_source($template, $source_code);
64 $display_lists = 1;
65 } elsif ($op eq "edit_source") {
66 edit_class_source_form($template, $source_code);
67 } elsif ($op eq "edit_source_confirmed") {
68 edit_class_source($template,
69 $source_code,
70 $input->param('description'),
71 $input->param('used') eq "used" ? 1 : 0,
72 $rule_code);
73 $display_lists = 1;
74 } elsif ($op eq "add_sort_rule") {
75 add_class_sort_rule_form($template);
76 } elsif ($op eq "add_sort_rule_confirmed") {
77 add_class_sort_rule($template,
78 $rule_code,
79 $input->param('description'),
80 $input->param('sort_routine'));
81 $display_lists = 1;
82 } elsif ($op eq "delete_sort_rule") {
83 delete_sort_rule_form($template, $rule_code);
84 } elsif ($op eq "delete_sort_rule_confirmed") {
85 delete_sort_rule($template, $rule_code);
86 $display_lists = 1;
87 } elsif ($op eq "edit_sort_rule") {
88 edit_class_sort_rule_form($template, $rule_code);
89 } elsif ($op eq "edit_sort_rule_confirmed") {
90 edit_class_sort_rule($template,
91 $rule_code,
92 $input->param('description'),
93 $input->param('sort_routine'));
94 $display_lists = 1;
95 } else {
96 $display_lists = 1;
99 if ($display_lists) {
100 $template->param(display_lists => 1);
101 class_source_list($template);
102 class_sort_rule_list($template);
105 output_html_with_http_headers $input, $cookie, $template->output;
107 exit 0;
109 sub add_class_source_form {
110 my ($template) = @_;
111 $template->param(
112 class_source_form => 1,
113 confirm_op => "add_source_confirmed",
114 used => 0
116 get_sort_rule_codes($template, '');
119 sub add_class_source {
120 my ($template, $source_code, $description, $used, $sort_rule) = @_;
121 my $success = AddClassSource($source_code, $description, $used, $sort_rule);
122 if ($success > 0) {
123 $template->param(added_source => $source_code);
124 } else {
125 $template->param(failed_add_source => $source_code);
129 sub edit_class_source_form {
130 my ($template, $source_code) = @_;
132 my $source = GetClassSource($source_code);
133 $template->param(
134 class_source_form => 1,
135 edit_class_source => 1,
136 class_source => $source_code,
137 confirm_op => "edit_source_confirmed",
138 description => $source->{'description'},
139 used => $source->{'used'},
142 get_sort_rule_codes($template, $source->{'class_sort_rule'});
145 sub edit_class_source {
146 my ($template, $source_code, $description, $used, $sort_rule) = @_;
147 ModClassSource($source_code, $description, $used, $sort_rule);
148 $template->param(edited_source => $source_code);
152 sub delete_class_source_form {
153 my ($template, $source_code) = @_;
154 $template->param(
155 delete_class_source_form => 1,
156 confirm_op => "delete_source_confirmed",
157 class_source => $source_code,
161 sub delete_class_source {
162 my ($template, $source_code) = @_;
163 DelClassSource($source_code);
164 $template->param(deleted_source => $source_code);
167 sub get_sort_rule_codes {
168 my ($template, $current_rule) = @_;
170 my $sort_rules = GetClassSortRules();
172 my @sort_rules = ();
173 foreach my $sort_rule (sort keys %$sort_rules) {
174 my $sort_rule = $sort_rules->{$sort_rule};
175 push @sort_rules, {
176 rule => $sort_rule->{'class_sort_rule'},
177 description => $sort_rule->{'description'},
178 selected => $sort_rule->{'class_sort_rule'} eq $current_rule ? 1 : 0
181 $template->param(rules_dropdown => \@sort_rules);
185 sub add_class_sort_rule_form {
186 my ($template) = @_;
187 $template->param(
188 sort_rule_form => 1,
189 confirm_op => "add_sort_rule_confirmed"
191 get_class_sort_routines($template, "");
194 sub add_class_sort_rule {
195 my ($template, $rule_code, $description, $sort_routine) = @_;
196 my $success = AddClassSortRule($rule_code, $description, $sort_routine);
197 if ($success > 0) {
198 $template->param(added_rule => $rule_code);
199 } else {
200 $template->param(failed_add_rule => $rule_code);
204 sub delete_sort_rule_form {
205 my ($template, $rule_code) = @_;
207 my @sources = GetSourcesForSortRule($rule_code);
208 if ($#sources == -1) {
209 $template->param(
210 delete_sort_rule_form => 1,
211 confirm_op => "delete_sort_rule_confirmed",
212 sort_rule => $rule_code,
214 } else {
215 $template->param(
216 delete_sort_rule_impossible => 1,
217 sort_rule => $rule_code
222 sub delete_sort_rule {
223 my ($template, $rule_code) = @_;
224 DelClassSortRule($rule_code);
225 $template->param(deleted_rule => $rule_code);
228 sub edit_class_sort_rule_form {
229 my ($template, $rule_code) = @_;
231 my $rule = GetClassSortRule($rule_code);
232 $template->param(
233 sort_rule_form => 1,
234 edit_sort_rule => 1,
235 confirm_op => "edit_sort_rule_confirmed",
236 sort_rule => $rule_code,
237 description => $rule->{'description'},
238 sort_routine => $rule->{'sort_routine'}
241 get_class_sort_routines($template, $rule->{'sort_routine'});
245 sub get_class_sort_routines {
246 my ($template, $current_routine) = @_;
248 my @sort_routines = GetSortRoutineNames();
249 my @sort_form = ();
251 foreach my $sort_routine (sort @sort_routines) {
252 push @sort_form, {
253 routine => $sort_routine,
254 selected => $sort_routine eq $current_routine ? 1 : 0
257 $template->param(routines_dropdown => \@sort_form);
261 sub edit_class_sort_rule {
262 my ($template, $rule_code, $description, $sort_routine) = @_;
263 ModClassSortRule($rule_code, $description, $sort_routine);
264 $template->param(edited_rule => $rule_code);
267 sub class_source_list {
268 my ($template) = @_;
269 my $sources = GetClassSources();
271 my @sources = ();
272 foreach my $cn_source (sort keys %$sources) {
273 my $source = $sources->{$cn_source};
274 push @sources, {
275 code => $source->{'cn_source'},
276 description => $source->{'description'},
277 used => $source->{'used'},
278 sortrule => $source->{'class_sort_rule'}
281 $template->param(class_sources => \@sources);
284 sub class_sort_rule_list {
286 my ($template) = @_;
287 my $sort_rules = GetClassSortRules();
289 my @sort_rules = ();
290 foreach my $sort_rule (sort keys %$sort_rules) {
291 my $sort_rule = $sort_rules->{$sort_rule};
292 push @sort_rules, {
293 rule => $sort_rule->{'class_sort_rule'},
294 description => $sort_rule->{'description'},
295 sort_routine => $sort_rule->{'sort_routine'}
298 $template->param(class_sort_rules => \@sort_rules);