Bug 27070: Add cross_fields type to our searches
[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
23 use CGI qw ( -utf8 );
24 use Try::Tiny;
25
26 use C4::Auth;
27 use C4::Context;
28 use C4::Output;
29 use C4::Koha;
30
31 use Koha::Database;
32 use Koha::Patrons;
33 use Koha::Items;
34 use Koha::Libraries;
35 use Koha::SMTP::Servers;
36
37 my $input        = CGI->new;
38 my $branchcode   = $input->param('branchcode');
39 my $categorycode = $input->param('categorycode');
40 my $op           = $input->param('op') || 'list';
41 my @messages;
42
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {   template_name   => "admin/branches.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { parameters => 'manage_libraries' },
48         debug           => 1,
49     }
50 );
51
52 if ( $op eq 'add_form' ) {
53     my $library;
54     if ($branchcode) {
55         $library = Koha::Libraries->find($branchcode);
56         $template->param( selected_smtp_server => $library->smtp_server );
57     }
58
59     my @smtp_servers = Koha::SMTP::Servers->search;
60
61     $template->param(
62         library      => $library,
63         smtp_servers => \@smtp_servers
64     );
65 } elsif ( $op eq 'add_validate' ) {
66     my @fields = qw(
67       branchname
68       branchaddress1
69       branchaddress2
70       branchaddress3
71       branchzip
72       branchcity
73       branchstate
74       branchcountry
75       branchphone
76       branchfax
77       branchemail
78       branchillemail
79       branchreplyto
80       branchreturnpath
81       branchurl
82       issuing
83       branchip
84       branchnotes
85       opac_info
86       marcorgcode
87       pickup_location
88     );
89     my $is_a_modif = $input->param('is_a_modif');
90
91     if ($is_a_modif) {
92         my $library = Koha::Libraries->find($branchcode);
93         for my $field (@fields) {
94             $library->$field( scalar $input->param($field) );
95         }
96
97         try {
98             Koha::Database->new->schema->txn_do(
99                 sub {
100                     $library->store->discard_changes;
101                     # Deal with SMTP server
102                     my $smtp_server_id = $input->param('smtp_server');
103
104                     if ( $smtp_server_id ) {
105                         if ( $smtp_server_id eq '*' ) {
106                             $library->smtp_server({ smtp_server => undef });
107                         }
108                         else {
109                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
110                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
111                                 unless $smtp_server;
112                             $library->smtp_server({ smtp_server => $smtp_server });
113                         }
114                     }
115
116                     push @messages, { type => 'message', code => 'success_on_update' };
117                 }
118             );
119         }
120         catch {
121             push @messages, { type => 'alert', code => 'error_on_update' };
122         }
123     } else {
124         $branchcode =~ s|\s||g;
125         my $library = Koha::Library->new(
126             {   branchcode => $branchcode,
127                 ( map { $_ => scalar $input->param($_) || undef } @fields )
128             }
129         );
130
131         try {
132             Koha::Database->new->schema->txn_do(
133                 sub {
134                     $library->store->discard_changes;
135
136                     my $smtp_server_id = $input->param('smtp_server');
137
138                     if ( $smtp_server_id ) {
139                         if ( $smtp_server_id ne '*' ) {
140                             my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
141                             Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
142                                 unless $smtp_server;
143                             $library->smtp_server({ smtp_server => $smtp_server });
144                         }
145                     }
146
147                     push @messages, { type => 'message', code => 'success_on_insert' };
148                 }
149             );
150         }
151         catch {
152             push @messages, { type => 'alert', code => 'error_on_insert' };
153         };
154     }
155     $op = 'list';
156 } elsif ( $op eq 'delete_confirm' ) {
157     my $library       = Koha::Libraries->find($branchcode);
158     my $items_count = Koha::Items->search(
159         {   -or => {
160                 holdingbranch => $branchcode,
161                 homebranch    => $branchcode
162             },
163         }
164     )->count;
165     my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
166
167     if ( $items_count or $patrons_count ) {
168         push @messages,
169           { type => 'alert',
170             code => 'cannot_delete_library',
171             data => {
172                 items_count   => $items_count,
173                 patrons_count => $patrons_count,
174             },
175           };
176         $op = 'list';
177     } else {
178         $template->param(
179             library       => $library,
180             items_count   => $items_count,
181             patrons_count => $patrons_count,
182         );
183     }
184 } elsif ( $op eq 'delete_confirmed' ) {
185     my $library = Koha::Libraries->find($branchcode);
186
187     my $deleted = eval { $library->delete; };
188
189     if ( $@ or not $deleted ) {
190         push @messages, { type => 'alert', code => 'error_on_delete' };
191     } else {
192         push @messages, { type => 'message', code => 'success_on_delete' };
193     }
194     $op = 'list';
195 } else {
196     $op = 'list';
197 }
198
199 $template->param( libraries_count => Koha::Libraries->search->count )
200     if $op eq 'list';
201
202 $template->param(
203     messages => \@messages,
204     op       => $op,
205 );
206
207 output_html_with_http_headers $input, $cookie, $template->output;