Experimental rewrite of Stop Words administration screen. Feedback welcomed.
authorplg <plg>
Thu, 6 Apr 2006 11:00:08 +0000 (11:00 +0000)
committerplg <plg>
Thu, 6 Apr 2006 11:00:08 +0000 (11:00 +0000)
admin/stopwords.pl
koha-tmpl/intranet-tmpl/prog/en/admin/stopwords.tmpl
koha-tmpl/intranet-tmpl/prog/en/includes/intranet.css

index 5a1e50d..8d26716 100755 (executable)
@@ -1,27 +1,5 @@
 #!/usr/bin/perl
 
-#script to administer the stopwords table
-#written 20/02/2002 by paul.poulain@free.fr
-# This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
-
-# ALGO :
-# this script use an $op to know what to do.
-# if $op is empty or none of the above values,
-#      - the default screen is build (with all records, or filtered datas).
-#      - the   user can clic on add, modify or delete record.
-# if $op=add_form
-#      - if primkey exists, this is a modification,so we read the $primkey record
-#      - builds the add/modify form
-# if $op=add_validate
-#      - the user has just send datas, so we create/modify the record
-# if $op=delete_form
-#      - we show the record having primkey=$primkey and ask for deletion validation form
-# if $op=delete_confirm
-#      - we delete the record having primkey=$primkey
-
-
-# Copyright 2000-2002 Katipo Communications
-#
 # This file is part of Koha.
 #
 # Koha is free software; you can redistribute it and/or modify it under the
 # You should have received a copy of the GNU General Public License along with
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
+#
+###
+#
+# script to administer the stopwords table
+#
+# - written on 2002/02/20 by paul.poulain@free.fr
+#
+# - experimentaly rewrittten on 2006/04/06 by Pierrick LE GALL (INEO media
+#   system)
+#
 
 use strict;
 use CGI;
+use List::Util qw/min/;
+
 use C4::Context;
 use C4::Output;
 use C4::Search;
@@ -47,31 +37,37 @@ use C4::Auth;
 use C4::Interface::CGI::Output;
 
 sub StringSearch  {
-       my ($env,$searchstring,$type)=@_;
-       my $dbh = C4::Context->dbh;
-       $searchstring=~ s/\'/\\\'/g;
-       my @data=split(' ',$searchstring);
-       my $count=@data;
-       my $query="";
-       my $sth=$dbh->prepare("Select word from stopwords where (word like ?) order by word");
-       $sth->execute("$data[0]%");
-       my @results;
-       my $cnt=0;
-       while (my $data=$sth->fetchrow_hashref){
-       push(@results,$data);
-       $cnt ++;
-       }
-       #  $sth->execute;
-       $sth->finish;
-       return ($cnt,\@results);
+    my ($searchstring) = @_;
+
+    my $dbh = C4::Context->dbh;
+    $searchstring =~ s/\'/\\\'/g;
+    my @tokens = split(' ',$searchstring);
+
+    my $query = '
+SELECT word
+  FROM stopwords
+  WHERE (word like ?)
+  ORDER BY word
+';
+    my $sth = $dbh->prepare($query);
+    $sth->execute($tokens[0].'%');
+    my @results;
+    while (my $row = $sth->fetchrow_hashref) {
+        push(@results, $row->{word});
+    }
+    $sth->finish;
+
+    return @results;
 }
 
+my $dbh = C4::Context->dbh;
+my $sth;
+my $query;
 my $input = new CGI;
-my $searchfield=$input->param('searchfield');
-my $offset=$input->param('offset');
+my $searchfield = $input->param('searchfield');
 my $script_name="/cgi-bin/koha/admin/stopwords.pl";
 
-my $pagesize=20;
+my $pagesize = 40;
 my $op = $input->param('op');
 $searchfield=~ s/\,//g;
 
@@ -87,86 +83,88 @@ my ($template, $loggedinuser, $cookie)
 $template->param(script_name => $script_name,
                 searchfield => $searchfield);
 
