View | Details | Raw Unified | Return to bug 14888
Collapse All | Expand All

(-)a/admin/cities.pl (-81 / +83 lines)
Lines 17-113 Link Here
17
# You should have received a copy of the GNU General Public License
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use strict;
20
use Modern::Perl;
21
use warnings;
22
use CGI qw ( -utf8 );
21
use CGI qw ( -utf8 );
23
use C4::Context;
22
use C4::Context;
24
use C4::Auth;
23
use C4::Auth;
25
use C4::Output;
24
use C4::Output;
26
25
27
sub StringSearch {
26
use Koha::Cities;
28
	my $sth = C4::Context->dbh->prepare("SELECT * FROM cities WHERE (city_name LIKE ?)");
29
	$sth->execute("%" . (shift || '') . "%");
30
    return $sth->fetchall_arrayref({});
31
}
32
27
33
my $input = new CGI;
28
my $input       = new CGI;
34
my $script_name = "/cgi-bin/koha/admin/cities.pl";
29
my $searchfield = $input->param('city_name') // q||;
35
my $searchfield = $input->param('city_name');
36
my $cityid      = $input->param('cityid');
30
my $cityid      = $input->param('cityid');
37
my $op          = $input->param('op') || '';
31
my $op          = $input->param('op') || 'list';
38
32
my @messages;
39
my ($template, $loggedinuser, $cookie)
40
    = get_template_and_user({template_name => "admin/cities.tt",
41
			     query => $input,
42
			     type => "intranet",
43
			     authnotrequired => 0,
44
                 flagsrequired => {parameters => 'parameters_remaining_permissions'},
45
			     debug => 1,
46
			     });
47
33
48
$template->param(	script_name => $script_name,
34
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49
		 	cityid     => $cityid ,
35
    {   template_name   => "admin/cities.tt",
50
		 	searchfield => $searchfield);
36
        query           => $input,
37
        type            => "intranet",
38
        authnotrequired => 0,
39
        flagsrequired   => { parameters => 'parameters_remaining_permissions' },
40
        debug           => 1,
41
    }
