Experimental rewrite of Stop Words administration screen. Feedback welcomed.
[koha.git] / admin / stopwords.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17 #
18 ###
19 #
20 # script to administer the stopwords table
21 #
22 # - written on 2002/02/20 by paul.poulain@free.fr
23 #
24 # - experimentaly rewrittten on 2006/04/06 by Pierrick LE GALL (INEO media
25 #   system)
26 #
27
28 use strict;
29 use CGI;
30 use List::Util qw/min/;
31
32 use C4::Context;
33 use C4::Output;
34 use C4::Search;
35 use HTML::Template;
36 use C4::Auth;
37 use C4::Interface::CGI::Output;
38
39 sub StringSearch  {
40     my ($searchstring) = @_;
41
42     my $dbh = C4::Context->dbh;
43     $searchstring =~ s/\'/\\\'/g;
44     my @tokens = split(' ',$searchstring);
45
46     my $query = '
47 SELECT word
48   FROM stopwords
49   WHERE (word like ?)
50   ORDER BY word
51 ';
52     my $sth = $dbh->prepare($query);
53     $sth->execute($tokens[0].'%');
54     my @results;
55     while (my $row = $sth->fetchrow_hashref) {
56         push(@results, $row->{word});
57     }
58     $sth->finish;
59
60     return @results;
61 }
62
63 my $dbh = C4::Context->dbh;
64 my $sth;
65 my $query;
66 my $input = new CGI;
67 my $searchfield = $input->param('searchfield');
68 my $script_name="/cgi-bin/koha/admin/stopwords.pl";
69
70 my $pagesize = 40;
71 my $op = $input->param('op');
72 $searchfield=~ s/\,//g;
73
74 my ($template, $loggedinuser, $cookie) 
75     = get_template_and_user({template_name => "admin/stopwords.tmpl",
76                             query => $input,
77                             type => "intranet",
78                             flagsrequired => {parameters => 1, management => 1},
79                             authnotrequired => 0,
80                             debug => 1,
81                             });
82
83 $template->param(script_name => $script_name,
84                  searchfield => $searchfield);
85
86 if ($input->param('add')) {
87     if ($input->param('word')) {
88         my @words = split / |,/, $input->param('word');
89
90         $query = '
91 DELETE
92   FROM stopwords
93   WHERE word IN (?'.(',?' x scalar @words - 1).')
94 ';
95         $sth = $dbh->prepare($query);
96         $sth->execute(@words);
97         $sth->finish;
98
99         $query = '
100 INSERT
101   INTO stopwords
102   (word)
103   VALUES
104   (?)'.(',(?)' x scalar @words - 1).'
105 ';
106         $sth = $dbh->prepare($query);
107         $sth->execute(@words);
108         $sth->finish;
109
110         $template->param(stopword_added => 1);
111     }
112 }
113 elsif ($input->param('deleteSelected')) {
114     if ($input->param('stopwords[]')) {
115         my @stopwords_loop = ();
116
117         foreach my $word ($input->param('stopwords[]')) {
118             push @stopwords_loop,  {word => $word};
119         }
120
121         $template->param(
122             delete_confirm => 1,
123             stopwords_to_delete => \@stopwords_loop,
124         );
125     }
126 }
127 elsif ($input->param('confirmDeletion')) {
128     my @words = $input->param('confirmed_stopwords[]');
129
130     $query = '
131 DELETE
132   FROM stopwords
133   WHERE word IN (?'.(',?' x scalar @words - 1).')
134 ';
135     $sth = $dbh->prepare($query);
136     $sth->execute(@words);
137     $sth->finish;
138
139     $template->param(delete_confirmed => 1);
140 }
141
142 my $page = $input->param('page') || 1;
143
144 my @results = StringSearch($searchfield);
145 my @loop;
146
147 my $first = ($page - 1) * $pagesize;
148
149 # if we are on the last page, the number of the last word to display must
150 # not exceed the length of the results array
151 my $last = min(
152     $first + $pagesize - 1,
153     scalar(@results) - 1,
154 );
155
156 foreach my $word (@results[$first .. $last]) {
157     push @loop, {word => $word};
158 }
159
160 $template->param(
161     loop => \@loop,
162     pagination_bar => pagination_bar(
163         $script_name,
164         int(scalar(@results) / $pagesize) + 1,
165         $page,
166         'page'
167     )
168 );
169
170 output_html_with_http_headers $input, $cookie, $template->output;