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