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

Return to bug 33537