fix for #577
[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=5;
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         } elsif ( ! $data->{'category'} ) {
94                 $template->param(action => "Add new category");
95         } else {
96                 $template->param(action => "Add authorised value");
97         }
98         $template->param(category => $data->{'category'},
99                                                         authorised_value => $data->{'authorised_value'},
100                                                         lib => $data->{'lib'},
101                                                         id => $data->{'id'}
102                                                         );
103         if ($data->{'category'}) {
104                 $template->param(category => "<input type=\"hidden\" name=\"category\" value='$data->{'category'}'>$data->{'category'}");
105         } else {
106                 $template->param(category => "<input type=text name=\"category\" size=8 maxlength=8>");
107         }
108 ################## ADD_VALIDATE ##################################
109 # called by add_form, used to insert/modify data in DB
110 } elsif ($op eq 'add_validate') {
111         my $dbh = C4::Context->dbh;
112         my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
113         my $lib = $input->param('lib');
114         undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
115         
116         $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $lib);
117         $sth->finish;
118         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
119         exit;
120 ################## DELETE_CONFIRM ##################################
121 # called by default form, used to confirm deletion of data in DB
122 } elsif ($op eq 'delete_confirm') {
123         my $dbh = C4::Context->dbh;
124         my $sth=$dbh->prepare($reqsel);
125         $sth->execute;
126         my $data=$sth->fetchrow_hashref;
127         $sth->finish;
128         $template->param(searchfield => $searchfield,
129                                                         Tvalue => $data->{'authorised_value'},
130                                                         id =>$id,
131                                                         );
132
133                                                                                                         # END $OP eq DELETE_CONFIRM
134 ################## DELETE_CONFIRMED ##################################
135 # called by delete_confirm, used to effectively confirm deletion of data in DB
136 } elsif ($op eq 'delete_confirmed') {
137         my $dbh = C4::Context->dbh;
138         my $sth=$dbh->prepare($reqdel);
139         $sth->execute;
140         $sth->finish;
141         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
142         exit;
143
144                                                                                                         # END $OP eq DELETE_CONFIRMED
145 ################## DEFAULT ##################################
146 } else { # DEFAULT
147         # build categories list
148         my $sth = $dbh->prepare("select distinct category from authorised_values");
149         $sth->execute;
150         my @category_list;
151         while ( my ($category) = $sth->fetchrow_array) {
152                 push(@category_list,$category);
153         }
154         # push koha system categories
155         my $tab_list = CGI::scrolling_list(-name=>'searchfield',
156                         -values=> \@category_list,
157                         -default=>"",
158                         -size=>1,
159                         -multiple=>0,
160                         );
161         if (!$searchfield) {
162                 $searchfield=$category_list[0];
163         }
164         my $env;
165         my ($count,$results)=StringSearch($env,$searchfield,'web');
166         my $toggle="white";
167         my @loop_data = ();
168         # builds value list
169         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
170                 if ($toggle eq 'white'){
171                         $toggle="#ffffcc";
172                 } else {
173                         $toggle="white";
174                 }
175                 my %row_data;  # get a fresh hash for the row data
176                 $row_data{category} = $results->[$i]{'category'};
177                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
178                 $row_data{lib} = $results->[$i]{'lib'};
179                 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
180                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
181                 push(@loop_data, \%row_data);
182         }
183
184         $template->param(loop => \@loop_data,
185                                                         tab_list => $tab_list,
186                                                         category => $searchfield);
187
188         if ($offset>0) {
189                 my $prevpage = $offset-$pagesize;
190                 $template->param(isprevpage => $offset,
191                                                 prevpage=> $prevpage,
192                                                 searchfield => $searchfield,
193                                                 script_name => $script_name,
194                  );
195         }
196         if ($offset+$pagesize<$count) {
197                 my $nextpage =$offset+$pagesize;
198                 $template->param(nextpage =>$nextpage,
199                                                 searchfield => $searchfield,
200                                                 script_name => $script_name,
201                 );
202         }
203 } #---- END $OP eq DEFAULT
204
205 output_html_with_http_headers $input, $cookie, $template->output;