Bug 25030: (QA follow-up) Add POD
[koha.git] / admin / branches.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2015 Koha Development Team
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use Try::Tiny qw( catch try );
25
26 use C4::Auth qw( get_template_and_user );
27 use C4::Context;
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Koha;
30
31 use Koha::Database;
32 use Koha::Patrons;
33 use Koha::Items;
34 use Koha::Libraries;
35 use Koha::SMTP::Servers;
36
37 my $input        = CGI->new;
38 my $branchcode   = $input->param('branchcode');
39 my $categorycode = $input->param('categorycode');
40 my $op           = $input->param('op') || 'list';
41 my @messages;
42
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {   template_name   => "admin/branches.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { parameters => 'manage_libraries' },
48     }
49 );
50
51 if ( $op eq 'add_form' ) {
52     my $library;
53     if ($branchcode) {
54         $library = Koha::Libraries->find($branchcode);
55         $template->param( selected_smtp_server => $library->smtp_server );
56     }
57
58     my @smtp_servers = Koha::SMTP::Servers->search;
59
60     $template->param(
61         library      => $library,
62         smtp_servers => \@smtp_servers
63     );
64 } elsif ( $op eq 'add_validate' ) {
65     my @fields = qw(
66       branchname
67       branchaddress1
68       branchaddress2
69       branchaddress3
70       branchzip
71       branchcity
72       branchstate
73       branchcountry
74       branchphone
75       branchfax
76       branchemail
77       branchillemail
78       branchreplyto
79       branchreturnpath
80       branchurl
81       issuing
82       branchip
83       branchnotes
84       opac_info
85       marcorgcode
86       pickup_location
87     );
88     my $is_a_modif = $input->param('is_a_modif');
89
90     if ($is_a_modif) {
91         my $library = Koha::Libraries->find($branchcode);
92         for my $field (@fields) {
93             if ( $field eq 'pickup_location' ) {
94                 # Don't fallback to undef/NULL, default is 1 in DB
95                 $library->$field( scalar $input->param($field) );
96             } else {
97                 $library->$field( scalar $input->param($field) || undef );
98             }
99         }
100
101         try {
102             Koha::Database->new->schema->txn_do(
103                 sub {
104                     $library->store->discard_changes;
105                     # Deal with SMTP server
106                     my $smtp_server_id = $input->param('smtp_server');
107
108                     if ( $smtp_server_id ) {
109                         if ( $smtp_server_id eq '*' ) {
110                             $library->smtp_server({ smtp_server => undef });
111                         }
112                         else {
113                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
114                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
115                                 unless $smtp_server;
116                             $library->smtp_server({ smtp_server => $smtp_server });
117                         }
118                     }
119
120                     push @messages, { type => 'message', code => 'success_on_update' };
121                 }
122             );
123         }
124         catch {
125             push @messages, { type => 'alert', code => 'error_on_update' };
126         };
127     } else {
128         $branchcode =~ s|\s||g;
129         my $library = Koha::Library->new(
130             {
131                 branchcode => $branchcode,
132                 (
133                     map {
134                         $_ eq 'pickup_location' # Don't fallback to undef/NULL, default is 1 in DB
135                           ? ( $_ => scalar $input->param($_) )
136                           : ( $_ => scalar $input->param($_) || undef )
137                     } @fields
138                 )
139             }
140         );
141
142         try {
143             Koha::Database->new->schema->txn_do(
144                 sub {
145                     $library->store->discard_changes;
146
147                     my $smtp_server_id = $input->param('smtp_server');
148
149                     if ( $smtp_server_id ) {
150                         if ( $smtp_server_id ne '*' ) {
151                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
152                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
153                                 unless $smtp_server;
154                             $library->smtp_server({ smtp_server => $smtp_server });
155                         }
156                     }
157
158                     push @messages, { type => 'message', code => 'success_on_insert' };
159                 }
160             );
161         }
162         catch {
163             push @messages, { type => 'alert', code => 'error_on_insert' };
164         };
165     }
166     $op = 'list';
167 } elsif ( $op eq 'delete_confirm' ) {
168     my $library       = Koha::Libraries->find($branchcode);
169     my $items_count = Koha::Items->search(
170         {   -or => {
171                 holdingbranch => $branchcode,
172                 homebranch    => $branchcode
173             },
174         }
175     )->count;
176     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
177
178     if ( $items_count or $patrons_count ) {
179         push @messages,
180           { type => 'alert',
181             code => 'cannot_delete_library',
182             data => {
183                 items_count   => $items_count,
184                 patrons_count => $patrons_count,
185             },
186           };
187         $op = 'list';
188     } else {
189         $template->param(
190             library       => $library,
191             items_count   => $items_count,
192             patrons_count => $patrons_count,
193         );
194     }
195 } elsif ( $op eq 'delete_confirmed' ) {
196     my $library = Koha::Libraries->find($branchcode);
197
198     my $deleted = eval { $library->delete; };
199
200     if ( $@ or not $deleted ) {
201         push @messages, { type => 'alert', code => 'error_on_delete' };
202     } else {
203         push @messages, { type => 'message', code => 'success_on_delete' };
204     }
205     $op = 'list';
206 } else {
207     $op = 'list';
208 }
209
210 $template->param( libraries_count => Koha::Libraries->search->count )
211     if $op eq 'list';
212
213 $template->param(
214     messages => \@messages,
215     op       => $op,
216 );
217
218 output_html_with_http_headers $input, $cookie, $template->output;