Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 #script to administer the systempref 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
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Context;
43 use C4::Output;
44 use C4::Search;
45
46 sub StringSearch  {
47         my ($env,$searchstring,$type)=@_;
48         my $dbh = C4::Context->dbh;
49         $searchstring=~ s/\'/\\\'/g;
50         my @data=split(' ',$searchstring);
51         my $count=@data;
52         my $query="Select variable,value,explanation from systempreferences where (variable like \"$data[0]%\") order by variable";
53         my $sth=$dbh->prepare($query);
54         $sth->execute;
55         my @results;
56         my $cnt=0;
57         while (my $data=$sth->fetchrow_hashref){
58         push(@results,$data);
59         $cnt ++;
60         }
61         #  $sth->execute;
62         $sth->finish;
63         return ($cnt,\@results);
64 }
65
66 my $input = new CGI;
67 my $searchfield=$input->param('searchfield');
68 my $pkfield="variable";
69 my $reqsel="select variable,value,explanation from systempreferences where $pkfield='$searchfield'";
70 my $reqdel="delete from systempreferences where $pkfield='$searchfield'";
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
73
74 my $pagesize=20;
75 my $op = $input->param('op');
76 $searchfield=~ s/\,//g;
77 print $input->header;
78
79 #start the page and read in includes
80 print startpage();
81 print startmenu('admin');
82
83 ################## ADD_FORM ##################################
84 # called by default. Used to create form to add or  modify a record
85 if ($op eq 'add_form') {
86         #---- if primkey exists, it's a modify action, so read values to modify...
87         my $data;
88         if ($searchfield) {
89                 my $dbh = C4::Context->dbh;
90                 my $sth=$dbh->prepare("select variable,value,explanation from systempreferences where variable='$searchfield'");
91                 $sth->execute;
92                 $data=$sth->fetchrow_hashref;
93                 $sth->finish;
94         }
95         print <<printend
96         <script>
97         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98         function isNotNull(f,noalert) {
99                 if (f.value.length ==0) {
100    return false;
101                 }
102                 return true;
103         }
104         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105         function toUC(f) {
106                 var x=f.value.toUpperCase();
107                 f.value=x;
108                 return true;
109         }
110         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111         function isNum(v,maybenull) {
112         var n = new Number(v.value);
113         if (isNaN(n)) {
114                 return false;
115                 }
116         if (maybenull==0 && v.value=='') {
117                 return false;
118         }
119         return true;
120         }
121         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122         function isDate(f) {
123                 var t = Date.parse(f.value);
124                 if (isNaN(t)) {
125                         return false;
126                 }
127         }
128         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129         function Check(f) {
130                 var ok=1;
131                 var _alertString="";
132                 var alertString2;
133                 if (f.variable.value.length==0) {
134                         _alertString += "- variable missing\\n";
135                 }
136                 if (f.value.value.length==0) {
137                         _alertString += "- value missing\\n";
138                 }
139                 if (_alertString.length==0) {
140                         document.Aform.submit();
141                 } else {
142                         alertString2 = "Form not submitted because of the following problem(s)\\n";
143                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
144                         alertString2 += _alertString;
145                         alert(alertString2);
146                 }
147         }
148         </SCRIPT>
149 printend
150 ;#/
151         if ($searchfield) {
152                 print "<h1>Modify pref</h1>";
153         } else {
154                 print "<h1>Add pref</h1>";
155         }
156         print "<form action='$script_name' name=Aform method=post>";
157         print "<input type=hidden name=op value='add_validate'>";
158         print "<input type=hidden name=explanation value='".$data->{'explanation'}."'>";
159         print "<table>";
160         if ($searchfield) {
161                 print "<tr><td>Variable</td><td><input type=hidden name=variable value='$searchfield'>$searchfield</td></tr>";
162         } else {
163                 print "<tr><td>Variable</td><td><input type=text name=variable size=255 maxlength=255></td></tr>";
164         }
165         print "<tr><td>Value</td><td><input type=text name=value value='$data->{'value'}'></td></tr>";
166         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
167         print "</table>";
168         print "</form>";
169 ;
170                                                                                                         # END $OP eq ADD_FORM
171 ################## ADD_VALIDATE ##################################
172 # called by add_form, used to insert/modify data in DB
173 } elsif ($op eq 'add_validate') {
174         my $dbh = C4::Context->dbh;
175         my $query = "replace systempreferences (variable,value,explanation) values (";
176         $query.= $dbh->quote($input->param('variable')).",";
177         $query.= $dbh->quote($input->param('value')).",";
178         $query.= $dbh->quote($input->param('explanation')).")";
179         my $sth=$dbh->prepare($query);
180         $sth->execute;
181         $sth->finish;
182         print "data recorded";
183         print "<form action='$script_name' method=post>";
184         print "<input type=submit value=OK>";
185         print "</form>";
186                                                                                                         # END $OP eq ADD_VALIDATE
187 ################## DELETE_CONFIRM ##################################
188 # called by default form, used to confirm deletion of data in DB
189 } elsif ($op eq 'delete_confirm') {
190         my $dbh = C4::Context->dbh;
191         my $sth=$dbh->prepare($reqsel);
192         $sth->execute;
193         my $data=$sth->fetchrow_hashref;
194         $sth->finish;
195         print mktablehdr;
196         print mktablerow(2,'#99cc33',bold('Variable'),bold("$searchfield"),'/images/background-mem.gif');
197         print "<tr><td>Value</td><td>$data->{'value'}</td></tr>";
198         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=searchfield value='$searchfield'>";
199         print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
200         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>";
201                                                                                                         # END $OP eq DELETE_CONFIRM
202 ################## DELETE_CONFIRMED ##################################
203 # called by delete_confirm, used to effectively confirm deletion of data in DB
204 } elsif ($op eq 'delete_confirmed') {
205         my $dbh = C4::Context->dbh;
206 #       my $searchfield=$input->param('branchcode');
207         my $sth=$dbh->prepare($reqdel);
208         $sth->execute;
209         $sth->finish;
210         print "data deleted";
211         print "<form action='$script_name' method=post>";
212         print "<input type=submit value=OK>";
213         print "</form>";
214                                                                                                         # END $OP eq DELETE_CONFIRMED
215 ################## DEFAULT ##################################
216 } else { # DEFAULT
217         my @inputs=(["text","searchfield",$searchfield],
218                 ["reset","reset","clr"]);
219         print mkheadr(2,'System preferences admin');
220         print mkformnotable("$script_name",@inputs);
221
222         if  ($searchfield ne '') {
223                 print "You Searched for <b>$searchfield<b><p>";
224         }
225         print mktablehdr;
226         print mktablerow(5,'#99cc33',bold('Variable'),bold('Value'),bold('Explanation'),
227         '&nbsp;','&nbsp;','/images/background-mem.gif');
228         my $env;
229         my ($count,$results)=StringSearch($env,$searchfield,'web');
230         my $toggle="white";
231         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
232                 if ($toggle eq 'white'){
233                         $toggle="#ffffcc";
234                 } else {
235                         $toggle="white";
236                 }
237                 print mktablerow(5,$toggle,$results->[$i]{'variable'},$results->[$i]{'value'},$results->[$i]{'explanation'},
238                 mklink("$script_name?op=add_form&searchfield=".$results->[$i]{'variable'},'Edit'),
239                 mklink("$script_name?op=delete_confirm&searchfield=".$results->[$i]{'variable'},'Delete'));
240         }
241         print mktableft;
242         print "<form action='$script_name' method=post>";
243         print "<input type=hidden name=op value=add_form>";
244         if ($offset>0) {
245                 my $prevpage = $offset-$pagesize;
246                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
247         }
248         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
249         if ($offset+$pagesize<$count) {
250                 my $nextpage =$offset+$pagesize;
251                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
252         }
253         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
254         print "</form>";
255 } #---- END $OP eq DEFAULT
256 print endmenu('admin');
257 print endpage();