Bug 29407: Make the pickup locations dropdown JS reusable
[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       public
88     );
89     my $is_a_modif = $input->param('is_a_modif');
90
91     if ($is_a_modif) {
92         my $library = Koha::Libraries->find($branchcode);
93         for my $field (@fields) {
94             if ( $field eq 'pickup_location' ) {
95                 # Don't fallback to undef/NULL, default is 1 in DB
96                 $library->$field( scalar $input->param($field) );
97             } else {
98                 $library->$field( scalar $input->param($field) || undef );
99             }
100         }
101
102         try {
103             Koha::Database->new->schema->txn_do(
104                 sub {
105                     $library->store->discard_changes;
106                     # Deal with SMTP server
107                     my $smtp_server_id = $input->param('smtp_server');
108
109                     if ( $smtp_server_id ) {
110                         if ( $smtp_server_id eq '*' ) {
111                             $library->smtp_server({ smtp_server => undef });
112                         }
113                         else {
114                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
115                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
116                                 unless $smtp_server;
117                             $library->smtp_server({ smtp_server => $smtp_server });
118                         }
119                     }
120
121                     push @messages, { type => 'message', code => 'success_on_update' };
122                 }
123             );
124         }
125         catch {
126             push @messages, { type => 'alert', code => 'error_on_update' };
127         };
128     } else {
129         $branchcode =~ s|\s||g;
130         my $library = Koha::Library->new(
131             {
132                 branchcode => $branchcode,
133                 (
134                     map {
135                         $_ eq 'pickup_location' # Don't fallback to undef/NULL, default is 1 in DB
136                           ? ( $_ => scalar $input->param($_) )
137                           : ( $_ => scalar $input->param($_) || undef )
138                     } @fields
139                 )
140             }
141         );
142
143         try {
144             Koha::Database->new->schema->txn_do(
145                 sub {
146                     $library->store->discard_changes;
147
148                     my $smtp_server_id = $input->param('smtp_server');
149
150                     if ( $smtp_server_id ) {
151                         if ( $smtp_server_id ne '*' ) {
152                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
153                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
154                                 unless $smtp_server;
155                             $library->smtp_server({ smtp_server => $smtp_server });
156                         }
157                     }
158
159                     push @messages, { type => 'message', code => 'success_on_insert' };
160                 }
161             );
162         }
163         catch {
164             push @messages, { type => 'alert', code => 'error_on_insert' };
165         };
166     }
167     $op = 'list';
168 } elsif ( $op eq 'delete_confirm' ) {
169     my $library       = Koha::Libraries->find($branchcode);
170     my $items_count = Koha::Items->search(
171         {   -or => {
172                 holdingbranch => $branchcode,
173                 homebranch    => $branchcode
174             },
175         }
176     )->count;
177     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
178
179     if ( $items_count or $patrons_count ) {
180         push @messages,
181           { type => 'alert',
182             code => 'cannot_delete_library',
183             data => {
184                 items_count   => $items_count,
185                 patrons_count => $patrons_count,
186             },
187           };
188         $op = 'list';
189     } else {
190         $template->param(
191             library       => $library,
192             items_count   => $items_count,
193             patrons_count => $patrons_count,
194         );
195     }
196 } elsif ( $op eq 'delete_confirmed' ) {
197     my $library = Koha::Libraries->find($branchcode);
198
199     my $deleted = eval { $library->delete; };
200
201     if ( $@ or not $deleted ) {
202         push @messages, { type => 'alert', code => 'error_on_delete' };
203     } else {
204         push @messages, { type => 'message', code => 'success_on_delete' };
205     }
206     $op = 'list';
207 } else {
208     $op = 'list';
209 }
210
211 $template->param( libraries_count => Koha::Libraries->search->count )
212     if $op eq 'list';
213
214 $template->param(
215     messages => \@messages,
216     op       => $op,
217 );
218
219 output_html_with_http_headers $input, $cookie, $template->output;