+if ($input->param('add')) {
+    if ($input->param('word')) {
+        my @words = split / |,/, $input->param('word');
+
+        $query = '
+DELETE
+  FROM stopwords
+  WHERE word IN (?'.(',?' x scalar @words - 1).')
+';
+        $sth = $dbh->prepare($query);
+        $sth->execute(@words);
+        $sth->finish;
+
+        $query = '
+INSERT
+  INTO stopwords
+  (word)
+  VALUES
+  (?)'.(',(?)' x scalar @words - 1).'
+';
+        $sth = $dbh->prepare($query);
+        $sth->execute(@words);
+        $sth->finish;
+
+        $template->param(stopword_added => 1);
+    }
+}
+elsif ($input->param('deleteSelected')) {
+    if ($input->param('stopwords[]')) {
+        my @stopwords_loop = ();
+
+        foreach my $word ($input->param('stopwords[]')) {
+            push @stopwords_loop,  {word => $word};
+        }
+
+        $template->param(
+            delete_confirm => 1,
+            stopwords_to_delete => \@stopwords_loop,
+        );
+    }
+}
+elsif ($input->param('confirmDeletion')) {
+    my @words = $input->param('confirmed_stopwords[]');
+
+    $query = '
+DELETE
+  FROM stopwords
+  WHERE word IN (?'.(',?' x scalar @words - 1).')
+';
+    $sth = $dbh->prepare($query);
+    $sth->execute(@words);
+    $sth->finish;
+
+    $template->param(delete_confirmed => 1);
+}
+
+my $page = $input->param('page') || 1;
+
+my @results = StringSearch($searchfield);
+my @loop;
+
+my $first = ($page - 1) * $pagesize;
 
-################## ADD_FORM ##################################
-# called by default. Used to create form to add or  modify a record
-if ($op eq 'add_form') {
-       $template->param(add_form => 1);
-       #---- if primkey exists, it's a modify action, so read values to modify...
-       my $data;
-       if ($searchfield) {
-               my $dbh = C4::Context->dbh;
-               my $sth=$dbh->prepare("select word from stopwords where word=?");
-               $sth->execute($searchfield);
-               $data=$sth->fetchrow_hashref;
-               $sth->finish;
-       }
-
-                                                                                                       # END $OP eq ADD_FORM
-################## ADD_VALIDATE ##################################
-# called by add_form, used to insert/modify data in DB
-} elsif ($op eq 'add_validate') {
-       $template->param(add_validate => 1);
-       my $dbh = C4::Context->dbh;
-       my @tab = split / |,/, $input->param('word');
-       my $sth=$dbh->prepare("replace stopwords (word) values (?)");
-       foreach my $insert_value (@tab) {
-               $sth->execute($insert_value);
-       }
-       $sth->finish;
-                                                                                                       # END $OP eq ADD_VALIDATE
-################## DELETE_CONFIRM ##################################
-# called by default form, used to confirm deletion of data in DB
-} elsif ($op eq 'delete_confirm') {
-       $template->param(delete_confirm => 1);
-       my $dbh = C4::Context->dbh;
-       my $sth=$dbh->prepare("select word from stopwords where word=?");
-       $sth->execute($searchfield);
-       my $data=$sth->fetchrow_hashref;
-       $sth->finish;
-                                                                                                       # END $OP eq DELETE_CONFIRM
-################## DELETE_CONFIRMED ##################################
-# called by delete_confirm, used to effectively confirm deletion of data in DB
-} elsif ($op eq 'delete_confirmed') {
-       $template->param(delete_confirmed => 1);
-       my $dbh = C4::Context->dbh;
-       my $sth=$dbh->prepare("delete from stopwords where word=?");
-       $sth->execute($searchfield);
-       $sth->finish;
-                                                                                                       # END $OP eq DELETE_CONFIRMED
-################## DEFAULT ##################################
-} else { # DEFAULT
-       $template->param(else => 1);
-
-       my $env;
-       my ($count,$results)=StringSearch($env,$searchfield,'web');
-       my @loop;
-       my $toggle = 'white';
-       for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
-               my %row = (word => $results->[$i]{'word'},
-                          toggle => $toggle);
-               push @loop, \%row;
-
-                if ( $toggle eq 'white' )
-                {
-                        $toggle = '#ffffcc';
-                }
-                else
-                {
-                        $toggle = 'white';
-                }
-       }
-       $template->param(loop => \@loop);
-
-       if ($offset>0) {
-               $template->param(offsetgtzero => 1,
-                                prevpage => $offset-$pagesize);
-       }
-       if ($offset+$pagesize<$count) {
-               $template->param(ltcount => 1,
-                                nextpage => $offset+$pagesize);
-       }
+# if we are on the last page, the number of the last word to display must
+# not exceed the length of the results array
+my $last = min(
+    $first + $pagesize - 1,
+    scalar(@results) - 1,
+);
+
+foreach my $word (@results[$first .. $last]) {
+    push @loop, {word => $word};
 }
