Markup corrections, style fixes, and other minor bugs, including fix for Bug 2679...
[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::Koha;
25 use C4::Output;
26
27
28 sub AuthorizedValuesForCategory  {
29     my ($searchstring,$type)=@_;
30     my $dbh = C4::Context->dbh;
31     $searchstring=~ s/\'/\\\'/g;
32     my @data=split(' ',$searchstring);
33     my $count=@data;
34     my $sth=$dbh->prepare( 'Select id, category, authorised_value, lib, imageurl
35                               from authorised_values
36                               where (category = ?)
37                               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 => "admin/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, imageurl 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 ($id) {
89                 $template->param(action_modify => 1);
90                 $template->param('heading-modify-authorized-value-p' => 1);
91         } elsif ( ! $data->{'category'} ) {
92                 $template->param(action_add_category => 1);
93                 $template->param('heading-add-new-category-p' => 1);
94         } else {
95                 $template->param(action_add_value => 1);
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                          imagesets        => C4::Koha::getImageSets( checked => $data->{'imageurl'} )
104                      );
105                           
106 ################## ADD_VALIDATE ##################################
107 # called by add_form, used to insert/modify data in DB
108 } elsif ($op eq 'add_validate') {
109         my $dbh = C4::Context->dbh;
110     my $new_category = $input->param('category');
111     my $new_authorised_value = $input->param('authorised_value');
112     my $imageurl=$input->param( 'imageurl' )|'';   
113         $imageurl = '' if $imageurl =~ /removeImage/;
114     my $duplicate_entry = 0;
115
116     if ( $id ) { # Update
117         my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
118         $sth->execute();
119         my ($category, $authorised_value) = $sth->fetchrow_array();
120         $sth->finish;
121         if ( $authorised_value ne $new_authorised_value ) {
122             my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
123                 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
124             $sth->execute();
125             ($duplicate_entry) = $sth->fetchrow_array();
126             warn "**** duplicate_entry = $duplicate_entry";
127         }
128         unless ( $duplicate_entry ) {
129             my $sth=$dbh->prepare( 'UPDATE authorised_values
130                                       SET category         = ?,
131                                           authorised_value = ?,
132                                           lib              = ?,
133                                           imageurl         = ?
134                                       WHERE id=?' );
135             my $lib = $input->param('lib');
136             undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
137             $sth->execute($new_category, $new_authorised_value, $lib, $imageurl, $id);          
138             print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."\"></html>";
139             exit;
140         }
141     }
142     else { # Insert
143         my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
144             "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
145         $sth->execute();
146         ($duplicate_entry) = $sth->fetchrow_array();
147         $sth->finish();
148         unless ( $duplicate_entry ) {
149             my $sth=$dbh->prepare( 'INSERT INTO authorised_values
150                                     ( id, category, authorised_value, lib, imageurl )
151                                     values (?, ?, ?, ?, ?)' );
152             my $lib = $input->param('lib');
153             undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
154             $sth->execute($id, $new_category, $new_authorised_value, $lib, $imageurl );
155             $sth->finish;
156             print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
157             exit;
158         }
159     }
160     if ( $duplicate_entry ) {       
161         $template->param(duplicate_category => $new_category,
162                          duplicate_value =>  $new_authorised_value,
163                          else => 1);
164         default_form();
165      }           
166         
167 ################## DELETE_CONFIRM ##################################
168 # called by default form, used to confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirm') {
170         my $dbh = C4::Context->dbh;
171         my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
172         $sth->execute($id);
173         my $data=$sth->fetchrow_hashref;
174         $sth->finish;
175         $id = $input->param('id') unless $id;
176         $template->param(searchfield => $searchfield,
177                                                         Tlib => $data->{'lib'},
178                                                         Tvalue => $data->{'authorised_value'},
179                                                         id =>$id,
180                                                         );
181
182                                                                                                         # END $OP eq DELETE_CONFIRM
183 ################## DELETE_CONFIRMED ##################################
184 # called by delete_confirm, used to effectively confirm deletion of data in DB
185 } elsif ($op eq 'delete_confirmed') {
186         my $dbh = C4::Context->dbh;
187         my $id = $input->param('id');
188         my $sth=$dbh->prepare("delete from authorised_values where id=?");
189         $sth->execute($id);
190         $sth->finish;
191         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
192         exit;
193
194                                                                                                         # END $OP eq DELETE_CONFIRMED
195 ################## DEFAULT ##################################
196 } else { # DEFAULT
197     default_form();
198 } #---- END $OP eq DEFAULT
199 output_html_with_http_headers $input, $cookie, $template->output;
200
201 exit 0;
202
203 sub default_form {
204         # build categories list
205         my $sth = $dbh->prepare("select distinct category from authorised_values");
206         $sth->execute;
207         # the list
208         my @category_list;
209         # a hash, to check that some hardcoded categories exist.
210         my %categories;
211         while ( my ($category) = $sth->fetchrow_array) {
212                 push(@category_list,$category);
213                 $categories{$category} = 1;
214         }
215         # push koha system categories
216         push @category_list, 'Asort1' unless $categories{'Asort1'};
217         push @category_list, 'Asort2' unless $categories{'Asort2'};
218         push @category_list, 'Bsort1' unless $categories{'Bsort1'};
219         push @category_list, 'Bsort2' unless $categories{'Bsort2'};
220         push @category_list, 'SUGGEST' unless $categories{'SUGGEST'};
221         push @category_list, 'DAMAGED' unless $categories{'DAMAGED'};
222         push @category_list, 'LOST' unless $categories{'LOST'};
223         #reorder the list
224         @category_list = sort {$a cmp $b} @category_list;
225         my $tab_list = CGI::scrolling_list(-name=>'searchfield',
226                 -id=>'searchfield',
227                         -values=> \@category_list,
228                         -default=>"",
229                         -size=>1,
230                         -multiple=>0,
231                         );
232         if (!$searchfield) {
233                 $searchfield=$category_list[0];
234         }
235         my ($count,$results)=AuthorizedValuesForCategory($searchfield,'web');
236         my $toggle=1;
237         my @loop_data = ();
238         # builds value list
239         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
240                 if ($toggle eq 1){
241                         $toggle=1;
242                 } else {
243                         $toggle=0;
244                 }
245                 my %row_data;  # get a fresh hash for the row data
246                 $row_data{category}         = $results->[$i]{'category'};
247                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
248                 $row_data{lib}              = $results->[$i]{'lib'};
249                 $row_data{imageurl}         = getitemtypeimagelocation( 'intranet', $results->[$i]{'imageurl'} );
250                 $row_data{edit}             = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'};
251                 $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'};
252                 push(@loop_data, \%row_data);
253         }
254
255         $template->param( loop     => \@loop_data,
256                           tab_list => $tab_list,
257                           category => $searchfield );
258
259         if ($offset>0) {
260                 my $prevpage = $offset-$pagesize;
261                 $template->param(isprevpage => $offset,
262                                                 prevpage=> $prevpage,
263                                                 searchfield => $searchfield,
264                                                 script_name => $script_name,
265                  );
266         }
267         if ($offset+$pagesize<$count) {
268                 my $nextpage =$offset+$pagesize;
269                 $template->param(nextpage =>$nextpage,
270                                                 searchfield => $searchfield,
271                                                 script_name => $script_name,
272                 );
273         }
274 }
275