From b755e657e5d755283c6d47f94514bf4a94cde7ad Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 24 Sep 2015 15:01:31 +0100 Subject: [PATCH] Bug 14888: use Koha::Cit[y|ies] in admin/itemtypes MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Test plan: Add/edit/remove cities from the administration module (admin/cities.pl). You should get message feedback after each action. Tested both patches together, works as expected. Signed-off-by: Marc Véron Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- admin/cities.pl | 164 ++++++------ .../prog/en/modules/admin/cities.tt | 233 ++++++++++-------- 2 files changed, 215 insertions(+), 182 deletions(-) diff --git a/admin/cities.pl b/admin/cities.pl index 5fe384727a..306927f65b 100755 --- a/admin/cities.pl +++ b/admin/cities.pl @@ -17,97 +17,99 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . -use strict; -use warnings; +use Modern::Perl; use CGI qw ( -utf8 ); use C4::Context; use C4::Auth; use C4::Output; -sub StringSearch { - my $sth = C4::Context->dbh->prepare("SELECT * FROM cities WHERE (city_name LIKE ?)"); - $sth->execute("%" . (shift || '') . "%"); - return $sth->fetchall_arrayref({}); -} +use Koha::Cities; -my $input = new CGI; -my $script_name = "/cgi-bin/koha/admin/cities.pl"; -my $searchfield = $input->param('city_name'); +my $input = new CGI; +my $searchfield = $input->param('city_name') // q||; my $cityid = $input->param('cityid'); -my $op = $input->param('op') || ''; - -my ($template, $loggedinuser, $cookie) - = get_template_and_user({template_name => "admin/cities.tt", - query => $input, - type => "intranet", - authnotrequired => 0, - flagsrequired => {parameters => 'parameters_remaining_permissions'}, - debug => 1, - }); +my $op = $input->param('op') || 'list'; +my @messages; -$template->param( script_name => $script_name, - cityid => $cityid , - searchfield => $searchfield); +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { template_name => "admin/cities.tt", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { parameters => 'parameters_remaining_permissions' }, + debug => 1, + } +); my $dbh = C4::Context->dbh; -################## ADD_FORM ################################## -# called by default. Used to create form to add or modify a record -if ($op eq 'add_form') { - $template->param(add_form => 1); - - #---- if primkey exists, it's a modify action, so read values to modify... - my $data; - if ($cityid) { - my $sth=$dbh->prepare("select cityid,city_name,city_state,city_zipcode,city_country from cities where cityid=?"); - $sth->execute($cityid); - $data=$sth->fetchrow_hashref; - } +if ( $op eq 'add_form' ) { + my $city; + if ($cityid) { + $city = Koha::Cities->find($cityid); + } + + $template->param( city => $city, ); +} elsif ( $op eq 'add_validate' ) { + my $cityid = $input->param('cityid'); + my $city_name = $input->param('city_name'); + my $city_state = $input->param('city_state'); + my $city_zipcode = $input->param('city_zipcode'); + my $city_country = $input->param('city_country'); + + if ($cityid) { + my $city = Koha::Cities->find($cityid); + $city->city_name($city_name); + $city->city_state($city_state); + $city->city_zipcode($city_zipcode); + $city->city_country($city_country); + eval { $city->store; }; + if ($@) { + push @messages, { type => 'error', code => 'error_on_update' }; + } else { + push @messages, { type => 'message', code => 'success_on_update' }; + } + } else { + my $city = Koha::City->new( + { city_name => $city_name, + city_state => $city_state, + city_zipcode => $city_zipcode, + city_country => $city_country, + } + ); + eval { $city->store; }; + if ($@) { + push @messages, { type => 'error', code => 'error_on_insert' }; + } else { + push @messages, { type => 'message', code => 'success_on_insert' }; + } + } + $searchfield = q||; + $op = 'list'; +} elsif ( $op eq 'delete_confirm' ) { + my $city = Koha::Cities->find($cityid); + $template->param( city => $city, ); +} elsif ( $op eq 'delete_confirmed' ) { + my $city = Koha::Cities->find($cityid); + my $deleted = eval { $city->delete; }; + + if ( $@ or not $deleted ) { + push @messages, { type => 'error', code => 'error_on_delete' }; + } else { + push @messages, { type => 'message', code => 'success_on_delete' }; + } + $op = 'list'; +} + +if ( $op eq 'list' ) { + my $cities = Koha::Cities->search( { city_name => { -like => "%$searchfield%" } } ); + $template->param( cities => $cities, ); +} - $template->param( - city_name => $data->{'city_name'}, - city_state => $data->{'city_state'}, - city_zipcode => $data->{'city_zipcode'}, - city_country => $data->{'city_country'}); -# END $OP eq ADD_FORM -################## ADD_VALIDATE ################################## -# called by add_form, used to insert/modify data in DB -} elsif ($op eq 'add_validate') { - my $sth; - - if ($input->param('cityid') ){ - $sth=$dbh->prepare("UPDATE cities SET city_name=?,city_state=?,city_zipcode=?,city_country=? WHERE cityid=?"); - $sth->execute($input->param('city_name'),$input->param('city_state'),$input->param('city_zipcode'),$input->param('city_country'),$input->param('cityid')); - } - else{ - $sth=$dbh->prepare("INSERT INTO cities (city_name,city_state,city_zipcode,city_country) values (?,?,?,?)"); - $sth->execute($input->param('city_name'),$input->param('city_state'),$input->param('city_zipcode'),$input->param('city_country')); - } - print $input->redirect($script_name); - exit; -################## DELETE_CONFIRM ################################## -# called by default form, used to confirm deletion of data in DB -} elsif ($op eq 'delete_confirm') { - $template->param(delete_confirm => 1); - my $sth=$dbh->prepare("select cityid,city_name,city_state,city_zipcode,city_country from cities where cityid=?"); - $sth->execute($cityid); - my $data=$sth->fetchrow_hashref; - $template->param( - city_name => $data->{'city_name'}, - city_state => $data->{'city_state'}, - city_zipcode => $data->{'city_zipcode'}, - city_country => $data->{'city_country'}, - ); -################## DELETE_CONFIRMED ################################## -# called by delete_confirm, used to effectively confirm deletion of data in DB -} elsif ($op eq 'delete_confirmed') { - my $sth=$dbh->prepare("delete from cities where cityid=?"); - $sth->execute($cityid); - print $input->redirect($script_name); - exit; # FIXME: what's the point of redirecting to this same page? - # END $OP eq DELETE_CONFIRMED -} else { # DEFAULT - $template->param(else => 1); - $template->param(loop => StringSearch($searchfield)); +$template->param( + cityid => $cityid, + searchfield => $searchfield, + messages => \@messages, + op => $op, +); -} #---- END $OP eq DEFAULT output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt index 34044832a5..a5262097d1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt @@ -1,5 +1,5 @@ [% INCLUDE 'doc-head-open.inc' %] -Koha › Administration › [% IF ( add_form ) %]Cities › [% IF ( cityid ) %] Modify city[% ELSE %] New city[% END %][% ELSE %][% IF ( delete_confirm ) %]Cities › Confirm deletion of city[% ELSE %] Cities[% END %][% END %] +Koha › Administration › [% IF op =='add_form' %]Cities › [% IF city.cityid %] Modify city[% ELSE %] New city[% END %][% ELSE %][% IF op == 'delete_confirm' %]Cities › Confirm deletion of city[% ELSE %] Cities[% END %][% END %] [% INCLUDE 'doc-head-close.inc' %] [% INCLUDE 'datatables.inc' %] @@ -26,123 +26,154 @@ HomeAdministrationCities - [% IF ( add_form ) %] - › [% IF ( cityid ) %]Modify[% ELSE %]New[% END %] City - [% ELSIF ( delete_confirm ) %] + [% IF op == 'add_form' %] + › [% IF city.cityid %]Modify[% ELSE %]New[% END %] City + [% ELSIF op == 'delete_confirm' %] › Confirm deletion of city [% END %]
- +
-
-
+
+
+ +[% FOR m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'error_on_update' %] + An error occurred when updating this city. Perhaps it already exists. + [% CASE 'error_on_insert' %] + An error occurred when inserting this city. The city id might already exist. + [% CASE 'error_on_delete' %] + An error occurred when deleting this city. Check the logs. + [% CASE 'success_on_update' %] + City updated successfully. + [% CASE 'success_on_insert' %] + City inserted successfully. + [% CASE 'success_on_delete' %] + City deleted successfully. + [% CASE 'already_exists' %] + This city already exists. + [% CASE %] + [% m.code %] + [% END %] +
+[% END %] -[% IF ( add_form ) %] - [% IF ( cityid ) %] -