-                                                              
-output_html_with_http_headers $input, $cookie, $template->output;
 
+$template->param(
+    loop => \@loop,
+    pagination_bar => pagination_bar(
+        $script_name,
+        int(scalar(@results) / $pagesize) + 1,
+        $page,
+        'page'
+    )
+);
+
+output_html_with_http_headers $input, $cookie, $template->output;
index c20fc89..e998666 100644 (file)
-<!-- TMPL_INCLUDE NAME="doc-head-open.inc" -->Koha -- System Administration: 
-<!-- TMPL_IF NAME="else" -->Stop Words Administration<!-- /TMPL_IF -->
-<!-- TMPL_IF NAME="add_form" --><!-- TMPL_IF NAME="searchfield" -->Modify Stop Word<!-- TMPL_ELSE -->Add Stop Word<!-- /TMPL_IF --><!-- /TMPL_IF --> 
-<!-- TMPL_IF NAME="add_validate" -->Stop Word Added<!-- /TMPL_IF --> 
-<!-- TMPL_IF NAME="delete_confirm" -->Confirm Deletion of Stop Word '<!-- TMPL_VAR NAME="searchfield" -->'<!-- /TMPL_IF --> 
-<!-- TMPL_IF NAME="delete_confirmed" -->Stop Word Deleted<!-- /TMPL_IF --> 
+<!-- TMPL_INCLUDE NAME="doc-head-open.inc" -->
+Koha -- System Administration: Stop Words Administration
 <!-- TMPL_INCLUDE NAME="doc-head-close.inc" -->
 
 <!-- TMPL_INCLUDE NAME="menus.inc" -->
 <!-- TMPL_INCLUDE NAME="menu-admin.inc" -->
 
-<!-- TMPL_IF NAME="add_form" -->
-        <script>
-        //
-        function isNotNull(f,noalert) {
-                if (f.value.length ==0) {
-   return false;
-                }
-                return true;
-        }
-        //
-        function toUC(f) {
-                var x=f.value.toUpperCase();
-                f.value=x;
-                return true;
-        }
-        //
-        function isNum(v,maybenull) {
-        var n = new Number(v.value);
-        if (isNaN(n)) {
-                return false;
-                }
-        if (maybenull==0 && v.value=="") {
-                return false;
-        }
-        return true;
-        }
-        //
-        function isDate(f) {
-                var t = Date.parse(f.value);
-                if (isNaN(t)) {
-                        return false;
-                }
-        }
-        //
-        function Check(f) {
-                var ok=1;
-                var _alertString="";
-                var alertString2;
-                if (f.word.value.length==0) {
-                        _alertString += "- word missing\n";
-                }
-                if (_alertString.length==0) {
-                        document.Aform.submit();
-                } else {
-                        alertString2 = "Form not submitted because of the following problem(s)\n";
-                        alertString2 += "------------------------------------------------------------------------------------\n\n";
-                        alertString2 += _alertString;
-                        alert(alertString2);
-                }
-        }
-        </SCRIPT>
-
-
-
-        <form action="<!-- TMPL_VAR NAME=script_name -->" name="Aform" method="post">
-        <input type="hidden" name="op" value="add_validate" />
-<!-- TMPL_IF NAME="searchfield" -->
-                <h1>Modify Stop Word</h1>
-        <!-- TMPL_ELSE -->
-                <h1>Add Stop Word</h1>
-        <!-- /TMPL_IF -->         
-<table>
-        <!-- TMPL_IF NAME="searchfield" -->
-                <tr>
-                        <td>Stop Word</td>
-                        <td><input type="hidden" name="word" value="<!-- TMPL_VAR NAME=searchfield -->" /><!-- TMPL_VAR NAME="searchfield" -->
-                        </td>
-                </tr>
-        <!-- TMPL_ELSE -->
-                <tr>
-                        <td>Stop Word: </td>
-                        <td>
-                                <input type="text" name="word" size="50" maxlength="250" onblur="toUC(this);" />
-                        </td>
-                </tr>
-        <!-- /TMPL_IF -->
-        </table><p><input type="button" value="<!-- TMPL_IF NAME="searchfield" -->Update Stop Word<!-- TMPL_ELSE -->Add Stop Word <!-- /TMPL_IF -->" onclick="Check(this.form);" /> <input type="button" value="Cancel" onclick="location.href='<!-- TMPL_VAR NAME="script_name" -->';" /></p>
-        </form>
-<!-- /TMPL_IF -->
+<script>
+  function toUC(f) {
+    var x=f.value.toUpperCase();
+    f.value=x;
+    return true;
+  }
+</script>
 
