bugfix: prevent multiple addition of the same authorised value in a category
authorGalen Charlton <galen.charlton@liblime.com>
Thu, 8 Nov 2007 19:00:24 +0000 (13:00 -0600)
committerJoshua Ferraro <jmf@liblime.com>
Fri, 9 Nov 2007 00:02:39 +0000 (18:02 -0600)
Also put the prev/next page buttons in the same place relative to the
action links

Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
admin/authorised_values.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tmpl

index 8c1fd05..4d6eb17 100755 (executable)
@@ -26,14 +26,14 @@ use C4::Output;
 use C4::Context;
 
 
-sub StringSearch  {
+sub AuthorizedValuesForCategory  {
        my ($searchstring,$type)=@_;
        my $dbh = C4::Context->dbh;
        $searchstring=~ s/\'/\\\'/g;
        my @data=split(' ',$searchstring);
        my $count=@data;
-       my $sth=$dbh->prepare("Select id,category,authorised_value,lib from authorised_values where (category like ?) order by category,authorised_value");
-       $sth->execute("$data[0]%");
+       my $sth=$dbh->prepare("Select id,category,authorised_value,lib from authorised_values where (category = ?) order by category,authorised_value");
+       $sth->execute("$data[0]");
        my @results;
        my $cnt=0;
        while (my $data=$sth->fetchrow_hashref){
@@ -44,6 +44,18 @@ sub StringSearch  {
        return ($cnt,\@results);
 }
 
+sub _already_exists {
+    my ($category, $authorised_value) = @_;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare_cached("SELECT COUNT(*) FROM authorised_values
+                                    WHERE category = ?
+                                    AND authorised_value = ?");
+    $sth->execute($category, $authorised_value);
+    my ($count) = $sth->fetchrow_array();
+    $sth->finish();
+    return $count;
+}
+
 my $input = new CGI;
 my $searchfield=$input->param('searchfield');
 $searchfield=~ s/\,//g;
@@ -103,14 +115,22 @@ if ($op eq 'add_form') {
 # called by add_form, used to insert/modify data in DB
 } elsif ($op eq 'add_validate') {
        my $dbh = C4::Context->dbh;
-       my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
-       my $lib = $input->param('lib');
-       undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
+
+    if (_already_exists($input->param('category'), $input->param('authorised_value'))) {
+        $template->param(duplicate_category => $input->param('category'),
+                         duplicate_value =>  $input->param('authorised_value'),
+                         else => 1);
+        default_form();
+    } else {
+           my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
+           my $lib = $input->param('lib');
+           undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
        
-       $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $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;
+           $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $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;
+    }
 ################## DELETE_CONFIRM ##################################
 # called by default form, used to confirm deletion of data in DB
 } elsif ($op eq 'delete_confirm') {
@@ -141,6 +161,13 @@ if ($op eq 'add_form') {
                                                                                                        # END $OP eq DELETE_CONFIRMED
 ################## DEFAULT ##################################
 } else { # DEFAULT
+    default_form();
+} #---- END $OP eq DEFAULT
+output_html_with_http_headers $input, $cookie, $template->output;
+
+exit 0;
+
+sub default_form {
        # build categories list
        my $sth = $dbh->prepare("select distinct category from authorised_values");
        $sth->execute;
@@ -160,7 +187,7 @@ if ($op eq 'add_form') {
        if (!$searchfield) {
                $searchfield=$category_list[0];
        }
-       my ($count,$results)=StringSearch($searchfield,'web');
+       my ($count,$results)=AuthorizedValuesForCategory($searchfield,'web');
        my $toggle=1;
        my @loop_data = ();
        # builds value list
@@ -198,5 +225,5 @@ if ($op eq 'add_form') {
                                                script_name => $script_name,
                );
        }
-} #---- END $OP eq DEFAULT
-output_html_with_http_headers $input, $cookie, $template->output;
+}
+
index 8523c40..1350590 100644 (file)
 <p>This table is used in MARC definition. You can define as many categories as you want, and as many authorized values as you want in each category.</p>
 <p>When you define the MARC subfield structure, you can link a subfield to a authorized-value category. When the user ask for adding of modifying a biblio,
 the subfield is not entered through a free field, but though a list of authorized values</p>
+<!-- TMPL_IF name="duplicate_category" -->
+<div>
+<span class="problem">Could not add value &quot;<!-- TMPL_VAR name="duplicate_value" -->&quot; for category 
+    &quot;<!-- TMPL_VAR name="duplicate_category" -->&quot; &mdash; value already present.
+</span>
+</div>
+<!-- /TMPL_IF -->
 <form action="/cgi-bin/koha/admin/authorised_values.pl" method="post"><label for="searchfield">Show Category: </label><!-- TMPL_VAR name="tab_list" --> <input type="submit" value="Submit" /></form>
 <table>
 <tr>
@@ -135,6 +142,11 @@ the subfield is not entered through a free field, but though a list of authorize
        <input type="submit" value="Next Page &gt;&gt;" /></form>
 <!-- /TMPL_IF -->
 
+<div id="action">
+    <a href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;category=<!-- TMPL_VAR NAME="category" -->">Add Authorized value for <!-- TMPL_VAR name="category" --></a>
+    <a href= "/cgi-bin/koha/admin/authorised_values.pl?op=add_form">Add New Category</a>
+</div>
+
 <!-- /TMPL_IF -->
 
 </div>