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>.
23 use List
::MoreUtils
qw(any);
30 use Koha
::AuthorisedValues
;
31 use Koha
::AuthorisedValueCategories
;
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;
42 our ($template, $borrowernumber, $cookie)= get_template_and_user
({
43 template_name
=> "admin/authorised_values.tt",
45 flagsrequired
=> {parameters
=> 'manage_auth_values'},
51 ################## ADD_FORM ##################################
52 # called by default. Used to create form to add or modify a record
53 if ($op eq 'add_form') {
54 my ( @selected_branches, $category, $av );
56 $av = Koha
::AuthorisedValues
->new->find( $id );
57 @selected_branches = $av->library_limits ?
$av->library_limits->as_list : ();
59 $category = $input->param('category');
62 my $branches = Koha
::Libraries
->search( {}, { order_by
=> ['branchname'] } );
64 while ( my $branch = $branches->next ) {
65 push @branches_loop, {
66 branchcode
=> $branch->branchcode,
67 branchname
=> $branch->branchname,
68 selected
=> any
{$_->branchcode eq $branch->branchcode} @selected_branches,
73 $template->param(action_modify
=> 1);
74 } elsif ( ! $category ) {
75 $template->param(action_add_category
=> 1);
77 $template->param(action_add_value
=> 1);
82 category
=> $av->category,
83 authorised_value
=> $av->authorised_value,
85 lib_opac
=> $av->lib_opac,
87 imagesets
=> C4
::Koha
::getImageSets
( checked
=> $av->imageurl ),
91 category
=> $category,
92 imagesets
=> C4
::Koha
::getImageSets
(),
96 branches_loop
=> \
@branches_loop,
99 } elsif ($op eq 'add') {
100 my $new_authorised_value = $input->param('authorised_value');
101 my $new_category = $input->param('category');
102 my $imageurl = $input->param( 'imageurl' ) || '';
103 $imageurl = '' if $imageurl =~ /removeImage/;
104 my $duplicate_entry = 0;
105 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
107 my $already_exists = Koha
::AuthorisedValues
->search(
109 category
=> $new_category,
110 authorised_value
=> $new_authorised_value,
114 if ( $already_exists and ( not $id or $already_exists->id != $id ) ) {
115 push @messages, {type
=> 'error', code
=> 'already_exists' };
117 elsif ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
118 push @messages, {type
=> 'error', code
=> 'invalid_category_name' };
120 elsif ( $id ) { # Update
121 my $av = Koha
::AuthorisedValues
->new->find( $id );
123 $av->lib( scalar $input->param('lib') || undef );
124 $av->lib_opac( scalar $input->param('lib_opac') || undef );
125 $av->category( $new_category );
126 $av->authorised_value( $new_authorised_value );
127 $av->imageurl( $imageurl );
130 $av->replace_library_limits( \
@branches );
133 push @messages, {type
=> 'error', code
=> 'error_on_update' };
135 push @messages, { type
=> 'message', code
=> 'success_on_update' };
139 my $av = Koha
::AuthorisedValue
->new( {
140 category
=> $new_category,
141 authorised_value
=> $new_authorised_value,
142 lib
=> scalar $input->param('lib') || undef,
143 lib_opac
=> scalar $input->param('lib_opac') || undef,
144 imageurl
=> $imageurl,
149 $av->replace_library_limits( \
@branches );
153 push @messages, {type
=> 'error', code
=> 'error_on_insert' };
155 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
160 $searchfield = $new_category;
161 } elsif ($op eq 'add_category' ) {
162 my $new_category = $input->param('category');
164 my $already_exists = Koha
::AuthorisedValueCategories
->find(
166 category_name
=> $new_category,
170 if ( $already_exists ) {
171 if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
172 push @messages, {type
=> 'error', code
=> 'invalid_category_name' };
174 push @messages, {type
=> 'error', code
=> 'cat_already_exists' };
178 my $av = Koha
::AuthorisedValueCategory
->new( {
179 category_name
=> $new_category,
187 push @messages, {type
=> 'error', code
=> 'error_on_insert_cat' };
189 push @messages, { type
=> 'message', code
=> 'success_on_insert_cat' };
190 $searchfield = $new_category;
195 } elsif ($op eq 'delete') {
196 my $av = Koha
::AuthorisedValues
->new->find( $id );
197 my $deleted = eval {$av->delete};
198 if ( $@
or not $deleted ) {
199 push @messages, {type
=> 'error', code
=> 'error_on_delete' };
201 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
205 $template->param( delete_success
=> 1 );
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'] } );
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 } );
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);
241 category
=> $searchfield,
242 categories
=> \
@category_list,
246 output_html_with_http_headers
$input, $cookie, $template->output;