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