View | Details | Raw Unified | Return to bug 28869
Collapse All | Expand All

(-)a/Koha/AuthorisedValue.pm (+37 lines)
Lines 21-29 use Modern::Perl; Link Here
21
21
22
use Koha::Caches;
22
use Koha::Caches;
23
use Koha::Database;
23
use Koha::Database;
24
use Koha::Exceptions;
25
use Koha::Object;
26
use Koha::Object::Limit::Library;
24
27
25
use base qw(Koha::Object Koha::Object::Limit::Library);
28
use base qw(Koha::Object Koha::Object::Limit::Library);
26
29
30
use constant NUM_PATTERN    => q{^(-[1-9][0-9]*|0|[1-9][0-9]*)$};
31
use constant NUM_PATTERN_JS => q{(-[1-9][0-9]*|0|[1-9][0-9]*)};     # ^ and $ removed
32
27
my $cache = Koha::Caches->get_instance();
33
my $cache = Koha::Caches->get_instance();
28
34
29
=head1 NAME
35
=head1 NAME
Lines 60-65 sub store { Link Here
60
        }
66
        }
61
    }
67
    }
62
68
69
    $self->_check_is_integer_only;
70
63
    $self = $self->SUPER::store;
71
    $self = $self->SUPER::store;
64
72
65
    if ($flush) {
73
    if ($flush) {
Lines 113-120 sub to_api_mapping { Link Here
113
    };
121
    };
114
}
122
}
115
123
124
=head3 is_integer_only
125
126
This helper method tells you if the category for this value allows numbers only.
127
128
=cut
129
130
sub is_integer_only {
131
    my $self = shift;
132
    return $self->_result->category->is_integer_only;
133
}
134
116
=head2 Internal methods
135
=head2 Internal methods
117
136
137
=head3 _check_is_integer_only
138
139
    Raise an exception if the category only allows integer values and the value is not.
140
    Otherwise returns true.
141
142
=cut
143
144
sub _check_is_integer_only {
145
    my ($self)  = @_;
146
    my $pattern = NUM_PATTERN;
147
    my $value   = $self->authorised_value // q{};
148
    return 1 if $value =~ qr/${pattern}/;    # no need to check category here yet
149
    if ( $self->is_integer_only ) {
150
        Koha::Exceptions::NoInteger->throw("'$value' is no integer value");
151
    }
152
    return 1;
153
}
154
118
=head3 _type
155
=head3 _type
119
156
120
=cut
157
=cut
(-)a/Koha/Exceptions.pm (+4 lines)
Lines 79-84 use Exception::Class ( Link Here
79
        isa => 'Koha::Exception',
79
        isa => 'Koha::Exception',
80
        description => 'Koha is under maintenance.'
80
        description => 'Koha is under maintenance.'
81
    },
81
    },
82
    'Koha::Exceptions::NoInteger' => {
83
        isa         => 'Koha::Exception',
84
        description => 'Should be an integer.'
85
    },
