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

(-)a/admin/restrictions.pl (+112 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2019 PTFS Europe
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use CGI qw ( -utf8 );
23
use C4::Auth;
24
use C4::Output;
25
use Koha::RestrictionTypes;
26
27
my $input = new CGI;
28
my $op    = $input->param('op') // 'list';
29
my $code  = uc $input->param('code');
30
my @messages = ();
31
32
33
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34
    {
35
        template_name   => "admin/restrictions.tt",
36
        query           => $input,
37
        type            => "intranet",
38
        authnotrequired => 0,
39
        flagsrequired   => { parameters => 'manage_patron_restrictions' },
40
        debug           => 1,
41
    }
42
);
43
44
if ( $op eq 'add_form') {
45
    # Get all existing restrictions, so we can do client-side validation
46
    $template->param(
47
        existing => scalar Koha::RestrictionTypes->search()
48
    );
49
    if ($code) {
50
        $template->param(
51
            restriction => scalar Koha::RestrictionTypes->find($code)
52
        );
53
    }
54
} elsif ( $op eq 'add_validate' ) {
55
56
    my $display_text = $input->param('display_text');
57
    my $is_a_modif = $input->param("is_a_modif");
58
59
    if ($is_a_modif) {
60
        # Check whether another restriction already has this display text
61
        my $dupe = Koha::RestrictionTypes->find({
62
            display_text => $display_text
63
        });
64
        if ($dupe) {
65
            push @messages, {
66
                type => 'error', code => 'duplicate_display_text'
67
            };
68
        } else {
69
            my $restriction = Koha::RestrictionTypes->find($code);
70
            $restriction->display_text($display_text);
71
            $restriction->store;
72
        }
73
    } else {
74
        # Check whether another restriction already has this code
75
        my $dupe = Koha::RestrictionTypes->find($code);
76
        if ($dupe) {
77
            push @messages, {
78
                type => 'error', code => 'duplicate_code'
79
            };
80
        } else {
81
            my $restriction = Koha::RestrictionType->new({
82
                code => $code,
83
                display_text => $display_text
84
            });
85
            $restriction->store;
86
        }
87
    }
88
    $op = 'list';
89
} elsif ( $op eq 'delete_confirm' ) {
90
    $template->param(
91
        restriction => scalar Koha::RestrictionTypes->find($code)
92
    );
93
} elsif ( $op eq 'delete_confirmed' ) {
94
    Koha::RestrictionTypes->find($code)->delete;
95
    $op = 'list';
96
}
97
98
$template->param(
99
    messages => \@messages,
100
    op       => $op
101
);
102
103
if ( $op eq 'list' ) {
104
    my $restrictions = Koha::RestrictionTypes->search();
105
    $template->param(
106
        restrictions => $restrictions,
107
    )
108
}
109
110
output_html_with_http_headers $input, $cookie, $template->output;
111
112
exit 0;
(-)a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss (+6 lines)
Lines 4262-4267 input.renew { Link Here
4262
    background-color: rgba(0, 0, 0, 0.1);
4262
    background-color: rgba(0, 0, 0, 0.1);
4263
}
4263
}
4264
4264
4265
#restriction_form {
4266
    .type_input {
4267
        text-transform: uppercase;
4268
    }
4269
}
4270
4265
#stage_list_headings {
4271
#stage_list_headings {
4266
    font-weight: bold;
4272
    font-weight: bold;
