Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha.git] / admin / cities.pl
1 #! /usr/bin/perl
2
3 # Copyright 2006 SAN OUEST-PROVENCE et Paul POULAIN
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::Context;
24 use C4::Auth;
25 use C4::Output;
26
27 use Koha::Cities;
28
29 my $input       = new CGI;
30 my $searchfield = $input->param('city_name') // q||;
31 my $cityid      = $input->param('cityid');
32 my $op          = $input->param('op') || 'list';
33 my @messages;
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {   template_name   => "admin/cities.tt",
37         query           => $input,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { parameters => 'manage_cities' },
41         debug           => 1,
42     }
43 );
44
45 my $dbh = C4::Context->dbh;
46 if ( $op eq 'add_form' ) {
47     my $city;
48     if ($cityid) {
49         $city = Koha::Cities->find($cityid);
50     }
51
52     $template->param( city => $city, );
53 } elsif ( $op eq 'add_validate' ) {
54     my $cityid       = $input->param('cityid');
55     my $city_name    = $input->param('city_name');
56     my $city_state   = $input->param('city_state');
57     my $city_zipcode = $input->param('city_zipcode');
58     my $city_country = $input->param('city_country');
59
60     if ($cityid) {
61         my $city = Koha::Cities->find($cityid);
62         $city->city_name($city_name);
63         $city->city_state($city_state);
64         $city->city_zipcode($city_zipcode);
65         $city->city_country($city_country);
66         eval { $city->store; };
67         if ($@) {
68             push @messages, { type => 'error', code => 'error_on_update' };
69         } else {
70             push @messages, { type => 'message', code => 'success_on_update' };
71         }
72     } else {
73         my $city = Koha::City->new(
74             {   city_name    => $city_name,
75                 city_state   => $city_state,
76                 city_zipcode => $city_zipcode,
77                 city_country => $city_country,
78             }
79         );
80         eval { $city->store; };
81         if ($@) {
82             push @messages, { type => 'error', code => 'error_on_insert' };
83         } else {
84             push @messages, { type => 'message', code => 'success_on_insert' };
85         }
86     }
87     $searchfield = q||;
88     $op          = 'list';
89 } elsif ( $op eq 'delete_confirm' ) {
90     my $city = Koha::Cities->find($cityid);
91     $template->param( city => $city, );
92 } elsif ( $op eq 'delete_confirmed' ) {
93     my $city = Koha::Cities->find($cityid);
94     my $deleted = eval { $city->delete; };
95
96     if ( $@ or not $deleted ) {
97         push @messages, { type => 'error', code => 'error_on_delete' };
98     } else {
99         push @messages, { type => 'message', code => 'success_on_delete' };
100     }
101     $op = 'list';
102 }
103
104 if ( $op eq 'list' ) {
105     my $cities = Koha::Cities->search( { city_name => { -like => "%$searchfield%" } } );
106     $template->param( cities => $cities, );
107 }
108
109 $template->param(
110     cityid      => $cityid,
111     searchfield => $searchfield,
112     messages    => \@messages,
113     op          => $op,
114 );
115
116 output_html_with_http_headers $input, $cookie, $template->output;