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

(-)a/admin/smtp_servers.pl (+236 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2020 Theke Solutions
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 Try::Tiny;
24
25
use C4::Auth qw(get_template_and_user);
26
use C4::Output qw(output_html_with_http_headers);
27
28
use Koha::Libraries;
29
use Koha::SMTP::Servers;
30
31
my $input = CGI->new;
32
my $op    = $input->param('op') || 'list';
33
34
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35
    {   template_name   => "admin/smtp_servers.tt",
36
        query           => $input,
37
        type            => "intranet",
38
        authnotrequired => 0,
39
        flagsrequired   => { parameters => 1 },
40
        debug           => 1,
41
    }
42
);
43
44
my @messages;
45
46
if ( $op eq 'add' ) {
47
48
    my $library_id = $input->param('library_id');
49
    $library_id = undef
50
        if defined $library_id and $library_id eq '*';
51
52
    my $name       = $input->param('smtp_name');
53
    my $host       = $input->param('smtp_host');
54
    my $port       = $input->param('smtp_port') || 25;
55
    my $timeout    = $input->param('smtp_timeout') || 120;
56
    my $ssl        = (scalar $input->param('smtp_ssl')) ? 1 : 0;
57
    my $user_name  = $input->param('smtp_user_name') || undef;
58
    my $password   = $input->param('smtp_password') || undef;
59
60
    try {
61
        Koha::SMTP::Server->new(
62
            {
63
                library_id => $library_id,
64
                name       => $name,
65
                host       => $host,
66
                port       => $port,
67
                timeout    => $timeout,
68
                ssl        => $ssl,
69
                user_name  => $user_name,
70
                password   => $password
71
            }
72
        )->store;
73
74
        push @messages, { type => 'message', code => 'success_on_insert' };
75
    }
76
    catch {
77
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
78
            push @messages,
79
              {
80
                type   => 'alert',
81
                code   => 'error_on_insert',
82
                reason => 'duplicate_id'
83
              };
84
        }
85
    };
86
87
    # list servers after adding
88
    $op = 'list';
89
}
90
elsif ( $op eq 'delete' ) {
91
    my $smtp_server_id = $input->param('smtp_server_id');
92
    my $smtp_server;
93
94
    $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
95
        unless !$smtp_server_id;
96
97
    if ( $smtp_server ) {
98
        try {
99
            my $name = $smtp_server->name;
100
            $smtp_server->delete;
101
            push @messages,
102
              {
103
                type => 'message',
104
                code => 'success_on_delete'
105
              };
106
        }
107
        catch {
108
            push @messages,
109
              {
110
                type   => 'alert',
111
                code   => 'error_on_delete'
112
              };
113
        };
114
    }
115
    else {
116
        push @messages,
117
            {
118
                type   => 'alert',
119
                code   => 'error_on_delete',
120
                reason => 'invalid_id'
121
            };
122
    }
123
124
    # list servers after delete
125
    $op = 'list';
126
}
127
elsif ( $op eq 'edit_form' ) {
128
    my $smtp_server_id = $input->param('smtp_server_id');
129
    my $smtp_server;
130
131
    $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
132
        unless !$smtp_server_id;
133
134
    if ( $smtp_server ) {
135
        $template->param(
136
            smtp_server => $smtp_server
137
        );
138
    }
139
    else {
140
        push @messages,
141
            {
142
                type   => 'alert',
143
                code   => 'error_on_edit',
144
                reason => 'invalid_id'
145
            };
146
    }
147
}
148
elsif ( $op eq 'edit_save' ) {
149
150
    my $smtp_server_id = $input->param('smtp_server_id');
151
    my $smtp_server;
152
153
    $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
154
        unless !$smtp_server_id;
155
156
    if ( $smtp_server ) {
157
158
        my $name       = $input->param('smtp_name');
159
        my $host       = $input->param('smtp_host');
160
        my $port       = $input->param('smtp_port') || 25;
161
        my $timeout    = $input->param('smtp_timeout') || 120;
162
        my $ssl        = (scalar $input->param('smtp_ssl')) ? 1 : 0;
163
        my $user_name  = $input->param('smtp_user_name') || undef;
164
        my $password   = $input->param('smtp_password') || undef;
165
166
        try {
167
            $smtp_server->set(
168
                {
169
                    name      => $name,
170
                    host      => $host,
171
                    port      => $port,
172
                    timeout   => $timeout,
173
                    ssl       => $ssl,
174
                    user_name => $user_name,
175
                    password  => $password
176
177
                }
178
            )->store;
179
180
            push @messages,
181
            {
182
                type => 'message',
183
                code => 'success_on_update'
184
            };
185
        }
186
        catch {
187
            push @messages,
188
            {
189
                type   => 'alert',
190
                code   => 'error_on_update'
191
            };
192
        };
193
194
        # list servers after adding
195
        $op = 'list';
196
    }
197
    else {
198
        push @messages,
199
            {
200
                type   => 'alert',
201
                code   => 'error_on_update',
202
                reason => 'invalid_id'
203
            };
204
    }
205
}
206
207
if ( $op eq 'list' ) {
208
    my $smtp_servers = Koha::SMTP::Servers->search;
209
    $template->param(
210
        servers_count  => $smtp_servers->count,
211
        default_config => $smtp_servers->default_setting
212
    );
213
}
214
215
my @already_set_library_ids = Koha::SMTP::Servers->search(
216
    {
217
        library_id => { '!=' => undef }
218
    }
219
)->get_column('library_id');
220
221
my $libraries = Koha::Libraries->search(
222
    {
223
        'me.branchcode' => { -not_in => \@already_set_library_ids }
224
    }
225
);
226
227
my $default = Koha::SMTP::Servers->search({ library_id => undef })->count;
228
229
$template->param(
230
    op              => $op,
231
    default_set     => ( $default ) ? 1 : 0,
232
    unset_libraries => $libraries,
233
    messages        => \@messages,
234
);
235
236
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt (+4 lines)
Lines 208-213 Link Here
208
                        <dt><a href="/cgi-bin/koha/admin/z3950servers.pl">Z39.50/SRU servers</a></dt>
