Bug 25650: Add location and itype descriptions in ILS-DI GetRecords
[koha.git] / admin / authorised_values.pl
blob62cabcc1b327308229c6a2866adc10e4558b045a
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use List::MoreUtils qw(any);
25 use C4::Auth;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Output;
30 use Koha::AuthorisedValues;
31 use Koha::AuthorisedValueCategories;
32 use Koha::Libraries;
34 my $input = CGI->new;
35 my $id = $input->param('id');
36 my $op = $input->param('op') || 'list';
37 my $searchfield = $input->param('searchfield');
38 $searchfield = '' unless defined $searchfield;
39 $searchfield =~ s/\,//g;
40 my @messages;
42 our ($template, $borrowernumber, $cookie)= get_template_and_user({
43 template_name => "admin/authorised_values.tt",
44 flagsrequired => {parameters => 'manage_auth_values'},
45 query => $input,
46 type => "intranet",
47 debug => 1,
48 });
50 ################## ADD_FORM ##################################
51 # called by default. Used to create form to add or modify a record
52 if ($op eq 'add_form') {
53 my ( @selected_branches, $category, $av );
54 if ($id) {
55 $av = Koha::AuthorisedValues->new->find( $id );
56 @selected_branches = $av->library_limits ? $av->library_limits->as_list : ();
57 } else {
58 $category = $input->param('category');
61 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
62 my @branches_loop;
63 while ( my $branch = $branches->next ) {
64 push @branches_loop, {
65 branchcode => $branch->branchcode,
66 branchname => $branch->branchname,
67 selected => any {$_->branchcode eq $branch->branchcode} @selected_branches,
71 if ($id) {
72 $template->param(action_modify => 1);
73 } elsif ( ! $category ) {
74 $template->param(action_add_category => 1);
75 } else {
76 $template->param(action_add_value => 1);
79 if ( $av ) {
80 $template->param(
81 category_name => $av->category,
82 authorised_value => $av->authorised_value,
83 lib => $av->lib,
84 lib_opac => $av->lib_opac,
85 id => $av->id,
86 imagesets => C4::Koha::getImageSets( checked => $av->imageurl ),
88 } else {
89 $template->param(
90 category_name => $category,
91 imagesets => C4::Koha::getImageSets(),
94 $template->param(
95 branches_loop => \@branches_loop,
98 } elsif ($op eq 'add') {
99 my $new_authorised_value = $input->param('authorised_value');
100 my $new_category = $input->param('category');
101 my $imageurl = $input->param( 'imageurl' ) || '';
102 $imageurl = '' if $imageurl =~ /removeImage/;
103 my $duplicate_entry = 0;
104 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
106 if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
107 push @messages, {type => 'error', code => 'invalid_category_name' };
109 elsif ( $id ) { # Update
110 my $av = Koha::AuthorisedValues->new->find( $id );
112 $av->lib( scalar $input->param('lib') || undef );
113 $av->lib_opac( scalar $input->param('lib_opac') || undef );
114 $av->category( $new_category );
115 $av->authorised_value( $new_authorised_value );
116 $av->imageurl( $imageurl );
117 eval{
118 $av->store;
119 $av->replace_library_limits( \@branches );
121 if ( $@ ) {
122 push @messages, {type => 'error', code => 'error_on_update' };
123 } else {
124 push @messages, { type => 'message', code => 'success_on_update' };
127 else { # Insert
128 eval {
129 my $av = Koha::AuthorisedValue->new(
131 category => $new_category,
132 authorised_value => $new_authorised_value,
133 lib => scalar $input->param('lib') || undef,
134 lib_opac => scalar $input->param('lib_opac') || undef,
135 imageurl => $imageurl,
137 )->store;
138 $av->replace_library_limits( \@branches );
139 $av->store;
142 if ( $@ ) {
143 push @messages, {type => 'error', code => 'error_on_insert' };
144 } else {
145 push @messages, { type => 'message', code => 'success_on_insert' };
149 $op = 'list';
150 $searchfield = $new_category;
151 } elsif ($op eq 'add_category' ) {
152 my $new_category = $input->param('category');
154 my $already_exists = Koha::AuthorisedValueCategories->find(
156 category_name => $new_category,
160 if ( $already_exists ) {
161 if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
162 push @messages, {type => 'error', code => 'invalid_category_name' };
163 } else {
164 push @messages, {type => 'error', code => 'cat_already_exists' };
167 else { # Insert
168 my $av = Koha::AuthorisedValueCategory->new( {
169 category_name => $new_category,
170 } );
172 eval {
173 $av->store;
176 if ( $@ ) {
177 push @messages, {type => 'error', code => 'error_on_insert_cat' };
178 } else {
179 push @messages, { type => 'message', code => 'success_on_insert_cat' };
180 $searchfield = $new_category;
184 $op = 'list';
185 } elsif ($op eq 'delete') {
186 my $av = Koha::AuthorisedValues->new->find( $id );
187 my $deleted = eval {$av->delete};
188 if ( $@ or not $deleted ) {
189 push @messages, {type => 'error', code => 'error_on_delete' };
190 } else {
191 push @messages, { type => 'message', code => 'success_on_delete' };
194 $op = 'list';
195 } elsif ($op eq 'delete_category') {
196 my $category_name = $input->param('category_name');
197 my $avc = Koha::AuthorisedValueCategories->find( $category_name );
198 my $deleted = eval {$avc->delete};
199 if ( $@ or not $deleted ) {
200 push @messages, {type => 'error', code => 'error_on_delete_category' };
201 } else {
202 push @messages, { type => 'message', code => 'success_on_delete_category' };
205 $op = 'list';
208 $template->param(
209 op => $op,
210 searchfield => $searchfield,
211 messages => \@messages,
214 if ( $op eq 'list' ) {
215 # build categories list
216 my @categories = Koha::AuthorisedValueCategories->search({ category_name => { -not_in => ['', 'branches', 'itemtypes', 'cn_source']}}, { order_by => ['category_name'] } );
217 my @category_list;
218 for my $category ( @categories ) {
219 push( @category_list, $category->category_name );
222 $searchfield ||= $category_list[0];
224 my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );
225 my @loop_data = ();
226 # builds value list
227 for my $av ( @avs_by_category ) {
228 my %row_data; # get a fresh hash for the row data
229 $row_data{category} = $av->category;
230 $row_data{authorised_value} = $av->authorised_value;
231 $row_data{lib} = $av->lib;
232 $row_data{lib_opac} = $av->lib_opac;
233 $row_data{imageurl} = getitemtypeimagelocation( 'intranet', $av->imageurl );
234 $row_data{branches} = $av->library_limits ? $av->library_limits->as_list : [];
235 $row_data{id} = $av->id;
236 push(@loop_data, \%row_data);
239 $template->param(
240 loop => \@loop_data,
241 category => Koha::AuthorisedValueCategories->find($searchfield), # TODO Move this up and add a Koha::AVC->authorised_values method to replace call for avs_by_category
242 categories => \@category_list,
246 output_html_with_http_headers $input, $cookie, $template->output;