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