208
                        <dt><a href="/cgi-bin/koha/admin/z3950servers.pl">Z39.50/SRU servers</a></dt>
209
                        <dd>Define which external servers to query for MARC data.</dd>
209
                        <dd>Define which external servers to query for MARC data.</dd>
210
                    [% END %]
210
                    [% END %]
211
                    [% IF ( CAN_user_parameters ) %]
212
                        <dt><a href="/cgi-bin/koha/admin/smtp_servers.pl">SMTP servers</a></dt>
213
                        <dd>Define which SMTP servers to use.</dd>
214
                    [% END %]
211
                    [% IF ( CAN_user_parameters_manage_didyoumean ) %]
215
                    [% IF ( CAN_user_parameters_manage_didyoumean ) %]
212
                        <dt><a href="/cgi-bin/koha/admin/didyoumean.pl">Did you mean?</a></dt>
216
                        <dt><a href="/cgi-bin/koha/admin/didyoumean.pl">Did you mean?</a></dt>
213
                        <dd>Choose which plugins to use to suggest searches to patrons and staff.</dd>
217
                        <dd>Choose which plugins to use to suggest searches to patrons and staff.</dd>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt (-1 / +368 lines)
Line 0 Link Here
0
- 
1
[% USE raw %]
2
[% USE Asset %]
3
[% SET footerjs = 1 %]
4
[% USE TablesSettings %]
5
[% INCLUDE 'doc-head-open.inc' %]
6
<title>Koha &rsaquo; Administration &rsaquo; SMTP servers
7
[% IF op == 'add_form' %]
8
    &rsaquo;[% IF library %]Modify SMTP server[% ELSE %]New SMTP server[% END %]
9
[% ELSIF op == 'delete_confirm' %]
10
    &rsaquo; Confirm deletion of SMTP server '[% server.name | html %]'
11
[% END %]
12
</title>
13
[% INCLUDE 'doc-head-close.inc' %]
14
</head>
15
16
<body id="admin_smtp_servers" class="admin">
17
[% INCLUDE 'header.inc' %]
18
[% INCLUDE 'prefs-admin-search.inc' %]
19
20
<div id="breadcrumbs">
21
    <a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo;
22
    <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo;
23
    <a href="/cgi-bin/koha/admin/smtp_servers.pl">SMTP servers</a>
24
[% IF op == 'add_form' %]
25
 &rsaquo; <span> New</span>
26
[% ELSIF op == 'edit_form' %]
27
 &rsaquo; <span> Edit</span>
28
[% END %]
29
</div>
30
31
<div class="main container-fluid">
32
    <div class="row">
33
        <div class="col-sm-10 col-sm-push-2">
34
            <main>
35
36
[% FOREACH m IN messages %]
37
    <div class="dialog [% me.type | html %]">
38
        [% SWITCH m.code %]
39
        [% CASE 'error_on_edit' %]
40
            An error occurred trying to open the server for editing. The passed id is invalid.
