Insert the filename of the token into the TmplToken object too
[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 $sth=$dbh->prepare("Select id,category,authorised_value,lib from authorised_values where (category like ?) order by category,authorised_value");
38         $sth->execute("$data[0]%");
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 $offset=$input->param('offset');
54 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
55 my $dbh = C4::Context->dbh;
56
57 my ($template, $borrowernumber, $cookie)
58     = get_template_and_user({template_name => "parameters/authorised_values.tmpl",
59                              query => $input,
60                              type => "intranet",
61                              authnotrequired => 0,
62                              flagsrequired => {parameters => 1},
63                              debug => 1,
64                              });
65 my $pagesize=5;
66 my $op = $input->param('op');
67
68 if ($op) {
69 $template->param(script_name => $script_name,
70                                                 $op              => 1); # we show only the TMPL_VAR names $op
71 } else {
72 $template->param(script_name => $script_name,
73                                                 else              => 1); # we show only the TMPL_VAR names $op
74 }
75 ################## ADD_FORM ##################################
76 # called by default. Used to create form to add or  modify a record
77 if ($op eq 'add_form') {
78         my $data;
79         if ($id) {
80                 my $dbh = C4::Context->dbh;
81                 my $sth=$dbh->prepare("select id,category,authorised_value,lib from authorised_values where id=?");
82                 $sth->execute($id);
83                 $data=$sth->fetchrow_hashref;
84                 $sth->finish;
85         } else {
86                 $data->{'category'} = $input->param('category');
87         }
88         if ($searchfield) {
89                 $template->param(action => "Modify authorised value");
90         } elsif ( ! $data->{'category'} ) {
91                 $template->param(action => "Add new category");
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         my $lib = $input->param('lib');
111         undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
112         
113         $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $lib);
114         $sth->finish;
115         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
116         exit;
117 ################## DELETE_CONFIRM ##################################
118 # called by default form, used to confirm deletion of data in DB
119 } elsif ($op eq 'delete_confirm') {
120         my $dbh = C4::Context->dbh;
121         my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
122         $sth->execute($id);
123         my $data=$sth->fetchrow_hashref;
124         $sth->finish;
125         $template->param(searchfield => $searchfield,
126                                                         Tvalue => $data->{'authorised_value'},
127                                                         id =>$id,
128                                                         );
129
130                                                                                                         # END $OP eq DELETE_CONFIRM
131 ################## DELETE_CONFIRMED ##################################
132 # called by delete_confirm, used to effectively confirm deletion of data in DB
133 } elsif ($op eq 'delete_confirmed') {
134         my $dbh = C4::Context->dbh;
135         my $sth=$dbh->prepare("delete from authorised_values where id=?");
136         $sth->execute($id);
137         $sth->finish;
138         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
139         exit;
140
141                                                                                                         # END $OP eq DELETE_CONFIRMED
142 ################## DEFAULT ##################################
143 } else { # DEFAULT
144         # build categories list
145         my $sth = $dbh->prepare("select distinct category from authorised_values");
146         $sth->execute;
147         my @category_list;
148         while ( my ($category) = $sth->fetchrow_array) {
149                 push(@category_list,$category);
150         }
151         # push koha system categories
152         my $tab_list = CGI::scrolling_list(-name=>'searchfield',
153                         -values=> \@category_list,
154                         -default=>"",
155                         -size=>1,
156                         -multiple=>0,
157                         );
158         if (!$searchfield) {
159                 $searchfield=$category_list[0];
160         }
161         my $env;
162         my ($count,$results)=StringSearch($env,$searchfield,'web');
163         my $toggle="white";
164         my @loop_data = ();
165         # builds value list
166         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
167                 if ($toggle eq 'white'){
168                         $toggle="#ffffcc";
169                 } else {
170                         $toggle="white";
171                 }
172                 my %row_data;  # get a fresh hash for the row data
173                 $row_data{category} = $results->[$i]{'category'};
174                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
175                 $row_data{lib} = $results->[$i]{'lib'};
176                 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
177                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
178                 push(@loop_data, \%row_data);
179         }
180
181         $template->param(loop => \@loop_data,
182                                                         tab_list => $tab_list,
183                                                         category => $searchfield);
184
185         if ($offset>0) {
186                 my $prevpage = $offset-$pagesize;
187                 $template->param(isprevpage => $offset,
188                                                 prevpage=> $prevpage,
189                                                 searchfield => $searchfield,
190                                                 script_name => $script_name,
191                  );
192         }
193         if ($offset+$pagesize<$count) {
194                 my $nextpage =$offset+$pagesize;
195                 $template->param(nextpage =>$nextpage,
196                                                 searchfield => $searchfield,
197                                                 script_name => $script_name,
198                 );
199         }
200 } #---- END $OP eq DEFAULT
201
202 output_html_with_http_headers $input, $cookie, $template->output;