Bug 27424: Add ability to specify an SMTP server in the database as the default server
[koha.git] / admin / smtp_servers.pl
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 Scalar::Util qw( blessed );
24 use Try::Tiny qw( catch try );
25
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28
29 use Koha::Libraries;
30 use Koha::SMTP::Servers;
31
32 my $input = CGI->new;
33 my $op    = $input->param('op') || 'list';
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {   template_name   => "admin/smtp_servers.tt",
37         query           => $input,
38         type            => "intranet",
39         flagsrequired   => { parameters => 'manage_smtp_servers' },
40     }
41 );
42
43 my @messages;
44
45 if ( $op eq 'add' ) {
46
47     my $name       = $input->param('smtp_name');
48     my $host       = $input->param('smtp_host');
49     my $port       = $input->param('smtp_port') || 25;
50     my $timeout    = $input->param('smtp_timeout') || 120;
51     my $ssl_mode   = $input->param('smtp_ssl_mode');
52     my $user_name  = $input->param('smtp_user_name') || undef;
53     my $password   = $input->param('smtp_password') || undef;
54     my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
55     my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
56
57     my $schema = Koha::Database->new->schema;
58     try {
59         $schema->storage->txn_begin;
60
61         Koha::SMTP::Servers->search->update({ is_default => 0 }) if $is_default;
62
63         Koha::SMTP::Server->new(
64             {
65                 name       => $name,
66                 host       => $host,
67                 port       => $port,
68                 timeout    => $timeout,
69                 ssl_mode   => $ssl_mode,
70                 user_name  => $user_name,
71                 password   => $password,
72                 debug      => $debug,
73                 is_default => $is_default,
74             }
75         )->store;
76
77         push @messages, { type => 'message', code => 'success_on_insert' };
78         $schema->storage->txn_commit;
79     }
80     catch {
81         $schema->storage->txn_rollback;
82         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
83             push @messages,
84               {
85                 type   => 'alert',
86                 code   => 'error_on_insert',
87                 reason => 'duplicate_id'
88               };
89         }
90     };
91
92     # list servers after adding
93     $op = 'list';
94 }
95 elsif ( $op eq 'edit_form' ) {
96     my $smtp_server_id = $input->param('smtp_server_id');
97     my $smtp_server;
98
99     $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
100         unless !$smtp_server_id;
101
102     if ( $smtp_server ) {
103         $template->param(
104             smtp_server => $smtp_server
105         );
106     }
107     else {
108         push @messages,
109             {
110                 type   => 'alert',
111                 code   => 'error_on_edit',
112                 reason => 'invalid_id'
113             };
114     }
115 }
116 elsif ( $op eq 'edit_save' ) {
117
118     my $smtp_server_id = $input->param('smtp_server_id');
119     my $smtp_server;
120
121     $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
122         unless !$smtp_server_id;
123
124     if ( $smtp_server ) {
125
126         my $name       = $input->param('smtp_name');
127         my $host       = $input->param('smtp_host');
128         my $port       = $input->param('smtp_port') || 25;
129         my $timeout    = $input->param('smtp_timeout') || 120;
130         my $ssl_mode   = $input->param('smtp_ssl_mode');
131         my $user_name  = $input->param('smtp_user_name') || undef;
132         my $password   = $input->param('smtp_password') || undef;
133         my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
134         my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
135
136         my $schema = Koha::Database->new->schema;
137
138         try {
139             $schema->storage->txn_begin;
140
141             Koha::SMTP::Servers->search->update({ is_default => 0 }) if $is_default;
142
143             $smtp_server->password( $password )
144                 if defined $password and $password ne '****'
145                     or not defined $password;
146
147             $smtp_server->set(
148                 {
149                     name       => $name,
150                     host       => $host,
151                     port       => $port,
152                     timeout    => $timeout,
153                     ssl_mode   => $ssl_mode,
154                     user_name  => $user_name,
155                     debug      => $debug,
156                     is_default => $is_default,
157                 }
158             )->store;
159
160             push @messages,
161             {
162                 type => 'message',
163                 code => 'success_on_update'
164             };
165             $schema->storage->txn_commit;
166         }
167         catch {
168             $schema->storage->txn_rollback;
169             push @messages,
170             {
171                 type   => 'alert',
172                 code   => 'error_on_update'
173             };
174         };
175
176         # list servers after adding
177         $op = 'list';
178     }
179     else {
180         push @messages,
181             {
182                 type   => 'alert',
183                 code   => 'error_on_update',
184                 reason => 'invalid_id'
185             };
186     }
187 }
188
189 if ( $op eq 'list' ) {
190     my $smtp_servers = Koha::SMTP::Servers->search;
191     $template->param(
192         servers_count  => $smtp_servers->count,
193         default_config => $smtp_servers->get_default,
194     );
195 }
196
197 $template->param(
198     op       => $op,
199     messages => \@messages,
200 );
201
202 output_html_with_http_headers $input, $cookie, $template->output;