From a1d8f17cdcf4535f912e70bef9cb020716f2631b Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 29 Jan 2024 10:31:23 +0000 Subject: [PATCH] Bug 28869: Implement authorised_value_categories.is_integer_only Test plan: Add category with/without integer restriction. Test adding/editing values. Signed-off-by: Marcel de Rooy Signed-off-by: Victor Grousset/tuxayo --- Koha/AuthorisedValue.pm | 39 ++++++++++++++- Koha/Exceptions.pm | 4 ++ admin/authorised_values.pl | 42 +++++++++++----- .../en/modules/admin/authorised_values.tt | 48 +++++++++++++++++-- 4 files changed, 117 insertions(+), 16 deletions(-) diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm index 407df68917..4cc9c6eed9 100644 --- a/Koha/AuthorisedValue.pm +++ b/Koha/AuthorisedValue.pm @@ -19,14 +19,18 @@ package Koha::AuthorisedValue; use Modern::Perl; -use Koha::Caches; -use Koha::Database; use Koha::Object; use Koha::Object::Limit::Library; +use Koha::Caches; +use Koha::Database; +use Koha::Exceptions; # Bug 35959 - Avoid Inconsistent hierarchy warn by replacing use base line @Koha::AuthorisedValue::ISA = qw(Koha::Object Koha::Object::Limit::Library); +use constant NUM_PATTERN => q{^(-[1-9][0-9]*|0|[1-9][0-9]*)$}; +use constant NUM_PATTERN_JS => q{(-[1-9][0-9]*|0|[1-9][0-9]*)}; # ^ and $ removed + my $cache = Koha::Caches->get_instance(); =head1 NAME @@ -63,6 +67,8 @@ sub store { } } + $self->_check_is_integer_only; + $self = $self->SUPER::store; if ($flush) { @@ -116,8 +122,37 @@ sub to_api_mapping { }; } +=head3 is_integer_only + +This helper method tells you if the category for this value allows numbers only. + +=cut + +sub is_integer_only { + my $self = shift; + return $self->_result->category->is_integer_only; +} + =head2 Internal methods +=head3 _check_is_integer_only + + Raise an exception if the category only allows integer values and the value is not. + Otherwise returns true. + +=cut + +sub _check_is_integer_only { + my ($self) = @_; + my $pattern = NUM_PATTERN; + my $value = $self->authorised_value // q{}; + return 1 if $value =~ qr/${pattern}/; # no need to check category here yet + if ( $self->is_integer_only ) { + Koha::Exceptions::NoInteger->throw("'$value' is no integer value"); + } + return 1; +} + =head3 _type =cut diff --git a/Koha/Exceptions.pm b/Koha/Exceptions.pm index 57650afc18..0d5a1ac1ed 100644 --- a/Koha/Exceptions.pm +++ b/Koha/Exceptions.pm @@ -79,6 +79,10 @@ use Exception::Class ( isa => 'Koha::Exception', description => 'Koha is under maintenance.' }, + 'Koha::Exceptions::NoInteger' => { + isa => 'Koha::Exception', + description => 'Should be an integer.' + }, ); 1; diff --git a/admin/authorised_values.pl b/admin/authorised_values.pl index 2130e86819..d16c48df14 100755 --- a/admin/authorised_values.pl +++ b/admin/authorised_values.pl @@ -48,13 +48,14 @@ our ($template, $borrowernumber, $cookie)= get_template_and_user({ ################## ADD_FORM ################################## # called by default. Used to create form to add or modify a record -if ($op eq 'add_form') { - my ( @selected_branches, $category, $av ); +if ( $op eq 'add_form' or $op eq 'edit_form' ) { + my ( @selected_branches, $category, $category_name, $av ); if ($id) { $av = Koha::AuthorisedValues->new->find( $id ); @selected_branches = $av->library_limits ? $av->library_limits->as_list : (); } else { - $category = $input->param('category'); + $category_name = $input->param('category'); + $category = Koha::AuthorisedValueCategories->find($category_name); } my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } ); @@ -69,7 +70,7 @@ if ($op eq 'add_form') { if ($id) { $template->param(action_modify => 1); - } elsif ( ! $category ) { + } elsif ( ! $category_name ) { $template->param(action_add_category => 1); } else { $template->param(action_add_value => 1); @@ -83,12 +84,14 @@ if ($op eq 'add_form') { ); } else { $template->param( - category_name => $category, - imagesets => C4::Koha::getImageSets(), + category => $category, + category_name => $category_name, + imagesets => C4::Koha::getImageSets(), ); } $template->param( branches_loop => \@branches_loop, + num_pattern => Koha::AuthorisedValue::NUM_PATTERN_JS(), ); } elsif ($op eq 'add') { @@ -102,7 +105,7 @@ if ($op eq 'add_form') { : $image ); my $duplicate_entry = 0; - my @branches = grep { $_ ne q{} } $input->multi_param('branches'); + my @branches = grep { $_ ne q{} } $input->multi_param('branches'); if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) { push @messages, {type => 'error', code => 'invalid_category_name' }; @@ -150,7 +153,8 @@ if ($op eq 'add_form') { $op = 'list'; $searchfield = $new_category; } elsif ($op eq 'add_category' ) { - my $new_category = $input->param('category'); + my $new_category = $input->param('category'); + my $is_integer_only = $input->param('is_integer_only') ? 1 : 0; my $already_exists = Koha::AuthorisedValueCategories->find( { @@ -167,7 +171,7 @@ if ($op eq 'add_form') { } else { # Insert my $av = Koha::AuthorisedValueCategory->new( { - category_name => $new_category, + category_name => $new_category, is_integer_only => $is_integer_only, } ); eval { @@ -182,6 +186,18 @@ if ($op eq 'add_form') { } } + $op = 'list'; +} elsif ( $op eq 'edit_category' ) { + my $category_name = $input->param('category'); + my $is_integer_only = $input->param('is_integer_only') ? 1 : 0; + my $category = Koha::AuthorisedValueCategories->find($category_name); + + if ($category) { + $category->is_integer_only($is_integer_only)->store; + } else { + push @messages, {type => 'error', code => 'error_on_edit_cat' }; + } + $op = 'list'; } elsif ($op eq 'delete') { my $av = Koha::AuthorisedValues->new->find( $id ); @@ -214,18 +230,21 @@ $template->param( if ( $op eq 'list' ) { # build categories list - my @category_names = Koha::AuthorisedValueCategories->search( + my $category_rs = Koha::AuthorisedValueCategories->search( { category_name => { -not_in => [ '', 'branches', 'itemtypes', 'cn_source' ] } }, { order_by => ['category_name'] } - )->get_column('category_name'); + ); + my @category_names = $category_rs->get_column('category_name'); $searchfield ||= ""; my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } )->as_list; my @loop_data = (); + my $category = $category_rs->find($searchfield); + my $is_integer_only = $category && $category->is_integer_only; # builds value list for my $av ( @avs_by_category ) { my %row_data; # get a fresh hash for the row data @@ -236,6 +255,7 @@ if ( $op eq 'list' ) { $row_data{image} = getitemtypeimagelocation( 'intranet', $av->imageurl ); $row_data{branches} = $av->library_limits ? $av->library_limits->as_list : []; $row_data{id} = $av->id; + $row_data{is_integer_only} = $is_integer_only; push(@loop_data, \%row_data); } 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 8ae422c444..aa9045241a 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 @@ -49,6 +49,9 @@ float: none; width: auto; } + #authorised_value:invalid { + color:red; + } [% END %] @@ -110,11 +113,12 @@
[% INCLUDE 'messages.inc' %] - [% IF op == 'add_form' %] + [% IF op == 'add_form' OR op == 'edit_form' %]

