scripts to manage parameters tables
[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 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 variable,value from systempreferences where (variable like \"$data[0]%\") order by variable";
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="variable";
52 my $reqsel="select variable,value from systempreferences where $pkfield='$searchfield'";
53 my $reqdel="delete from systempreferences where $pkfield='$searchfield'";
54 my $offset=$input->param('offset');
55 my $script_name="/cgi-bin/koha/admin/systempreferences.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 variable,value from systempreferences where variable='$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.variable.value.length==0) {
117                         _alertString += "- variable missing\\n";
118                 }
119                 if (f.value.value.length==0) {
120                         _alertString += "- value missing\\n";
121                 }
122                 if (_alertString.length==0) {
123                         document.Aform.submit();
124                 } else {
125                         alertString2 = "Form not submitted because of the following problem(s)\\n";
126                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
127                         alertString2 += _alertString;
128                         alert(alertString2);
129                 }
130         }
131         </SCRIPT>
132 printend
133 ;#/
134         if ($searchfield) {
135                 print "<h1>Modify pref</h1>";
136         } else {
137                 print "<h1>Add pref</h1>";
138         }
139         print "<form action='$script_name' name=Aform method=post>";
140         print "<input type=hidden name=op value='add_validate'>";
141         print "<table>";
142         if ($searchfield) {
143                 print "<tr><td>Variable</td><td><input type=hidden name=variable value='$searchfield'>$searchfield</td></tr>";
144         } else {
145                 print "<tr><td>Variable</td><td><input type=text name=variable size=255 maxlength=255></td></tr>";
146         }
147         print "<tr><td>Value</td><td><input type=text name=value value='$data->{'value'}'></td></tr>";
148         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
149         print "</table>";
150         print "</form>";
151 ;
152                                                                                                         # END $OP eq ADD_FORM
153 ################## ADD_VALIDATE ##################################
154 # called by add_form, used to insert/modify data in DB
155 } elsif ($op eq 'add_validate') {
156         my $dbh=C4Connect;
157         my $query = "replace systempreferences (variable,value) values (";
158         $query.= $dbh->quote($input->param('variable')).",";
159         $query.= $dbh->quote($input->param('value')).")";
160         my $sth=$dbh->prepare($query);
161         $sth->execute;
162         $sth->finish;
163         print "data recorded";
164         print "<form action='$script_name' method=post>";
165         print "<input type=submit value=OK>";
166         print "</form>";
167                                                                                                         # END $OP eq ADD_VALIDATE
168 ################## DELETE_CONFIRM ##################################
169 # called by default form, used to confirm deletion of data in DB
170 } elsif ($op eq 'delete_confirm') {
171         my $dbh = &C4Connect;
172         my $sth=$dbh->prepare($reqsel);
173         $sth->execute;
174         my $data=$sth->fetchrow_hashref;
175         $sth->finish;
176         print mktablehdr;
177         print mktablerow(2,'#99cc33',bold('Variable'),bold("$searchfield"),'/images/background-mem.gif');
178         print "<tr><td>Value</td><td>$data->{'value'}</td></tr>";
179         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=searchfield value='$searchfield'>";
180         print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
181         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>";
182                                                                                                         # END $OP eq DELETE_CONFIRM
183 ################## DELETE_CONFIRMED ##################################
184 # called by delete_confirm, used to effectively confirm deletion of data in DB
185 } elsif ($op eq 'delete_confirmed') {
186         my $dbh=C4Connect;
187 #       my $searchfield=$input->param('branchcode');
188         my $sth=$dbh->prepare($reqdel);
189         $sth->execute;
190         $sth->finish;
191         print "data deleted";
192         print "<form action='$script_name' method=post>";
193         print "<input type=submit value=OK>";
194         print "</form>";
195                                                                                                         # END $OP eq DELETE_CONFIRMED
196 ################## DEFAULT ##################################
197 } else { # DEFAULT
198         my @inputs=(["text","searchfield",$searchfield],
199                 ["reset","reset","clr"]);
200         print mkheadr(2,'System preferences admin');
201         print mkformnotable("$script_name",@inputs);
202         print <<printend
203 <b>Hints :</b>
204 2 variables are useful in this table :
205 <li><i>acquisitions</i>, which value may be "simple" or "normal"</li>
206 <li><i>autoMemberNum</i> which may be 1 or 0</li>
207 <br><br>
208 printend
209         ;
210         if  ($searchfield ne '') {
211                 print "You Searched for <b>$searchfield<b><p>";
212         }
213         print mktablehdr;
214         print mktablerow(4,'#99cc33',bold('Variable'),bold('Value'),
215         '&nbsp;','&nbsp;','/images/background-mem.gif');
216         my $env;
217         my ($count,$results)=StringSearch($env,$searchfield,'web');
218         my $toggle="white";
219         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
220                 if ($toggle eq 'white'){
221                         $toggle="#ffffcc";
222                 } else {
223                         $toggle="white";
224                 }
225                 print mktablerow(4,$toggle,$results->[$i]{'variable'},$results->[$i]{'value'},
226                 mklink("$script_name?op=add_form&searchfield=".$results->[$i]{'variable'},'Edit'),
227                 mklink("$script_name?op=delete_confirm&searchfield=".$results->[$i]{'variable'},'Delete',''));
228         }
229         print mktableft;
230         print "<form action='$script_name' method=post>";
231         print "<input type=hidden name=op value=add_form>";
232         if ($offset>0) {
233                 my $prevpage = $offset-$pagesize;
234                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
235         }
236         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
237         if ($offset+$pagesize<$count) {
238                 my $nextpage =$offset+$pagesize;
239                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
240         }
241         print "<br><input type=image src=\"/images/button-add-member.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
242         print "</form>";
243 } #---- END $OP eq DEFAULT
244 print endmenu('admin');
245 print endpage();