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