41
        [% CASE 'error_on_insert' %]
42
            An error occurred when adding the server. The library already has an SMTP server set.
43
        [% CASE 'error_on_delete' %]
44
            An error occurred when deleting this server. Check the logs.
45
        [% CASE 'success_on_update' %]
46
            Server updated successfully.
47
        [% CASE 'success_on_insert' %]
48
            Server added successfully.
49
        [% CASE 'success_on_delete' %]
50
            Server deleted successfully.
51
        [% CASE %]
52
            [% m.code | html %]
53
        [% END %]
54
    </div>
55
[% END %]
56
57
[% IF op == 'add_form' %]
58
    <h3>New SMTP server</h3>
59
    <form action="/cgi-bin/koha/admin/smtp_servers.pl" id="add" name="add" class="validated" method="post">
60
        <input type="hidden" name="op" value="add" />
61
        <fieldset class="rows">
62
            <ol>
63
                <li>
64
                    <label for="library_id" class="required">Library: </label>
65
                    <select name="library_id" id="library_id">
66
                    [%- UNLESS default_set -%]
67
                        <option value="*">All libraries</option>
68
                    [%- END -%]
69
                    [%- FOREACH library IN unset_libraries -%]
70
                        <option value="[% library.branchcode | html %]">[% library.branchname | html %]</option>
71
                    [%- END -%]
72
                    </select>
73
                    <span class="required">Required</span>
74
                </li>
75
                </select>
76
                <li>
77
                    <label for="smtp_name" class="required">Name: </label>
78
                    <input type="text" name="smtp_name" id="smtp_name" size="60" class="required" required="required" />
79
                    <span class="required">Required</span>
80
                </li>
81
            </ol>
82
        </fieldset>
83
84
        <fieldset class="rows">
85
            <ol>
86
                <li>
87
                    <label for="smtp_host">Host: </label>
88
                    <input type="text" name="smtp_host" id="smtp_host" size="60" class="required"/>
89
                    <span class="required">Required</span>
90
                </li>
91
                <li>
92
                    <label for="smtp_port">Port: </label>
93
                    <input type="number" step="1" value="25" name="smtp_port" id="smtp_port" size="20" class="required"/>
94
                    <span class="required">Required</span>
95
                </li>
96
                <li>
97
                    <label for="smtp_timeout">Timeout (seconds): </label>
98
                    <input type="number" step="1" value="120" name="smtp_timeout" id="smtp_timeout" size="20" />
99
                </li>
100
                <li>
101
                    <label for="smtp_ssl">SSL: </label>
102
                    <select name="smtp_ssl" id="smtp_ssl">
103
                        <option value="1" selected="selected">Yes</option>
104
                        <option value="0">No</option>
105
                    </select>
106
                </li>
107
                <li>
108
                    <label for="smtp_user_name">User name: </label>
109
                    <input type="text" name="smtp_user_name" id="smtp_user_name" size="60" />
110
                </li>
111
                <li>
112
                    <label for="smtp_password">Password: </label>
113
                    <input type="password" name="smtp_password" id="smtp_password" size="60" />
114
                </li>
115
            </ol>
116
        </fieldset>
117
        <fieldset class="action">
118
            <input type="submit" value="Submit" />
119
            <a class="cancel" href="/cgi-bin/koha/admin/smtp_servers.pl">Cancel</a>
120
        </fieldset>
121
    </form>
122
[% END %]
123
124
[% IF op == 'edit_form' %]
125
    <h3>Edit SMTP server</h3>
126
    <form action="/cgi-bin/koha/admin/smtp_servers.pl" id="edit_save" name="edit_save" class="validated" method="post">
127
        <input type="hidden" name="op" value="edit_save" />
128
        <input type="hidden" name="smtp_server_id" value="[%- smtp_server.id | html -%]" />
129
        <fieldset class="rows">
130
            <ol>
131
                <li>
132
                    <label for="library_id">Library: </label>
133
                    [% IF smtp_server.library %]
134
                        <span name="library_id" id="library_id">[%- smtp_server.library.branchname | html -%]</span>
135
                    [%- ELSE -%]
136
                        <span name="library_id" id="library_id">All libraries</span>
137
                    [%- END -%]
138
                </li>
139
                </select>
140
                <li>
141
                    <label for="smtp_name" class="required">Name: </label>
142
                    <input type="text" name="smtp_name" id="smtp_name" size="60" class="required" required="required" value="[%- smtp_server.name | html -%]"/>
143
                    <span class="required">Required</span>
144
                </li>
