diff --git a/admin/authorised_values.pl b/admin/authorised_values.pl index 49fb661825..f8120382c8 100755 --- a/admin/authorised_values.pl +++ b/admin/authorised_values.pl @@ -26,6 +26,7 @@ use C4::Koha; use C4::Output; use Koha::AuthorisedValues; +use Koha::AuthorisedValueCategories; use Koha::Libraries; my $input = new CGI; @@ -112,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 ); @@ -151,6 +155,36 @@ 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 ) { + 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' }; + } + } + $op = 'list'; $searchfield = $new_category; } elsif ($op eq 'delete') { @@ -174,22 +208,12 @@ $template->param( if ( $op eq 'list' ) { # build categories list - my @categories = Koha::AuthorisedValues->new->categories; + my @categories = Koha::AuthorisedValueCategories->search({}, { 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 ITEMTYPECAT)) { - 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 } ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt index 2aa3b5ffd6..f4f56cf75e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt @@ -77,25 +77,24 @@ $(document).ready(function() { [% IF ( action_modify ) %]
NOTE: If you change an authorized value code, existing records using it won't be updated. Changes to value descriptions will show immediately.
[% END %]
-
    -
  1. - [% IF ( action_add_category ) %] - - - Required - [% ELSE %]Category - [% category %] - [% END %] -
  2. + [% IF action_add_category %] +
  3. + + + Required + +
  4. + [% ELSE %] +
  5. + Category + + [% category %] +
  6. - [% IF ( action_modify ) %][% END %] - [% IF ( action_add_category ) %] - - [% ELSE %] + [% IF ( action_modify ) %][% END %] - [% END %]
  7. @@ -154,6 +153,7 @@ $(document).ready(function() { [% END %] + [% END %]
Cancel
@@ -178,16 +178,24 @@ $(document).ready(function() { An error occurred when updating this authorized value. Perhaps the value already exists. [% CASE 'error_on_insert' %] An error occurred when inserting this authorized value. Perhaps the value or the category already exists. + [% CASE 'error_on_insert_cat' %] + An error occurred when inserting this authorized value category. Perhaps the category name already exists. [% CASE 'error_on_delete' %] An error occurred when deleting this authorized value. Check the logs. [% CASE 'success_on_update' %] Authorized value updated successfully. [% CASE 'success_on_insert' %] Authorized value added successfully. + [% CASE 'success_on_insert_cat' %] + Authorized value category added successfully. [% CASE 'success_on_delete' %] Authorized value deleted successfully. [% CASE 'already_exists' %] This authorized value already exists. + [% CASE 'cat_already_exists' %] + This authorized value category already exists. + [% CASE 'invalid_category_name' %] + The authorized value category 'branches', 'itemtypes' and 'cn_source' are used internally by Koha and are not valid. [% CASE %] [% m.code %] [% END %]