Lines 1-6
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
# Copyright 2000-2002 Katipo Communications |
3 |
# Copyright 2000-2002 Katipo Communications |
|
|
4 |
# Copyright 2013 BibLibre |
4 |
# |
5 |
# |
5 |
# This file is part of Koha. |
6 |
# This file is part of Koha. |
6 |
# |
7 |
# |
Lines 17-24
Link Here
|
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
|
20 |
use strict; |
21 |
use Modern::Perl; |
21 |
use warnings; |
|
|
22 |
|
22 |
|
23 |
use CGI; |
23 |
use CGI; |
24 |
use C4::Auth; |
24 |
use C4::Auth; |
Lines 26-60
use C4::Branch;
Link Here
|
26 |
use C4::Context; |
26 |
use C4::Context; |
27 |
use C4::Koha; |
27 |
use C4::Koha; |
28 |
use C4::Output; |
28 |
use C4::Output; |
|
|
29 |
use Koha::AuthorisedValue; |
30 |
use Koha::AuthorisedValues; |
29 |
|
31 |
|
30 |
|
32 |
|
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 |
39 |
WHERE (category = ?) |
40 |
ORDER BY category, authorised_value |
41 |
'); |
42 |
$sth->execute("$data[0]"); |
43 |
return $sth->fetchall_arrayref({}); |
44 |
} |
45 |
|
46 |
my $input = new CGI; |
33 |
my $input = new CGI; |
47 |
my $id = $input->param('id'); |
34 |
my $id = $input->param('id'); |
48 |
my $op = $input->param('op') || ''; |
35 |
my $op = $input->param('op') || 'list'; |
49 |
our $offset = $input->param('offset') || 0; |
36 |
my $searchfield = $input->param('searchfield'); |
50 |
our $searchfield = $input->param('searchfield'); |
|
|
51 |
$searchfield = '' unless defined $searchfield; |
37 |
$searchfield = '' unless defined $searchfield; |
52 |
$searchfield =~ s/\,//g; |
38 |
$searchfield =~ s/\,//g; |
53 |
our $script_name = "/cgi-bin/koha/admin/authorised_values.pl"; |
39 |
my @messages; |
54 |
our $dbh = C4::Context->dbh; |
|
|
55 |
|
40 |
|
56 |
our ($template, $borrowernumber, $cookie)= get_template_and_user({ |
41 |
our ($template, $borrowernumber, $cookie)= get_template_and_user({ |
57 |
template_name => "admin/authorised_values.tmpl", |
42 |
template_name => "admin/authorised_values.tt", |
58 |
authnotrequired => 0, |
43 |
authnotrequired => 0, |
59 |
flagsrequired => {parameters => 'parameters_remaining_permissions'}, |
44 |
flagsrequired => {parameters => 'parameters_remaining_permissions'}, |
60 |
query => $input, |
45 |
query => $input, |
Lines 62-92
our ($template, $borrowernumber, $cookie)= get_template_and_user({
Link Here
|
62 |
debug => 1, |
47 |
debug => 1, |
63 |
}); |
48 |
}); |
64 |
|
49 |
|
65 |
$template->param( script_name => $script_name, |
|
|
66 |
($op||'else') => 1 ); |
67 |
################## ADD_FORM ################################## |
50 |
################## ADD_FORM ################################## |
68 |
# called by default. Used to create form to add or modify a record |
51 |
# called by default. Used to create form to add or modify a record |
69 |
if ($op eq 'add_form') { |
52 |
if ($op eq 'add_form') { |
70 |
my $data; |
53 |
my ( $selected_branches, $category, $av ); |
71 |
my @selected_branches; |
54 |
if ($id) { |
72 |
if ($id) { |
55 |
$av = Koha::AuthorisedValue->new( { id => $id } )->fetch; |
73 |
my $sth=$dbh->prepare("select id, category, authorised_value, lib, lib_opac, imageurl from authorised_values where id=?"); |
56 |
$selected_branches = $av->branches_limitations; |
74 |
$sth->execute($id); |
57 |
} else { |
75 |
$data=$sth->fetchrow_hashref; |
58 |
$category = $input->param('category'); |
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 = ?;"); |
59 |
} |
77 |
$sth->execute( $id ); |
|
|
78 |
while ( my $branch = $sth->fetchrow_hashref ) { |
79 |
push @selected_branches, $branch; |
80 |
} |
81 |
} else { |
82 |
$data->{'category'} = $input->param('category'); |
83 |
} |
84 |
|
60 |
|
85 |
my $branches = GetBranches; |
61 |
my $branches = GetBranches; |
86 |
my @branches_loop; |
62 |
my @branches_loop; |
87 |
|
63 |
|
88 |
foreach my $branch (sort keys %$branches) { |
64 |
foreach my $branch (sort keys %$branches) { |
89 |
my $selected = ( grep {$_->{branchcode} eq $branch} @selected_branches ) ? 1 : 0; |
65 |
my $selected = ( grep {$_->{branchcode} eq $branch} @$selected_branches ) ? 1 : 0; |
90 |
push @branches_loop, { |
66 |
push @branches_loop, { |
91 |
branchcode => $branches->{$branch}{branchcode}, |
67 |
branchcode => $branches->{$branch}{branchcode}, |
92 |
branchname => $branches->{$branch}{branchname}, |
68 |
branchname => $branches->{$branch}{branchname}, |
Lines 96-247
if ($op eq 'add_form') {
Link Here
|
96 |
|
72 |
|
97 |
if ($id) { |
73 |
if ($id) { |
98 |
$template->param(action_modify => 1); |
74 |
$template->param(action_modify => 1); |
99 |
$template->param('heading_modify_authorized_value_p' => 1); |
75 |
} elsif ( ! $category ) { |
100 |
} elsif ( ! $data->{'category'} ) { |
|
|
101 |
$template->param(action_add_category => 1); |
76 |
$template->param(action_add_category => 1); |
102 |
$template->param('heading_add_new_category_p' => 1); |
|
|
103 |
} else { |
77 |
} else { |
104 |
$template->param(action_add_value => 1); |
78 |
$template->param(action_add_value => 1); |
105 |
$template->param('heading_add_authorized_value_p' => 1); |
|
|
106 |
} |
79 |
} |
107 |
$template->param('use_heading_flags_p' => 1); |
80 |
|
108 |
$template->param( category => $data->{'category'}, |
81 |
$template->param( |
109 |
authorised_value => $data->{'authorised_value'}, |
82 |
category => ( $av ? $av->{category} : $category ), |
110 |
lib => $data->{'lib'}, |
83 |
authorised_value => $av->{authorised_value}, |
111 |
lib_opac => $data->{'lib_opac'}, |
84 |
lib => $av->{lib}, |
112 |
id => $data->{'id'}, |
85 |
lib_opac => $av->{lib_opac}, |
113 |
imagesets => C4::Koha::getImageSets( checked => $data->{'imageurl'} ), |
86 |
id => $av->{id}, |
114 |
offset => $offset, |
87 |
imagesets => C4::Koha::getImageSets( checked => $av->{imageurl} ), |
115 |
branches_loop => \@branches_loop, |
88 |
branches_loop => \@branches_loop, |
116 |
); |
89 |
); |
117 |
|
90 |
|
118 |
################## ADD_VALIDATE ################################## |
91 |
} elsif ($op eq 'add') { |
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'); |
92 |
my $new_authorised_value = $input->param('authorised_value'); |
122 |
my $new_category = $input->param('category'); |
93 |
my $new_category = $input->param('category'); |
123 |
my $imageurl = $input->param( 'imageurl' ) || ''; |
94 |
my $imageurl = $input->param( 'imageurl' ) || ''; |
124 |
$imageurl = '' if $imageurl =~ /removeImage/; |
95 |
$imageurl = '' if $imageurl =~ /removeImage/; |
125 |
my $duplicate_entry = 0; |
96 |
my $duplicate_entry = 0; |
126 |
my @branches = $input->param('branches'); |
97 |
my @branches = $input->param('branches'); |
127 |
|
98 |
|
128 |
if ( $id ) { # Update |
99 |
if ( $id ) { # Update |
129 |
my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id = ? "); |
100 |
my $av = Koha::AuthorisedValue->new( { id => $id } ); |
130 |
$sth->execute($id); |
101 |
|
131 |
my ($category, $authorised_value) = $sth->fetchrow_array(); |
102 |
# TODO in updatedatabase and kohastructure |
132 |
if ( $authorised_value ne $new_authorised_value ) { |
103 |
# Add a uniq key on auv.category and av.authorised_values |
133 |
my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " . |
104 |
#if ( $av->{authorised_value} ne $new_authorised_value ) { |
134 |
"WHERE category = ? AND authorised_value = ? and id <> ? "); |
105 |
# my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " . |
135 |
$sth->execute($new_category, $new_authorised_value, $id); |
106 |
# "WHERE category = ? AND authorised_value = ? and id <> ? "); |
136 |
($duplicate_entry) = $sth->fetchrow_array(); |
107 |
# $sth->execute($new_category, $new_authorised_value, $id); |
137 |
} |
108 |
# ($duplicate_entry) = $sth->fetchrow_array(); |
138 |
unless ( $duplicate_entry ) { |
109 |
#} |
139 |
my $sth=$dbh->prepare( 'UPDATE authorised_values |
110 |
#unless ( $duplicate_entry ) { |
140 |
SET category = ?, |
111 |
$av->{lib} = $input->param('lib') || undef; |
141 |
authorised_value = ?, |
112 |
$av->{lib_opac} = $input->param('lib_opac') || undef; |
142 |
lib = ?, |
113 |
$av->{category} = $new_category; |
143 |
lib_opac = ?, |
114 |
$av->{authorised_value} = $new_authorised_value; |
144 |
imageurl = ? |
115 |
$av->{imageurl} = $imageurl; |
145 |
WHERE id=?' ); |
116 |
$av->{id} = $id; |
146 |
my $lib = $input->param('lib'); |
117 |
$av->{branches_limitations} = [ |
147 |
my $lib_opac = $input->param('lib_opac'); |
118 |
map { {branchcode => $_, branchname => undef} } @branches |
148 |
undef $lib if ($lib eq ""); # to insert NULL instead of a blank string |
119 |
]; |
149 |
undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string |
120 |
if ( $@ ) { |
150 |
$sth->execute($new_category, $new_authorised_value, $lib, $lib_opac, $imageurl, $id); |
121 |
push @messages, {type => 'error', code => 'error_on_update' }; |
151 |
if ( @branches ) { |
122 |
} else { |
152 |
$sth = $dbh->prepare("DELETE FROM authorised_values_branches WHERE av_id = ?"); |
123 |
push @messages, { type => 'message', code => 'success_on_update' }; |
153 |
$sth->execute( $id ); |
|
|
154 |
$sth = $dbh->prepare( |
155 |
"INSERT INTO authorised_values_branches |
156 |
( av_id, branchcode ) |
157 |
VALUES ( ?, ? )" |
158 |
); |
159 |
for my $branchcode ( @branches ) { |
160 |
next if not $branchcode; |
161 |
$sth->execute($id, $branchcode); |
162 |
} |
163 |
} |
124 |
} |
164 |
$sth->finish; |
125 |
$av->update; |
165 |
print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."&offset=$offset\"></html>"; |
126 |
$template->param( update_success => 1 ); |
166 |
exit; |
127 |
#} |
167 |
} |
|
|
168 |
} |
128 |
} |
169 |
else { # Insert |
129 |
else { # Insert |
170 |
my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " . |
130 |
my $av = Koha::AuthorisedValue->new( { |
171 |
"WHERE category = ? AND authorised_value = ? "); |
131 |
category => $new_category, |
172 |
$sth->execute($new_category, $new_authorised_value); |
132 |
authorised_value => $new_authorised_value, |
173 |
($duplicate_entry) = $sth->fetchrow_array(); |
133 |
lib => $input->param('lib') || undef, |
174 |
unless ( $duplicate_entry ) { |
134 |
lib_opac => $input->param('lib_opac') || undef, |
175 |
my $sth=$dbh->prepare( 'INSERT INTO authorised_values |
135 |
imageurl => $imageurl, |
176 |
( category, authorised_value, lib, lib_opac, imageurl ) |
136 |
branches_limitations => [ map { {branchcode => $_, branchname => undef} } @branches ], |
177 |
values (?, ?, ?, ?, ?)' ); |
137 |
} ); |
178 |
my $lib = $input->param('lib'); |
138 |
eval {$av->insert}; |
179 |
my $lib_opac = $input->param('lib_opac'); |
139 |
if ( $@ ) { |
180 |
undef $lib if ($lib eq ""); # to insert NULL instead of a blank string |
140 |
push @messages, {type => 'error', code => 'error_on_insert' }; |
181 |
undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string |
141 |
} else { |
182 |
$sth->execute( $new_category, $new_authorised_value, $lib, $lib_opac, $imageurl ); |
142 |
push @messages, { type => 'message', code => 'success_on_insert' }; |
183 |
$id = $dbh->{'mysql_insertid'}; |
|
|
184 |
if ( @branches ) { |
185 |
$sth = $dbh->prepare( |
186 |
"INSERT INTO authorised_values_branches |
187 |
( av_id, branchcode ) |
188 |
VALUES ( ?, ? )" |
189 |
); |
190 |
for my $branchcode ( @branches ) { |
191 |
next if not $branchcode; |
192 |
$sth->execute($id, $branchcode); |
193 |
} |
194 |
} |
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>"; |
196 |
exit; |
197 |
} |
143 |
} |
198 |
} |
144 |
} |
199 |
if ( $duplicate_entry ) { |
145 |
if ( $duplicate_entry ) { |
200 |
$template->param(duplicate_category => $new_category, |
146 |
$template->param(duplicate_category => $new_category, |
201 |
duplicate_value => $new_authorised_value, |
147 |
duplicate_value => $new_authorised_value); |
202 |
else => 1); |
148 |
} |
203 |
default_form(); |
|
|
204 |
} |
205 |
|
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=?"); |
210 |
$sth->execute($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'}, |
217 |
id =>$id, |
218 |
); |
219 |
|
149 |
|
220 |
# END $OP eq DELETE_CONFIRM |
150 |
$op = 'list'; |
221 |
################## DELETE_CONFIRMED ################################## |
151 |
$searchfield = $new_category; |
222 |
# called by delete_confirm, used to effectively confirm deletion of data in DB |
152 |
} elsif ($op eq 'delete') { |
223 |
} elsif ($op eq 'delete_confirmed') { |
153 |
my $av = Koha::AuthorisedValue->new( { id => $input->param('id') } ); |
224 |
my $id = $input->param('id'); |
154 |
eval {$av->delete}; |
225 |
my $sth=$dbh->prepare("delete from authorised_values where id=?"); |
155 |
if ( $@ ) { |
226 |
$sth->execute($id); |
156 |
push @messages, {type => 'error', code => 'error_on_delete' }; |
227 |
print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield&offset=$offset\"></html>"; |
157 |
} else { |
228 |
exit; |
158 |
push @messages, { type => 'message', code => 'success_on_delete' }; |
229 |
# END $OP eq DELETE_CONFIRMED |
159 |
} |
230 |
################## DEFAULT ################################## |
160 |
|
231 |
} else { # DEFAULT |
161 |
$op = 'list'; |
232 |
default_form(); |
162 |
$template->param( delete_success => 1 ); |
233 |
} #---- END $OP eq DEFAULT |
163 |
} |
234 |
output_html_with_http_headers $input, $cookie, $template->output; |
|
|
235 |
|
164 |
|
236 |
exit 0; |
165 |
$template->param( |
|
|
166 |
op => $op, |
167 |
searchfield => $searchfield, |
168 |
messages => \@messages, |
169 |
); |
237 |
|
170 |
|
238 |
sub default_form { |
171 |
if ( $op eq 'list' ) { |
239 |
# build categories list |
172 |
# build categories list |
240 |
my $sth = $dbh->prepare("select distinct category from authorised_values"); |
173 |
my $avs = Koha::AuthorisedValues->new; |
241 |
$sth->execute; |
|
|
242 |
my @category_list; |
174 |
my @category_list; |
243 |
my %categories; # a hash, to check that some hardcoded categories exist. |
175 |
my %categories; # a hash, to check that some hardcoded categories exist. |
244 |
while ( my ($category) = $sth->fetchrow_array ) { |
176 |
for my $category ( $avs->categories ) { |
245 |
push( @category_list, $category ); |
177 |
push( @category_list, $category ); |
246 |
$categories{$category} = 1; |
178 |
$categories{$category} = 1; |
247 |
} |
179 |
} |
Lines 251-294
sub default_form {
Link Here
|
251 |
push @category_list, $_ unless $categories{$_}; |
183 |
push @category_list, $_ unless $categories{$_}; |
252 |
} |
184 |
} |
253 |
|
185 |
|
254 |
#reorder the list |
186 |
#reorder the list |
255 |
@category_list = sort {$a cmp $b} @category_list; |
187 |
@category_list = sort {$a cmp $b} @category_list; |
256 |
my $tab_list = CGI::scrolling_list(-name=>'searchfield', |
188 |
|
257 |
-id=>'searchfield', |
189 |
$searchfield ||= $category_list[0]; |
258 |
-values=> \@category_list, |
190 |
|
259 |
-default=>"", |
191 |
my $avs_for_category = $avs->filter( { category => $searchfield } ); |
260 |
-size=>1, |
192 |
my @loop_data = (); |
261 |
-multiple=>0, |
193 |
# builds value list |
262 |
); |
194 |
for my $av ( @$avs_for_category ) { |
263 |
if (!$searchfield) { |
195 |
my $branches = $av->branches_limitations; |
264 |
$searchfield=$category_list[0]; |
196 |
my %row_data; # get a fresh hash for the row data |
265 |
} |
197 |
$row_data{category} = $av->{category}; |
266 |
my ($results) = AuthorizedValuesForCategory($searchfield); |
198 |
$row_data{authorised_value} = $av->{authorised_value}; |
267 |
my $count = scalar(@$results); |
199 |
$row_data{lib} = $av->{lib}; |
268 |
my @loop_data = (); |
200 |
$row_data{lib_opac} = $av->{lib_opac}; |
269 |
# builds value list |
201 |
$row_data{imageurl} = getitemtypeimagelocation( 'intranet', $av->{imageurl} ); |
270 |
my $dbh = C4::Context->dbh; |
202 |
$row_data{branches} = $branches; |
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 = ?"); |
203 |
$row_data{id} = $av->{id}; |
272 |
for (my $i=0; $i < $count; $i++){ |
204 |
push(@loop_data, \%row_data); |
273 |
$sth->execute( $results->[$i]{id} ); |
205 |
} |
274 |
my @selected_branches; |
|
|
275 |
while ( my $branch = $sth->fetchrow_hashref ) { |
276 |
push @selected_branches, $branch; |
277 |
} |
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); |
288 |
} |
289 |
|
206 |
|
290 |
$template->param( loop => \@loop_data, |
207 |
$template->param( |
291 |
tab_list => $tab_list, |
208 |
loop => \@loop_data, |
292 |
category => $searchfield ); |
209 |
category => $searchfield, |
293 |
} |
210 |
categories => \@category_list, |
|
|
211 |
); |
294 |
|
212 |
|
|
|
213 |
} |
214 |
output_html_with_http_headers $input, $cookie, $template->output; |