adding authentification with Auth.pm
[koha.git] / admin / authorised_values.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Output;
25 use C4::Search;
26 use HTML::Template;
27 use C4::Context;
28
29
30 sub StringSearch  {
31         my ($env,$searchstring,$type)=@_;
32         my $dbh = C4::Context->dbh;
33         $searchstring=~ s/\'/\\\'/g;
34         my @data=split(' ',$searchstring);
35         my $count=@data;
36         my $query="Select id,category,authorised_value,lib from authorised_values where (category like \"$data[0]%\") order by category,authorised_value";
37         my $sth=$dbh->prepare($query);
38         $sth->execute;
39         my @results;
40         my $cnt=0;
41         while (my $data=$sth->fetchrow_hashref){
42         push(@results,$data);
43         $cnt ++;
44         }
45         $sth->finish;
46         return ($cnt,\@results);
47 }
48
49 my $input = new CGI;
50 my $searchfield=$input->param('searchfield');
51 $searchfield=~ s/\,//g;
52 my $id = $input->param('id');
53 my $reqsel="select category,authorised_value,lib from authorised_values where id='$id'";
54 my $reqdel="delete from authorised_values where id='$id'";
55 my $offset=$input->param('offset');
56 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
57 my $dbh = C4::Context->dbh;
58
59 my ($template, $borrowernumber, $cookie)
60     = get_template_and_user({template_name => "parameters/authorised_values.tmpl",
61                              query => $input,
62                              type => "intranet",
63                              authnotrequired => 0,
64                              flagsrequired => {parameters => 1},
65                              debug => 1,
66                              });
67 my $pagesize=20;
68 my $op = $input->param('op');
69
70 if ($op) {
71 $template->param(script_name => $script_name,
72                                                 $op              => 1); # we show only the TMPL_VAR names $op
73 } else {
74 $template->param(script_name => $script_name,
75                                                 else              => 1); # we show only the TMPL_VAR names $op
76 }
77 ################## ADD_FORM ##################################
78 # called by default. Used to create form to add or  modify a record
79 if ($op eq 'add_form') {
80         my $data;
81         if ($id) {
82                 my $dbh = C4::Context->dbh;
83                 my $sth=$dbh->prepare("select id,category,authorised_value,lib from authorised_values where id='$id'");
84                 $sth->execute;
85                 $data=$sth->fetchrow_hashref;
86                 $sth->finish;
87         } else {
88                 $data->{'category'} = $input->param('category');
89         }
90         if ($searchfield) {
91                 $template->param(action => "Modify authorised value");
92         } else {
93                 $template->param(action => "Add authorised value");
94         }
95         $template->param(category => $data->{'category'},
96                                                         authorised_value => $data->{'authorised_value'},
97                                                         lib => $data->{'lib'},
98                                                         id => $data->{'id'}
99                                                         );
100         if ($data->{'category'}) {
101                 $template->param(category => "<input type=\"hidden\" name=\"category\" value='$data->{'category'}'>$data->{'category'}");
102         } else {
103                 $template->param(category => "<input type=text name=\"category\" size=8 maxlength=8>");
104         }
105 ################## ADD_VALIDATE ##################################
106 # called by add_form, used to insert/modify data in DB
107 } elsif ($op eq 'add_validate') {
108         my $dbh = C4::Context->dbh;
109         my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
110         $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'),$input->param('lib'));
111         $sth->finish;
112         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
113         exit;
114 ################## DELETE_CONFIRM ##################################
115 # called by default form, used to confirm deletion of data in DB
116 } elsif ($op eq 'delete_confirm') {
117         my $dbh = C4::Context->dbh;
118         my $sth=$dbh->prepare($reqsel);
119         $sth->execute;
120         my $data=$sth->fetchrow_hashref;
121         $sth->finish;
122         $template->param(searchfield => $searchfield,
123                                                         Tvalue => $data->{'authorised_value'},
124                                                         id =>$id,
125                                                         );
126
127                                                                                                         # END $OP eq DELETE_CONFIRM
128 ################## DELETE_CONFIRMED ##################################
129 # called by delete_confirm, used to effectively confirm deletion of data in DB
130 } elsif ($op eq 'delete_confirmed') {
131         my $dbh = C4::Context->dbh;
132         my $sth=$dbh->prepare($reqdel);
133         $sth->execute;
134         $sth->finish;
135         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
136         exit;
137
138                                                                                                         # END $OP eq DELETE_CONFIRMED
139 ################## DEFAULT ##################################
140 } else { # DEFAULT
141         # build categories list
142         my $sth = $dbh->prepare("select distinct category from authorised_values");
143         $sth->execute;
144         my @category_list;
145         while ( my ($category) = $sth->fetchrow_array) {
146                 push(@category_list,$category);
147         }
148         # push koha system categories
149         my $tab_list = CGI::scrolling_list(-name=>'searchfield',
150                         -values=> \@category_list,
151                         -default=>"",
152                         -size=>1,
153                         -multiple=>0,
154                         );
155         if (!$searchfield) {
156                 $searchfield=$category_list[0];
157         }
158         my $env;
159         my ($count,$results)=StringSearch($env,$searchfield,'web');
160         my $toggle="white";
161         my @loop_data = ();
162         # builds value list
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{category} = $results->[$i]{'category'};
171                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
172                 $row_data{lib} = $results->[$i]{'lib'};
173                 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
174                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
175                 push(@loop_data, \%row_data);
176         }
177
178         $template->param(loop => \@loop_data,
179                                                         tab_list => $tab_list,
180                                                         category => $searchfield);
181         if ($offset>0) {
182                 my $prevpage = $offset-$pagesize;
183                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
184         }
185         if ($offset+$pagesize<$count) {
186                 my $nextpage =$offset+$pagesize;
187                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
188         }
189 } #---- END $OP eq DEFAULT
190
191 print $input->header(-cookie => $cookie), $template->output;