Bugzilla – Attachment 166699 Details for
Bug 28869
Optionally restrict authorised values to tinyint
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 28869: Implement authorised_value_categories.is_integer_only
Bug-28869-Implement-authorisedvaluecategoriesisint.patch (text/plain), 14.70 KB, created by
Martin Renvoize (ashimema)
on 2024-05-14 16:39:20 UTC
(
hide
)
Description:
Bug 28869: Implement authorised_value_categories.is_integer_only
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2024-05-14 16:39:20 UTC
Size:
14.70 KB
patch
obsolete
>From 3c323839b92eaf2c17aca8d637446c9a0fae251c Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >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 <m.de.rooy@rijksmuseum.nl> >Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net> >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> >--- > Koha/AuthorisedValue.pm | 37 ++++++++++++++ > Koha/Exceptions.pm | 4 ++ > admin/authorised_values.pl | 42 +++++++++++----- > .../en/modules/admin/authorised_values.tt | 48 +++++++++++++++++-- > 4 files changed, 117 insertions(+), 14 deletions(-) > >diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm >index da25d5e0584..f831e4b75b5 100644 >--- a/Koha/AuthorisedValue.pm >+++ b/Koha/AuthorisedValue.pm >@@ -21,9 +21,15 @@ use Modern::Perl; > > use Koha::Caches; > use Koha::Database; >+use Koha::Exceptions; >+use Koha::Object; >+use Koha::Object::Limit::Library; > > use base 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 >@@ -60,6 +66,8 @@ sub store { > } > } > >+ $self->_check_is_integer_only; >+ > $self = $self->SUPER::store; > > if ($flush) { >@@ -113,8 +121,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 57650afc18e..0d5a1ac1ed2 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 c40158f15dd..a05a4f77bf3 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 'cud-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 'cud-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 'cud-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 74543db7f7e..ed06b3afc1e 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; >+ } > </style> > [% END %] > </head> >@@ -110,11 +113,12 @@ > <main> > [% INCLUDE 'messages.inc' %] > >- [% IF op == 'add_form' %] >+ [% IF op == 'add_form' OR op == 'edit_form' %] > <h1> > [% IF ( action_modify ) %]<span>Modify authorized value</span>[% END %] > [% IF ( action_add_value ) %]<span>New authorized value</span>[% END %] > [% IF ( action_add_category ) %]<span>New category</span>[% END %] >+ [% IF ( op == 'edit_form' ) %]<span>Edit category</span>[% END %] > </h1> > > [% IF ( action_modify ) %] >@@ -123,7 +127,7 @@ > </div> > [% END %] > >- <form action="/cgi-bin/koha/admin/authorised_values.pl" name="Aform" method="post" class="validated"> >+ <form action="/cgi-bin/koha/admin/authorised_values.pl" name="Aform" id="Aform" method="post" class="validated"> > [% INCLUDE 'csrf-token.inc' %] > <fieldset class="rows"> > [% IF action_add_category %] >@@ -134,7 +138,27 @@ > <span class="required">Required</span> > <input type="hidden" name="op" value="cud-add_category" /> > </li> >+ <li> >+ <input type="checkbox" name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input> >+ </li> >+ </ol> >+ [% ELSIF op == 'edit_form' %] >+ <ol> >+ <li> >+ <label for="category" class="required">Category: </label> >+ <input type="text" disabled value="[% category_name | html %]"/> >+ </li> >+ <li> >+ [% IF category.is_integer_only %] >+ <input type="checkbox" checked name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input> >+ [% ELSE %] >+ <input type="checkbox" name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input> >+ [% END %] >+ </li> > </ol> >+ <input type="hidden" name="op" value="edit_category" /> >+ <input type="hidden" name="category" value="[% category_name | html %]" /> >+ <input type="hidden" name="searchfield" value="[% category_name | html %]" /> > [% ELSE %] > <ol> > <li> >@@ -147,7 +171,11 @@ > [% IF ( action_modify ) %] > <input type="hidden" id="id" name="id" value="[% av.id | html %]" /> > [% END %] >- <input type="text" id="authorised_value" name="authorised_value" value="[% av.authorised_value | html %]" maxlength="80" class="focus" /> >+ [% IF ( av && av.is_integer_only ) || category.is_integer_only %] >+ <input type="text" inputmode="numeric" pattern="[% num_pattern | $raw %]" id="authorised_value" name="authorised_value" value="[% av.authorised_value | html %]" class="focus" title="Should be numeric" required/> >+ [% ELSE %] >+ <input type="text" id="authorised_value" name="authorised_value" value="[% av.authorised_value | html %]" maxlength="80" class="focus"/> >+ [% END %] > </li> > <li> > <label for="lib">Description: </label> >@@ -189,6 +217,7 @@ > <div id="toolbar" class="btn-toolbar"> > <a id="addcat" class="btn btn-default" href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form"><i class="fa fa-plus"> </i> New category</a> > [% IF ( searchfield ) %] >+ <a id="editcat" class="btn btn-default" href= "/cgi-bin/koha/admin/authorised_values.pl?op=edit_form&category=[% category.category_name | url %]"><i class="fa fa-plus"> </i> Edit category</a> > <a id="addauth" class="btn btn-default" href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form&category=[% category.category_name | url %]"><i class="fa fa-plus"> </i> New authorized value for [% category.category_name | html %]</a> > [% END %] > </div> >@@ -213,6 +242,8 @@ > <span>An error occurred when updating this authorized value. Perhaps the value already exists.</span> > [% CASE 'error_on_insert' %] > <span>An error occurred when inserting this authorized value. Perhaps the value or the category already exists.</span> >+ [% CASE 'error_on_edit_cat' %] >+ <span>An error occurred when updating this authorized value category.</span> > [% CASE 'error_on_insert_cat' %] > <span>An error occurred when inserting this authorized value category. Perhaps the category name already exists.</span> > [% CASE 'error_on_delete' %] >@@ -399,6 +430,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; >+ }); > }); > </script> > [% END %] >-- >2.45.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 28869
:
161745
|
161746
|
161747
|
161748
|
161749
|
162412
|
162413
|
162414
|
162415
|
162416
|
163996
|
163997
|
163998
|
163999
|
164000
|
164001
|
166697
|
166698
| 166699 |
166700
|
166701
|
166702
|
166906