82
);
86
);
83
87
84
1;
88
1;
(-)a/admin/authorised_values.pl (-11 / +31 lines)
Lines 48-60 our ($template, $borrowernumber, $cookie)= get_template_and_user({ Link Here
48
48
49
################## ADD_FORM ##################################
49
################## ADD_FORM ##################################
50
# called by default. Used to create form to add or  modify a record
50
# called by default. Used to create form to add or  modify a record
51
if ($op eq 'add_form') {
51
if ( $op eq 'add_form' or $op eq 'edit_form' ) {
52
    my ( @selected_branches, $category, $av );
52
    my ( @selected_branches, $category, $category_name, $av );
53
    if ($id) {
53
    if ($id) {
54
        $av = Koha::AuthorisedValues->new->find( $id );
54
        $av = Koha::AuthorisedValues->new->find( $id );
55
        @selected_branches = $av->library_limits ? $av->library_limits->as_list : ();
55
        @selected_branches = $av->library_limits ? $av->library_limits->as_list : ();
56
    } else {
56
    } else {
57
        $category = $input->param('category');
57
        $category_name = $input->param('category');
58
        $category      = Koha::AuthorisedValueCategories->find($category_name);
58
    }
59
    }
59
60
60
    my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
61
    my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
Lines 69-75 if ($op eq 'add_form') { Link Here
69
70
70
	if ($id) {
71
	if ($id) {
71
		$template->param(action_modify => 1);
72
		$template->param(action_modify => 1);
72
   } elsif ( ! $category ) {
73
   } elsif ( ! $category_name ) {
73
		$template->param(action_add_category => 1);
74
		$template->param(action_add_category => 1);
74
	} else {
75
	} else {
75
		$template->param(action_add_value => 1);
76
		$template->param(action_add_value => 1);
Lines 83-94 if ($op eq 'add_form') { Link Here
83
        );
84
        );
84
    } else {
85
    } else {
85
        $template->param(
86
        $template->param(
86
            category_name  => $category,
87
            category      => $category,
87
            imagesets => C4::Koha::getImageSets(),
88
            category_name => $category_name,
89
            imagesets     => C4::Koha::getImageSets(),
88
        );
90
        );
89
    }
91
    }
90
    $template->param(
92
    $template->param(
91
        branches_loop    => \@branches_loop,
93
        branches_loop    => \@branches_loop,
94
        num_pattern      => Koha::AuthorisedValue::NUM_PATTERN_JS(),
92
    );
95
    );
93
96
94
} elsif ($op eq 'cud-add') {
97
} elsif ($op eq 'cud-add') {
Lines 102-108 if ($op eq 'add_form') { Link Here
102
        : $image
105
        : $image
103
      );
106
      );
104
    my $duplicate_entry = 0;
107
    my $duplicate_entry = 0;
105
    my @branches = grep { $_ ne q{} } $input->multi_param('branches');
108
    my @branches        = grep { $_ ne q{} } $input->multi_param('branches');
106
109
107
    if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
110
    if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
108
        push @messages, {type => 'error', code => 'invalid_category_name' };
111
        push @messages, {type => 'error', code => 'invalid_category_name' };
Lines 150-156 if ($op eq 'add_form') { Link Here
150
    $op = 'list';
153
    $op = 'list';
151
    $searchfield = $new_category;
154
    $searchfield = $new_category;
152
} elsif ($op eq 'cud-add_category' ) {
155
} elsif ($op eq 'cud-add_category' ) {
153
    my $new_category = $input->param('category');
156
    my $new_category    = $input->param('category');
157
    my $is_integer_only = $input->param('is_integer_only') ? 1 : 0;
154
158
155
    my $already_exists = Koha::AuthorisedValueCategories->find(
159
    my $already_exists = Koha::AuthorisedValueCategories->find(
156
        {
160
        {
Lines 167-173 if ($op eq 'add_form') { Link Here
167
    }
171
    }
168
    else { # Insert
172
    else { # Insert
169
        my $av = Koha::AuthorisedValueCategory->new( {
173
        my $av = Koha::AuthorisedValueCategory->new( {
170
            category_name => $new_category,
174
            category_name => $new_category, is_integer_only => $is_integer_only,
171
        } );
175
        } );
172
176
173
        eval {
177
        eval {
Lines 182-187 if ($op eq 'add_form') { Link Here
182
        }
186
        }
183
    }
187
    }
184
188
189
    $op = 'list';
190
} elsif ( $op eq 'edit_category' ) {
191
    my $category_name   = $input->param('category');
192
    my $is_integer_only = $input->param('is_integer_only') ? 1 : 0;
193
    my $category        = Koha::AuthorisedValueCategories->find($category_name);
194
195
    if ($category) {
196
        $category->is_integer_only($is_integer_only)->store;
197
    } else {
198
        push @messages, {type => 'error', code => 'error_on_edit_cat' };
199
    }
200
185
    $op = 'list';
201
    $op = 'list';
186
} elsif ($op eq 'cud-delete') {
202
} elsif ($op eq 'cud-delete') {
187
    my $av = Koha::AuthorisedValues->new->find( $id );
203
    my $av = Koha::AuthorisedValues->new->find( $id );
Lines 214-231 $template->param( Link Here
214
230
215
if ( $op eq 'list' ) {
231
if ( $op eq 'list' ) {
216
    # build categories list
232
    # build categories list
217
    my @category_names = Koha::AuthorisedValueCategories->search(
233
    my $category_rs = Koha::AuthorisedValueCategories->search(
218
        {
234
        {
219
            category_name =>
235
            category_name =>
220
              { -not_in => [ '', 'branches', 'itemtypes', 'cn_source' ] }
236
              { -not_in => [ '', 'branches', 'itemtypes', 'cn_source' ] }
221
        },
237
        },
222
        { order_by => ['category_name'] }
238
        { order_by => ['category_name'] }
223
    )->get_column('category_name');
239
    );
240
    my @category_names = $category_rs->get_column('category_name');
224
241
225
    $searchfield ||= "";
242
    $searchfield ||= "";
226
243
227
    my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } )->as_list;
244
    my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } )->as_list;
228
    my @loop_data = ();
245
    my @loop_data = ();
246
    my $category        = $category_rs->find($searchfield);
247
    my $is_integer_only = $category && $category->is_integer_only;
229
    # builds value list
248
    # builds value list
230
    for my $av ( @avs_by_category ) {
249
    for my $av ( @avs_by_category ) {
231
        my %row_data;  # get a fresh hash for the row data
250
        my %row_data;  # get a fresh hash for the row data
Lines 236-241 if ( $op eq 'list' ) { Link Here
236
        $row_data{image}                 = getitemtypeimagelocation( 'intranet', $av->imageurl );
255
        $row_data{image}                 = getitemtypeimagelocation( 'intranet', $av->imageurl );
237
        $row_data{branches}              = $av->library_limits ? $av->library_limits->as_list : [];
256
        $row_data{branches}              = $av->library_limits ? $av->library_limits->as_list : [];
238
        $row_data{id}                    = $av->id;
257
        $row_data{id}                    = $av->id;
258
        $row_data{is_integer_only}       = $is_integer_only;
239
        push(@loop_data, \%row_data);
259
        push(@loop_data, \%row_data);
240
    }
260
    }
241
261
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt (-4 / +45 lines)
Lines 49-54 Link Here
49
            float: none;
49
            float: none;
50
            width: auto;
50
            width: auto;
51
        }
51
        }
52
        #authorised_value:invalid {
53
            color:red;
54
        }
52
    </style>
55
    </style>
53
[% END %]
56
[% END %]
54
</head>
57
</head>
Lines 110-120 Link Here
110
            <main>
113
            <main>
111
                [% INCLUDE 'messages.inc' %]
114
                [% INCLUDE 'messages.inc' %]
112
115
113
                [% IF op == 'add_form' %]
116
                [% IF op == 'add_form' OR op == 'edit_form' %]
114
                    <h1>
117
                    <h1>
115
                        [% IF ( action_modify ) %]<span>Modify authorized value</span>[% END %]
118
                        [% IF ( action_modify ) %]<span>Modify authorized value</span>[% END %]
116
                        [% IF ( action_add_value ) %]<span>New authorized value</span>[% END %]
119
                        [% IF ( action_add_value ) %]<span>New authorized value</span>[% END %]
117
                        [% IF ( action_add_category ) %]<span>New category</span>[% END %]
120
                        [% IF ( action_add_category ) %]<span>New category</span>[% END %]
121
                        [% IF ( op == 'edit_form' ) %]<span>Edit category</span>[% END %]
118
                    </h1>
122
                    </h1>
119
123
120
                    [% IF ( action_modify ) %]
124
                    [% IF ( action_modify ) %]
Lines 123-129 Link Here
123
                        </div>
127
                        </div>
124
                    [% END %]
128
                    [% END %]
125
129
126
                    <form action="/cgi-bin/koha/admin/authorised_values.pl" name="Aform" method="post" class="validated">
130
                    <form action="/cgi-bin/koha/admin/authorised_values.pl" name="Aform" id="Aform" method="post" class="validated">
127
                        [% INCLUDE 'csrf-token.inc' %]
131
                        [% INCLUDE 'csrf-token.inc' %]
128
                        <fieldset class="rows">
132
                        <fieldset class="rows">
129
                            [% IF action_add_category %]
133
                            [% IF action_add_category %]
Lines 134-140 Link Here
134
                                        <span class="required">Required</span>
138
                                        <span class="required">Required</span>
135
                                        <input type="hidden" name="op" value="cud-add_category" />
139
                                        <input type="hidden" name="op" value="cud-add_category" />
136
                                    </li>
140
                                    </li>
141
                                    <li>
142
                                        <input type="checkbox" name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input>
143
                                    </li>
144
                                </ol>
145
                            [% ELSIF op == 'edit_form' %]
146
                                <ol>
147
                                    <li>
148
                                        <label for="category" class="required">Category: </label>
149
                                        <input type="text" disabled value="[% category_name | html %]"/>
150
                                    </li>
151
                                    <li>
152
                                        [% IF category.is_integer_only %]
153
                                            <input type="checkbox" checked name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input>
154
                                        [% ELSE %]
155
                                            <input type="checkbox" name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input>
156
                                        [% END %]
157
                                    </li>
137
                                </ol>
158
                                </ol>
159
                                <input type="hidden" name="op" value="edit_category" />
160
                                <input type="hidden" name="category" value="[% category_name | html %]" />
161
                                <input type="hidden" name="searchfield" value="[% category_name | html %]" />
138
                            [% ELSE %]
162
                            [% ELSE %]
139
                                <ol>
163
                                <ol>
140
                                    <li>
164
                                    <li>
Lines 147-153 Link Here
147
                                        [% IF ( action_modify ) %]
171
                                        [% IF ( action_modify ) %]
148
                                            <input type="hidden" id="id" name="id" value="[% av.id | html %]" />
172
                                            <input type="hidden" id="id" name="id" value="[% av.id | html %]" />
149
                                        [% END %]
173
                                        [% END %]
150
                                        <input type="text" id="authorised_value" name="authorised_value" value="[% av.authorised_value | html %]" maxlength="80" class="focus" />
174
                                        [% IF ( av && av.is_integer_only ) || category.is_integer_only %]
175
                                            <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/>
176
                                        [% ELSE %]
177
                                            <input type="text" id="authorised_value" name="authorised_value" value="[% av.authorised_value | html %]" maxlength="80" class="focus"/>
178
                                        [% END %]
151
                                    </li>
179
                                    </li>
152
                                    <li>
180
                                    <li>
153
                                        <label for="lib">Description: </label>
181
                                        <label for="lib">Description: </label>
Lines 189-194 Link Here
189
                    <div id="toolbar" class="btn-toolbar">
217
                    <div id="toolbar" class="btn-toolbar">
190
                        <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>
218
                        <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>
191
                        [% IF ( searchfield ) %]
219
                        [% IF ( searchfield ) %]
220
                            <a id="editcat" class="btn btn-default" href= "/cgi-bin/koha/admin/authorised_values.pl?op=edit_form&amp;category=[% category.category_name | url %]"><i class="fa fa-plus"> </i> Edit category</a>
192
                            <a id="addauth" class="btn btn-default" href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;category=[% category.category_name | url %]"><i class="fa fa-plus"> </i> New authorized value for [% category.category_name | html %]</a>
221
                            <a id="addauth" class="btn btn-default" href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;category=[% category.category_name | url %]"><i class="fa fa-plus"> </i> New authorized value for [% category.category_name | html %]</a>
193
                        [% END %]
222
                        [% END %]
194
                    </div>
223
                    </div>
Lines 213-218 Link Here
213
                                <span>An error occurred when updating this authorized value. Perhaps the value already exists.</span>
242
                                <span>An error occurred when updating this authorized value. Perhaps the value already exists.</span>
214
                            [% CASE 'error_on_insert' %]
243
                            [% CASE 'error_on_insert' %]
215
                                <span>An error occurred when inserting this authorized value. Perhaps the value or the category already exists.</span>
244
                                <span>An error occurred when inserting this authorized value. Perhaps the value or the category already exists.</span>
245
                            [% CASE 'error_on_edit_cat' %]
246
                                <span>An error occurred when updating this authorized value category.</span>
216
                            [% CASE 'error_on_insert_cat' %]
247
                            [% CASE 'error_on_insert_cat' %]
217
                                <span>An error occurred when inserting this authorized value category. Perhaps the category name already exists.</span>
248
                                <span>An error occurred when inserting this authorized value category. Perhaps the category name already exists.</span>
218
                            [% CASE 'error_on_delete' %]
249
                            [% CASE 'error_on_delete' %]
Lines 399-404 Link Here
399
            if( $("#icons .tab-pane.active").length < 1 ){
430
            if( $("#icons .tab-pane.active").length < 1 ){
400
                $("#icons a:first").tab("show");
431
                $("#icons a:first").tab("show");
401
            }
432
            }
433
434
            $("#Aform").submit(function() {
435
                if ( $('#authorised_value').length ) {
436
                    if ( ! $('#authorised_value').get(0).checkValidity() ) {
437
                        alert( _("Authorised value should be numeric.") );
438
                        $('#authorised_value').focus();
439
                        return false;
440
                    }
441
                }
442
                return true;
443
            });
402
        });
444
        });
403
    </script>
445
    </script>
404
[% END %]
446
[% END %]
405
- 

Return to bug 28869