Bug 25642: Fix description of new feature in update_dbix_class_files.pl
[koha.git] / admin / branches.pl
blob68b9c25ce5e0224b71fc165f0362a54b3171946a
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;
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;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 { template_name => "admin/branches.tt",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => { parameters => 'manage_libraries' },
43 debug => 1,
47 if ( $op eq 'add_form' ) {
48 my $library;
49 if ($branchcode) {
50 $library = Koha::Libraries->find($branchcode);
53 $template->param(
54 library => $library,
56 } elsif ( $op eq 'add_validate' ) {
57 my @fields = qw(
58 branchname
59 branchaddress1
60 branchaddress2
61 branchaddress3
62 branchzip
63 branchcity
64 branchstate
65 branchcountry
66 branchphone
67 branchfax
68 branchemail
69 branchreplyto
70 branchreturnpath
71 branchurl
72 issuing
73 branchip
74 branchnotes
75 opac_info
76 marcorgcode
77 pickup_location
79 my $is_a_modif = $input->param('is_a_modif');
81 if ($is_a_modif) {
82 my $library = Koha::Libraries->find($branchcode);
83 for my $field (@fields) {
84 $library->$field( scalar $input->param($field) );
86 eval { $library->store; };
87 if ($@) {
88 push @messages, { type => 'alert', code => 'error_on_update' };
89 } else {
90 push @messages, { type => 'message', code => 'success_on_update' };
92 } else {
93 $branchcode =~ s|\s||g;
94 my $library = Koha::Library->new(
95 { branchcode => $branchcode,
96 ( map { $_ => scalar $input->param($_) || undef } @fields )
99 eval { $library->store; };
100 if ($@) {
101 push @messages, { type => 'alert', code => 'error_on_insert' };
102 } else {
103 push @messages, { type => 'message', code => 'success_on_insert' };
106 $op = 'list';
107 } elsif ( $op eq 'delete_confirm' ) {
108 my $library = Koha::Libraries->find($branchcode);
109 my $items_count = Koha::Items->search(
110 { -or => {
111 holdingbranch => $branchcode,
112 homebranch => $branchcode
115 )->count;
116 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
118 if ( $items_count or $patrons_count ) {
119 push @messages,
120 { type => 'alert',
121 code => 'cannot_delete_library',
122 data => {
123 items_count => $items_count,
124 patrons_count => $patrons_count,
127 $op = 'list';
128 } else {
129 $template->param(
130 library => $library,
131 items_count => $items_count,
132 patrons_count => $patrons_count,
135 } elsif ( $op eq 'delete_confirmed' ) {
136 my $library = Koha::Libraries->find($branchcode);
138 my $deleted = eval { $library->delete; };
140 if ( $@ or not $deleted ) {
141 push @messages, { type => 'alert', code => 'error_on_delete' };
142 } else {
143 push @messages, { type => 'message', code => 'success_on_delete' };
145 $op = 'list';
146 } else {
147 $op = 'list';
150 if ( $op eq 'list' ) {
151 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
152 $template->param( libraries => $libraries, );
155 $template->param(
156 messages => \@messages,
157 op => $op,
160 output_html_with_http_headers $input, $cookie, $template->output;