Bug 33335: (QA follow-up) Polishing and comments
[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     try {
58
59         Koha::SMTP::Server->new(
60             {
61                 name       => $name,
62                 host       => $host,
63                 port       => $port,
64                 timeout    => $timeout,
65                 ssl_mode   => $ssl_mode,
66                 user_name  => $user_name,
67                 password   => $password,
68                 debug      => $debug,
69                 is_default => $is_default,
70             }
71         )->store;
72
73         push @messages, { type => 'message', code => 'success_on_insert' };
74     }
75     catch {
76         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
77             push @messages,
78               {
79                 type   => 'alert',
80                 code   => 'error_on_insert',
81                 reason => 'duplicate_id'
82               };
83         }
84     };
85
86     # list servers after adding
87     $op = 'list';
88 }
89 elsif ( $op eq 'edit_form' ) {
90     my $smtp_server_id = $input->param('smtp_server_id');
91     my $smtp_server;
92
93     $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
94         unless !$smtp_server_id;
95
96     if ( $smtp_server ) {
97         $template->param(
98             smtp_server => $smtp_server
99         );
100     }
101     else {
102         push @messages,
103             {
104                 type   => 'alert',
105                 code   => 'error_on_edit',
106                 reason => 'invalid_id'
107             };
108     }
109 }
110 elsif ( $op eq 'edit_save' ) {
111
112     my $smtp_server_id = $input->param('smtp_server_id');
113     my $smtp_server;
114
115     $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
116         unless !$smtp_server_id;
117
118     if ( $smtp_server ) {
119
120         my $name       = $input->param('smtp_name');
121         my $host       = $input->param('smtp_host');
122         my $port       = $input->param('smtp_port') || 25;
123         my $timeout    = $input->param('smtp_timeout') || 120;
124         my $ssl_mode   = $input->param('smtp_ssl_mode');
125         my $user_name  = $input->param('smtp_user_name') || undef;
126         my $password   = $input->param('smtp_password') || undef;
127         my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
128         my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
129
130         try {
131
132             $smtp_server->password( $password )
133                 if defined $password and $password ne '****'
134                     or not defined $password;
135
136             $smtp_server->set(
137                 {
138                     name       => $name,
139                     host       => $host,
140                     port       => $port,
141                     timeout    => $timeout,
142                     ssl_mode   => $ssl_mode,
143                     user_name  => $user_name,
144                     debug      => $debug,
145                     is_default => $is_default,
146                 }
147             )->store;
148
149             push @messages,
150             {
151                 type => 'message',
152                 code => 'success_on_update'
153             };
154         }
155         catch {
156             push @messages,
157             {
158                 type   => 'alert',
159                 code   => 'error_on_update'
160             };
161         };
162
163         # list servers after adding
164         $op = 'list';
165     }
166     else {
167         push @messages,
168             {
169                 type   => 'alert',
170                 code   => 'error_on_update',
171                 reason => 'invalid_id'
172             };
173     }
174 }
175
176 if ( $op eq 'list' ) {
177     my $smtp_servers = Koha::SMTP::Servers->search;
178     $template->param(
179         servers_count  => $smtp_servers->count,
180         default_config => $smtp_servers->get_default,
181     );
182 }
183
184 $template->param(
185     op       => $op,
186     messages => \@messages,
187 );
188
189 output_html_with_http_headers $input, $cookie, $template->output;