4267
    span {
4273
    span {
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt (+174 lines)
Line 0 Link Here
1
[% USE raw %]
2
[% USE Asset %]
3
[% USE Koha %]
4
[% SET footerjs = 1 %]
5
[% INCLUDE 'doc-head-open.inc' %]
6
<title>Koha &rsaquo; Administration &rsaquo; Patron restrictions &rsaquo; [% IF op == 'add_form' %][% IF ( restriction ) %]Modify restriction '[% restriction.display_text | html %]'[% ELSE %]New restriction[% END %][% END %]
7
[% IF op == 'delete_confirm' %]Confirm deletion of restriction '[% restriction.display_text | html %]'[% END %]
8
</title>
9
[% INCLUDE 'doc-head-close.inc' %]
10
</head>
11
12
<body id="admin_restrictions" class="admin">
13
[% INCLUDE 'header.inc' %]
14
[% INCLUDE 'patrons-admin-search.inc' %]
15
16
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo; [% IF op == 'add_form' %] <a href="/cgi-bin/koha/admin/restrictions.pl">Patron restrictions</a> &rsaquo; [% IF ( restriction ) %]Modify restriction '[% restriction.display_text | html %]'[% ELSE %]New restriction[% END %][% END %]
17
[% IF op == 'delete_confirm' %] <a href="/cgi-bin/koha/admin/restrictions.pl">Patron restrictions</a> &rsaquo; Confirm deletion of restriction '[% restriction.display_text | html %]'[% END %]
18
[% IF op == 'delete_confirmed' %] <a href="/cgi-bin/koha/admin/categories.pl">Patron restrictions</a> &rsaquo; Restriction deleted[% END %]
19
[% IF op == 'list' %]Patron restrictions[% END %]</div>
20
21
<div class="main container-fluid">
22
    <div class="row">
23
        <div class="col-sm-10 col-sm-push-2">
24
            <main>
25
26
[% FOR m IN messages %]
27
    <div class="dialog [% m.type | html %]">
28
        [% SWITCH m.code %]
29
        [% CASE 'duplicate_display_text' %]
30
            Another restriction already has this label
31
        [% CASE 'duplicate_code' %]
32
            Another restriction already has this code
33
        [% CASE %]
34
            [% m.code | html %]
35
        [% END %]
36
    </div>
37
[% END %]
38
39
[% IF op == 'add_form' %]
40
    <form id="restriction_form" action="/cgi-bin/koha/admin/restrictions.pl" method="post">
41
        <input type="hidden" name="op" value="add_validate" />
42
        <input type="hidden" name="checked" value="0" />
43
        [% IF restriction %]
44
            <h1>Modify restriction [% restriction.display_text | html %]</h1>
45
            <input type="hidden" name="is_a_modif" value="1" />
46
        [% ELSE %]
47
            <h1>New restriction</h1>
48
        [% END %]
49
        <fieldset class="rows">
50
            <ol>
51
                [% IF restriction %]
52
                    <li>
53
                        <span class="label">Code: </span>[% restriction.code | html %]
54
                        <input type="hidden" name="code" value="[% restriction.code | html %]" />
55
                    </li>
56
                    <li>
57
                        <label for="display_text" class="required">Label: </label>
58
                        <input type="text" value="[% restriction.display_text | html %]" name="display_text" id="display_text" size="50" maxlength="50" class="required" required="required" />
59
                        <span class="required">Required</span>
60
                    </li>
61
                [% ELSE %]
62
                    <li>
63
                        <label for="code" class="required">Code: </label>
64
                        <input type="text" name="code" id="code" size="50" maxlength="50" class="required type_input " required="required" />
65
                        <span class="required">Required</span>
66
                    </li>
67
                    <li>
68
                        <label for="display_text" class="required">Label: </label>
69
                        <input type="text" name="display_text" id="display_text" size="50" maxlength="50" class="required" required="required" />
70
                        <span class="required">Required</span>
71
                    </li>
72
                [% END %]
73
            </ol>
74
        </fieldset>
75
76
        <fieldset class="action">
77
            <input type="submit" value="Save" />
78
            <a href="/cgi-bin/koha/admin/restrictions.pl" class="cancel">Cancel</a>
79
        </fieldset>
80
    </form>
81
[% END %]
82
83
[% IF op == 'delete_confirm' %]
84
    <form action="/cgi-bin/koha/admin/restrictions.pl" method="post">
85
        <fieldset>
86
            <legend>
87
                Confirm restriction deletion
88
            </legend>
89
90
            <p>Are you sure you want to delete &quot;[% restriction.display_text | html %]&quot;</p>
91
92
            <fieldset class="action">
93
                <input type="hidden" name="op" value="delete_confirmed" />
94
                <input type="hidden" name="code" value="[% restriction.code | html %]" />
95
                <input type="submit" value="Delete this restriction" />
96
                <a class="cancel" href="/cgi-bin/koha/admin/restrictions.pl">Cancel</a>
97
            </fieldset>
98
        </fieldset>
99
    </form>
100
[% END %]
101
102
[% IF op == 'list' %]
103
104
    <div id="toolbar" class="btn-toolbar">
105
        <a class="btn btn-default" id="newrestriction" href="/cgi-bin/koha/admin/restrictions.pl?op=add_form"><i class="fa fa-plus"></i> New restriction</a>
106
    </div>
107
108
    <h2>Patron restrictions</h2>
109
    [% IF searchfield %]
110
        You Searched for [% searchfield | html %]</span>
111
    [% END %]
112
    [% IF restrictions %]
113
        <table id="restriction_types">
114
            <thead>
115
                <tr>
116
                    <th scope="col">Code</th>
117
                    <th scope="col">Label</th>
118
                    <th scope="col">Default</th>
119
                    <th scope="col">Actions</th>
120
                </tr>
121
            </thead>
122
            <tbody>
123
                [% FOREACH restriction IN restrictions %]
124
                    <tr>
125
                        <td>
126
                            [% restriction.code | html %]
127
                        </td>
128
                        <td>
129
                            [% restriction.display_text | html %]
130
                        </td>
131
                        <td>
132
                            [% IF restriction.dflt %]Yes[% END %]
133
                        </td>
134
                        <td class="actions">
135
                            <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/restrictions.pl?op=add_form&amp;code=[% restriction.code | uri %]"><i class="fa fa-pencil"></i> Edit</a>
136
                            [% IF !restriction.ronly %]
137
                                <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/restrictions.pl?op=delete_confirm&amp;code=[% restriction.code | uri %]"><i class="fa fa-trash"></i> Delete</a>
138
                            [% END %]
139
                        </td>
140
                    </tr>
141
                [% END %]
142
            </tbody>
143
        </table>
144
    [% ELSE %]
145
        <div class="dialog alert">No restrictions have been defined. <a href="/cgi-bin/koha/admin/restrictions.pl?op=add_form">Create a new restriction</a>.</div>
146
    [% END %]
147
[% END %]
148
149
            </main>
150
        </div> <!-- /.col-sm-10.col-sm-push-2 -->
151
152
        <div class="col-sm-2 col-sm-pull-10">
153
            <aside>
154
                [% INCLUDE 'admin-menu.inc' %]
155
            </aside>
156
        </div> <!-- /.col-sm-2.col-sm-pull-10 -->
157
     </div> <!-- /.row -->
158
159
[% MACRO jsinclude BLOCK %]
160
    [% Asset.js("js/admin-menu.js") | $raw %]
161
    [% INCLUDE 'datatables.inc' %]
162
    [% INCLUDE 'columns_settings.inc' %]
163
    <script>
164
        var MSG_DUPLICATE_CODE = _("Restriction code is already in use");
165
        var MSG_DUPLICATE_DISPLAY_TEXT = _("Label is already in use");
166
        var existing = {
167
        [% FOREACH ex IN existing %]
168
            [% ex.code | $raw %]: '[% ex.display_text | $raw %]',
169
        [% END %]
170
        };
171
    </script>
172
    [% Asset.js("js/restrictiontypes.js") | $raw %]
173
[% END %]
174
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/koha-tmpl/intranet-tmpl/prog/js/restrictiontypes.js (-1 / +54 lines)
Line 0 Link Here
0
- 
1
jQuery.validator.addMethod( "restrictionCode", function(value){
2
    var ex = Object.keys(existing);
3
    return (value.length > 0 && ex.indexOf(value.toUpperCase()) > -1) ?
4
        false :
5
        true;
6
}, MSG_DUPLICATE_CODE);
7
8
jQuery.validator.addMethod( "restrictionDisplayText", function(value){
9
    var ex = Object.values(existing).map(function(el) {
10
        return el.toLowerCase();
11
    });
12
    return (value.length > 0 && ex.indexOf(value.toLowerCase()) > -1) ?
13
        false :
14
        true;
15
}, MSG_DUPLICATE_DISPLAY_TEXT);
16
17
$(document).ready(function() {
18
    KohaTable("restriction_types", {
19
        "aoColumnDefs": [{
20
            "aTargets": [-1],
21
            "bSortable": false,
22
            "bSearchable": false
23
        }, {
24
            "aTargets": [0, 1],
25
            "sType": "natural"
26
        }, ],
27
        "aaSorting": [
28
            [1, "asc"]
29
        ],
30
        "sPaginationType": "full",
31
        "exportColumns": [0,1],
32
    });
33
34
    $("#restriction_form").validate({
35
        rules: {
36
            code: {
37
                required: true,
38
                restrictionCode: true
39
            },
40
            display_text: {
41
                required: true,
42
                restrictionDisplayText: true
43
            }
44
        },
45
        messages: {
46
            code: {
47
                restrictionCode: MSG_DUPLICATE_CODE
48
            },
49
            display_text: {
50
                restrictionDisplayText: MSG_DUPLICATE_DISPLAY_TEXT
51
            }
52
        }
53
    });
54
});

Return to bug 23681