42
);
51
43
52
my $dbh = C4::Context->dbh;
44
my $dbh = C4::Context->dbh;
53
################## ADD_FORM ##################################
45
if ( $op eq 'add_form' ) {
54
# called by default. Used to create form to add or  modify a record
46
    my $city;
55
if ($op eq 'add_form') {
47
    if ($cityid) {
56
	$template->param(add_form => 1);
48
        $city = Koha::Cities->find($cityid);
57
	
49
    }
58
	#---- if primkey exists, it's a modify action, so read values to modify...
50
59
	my $data;
51
    $template->param( city => $city, );
60
	if ($cityid) {
52
} elsif ( $op eq 'add_validate' ) {
61
		my $sth=$dbh->prepare("select cityid,city_name,city_state,city_zipcode,city_country from cities where  cityid=?");
53
    my $cityid       = $input->param('cityid');
62
		$sth->execute($cityid);
54
    my $city_name    = $input->param('city_name');
63
		$data=$sth->fetchrow_hashref;
55
    my $city_state   = $input->param('city_state');
64
	}
56
    my $city_zipcode = $input->param('city_zipcode');
57
    my $city_country = $input->param('city_country');
58
59
    if ($cityid) {
60
        my $city = Koha::Cities->find($cityid);
61
        $city->city_name($city_name);
62
        $city->city_state($city_state);
63
        $city->city_zipcode($city_zipcode);
64
        $city->city_country($city_country);
65
        eval { $city->store; };
66
        if ($@) {
67
            push @messages, { type => 'error', code => 'error_on_update' };
68
        } else {
69
            push @messages, { type => 'message', code => 'success_on_update' };
70
        }
71
    } else {
72
        my $city = Koha::City->new(
73
            {   city_name    => $city_name,
74
                city_state   => $city_state,
75
                city_zipcode => $city_zipcode,
76
                city_country => $city_country,
77
            }
78
        );
79
        eval { $city->store; };
80
        if ($@) {
81
            push @messages, { type => 'error', code => 'error_on_insert' };
82
        } else {
83
            push @messages, { type => 'message', code => 'success_on_insert' };
84
        }
85
    }
86
    $searchfield = q||;
87
    $op          = 'list';
88
} elsif ( $op eq 'delete_confirm' ) {
89
    my $city = Koha::Cities->find($cityid);
90
    $template->param( city => $city, );
91
} elsif ( $op eq 'delete_confirmed' ) {
92
    my $city = Koha::Cities->find($cityid);
93
    my $deleted = eval { $city->delete; };
94
95
    if ( $@ or not $deleted ) {
96
        push @messages, { type => 'error', code => 'error_on_delete' };
97
    } else {
98
        push @messages, { type => 'message', code => 'success_on_delete' };
99
    }
100
    $op = 'list';
101
}
102
103
if ( $op eq 'list' ) {
104
    my $cities = Koha::Cities->search( { city_name => { -like => "%$searchfield%" } } );
105
    $template->param( cities => $cities, );
106
}
65
107
66
	$template->param(	
108
$template->param(
67
				city_name       => $data->{'city_name'},
109
    cityid      => $cityid,
68
				city_state      => $data->{'city_state'},
110
    searchfield => $searchfield,
69
				city_zipcode    => $data->{'city_zipcode'},
111
    messages    => \@messages,
70
				city_country    => $data->{'city_country'});
112
    op          => $op,
71
# END $OP eq ADD_FORM
113
);
72
################## ADD_VALIDATE ##################################
73
# called by add_form, used to insert/modify data in DB
74
} elsif ($op eq 'add_validate') {
75
 	my $sth;
76
	
77
	if ($input->param('cityid') ){
78
		$sth=$dbh->prepare("UPDATE cities SET city_name=?,city_state=?,city_zipcode=?,city_country=? WHERE cityid=?");
79
		$sth->execute($input->param('city_name'),$input->param('city_state'),$input->param('city_zipcode'),$input->param('city_country'),$input->param('cityid'));
80
	}
81
	else{	
82
		$sth=$dbh->prepare("INSERT INTO cities (city_name,city_state,city_zipcode,city_country) values (?,?,?,?)");
83
		$sth->execute($input->param('city_name'),$input->param('city_state'),$input->param('city_zipcode'),$input->param('city_country'));
84
	}
85
	print $input->redirect($script_name);
86
	exit;
87
################## DELETE_CONFIRM ##################################
88
# called by default form, used to confirm deletion of data in DB
89
} elsif ($op eq 'delete_confirm') {
90
	$template->param(delete_confirm => 1);
91
	my $sth=$dbh->prepare("select cityid,city_name,city_state,city_zipcode,city_country from cities where  cityid=?");
92
	$sth->execute($cityid);
93
	my $data=$sth->fetchrow_hashref;
94
    $template->param(
95
        city_name    =>	$data->{'city_name'},
96
        city_state   =>	$data->{'city_state'},
97
        city_zipcode => $data->{'city_zipcode'},
98
        city_country => $data->{'city_country'},
99
    );
100
################## DELETE_CONFIRMED ##################################
101
# called by delete_confirm, used to effectively confirm deletion of data in DB
102
} elsif ($op eq 'delete_confirmed') {
103
	my $sth=$dbh->prepare("delete from cities where cityid=?");
104
	$sth->execute($cityid);
105
	print $input->redirect($script_name);
106
	exit;   # FIXME: what's the point of redirecting to this same page?
107
													# END $OP eq DELETE_CONFIRMED
108
} else { # DEFAULT
109
	$template->param(else => 1);
110
	$template->param(loop => StringSearch($searchfield));
111
114
112
} #---- END $OP eq DEFAULT
113
output_html_with_http_headers $input, $cookie, $template->output;
115
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt (-102 / +132 lines)
Lines 1-5 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
1
[% INCLUDE 'doc-head-open.inc' %]
2
<title>Koha &rsaquo; Administration &rsaquo; [% IF ( add_form ) %]Cities &rsaquo; [% IF ( cityid ) %] Modify city[% ELSE %] New city[% END %][% ELSE %][% IF ( delete_confirm ) %]Cities &rsaquo; Confirm deletion of city[% ELSE %] Cities[% END %][% END %]</title>
2
<title>Koha &rsaquo; Administration &rsaquo; [% IF op =='add_form' %]Cities &rsaquo; [% IF city.cityid %] Modify city[% ELSE %] New city[% END %][% ELSE %][% IF op == 'delete_confirm' %]Cities &rsaquo; Confirm deletion of city[% ELSE %] Cities[% END %][% END %]</title>
3
[% INCLUDE 'doc-head-close.inc' %]
3
[% INCLUDE 'doc-head-close.inc' %]
4
<link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
4
<link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
5
[% INCLUDE 'datatables.inc' %]
5
[% INCLUDE 'datatables.inc' %]
Lines 26-148 Link Here
26
    <a href="/cgi-bin/koha/mainpage.pl">Home</a>
