Bug 24663: Remove authnotrequired if set to 0
[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 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Context;
25 use C4::Output;
26 use C4::Koha;
27 use Koha::Patrons;
28 use Koha::Items;
29 use Koha::Libraries;
30
31 my $input        = new CGI;
32 my $branchcode   = $input->param('branchcode');
33 my $categorycode = $input->param('categorycode');
34 my $op           = $input->param('op') || 'list';
35 my @messages;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {   template_name   => "admin/branches.tt",
39         query           => $input,
40         type            => "intranet",
41         flagsrequired   => { parameters => 'manage_libraries' },
42         debug           => 1,
43     }
44 );
45
46 if ( $op eq 'add_form' ) {
47     my $library;
48     if ($branchcode) {
49         $library = Koha::Libraries->find($branchcode);
50     }
51
52     $template->param(
53         library    => $library,
54     );
55 } elsif ( $op eq 'add_validate' ) {
56     my @fields = qw(
57       branchname
58       branchaddress1
59       branchaddress2
60       branchaddress3
61       branchzip
62       branchcity
63       branchstate
64       branchcountry
65       branchphone
66       branchfax
67       branchemail
68       branchreplyto
69       branchreturnpath
70       branchurl
71       issuing
72       branchip
73       branchnotes
74       opac_info
75       marcorgcode
76       pickup_location
77     );
78     my $is_a_modif = $input->param('is_a_modif');
79
80     if ($is_a_modif) {
81         my $library = Koha::Libraries->find($branchcode);
82         for my $field (@fields) {
83             $library->$field( scalar $input->param($field) );
84         }
85         eval { $library->store; };
86         if ($@) {
87             push @messages, { type => 'alert', code => 'error_on_update' };
88         } else {
89             push @messages, { type => 'message', code => 'success_on_update' };
90         }
91     } else {
92         $branchcode =~ s|\s||g;
93         my $library = Koha::Library->new(
94             {   branchcode => $branchcode,
95                 ( map { $_ => scalar $input->param($_) || undef } @fields )
96             }
97         );
98         eval { $library->store; };
99         if ($@) {
100             push @messages, { type => 'alert', code => 'error_on_insert' };
101         } else {
102             push @messages, { type => 'message', code => 'success_on_insert' };
103         }
104     }
105     $op = 'list';
106 } elsif ( $op eq 'delete_confirm' ) {
107     my $library       = Koha::Libraries->find($branchcode);
108     my $items_count = Koha::Items->search(
109         {   -or => {
110                 holdingbranch => $branchcode,
111                 homebranch    => $branchcode
112             },
113         }
114     )->count;
115     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
116
117     if ( $items_count or $patrons_count ) {
118         push @messages,
119           { type => 'alert',
120             code => 'cannot_delete_library',
121             data => {
122                 items_count   => $items_count,
123                 patrons_count => $patrons_count,
124             },
125           };
126         $op = 'list';
127     } else {
128         $template->param(
129             library       => $library,
130             items_count   => $items_count,
131             patrons_count => $patrons_count,
132         );
133     }
134 } elsif ( $op eq 'delete_confirmed' ) {
135     my $library = Koha::Libraries->find($branchcode);
136
137     my $deleted = eval { $library->delete; };
138
139     if ( $@ or not $deleted ) {
140         push @messages, { type => 'alert', code => 'error_on_delete' };
141     } else {
142         push @messages, { type => 'message', code => 'success_on_delete' };
143     }
144     $op = 'list';
145 } else {
146     $op = 'list';
147 }
148
149 $template->param( libraries_count => Koha::Libraries->search->count )
150     if $op eq 'list';
151
152 $template->param(
153     messages => \@messages,
154     op       => $op,
155 );
156
157 output_html_with_http_headers $input, $cookie, $template->output;