-<!-- TMPL_IF NAME="add_validate" -->
-<h3>Stop Word Added</h3>
-<form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-        <input type="submit" value="OK" />
-        </form>
-<!-- /TMPL_IF -->
+<h1>Stop Words Administration</h1>
 
-<!-- TMPL_IF NAME="delete_confirm" -->
- <h3>Confirm Deletion of Stop Word <em><!-- TMPL_VAR NAME="searchfield" --></em></h3>                       <table>
-                <tr>
-                        <td>Stop Word: </td>
-                        <td><!-- TMPL_VAR NAME="searchfield" --></td>
-                </tr>
-       </table><form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-                        <input type="hidden" name="op" value="delete_confirmed" />
-                        <input type="hidden" name="searchfield" value="<!-- TMPL_VAR NAME="searchfield" -->" /><input type="submit" value="Delete this Stop Word" />
-                                </form> <form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-                                        <input type="submit" value="Do Not Delete" />
-                                </form>
-<!-- /TMPL_IF -->
+<ul id="admin_stopwords_informations">
+<!-- TMPL_IF NAME="stopword_added" -->
+  <li>Stop Words Added</li>
+<!-- /TMPL_IF --> <!-- stopword_added -->
 
 <!-- TMPL_IF NAME="delete_confirmed" -->
-<h3>Stop Word Deleted</h3>
-<form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-                <input type="submit" value="OK" />
-</form>
-<!-- /TMPL_IF -->
+  <li>Stop Words Deleted</li>
+<!-- /TMPL_IF --> <!-- delete_confirmed -->
+</ul>
 
-<!-- TMPL_IF NAME="else" -->
- <h3>Stop Words Administration</h3>
-        <form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-                <input type="text" name="searchfield" value="<!-- TMPL_VAR NAME="searchfield" -->" />
-                <input type="submit" name="submit" value="Search" />
-        </form>
-        <!-- TMPL_IF NAME="searchfield" -->
-                You searched for <!-- TMPL_VAR NAME="searchfield" -->
-        <!-- /TMPL_IF -->
-<!-- TMPL_IF NAME="loop" -->
-<table>
-                <tr>
-                        <th>Word</th>
-                        <th>&nbsp;</th>
-                </tr>
-                <!-- TMPL_LOOP NAME="loop" -->
-                <tr>
-                        <td><!-- TMPL_VAR NAME="word" --></td>
-                        <td><a href="<!-- TMPL_VAR NAME="script_name" -->?op=delete_confirm&amp;searchfield=<!-- TMPL_VAR NAME="word" -->">Delete</a></td>
-                </tr>
-                <!-- /TMPL_LOOP -->
-        </table>
-<!-- /TMPL_IF -->
-<!-- TMPL_IF NAME="offsetgtzero" -->
-               <form action="<!-- TMPL_VAR NAME="script_name" -->" method="get"><input type="hidden" name="offset" value="<!-- TMPL_VAR NAME="prevpage" -->" /><input type="submit" value="&lt;&lt; Prev" /></form>
-        <!-- /TMPL_IF --> <form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-        <input type="hidden" name="op" value="add_form" /><input type="submit" value="Add Word" />
-        </form> <!-- TMPL_IF NAME="ltcount" -->
-                <form action="<!-- TMPL_VAR NAME="script_name" -->" method="get"><input type="hidden" name="offset" value="<!-- TMPL_VAR NAME="next_page" -->" /><input type="submit" value="Next &gt;&gt;" /></form>
-        <!-- /TMPL_iF -->
-        
+<form action="<!-- TMPL_VAR NAME=script_name -->" method="post">
 
