Bug 16122: Add display and Table settings
[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     $template->param(
54         library      => Koha::Libraries->find($branchcode),
55         smtp_servers => Koha::SMTP::Servers->search,
56     );
57 } elsif ( $branchcode && $op eq 'view' ) {
58     my $library = Koha::Libraries->find($branchcode);
59     $template->param(
60         library      => $library,
61     );
62 } elsif ( $op eq 'cud-add_validate' ) {
63     my @fields = qw(
64       branchname
65       branchaddress1
66       branchaddress2
67       branchaddress3
68       branchzip
69       branchcity
70       branchstate
71       branchcountry
72       branchphone
73       branchfax
74       branchemail
75       branchillemail
76       branchreplyto
77       branchreturnpath
78       branchurl
79       issuing
80       branchip
81       branchnotes
82       marcorgcode
83       pickup_location
84       public
85       opacuserjs
86       opacusercss
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 =~ /^(pickup_location|public)$/  ) {
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
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                         } 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                     # Deal with opening hours
121                     my @days        = $input->multi_param("day");
122                     my @open_times  = $input->multi_param("open_time");
123                     my @close_times = $input->multi_param("close_time");
124
125                     my $index = 0;
126                     foreach my $day (@days) {
127                         if ( $open_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
128                             $open_times[$index] = undef;
129                         }
130                         if ( $close_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
131                             $close_times[$index] = undef;
132                         }
133
134                         my $openday = Koha::Library::Hours->find( { library_id => $branchcode, day => $day } );
135                         if ($openday) {
136                             $openday->update(
137                                 { open_time => $open_times[$index], close_time => $close_times[$index] } );
138                         } else {
139                             $openday = Koha::Library::Hour->new(
140                                 {
141                                     library_id => $branchcode, day => $day, open_time => $open_times[$index],
142                                     close_time => $close_times[$index]
143                                 }
144                             )->store;
145                         }
146                         $index++;
147                     }
148
149                     push @messages, { type => 'message', code => 'success_on_update' };
150                 }
151             );
152         }
153         catch {
154             push @messages, { type => 'alert', code => 'error_on_update' };
155         };
156     } else {
157         $branchcode =~ s|\s||g;
158         my $library = Koha::Library->new(
159             {
160                 branchcode => $branchcode,
161                 (
162                     map {
163                         /^(pickup_location|public)$/ # Don't fallback to undef for those fields
164                           ? ( $_ => scalar $input->param($_) )
165                           : ( $_ => scalar $input->param($_) || undef )
166                     } @fields
167                 )
168             }
169         );
170
171         try {
172             Koha::Database->new->schema->txn_do(
173                 sub {
174                     $library->store->discard_changes;
175
176                     my $smtp_server_id = $input->param('smtp_server');
177
178                     # Deal with SMTP server
179                     if ($smtp_server_id) {
180                         if ( $smtp_server_id ne '*' ) {
181                             my $smtp_server = Koha::SMTP::Servers->find($smtp_server_id);
182                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
183                                 unless $smtp_server;
184                             $library->smtp_server( { smtp_server => $smtp_server } );
185                         }
186                     }
187
188                     # Deal with opening hours
189                     my @days        = $input->multi_param("day");
190                     my @open_times  = $input->multi_param("open_time");
191                     my @close_times = $input->multi_param("close_time");
192
193                     my $index = 0;
194                     foreach my $day (@days) {
195                         if ( $open_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
196                             $open_times[$index] = undef;
197                         }
198                         if ( $close_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
199                             $close_times[$index] = undef;
200                         }
201
202                         my $openday = Koha::Library::Hour->new(
203                             {
204                                 library_id => $branchcode, day => $day, open_time => $open_times[$index],
205                                 close_time => $close_times[$index]
206                             }
207                         )->store;
208                         $index++;
209                     }
210
211                     push @messages, { type => 'message', code => 'success_on_insert' };
212                 }
213             );
214         }
215         catch {
216             push @messages, { type => 'alert', code => 'error_on_insert' };
217         };
218     }
219     $op = 'list';
220 } elsif ( $op eq 'delete_confirm' ) {
221     my $library       = Koha::Libraries->find($branchcode);
222     my $items_count = Koha::Items->search(
223         {   -or => {
224                 holdingbranch => $branchcode,
225                 homebranch    => $branchcode
226             },
227         }
228     )->count;
229     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
230
231     if ( $items_count or $patrons_count ) {
232         push @messages,
233           { type => 'alert',
234             code => 'cannot_delete_library',
235             data => {
236                 items_count   => $items_count,
237                 patrons_count => $patrons_count,
238             },
239           };
240         $op = 'list';
241     } else {
242         $template->param(
243             library       => $library,
244             items_count   => $items_count,
245             patrons_count => $patrons_count,
246         );
247     }
248 } elsif ( $op eq 'cud-delete_confirmed' ) {
249     my $library = Koha::Libraries->find($branchcode);
250
251     my $deleted = eval { $library->delete; };
252
253     if ( $@ or not $deleted ) {
254         push @messages, { type => 'alert', code => 'error_on_delete' };
255     } else {
256         push @messages, { type => 'message', code => 'success_on_delete' };
257     }
258     $op = 'list';
259 } else {
260     $op = 'list';
261 }
262
263 $template->param( libraries_count => Koha::Libraries->search->count )
264     if $op eq 'list';
265
266 $template->param(
267     messages => \@messages,
268     op       => $op,
269 );
270
271 output_html_with_http_headers $input, $cookie, $template->output;