145
            </ol>
146
        </fieldset>
147
148
        <fieldset class="rows">
149
            <ol>
150
                <li>
151
                    <label for="smtp_host">Host: </label>
152
                    <input type="text" name="smtp_host" id="smtp_host" size="60" class="required" value="[%- smtp_server.host | html -%]"/>
153
                    <span class="required">Required</span>
154
                </li>
155
                <li>
156
                    <label for="smtp_port">Port: </label>
157
                    <input type="number" step="1" value="25" name="smtp_port" id="smtp_port" size="20" class="required" value="[%- smtp_server.port | html -%]"/>
158
                    <span class="required">Required</span>
159
                </li>
160
                <li>
161
                    <label for="smtp_timeout">Timeout (seconds): </label>
162
                    <input type="number" step="1" value="120" name="smtp_timeout" id="smtp_timeout" size="20" value="[%- smtp_server.timeout | html -%]"/>
163
                </li>
164
                <li>
165
                    <label for="smtp_ssl">SSL: </label>
166
                    <select name="smtp_ssl" id="smtp_ssl">
167
                    [%- IF smtp_server.ssl -%]
168
                        <option value="1" selected="selected">Yes</option>
169
                        <option value="0">No</option>
170
                    [%- ELSE -%]
171
                        <option value="1">Yes</option>
172
                        <option value="0" selected="selected">No</option>
173
                    [%- END -%]
174
                    </select>
175
                </li>
176
                <li>
177
                    <label for="smtp_user_name">User name: </label>
178
                    <input type="text" name="smtp_user_name" id="smtp_user_name" size="60"  value="[%- smtp_server.user_name | html -%]"/>
179
                </li>
180
                <li>
181
                    <label for="smtp_password">Password: </label>
182
                    <input type="password" name="smtp_password" id="smtp_password" size="60"  value="[%- smtp_server.password | html -%]"/>
183
                </li>
184
            </ol>
185
        </fieldset>
186
        <fieldset class="action">
187
            <input type="submit" value="Submit" />
188
            <a class="cancel" href="/cgi-bin/koha/admin/smtp_servers.pl">Cancel</a>
189
        </fieldset>
190
    </form>
191
[% END %]
192
193
[% IF op == 'list' %]
194
195
    [%- IF unset_libraries.count > 0 -%]
196
    <div id="toolbar" class="btn-toolbar">
197
        <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>
198
    </div>
199
    [%- END -%]
200
201
    <h3>SMTP servers</h3>
202
203
    [% IF servers_count > 0 %]
204
        <table id="smtp_servers">
205
            <thead>
206
                <tr>
207
                    <th>Library</th>
208
                    <th>Name</th>
209
                    <th>Host</th>
210
                    <th>Port</th>
211
                    <th>Timeout (secs)</th>
212
                    <th>SSL</th>
213
                    <th>Authenticated</th>
214
                    <th data-class-name="actions">Actions</th>
215
                </tr>
216
            </thead>
217
        </table>
218
    [% ELSE %]
219
        <div class="dialog message">No user-defined SMTP servers. Using the default configuration:
220
            <ul>
221
                <li><strong>Host</strong>: [%- default_config.host | html -%]</li>
222
                <li><strong>Port</strong>: [%- default_config.port | html -%]</li>
223
                <li><strong>Timeout (secs)</strong>: [%- default_config.timeout | html -%]</li>
224
                <li><strong>SSL</strong>: [%- IF default_config.ssl -%]Yes[%- ELSE -%]No[%- END -%]</li>
225
            </ul>
226
            </pre>
227
        </div>
228
    [% END %]
229
[% END %]
230
231
            <div id="delete_confirm_modal" class="modal" tabindex="-1" role="dialog" aria-labelledby="delete_confirm_modal_label" aria-hidden="true">
232
                <div class="modal-dialog">
233
                    <div class="modal-content">
234
                        <div class="modal-header">
235
                            <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
236
                            <h3 id="delete_confirm_modal_label">Delete server</h3>
237
                        </div>
238
                        <div class="modal-body">
239
                            <div id="delete_confirm_dialog"></div>
240
                        </div>
241
                        <div class="modal-footer">
242
                            <a href="#" class="btn btn-default" id="delete_confirm_modal_button" role="button" data-toggle="modal">Delete</a>
243
                            <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
244
                        </div>
245
                    </div> <!-- /.modal-content -->
246
                </div> <!-- /.modal-dialog -->
247
            </div> <!-- #delete_confirm_modal -->
248
249
            </main>
