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