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

(-)a/Koha/AuthorisedValue.pm (-2 / +37 lines)
Lines 19-32 package Koha::AuthorisedValue; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Koha::Caches;
23
use Koha::Database;
24
use Koha::Object;
22
use Koha::Object;
25
use Koha::Object::Limit::Library;
23
use Koha::Object::Limit::Library;
24
use Koha::Caches;
25
use Koha::Database;
26
use Koha::Exceptions;
26
27
27
# Bug 35959 - Avoid Inconsistent hierarchy warn by replacing use base line
28
# Bug 35959 - Avoid Inconsistent hierarchy warn by replacing use base line
28
@Koha::AuthorisedValue::ISA = qw(Koha::Object Koha::Object::Limit::Library);
29
@Koha::AuthorisedValue::ISA = qw(Koha::Object Koha::Object::Limit::Library);
29
30
31
use constant NUM_PATTERN    => q{^(-[1-9][0-9]*|0|[1-9][0-9]*)$};
32
use constant NUM_PATTERN_JS => q{(-[1-9][0-9]*|0|[1-9][0-9]*)};     # ^ and $ removed
33
30
my $cache = Koha::Caches->get_instance();
34
my $cache = Koha::Caches->get_instance();
31
35
32
=head1 NAME
36
=head1 NAME
Lines 63-68 sub store { Link Here
63
        }
67
        }
64
    }
68
    }
65
69
70
    $self->_check_is_integer_only;
71
66
    $self = $self->SUPER::store;
72
    $self = $self->SUPER::store;
67
73
68
    if ($flush) {
74
    if ($flush) {
Lines 116-123 sub to_api_mapping { Link Here
116
    };
122
    };
117
}
123
}
118
124
125
=head3 is_integer_only
126
127
This helper method tells you if the category for this value allows numbers only.
128
129
=cut
130
131
sub is_integer_only {
132
    my $self = shift;
133
    return $self->_result->category->is_integer_only;
134
}
135
119
=head2 Internal methods
136
=head2 Internal methods
120
137
138
=head3 _check_is_integer_only
139
140
    Raise an exception if the category only allows integer values and the value is not.
141
    Otherwise returns true.