250
        </div> <!-- /.col-sm-10.col-sm-push-2 -->
251
252
        <div class="col-sm-2 col-sm-pull-10">
253
            <aside>
254
                [% INCLUDE 'admin-menu.inc' %]
255
            </aside>
256
        </div> <!-- /.col-sm-2.col-sm-pull-10 -->
257
     </div> <!-- /.row -->
258
259
260
[% MACRO jsinclude BLOCK %]
261
    [% Asset.js("js/admin-menu.js") | $raw %]
262
    [% INCLUDE 'datatables.inc' %]
263
    [% INCLUDE 'columns_settings.inc' %]
264
    [% Asset.js("lib/tiny_mce/tinymce.min.js") | $raw %]
265
    [% INCLUDE 'str/tinymce_i18n.inc' %]
266
    <script>
267
        $(document).ready(function() {
268
269
            var smtp_servers_url = '/api/v1/config/smtp_servers';
270
            var smtp_servers = $("#smtp_servers").api({
271
                "ajax": {
272
                    "url": smtp_servers_url
273
                },
274
                "embed": [
275
                    "library"
276
                ],
277
                'emptyTable': '<div class="dialog message">'+_("There are no SMTP servers defined.")+'</div>',
278
                "columnDefs": [ {
279
                    "targets": [0,1,2],
280
                    "render": function (data, type, row, meta) {
281
                        if ( type == 'display' ) {
282
                            if ( data != null ) {
283
                                return data.escapeHtml();
284
                            }
285
                            else {
286
                                return "Default";
287
                            }
288
                        }
289
                        return data;
290
                    }
291
                } ],
292
                "columns": [
293
                    {
294
                        "data": "library.name",
295
                        "searchable": true,
296
                        "orderable": true
297
                    },
298
                    {
299
                        "data": "name",
300
                        "searchable": true,
301
                        "orderable": true
302
                    },
303
                    {
304
                        "data": "host",
305
                        "searchable": true,
306
                        "orderable": true
307
                    },
308
                    {
309
                        "data": "port",
310
                        "searchable": true,
311
                        "orderable": false
312
                    },
313
                    {
314
                        "data": "timeout",
315
                        "searchable": true,
316
                        "orderable": false
317
                    },
318
                    {
319
                        "data": "ssl",
320
                        "render": function (data, type, row, meta) {
321
                            if (data) {
322
                                return _("Yes");
323
                            }
324
                            else {
325
                                return _("No");
326
                            }
327
                        },
328
                        "searchable": false,
329
                        "orderable": false
330
                    },
331
                    {
332
                        "data": function( row, type, val, meta ) {
333
                            if ( row.user_name != null ) {
334
                                return _("Yes");
335
                            }
336
                            else {
337
                                return _("No");
338
                            }
339
                        },
340
                        "searchable": false,
341
                        "orderable": false
342
                    },
343
                    {
344
                        "data": function( row, type, val, meta ) {
345
                            var result = '<a class="btn btn-default btn-xs" role="button" href="/cgi-bin/koha/admin/smtp_servers.pl?op=edit_form&amp;smtp_server_id='+ encodeURIComponent(row.smtp_server_id) +'"><i class="fa fa-pencil" aria-hidden="true"></i> '+_("Edit")+'</a>'+"\n";
346
                            result += '<a class="btn btn-default btn-xs delete_server" role="button" href="#" data-toggle="modal" data-target="#delete_confirm_modal" data-smtp-server-id="'+ encodeURIComponent(row.smtp_server_id) +'" data-smtp-server-name="'+ encodeURIComponent(row.name) +'"><i class="fa fa-trash" aria-hidden="true"></i>'+_("Delete")+'</a>';
347
                            return result;
348
                        },
349
                        "searchable": false,
350
                        "orderable": false
351
                    }
352
                ]
353
            });
354
355
            $('#smtp_servers').on( "click", '.delete_server', function () {
356
                var smtp_server_id   = $(this).data('smtp-server-id');
357
                var smtp_server_name = $(this).data('smtp-server-name');
358
359
                $("#delete_confirm_dialog").html(
360
                    _("You are about to delete the \"%s\" SMTP server.").format(smtp_server_name)
361
                );
362
                $("#delete_confirm_modal_button").attr("href", "/cgi-bin/koha/admin/smtp_servers.pl?op=delete&amp;smtp_server_id=" + encodeURIComponent(smtp_server_id));
363
            });
364
        });
365
    </script>
366
[% END %]
367
368
[% INCLUDE 'intranet-bottom.inc' %]

Return to bug 22343