Modify a city

- [% ELSE %] -

New city

- [% END %] +[% IF op == 'add_form' %] + [% IF city %] +

Modify a city

+ [% ELSE %] +

New city

+ [% END %] -
- - - + + + -
    [% IF ( cityid ) %] -
  1. - City ID: [% cityid %]
  2. - [% END %] -
  3. - - Required -
  4. -
  5. - - -
  6. -
  7. - - Required -
  8. -
  9. - - -
- -
- Cancel -
-
+
+
    + [% IF city %] +
  1. City ID: [% city.cityid %]
  2. + [% END %] +
  3. + + Required +
  4. +
  5. + + +
  6. +
  7. + + Required +
  8. +
  9. + + +
  10. +
+
+
+ + Cancel +
+ [% END %] -[% IF ( delete_confirm ) %] + +[% IF op == 'delete_confirm' %]
-

Delete City "[% city_name %]?"

- - - - - - - - - - - - - - - - -
City id[% cityid %]
City[% city_name %]
State[% city_state %]
Zip/Postal code[% city_zipcode %]
Country[% city_country %]
-
- - - -
-
- -
-
+

Delete City "[% city.city_name %]?"

+ + + + + + + + + + + + + + + + +
City id[% city.cityid %]
City[% city.city_name %]
State[% city.city_state %]
Zip/Postal code[% city.city_zipcode %]
Country[% city.city_country %]
+
+ + + +
+
+ +
+
[% END %] -[% IF ( else ) %] +[% IF op == 'list' %] - + -

Cities

- [% IF ( searchfield ) %] - Searching: [% searchfield %] - [% END %] +

Cities

+ [% IF searchfield %] + Searching: [% searchfield %] + [% END %] -[% IF ( loop ) %] - - - - - - - - - - - [% FOREACH loo IN loop %] - - - - - - - - - - [% END %] -
City IDCityStateZip/Postal codeCountry  
[% loo.cityid %][% loo.city_name %][% loo.city_state %][% loo.city_zipcode %][% loo.city_country %]EditDelete
[% END %] + [% IF cities %] + + + + + + + + + + + + [% FOREACH city IN cities %] + + + + + + + + + + [% END %] + +
City IDCityStateZip/Postal codeCountry  
[% city.cityid %][% city.city_name %][% city.city_state %][% city.city_zipcode %][% city.city_country %]EditDelete
+ [% ELSE %] + There is no city defined. Create a new city. + [% END %] [% END %]
-- 2.39.5