Bug 6796: Fix saving of libraries and tests
[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 use Koha::Library::Hours;
37
38 my $input        = CGI->new;
39 my $branchcode   = $input->param('branchcode');
40 my $categorycode = $input->param('categorycode');
41 my $op           = $input->param('op') || 'list';
42 my @messages;
43
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45     {   template_name   => "admin/branches.tt",
46         query           => $input,
47         type            => "intranet",
48         flagsrequired   => { parameters => 'manage_libraries' },
49     }
50 );
51
52 if ( $op eq 'add_form' ) {
53     my @opening_hours = Koha::Library::Hours->search({ library_id => $branchcode }, { order_by => { -asc => 'day' } })->as_list;
54
55     $template->param(
56         library      => Koha::Libraries->find($branchcode),
57         smtp_servers => Koha::SMTP::Servers->search,
58         opening_hours => \@opening_hours
59     );
60 } elsif ( $branchcode && $op eq 'view' ) {
61     my $library = Koha::Libraries->find($branchcode);
62     $template->param(
63         library      => $library,
64     );
65 } elsif ( $op eq 'cud-add_validate' ) {
66     my @fields = qw(
67       branchname
68       branchaddress1
69       branchaddress2
70       branchaddress3
71       branchzip
72       branchcity
73       branchstate
74       branchcountry
75       branchphone
76       branchfax
77       branchemail
78       branchillemail
79       branchreplyto
80       branchreturnpath
81       branchurl
82       issuing
83       branchip
84       branchnotes
85       marcorgcode
86       pickup_location
87       public
88       opacuserjs
89       opacusercss
90     );
91     my $is_a_modif = $input->param('is_a_modif');
92
93     if ($is_a_modif) {
94         my $library = Koha::Libraries->find($branchcode);
95         for my $field (@fields) {
96             if( $field =~ /^(pickup_location|public)$/  ) {
97                 # Don't fallback to undef/NULL, default is 1 in DB
98                 $library->$field( scalar $input->param($field) );
99             } else {
100                 $library->$field( scalar $input->param($field) || undef );
101             }
102         }
103
104         try {
105             Koha::Database->new->schema->txn_do(
106                 sub {
107                     $library->store->discard_changes;
108                     # Deal with SMTP server
109                     my $smtp_server_id = $input->param('smtp_server');
110
111                     if ( $smtp_server_id ) {
112                         if ( $smtp_server_id eq '*' ) {
113                             $library->smtp_server({ smtp_server => undef });
114                         }
115                         else {
116                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
117                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
118                                 unless $smtp_server;
119                             $library->smtp_server({ smtp_server => $smtp_server });
120                         }
121                     }
122
123                     my @days = $input->multi_param("day");
124                     my @open_times = $input->multi_param("open_time");
125                     my @close_times = $input->multi_param("close_time");
126
127                     foreach my $day ( @days ) {
128                         if ( $open_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
129                             $open_times[$day] = undef;
130                         }
131                         if ( $close_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
132                             $close_times[$day] = undef;
133                         }
134
135                         my $openday = Koha::Library::Hours->find({ library_id => $branchcode, day => $day })->update({ open_time => $open_times[$day], close_time => $close_times[$day] });
136                     }
137
138                     push @messages, { type => 'message', code => 'success_on_update' };
139                 }
140             );
141         }
142         catch {
143             push @messages, { type => 'alert', code => 'error_on_update' };
144         };
145     } else {
146         $branchcode =~ s|\s||g;
147         my $library = Koha::Library->new(
148             {
149                 branchcode => $branchcode,
150                 (
151                     map {
152                         /^(pickup_location|public)$/ # Don't fallback to undef for those fields
153                           ? ( $_ => scalar $input->param($_) )
154                           : ( $_ => scalar $input->param($_) || undef )
155                     } @fields
156                 )
157             }
158         );
159
160         try {
161             Koha::Database->new->schema->txn_do(
162                 sub {
163                     $library->store->discard_changes;
164
165                     my $smtp_server_id = $input->param('smtp_server');
166
167                     if ( $smtp_server_id ) {
168                         if ( $smtp_server_id ne '*' ) {
169                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
170                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
171                                 unless $smtp_server;
172                             $library->smtp_server({ smtp_server => $smtp_server });
173                         }
174                     }
175
176                     my @days = $input->multi_param("day");
177                     my @open_times = $input->multi_param("open_time");
178                     my @close_times = $input->multi_param("close_time");
179
180                     foreach my $day ( @days ) {
181                         if ( $open_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
182                             $open_times[$day] = undef;
183                         }
184                         if ( $close_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
185                             $close_times[$day] = undef;
186                         }
187
188                         my $openday = Koha::Library::Hour->new({ library_id => $branchcode, day => $day, open_time => $open_times[$day], close_time => $close_times[$day] })->store;
189                     }
190
191                     push @messages, { type => 'message', code => 'success_on_insert' };
192                 }
193             );
194         }
195         catch {
196             push @messages, { type => 'alert', code => 'error_on_insert' };
197         };
198     }
199     $op = 'list';
200 } elsif ( $op eq 'delete_confirm' ) {
201     my $library       = Koha::Libraries->find($branchcode);
202     my $items_count = Koha::Items->search(
203         {   -or => {
204                 holdingbranch => $branchcode,
205                 homebranch    => $branchcode
206             },
207         }
208     )->count;
209     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
210
211     if ( $items_count or $patrons_count ) {
212         push @messages,
213           { type => 'alert',
214             code => 'cannot_delete_library',
215             data => {
216                 items_count   => $items_count,
217                 patrons_count => $patrons_count,
218             },
219           };
220         $op = 'list';
221     } else {
222         $template->param(
223             library       => $library,
224             items_count   => $items_count,
225             patrons_count => $patrons_count,
226         );
227     }
228 } elsif ( $op eq 'cud-delete_confirmed' ) {
229     my $library = Koha::Libraries->find($branchcode);
230
231     my $deleted = eval { $library->delete; };
232
233     if ( $@ or not $deleted ) {
234         push @messages, { type => 'alert', code => 'error_on_delete' };
235     } else {
236         push @messages, { type => 'message', code => 'success_on_delete' };
237     }
238     $op = 'list';
239 } else {
240     $op = 'list';
241 }
242
243 $template->param( libraries_count => Koha::Libraries->search->count )
244     if $op eq 'list';
245
246 $template->param(
247     messages => \@messages,
248     op       => $op,
249 );
250
251 output_html_with_http_headers $input, $cookie, $template->output;