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