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