26
    <a href="/cgi-bin/koha/mainpage.pl">Home</a>
27
    &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
27
    &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
28
    &rsaquo; <a href="/cgi-bin/koha/admin/cities.pl">Cities</a>
28
    &rsaquo; <a href="/cgi-bin/koha/admin/cities.pl">Cities</a>
29
    [% IF ( add_form ) %]
29
    [% IF op == 'add_form' %]
30
    &rsaquo; [% IF ( cityid ) %]Modify[% ELSE %]New[% END %] City
30
    &rsaquo; [% IF city.cityid %]Modify[% ELSE %]New[% END %] City
31
    [% ELSIF ( delete_confirm ) %]
31
    [% ELSIF op == 'delete_confirm' %]
32
    &rsaquo; Confirm deletion of city
32
    &rsaquo; Confirm deletion of city
33
    [% END %]
33
    [% END %]
34
</div>
34
</div>
35
35
36
<div id="doc3" class="yui-t2">
36
<div id="doc3" class="yui-t2">
37
   
37
38
   <div id="bd">
38
   <div id="bd">
39
	<div id="yui-main">
39
    <div id="yui-main">
40
	<div class="yui-b">
40
    <div class="yui-b">
41
42
[% FOR m IN messages %]
43
    <div class="dialog [% m.type %]">
44
        [% SWITCH m.code %]
45
        [% CASE 'error_on_update' %]
46
            An error occurred when updating this city. Perhaps it already exists.
47
        [% CASE 'error_on_insert' %]
48
            An error occurred when inserting this city. The city id might already exist.
49
        [% CASE 'error_on_delete' %]
50
            An error occurred when deleting this city. Check the logs.
51
        [% CASE 'success_on_update' %]
52
            City updated successfully.
53
        [% CASE 'success_on_insert' %]
54
            City inserted successfully.
55
        [% CASE 'success_on_delete' %]
56
            City deleted successfully.
57
        [% CASE 'already_exists' %]
58
            This city already exists.
59
        [% CASE %]
60
            [% m.code %]
61
        [% END %]
62
    </div>
63
[% END %]
41
64
42
[% IF ( add_form ) %]
65
[% IF op == 'add_form' %]
43
	[% IF ( cityid ) %]
66
    [% IF city %]
44
		<h1>Modify a city</h1>
67
        <h1>Modify a city</h1>
45
	[% ELSE %]
68
    [% ELSE %]
46
		<h1>New city</h1>
69
        <h1>New city</h1>
47
	[% END %]
70
    [% END %]
48
71
49
    <form action="[% script_name %]" name="Aform" method="post" class="validated">
72
    <form action="/cgi-bin/koha/admin/cities.pl" name="Aform" method="post" class="validated">
50
	<input type="hidden" name="op" value="add_validate" />
73
        <input type="hidden" name="op" value="add_validate" />
51
	<input type="hidden" name="checked" value="0" />
74
        <input type="hidden" name="cityid" value="[% city.cityid %]" />
52
	<input type="hidden" name="cityid" value="[% cityid %]" />
53
75
54
<fieldset class="rows"><ol>	[% IF ( cityid ) %]
76
        <fieldset class="rows">
55
		<li>
77
            <ol>
56
		<span class="label">City ID: </span>[% cityid %]</li>
78
                [% IF city %]
57
	[% END %]