142
143
=cut
144
145
sub _check_is_integer_only {
146
    my ($self)  = @_;
147
    my $pattern = NUM_PATTERN;
148
    my $value   = $self->authorised_value // q{};
149
    return 1 if $value =~ qr/${pattern}/;    # no need to check category here yet
150
    if ( $self->is_integer_only ) {
151
        Koha::Exceptions::NoInteger->throw("'$value' is no integer value");
152
    }
153
    return 1;
154
}
155
121
=head3 _type
156
=head3 _type
122
157
123
=cut
158
=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 'add') {
97
} elsif ($op eq '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 'add_category' ) {
155
} elsif ($op eq '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 'delete') {
202
} elsif ($op eq '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
                        <fieldset class="rows">
131
                        <fieldset class="rows">
128
                            [% IF action_add_category %]
132
                            [% IF action_add_category %]
129
                                <ol>
133
                                <ol>
Lines 133-139 Link Here
133
                                        <span class="required">Required</span>
137
                                        <span class="required">Required</span>
134
                                        <input type="hidden" name="op" value="add_category" />
138
                                        <input type="hidden" name="op" value="add_category" />
135
                                    </li>
139
                                    </li>
140
                                    <li>
141
                                        <input type="checkbox" name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input>
142
                                    </li>
143
                                </ol>
144
                            [% ELSIF op == 'edit_form' %]
145
                                <ol>
146
                                    <li>
147
                                        <label for="category" class="required">Category: </label>
148
                                        <input type="text" disabled value="[% category_name | html %]"/>
149
                                    </li>
150
                                    <li>
151
                                        [% IF category.is_integer_only %]
152
                                            <input type="checkbox" checked name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input>
153
                                        [% ELSE %]
154
                                            <input type="checkbox" name="is_integer_only" id="is_integer_only"/><label for="is_integer_only">Restrict value to numbers only</label></input>
155
                                        [% END %]
156
                                    </li>
136
                                </ol>
157
                                </ol>
158
                                <input type="hidden" name="op" value="edit_category" />
159
                                <input type="hidden" name="category" value="[% category_name | html %]" />
160
                                <input type="hidden" name="searchfield" value="[% category_name | html %]" />
137
                            [% ELSE %]
161
                            [% ELSE %]
138
                                <ol>
162
                                <ol>
139
                                    <li>
163
                                    <li>
Lines 146-152 Link Here
146
                                        [% IF ( action_modify ) %]
170
                                        [% IF ( action_modify ) %]
147
                                            <input type="hidden" id="id" name="id" value="[% av.id | html %]" />
171
                                            <input type="hidden" id="id" name="id" value="[% av.id | html %]" />
148
                                        [% END %]
172
                                        [% END %]
149
                                        <input type="text" id="authorised_value" name="authorised_value" value="[% av.authorised_value | html %]" maxlength="80" class="focus" />
173
                                        [% IF ( av && av.is_integer_only ) || category.is_integer_only %]
174
                                            <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/>
175
                                        [% ELSE %]
176
                                            <input type="text" id="authorised_value" name="authorised_value" value="[% av.authorised_value | html %]" maxlength="80" class="focus"/>
177
                                        [% END %]
150
                                    </li>
178
                                    </li>
151
                                    <li>
179
                                    <li>
152
                                        <label for="lib">Description: </label>
180
                                        <label for="lib">Description: </label>
Lines 188-193 Link Here
188
                    <div id="toolbar" class="btn-toolbar">
216
                    <div id="toolbar" class="btn-toolbar">
189
                        <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>
217
                        <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>
190
                        [% IF ( searchfield ) %]
218
                        [% IF ( searchfield ) %]
219
                            <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>
191
                            <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>
220
                            <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>
192
                        [% END %]
221
                        [% END %]
193
                    </div>
222
                    </div>
Lines 212-217 Link Here
212
                                <span>An error occurred when updating this authorized value. Perhaps the value already exists.</span>
241
                                <span>An error occurred when updating this authorized value. Perhaps the value already exists.</span>
213
                            [% CASE 'error_on_insert' %]
242
                            [% CASE 'error_on_insert' %]
214
                                <span>An error occurred when inserting this authorized value. Perhaps the value or the category already exists.</span>
243
                                <span>An error occurred when inserting this authorized value. Perhaps the value or the category already exists.</span>
244
                            [% CASE 'error_on_edit_cat' %]
245
                                <span>An error occurred when updating this authorized value category.</span>
215
                            [% CASE 'error_on_insert_cat' %]
246
                            [% CASE 'error_on_insert_cat' %]
216
                                <span>An error occurred when inserting this authorized value category. Perhaps the category name already exists.</span>
247
                                <span>An error occurred when inserting this authorized value category. Perhaps the category name already exists.</span>
217
                            [% CASE 'error_on_delete' %]
248
                            [% CASE 'error_on_delete' %]
Lines 389-394 Link Here
389
            if( $("#icons .tab-pane.active").length < 1 ){
420
            if( $("#icons .tab-pane.active").length < 1 ){
390
                $("#icons a:first").tab("show");
421
                $("#icons a:first").tab("show");
391
            }
422
            }
423
424
            $("#Aform").submit(function() {
425
                if ( $('#authorised_value').length ) {
426
                    if ( ! $('#authorised_value').get(0).checkValidity() ) {
427
                        alert( _("Authorised value should be numeric.") );
428
                        $('#authorised_value').focus();
429
                        return false;
430
                    }
431
                }
432
                return true;
433
            });
392
        });
434
        });
393
    </script>
435
    </script>
394
[% END %]
436
[% END %]
395
- 

Return to bug 28869