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