[Koha-patches] [PATCH] Fix for bug 2885, now an error message is thrown instead of a silent fail

Chris Cormack chris at bigballofwax.co.nz
Sat Dec 26 10:23:05 CET 2009


---
 C4/Branch.pm                                       |   47 +++++++++++++++-----
 admin/branches.pl                                  |    9 ++++
 .../prog/en/modules/admin/branches.tmpl            |    2 +-
 3 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/C4/Branch.pm b/C4/Branch.pm
index 34f716a..a6b1a3a 100644
--- a/C4/Branch.pm
+++ b/C4/Branch.pm
@@ -44,6 +44,7 @@ BEGIN {
 		&ModBranchCategoryInfo
 		&DelBranch
 		&DelBranchCategory
+	        &CheckCategoryUnique
 	);
 	@EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
 }
@@ -526,20 +527,42 @@ sets the data from the editbranch form, and writes to the database...
 sub ModBranchCategoryInfo {
     my ($data) = @_;
     my $dbh    = C4::Context->dbh;
-	if ($data->{'add'}){
-		# we are doing an insert
-		my $sth   = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
-		$sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
-		$sth->finish();		
-	}
-	else {
-		# modifying
-		my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
-		$sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
-		$sth->finish();
-	}
+    if ($data->{'add'}){
+	# we are doing an insert
+	my $sth   = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
+	$sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
+	$sth->finish();		
+    }
+    else {
+	# modifying
+	my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
+	$sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
+	$sth->finish();
+    }
+}
+
+=head2 CheckCategoryUnique
+
+if (CheckCategoryUnique($categorycode)){
+  # do something
 }
 
+=cut
+
+sub CheckCategoryUnique {
+    my $categorycode = shift;
+    my $dbh    = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
+    $sth->execute(uc( $categorycode) );
+    if (my $data = $sth->fetchrow_hashref){
+	return 0;
+    }
+    else {
+	return 1;
+    }
+}
+
+    
 =head2 DeleteBranchCategory
 
 DeleteBranchCategory($categorycode);
diff --git a/admin/branches.pl b/admin/branches.pl
index 744ccfb..1d6b188 100755
--- a/admin/branches.pl
+++ b/admin/branches.pl
@@ -151,6 +151,15 @@ elsif ( $op eq 'addcategory_validate' ) {
     unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
         default("MESSAGE4",$template);
     }
+    elsif ($input->param('add')){
+	# doing an add must check the code is unique
+	if (CheckCategoryUnique($input->param('categorycode'))){
+	    ModBranchCategoryInfo($params);
+	}
+	else {
+	    default("MESSAGE9",$template);
+	}
+    }
     else {
         ModBranchCategoryInfo($params);
         default("MESSAGE5",$template);
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tmpl
index de96c18..e05024e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tmpl
@@ -158,7 +158,7 @@
     <!-- TMPL_IF name="MESSAGE6" --><div class="dialog message">Library category deleted</div><!-- /TMPL_IF -->
     <!-- TMPL_IF name="MESSAGE7" --><div class="dialog message">Library cannot be deleted because there are patrons or items using that library</div><!-- /TMPL_IF -->
     <!-- TMPL_IF name="MESSAGE8" --><div class="dialog message">Category cannot be deleted because there are libraries using that category</div><!-- /TMPL_IF -->
-
+    <!-- TMPL_IF name="MESSAGE9" --><div class="dialog message">Category cannot be added, categorycode already exists</div><!-- /TMPL_IF -->
 <!-- TMPL_IF NAME="branches" -->
     <table id="branchest">
 <thead><tr>
-- 
1.6.3.3




More information about the Koha-patches mailing list