-        
+<!-- TMPL_IF NAME="delete_confirm" -->
+  <fieldset id="admin_stopwords_confirmation">
+    <legend>Confirm</legend>
 
-        
+    <p>Confirm Deletion of the following Stop Words:</p>
+    <ul>
+  <!-- TMPL_LOOP NAME="stopwords_to_delete" -->
+      <li>
+        <!-- TMPL_VAR NAME="word" -->
+        <input type="hidden" name="confirmed_stopwords[]" value="<!-- TMPL_VAR NAME="word" -->" />
+      </li>
+  <!-- /TMPL_LOOP -->
+    </ul>
 
+    <p id="action">
+      <input type="submit" name="confirmDeletion" value="Confirm Deletion" />
+      <input type="submit" name="doNotConfirmDeletion" value="Do Not Confirm" />
+    </p>
+  </fieldset>
 <!-- /TMPL_IF -->
 
+  <fieldset id="admin_stopwords_add">
+    <legend>Add Stop Words</legend>
+
+    <table>
+      <tr>
+        <th>Stop Words</th>
+        <td>
+          <input type="text" name="word" size="50" maxlength="250" onblur="toUC(this);" />
+        </td>
+      </tr>
+    </table>
+
+    <p id="action">
+      <input type="submit" name="add" value="Add Stop Words" />
+      <input type="reset" value="Reset" />
+    </p>
+  </fieldset>
+
+  <fieldset id="admin_stopwords_select">
+    <legend>Stop Words selection</legend>
+
+    <p>
+      <input type="text" name="searchfield" value="<!-- TMPL_VAR NAME="searchfield" -->" />
+      <input type="submit" name="filter" value="Filter" />
+    </p>
+
+    <ul>
+<!-- TMPL_LOOP NAME="loop" -->
+      <li style="display: inline;">
+        <label>
+          <input type="checkbox" name="stopwords[]" value="<!-- TMPL_VAR NAME="word" -->" />
+          <!-- TMPL_VAR NAME="word" -->
+        </label>
+      </li>
+<!-- /TMPL_LOOP -->
+    </ul>
+
+    <div class="paginationBar"><!-- TMPL_VAR NAME="pagination_bar" --></div>
+
+    <p id="action">
+      <input type="submit" name="deleteSelected" value="Delete selected Stop Words" />
+      <input type="reset" value="Reset" />
+    </p>
+  </fieldset>
+</form>
 
 <!-- TMPL_INCLUDE NAME="intranet-bottom.inc" -->
index 1ddaa5d..eb86c6a 100644 (file)
@@ -33,7 +33,7 @@ th[scope=row] {
        text-align : right;
 }
 
-input[type=submit], input[type=button] {
+input[type=submit], input[type=button], input[type=reset] {
        background-color : #6699cc;
        color : #FFFFFF;
        font-size : 1em;
@@ -44,3 +44,10 @@ tr.highlight td, tr.highlight th {
        background-color : #EEEEEE;
 }
 
+#admin_stopwords_select UL {
+   width: 600px;
+}
+
+#admin_stopwords_select UL LI {
+   white-space: nowrap;
+}
\ No newline at end of file