Bug 20582: (QA follow-up) Add POD to satisfy coding guidelines
[koha.git] / admin / branches.pl
blob98a2d1f49fb1aff2f9d73d1dffadfcd683ed2518
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2015 Koha Development Team
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use Try::Tiny;
26 use C4::Auth;
27 use C4::Context;
28 use C4::Output;
29 use C4::Koha;
31 use Koha::Database;
32 use Koha::Patrons;
33 use Koha::Items;
34 use Koha::Libraries;
35 use Koha::SMTP::Servers;
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;
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 debug => 1,
52 if ( $op eq 'add_form' ) {
53 my $library;
54 if ($branchcode) {
55 $library = Koha::Libraries->find($branchcode);
56 $template->param( selected_smtp_server => $library->smtp_server );
59 my @smtp_servers = Koha::SMTP::Servers->search;
61 $template->param(
62 library => $library,
63 smtp_servers => \@smtp_servers
65 } elsif ( $op eq '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 branchreplyto
79 branchreturnpath
80 branchurl
81 issuing
82 branchip
83 branchnotes
84 opac_info
85 marcorgcode
86 pickup_location
88 my $is_a_modif = $input->param('is_a_modif');
90 if ($is_a_modif) {
91 my $library = Koha::Libraries->find($branchcode);
92 for my $field (@fields) {
93 $library->$field( scalar $input->param($field) );
96 try {
97 Koha::Database->new->schema->txn_do(
98 sub {
99 $library->store->discard_changes;
100 # Deal with SMTP server
101 my $smtp_server_id = $input->param('smtp_server');
103 if ( $smtp_server_id ) {
104 if ( $smtp_server_id eq '*' ) {
105 $library->smtp_server({ smtp_server => undef });
107 else {
108 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
109 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
110 unless $smtp_server;
111 $library->smtp_server({ smtp_server => $smtp_server });
115 push @messages, { type => 'message', code => 'success_on_update' };
119 catch {
120 push @messages, { type => 'alert', code => 'error_on_update' };
122 } else {
123 $branchcode =~ s|\s||g;
124 my $library = Koha::Library->new(
125 { branchcode => $branchcode,
126 ( map { $_ => scalar $input->param($_) || undef } @fields )
130 try {
131 Koha::Database->new->schema->txn_do(
132 sub {
133 $library->store->discard_changes;
135 my $smtp_server_id = $input->param('smtp_server');
137 if ( $smtp_server_id ) {
138 if ( $smtp_server_id ne '*' ) {
139 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
140 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
141 unless $smtp_server;
142 $library->smtp_server({ smtp_server => $smtp_server });
146 push @messages, { type => 'message', code => 'success_on_insert' };
150 catch {
151 push @messages, { type => 'alert', code => 'error_on_insert' };
154 $op = 'list';
155 } elsif ( $op eq 'delete_confirm' ) {
156 my $library = Koha::Libraries->find($branchcode);
157 my $items_count = Koha::Items->search(
158 { -or => {
159 holdingbranch => $branchcode,
160 homebranch => $branchcode
163 )->count;
164 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
166 if ( $items_count or $patrons_count ) {
167 push @messages,
168 { type => 'alert',
169 code => 'cannot_delete_library',
170 data => {
171 items_count => $items_count,
172 patrons_count => $patrons_count,
175 $op = 'list';
176 } else {
177 $template->param(
178 library => $library,
179 items_count => $items_count,
180 patrons_count => $patrons_count,
183 } elsif ( $op eq 'delete_confirmed' ) {
184 my $library = Koha::Libraries->find($branchcode);
186 my $deleted = eval { $library->delete; };
188 if ( $@ or not $deleted ) {
189 push @messages, { type => 'alert', code => 'error_on_delete' };
190 } else {
191 push @messages, { type => 'message', code => 'success_on_delete' };
193 $op = 'list';
194 } else {
195 $op = 'list';
198 $template->param( libraries_count => Koha::Libraries->search->count )
199 if $op eq 'list';
201 $template->param(
202 messages => \@messages,
203 op => $op,
206 output_html_with_http_headers $input, $cookie, $template->output;