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