-################## 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_zipcode 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 $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' };
+ }
+ }
+ $city_name = 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' ) {
+ $template->param( cities_count => Koha::Cities->search->count );
+}