Bug 12965: Prevent to erase an existing item type
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 10 Jul 2015 15:11:48 +0000 (16:11 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Wed, 2 Sep 2015 17:39:11 +0000 (14:39 -0300)
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 <amit.gupta@informaticsglobal.com>
Signed-off-by: Joonas Kylmälä <j.kylmala@gmail.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Warning message is triggered.
Adding, editing and deleting item types still works.
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
admin/itemtypes.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt

index 9effa7e..3aa9c1d 100755 (executable)
@@ -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;
index 2e61a16..a3d8af4 100644 (file)
@@ -85,6 +85,21 @@ Item types administration
     <a class="btn btn-small" id="newitemtype" href="/cgi-bin/koha/admin/itemtypes.pl?op=add_form"><i class="icon-plus"></i> New item type</a>
 </div>[% END %]
 
+[% FOREACH message IN messages %]
+  [% IF message.type == 'success' %]
+    <div class="dialog message">
+  [% ELSIF message.type == 'warning' %]
+    <div class="dialog alert">
+  [% ELSIF message.type == 'error' %]
+    <div class="dialog error" style="margin:auto;">
+  [% END %]
+  [% IF message.code == 'already_exists' %]
+    This item type already exists.
+  [% END %]
+  </div>
+[% END %]
+
+
 [% IF ( add_form ) %]
   [% IF ( itemtype ) %]
       <h3>Modify item type</h3>
@@ -99,6 +114,7 @@ Item types administration
        <ol>
   [% IF ( itemtype ) %]
       <li>
+          <input type="hidden" name="is_a_modif" value="1" />
           <span class="label">Item type: </span> <input type="hidden" name="itemtype" value="[% itemtype %]" />
           [% itemtype %]
      </li>