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

(-)a/admin/domain_limits.pl (+35 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2023 Rijksmuseum, Koha development team
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
use CGI qw ( -utf8 );
22
23
use C4::Auth qw( get_template_and_user );
24
use C4::Output qw( output_html_with_http_headers );
25
26
my $input = CGI->new;
27
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
28
    {
29
        template_name => "admin/domain_limits.tt",
30
        query         => $input,
31
        type          => "intranet",
32
        flagsrequired => { parameters => 'manage_smtp_servers' },
33
    }
34
);
35
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/domain_limits.tt (+116 lines)
Line 0 Link Here
1
[% USE raw %]
2
[% USE Asset %]
3
[% SET footerjs = 1 %]
4
5
[% INCLUDE 'doc-head-open.inc' %]
6
<title>Domain limits &rsaquo; Administration &rsaquo; Koha</title>
7
[% INCLUDE 'doc-head-close.inc' %]
8
</head>
9
10
<body id="admin_domain_limits" class="admin">
11
[% INCLUDE 'header.inc' %]
12
[% INCLUDE 'prefs-admin-search.inc' %]
13
14
[% BLOCK modal_form %]
15
<div id="domain_limits_modal" class="modal" tabindex="-1" role="dialog" aria-labelledby="domain_limits_modal" aria-hidden="true">
16
    <div class="modal-dialog">
17
        <div class="modal-content">
18
            <form class="modal-form">
19
                <div class="modal-header">
20
                    <h3 class="modal-title add">Add domain limit</h4>
21
                    <h3 class="modal-title add_member">Add member</h4>
22
                    <h3 class="modal-title edit edit_member">Edit domain limit</h4>
23
                </div>
24
                <div class="modal-body">
25
                    <fieldset class="rows">
26
                        <ol>
27
                            <li class="add edit add_member edit_member"><label for="domain_name" class="">Domain name</label>
28
                                <input type="text" name="domain_name" class="" required/>
29
                            </li>
30
                            <li class="add edit"><label for="domain_limit">Message limit</label>
31
                                <input type="text" name="domain_limit" class="toggle_validation" required inputmode="numeric" pattern="[0-9]*"/>
32
                            </li>
33
                            <li class="add edit"><label for="units">Units</label></td>
34
                                <input type="text" name="units" class="toggle_validation" required inputmode="numeric" pattern="[0-9]*"/>
35
                            </li>
36
                            <li class="add edit"><label for="unit_type">Unit type</label>
37
                                <select name="unit_type" class="toggle_validation" required>
38
                                    <option value="days"   >days   </option>
39
                                    <option value="hours"  >hours  </option>
40
                                    <option value="minutes">minutes</option>
41
                                </select>
42
                            </li>
43
                            <li class="add_member edit_member"><label for="group_domain">Group domain</label>
44
                                <input type="text" name="group_domain" class="" disabled/>
45
                            </li>
46
                        </ol>
47
                    </fieldset>
48
                </div>
49
                <div class="modal-footer">
50
                    <button class="btn btn-primary save" data-dismiss="modal" role="button">Save</button>
51
                    <button class="btn btn-default cancel" data-dismiss="modal" role="button">Cancel</button>
52
                </div>
53
            </form>
54
        </div>
55
    </div>
56
</div>
57
[% END %]
58
59
[% WRAPPER 'sub-header.inc' %]
60
    [% WRAPPER breadcrumbs %]
61
        [% WRAPPER breadcrumb_item %]
62
            <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
63
        [% END %]
64
        [% WRAPPER breadcrumb_item bc_active= 1 %]
65
            <span>Domain limits</span>
66
        [% END %]
