use CGI qw ( -utf8 );
use C4::Auth;
-use C4::Branch;
use C4::Context;
use C4::Koha;
use C4::Output;
use Koha::AuthorisedValues;
+use Koha::AuthorisedValueCategories;
+use Koha::Libraries;
my $input = new CGI;
my $id = $input->param('id');
our ($template, $borrowernumber, $cookie)= get_template_and_user({
template_name => "admin/authorised_values.tt",
authnotrequired => 0,
- flagsrequired => {parameters => 'parameters_remaining_permissions'},
+ flagsrequired => {parameters => 'manage_auth_values'},
query => $input,
type => "intranet",
debug => 1,
$category = $input->param('category');
}
- my $branches = GetBranches;
+ my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
my @branches_loop;
-
- foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
- my $selected = ( grep {$_->{branchcode} eq $branchcode} @$selected_branches ) ? 1 : 0;
+ foreach my $branch ( @$branches ) {
+ my $selected = ( grep {$_ eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
push @branches_loop, {
- branchcode => $branchcode,
- branchname => $branches->{$branchcode}->{branchname},
- selected => $selected,
+ branchcode => $branch->{branchcode},
+ branchname => $branch->{branchname},
+ selected => $selected,
};
}
my $imageurl = $input->param( 'imageurl' ) || '';
$imageurl = '' if $imageurl =~ /removeImage/;
my $duplicate_entry = 0;
- my @branches = $input->param('branches');
+ my @branches = grep { $_ ne q{} } $input->multi_param('branches');
+
+ my $already_exists = Koha::AuthorisedValues->search(
+ {
+ category => $new_category,
+ authorised_value => $new_authorised_value,
+ }
+ )->next;
- if ( $id ) { # Update
+ if ( $already_exists and ( not $id or $already_exists->id != $id ) ) {
+ push @messages, {type => 'error', code => 'already_exists' };
+ }
+ elsif ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
+ push @messages, {type => 'error', code => 'invalid_category_name' };
+ }
+ elsif ( $id ) { # Update
my $av = Koha::AuthorisedValues->new->find( $id );
- $av->lib( $input->param('lib') || undef );
- $av->lib_opac( $input->param('lib_opac') || undef );
+ $av->lib( scalar $input->param('lib') || undef );
+ $av->lib_opac( scalar $input->param('lib_opac') || undef );
$av->category( $new_category );
$av->authorised_value( $new_authorised_value );
$av->imageurl( $imageurl );
eval{
$av->store;
- $av->branch_limitations( \@branches );
+ $av->replace_branch_limitations( \@branches );
};
if ( $@ ) {
push @messages, {type => 'error', code => 'error_on_update' };
my $av = Koha::AuthorisedValue->new( {
category => $new_category,
authorised_value => $new_authorised_value,
- lib => $input->param('lib') || undef,
- lib_opac => $input->param('lib_opac') || undef,
+ lib => scalar $input->param('lib') || undef,
+ lib_opac => scalar $input->param('lib_opac') || undef,
imageurl => $imageurl,
} );
+
eval {
$av->store;
- $av->branch_limitations( \@branches );
+ $av->replace_branch_limitations( \@branches );
};
+
if ( $@ ) {
push @messages, {type => 'error', code => 'error_on_insert' };
} else {
$op = 'list';
$searchfield = $new_category;
+} elsif ($op eq 'add_category' ) {
+ my $new_category = $input->param('category');
+
+ my $already_exists = Koha::AuthorisedValueCategories->find(
+ {
+ category_name => $new_category,
+ }
+ );
+
+ if ( $already_exists ) {
+ if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
+ push @messages, {type => 'error', code => 'invalid_category_name' };
+ } else {
+ push @messages, {type => 'error', code => 'cat_already_exists' };
+ }
+ }
+ else { # Insert
+ my $av = Koha::AuthorisedValueCategory->new( {
+ category_name => $new_category,
+ } );
+
+ eval {
+ $av->store;
+ };
+
+ if ( $@ ) {
+ push @messages, {type => 'error', code => 'error_on_insert_cat' };
+ } else {
+ push @messages, { type => 'message', code => 'success_on_insert_cat' };
+ $searchfield = $new_category;
+ }
+ }
+
+ $op = 'list';
} elsif ($op eq 'delete') {
- my $av = Koha::AuthorisedValues->new->find( $input->param('id') );
+ my $av = Koha::AuthorisedValues->new->find( $id );
my $deleted = eval {$av->delete};
if ( $@ or not $deleted ) {
push @messages, {type => 'error', code => 'error_on_delete' };
if ( $op eq 'list' ) {
# build categories list
- my @categories = Koha::AuthorisedValues->new->categories;
+ my @categories = Koha::AuthorisedValueCategories->search({ category_name => { -not_in => ['', 'branches', 'itemtypes', 'cn_source']}}, { order_by => ['category_name'] } );
my @category_list;
- my %categories; # a hash, to check that some hardcoded categories exist.
for my $category ( @categories ) {
- push( @category_list, $category );
- $categories{$category} = 1;
- }
-
- # push koha system categories
- foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST REPORT_GROUP REPORT_SUBGROUP DEPARTMENT TERM SUGGEST_STATUS)) {
- push @category_list, $_ unless $categories{$_};
+ push( @category_list, $category->category_name );
}
- #reorder the list
- @category_list = sort {$a cmp $b} @category_list;
-
$searchfield ||= $category_list[0];
my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );