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
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.
31 sub AuthorizedValuesForCategory
{
32 my ($searchstring) = shift or return;
33 my $dbh = C4
::Context
->dbh;
34 $searchstring=~ s/\'/\\\'/g;
35 my @data=split(' ',$searchstring);
36 my $sth=$dbh->prepare('
37 SELECT id, category, authorised_value, lib, lib_opac, imageurl
38 FROM authorised_values
40 ORDER BY category, authorised_value
42 $sth->execute("$data[0]");
43 return $sth->fetchall_arrayref({});
47 my $id = $input->param('id');
48 my $op = $input->param('op') || '';
49 our $offset = $input->param('offset') || 0;
50 our $searchfield = $input->param('searchfield');
51 $searchfield = '' unless defined $searchfield;
52 $searchfield =~ s/\,//g;
53 our $script_name = "/cgi-bin/koha/admin/authorised_values.pl";
54 our $dbh = C4
::Context
->dbh;
56 our ($template, $borrowernumber, $cookie)= get_template_and_user
({
57 template_name
=> "admin/authorised_values.tmpl",
59 flagsrequired
=> {parameters
=> 'parameters_remaining_permissions'},
65 $template->param( script_name
=> $script_name,
67 ################## ADD_FORM ##################################
68 # called by default. Used to create form to add or modify a record
69 if ($op eq 'add_form') {
71 my @selected_branches;
73 my $sth=$dbh->prepare("select id, category, authorised_value, lib, lib_opac, imageurl from authorised_values where id=?");
75 $data=$sth->fetchrow_hashref;
76 $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM authorised_values_branches AS avb, branches AS b WHERE avb.branchcode = b.branchcode AND avb.av_id = ?;");
78 while ( my $branch = $sth->fetchrow_hashref ) {
79 push @selected_branches, $branch;
82 $data->{'category'} = $input->param('category');
85 my $branches = GetBranches
;
88 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname
}) cmp uc($branches->{$b}->{branchname
}) } keys %$branches ) {
89 my $selected = ( grep {$_->{branchcode
} eq $branchcode} @selected_branches ) ?
1 : 0;
90 push @branches_loop, {
91 branchcode
=> $branchcode,
92 branchname
=> $branches->{$branchcode}->{branchname
},
93 selected
=> $selected,
98 $template->param(action_modify
=> 1);
99 $template->param('heading_modify_authorized_value_p' => 1);
100 } elsif ( ! $data->{'category'} ) {
101 $template->param(action_add_category
=> 1);
102 $template->param('heading_add_new_category_p' => 1);
104 $template->param(action_add_value
=> 1);
105 $template->param('heading_add_authorized_value_p' => 1);
107 $template->param('use_heading_flags_p' => 1);
108 $template->param( category
=> $data->{'category'},
109 authorised_value
=> $data->{'authorised_value'},
110 lib
=> $data->{'lib'},
111 lib_opac
=> $data->{'lib_opac'},
113 imagesets
=> C4
::Koha
::getImageSets
( checked
=> $data->{'imageurl'} ),
115 branches_loop
=> \
@branches_loop,
118 ################## ADD_VALIDATE ##################################
119 # called by add_form, used to insert/modify data in DB
120 } elsif ($op eq 'add_validate') {
121 my $new_authorised_value = $input->param('authorised_value');
122 my $new_category = $input->param('category');
123 my $imageurl = $input->param( 'imageurl' ) || '';
124 $imageurl = '' if $imageurl =~ /removeImage/;
125 my $duplicate_entry = 0;
126 my @branches = $input->param('branches');
128 if ( $id ) { # Update
129 my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id = ? ");
131 my ($category, $authorised_value) = $sth->fetchrow_array();
132 if ( $authorised_value ne $new_authorised_value ) {
133 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
134 "WHERE category = ? AND authorised_value = ? and id <> ? ");
135 $sth->execute($new_category, $new_authorised_value, $id);
136 ($duplicate_entry) = $sth->fetchrow_array();
138 unless ( $duplicate_entry ) {
139 my $sth=$dbh->prepare( 'UPDATE authorised_values
141 authorised_value = ?,
146 my $lib = $input->param('lib');
147 my $lib_opac = $input->param('lib_opac');
148 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
149 undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
150 $sth->execute($new_category, $new_authorised_value, $lib, $lib_opac, $imageurl, $id);
152 $sth = $dbh->prepare("DELETE FROM authorised_values_branches WHERE av_id = ?");
153 $sth->execute( $id );
154 $sth = $dbh->prepare(
155 "INSERT INTO authorised_values_branches
156 ( av_id, branchcode )
159 for my $branchcode ( @branches ) {
160 next if not $branchcode;
161 $sth->execute($id, $branchcode);
165 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."&offset=$offset\"></html>";
170 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
171 "WHERE category = ? AND authorised_value = ? ");
172 $sth->execute($new_category, $new_authorised_value);
173 ($duplicate_entry) = $sth->fetchrow_array();
174 unless ( $duplicate_entry ) {
175 my $sth=$dbh->prepare( 'INSERT INTO authorised_values
176 ( category, authorised_value, lib, lib_opac, imageurl )
177 values (?, ?, ?, ?, ?)' );
178 my $lib = $input->param('lib');
179 my $lib_opac = $input->param('lib_opac');
180 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
181 undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
182 $sth->execute( $new_category, $new_authorised_value, $lib, $lib_opac, $imageurl );
183 $id = $dbh->{'mysql_insertid'};
185 $sth = $dbh->prepare(
186 "INSERT INTO authorised_values_branches
187 ( av_id, branchcode )
190 for my $branchcode ( @branches ) {
191 next if not $branchcode;
192 $sth->execute($id, $branchcode);
195 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."&offset=$offset\"></html>";
199 if ( $duplicate_entry ) {
200 $template->param(duplicate_category
=> $new_category,
201 duplicate_value
=> $new_authorised_value,
206 ################## DELETE_CONFIRM ##################################
207 # called by default form, used to confirm deletion of data in DB
208 } elsif ($op eq 'delete_confirm') {
209 my $sth=$dbh->prepare("select category,authorised_value,lib,lib_opac from authorised_values where id=?");
211 my $data=$sth->fetchrow_hashref;
212 $id = $input->param('id') unless $id;
213 $template->param(searchfield
=> $searchfield,
214 Tlib
=> $data->{'lib'},
215 Tlib_opac
=> $data->{'lib_opac'},
216 Tvalue
=> $data->{'authorised_value'},
220 # END $OP eq DELETE_CONFIRM
221 ################## DELETE_CONFIRMED ##################################
222 # called by delete_confirm, used to effectively confirm deletion of data in DB
223 } elsif ($op eq 'delete_confirmed') {
224 my $id = $input->param('id');
225 my $sth=$dbh->prepare("delete from authorised_values where id=?");
227 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield&offset=$offset\"></html>";
229 # END $OP eq DELETE_CONFIRMED
230 ################## DEFAULT ##################################
233 } #---- END $OP eq DEFAULT
234 output_html_with_http_headers
$input, $cookie, $template->output;
239 # build categories list
240 my $sth = $dbh->prepare("select distinct category from authorised_values");
243 my %categories; # a hash, to check that some hardcoded categories exist.
244 while ( my ($category) = $sth->fetchrow_array ) {
245 push( @category_list, $category );
246 $categories{$category} = 1;
249 # push koha system categories
250 foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST REPORT_GROUP REPORT_SUBGROUP DEPARTMENT TERM SUGGEST_STATUS)) {
251 push @category_list, $_ unless $categories{$_};
255 @category_list = sort {$a cmp $b} @category_list;
256 my $tab_list = CGI
::scrolling_list
(-name
=>'searchfield',
258 -values=> \
@category_list,
264 $searchfield=$category_list[0];
266 my ($results) = AuthorizedValuesForCategory
($searchfield);
267 my $count = scalar(@
$results);
270 my $dbh = C4
::Context
->dbh;
271 $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM authorised_values_branches AS avb, branches AS b WHERE avb.branchcode = b.branchcode AND avb.av_id = ?");
272 for (my $i=0; $i < $count; $i++){
273 $sth->execute( $results->[$i]{id
} );
274 my @selected_branches;
275 while ( my $branch = $sth->fetchrow_hashref ) {
276 push @selected_branches, $branch;
278 my %row_data; # get a fresh hash for the row data
279 $row_data{category
} = $results->[$i]{'category'};
280 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
281 $row_data{lib
} = $results->[$i]{'lib'};
282 $row_data{lib_opac
} = $results->[$i]{'lib_opac'};
283 $row_data{imageurl
} = getitemtypeimagelocation
( 'intranet', $results->[$i]{'imageurl'} );
284 $row_data{edit
} = "$script_name?op=add_form&id=".$results->[$i]{'id'}."&offset=$offset";
285 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'}."&offset=$offset";
286 $row_data{branches
} = \
@selected_branches;
287 push(@loop_data, \
%row_data);
290 $template->param( loop => \
@loop_data,
291 tab_list
=> $tab_list,
292 category
=> $searchfield );