fixes to enable truncation to play nicely with
[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
26 use C4::Context;
27
28
29 sub StringSearch  {
30         my ($searchstring,$type)=@_;
31         my $dbh = C4::Context->dbh;
32         $searchstring=~ s/\'/\\\'/g;
33         my @data=split(' ',$searchstring);
34         my $count=@data;
35         my $sth=$dbh->prepare("Select id,category,authorised_value,lib from authorised_values where (category like ?) order by category,authorised_value");
36         $sth->execute("$data[0]%");
37         my @results;
38         my $cnt=0;
39         while (my $data=$sth->fetchrow_hashref){
40         push(@results,$data);
41         $cnt ++;
42         }
43         $sth->finish;
44         return ($cnt,\@results);
45 }
46
47 my $input = new CGI;
48 my $searchfield=$input->param('searchfield');
49 $searchfield=~ s/\,//g;
50 my $id = $input->param('id');
51 my $offset=$input->param('offset');
52 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
53 my $dbh = C4::Context->dbh;
54
55 my ($template, $borrowernumber, $cookie)
56     = get_template_and_user({template_name => "admin/authorised_values.tmpl",
57                              query => $input,
58                              type => "intranet",
59                              authnotrequired => 0,
60                              flagsrequired => {parameters => 1},
61                              debug => 1,
62                              });
63 my $pagesize=20;
64 my $op = $input->param('op');
65
66 if ($op) {
67 $template->param(script_name => $script_name,
68                                                 $op              => 1); # we show only the TMPL_VAR names $op
69 } else {
70 $template->param(script_name => $script_name,
71                                                 else              => 1); # we show only the TMPL_VAR names $op
72 }
73 ################## ADD_FORM ##################################
74 # called by default. Used to create form to add or  modify a record
75 if ($op eq 'add_form') {
76         my $data;
77         if ($id) {
78                 my $dbh = C4::Context->dbh;
79                 my $sth=$dbh->prepare("select id,category,authorised_value,lib from authorised_values where id=?");
80                 $sth->execute($id);
81                 $data=$sth->fetchrow_hashref;
82                 $sth->finish;
83         } else {
84                 $data->{'category'} = $input->param('category');
85         }
86         if ($id) {
87                 $template->param(action_modify => 1);
88                 $template->param('heading-modify-authorized-value-p' => 1);
89         } elsif ( ! $data->{'category'} ) {
90                 $template->param(action_add_category => 1);
91                 $template->param('heading-add-new-category-p' => 1);
92         } else {
93                 $template->param(action_add_value => 1);
94                 $template->param('heading-add-authorized-value-p' => 1);
95         }
96         $template->param('use-heading-flags-p' => 1);
97         $template->param(category => $data->{'category'},
98                                                         authorised_value => $data->{'authorised_value'},
99                                                         lib => $data->{'lib'},
100                                                         id => $data->{'id'}
101                                                         );
102 ################## ADD_VALIDATE ##################################
103 # called by add_form, used to insert/modify data in DB
104 } elsif ($op eq 'add_validate') {
105         my $dbh = C4::Context->dbh;
106         my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
107         my $lib = $input->param('lib');
108         undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
109         
110         $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $lib);
111         $sth->finish;
112         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
113         exit;
114 ################## DELETE_CONFIRM ##################################
115 # called by default form, used to confirm deletion of data in DB
116 } elsif ($op eq 'delete_confirm') {
117         my $dbh = C4::Context->dbh;
118         my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
119         $sth->execute($id);
120         my $data=$sth->fetchrow_hashref;
121         $sth->finish;
122         $id = $input->param('id') unless $id;
123         $template->param(searchfield => $searchfield,
124                                                         Tlib => $data->{'lib'},
125                                                         Tvalue => $data->{'authorised_value'},
126                                                         id =>$id,
127                                                         );
128
129                                                                                                         # END $OP eq DELETE_CONFIRM
130 ################## DELETE_CONFIRMED ##################################
131 # called by delete_confirm, used to effectively confirm deletion of data in DB
132 } elsif ($op eq 'delete_confirmed') {
133         my $dbh = C4::Context->dbh;
134         my $id = $input->param('id');
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                 -id=>'searchfield',
154                         -values=> \@category_list,
155                         -default=>"",
156                         -size=>1,
157                         -tabindex=>'',
158                         -multiple=>0,
159                         );
160         if (!$searchfield) {
161                 $searchfield=$category_list[0];
162         }
163         my ($count,$results)=StringSearch($searchfield,'web');
164         my $toggle=1;
165         my @loop_data = ();
166         # builds value list
167         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
168                 if ($toggle eq 1){
169                         $toggle=1;
170                 } else {
171                         $toggle=0;
172                 }
173                 my %row_data;  # get a fresh hash for the row data
174                 $row_data{category} = $results->[$i]{'category'};
175                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
176                 $row_data{lib} = $results->[$i]{'lib'};
177                 $row_data{edit} = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'};
178                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'};
179                 push(@loop_data, \%row_data);
180         }
181
182         $template->param(loop => \@loop_data,
183                                                         tab_list => $tab_list,
184                                                         category => $searchfield);
185
186         if ($offset>0) {
187                 my $prevpage = $offset-$pagesize;
188                 $template->param(isprevpage => $offset,
189                                                 prevpage=> $prevpage,
190                                                 searchfield => $searchfield,
191                                                 script_name => $script_name,
192                  );
193         }
194         if ($offset+$pagesize<$count) {
195                 my $nextpage =$offset+$pagesize;
196                 $template->param(nextpage =>$nextpage,
197                                                 searchfield => $searchfield,
198                                                 script_name => $script_name,
199                 );
200         }
201 } #---- END $OP eq DEFAULT
202 output_html_with_http_headers $input, $cookie, $template->output;