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