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