+ my $new_authorised_value = $input->param('authorised_value');
+ my $new_category = $input->param('category');
+ my $imageurl = $input->param( 'imageurl' ) || '';
+ $imageurl = '' if $imageurl =~ /removeImage/;
+ my $duplicate_entry = 0;
+
+ if ( $id ) { # Update
+ my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id = ? ");
+ $sth->execute($id);
+ my ($category, $authorised_value) = $sth->fetchrow_array();
+ if ( $authorised_value ne $new_authorised_value ) {
+ my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
+ "WHERE category = ? AND authorised_value = ? and id <> ? ");
+ $sth->execute($new_category, $new_authorised_value, $id);
+ ($duplicate_entry) = $sth->fetchrow_array();
+ }
+ unless ( $duplicate_entry ) {
+ my $sth=$dbh->prepare( 'UPDATE authorised_values
+ SET category = ?,
+ authorised_value = ?,
+ lib = ?,
+ lib_opac = ?,
+ imageurl = ?
+ WHERE id=?' );
+ my $lib = $input->param('lib');
+ my $lib_opac = $input->param('lib_opac');
+ undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
+ undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
+ $sth->execute($new_category, $new_authorised_value, $lib, $lib_opac, $imageurl, $id);
+ print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."&offset=$offset\"></html>";
+ exit;
+ }
+ }
+ else { # Insert
+ my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
+ "WHERE category = ? AND authorised_value = ? ");
+ $sth->execute($new_category, $new_authorised_value);
+ ($duplicate_entry) = $sth->fetchrow_array();
+ unless ( $duplicate_entry ) {
+ my $sth=$dbh->prepare( 'INSERT INTO authorised_values
+ ( id, category, authorised_value, lib, lib_opac, imageurl )
+ values (?, ?, ?, ?, ?, ?)' );
+ my $lib = $input->param('lib');
+ my $lib_opac = $input->param('lib_opac');
+ undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
+ undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
+ $sth->execute($id, $new_category, $new_authorised_value, $lib, $lib_opac, $imageurl );
+ print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."&offset=$offset\"></html>";
+ exit;
+ }
+ }
+ if ( $duplicate_entry ) {
+ $template->param(duplicate_category => $new_category,
+ duplicate_value => $new_authorised_value,
+ else => 1);
+ default_form();
+ }