Bug 16259: Replace CGI->param with CGI->multi_param in list context - part 2
[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 use Koha::LibraryCategories;
31
32 my $input        = new CGI;
33 my $branchcode   = $input->param('branchcode');
34 my $categorycode = $input->param('categorycode');
35 my $op           = $input->param('op') || 'list';
36 my @messages;
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39     {   template_name   => "admin/branches.tt",
40         query           => $input,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
44         debug           => 1,
45     }
46 );
47
48 if ( $op eq 'add_form' ) {
49     my $library;
50     if ($branchcode) {
51         $library = Koha::Libraries->find($branchcode);
52     }
53
54     $template->param(
55         library    => $library,
56         categories => [ Koha::LibraryCategories->search( {}, { order_by => [ 'categorytype', 'categoryname' ] } ) ],
57         $library ? ( selected_categorycodes => [ map { $_->categorycode } $library->get_categories ] ) : (),
58     );
59 } elsif ( $op eq 'add_validate' ) {
60     my @fields = qw(
61       branchname
62       branchaddress1
63       branchaddress2
64       branchaddress3
65       branchzip
66       branchcity
67       branchstate
68       branchcountry
69       branchphone
70       branchfax
71       branchemail
72       branchreplyto
73       branchreturnpath
74       branchurl
75       issuing
76       branchip
77       branchnotes
78       opac_info
79     );
80     my $is_a_modif = $input->param('is_a_modif');
81
82     my @categories;
83     for my $category ( Koha::LibraryCategories->search ) {
84         push @categories, $category
85           if $input->param( "selected_categorycode_" . $category->categorycode );
86     }
87     if ($is_a_modif) {
88         my $library = Koha::Libraries->find($branchcode);
89         for my $field (@fields) {
90             $library->$field( scalar $input->param($field) );
91         }
92         $library->update_categories( \@categories );
93
94         eval { $library->store; };
95         if ($@) {
96             push @messages, { type => 'alert', code => 'error_on_update' };
97         } else {
98             push @messages, { type => 'message', code => 'success_on_update' };
99         }
100     } else {
101         $branchcode =~ s|\s||g;
102         my $library = Koha::Library->new(
103             {   branchcode => $branchcode,
104                 ( map { $_ => scalar $input->param($_) || undef } @fields )
105             }
106         );
107         eval { $library->store; };
108         $library->add_to_categories( \@categories );
109         if ($@) {
110             push @messages, { type => 'alert', code => 'error_on_insert' };
111         } else {
112             push @messages, { type => 'message', code => 'success_on_insert' };
113         }
114     }
115     $op = 'list';
116 } elsif ( $op eq 'delete_confirm' ) {
117     my $library       = Koha::Libraries->find($branchcode);
118     my $items_count = Koha::Items->search(
119         {   -or => {
120                 holdingbranch => $branchcode,
121                 homebranch    => $branchcode
122             },
123         }
124     )->count;
125     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
126
127     if ( $items_count or $patrons_count ) {
128         push @messages,
129           { type => 'alert',
130             code => 'cannot_delete_library',
131             data => {
132                 items_count   => $items_count,
133                 patrons_count => $patrons_count,
134             },
135           };
136         $op = 'list';
137     } else {
138         $template->param(
139             library       => $library,
140             items_count   => $items_count,
141             patrons_count => $patrons_count,
142         );
143     }
144 } elsif ( $op eq 'delete_confirmed' ) {
145     my $library = Koha::Libraries->find($branchcode);
146
147     my $deleted = eval { $library->delete; };
148
149     if ( $@ or not $deleted ) {
150         push @messages, { type => 'alert', code => 'error_on_delete' };
151     } else {
152         push @messages, { type => 'message', code => 'success_on_delete' };
153     }
154     $op = 'list';
155 } elsif ( $op eq 'add_form_category' ) {
156     my $category;
157     if ($categorycode) {
158         $category = Koha::LibraryCategories->find($categorycode);
159     }
160     $template->param( category => $category, );
161 } elsif ( $op eq 'add_validate_category' ) {
162     my $is_a_modif = $input->param('is_a_modif');
163     my @fields     = qw(
164       categoryname
165       codedescription
166       categorytype
167     );
168     if ($is_a_modif) {
169         my $category = Koha::LibraryCategories->find($categorycode);
170         for my $field (@fields) {
171             $category->$field( scalar $input->param($field) );
172         }
173         $category->show_in_pulldown( scalar $input->param('show_in_pulldown') eq 'on' );
174         eval { $category->store; };
175         if ($@) {
176             push @messages, { type => 'alert', code => 'error_on_update_category' };
177         } else {
178             push @messages, { type => 'message', code => 'success_on_update_category' };
179         }
180     } else {
181         my $category = Koha::LibraryCategory->new(
182             {   categorycode => $categorycode,
183                 ( map { $_ => scalar $input->param($_) || undef } @fields )
184             }
185         );
186         $category->show_in_pulldown( scalar $input->param('show_in_pulldown') eq 'on' );
187         eval { $category->store; };
188         if ($@) {
189             push @messages, { type => 'alert', code => 'error_on_insert_category' };
190         } else {
191             push @messages, { type => 'message', code => 'success_on_insert_category' };
192         }
193     }
194     $op = 'list';
195 } elsif ( $op eq 'delete_confirm_category' ) {
196     my $category = Koha::LibraryCategories->find($categorycode);
197     if ( my $libraries_count = $category->libraries->count ) {
198         push @messages,
199           { type => 'alert',
200             code => 'cannot_delete_category',
201             data => { libraries_count => $libraries_count, },
202           };
203         $op = 'list';
204     } else {
205         $template->param( category => $category );
206     }
207 } elsif ( $op eq 'delete_confirmed_category' ) {
208     my $category = Koha::LibraryCategories->find($categorycode);
209     my $deleted = eval { $category->delete; };
210
211     if ( $@ or not $deleted ) {
212         push @messages, { type => 'alert', code => 'error_on_delete_category' };
213     } else {
214         push @messages, { type => 'message', code => 'success_on_delete_category' };
215     }
216     $op = 'list';
217 } else {
218     $op = 'list';
219 }
220
221 if ( $op eq 'list' ) {
222     my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
223     $template->param(
224         libraries   => $libraries,
225         group_types => [
226             {   categorytype => 'searchdomain',
227                 categories   => [ Koha::LibraryCategories->search( { categorytype => 'searchdomain' } ) ],
228             },
229             {   categorytype => 'properties',
230                 categories   => [ Koha::LibraryCategories->search( { categorytype => 'properties' } ) ],
231             },
232         ]
233     );
234 }
235
236 $template->param(
237     messages => \@messages,
238     op       => $op,
239 );
240
241 output_html_with_http_headers $input, $cookie, $template->output;