From e76def3a946d39bc56737236554be6aa21c725a9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 10 Jul 2015 16:11:48 +0100 Subject: [PATCH] Bug 12965: Prevent to erase an existing item type MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit On creating an item type, if it already exists, it will replace the existing one. This patch prevent that and display a message to the interface. Note: The fields are lost. Test plan: 1/ Create an item type 'AAA', description 'AAA' 2/ Edit it, update the description with 'BBB' 3/ Create an item type 'AAA' with a description 'CCC' => you should get a warning "already exists". Works well, no errors Signed-off-by: Amit Gupta Signed-off-by: Joonas Kylmälä Signed-off-by: Katrin Fischer Warning message is triggered. Adding, editing and deleting item types still works. Signed-off-by: Tomas Cohen Arazi (cherry picked from commit eb12ee1e223b662cd1e89c0a56a5d1c083ba1ddc) Signed-off-by: Chris Cormack --- admin/itemtypes.pl | 38 +++++++++++-------- .../prog/en/modules/admin/itemtypes.tt | 16 ++++++++ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index 9effa7e607..3aa9c1d161 100755 --- a/admin/itemtypes.pl +++ b/admin/itemtypes.pl @@ -69,7 +69,8 @@ my $input = new CGI; my $searchfield = $input->param('description'); my $script_name = "/cgi-bin/koha/admin/itemtypes.pl"; my $itemtype = $input->param('itemtype'); -my $op = $input->param('op'); +my $op = $input->param('op') // 'list'; +my @messages; $searchfield =~ s/\,//g; my ( $template, $borrowernumber, $cookie ) = get_template_and_user( { @@ -85,8 +86,6 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( $template->param(script_name => $script_name); if ($op) { $template->param($op => 1); # we show only the TMPL_VAR names $op -} else { - $template->param(else => 1); } my $dbh = C4::Context->dbh; @@ -132,14 +131,13 @@ if ( $op eq 'add_form' ) { # called by add_form, used to insert/modify data in DB } elsif ( $op eq 'add_validate' ) { - my $query = " + my $is_a_modif = $input->param('is_a_modif'); + my ( $already_exists ) = $dbh->selectrow_array(q| SELECT itemtype FROM itemtypes WHERE itemtype = ? - "; - my $sth = $dbh->prepare($query); - $sth->execute($itemtype); - if ( $sth->fetchrow ) { # it's a modification + |, undef, $itemtype ); + if ( $already_exists and $is_a_modif ) { # it's a modification my $query2 = ' UPDATE itemtypes SET description = ? @@ -152,7 +150,7 @@ elsif ( $op eq 'add_validate' ) { , sip_media_type = ? WHERE itemtype = ? '; - $sth = $dbh->prepare($query2); + my $sth = $dbh->prepare($query2); $sth->execute( $input->param('description'), $input->param('rentalcharge'), @@ -171,7 +169,7 @@ elsif ( $op eq 'add_validate' ) { $input->param('itemtype') ); } - else { # add a new itemtype & not modif an old + elsif ( not $already_exists and not $is_a_modif ) { my $query = " INSERT INTO itemtypes (itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type) @@ -194,10 +192,15 @@ elsif ( $op eq 'add_validate' ) { $sip_media_type, ); } + else { + push @messages, { + type => 'error', + code => 'already_exists', + }; + } - print $input->redirect('itemtypes.pl'); - exit; - + $searchfield = ''; + $op = 'list'; # END $OP eq ADD_VALIDATE ################## DELETE_CONFIRM ################################## # called by default form, used to confirm deletion of data in DB @@ -244,7 +247,8 @@ elsif ( $op eq 'delete_confirmed' ) { # END $OP eq DELETE_CONFIRMED ################## DEFAULT ################################## } -else { # DEFAULT + +if ( $op eq 'list' ) { my ($results) = StringSearch( $searchfield, 'web' ); my @loop; foreach my $itemtype ( @{$results} ) { @@ -254,8 +258,10 @@ else { # DEFAULT } $template->param( - loop => \@loop, + loop => \@loop, + else => 1, + messages => \@messages, ); -} #---- END $OP eq DEFAULT +} output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt index 2e61a16d32..a3d8af4c11 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt @@ -85,6 +85,21 @@ Item types administration New item type [% END %] +[% FOREACH message IN messages %] + [% IF message.type == 'success' %] +
+ [% ELSIF message.type == 'warning' %] +
+ [% ELSIF message.type == 'error' %] +
+ [% END %] + [% IF message.code == 'already_exists' %] + This item type already exists. + [% END %] +
+[% END %] + + [% IF ( add_form ) %] [% IF ( itemtype ) %]

Modify item type

@@ -99,6 +114,7 @@ Item types administration
    [% IF ( itemtype ) %]
  1. + Item type: [% itemtype %]
  2. -- 2.39.5