synch fr & en
[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         if ($data->{'category'}) {
103                 $template->param(category => "<input type=\"hidden\" name=\"category\" value='$data->{'category'}'>$data->{'category'}");
104         } else {
105                 $template->param(category => "<input type=text name=\"category\" size=8 maxlength=8>");
106         }
107 ################## ADD_VALIDATE ##################################
108 # called by add_form, used to insert/modify data in DB
109 } elsif ($op eq 'add_validate') {
110         my $dbh = C4::Context->dbh;
111         my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
112         my $lib = $input->param('lib');
113         undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
114         
115         $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $lib);
116         $sth->finish;
117         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
118         exit;
119 ################## DELETE_CONFIRM ##################################
120 # called by default form, used to confirm deletion of data in DB
121 } elsif ($op eq 'delete_confirm') {
122         my $dbh = C4::Context->dbh;
123         my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
124         $sth->execute($id);
125         my $data=$sth->fetchrow_hashref;
126         $sth->finish;
127         $id = $input->param('id') unless $id;
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 $id = $input->param('id');
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                         -tabindex=>'',
161                         -multiple=>0,
162                         );
163         if (!$searchfield) {
164                 $searchfield=$category_list[0];
165         }
166         my ($count,$results)=StringSearch($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 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
206                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
207                 IntranetNav => C4::Context->preference("IntranetNav"),
208                 );
209 output_html_with_http_headers $input, $cookie, $template->output;