Added a FIXME comment.
[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 use HTML::Template;
46 use C4::Context;
47
48
49 sub StringSearch  {
50         my ($env,$searchstring,$type)=@_;
51         my $dbh = C4::Context->dbh;
52         $searchstring=~ s/\'/\\\'/g;
53         my @data=split(' ',$searchstring);
54         my $count=@data;
55         my $query="Select variable,value,explanation from systempreferences where (variable like \"$data[0]%\") order by variable";
56         my $sth=$dbh->prepare($query);
57         $sth->execute;
58         my @results;
59         my $cnt=0;
60         while (my $data=$sth->fetchrow_hashref){
61         push(@results,$data);
62         $cnt ++;
63         }
64         $sth->finish;
65         return ($cnt,\@results);
66 }
67
68 my $input = new CGI;
69 my $searchfield=$input->param('searchfield');
70 my $pkfield="variable";
71 my $reqsel="select variable,value,explanation from systempreferences where $pkfield='$searchfield'";
72 my $reqdel="delete from systempreferences where $pkfield='$searchfield'";
73 my $offset=$input->param('offset');
74 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
75
76 my $template = gettemplate("parameters/systempreferences.tmpl",0);
77 my $pagesize=20;
78 my $op = $input->param('op');
79 $searchfield=~ s/\,//g;
80
81 if ($op) {
82 $template->param(script_name => $script_name,
83                                                 $op              => 1); # we show only the TMPL_VAR names $op
84 } else {
85 $template->param(script_name => $script_name,
86                                                 else              => 1); # we show only the TMPL_VAR names $op
87 }
88 ################## ADD_FORM ##################################
89 # called by default. Used to create form to add or  modify a record
90 if ($op eq 'add_form') {
91         #---- if primkey exists, it's a modify action, so read values to modify...
92         my $data;
93         if ($searchfield) {
94                 my $dbh = C4::Context->dbh;
95                 my $sth=$dbh->prepare("select variable,value,explanation from systempreferences where variable='$searchfield'");
96                 $sth->execute;
97                 $data=$sth->fetchrow_hashref;
98                 $sth->finish;
99         }
100         if ($searchfield) {
101                 $template->param(action => "Modify pref");
102         } else {
103                 $template->param(action => "Add pref");
104         }
105         $template->param(explanation => $data->{'explanation'},
106                                                         value => $data->{'value'},
107                                                         );
108         if ($searchfield) {
109                 $template->param(searchfield => "<input type=hidden name=variable value='$searchfield'>$searchfield");
110         } else {
111                 $template->param(searchfield => "<input type=text name=variable size=80 maxlength=80>");
112         }
113 ################## ADD_VALIDATE ##################################
114 # called by add_form, used to insert/modify data in DB
115 } elsif ($op eq 'add_validate') {
116         my $dbh = C4::Context->dbh;
117         my $query = "replace systempreferences (variable,value,explanation) values (";
118         $query.= $dbh->quote($input->param('variable')).",";
119         $query.= $dbh->quote($input->param('value')).",";
120         $query.= $dbh->quote($input->param('explanation')).")";
121         my $sth=$dbh->prepare($query);
122         $sth->execute;
123         $sth->finish;
124 ################## DELETE_CONFIRM ##################################
125 # called by default form, used to confirm deletion of data in DB
126 } elsif ($op eq 'delete_confirm') {
127         my $dbh = C4::Context->dbh;
128         my $sth=$dbh->prepare($reqsel);
129         $sth->execute;
130         my $data=$sth->fetchrow_hashref;
131         $sth->finish;
132         $template->param(searchfield => $searchfield,
133                                                         Tvalue => $data->{'value'},
134                                                         );
135
136                                                                                                         # END $OP eq DELETE_CONFIRM
137 ################## DELETE_CONFIRMED ##################################
138 # called by delete_confirm, used to effectively confirm deletion of data in DB
139 } elsif ($op eq 'delete_confirmed') {
140         my $dbh = C4::Context->dbh;
141         my $sth=$dbh->prepare($reqdel);
142         $sth->execute;
143         $sth->finish;
144                                                                                                         # END $OP eq DELETE_CONFIRMED
145 ################## DEFAULT ##################################
146 } else { # DEFAULT
147         if  ($searchfield ne '') {
148                  $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
149         }
150         my $env;
151         my ($count,$results)=StringSearch($env,$searchfield,'web');
152         my $toggle="white";
153         my @loop_data = ();
154         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
155                 if ($toggle eq 'white'){
156                         $toggle="#ffffcc";
157                 } else {
158                         $toggle="white";
159                 }
160                 my %row_data;  # get a fresh hash for the row data
161                 $row_data{variable} = $results->[$i]{'variable'};
162                 $row_data{value} = $results->[$i]{'value'};
163                 $row_data{explanation} = $results->[$i]{'explanation'};
164                 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'variable'};
165                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'variable'};
166                 push(@loop_data, \%row_data);
167         }
168         $template->param(loop => \@loop_data);
169         if ($offset>0) {
170                 my $prevpage = $offset-$pagesize;
171                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
172         }
173         if ($offset+$pagesize<$count) {
174                 my $nextpage =$offset+$pagesize;
175                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
176         }
177 } #---- END $OP eq DEFAULT
178
179 print "Content-Type: text/html\n\n", $template->output;