Bug 22417: Adapt the batch_record_modification tool
[koha.git] / admin / classsources.pl
bloba937f7bc1845b20a3c7beedbc0aad9944bc56995
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 flagsrequired => { parameters => 'manage_classifications' },
53 debug => 1,
57 my @messages;
58 $template->param( script_name => $script_name );
60 if ( $op eq "add_source" ) {
61 my $class_source =
62 $cn_source ? Koha::ClassSources->find($cn_source) : undef;
63 $template->param(
64 class_source => $class_source,
65 sort_rules => scalar Koha::ClassSortRules->search,
66 split_rules => scalar Koha::ClassSplitRules->search,
69 elsif ( $op eq "add_source_validate" ) {
70 my $class_source = Koha::ClassSources->find($cn_source);
71 if ($class_source) {
72 $class_source->set(
74 description => $description,
75 used => ( $used eq 'used' ? 1 : 0 ),
76 class_sort_rule => $class_sort_rule,
77 class_split_rule => $class_split_rule,
80 eval { $class_source->store; };
81 if ($@) {
82 push @messages,
83 { type => 'error', code => 'error_on_update_source' };
85 else {
86 push @messages,
87 { type => 'message', code => 'success_on_update_source' };
91 else {
92 $class_source = Koha::ClassSource->new(
94 cn_source => $cn_source,
95 description => $description,
96 used => ( $used eq 'used' ? 1 : 0 ),
97 class_sort_rule => $class_sort_rule,
98 class_split_rule => $class_split_rule,
101 eval { $class_source->store; };
102 if ($@) {
103 push @messages,
104 { type => 'error', code => 'error_on_insert_source' };
106 else {
107 push @messages,
108 { type => 'message', code => 'success_on_insert_source' };
112 $op = 'list';
114 elsif ( $op eq "delete_source_confirmed" ) {
115 my $class_source = Koha::ClassSources->find($cn_source);
116 my $deleted = eval { $class_source->delete };
117 if ( $@ or not $deleted ) {
118 push @messages, { type => 'error', code => 'error_on_delete_source' };
120 else {
121 push @messages,
122 { type => 'message', code => 'success_on_delete_source' };
125 $op = 'list';
127 elsif ( $op eq "add_sort_rule" ) {
128 my $sort_rule =
129 $class_sort_rule ? Koha::ClassSortRules->find($class_sort_rule) : undef;
130 $template->param(
131 sort_rule => $sort_rule,
132 sort_routines => get_class_sort_routines(),
135 elsif ( $op eq "add_sort_rule_validate" ) {
136 my $sort_rule = Koha::ClassSortRules->find($class_sort_rule);
137 if ($sort_rule) {
138 $sort_rule->set(
139 { description => $description, sort_routine => $sort_routine } );
140 eval { $sort_rule->store; };
141 if ($@) {
142 push @messages,
143 { type => 'error', code => 'error_on_update_sort_rule' };
145 else {
146 push @messages,
147 { type => 'message', code => 'success_on_update_sort_rule' };
151 else {
152 $sort_rule = Koha::ClassSortRule->new(
154 class_sort_rule => $class_sort_rule,
155 description => $description,
156 sort_routine => $sort_routine,
159 eval { $sort_rule->store; };
160 if ($@) {
161 push @messages,
162 { type => 'error', code => 'error_on_insert_sort_rule' };
164 else {
165 push @messages,
166 { type => 'message', code => 'success_on_insert_sort_rule' };
169 $op = 'list';
171 elsif ( $op eq "delete_sort_rule" ) {
172 my $sort_rule = Koha::ClassSortRules->find($class_sort_rule);
173 my $deleted = eval { $sort_rule->delete };
174 if ( $@ or not $deleted ) {
175 push @messages,
176 { type => 'error', code => 'error_on_delete_sort_rule' };
178 else {
179 push @messages,
180 { type => 'message', code => 'success_on_delete_sort_rule' };
182 $op = 'list';
184 elsif ( $op eq "add_split_rule" ) {
185 my $split_rule =
186 $class_split_rule
187 ? Koha::ClassSplitRules->find($class_split_rule)
188 : undef;
189 $template->param(
190 split_rule => $split_rule,
191 split_routines => get_class_split_routines(),
194 elsif ( $op eq "add_split_rule_validate" ) {
195 my $split_rule = Koha::ClassSplitRules->find($class_split_rule);
197 @split_regex = grep {!/^$/} @split_regex; # Remove empty
198 if ($split_rule) {
199 $split_rule->set(
201 description => $description,
202 split_routine => $split_routine,
205 eval {
206 $split_rule->regexs(\@split_regex)
207 if $split_routine eq 'RegEx';
208 $split_rule->store;
210 if ($@) {
211 push @messages,
212 { type => 'error', code => 'error_on_update_split_rule' };
214 else {
215 push @messages,
216 { type => 'message', code => 'success_on_update_split_rule' };
220 else {
221 $split_rule = Koha::ClassSplitRule->new(
223 class_split_rule => $class_split_rule,
224 description => $description,
225 split_routine => $split_routine,
226 regexs => \@split_regex,
229 eval { $split_rule->store; };
230 if ($@) {
231 warn $@;
232 push @messages,
233 { type => 'error', code => 'error_on_insert_split_rule' };
235 else {
236 push @messages,
237 { type => 'message', code => 'success_on_insert_split_rule' };
240 $op = 'list';
242 elsif ( $op eq "delete_split_rule" ) {
243 my $split_rule = Koha::ClassSplitRules->find($class_split_rule);
244 my $deleted = eval { $split_rule->delete };
245 if ( $@ or not $deleted ) {
246 push @messages,
247 { type => 'error', code => 'error_on_delete_split_rule' };
249 else {
250 push @messages,
251 { type => 'message', code => 'success_on_delete_split_rule' };
253 $op = 'list';
256 if ( $op eq 'list' ) {
257 my $class_sources = Koha::ClassSources->search;
258 my $sort_rules = Koha::ClassSortRules->search;
259 my $split_rules = Koha::ClassSplitRules->search;
260 $template->param(
261 class_sources => $class_sources,
262 sort_rules => $sort_rules,
263 split_rules => $split_rules,
267 $template->param( op => $op, messages => \@messages, );
268 output_html_with_http_headers $input, $cookie, $template->output;
270 exit 0;
272 sub get_class_sort_routines {
273 my @sort_routines = GetSortRoutineNames();
274 return \@sort_routines;
277 sub get_class_split_routines {
278 my @split_routines = GetSplitRoutineNames();
279 return \@split_routines;