fixed a typo
[koha.git] / admin / stopwords.pl
1 #!/usr/bin/perl
2
3 #script to administer the stopwords table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22 use strict;
23 use C4::Output;
24 use CGI;
25 use C4::Search;
26 use C4::Database;
27
28 sub StringSearch  {
29         my ($env,$searchstring,$type)=@_;
30         my $dbh = &C4Connect;
31         $searchstring=~ s/\'/\\\'/g;
32         my @data=split(' ',$searchstring);
33         my $count=@data;
34         my $query="Select word from stopwords where (word like \"$data[0]%\") order by word";
35         my $sth=$dbh->prepare($query);
36         $sth->execute;
37         my @results;
38         my $cnt=0;
39         while (my $data=$sth->fetchrow_hashref){
40         push(@results,$data);
41         $cnt ++;
42         }
43         #  $sth->execute;
44         $sth->finish;
45         $dbh->disconnect;
46         return ($cnt,\@results);
47 }
48
49 my $input = new CGI;
50 my $searchfield=$input->param('searchfield');
51 my $pkfield="word";
52 my $reqsel="select word from stopwords where $pkfield='$searchfield'";
53 my $reqdel="delete from stopwords where $pkfield='$searchfield'";
54 my $offset=$input->param('offset');
55 my $script_name="/cgi-bin/koha/admin/stopwords.pl";
56
57 my $pagesize=20;
58 my $op = $input->param('op');
59 $searchfield=~ s/\,//g;
60 print $input->header;
61
62 #start the page and read in includes
63 print startpage();
64 print startmenu('admin');
65
66 ################## ADD_FORM ##################################
67 # called by default. Used to create form to add or  modify a record
68 if ($op eq 'add_form') {
69         #---- if primkey exists, it's a modify action, so read values to modify...
70         my $data;
71         if ($searchfield) {
72                 my $dbh = &C4Connect;
73                 my $sth=$dbh->prepare("select word from stopwords where word='$searchfield'");
74                 $sth->execute;
75                 $data=$sth->fetchrow_hashref;
76                 $sth->finish;
77         }
78         print <<printend
79         <script>
80         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81         function isNotNull(f,noalert) {
82                 if (f.value.length ==0) {
83    return false;
84                 }
85                 return true;
86         }
87         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88         function toUC(f) {
89                 var x=f.value.toUpperCase();
90                 f.value=x;
91                 return true;
92         }
93         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
94         function isNum(v,maybenull) {
95         var n = new Number(v.value);
96         if (isNaN(n)) {
97                 return false;
98                 }
99         if (maybenull==0 && v.value=='') {
100                 return false;
101         }
102         return true;
103         }
104         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105         function isDate(f) {
106                 var t = Date.parse(f.value);
107                 if (isNaN(t)) {
108                         return false;
109                 }
110         }
111         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112         function Check(f) {
113                 var ok=1;
114                 var _alertString="";
115                 var alertString2;
116                 if (f.word.value.length==0) {
117                         _alertString += "- word missing\\n";
118                 }
119                 if (_alertString.length==0) {
120                         document.Aform.submit();
121                 } else {
122                         alertString2 = "Form not submitted because of the following problem(s)\\n";
123                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
124                         alertString2 += _alertString;
125                         alert(alertString2);
126                 }
127         }
128         </SCRIPT>
129 printend
130 ;#/
131         if ($searchfield) {
132                 print "<h1>Modify word</h1>";
133         } else {
134                 print "<h1>Add word</h1>";
135         }
136         print "<form action='$script_name' name=Aform method=post>";
137         print "<input type=hidden name=op value='add_validate'>";
138         print "<table>";
139         if ($searchfield) {
140                 print "<tr><td>Word</td><td><input type=hidden name=word value='$searchfield'>$searchfield</td></tr>";
141         } else {
142                 print "<tr><td>Word</td><td><input type=text name=word size=255 maxlength=255></td></tr>";
143         }
144         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
145         print "</table>";
146         print "</form>";
147 ;
148                                                                                                         # END $OP eq ADD_FORM
149 ################## ADD_VALIDATE ##################################
150 # called by add_form, used to insert/modify data in DB
151 } elsif ($op eq 'add_validate') {
152         my $dbh=C4Connect;
153         my $query = "replace stopwords (word) values (";
154         $query.= $dbh->quote($input->param('word')).")";
155         my $sth=$dbh->prepare($query);
156         $sth->execute;
157         $sth->finish;
158         print "data recorded";
159         print "<form action='$script_name' method=post>";
160         print "<input type=submit value=OK>";
161         print "</form>";
162                                                                                                         # END $OP eq ADD_VALIDATE
163 ################## DELETE_CONFIRM ##################################
164 # called by default form, used to confirm deletion of data in DB
165 } elsif ($op eq 'delete_confirm') {
166         my $dbh = &C4Connect;
167         my $sth=$dbh->prepare($reqsel);
168         $sth->execute;
169         my $data=$sth->fetchrow_hashref;
170         $sth->finish;
171         print mktablehdr;
172         print mktablerow(2,'#99cc33',bold('Word'),bold("$searchfield"),'/images/background-mem.gif');
173         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=searchfield value='$searchfield'>";
174         print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
175         print "<tr><td><INPUT type=submit value='YES'></form></td><td><form action='$script_name' method=post><input type=submit value=NO></form></td></tr>";
176                                                                                                         # END $OP eq DELETE_CONFIRM
177 ################## DELETE_CONFIRMED ##################################
178 # called by delete_confirm, used to effectively confirm deletion of data in DB
179 } elsif ($op eq 'delete_confirmed') {
180         my $dbh=C4Connect;
181 #       my $searchfield=$input->param('branchcode');
182         my $sth=$dbh->prepare($reqdel);
183         $sth->execute;
184         $sth->finish;
185         print "data deleted";
186         print "<form action='$script_name' method=post>";
187         print "<input type=submit value=OK>";
188         print "</form>";
189                                                                                                         # END $OP eq DELETE_CONFIRMED
190 ################## DEFAULT ##################################
191 } else { # DEFAULT
192         my @inputs=(["text","searchfield",$searchfield],
193                 ["reset","reset","clr"]);
194         print mkheadr(2,'Stopwords admin');
195         print mkformnotable("$script_name",@inputs);
196         print <<printend
197
198 printend
199         ;
200         if  ($searchfield ne '') {
201                 print "You Searched for <b>$searchfield<b><p>";
202         }
203         print mktablehdr;
204         print mktablerow(2,'#99cc33',bold('Word'),
205         '&nbsp;','&nbsp;','/images/background-mem.gif');
206         my $env;
207         my ($count,$results)=StringSearch($env,$searchfield,'web');
208         my $toggle="white";
209         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
210                 if ($toggle eq 'white'){
211                         $toggle="#ffffcc";
212                 } else {
213                         $toggle="white";
214                 }
215                 print mktablerow(2,$toggle,$results->[$i]{'word'},
216                 mklink("$script_name?op=delete_confirm&searchfield=".$results->[$i]{'word'},'Delete',''));
217         }
218         print mktableft;
219         print "<form action='$script_name' method=post>";
220         print "<input type=hidden name=op value=add_form>";
221         if ($offset>0) {
222                 my $prevpage = $offset-$pagesize;
223                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
224         }
225         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
226         if ($offset+$pagesize<$count) {
227                 my $nextpage =$offset+$pagesize;
228                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
229         }
230         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
231         print "</form>";
232 } #---- END $OP eq DEFAULT
233 print endmenu('admin');
234 print endpage();