67
    [% END #/ WRAPPER breadcrumbs %]
68
[% END #/ WRAPPER sub-header.inc %]
69
70
<div class="main container-fluid">
71
    <div class="row">
72
    <div class="col-sm-10 col-sm-push-2">
73
    <main>
74
75
    <div class="dialog message" id="domain_limit_message" style="display:none;"></div>
76
    <div class="dialog alert"   id="domain_limit_alert"   style="display:none;"></div>
77
78
    <div id="toolbar" class="btn-toolbar">
79
        <a class="btn btn-default add" href="#"><i class="fa fa-plus"></i> New limit</a>
80
    </div>
81
82
    <h1>Domain limits</h1>
83
    <div class="page-section">
84
        <table id="domain_limits">
85
            <thead>
86
                <tr>
87
                    <th>Domain</th>
88
                    <th>Message limit</th>
89
                    <th>Units</th>
90
                    <th>Unit type</th>
91
                    <th>Belongs to</th>
92
                    <th data-class-name="actions noExport">Actions</th>
93
                </tr>
94
            </thead>
95
        </table>
96
        [% INCLUDE modal_form %]
97
    </div>
98
99
    </main>
100
    </div> <!-- /.col-sm-10.col-sm-push-2 -->
101
102
    <div class="col-sm-2 col-sm-pull-10">
103
        <aside>
104
            [% INCLUDE 'admin-menu.inc' %]
105
        </aside>
106
    </div>
107
    </div> <!-- /.row -->
108
<!-- /div in bottom include -->
109
110
[% MACRO jsinclude BLOCK %]
111
    [% Asset.js("js/admin-menu.js") | $raw %]
112
    [% INCLUDE 'datatables.inc' %]
113
    [% Asset.js("js/domain_limits.js") | $raw %]
114
[% END %]
115
116
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt (+1 lines)
Lines 244-249 Link Here
244
244
245
    <div id="toolbar" class="btn-toolbar">
245
    <div id="toolbar" class="btn-toolbar">
246
        <a class="btn btn-default" id="new_smtp_server" href="/cgi-bin/koha/admin/smtp_servers.pl?op=add_form"><i class="fa fa-plus"></i> New SMTP server</a>
246
        <a class="btn btn-default" id="new_smtp_server" href="/cgi-bin/koha/admin/smtp_servers.pl?op=add_form"><i class="fa fa-plus"></i> New SMTP server</a>
247
        <a class="btn btn-default" id="mail_domain_limits" href="/cgi-bin/koha/admin/domain_limits.pl">Domain limits</a>
247
    </div>
248
    </div>
248
249
249
    <h1>SMTP servers</h1>
250
    <h1>SMTP servers</h1>
(-)a/koha-tmpl/intranet-tmpl/prog/js/domain_limits.js (-1 / +239 lines)
Line 0 Link Here
0
- 
1
// domain_limits.js: Code for the corresponding admin form
2
3
var domain_limits_table;
4
$(document).ready(function() {
5
    domain_limits_table = init_table();
6
    $('#toolbar').on( "click", 'a.add', function (ev) {
7
        ev.preventDefault();
8
        start_modal( 'add' );
9
    });
10
    $('#domain_limits').on( "click", 'a.add_member', function (ev) {
11
        ev.preventDefault();
12
        start_modal( 'add_member', { belongs_to: $(this).parents('tr').attr('id') } );
13
    });
14
    $('#domain_limits').on( "click", 'a.edit, a.edit_member', function (ev) {
15
        ev.preventDefault();
16
        hide_dialogs();
17
        var limit_id = $(this).parents('tr').attr('id');
18
        api_get(limit_id);
19
    });
20
    $('#domain_limits').on( "click", 'a.delete', function (ev) {
21
        ev.preventDefault();
22
        hide_dialogs();
23
        if ( window.confirm( _("Delete this limit?") ) ) {
24
            var limit_id = $(this).parents('tr').attr('id');
25
            api_delete( limit_id );
26
        }
27
    });
28
    $('#domain_limits_modal button.save').on( 'click', function(ev) {
29
        if ( !$('#domain_limits_modal form').get(0).checkValidity() ) {
30
            ev.stopPropagation();
31
            return; // Do not return false here to allow validation.
32
        }
33
        var limit_id = $('#domain_limits_modal .modal-form').data('id');
34
        var row = get_modal_inputs();
35
        if ( limit_id ) api_put(limit_id, row); else api_post(row);
36
        return false;
37
    });
38
});
39
40
function api_get (limit_id) {
41
    var row = domain_limits_table.DataTable().row('#'+limit_id).data();
42
    $.ajax({
43
        method: "GET",
44
        url: '/api/v1/smtp/domain_limits/' + limit_id,
45
    }).done(function (data) {
46
        start_modal( row.belongs_to ? 'edit_member' : 'edit', data );
47
    }).fail(function (xhr) {
48
        status_message( 'get', xhr.status, row );
49
    });
50
}
51
52
function api_delete (limit_id) {
53
    var limit_name = domain_limits_table.DataTable().row('#'+limit_id).data().domain_name;
54
    $.ajax({
55
        method: "DELETE",
56
        url: '/api/v1/smtp/domain_limits/' + limit_id,
57
    }).done(function () {
58
        domain_limits_table.api().ajax.reload();
59
        $('#domain_limit_message').html( _("Limit for '%s' deleted.").format(limit_name) ).show();
60
    }).fail(function (xhr) {
61
        status_message( 'delete', xhr.status, { domain_name: limit_name } );
62
    });
63
}
64
65
function api_post (row) {
66
    $.ajax({
67
        method: "POST",
68
        url: '/api/v1/smtp/domain_limits',
69
        data: JSON.stringify(row),
70
        contentType: 'application/json',
71
    }).done(function () {
72
        domain_limits_table.api().ajax.reload();
73
        $('#domain_limit_message').html( _("Limit for '%s' added.").format(row['domain_name']) ).show();
74
    }).fail(function (xhr, status, error) {
75
        status_message( 'post', xhr.status, row );
76
    }).always(function () {
77
        $('#domain_limits_modal').modal('hide');
78
    });;
79
}
80
81
function api_put (limit_id, row) {
82
    $.ajax({
83
        method: "PUT",
84
        url: '/api/v1/smtp/domain_limits/' + limit_id,
85
        data: JSON.stringify(row),
86
        contentType: 'application/json',
87
    }).done(function () {
88
        domain_limits_table.api().ajax.reload();
89
        $('#domain_limit_message').html( _("Limit for '%s' modified.").format(row.domain_name) ).show();
90
    }).fail(function (xhr, status, error) {
91
        status_message( 'put', xhr.status, row );
92
    }).always(function () {
93
        $('#domain_limits_modal').modal('hide');
94
    });
95
}
96
97
function status_message (method, status, row) { // only used in ajax.fail
98
    // Extending with parsing xhr.responseText seems not needed here (now).
99
    // POST and PUT may trigger similar exceptions, GET and DELETE are trivial.
100
    var messages = {
101
        delete          : _( "Deleting limit for '%s' failed." ),
102
        get             : _( "Retrieving limit for '%s' failed." ),
103
        put             : _( "Modifying limit for '%s' failed." ),
104
        post            : _( "Adding limit for '%s' failed." ),
105
        status_400      : _( " (Status 400 - Bad request: expected columns were empty.)" ),
106
        status_401      : _( " (Status 401 - Unauthorized: session may have expired.)" ),
107
        status_403      : _( " (Status 403 - Forbidden: check permissions.)" ),
108
        status_404      : _( " (Status 404 - Not found)" ),
109
        status_404_put  : _( " (Status 404 - Not found: group domain does not exist)" ),
110
        status_404_post : _( " (Status 404 - Not found: group domain does not exist)" ),
111
        status_409      : _( " (Status 409 - Conflict: duplicate domain name.)" ),
112
    };
113
    var str = messages['status_'+status+'_'+method] || messages['status_'+status] || '';
114
    $( '#domain_limit_alert' ).html( messages[method].format(row.domain_name) + str ).show();
115
}
116
117
function hide_dialogs () {
118
    $('div.dialog').html('').hide();
119
}
120
121
function get_actions_html (row) {
122
    var result = '';
123
    if ( row.belongs_to == undefined ) {
124
        result += '<a class="btn btn-default btn-xs edit" href="#"><i class="fa fa-pencil-alt"></i> '+_("Edit")+'</a>'+"\n";
125
        result += '<a class="btn btn-default btn-xs delete" href="#"><i class="fa fa-trash-can"></i> '+_("Delete")+'</a>'+"\n";
126
        result += '<a class="btn btn-default btn-xs add_member" href="#"><i class="fa fa-plus"></i> '+_("New member")+'</a>'+"\n";
127
    } else {
128
        result += '<a class="btn btn-default btn-xs edit_member" href="#"><i class="fa fa-pencil-alt"></i> '+_("Edit")+'</a>'+"\n";
129
        result += '<a class="btn btn-default btn-xs delete" href="#"><i class="fa fa-trash-can"></i> '+_("Delete")+'</a>'+"\n";
130
    }
131
    return result;
132
}
133
134
function get_modal_inputs () {
135
    var row = {};
136
    row.domain_name = $('input[name="domain_name"]').val();
137
    if( $('input[name="group_domain"]:visible').length ) {
138
        row.belongs_to = $('#domain_limits_modal .modal-form').data('belongs_to');
139
    } else {
140
        row.domain_limit = $('input[name="domain_limit"]').val();
141
        row.units = $('input[name="units"]').val();
142
        row.unit_type = $('select[name="unit_type"]').val();
143
    }
144
    return row;
145
}
146
147
function toggle_modal_inputs (action) {
148
    hide_dialogs();
149
    // Show one title
150
    $('h3.modal-title').hide().filter('.'+action).show()
151
    // Hide and disable some inputs (disable needed to skip validation)
152
    $('.modal-body li').hide().filter('.'+action).show();
153
    $('.modal-body .toggle_validation').prop('disabled', true);
154
    $('.modal-body li.'+action+' .toggle_validation').prop('disabled', false);
155
}
156
157
function fill_modal_inputs (action, row) {
158
    // Start by clearing
159
    $('#domain_limits_modal input').val('');
160
    $('#domain_limits_modal select').val('');
161
162
    if ( action=='edit' ) {
163
        for ( const field_name of [ 'domain_name', 'domain_limit', 'units' ] ) {
164
            $('#domain_limits_modal input[name="'+field_name+'"]').val(row[field_name]);
165
        }
166
        $('#domain_limits_modal select').val(row['unit_type']);
167
    } else if ( action=='edit_member' ) {
168
        $('#domain_limits_modal input[name="domain_name"]').val(row.domain_name);
169
        $('#domain_limits_modal input[name="group_domain"]').val(row.group_domain);
170
    } else if( action=='add_member' ) {
171
        $('#domain_limits_modal input[name="group_domain"]').val(row.group_domain);
172
    }
173
}
174
175
function start_modal (action, row) {
176
    toggle_modal_inputs(action);
177
    $('#domain_limits_modal .modal-form').data('id', row ? row.domain_limit_id : null).data('belongs_to', row ? row.belongs_to : null);
178
    if( action=='add_member' ) {
179
        // fetch group domain name from corresponding row
180
        var group_domain = domain_limits_table.DataTable().row('#' + row.belongs_to).data().domain_name;
181
        fill_modal_inputs( action, { group_domain: group_domain } );
182
    } else {
183
        fill_modal_inputs(action, row);
184
    }
185
    $('#domain_limits_modal').modal('show').find('input')[0].focus();
186
}
187
188
function init_table () {
189
    return $("#domain_limits").kohaTable({
190
        "ajax": {
191
            "url": '/api/v1/smtp/domain_limits',
192
        },
193
        'language': {
194
            'emptyTable': '<div>'+_("There are no domain limits defined.")+'</div>'
195
        },
196
        "columnDefs": [ {
197
            "targets": [0,1,2,3,4],
198
            "render": function (data, type, row, meta) {
199
                if ( type == 'display' && data != null ) {
200
                    return data.escapeHtml();
201
                }
202
                return data;
203
            }
204
        } ],
205
        "columns": [
206
            {
207
                "data": "domain_name",
208
                "searchable": true,
209
                "orderable": true,
210
            },
211
            {
212
                "data": "domain_limit",
213
                "searchable": true,
214
                "orderable": true,
215
            },
216
            {
217
                "data": "units",
218
                "searchable": true,
219
                "orderable": true,
220
            },
221
            {
222
                "data": "unit_type",
223
                "searchable": true,
224
                "orderable": true,
225
            },
226
            {
227
                "data": "group_domain",
228
                "searchable": false,
229
                "orderable": false,
230
            },
231
            {
232
                "data": function( row, type, val, meta ) { return get_actions_html(row) },
233
                "searchable": false,
234
                "orderable": false
235
            }
236
        ],
237
        rowId: 'domain_limit_id',
238
    });
239
}

Return to bug 33537