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