Bug 33575: (follow-up) don't allow priority and pickup location to be hidden
[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     $template->param(
53         library      => Koha::Libraries->find($branchcode),
54         smtp_servers => Koha::SMTP::Servers->search,
55     );
56 } elsif ( $branchcode && $op eq 'view' ) {
57     my $library = Koha::Libraries->find($branchcode);
58     $template->param(
59         library      => $library,
60     );
61 } elsif ( $op eq 'add_validate' ) {
62     my @fields = qw(
63       branchname
64       branchaddress1
65       branchaddress2
66       branchaddress3
67       branchzip
68       branchcity
69       branchstate
70       branchcountry
71       branchphone
72       branchfax
73       branchemail
74       branchillemail
75       branchreplyto
76       branchreturnpath
77       branchurl
78       issuing
79       branchip
80       branchnotes
81       marcorgcode
82       pickup_location
83       public
84       opacuserjs
85       opacusercss
86     );
87     my $is_a_modif = $input->param('is_a_modif');
88
89     if ($is_a_modif) {
90         my $library = Koha::Libraries->find($branchcode);
91         for my $field (@fields) {
92             if( $field =~ /^(pickup_location|public)$/  ) {
93                 # Don't fallback to undef/NULL, default is 1 in DB
94                 $library->$field( scalar $input->param($field) );
95             } else {
96                 $library->$field( scalar $input->param($field) || undef );
97             }
98         }
99
100         try {
101             Koha::Database->new->schema->txn_do(
102                 sub {
103                     $library->store->discard_changes;
104                     # Deal with SMTP server
105                     my $smtp_server_id = $input->param('smtp_server');
106
107                     if ( $smtp_server_id ) {
108                         if ( $smtp_server_id eq '*' ) {
109                             $library->smtp_server({ smtp_server => undef });
110                         }
111                         else {
112                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
113                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
114                                 unless $smtp_server;
115                             $library->smtp_server({ smtp_server => $smtp_server });
116                         }
117                     }
118
119                     push @messages, { type => 'message', code => 'success_on_update' };
120                 }
121             );
122         }
123         catch {
124             push @messages, { type => 'alert', code => 'error_on_update' };
125         };
126     } else {
127         $branchcode =~ s|\s||g;
128         my $library = Koha::Library->new(
129             {
130                 branchcode => $branchcode,
131                 (
132                     map {
133                         /^(pickup_location|public)$/ # Don't fallback to undef for those fields
134                           ? ( $_ => scalar $input->param($_) )
135                           : ( $_ => scalar $input->param($_) || undef )
136                     } @fields
137                 )
138             }
139         );
140
141         try {
142             Koha::Database->new->schema->txn_do(
143                 sub {
144                     $library->store->discard_changes;
145
146                     my $smtp_server_id = $input->param('smtp_server');
147
148                     if ( $smtp_server_id ) {
149                         if ( $smtp_server_id ne '*' ) {
150                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
151                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
152                                 unless $smtp_server;
153                             $library->smtp_server({ smtp_server => $smtp_server });
154                         }
155                     }
156
157                     push @messages, { type => 'message', code => 'success_on_insert' };
158                 }
159             );
160         }
161         catch {
162             push @messages, { type => 'alert', code => 'error_on_insert' };
163         };
164     }
165     $op = 'list';
166 } elsif ( $op eq 'delete_confirm' ) {
167     my $library       = Koha::Libraries->find($branchcode);
168     my $items_count = Koha::Items->search(
169         {   -or => {
170                 holdingbranch => $branchcode,
171                 homebranch    => $branchcode
172             },
173         }
174     )->count;
175     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
176
177     if ( $items_count or $patrons_count ) {
178         push @messages,
179           { type => 'alert',
180             code => 'cannot_delete_library',
181             data => {
182                 items_count   => $items_count,
183                 patrons_count => $patrons_count,
184             },
185           };
186         $op = 'list';
187     } else {
188         $template->param(
189             library       => $library,
190             items_count   => $items_count,
191             patrons_count => $patrons_count,
192         );
193     }
194 } elsif ( $op eq 'delete_confirmed' ) {
195     my $library = Koha::Libraries->find($branchcode);
196
197     my $deleted = eval { $library->delete; };
198
199     if ( $@ or not $deleted ) {
200         push @messages, { type => 'alert', code => 'error_on_delete' };
201     } else {
202         push @messages, { type => 'message', code => 'success_on_delete' };
203     }
204     $op = 'list';
205 } else {
206     $op = 'list';
207 }
208
209 $template->param( libraries_count => Koha::Libraries->search->count )
210     if $op eq 'list';
211
212 $template->param(
213     messages => \@messages,
214     op       => $op,
215 );
216
217 output_html_with_http_headers $input, $cookie, $template->output;