Bug 14187: (QA followup) Properly check DB structure before altering it
[koha.git] / admin / branches.pl
blobabe3e5a749f4ccb19e1200b0f4f8db2d3fa36c0c
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;
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;
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;
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,
48 if ( $op eq 'add_form' ) {
49 my $library;
50 if ($branchcode) {
51 $library = Koha::Libraries->find($branchcode);
54 $template->param(
55 library => $library,
56 categories => [ Koha::LibraryCategories->search( {}, { order_by => [ 'categorytype', 'categoryname' ] } ) ],
57 $library ? ( selected_categorycodes => [ map { $_->categorycode } $library->get_categories ] ) : (),
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
80 my $is_a_modif = $input->param('is_a_modif');
82 my @categories;
83 for my $category ( Koha::LibraryCategories->search ) {
84 push @categories, $category
85 if $input->param( "selected_categorycode_" . $category->categorycode );
87 if ($is_a_modif) {
88 my $library = Koha::Libraries->find($branchcode);
89 for my $field (@fields) {
90 $library->$field( scalar $input->param($field) );
92 $library->update_categories( \@categories );
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' };
100 } else {
101 $branchcode =~ s|\s||g;
102 my $library = Koha::Library->new(
103 { branchcode => $branchcode,
104 ( map { $_ => scalar $input->param($_) || undef } @fields )
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' };
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
124 )->count;
125 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
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,
136 $op = 'list';
137 } else {
138 $template->param(
139 library => $library,
140 items_count => $items_count,
141 patrons_count => $patrons_count,
144 } elsif ( $op eq 'delete_confirmed' ) {
145 my $library = Koha::Libraries->find($branchcode);
147 my $deleted = eval { $library->delete; };
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' };
154 $op = 'list';
155 } elsif ( $op eq 'add_form_category' ) {
156 my $category;
157 if ($categorycode) {
158 $category = Koha::LibraryCategories->find($categorycode);
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
168 if ($is_a_modif) {
169 my $category = Koha::LibraryCategories->find($categorycode);
170 for my $field (@fields) {
171 $category->$field( scalar $input->param($field) );
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' };
180 } else {
181 my $category = Koha::LibraryCategory->new(
182 { categorycode => $categorycode,
183 ( map { $_ => scalar $input->param($_) || undef } @fields )
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' };
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, },
203 $op = 'list';
204 } else {
205 $template->param( category => $category );
207 } elsif ( $op eq 'delete_confirmed_category' ) {
208 my $category = Koha::LibraryCategories->find($categorycode);
209 my $deleted = eval { $category->delete; };
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' };
216 $op = 'list';
217 } else {
218 $op = 'list';
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' } ) ],
229 { categorytype => 'properties',
230 categories => [ Koha::LibraryCategories->search( { categorytype => 'properties' } ) ],
236 $template->param(
237 messages => \@messages,
238 op => $op,
241 output_html_with_http_headers $input, $cookie, $template->output;