rel_3_0 moved to HEAD
[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::Koha;
33 use C4::Context;
34 use C4::Output;
35 use C4::Search;
36 use HTML::Template;
37 use C4::Auth;
38 use C4::Interface::CGI::Output;
39
40 sub StringSearch  {
41     my ($searchstring) = @_;
42
43     my $dbh = C4::Context->dbh;
44     $searchstring =~ s/\'/\\\'/g;
45     my @tokens = split(' ',$searchstring);
46
47     my $query = '
48 SELECT word
49   FROM stopwords
50   WHERE (word like ?)
51   ORDER BY word
52 ';
53     my $sth = $dbh->prepare($query);
54     $sth->execute($tokens[0].'%');
55     my @results;
56     while (my $row = $sth->fetchrow_hashref) {
57         push(@results, $row->{word});
58     }
59     $sth->finish;
60
61     return @results;
62 }
63
64 my $dbh = C4::Context->dbh;
65 my $sth;
66 my $query;
67 my $input = new CGI;
68 my $searchfield = $input->param('searchfield');
69 my $script_name="/cgi-bin/koha/admin/stopwords.pl";
70
71 my $pagesize = 40;
72 my $op = $input->param('op');
73 $searchfield=~ s/\,//g;
74
75 my ($template, $loggedinuser, $cookie) 
76     = get_template_and_user({template_name => "admin/stopwords.tmpl",
77                             query => $input,
78                             type => "intranet",
79                             flagsrequired => {parameters => 1, management => 1},
80                             authnotrequired => 0,
81                             debug => 1,
82                             });
83
84 $template->param(script_name => $script_name,
85                  searchfield => $searchfield);
86
87 if ($input->param('add')) {
88     if ($input->param('word')) {
89         my @words = split / |,/, $input->param('word');
90
91         $query = '
92 DELETE
93   FROM stopwords
94   WHERE word IN (?'.(',?' x scalar @words - 1).')
95 ';
96         $sth = $dbh->prepare($query);
97         $sth->execute(@words);
98         $sth->finish;
99
100         $query = '
101 INSERT
102   INTO stopwords
103   (word)
104   VALUES
105   (?)'.(',(?)' x scalar @words - 1).'
106 ';
107         $sth = $dbh->prepare($query);
108         $sth->execute(@words);
109         $sth->finish;
110
111         $template->param(stopword_added => 1);
112     }
113 }
114 elsif ($input->param('deleteSelected')) {
115     if ($input->param('stopwords[]')) {
116         my @stopwords_loop = ();
117
118         foreach my $word ($input->param('stopwords[]')) {
119             push @stopwords_loop,  {word => $word};
120         }
121
122         $template->param(
123             delete_confirm => 1,
124             stopwords_to_delete => \@stopwords_loop,
125         );
126     }
127 }
128 elsif ($input->param('confirmDeletion')) {
129     my @words = $input->param('confirmed_stopwords[]');
130
131     $query = '
132 DELETE
133   FROM stopwords
134   WHERE word IN (?'.(',?' x scalar @words - 1).')
135 ';
136     $sth = $dbh->prepare($query);
137     $sth->execute(@words);
138     $sth->finish;
139
140     $template->param(delete_confirmed => 1);
141 }
142
143 my $page = $input->param('page') || 1;
144
145 my @results = StringSearch($searchfield);
146 my @loop;
147
148 my $first = ($page - 1) * $pagesize;
149
150 # if we are on the last page, the number of the last word to display must
151 # not exceed the length of the results array
152 my $last = min(
153     $first + $pagesize - 1,
154     scalar(@results) - 1,
155 );
156
157 foreach my $word (@results[$first .. $last]) {
158     push @loop, {word => $word};
159 }
160
161 $template->param(
162     loop => \@loop,
163     pagination_bar => pagination_bar(
164         $script_name,
165         getnbpages(scalar @results, $pagesize),
166         $page,
167         'page'
168     )
169 );
170
171 output_html_with_http_headers $input, $cookie, $template->output;