79
                    <li><span class="label">City ID: </span>[% city.cityid %]</li>
58
	<li>
80
                [% END %]
59
    <label for="city_name" class="required">City: </label>
81
                <li>
60
    <input type="text" name="city_name" id="city_name" size="80" maxlength="100" value="[% city_name |html %]" required="required" class="required" /> <span class="required">Required</span>
82
                    <label for="city_name" class="required">City: </label>
61
	</li>
83
                    <input type="text" name="city_name" id="city_name" size="80" maxlength="100" value="[% city.city_name |html %]" required="required" class="required" /> <span class="required">Required</span>
62
	<li>
84
                </li>
63
	<label for="city_state">State: </label>
85
                <li>
64
	<input type="text" name="city_state" id="city_state" size="80" maxlength="100" value="[% city_state |html %]" />
86
                    <label for="city_state">State: </label>
65
	</li>
87
                    <input type="text" name="city_state" id="city_state" size="80" maxlength="100" value="[% city.city_state |html %]" />
66
	<li>				
88
                </li>
67
    <label for="city_zipcode" class="required">Zip/Postal code: </label>
89
                <li>
68
    <input type="text" name="city_zipcode" id="city_zipcode" size="20" maxlength="20" value="[% city_zipcode %]" required="required" class="required" /> <span class="required">Required</span>
90
                    <label for="city_zipcode" class="required">Zip/Postal code: </label>
69
	</li>
91
                    <input type="text" name="city_zipcode" id="city_zipcode" size="20" maxlength="20" value="[% city.city_zipcode %]" required="required" class="required" /> <span class="required">Required</span>
70
	<li>
92
                </li>
71
	<label for="city_country">Country: </label>
93
                <li>
72
	<input type="text" name="city_country" id="city_country" size="80" maxlength="100" value="[% city_country |html %]" />
94
                    <label for="city_country">Country: </label>
73
	</li></ol></fieldset>
95
                    <input type="text" name="city_country" id="city_country" size="80" maxlength="100" value="[% city.city_country |html %]" />
74
	
96
                </li>
75
	<fieldset class="action">
97
            </ol>
76
        <input type="submit" value="Submit" /> <a class="cancel" href="/cgi-bin/koha/admin/cities.pl">Cancel</a>
98
        </fieldset>
77
	</fieldset>
78
	</form>
79
99
100
        <fieldset class="action">
101
            <input type="submit" value="Submit" />
102
            <a class="cancel" href="/cgi-bin/koha/admin/cities.pl">Cancel</a>
103
        </fieldset>
104
    </form>
80
[% END %]
105
[% END %]
81
[% IF ( delete_confirm ) %]
106
107
[% IF op == 'delete_confirm' %]
82
    <div class="dialog alert">
108
    <div class="dialog alert">
83
    <h3>Delete City "[% city_name %]?"</h3>
109
        <h3>Delete City "[% city.city_name %]?"</h3>
84
    <table>
110
        <table>
85
        <tr><th>City id</th>
111
            <tr><th>City id</th>
86
            <td>[% cityid %]</td>
112
                <td>[% city.cityid %]</td>
87
        </tr>
113
            </tr>
88
        <tr><th>City</th>
114
            <tr><th>City</th>
89
            <td>[% city_name %]</td>
115
                <td>[% city.city_name %]</td>
90
        </tr>
116
            </tr>
91
        <tr><th>State</th>
117
            <tr><th>State</th>
92
            <td>[% city_state %]</td>
118
                <td>[% city.city_state %]</td>
93
        </tr>
119
            </tr>
94
        <tr><th>Zip/Postal code</th>
120
            <tr><th>Zip/Postal code</th>
95
            <td>[% city_zipcode %]</td>
121
                <td>[% city.city_zipcode %]</td>
96
        </tr>
122
            </tr>
97
        <tr><th>Country</th>
123
            <tr><th>Country</th>
98
            <td>[% city_country %]</td>
124
                <td>[% city.city_country %]</td>
99
        </tr>
125
            </tr>
100
    </table>
126
        </table>
101
    <form action="[% script_name %]" method="post">
127
        <form action="/cgi-bin/koha/admin/cities.pl" method="post">
