Bug 20607: (QA follow-up) Test corrections
[koha.git] / admin / classsources.pl
blob697db41e45cfe8eface13ffc47d1da8729e75453
1 #! /usr/bin/perl
3 # Copyright 2007 LibLime
4 # Copyright 2018 Koha Development Team
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::ClassSortRoutine;
29 use C4::ClassSplitRoutine;
30 use Koha::ClassSources;
31 use Koha::ClassSortRules;
32 use Koha::ClassSplitRules;
34 my $script_name = "/cgi-bin/koha/admin/classsources.pl";
36 my $input = new CGI;
37 my $op = $input->param('op') || 'list';
38 my $cn_source = $input->param('cn_source');
39 my $class_sort_rule = $input->param('class_sort_rule');
40 my $class_split_rule = $input->param('class_split_rule');
41 my $sort_routine = $input->param('sort_routine');
42 my $split_routine = $input->param('split_routine');
43 my @split_regex = $input->multi_param('split_regex');
44 my $description = $input->param('description');
45 my $used = $input->param('used');
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49 template_name => "admin/classsources.tt",
50 query => $input,
51 type => "intranet",
52 authnotrequired => 0,
53 flagsrequired => { parameters => 'manage_classifications' },
54 debug => 1,
58 my @messages;
59 $template->param( script_name => $script_name );
61 if ( $op eq "add_source" ) {
62 my $class_source =
63 $cn_source ? Koha::ClassSources->find($cn_source) : undef;
64 $template->param(
65 class_source => $class_source,
66 sort_rules => scalar Koha::ClassSortRules->search,
67 split_rules => scalar Koha::ClassSplitRules->search,
70 elsif ( $op eq "add_source_validate" ) {
71 my $class_source = Koha::ClassSources->find($cn_source);
72 if ($class_source) {
73 $class_source->set(
75 description => $description,
76 used => ( $used eq 'used' ? 1 : 0 ),
77 class_sort_rule => $class_sort_rule,
78 class_split_rule => $class_split_rule,
81 eval { $class_source->store; };
82 if ($@) {
83 push @messages,
84 { type => 'error', code => 'error_on_update_source' };
86 else {
87 push @messages,
88 { type => 'message', code => 'success_on_update_source' };
92 else {
93 $class_source = Koha::ClassSource->new(
95 cn_source => $cn_source,
96 description => $description,
97 used => ( $used eq 'used' ? 1 : 0 ),
98 class_sort_rule => $class_sort_rule,
99 class_split_rule => $class_split_rule,
102 eval { $class_source->store; };
103 if ($@) {
104 push @messages,
105 { type => 'error', code => 'error_on_insert_source' };
107 else {
108 push @messages,
109 { type => 'message', code => 'success_on_insert_source' };
113 $op = 'list';
115 elsif ( $op eq "delete_source_confirmed" ) {
116 my $class_source = Koha::ClassSources->find($cn_source);
117 my $deleted = eval { $class_source->delete };
118 if ( $@ or not $deleted ) {
119 push @messages, { type => 'error', code => 'error_on_delete_source' };
121 else {
122 push @messages,
123 { type => 'message', code => 'success_on_delete_source' };
126 $op = 'list';
128 elsif ( $op eq "add_sort_rule" ) {
129 my $sort_rule =
130 $class_sort_rule ? Koha::ClassSortRules->find($class_sort_rule) : undef;
131 $template->param(
132 sort_rule => $sort_rule,
133 sort_routines => get_class_sort_routines(),
136 elsif ( $op eq "add_sort_rule_validate" ) {
137 my $sort_rule = Koha::ClassSortRules->find($class_sort_rule);
138 if ($sort_rule) {
139 $sort_rule->set(
140 { description => $description, sort_routine => $sort_routine } );
141 eval { $sort_rule->store; };
142 if ($@) {
143 push @messages,
144 { type => 'error', code => 'error_on_update_sort_rule' };
146 else {
147 push @messages,
148 { type => 'message', code => 'success_on_update_sort_rule' };
152 else {
153 $sort_rule = Koha::ClassSortRule->new(
155 class_sort_rule => $class_sort_rule,
156 description => $description,
157 sort_routine => $sort_routine,
160 eval { $sort_rule->store; };
161 if ($@) {
162 push @messages,
163 { type => 'error', code => 'error_on_insert_sort_rule' };
165 else {
166 push @messages,
167 { type => 'message', code => 'success_on_insert_sort_rule' };
170 $op = 'list';
172 elsif ( $op eq "delete_sort_rule" ) {
173 my $sort_rule = Koha::ClassSortRules->find($class_sort_rule);
174 my $deleted = eval { $sort_rule->delete };
175 if ( $@ or not $deleted ) {
176 push @messages,
177 { type => 'error', code => 'error_on_delete_sort_rule' };
179 else {
180 push @messages,
181 { type => 'message', code => 'success_on_delete_sort_rule' };
183 $op = 'list';
185 elsif ( $op eq "add_split_rule" ) {
186 my $split_rule =
187 $class_split_rule
188 ? Koha::ClassSplitRules->find($class_split_rule)
189 : undef;
190 $template->param(
191 split_rule => $split_rule,
192 split_routines => get_class_split_routines(),
195 elsif ( $op eq "add_split_rule_validate" ) {
196 my $split_rule = Koha::ClassSplitRules->find($class_split_rule);
198 @split_regex = grep {!/^$/} @split_regex; # Remove empty
199 if ($split_rule) {
200 $split_rule->set(
202 description => $description,
203 split_routine => $split_routine,
206 eval {
207 $split_rule->regexs(\@split_regex)
208 if $split_routine eq 'RegEx';
209 $split_rule->store;
211 if ($@) {
212 push @messages,
213 { type => 'error', code => 'error_on_update_split_rule' };
215 else {
216 push @messages,
217 { type => 'message', code => 'success_on_update_split_rule' };
221 else {
222 $split_rule = Koha::ClassSplitRule->new(
224 class_split_rule => $class_split_rule,
225 description => $description,
226 split_routine => $split_routine,
227 regexs => \@split_regex,
230 eval { $split_rule->store; };
231 if ($@) {
232 warn $@;
233 push @messages,
234 { type => 'error', code => 'error_on_insert_split_rule' };
236 else {
237 push @messages,
238 { type => 'message', code => 'success_on_insert_split_rule' };
241 $op = 'list';
243 elsif ( $op eq "delete_split_rule" ) {
244 my $split_rule = Koha::ClassSplitRules->find($class_split_rule);
245 my $deleted = eval { $split_rule->delete };
246 if ( $@ or not $deleted ) {
247 push @messages,
248 { type => 'error', code => 'error_on_delete_split_rule' };
250 else {
251 push @messages,
252 { type => 'message', code => 'success_on_delete_split_rule' };
254 $op = 'list';
257 if ( $op eq 'list' ) {
258 my $class_sources = Koha::ClassSources->search;
259 my $sort_rules = Koha::ClassSortRules->search;
260 my $split_rules = Koha::ClassSplitRules->search;
261 $template->param(
262 class_sources => $class_sources,
263 sort_rules => $sort_rules,
264 split_rules => $split_rules,
268 $template->param( op => $op, messages => \@messages, );
269 output_html_with_http_headers $input, $cookie, $template->output;
271 exit 0;
273 sub get_class_sort_routines {
274 my @sort_routines = GetSortRoutineNames();
275 return \@sort_routines;
278 sub get_class_split_routines {
279 my @split_routines = GetSplitRoutineNames();
280 return \@split_routines;