Bug 5480 Some usual UNIMARC cataloguing plugins doesn't work anymore
[koha.git] / admin / authorised_values.pl
blobde055121956da0956b861a37272b27c483254f48
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Output;
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({});
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;
55 my ($template, $borrowernumber, $cookie)= get_template_and_user({
56 template_name => "admin/authorised_values.tmpl",
57 authnotrequired => 0,
58 flagsrequired => {parameters => 1},
59 query => $input,
60 type => "intranet",
61 debug => 1,
62 });
63 my $pagesize = 20;
65 $template->param( script_name => $script_name,
66 ($op||'else') => 1 );
67 ################## ADD_FORM ##################################
68 # called by default. Used to create form to add or modify a record
69 if ($op eq 'add_form') {
70 my $data;
71 if ($id) {
72 my $sth=$dbh->prepare("select id, category, authorised_value, lib, lib_opac, imageurl from authorised_values where id=?");
73 $sth->execute($id);
74 $data=$sth->fetchrow_hashref;
75 } else {
76 $data->{'category'} = $input->param('category');
78 if ($id) {
79 $template->param(action_modify => 1);
80 $template->param('heading-modify-authorized-value-p' => 1);
81 } elsif ( ! $data->{'category'} ) {
82 $template->param(action_add_category => 1);
83 $template->param('heading-add-new-category-p' => 1);
84 } else {
85 $template->param(action_add_value => 1);
86 $template->param('heading-add-authorized-value-p' => 1);
88 $template->param('use-heading-flags-p' => 1);
89 $template->param( category => $data->{'category'},
90 authorised_value => $data->{'authorised_value'},
91 lib => $data->{'lib'},
92 lib_opac => $data->{'lib_opac'},
93 id => $data->{'id'},
94 imagesets => C4::Koha::getImageSets( checked => $data->{'imageurl'} ),
95 offset => $offset,
98 ################## ADD_VALIDATE ##################################
99 # called by add_form, used to insert/modify data in DB
100 } elsif ($op eq 'add_validate') {
101 my $new_authorised_value = $input->param('authorised_value');
102 my $new_category = $input->param('category');
103 my $imageurl = $input->param( 'imageurl' ) || '';
104 $imageurl = '' if $imageurl =~ /removeImage/;
105 my $duplicate_entry = 0;
107 if ( $id ) { # Update
108 my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
109 $sth->execute();
110 my ($category, $authorised_value) = $sth->fetchrow_array();
111 if ( $authorised_value ne $new_authorised_value ) {
112 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
113 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
114 $sth->execute();
115 ($duplicate_entry) = $sth->fetchrow_array();
116 warn "**** duplicate_entry = $duplicate_entry";
118 unless ( $duplicate_entry ) {
119 my $sth=$dbh->prepare( 'UPDATE authorised_values
120 SET category = ?,
121 authorised_value = ?,
122 lib = ?,
123 lib_opac = ?,
124 imageurl = ?
125 WHERE id=?' );
126 my $lib = $input->param('lib');
127 my $lib_opac = $input->param('lib_opac');
128 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
129 undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
130 $sth->execute($new_category, $new_authorised_value, $lib, $lib_opac, $imageurl, $id);
131 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."&offset=$offset\"></html>";
132 exit;
135 else { # Insert
136 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
137 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
138 $sth->execute();
139 ($duplicate_entry) = $sth->fetchrow_array();
140 unless ( $duplicate_entry ) {
141 my $sth=$dbh->prepare( 'INSERT INTO authorised_values
142 ( id, category, authorised_value, lib, lib_opac, imageurl )
143 values (?, ?, ?, ?, ?, ?)' );
144 my $lib = $input->param('lib');
145 my $lib_opac = $input->param('lib_opac');
146 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
147 undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
148 $sth->execute($id, $new_category, $new_authorised_value, $lib, $lib_opac, $imageurl );
149 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."&offset=$offset\"></html>";
150 exit;
153 if ( $duplicate_entry ) {
154 $template->param(duplicate_category => $new_category,
155 duplicate_value => $new_authorised_value,
156 else => 1);
157 default_form();
160 ################## DELETE_CONFIRM ##################################
161 # called by default form, used to confirm deletion of data in DB
162 } elsif ($op eq 'delete_confirm') {
163 my $sth=$dbh->prepare("select category,authorised_value,lib,lib_opac from authorised_values where id=?");
164 $sth->execute($id);
165 my $data=$sth->fetchrow_hashref;
166 $id = $input->param('id') unless $id;
167 $template->param(searchfield => $searchfield,
168 Tlib => $data->{'lib'},
169 Tlib_opac => $data->{'lib_opac'},
170 Tvalue => $data->{'authorised_value'},
171 id =>$id,
174 # END $OP eq DELETE_CONFIRM
175 ################## DELETE_CONFIRMED ##################################
176 # called by delete_confirm, used to effectively confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirmed') {
178 my $id = $input->param('id');
179 my $sth=$dbh->prepare("delete from authorised_values where id=?");
180 $sth->execute($id);
181 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield&offset=$offset\"></html>";
182 exit;
183 # END $OP eq DELETE_CONFIRMED
184 ################## DEFAULT ##################################
185 } else { # DEFAULT
186 default_form();
187 } #---- END $OP eq DEFAULT
188 output_html_with_http_headers $input, $cookie, $template->output;
190 exit 0;
192 sub default_form {
193 # build categories list
194 my $sth = $dbh->prepare("select distinct category from authorised_values");
195 $sth->execute;
196 my @category_list;
197 my %categories; # a hash, to check that some hardcoded categories exist.
198 while ( my ($category) = $sth->fetchrow_array) {
199 push(@category_list,$category);
200 $categories{$category} = 1;
202 # push koha system categories
203 foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST)) {
204 push @category_list, $_ unless $categories{$_};
207 #reorder the list
208 @category_list = sort {$a cmp $b} @category_list;
209 my $tab_list = CGI::scrolling_list(-name=>'searchfield',
210 -id=>'searchfield',
211 -values=> \@category_list,
212 -default=>"",
213 -size=>1,
214 -multiple=>0,
216 if (!$searchfield) {
217 $searchfield=$category_list[0];
219 my ($results) = AuthorizedValuesForCategory($searchfield);
220 my $count = scalar(@$results);
221 my @loop_data = ();
222 # builds value list
223 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
224 my %row_data; # get a fresh hash for the row data
225 $row_data{category} = $results->[$i]{'category'};
226 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
227 $row_data{lib} = $results->[$i]{'lib'};
228 $row_data{lib_opac} = $results->[$i]{'lib_opac'};
229 $row_data{imageurl} = getitemtypeimagelocation( 'intranet', $results->[$i]{'imageurl'} );
230 $row_data{edit} = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'}."&amp;offset=$offset";
231 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'}."&amp;offset=$offset";
232 push(@loop_data, \%row_data);
235 $template->param( loop => \@loop_data,
236 tab_list => $tab_list,
237 category => $searchfield );
239 if ($offset>0) {
240 my $prevpage = $offset-$pagesize;
241 $template->param(isprevpage => $offset,
242 prevpage=> $prevpage,
243 searchfield => $searchfield,
246 if ($offset+$pagesize<$count) {
247 my $nextpage =$offset+$pagesize;
248 $template->param(nextpage =>$nextpage,
249 searchfield => $searchfield,