Merge git://git.koha.org/pub/scm/koha
[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 AuthorizedValuesForCategory  {
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 = ?) 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 sub _already_exists {
48     my ($category, $authorised_value) = @_;
49     my $dbh = C4::Context->dbh;
50     my $sth = $dbh->prepare_cached("SELECT COUNT(*) FROM authorised_values
51                                     WHERE category = ?
52                                     AND authorised_value = ?");
53     $sth->execute($category, $authorised_value);
54     my ($count) = $sth->fetchrow_array();
55     $sth->finish();
56     return $count;
57 }
58
59 my $input = new CGI;
60 my $searchfield=$input->param('searchfield');
61 $searchfield=~ s/\,//g;
62 my $id = $input->param('id');
63 my $offset=$input->param('offset');
64 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
65 my $dbh = C4::Context->dbh;
66
67 my ($template, $borrowernumber, $cookie)
68     = get_template_and_user({template_name => "admin/authorised_values.tmpl",
69                              query => $input,
70                              type => "intranet",
71                              authnotrequired => 0,
72                              flagsrequired => {parameters => 1},
73                              debug => 1,
74                              });
75 my $pagesize=20;
76 my $op = $input->param('op');
77
78 if ($op) {
79 $template->param(script_name => $script_name,
80                                                 $op              => 1); # we show only the TMPL_VAR names $op
81 } else {
82 $template->param(script_name => $script_name,
83                                                 else              => 1); # we show only the TMPL_VAR names $op
84 }
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or  modify a record
87 if ($op eq 'add_form') {
88         my $data;
89         if ($id) {
90                 my $dbh = C4::Context->dbh;
91                 my $sth=$dbh->prepare("select id,category,authorised_value,lib from authorised_values where id=?");
92                 $sth->execute($id);
93                 $data=$sth->fetchrow_hashref;
94                 $sth->finish;
95         } else {
96                 $data->{'category'} = $input->param('category');
97         }
98         if ($id) {
99                 $template->param(action_modify => 1);
100                 $template->param('heading-modify-authorized-value-p' => 1);
101         } elsif ( ! $data->{'category'} ) {
102                 $template->param(action_add_category => 1);
103                 $template->param('heading-add-new-category-p' => 1);
104         } else {
105                 $template->param(action_add_value => 1);
106                 $template->param('heading-add-authorized-value-p' => 1);
107         }
108         $template->param('use-heading-flags-p' => 1);
109         $template->param(category => $data->{'category'},
110                                                         authorised_value => $data->{'authorised_value'},
111                                                         lib => $data->{'lib'},
112                                                         id => $data->{'id'}
113                                                         );
114 ################## ADD_VALIDATE ##################################
115 # called by add_form, used to insert/modify data in DB
116 } elsif ($op eq 'add_validate') {
117         my $dbh = C4::Context->dbh;
118
119     if (_already_exists($input->param('category'), $input->param('authorised_value'))) {
120      if ($id){
121       my $sth=$dbh->prepare("UPDATE authorised_values SET category=?,authorised_value=?,lib=? where id=?");
122       my $lib = $input->param('lib');
123       undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
124   
125       $sth->execute($input->param('category'), $input->param('authorised_value'), $lib,$input->param('id'));          
126       print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
127       exit;
128      } else {       
129         $template->param(duplicate_category => $input->param('category'),
130                          duplicate_value =>  $input->param('authorised_value'),
131                          else => 1);
132         default_form();
133      }           
134     } else {
135             my $sth=$dbh->prepare("INSERT INTO authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
136             my $lib = $input->param('lib');
137             undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
138         
139             $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $lib);
140             $sth->finish;
141             print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
142             exit;
143     }
144 ################## DELETE_CONFIRM ##################################
145 # called by default form, used to confirm deletion of data in DB
146 } elsif ($op eq 'delete_confirm') {
147         my $dbh = C4::Context->dbh;
148         my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
149         $sth->execute($id);
150         my $data=$sth->fetchrow_hashref;
151         $sth->finish;
152         $id = $input->param('id') unless $id;
153         $template->param(searchfield => $searchfield,
154                                                         Tlib => $data->{'lib'},
155                                                         Tvalue => $data->{'authorised_value'},
156                                                         id =>$id,
157                                                         );
158
159                                                                                                         # END $OP eq DELETE_CONFIRM
160 ################## DELETE_CONFIRMED ##################################
161 # called by delete_confirm, used to effectively confirm deletion of data in DB
162 } elsif ($op eq 'delete_confirmed') {
163         my $dbh = C4::Context->dbh;
164         my $id = $input->param('id');
165         my $sth=$dbh->prepare("delete from authorised_values where id=?");
166         $sth->execute($id);
167         $sth->finish;
168         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
169         exit;
170
171                                                                                                         # END $OP eq DELETE_CONFIRMED
172 ################## DEFAULT ##################################
173 } else { # DEFAULT
174     default_form();
175 } #---- END $OP eq DEFAULT
176 output_html_with_http_headers $input, $cookie, $template->output;
177
178 exit 0;
179
180 sub default_form {
181         # build categories list
182         my $sth = $dbh->prepare("select distinct category from authorised_values");
183         $sth->execute;
184         # the list
185         my @category_list;
186         # a hash, to check that some hardcoded categories exist.
187         my %categories;
188         while ( my ($category) = $sth->fetchrow_array) {
189                 push(@category_list,$category);
190                 $categories{$category} = 1;
191         }
192         # push koha system categories
193         push @category_list, 'Asort1' unless $categories{'Asort1'};
194         push @category_list, 'Asort2' unless $categories{'Asort2'};
195         push @category_list, 'Bsort1' unless $categories{'Bsort1'};
196         push @category_list, 'Bsort2' unless $categories{'Bsort2'};
197         push @category_list, 'SUGGEST' unless $categories{'SUGGEST'};
198         push @category_list, 'DAMAGED' unless $categories{'DAMAGED'};
199         push @category_list, 'LOST' unless $categories{'LOST'};
200         #reorder the list
201         @category_list = sort {$a cmp $b} @category_list;
202         my $tab_list = CGI::scrolling_list(-name=>'searchfield',
203                 -id=>'searchfield',
204                         -values=> \@category_list,
205                         -default=>"",
206                         -size=>1,
207                         -tabindex=>'',
208                         -multiple=>0,
209                         );
210         if (!$searchfield) {
211                 $searchfield=$category_list[0];
212         }
213         my ($count,$results)=AuthorizedValuesForCategory($searchfield,'web');
214         my $toggle=1;
215         my @loop_data = ();
216         # builds value list
217         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
218                 if ($toggle eq 1){
219                         $toggle=1;
220                 } else {
221                         $toggle=0;
222                 }
223                 my %row_data;  # get a fresh hash for the row data
224                 $row_data{category} = $results->[$i]{'category'};
225                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
226                 $row_data{lib} = $results->[$i]{'lib'};
227                 $row_data{edit} = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'};
228                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'};
229                 push(@loop_data, \%row_data);
230         }
231
232         $template->param(loop => \@loop_data,
233                                                         tab_list => $tab_list,
234                                                         category => $searchfield);
235
236         if ($offset>0) {
237                 my $prevpage = $offset-$pagesize;
238                 $template->param(isprevpage => $offset,
239                                                 prevpage=> $prevpage,
240                                                 searchfield => $searchfield,
241                                                 script_name => $script_name,
242                  );
243         }
244         if ($offset+$pagesize<$count) {
245                 my $nextpage =$offset+$pagesize;
246                 $template->param(nextpage =>$nextpage,
247                                                 searchfield => $searchfield,
248                                                 script_name => $script_name,
249                 );
250         }
251 }
252