[% IF ( action_modify ) %]Modify authorized value[% END %] [% IF ( action_add_value ) %]New authorized value[% END %] [% IF ( action_add_category ) %]New category[% END %] + [% IF ( op == 'edit_form' ) %]Edit category[% END %]

[% IF ( action_modify ) %] @@ -123,7 +127,7 @@ [% END %] -
+
[% IF action_add_category %]
    @@ -133,7 +137,27 @@ Required +
  1. + +
  2. +
+ [% ELSIF op == 'edit_form' %] +
    +
  1. + + +
  2. +
  3. + [% IF category.is_integer_only %] + + [% ELSE %] + + [% END %] +
+ + + [% ELSE %]
  1. @@ -146,7 +170,11 @@ [% IF ( action_modify ) %] [% END %] - + [% IF ( av && av.is_integer_only ) || category.is_integer_only %] + + [% ELSE %] + + [% END %]
  2. @@ -188,6 +216,7 @@ @@ -212,6 +241,8 @@ 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_edit_cat' %] + An error occurred when updating this authorized value category. [% CASE 'error_on_insert_cat' %] An error occurred when inserting this authorized value category. Perhaps the category name already exists. [% CASE 'error_on_delete' %] @@ -389,6 +420,17 @@ if( $("#icons .tab-pane.active").length < 1 ){ $("#icons a:first").tab("show"); } + + $("#Aform").submit(function() { + if ( $('#authorised_value').length ) { + if ( ! $('#authorised_value').get(0).checkValidity() ) { + alert( _("Authorised value should be numeric.") ); + $('#authorised_value').focus(); + return false; + } + } + return true; + }); }); [% END %] -- 2.44.0