102
        <input type="hidden" name="op" value="delete_confirmed" />
128
            <input type="hidden" name="op" value="delete_confirmed" />
103
        <input type="hidden" name="cityid" value="[% cityid %]" />
129
            <input type="hidden" name="cityid" value="[% city.cityid %]" />
104
        <input type="submit" class="approve" value="Yes, delete" />
130
            <input type="submit" class="approve" value="Yes, delete" />
105
    </form>
131
        </form>
106
    <form action="[% script_name %]" method="get">
132
        <form action="/cgi-bin/koha/admin/cities.pl" method="get">
107
        <input type="submit" class="deny" value="No, do not Delete" />
133
            <input type="submit" class="deny" value="No, do not Delete" />
108
    </form>
134
        </form>
109
</div>
135
    </div>
110
[% END %]
136
[% END %]
111
137
112
[% IF ( else ) %]
138
[% IF op == 'list' %]
113
139
114
<div id="toolbar" class="btn-toolbar">
140
    <div id="toolbar" class="btn-toolbar">
115
    <a class="btn btn-small" id="newcity" href="[% script_name %]?op=add_form"><i class="icon-plus"></i> New city</a>
141
        <a class="btn btn-small" id="newcity" href="/cgi-bin/koha/admin/cities.pl?op=add_form"><i class="icon-plus"></i> New city</a>
116
</div>
142
    </div>
117
143
118
	<h2>Cities</h2>
144
    <h2>Cities</h2>
119
	[% IF ( searchfield ) %]
145
    [% IF searchfield %]
120
		Searching: [% searchfield %]
146
        Searching: [% searchfield %]
121
	[% END %]
147
    [% END %]
122
148
123
[% IF ( loop ) %]
149
    [% IF cities %]
124
<table id="table_cities">
150
        <table id="table_cities">
125
		<thead>
151
            <thead>
126
			<th>City ID</th>
152
                <th>City ID</th>
127
			<th>City</th>
153
                <th>City</th>
128
			<th>State</th>
154
                <th>State</th>
129
			<th>Zip/Postal code</th>
155
                <th>Zip/Postal code</th>
130
			<th>Country</th>
156
                <th>Country</th>
131
			<th>&nbsp;</th>
157
                <th>&nbsp;</th>
132
			<th>&nbsp;</th>
158
                <th>&nbsp;</th>
133
		</thead>
159
            </thead>
134
		[% FOREACH loo IN loop %]
160
            <tbody>
135
        <tr>
161
                [% FOREACH city IN cities %]
136
			<td>[% loo.cityid %]</td>
162
                <tr>
137
			<td>[% loo.city_name %]</td>
163
                    <td>[% city.cityid %]</td>
138
			<td>[% loo.city_state %]</td>
164
                    <td>[% city.city_name %]</td>
139
			<td>[% loo.city_zipcode %]</td>
165
                    <td>[% city.city_state %]</td>
140
			<td>[% loo.city_country %]</td>
166
                    <td>[% city.city_zipcode %]</td>
141
			<td><a href="[% loo.script_name %]?op=add_form&amp;cityid=[% loo.cityid %]">Edit</a></td>
167
                    <td>[% city.city_country %]</td>
142
			<td><a href="[% loo.script_name %]?op=delete_confirm&amp;cityid=[% loo.cityid %]">Delete</a></td>
168
                    <td><a href="/cgi-bin/koha/admin/cities.pl?op=add_form&amp;cityid=[% city.cityid %]">Edit</a></td>
143
		</tr>
169
                    <td><a href="/cgi-bin/koha/admin/cities.pl?op=delete_confirm&amp;cityid=[% city.cityid %]">Delete</a></td>
144
		[% END %]
170
                </tr>
145
	</table>[% END %]
171
                [% END %]
172
            </tbody>
173
        </table>
174
    [% ELSE %]
175
        There is no city defined. <a href="/cgi-bin/koha/admin/cities.pl?op=add_form">Create a new city</a>.
176
    [% END %]
146
[% END %]
177
[% END %]
147
178
148
</div>
179
</div>
149
- 

Return to bug 14888