- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
- $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'),$input->param('lib'));
- $sth->finish;
- print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
- exit;
+ 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='$id' ");
+ $sth->execute();
+ 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 = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
+ $sth->execute();
+ ($duplicate_entry) = $sth->fetchrow_array();
+ warn "**** duplicate_entry = $duplicate_entry";
+ }
+ unless ( $duplicate_entry ) {
+ my $sth=$dbh->prepare( 'UPDATE authorised_values
+ SET category = ?,
+ authorised_value = ?,
+ lib = ?,
+ imageurl = ?
+ WHERE id=?' );
+ my $lib = $input->param('lib');
+ undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
+ $sth->execute($new_category, $new_authorised_value, $lib, $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 = '$new_category' AND authorised_value = '$new_authorised_value' ");
+ $sth->execute();
+ ($duplicate_entry) = $sth->fetchrow_array();
+ unless ( $duplicate_entry ) {
+ my $sth=$dbh->prepare( 'INSERT INTO authorised_values
+ ( id, category, authorised_value, lib, imageurl )
+ values (?, ?, ?, ?, ?)' );
+ my $lib = $input->param('lib');
+ undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
+ $sth->execute($id, $new_category, $new_authorised_value, $lib, $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();
+ }
+