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>.
29 use Koha::AuthorisedValues;
32 my $id = $input->param('id');
33 my $op = $input->param('op') || 'list';
34 my $searchfield = $input->param('searchfield');
35 $searchfield = '' unless defined $searchfield;
36 $searchfield =~ s/\,//g;
39 our ($template, $borrowernumber, $cookie)= get_template_and_user({
40 template_name => "admin/authorised_values.tt",
42 flagsrequired => {parameters => 'parameters_remaining_permissions'},
48 ################## ADD_FORM ##################################
49 # called by default. Used to create form to add or modify a record
50 if ($op eq 'add_form') {
51 my ( $selected_branches, $category, $av );
53 $av = Koha::AuthorisedValues->new->find( $id );
54 $selected_branches = $av->branch_limitations;
56 $category = $input->param('category');
59 my $branches = GetBranches;
62 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
63 my $selected = ( grep {$_ eq $branchcode} @$selected_branches ) ? 1 : 0;
64 push @branches_loop, {
65 branchcode => $branchcode,
66 branchname => $branches->{$branchcode}->{branchname},
67 selected => $selected,
72 $template->param(action_modify => 1);
73 } elsif ( ! $category ) {
74 $template->param(action_add_category => 1);
76 $template->param(action_add_value => 1);
81 category => $av->category,
82 authorised_value => $av->authorised_value,
84 lib_opac => $av->lib_opac,
86 imagesets => C4::Koha::getImageSets( checked => $av->imageurl ),
90 category => $category,
91 imagesets => C4::Koha::getImageSets(),
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->param('branches');
106 if ( $id ) { # Update
107 my $av = Koha::AuthorisedValues->new->find( $id );
109 $av->lib( $input->param('lib') || undef );
110 $av->lib_opac( $input->param('lib_opac') || undef );
111 $av->category( $new_category );
112 $av->authorised_value( $new_authorised_value );
113 $av->imageurl( $imageurl );
116 $av->replace_branch_limitations( \@branches );
119 push @messages, {type => 'error', code => 'error_on_update' };
121 push @messages, { type => 'message', code => 'success_on_update' };
125 my $av = Koha::AuthorisedValue->new( {
126 category => $new_category,
127 authorised_value => $new_authorised_value,
128 lib => $input->param('lib') || undef,
129 lib_opac => $input->param('lib_opac') || undef,
130 imageurl => $imageurl,
135 $av->replace_branch_limitations( \@branches );
139 push @messages, {type => 'error', code => 'error_on_insert' };
141 push @messages, { type => 'message', code => 'success_on_insert' };
146 $searchfield = $new_category;
147 } elsif ($op eq 'delete') {
148 my $av = Koha::AuthorisedValues->new->find( $input->param('id') );
149 my $deleted = eval {$av->delete};
150 if ( $@ or not $deleted ) {
151 push @messages, {type => 'error', code => 'error_on_delete' };
153 push @messages, { type => 'message', code => 'success_on_delete' };
157 $template->param( delete_success => 1 );
162 searchfield => $searchfield,
163 messages => \@messages,
166 if ( $op eq 'list' ) {
167 # build categories list
168 my @categories = Koha::AuthorisedValues->new->categories;
170 my %categories; # a hash, to check that some hardcoded categories exist.
171 for my $category ( @categories ) {
172 push( @category_list, $category );
173 $categories{$category} = 1;
176 # push koha system categories
177 foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST REPORT_GROUP REPORT_SUBGROUP DEPARTMENT TERM SUGGEST_STATUS)) {
178 push @category_list, $_ unless $categories{$_};
182 @category_list = sort {$a cmp $b} @category_list;
184 $searchfield ||= $category_list[0];
186 my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );
189 for my $av ( @avs_by_category ) {
190 my %row_data; # get a fresh hash for the row data
191 $row_data{category} = $av->category;
192 $row_data{authorised_value} = $av->authorised_value;
193 $row_data{lib} = $av->lib;
194 $row_data{lib_opac} = $av->lib_opac;
195 $row_data{imageurl} = getitemtypeimagelocation( 'intranet', $av->imageurl );
196 $row_data{branches} = $av->branch_limitations;
197 $row_data{id} = $av->id;
198 push(@loop_data, \%row_data);
203 category => $searchfield,
204 categories => \@category_list,
208 output_html_with_http_headers $input, $cookie, $template->output;