Bug 19410: (follow-up) Fix typo in POD
[koha.git] / admin / branches.pl
blobbcd712f2925661fb3459142af99c67b773e2163e
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
79 marcorgcode
81 my $is_a_modif = $input->param('is_a_modif');
83 my @categories;
84 for my $category ( Koha::LibraryCategories->search ) {
85 push @categories, $category
86 if $input->param( "selected_categorycode_" . $category->categorycode );
88 if ($is_a_modif) {
89 my $library = Koha::Libraries->find($branchcode);
90 for my $field (@fields) {
91 $library->$field( scalar $input->param($field) );
93 $library->update_categories( \@categories );
95 eval { $library->store; };
96 if ($@) {
97 push @messages, { type => 'alert', code => 'error_on_update' };
98 } else {
99 push @messages, { type => 'message', code => 'success_on_update' };
101 } else {
102 $branchcode =~ s|\s||g;
103 my $library = Koha::Library->new(
104 { branchcode => $branchcode,
105 ( map { $_ => scalar $input->param($_) || undef } @fields )
108 eval { $library->store; };
109 $library->add_to_categories( \@categories );
110 if ($@) {
111 push @messages, { type => 'alert', code => 'error_on_insert' };
112 } else {
113 push @messages, { type => 'message', code => 'success_on_insert' };
116 $op = 'list';
117 } elsif ( $op eq 'delete_confirm' ) {
118 my $library = Koha::Libraries->find($branchcode);
119 my $items_count = Koha::Items->search(
120 { -or => {
121 holdingbranch => $branchcode,
122 homebranch => $branchcode
125 )->count;
126 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
128 if ( $items_count or $patrons_count ) {
129 push @messages,
130 { type => 'alert',
131 code => 'cannot_delete_library',
132 data => {
133 items_count => $items_count,
134 patrons_count => $patrons_count,
137 $op = 'list';
138 } else {
139 $template->param(
140 library => $library,
141 items_count => $items_count,
142 patrons_count => $patrons_count,
145 } elsif ( $op eq 'delete_confirmed' ) {
146 my $library = Koha::Libraries->find($branchcode);
148 my $deleted = eval { $library->delete; };
150 if ( $@ or not $deleted ) {
151 push @messages, { type => 'alert', code => 'error_on_delete' };
152 } else {
153 push @messages, { type => 'message', code => 'success_on_delete' };
155 $op = 'list';
156 } elsif ( $op eq 'add_form_category' ) {
157 my $category;
158 if ($categorycode) {
159 $category = Koha::LibraryCategories->find($categorycode);
161 $template->param( category => $category, );
162 } elsif ( $op eq 'add_validate_category' ) {
163 my $is_a_modif = $input->param('is_a_modif');
164 my @fields = qw(
165 categoryname
166 codedescription
167 categorytype
169 if ($is_a_modif) {
170 my $category = Koha::LibraryCategories->find($categorycode);
171 for my $field (@fields) {
172 $category->$field( scalar $input->param($field) );
174 $category->show_in_pulldown( scalar $input->param('show_in_pulldown') eq 'on' );
175 eval { $category->store; };
176 if ($@) {
177 push @messages, { type => 'alert', code => 'error_on_update_category' };
178 } else {
179 push @messages, { type => 'message', code => 'success_on_update_category' };
181 } else {
182 my $category = Koha::LibraryCategory->new(
183 { categorycode => $categorycode,
184 ( map { $_ => scalar $input->param($_) || undef } @fields )
187 $category->show_in_pulldown( scalar $input->param('show_in_pulldown') eq 'on' );
188 eval { $category->store; };
189 if ($@) {
190 push @messages, { type => 'alert', code => 'error_on_insert_category' };
191 } else {
192 push @messages, { type => 'message', code => 'success_on_insert_category' };
195 $op = 'list';
196 } elsif ( $op eq 'delete_confirm_category' ) {
197 my $category = Koha::LibraryCategories->find($categorycode);
198 if ( my $libraries_count = $category->libraries->count ) {
199 push @messages,
200 { type => 'alert',
201 code => 'cannot_delete_category',
202 data => { libraries_count => $libraries_count, },
204 $op = 'list';
205 } else {
206 $template->param( category => $category );
208 } elsif ( $op eq 'delete_confirmed_category' ) {
209 my $category = Koha::LibraryCategories->find($categorycode);
210 my $deleted = eval { $category->delete; };
212 if ( $@ or not $deleted ) {
213 push @messages, { type => 'alert', code => 'error_on_delete_category' };
214 } else {
215 push @messages, { type => 'message', code => 'success_on_delete_category' };
217 $op = 'list';
218 } else {
219 $op = 'list';
222 if ( $op eq 'list' ) {
223 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
224 $template->param(
225 libraries => $libraries,
226 group_types => [
227 { categorytype => 'searchdomain',
228 categories => [ Koha::LibraryCategories->search( { categorytype => 'searchdomain' } ) ],
230 { categorytype => 'properties',
231 categories => [ Koha::LibraryCategories->search( { categorytype => 'properties' } ) ],
237 $template->param(
238 messages => \@messages,
239 op => $op,
242 output_html_with_http_headers $input, $cookie, $template->output;