X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=admin%2Fauthorised_values.pl;h=bb94e239936aa1a2920cab8fdcf41ae89b645c94;hb=c6e488f4af72a2629fd86ee040a6973e2a6c73f4;hp=75343530c8d0d86b2388c619ec1f8e8d06ec3ddb;hpb=4b2b06311eb55d8a1175ee8d5e75eb465866199c;p=koha.git diff --git a/admin/authorised_values.pl b/admin/authorised_values.pl index 75343530c8..bb94e23993 100755 --- a/admin/authorised_values.pl +++ b/admin/authorised_values.pl @@ -21,12 +21,13 @@ use Modern::Perl; 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'); @@ -56,15 +57,14 @@ if ($op eq 'add_form') { $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 {$_ 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, }; } @@ -101,7 +101,7 @@ if ($op eq 'add_form') { my $imageurl = $input->param( 'imageurl' ) || ''; $imageurl = '' if $imageurl =~ /removeImage/; my $duplicate_entry = 0; - my @branches = grep { $_ ne q{} } $input->param('branches'); + my @branches = grep { $_ ne q{} } $input->multi_param('branches'); my $already_exists = Koha::AuthorisedValues->search( { @@ -113,6 +113,9 @@ if ($op eq 'add_form') { 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 ); @@ -135,8 +138,8 @@ if ($op eq 'add_form') { 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, } ); @@ -154,6 +157,40 @@ if ($op eq 'add_form') { $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 $deleted = eval {$av->delete}; @@ -175,22 +212,12 @@ $template->param( 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( @category_list, $category->category_name ); } - # 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{$_}; - } - - #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 } );