Added copyright statement to all .pl and .pm files
[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 C4::Output;
42 use CGI;
43 use C4::Search;
44 use C4::Database;
45
46 sub StringSearch  {
47         my ($env,$searchstring,$type)=@_;
48         my $dbh = &C4Connect;
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         $dbh->disconnect;
64         return ($cnt,\@results);
65 }
66
67 my $input = new CGI;
68 my $searchfield=$input->param('searchfield');
69 my $pkfield="variable";
70 my $reqsel="select variable,value,explanation from systempreferences where $pkfield='$searchfield'";
71 my $reqdel="delete from systempreferences where $pkfield='$searchfield'";
72 my $offset=$input->param('offset');
73 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
74
75 my $pagesize=20;
76 my $op = $input->param('op');
77 $searchfield=~ s/\,//g;
78 print $input->header;
79
80 #start the page and read in includes
81 print startpage();
82 print startmenu('admin');
83
84 ################## ADD_FORM ##################################
85 # called by default. Used to create form to add or  modify a record
86 if ($op eq 'add_form') {
87         #---- if primkey exists, it's a modify action, so read values to modify...
88         my $data;
89         if ($searchfield) {
90                 my $dbh = &C4Connect;
91                 my $sth=$dbh->prepare("select variable,value,explanation from systempreferences where variable='$searchfield'");
92                 $sth->execute;
93                 $data=$sth->fetchrow_hashref;
94                 $sth->finish;
95         }
96         print <<printend
97         <script>
98         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99         function isNotNull(f,noalert) {
100                 if (f.value.length ==0) {
101    return false;
102                 }
103                 return true;
104         }
105         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106         function toUC(f) {
107                 var x=f.value.toUpperCase();
108                 f.value=x;
109                 return true;
110         }
111         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112         function isNum(v,maybenull) {
113         var n = new Number(v.value);
114         if (isNaN(n)) {
115                 return false;
116                 }
117         if (maybenull==0 && v.value=='') {
118                 return false;
119         }
120         return true;
121         }
122         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123         function isDate(f) {
124                 var t = Date.parse(f.value);
125                 if (isNaN(t)) {
126                         return false;
127                 }
128         }
129         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130         function Check(f) {
131                 var ok=1;
132                 var _alertString="";
133                 var alertString2;
134                 if (f.variable.value.length==0) {
135                         _alertString += "- variable missing\\n";
136                 }
137                 if (f.value.value.length==0) {
138                         _alertString += "- value missing\\n";
139                 }
140                 if (_alertString.length==0) {
141                         document.Aform.submit();
142                 } else {
143                         alertString2 = "Form not submitted because of the following problem(s)\\n";
144                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
145                         alertString2 += _alertString;
146                         alert(alertString2);
147                 }
148         }
149         </SCRIPT>
150 printend
151 ;#/
152         if ($searchfield) {
153                 print "<h1>Modify pref</h1>";
154         } else {
155                 print "<h1>Add pref</h1>";
156         }
157         print "<form action='$script_name' name=Aform method=post>";
158         print "<input type=hidden name=op value='add_validate'>";
159         print "<input type=hidden name=explanation value='".$data->{'explanation'}."'>";
160         print "<table>";
161         if ($searchfield) {
162                 print "<tr><td>Variable</td><td><input type=hidden name=variable value='$searchfield'>$searchfield</td></tr>";
163         } else {
164                 print "<tr><td>Variable</td><td><input type=text name=variable size=255 maxlength=255></td></tr>";
165         }
166         print "<tr><td>Value</td><td><input type=text name=value value='$data->{'value'}'></td></tr>";
167         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
168         print "</table>";
169         print "</form>";
170 ;
171                                                                                                         # END $OP eq ADD_FORM
172 ################## ADD_VALIDATE ##################################
173 # called by add_form, used to insert/modify data in DB
174 } elsif ($op eq 'add_validate') {
175         my $dbh=C4Connect;
176         my $query = "replace systempreferences (variable,value,explanation) values (";
177         $query.= $dbh->quote($input->param('variable')).",";
178         $query.= $dbh->quote($input->param('value')).",";
179         $query.= $dbh->quote($input->param('explanation')).")";
180         my $sth=$dbh->prepare($query);
181         $sth->execute;
182         $sth->finish;
183         print "data recorded";
184         print "<form action='$script_name' method=post>";
185         print "<input type=submit value=OK>";
186         print "</form>";
187                                                                                                         # END $OP eq ADD_VALIDATE
188 ################## DELETE_CONFIRM ##################################
189 # called by default form, used to confirm deletion of data in DB
190 } elsif ($op eq 'delete_confirm') {
191         my $dbh = &C4Connect;
192         my $sth=$dbh->prepare($reqsel);
193         $sth->execute;
194         my $data=$sth->fetchrow_hashref;
195         $sth->finish;
196         print mktablehdr;
197         print mktablerow(2,'#99cc33',bold('Variable'),bold("$searchfield"),'/images/background-mem.gif');
198         print "<tr><td>Value</td><td>$data->{'value'}</td></tr>";
199         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=searchfield value='$searchfield'>";
200         print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
201         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>";
202                                                                                                         # END $OP eq DELETE_CONFIRM
203 ################## DELETE_CONFIRMED ##################################
204 # called by delete_confirm, used to effectively confirm deletion of data in DB
205 } elsif ($op eq 'delete_confirmed') {
206         my $dbh=C4Connect;
207 #       my $searchfield=$input->param('branchcode');
208         my $sth=$dbh->prepare($reqdel);
209         $sth->execute;
210         $sth->finish;
211         print "data deleted";
212         print "<form action='$script_name' method=post>";
213         print "<input type=submit value=OK>";
214         print "</form>";
215                                                                                                         # END $OP eq DELETE_CONFIRMED
216 ################## DEFAULT ##################################
217 } else { # DEFAULT
218         my @inputs=(["text","searchfield",$searchfield],
219                 ["reset","reset","clr"]);
220         print mkheadr(2,'System preferences admin');
221         print mkformnotable("$script_name",@inputs);
222         print <<printend
223 printend
224         ;
225         if  ($searchfield ne '') {
226                 print "You Searched for <b>$searchfield<b><p>";
227         }
228         print mktablehdr;
229         print mktablerow(5,'#99cc33',bold('Variable'),bold('Value'),bold('Explanation'),
230         '&nbsp;','&nbsp;','/images/background-mem.gif');
231         my $env;
232         my ($count,$results)=StringSearch($env,$searchfield,'web');
233         my $toggle="white";
234         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
235                 if ($toggle eq 'white'){
236                         $toggle="#ffffcc";
237                 } else {
238                         $toggle="white";
239                 }
240                 print mktablerow(5,$toggle,$results->[$i]{'variable'},$results->[$i]{'value'},$results->[$i]{'explanation'},
241                 mklink("$script_name?op=add_form&searchfield=".$results->[$i]{'variable'},'Edit'),
242                 mklink("$script_name?op=delete_confirm&searchfield=".$results->[$i]{'variable'},'Delete'));
243         }
244         print mktableft;
245         print "<form action='$script_name' method=post>";
246         print "<input type=hidden name=op value=add_form>";
247         if ($offset>0) {
248                 my $prevpage = $offset-$pagesize;
249                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
250         }
251         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
252         if ($offset+$pagesize<$count) {
253                 my $nextpage =$offset+$pagesize;
254                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
255         }
256         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
257         print "</form>";
258 } #---- END $OP eq DEFAULT
259 print endmenu('admin');
260 print endpage();