Lines 24-56
use Try::Tiny qw( catch try );
Link Here
|
24 |
|
24 |
|
25 |
use C4::Auth qw( get_template_and_user haspermission ); |
25 |
use C4::Auth qw( get_template_and_user haspermission ); |
26 |
use C4::Output qw( output_html_with_http_headers ); |
26 |
use C4::Output qw( output_html_with_http_headers ); |
27 |
use C4::Biblio qw( |
27 |
use C4::Circulation qw( barcodedecode ); |
28 |
DelBiblio |
|
|
29 |
GetAuthorisedValueDesc |
30 |
GetMarcFromKohaField |
31 |
GetMarcStructure |
32 |
IsMarcStructureInternal |
33 |
TransformHtmlToXml |
34 |
); |
35 |
use C4::Items qw( GetItemsInfo Item2Marc ModItemFromMarc ); |
36 |
use C4::Circulation qw( barcodedecode LostItem IsItemIssued ); |
37 |
use C4::Context; |
28 |
use C4::Context; |
38 |
use C4::Koha; |
|
|
39 |
use C4::BackgroundJob; |
40 |
use C4::ClassSource qw( GetClassSources GetClassSource ); |
41 |
use MARC::File::XML; |
29 |
use MARC::File::XML; |
42 |
use List::MoreUtils qw( uniq ); |
30 |
use List::MoreUtils qw( uniq ); |
|
|
31 |
use Encode qw( encode_utf8 ); |
43 |
|
32 |
|
44 |
use Koha::Database; |
33 |
use Koha::Database; |
|
|
34 |
use Koha::DateUtils qw( dt_from_string ); |
45 |
use Koha::Exceptions::Exception; |
35 |
use Koha::Exceptions::Exception; |
46 |
use Koha::AuthorisedValues; |
|
|
47 |
use Koha::Biblios; |
36 |
use Koha::Biblios; |
48 |
use Koha::DateUtils qw( dt_from_string ); |
|
|
49 |
use Koha::Items; |
37 |
use Koha::Items; |
50 |
use Koha::ItemTypes; |
|
|
51 |
use Koha::Patrons; |
38 |
use Koha::Patrons; |
52 |
use Koha::SearchEngine::Indexer; |
39 |
use Koha::Item::Attributes; |
|
|
40 |
use Koha::BackgroundJob::BatchDeleteItem; |
41 |
use Koha::BackgroundJob::BatchUpdateItem; |
53 |
use Koha::UI::Form::Builder::Item; |
42 |
use Koha::UI::Form::Builder::Item; |
|
|
43 |
use Koha::UI::Table::Builder::Items; |
54 |
|
44 |
|
55 |
my $input = CGI->new; |
45 |
my $input = CGI->new; |
56 |
my $dbh = C4::Context->dbh; |
46 |
my $dbh = C4::Context->dbh; |
Lines 90-346
my $restrictededition = $uid ? haspermission($uid, {'tools' => 'items_batchmod_
Link Here
|
90 |
# In case user is a superlibrarian, edition is not restricted |
80 |
# In case user is a superlibrarian, edition is not restricted |
91 |
$restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian()); |
81 |
$restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian()); |
92 |
|
82 |
|
93 |
$template->param(del => $del); |
|
|
94 |
|
95 |
my $nextop=""; |
83 |
my $nextop=""; |
96 |
my @errors; # store errors found while checking data BEFORE saving item. |
84 |
my $display_items; |
97 |
my $items_display_hashref; |
|
|
98 |
our $tagslib = &GetMarcStructure(1); |
99 |
|
100 |
my $deleted_items = 0; # Number of deleted items |
101 |
my $deleted_records = 0; # Number of deleted records ( with no items attached ) |
102 |
my $not_deleted_items = 0; # Number of items that could not be deleted |
103 |
my @not_deleted; # List of the itemnumbers that could not be deleted |
104 |
my $modified_items = 0; # Numbers of modified items |
105 |
my $modified_fields = 0; # Numbers of modified fields |
106 |
|
85 |
|
107 |
my %cookies = parse CGI::Cookie($cookie); |
86 |
my %cookies = parse CGI::Cookie($cookie); |
108 |
my $sessionID = $cookies{'CGISESSID'}->value; |
87 |
my $sessionID = $cookies{'CGISESSID'}->value; |
109 |
|
88 |
|
|
|
89 |
my @messages; |
110 |
|
90 |
|
111 |
#--- ---------------------------------------------------------------------------- |
91 |
if ( $op eq "action" ) { |
112 |
if ($op eq "action") { |
92 |
|
113 |
#------------------------------------------------------------------------------- |
93 |
if ($del) { |
114 |
my @tags = $input->multi_param('tag'); |
|
|
115 |
my @subfields = $input->multi_param('subfield'); |
116 |
my @values = $input->multi_param('field_value'); |
117 |
my @searches = $input->multi_param('regex_search'); |
118 |
my @replaces = $input->multi_param('regex_replace'); |
119 |
my @modifiers = $input->multi_param('regex_modifiers'); |
120 |
my @subfields_to_blank = $input->multi_param('disable_input'); |
121 |
# build indicator hash. |
122 |
my @ind_tag = $input->multi_param('ind_tag'); |
123 |
my @indicator = $input->multi_param('indicator'); |
124 |
|
125 |
my $upd_biblionumbers; |
126 |
my $del_biblionumbers; |
127 |
if ( $del ) { |
128 |
try { |
94 |
try { |
129 |
my $schema = Koha::Database->new->schema; |
95 |
my $params = { |
130 |
$schema->txn_do( |
96 |
record_ids => \@itemnumbers, |
131 |
sub { |
97 |
delete_biblios => $del_records, |
132 |
foreach my $itemnumber (@itemnumbers) { |
98 |
}; |
133 |
my $item = Koha::Items->find($itemnumber); |
99 |
my $job_id = |
134 |
next |
100 |
Koha::BackgroundJob::BatchDeleteItem->new->enqueue($params); |
135 |
unless $item |
101 |
$nextop = 'enqueued'; |
136 |
; # Should have been tested earlier, but just in case... |
102 |
$template->param( job_id => $job_id, ); |
137 |
my $itemdata = $item->unblessed; |
|
|
138 |
my $return = $item->safe_delete; |
139 |
if ( ref( $return ) ) { |
140 |
$deleted_items++; |
141 |
push @$upd_biblionumbers, $itemdata->{'biblionumber'}; |
142 |
} |
143 |
else { |
144 |
$not_deleted_items++; |
145 |
push @not_deleted, |
146 |
{ |
147 |
biblionumber => $itemdata->{'biblionumber'}, |
148 |
itemnumber => $itemdata->{'itemnumber'}, |
149 |
barcode => $itemdata->{'barcode'}, |
150 |
title => $itemdata->{'title'}, |
151 |
reason => $return, |
152 |
}; |
153 |
} |
154 |
|
155 |
# If there are no items left, delete the biblio |
156 |
if ($del_records) { |
157 |
my $itemscount = Koha::Biblios->find( $itemdata->{'biblionumber'} )->items->count; |
158 |
if ( $itemscount == 0 ) { |
159 |
my $error = DelBiblio( $itemdata->{'biblionumber'}, { skip_record_index => 1 } ); |
160 |
unless ($error) { |
161 |
$deleted_records++; |
162 |
push @$del_biblionumbers, $itemdata->{'biblionumber'}; |
163 |
if ( $src eq 'CATALOGUING' ) { |
164 |
# We are coming catalogue/detail.pl, there were items from a single bib record |
165 |
$template->param( biblio_deleted => 1 ); |
166 |
} |
167 |
} |
168 |
} |
169 |
} |
170 |
} |
171 |
if (@not_deleted) { |
172 |
Koha::Exceptions::Exception->throw( |
173 |
'Some items have not been deleted, rolling back'); |
174 |
} |
175 |
} |
176 |
); |
177 |
} |
103 |
} |
178 |
catch { |
104 |
catch { |
179 |
warn $_; |
105 |
warn $_; |
180 |
if ( $_->isa('Koha::Exceptions::Exception') ) { |
106 |
push @messages, |
181 |
$template->param( deletion_failed => 1 ); |
107 |
{ |
182 |
} |
108 |
type => 'error', |
183 |
die "Something terrible has happened!" |
109 |
code => 'cannot_enqueue_job', |
184 |
if ($_ =~ /Rollback failed/); # Rollback failed |
110 |
error => $_, |
|
|
111 |
}; |
112 |
$template->param( view => 'errors' ); |
185 |
}; |
113 |
}; |
186 |
} |
114 |
} |
187 |
|
115 |
|
188 |
else { # modification |
116 |
else { # modification |
189 |
|
117 |
|
190 |
my @columns = Koha::Items->columns; |
118 |
my @item_columns = Koha::Items->columns; |
191 |
|
119 |
|
192 |
my $new_item_data; |
120 |
my $new_item_data; |
193 |
my @columns_with_regex; |
121 |
my ( $columns_with_regex ); |
194 |
for my $c ( @columns ) { |
122 |
my @subfields_to_blank = $input->multi_param('disable_input'); |
195 |
if ( $c eq 'more_subfields_xml' ) { |
123 |
my @more_subfields = $input->multi_param("items.more_subfields_xml"); |
196 |
my @more_subfields_xml = $input->multi_param("items.more_subfields_xml"); |
124 |
for my $item_column (@item_columns) { |
197 |
my @unlinked_item_subfields; |
125 |
my @attributes = ($item_column); |
198 |
for my $subfield ( @more_subfields_xml ) { |
126 |
my $cgi_param_prefix = 'items.'; |
199 |
my $v = $input->param('items.more_subfields_xml_' . $subfield); |
127 |
if ( $item_column eq 'more_subfields_xml' ) { |
200 |
push @unlinked_item_subfields, $subfield, $v; |
128 |
@attributes = (); |
201 |
} |
129 |
$cgi_param_prefix = 'items.more_subfields_xml_'; |
202 |
if ( @unlinked_item_subfields ) { |
130 |
for my $subfield (@more_subfields) { |
203 |
my $marc = MARC::Record->new(); |
131 |
push @attributes, $subfield; |
204 |
# use of tag 999 is arbitrary, and doesn't need to match the item tag |
|
|
205 |
# used in the framework |
206 |
$marc->append_fields(MARC::Field->new('999', ' ', ' ', @unlinked_item_subfields)); |
207 |
$marc->encoding("UTF-8"); |
208 |
# FIXME This is WRONG! We need to use the values that haven't been modified by the batch tool! |
209 |
$new_item_data->{more_subfields_xml} = $marc->as_xml("USMARC"); |
210 |
next; |
211 |
} |
132 |
} |
212 |
$new_item_data->{more_subfields_xml} = undef; |
133 |
} |
213 |
# FIXME deal with more_subfields_xml and @subfields_to_blank |
|
|
214 |
} elsif ( grep { $c eq $_ } @subfields_to_blank ) { |
215 |
# Empty this column |
216 |
$new_item_data->{$c} = undef |
217 |
} else { |
218 |
|
134 |
|
219 |
my @v = grep { $_ ne "" } |
135 |
for my $attr (@attributes) { |
220 |
uniq $input->multi_param( "items." . $c ); |
|
|
221 |
|
136 |
|
222 |
next unless @v; |
137 |
my $cgi_var_name = $cgi_param_prefix |
|
|
138 |
. encode_utf8($attr) |
139 |
; # We need to deal correctly with encoding on subfield codes |
223 |
|
140 |
|
224 |
$new_item_data->{$c} = join ' | ', @v; |
141 |
if ( grep { $attr eq $_ } @subfields_to_blank ) { |
225 |
} |
142 |
|
|
|
143 |
# Empty this column |
144 |
$new_item_data->{$attr} = undef; |
145 |
} |
146 |
elsif ( my $regex_search = |
147 |
$input->param( $cgi_var_name . '_regex_search' ) ) |
148 |
{ |
149 |
$columns_with_regex->{$attr} = { |
150 |
search => $regex_search, |
151 |
replace => |
152 |
$input->param( $cgi_var_name . '_regex_replace' ), |
153 |
modifiers => |
154 |
$input->param( $cgi_var_name . '_regex_modifiers' ) |
155 |
}; |
156 |
} |
157 |
else { |
158 |
my @v = |
159 |
grep { $_ ne "" } uniq $input->multi_param($cgi_var_name); |
226 |
|
160 |
|
227 |
if ( my $regex_search = $input->param('items.'.$c.'_regex_search') ) { |
161 |
next unless @v; |
228 |
push @columns_with_regex, $c; |
162 |
|
|
|
163 |
$new_item_data->{$attr} = join '|', @v; |
164 |
} |
229 |
} |
165 |
} |
230 |
} |
166 |
} |
231 |
|
167 |
|
|
|
168 |
my $params = { |
169 |
record_ids => \@itemnumbers, |
170 |
regex_mod => $columns_with_regex, |
171 |
new_values => $new_item_data, |
172 |
exclude_from_local_holds_priority => ( |
173 |
defined $exclude_from_local_holds_priority |
174 |
&& $exclude_from_local_holds_priority ne "" |
175 |
) |
176 |
? $exclude_from_local_holds_priority |
177 |
: undef, |
178 |
|
179 |
}; |
232 |
try { |
180 |
try { |
233 |
my $schema = Koha::Database->new->schema; |
181 |
my $job_id = |
234 |
$schema->txn_do( |
182 |
Koha::BackgroundJob::BatchUpdateItem->new->enqueue($params); |
235 |
sub { |
183 |
$nextop = 'enqueued'; |
236 |
|
184 |
$template->param( job_id => $job_id, ); |
237 |
foreach my $itemnumber (@itemnumbers) { |
|
|
238 |
my $item = Koha::Items->find($itemnumber); |
239 |
next |
240 |
unless $item |
241 |
; # Should have been tested earlier, but just in case... |
242 |
my $itemdata = $item->unblessed; |
243 |
|
244 |
my $modified_holds_priority = 0; |
245 |
if ( defined $exclude_from_local_holds_priority && $exclude_from_local_holds_priority ne "" ) { |
246 |
if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) { |
247 |
$item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store; |
248 |
$modified_holds_priority = 1; |
249 |
} |
250 |
} |
251 |
|
252 |
my $modified = 0; |
253 |
for my $c ( @columns_with_regex ) { |
254 |
my $regex_search = $input->param('items.'.$c.'_regex_search'); |
255 |
my $old_value = $item->$c; |
256 |
|
257 |
my $value = apply_regex( |
258 |
{ |
259 |
search => $regex_search, |
260 |
replace => $input->param( |
261 |
'items' . $c . '_regex_replace' |
262 |
), |
263 |
modifiers => $input->param( |
264 |
'items' . $c . '_regex_modifiers' |
265 |
), |
266 |
value => $old_value, |
267 |
} |
268 |
); |
269 |
unless ( $old_value eq $value ) { |
270 |
$modified++; |
271 |
$item->$c($value); |
272 |
} |
273 |
} |
274 |
|
275 |
$modified += scalar(keys %$new_item_data); # FIXME This is incorrect if old value == new value. Should we loop of the keys and compare the before/after values? |
276 |
if ( $modified) { |
277 |
my $itemlost_pre = $item->itemlost; |
278 |
$item->set($new_item_data)->store({skip_record_index => 1}); |
279 |
|
280 |
push @$upd_biblionumbers, $itemdata->{'biblionumber'}; |
281 |
|
282 |
LostItem( |
283 |
$item->itemnumber, 'batchmod', undef, |
284 |
{ skip_record_index => 1 } |
285 |
) if $item->itemlost |
286 |
and not $itemlost_pre; |
287 |
} |
288 |
|
289 |
$modified_items++ if $modified || $modified_holds_priority; |
290 |
$modified_fields += $modified + $modified_holds_priority; |
291 |
} |
292 |
} |
293 |
); |
294 |
} |
185 |
} |
295 |
catch { |
186 |
catch { |
296 |
warn $_; |
187 |
push @messages, |
297 |
die "Something terrible has happened!" |
188 |
{ |
298 |
if ($_ =~ /Rollback failed/); # Rollback failed |
189 |
type => 'error', |
|
|
190 |
code => 'cannot_enqueue_job', |
191 |
error => $_, |
192 |
}; |
193 |
$template->param( view => 'errors' ); |
299 |
}; |
194 |
}; |
300 |
} |
195 |
} |
301 |
|
196 |
|
302 |
$upd_biblionumbers = [ uniq @$upd_biblionumbers ]; # Only update each bib once |
|
|
303 |
|
304 |
# Don't send specialUpdate for records we are going to delete |
305 |
my %del_bib_hash = map{ $_ => undef } @$del_biblionumbers; |
306 |
@$upd_biblionumbers = grep( ! exists( $del_bib_hash{$_} ), @$upd_biblionumbers ); |
307 |
|
308 |
my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); |
309 |
$indexer->index_records( $upd_biblionumbers, 'specialUpdate', "biblioserver", undef ) if @$upd_biblionumbers; |
310 |
$indexer->index_records( $del_biblionumbers, 'recordDelete', "biblioserver", undef ) if @$del_biblionumbers; |
311 |
|
312 |
# Once the job is done |
313 |
# If we have a reasonable amount of items, we display them |
314 |
my $max_items = $del ? C4::Context->preference("MaxItemsToDisplayForBatchDel") : C4::Context->preference("MaxItemsToDisplayForBatchMod"); |
315 |
if (scalar(@itemnumbers) <= $max_items ){ |
316 |
if (scalar(@itemnumbers) <= 1000 ) { |
317 |
$items_display_hashref=BuildItemsData(@itemnumbers); |
318 |
} else { |
319 |
# Else, we only display the barcode |
320 |
my @simple_items_display = map { |
321 |
my $itemnumber = $_; |
322 |
my $item = Koha::Items->find($itemnumber); |
323 |
{ |
324 |
itemnumber => $itemnumber, |
325 |
barcode => $item ? ( $item->barcode // q{} ) : q{}, |
326 |
biblionumber => $item ? $item->biblio->biblionumber : q{}, |
327 |
}; |
328 |
} @itemnumbers; |
329 |
$template->param("simple_items_display" => \@simple_items_display); |
330 |
} |
331 |
} else { |
332 |
$template->param( "too_many_items_display" => scalar(@itemnumbers) ); |
333 |
$template->param( "job_completed" => 1 ); |
334 |
} |
335 |
|
336 |
|
337 |
# Calling the template |
338 |
$template->param( |
339 |
modified_items => $modified_items, |
340 |
modified_fields => $modified_fields, |
341 |
); |
342 |
|
343 |
} |
197 |
} |
|
|
198 |
|
199 |
$template->param( |
200 |
messages => \@messages, |
201 |
); |
344 |
# |
202 |
# |
345 |
#------------------------------------------------------------------------------- |
203 |
#------------------------------------------------------------------------------- |
346 |
# build screen with existing items. and "new" one |
204 |
# build screen with existing items. and "new" one |
Lines 376-385
if ($op eq "show"){
Link Here
|
376 |
} |
234 |
} |
377 |
} else { |
235 |
} else { |
378 |
if (defined $biblionumber && !@itemnumbers){ |
236 |
if (defined $biblionumber && !@itemnumbers){ |
379 |
my @all_items = GetItemsInfo( $biblionumber ); |
237 |
my $biblio = Koha::Biblios->find($biblionumber); |
380 |
foreach my $itm (@all_items) { |
238 |
@itemnumbers = $biblio ? $biblio->items->get_column('itemnumber') : (); |
381 |
push @itemnumbers, $itm->{itemnumber}; |
|
|
382 |
} |
383 |
} |
239 |
} |
384 |
if ( my $list = $input->param('barcodelist') ) { |
240 |
if ( my $list = $input->param('barcodelist') ) { |
385 |
my @barcodelist = grep /\S/, ( split /[$split_chars]/, $list ); |
241 |
my @barcodelist = grep /\S/, ( split /[$split_chars]/, $list ); |
Lines 402-408
if ($op eq "show"){
Link Here
|
402 |
: C4::Context->preference("MaxItemsToDisplayForBatchMod"); |
258 |
: C4::Context->preference("MaxItemsToDisplayForBatchMod"); |
403 |
$template->param("too_many_items_process" => scalar(@itemnumbers)) if !$del && scalar(@itemnumbers) > C4::Context->preference("MaxItemsToProcessForBatchMod"); |
259 |
$template->param("too_many_items_process" => scalar(@itemnumbers)) if !$del && scalar(@itemnumbers) > C4::Context->preference("MaxItemsToProcessForBatchMod"); |
404 |
if (scalar(@itemnumbers) <= ( $max_display_items // 1000 ) ) { |
260 |
if (scalar(@itemnumbers) <= ( $max_display_items // 1000 ) ) { |
405 |
$items_display_hashref=BuildItemsData(@itemnumbers); |
261 |
$display_items = 1; |
406 |
} else { |
262 |
} else { |
407 |
$template->param("too_many_items_display" => scalar(@itemnumbers)); |
263 |
$template->param("too_many_items_display" => scalar(@itemnumbers)); |
408 |
# Even if we do not display the items, we need the itemnumbers |
264 |
# Even if we do not display the items, we need the itemnumbers |
Lines 410-419
if ($op eq "show"){
Link Here
|
410 |
} |
266 |
} |
411 |
|
267 |
|
412 |
# now, build the item form for entering a new item |
268 |
# now, build the item form for entering a new item |
413 |
my @loop_data =(); |
|
|
414 |
my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; |
415 |
|
416 |
my $pref_itemcallnumber = C4::Context->preference('itemcallnumber'); |
417 |
|
269 |
|
418 |
# Getting list of subfields to keep when restricted batchmod edit is enabled |
270 |
# Getting list of subfields to keep when restricted batchmod edit is enabled |
419 |
my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod') : (); |
271 |
my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod') : (); |
Lines 426-432
if ($op eq "show"){
Link Here
|
426 |
? ( subfields_to_allow => \@subfields_to_allow ) |
278 |
? ( subfields_to_allow => \@subfields_to_allow ) |
427 |
: () |
279 |
: () |
428 |
), |
280 |
), |
429 |
subfields_to_ignore => ['items.barcode'], |
281 |
ignore_not_allowed_subfields => 1, |
|
|
282 |
kohafields_to_ignore => ['items.barcode'], |
430 |
prefill_with_default_values => $use_default_values, |
283 |
prefill_with_default_values => $use_default_values, |
431 |
default_branches_empty => 1, |
284 |
default_branches_empty => 1, |
432 |
} |
285 |
} |
Lines 441-627
if ($op eq "show"){
Link Here
|
441 |
$nextop="action" |
294 |
$nextop="action" |
442 |
} # -- End action="show" |
295 |
} # -- End action="show" |
443 |
|
296 |
|
444 |
$template->param(%$items_display_hashref) if $items_display_hashref; |
297 |
if ( $display_items ) { |
445 |
$template->param( |
298 |
my $items_table = |
446 |
op => $nextop, |
299 |
Koha::UI::Table::Builder::Items->new( { itemnumbers => \@itemnumbers } ) |
447 |
); |
300 |
->build_table; |
448 |
$template->param( $op => 1 ) if $op; |
|
|
449 |
|
450 |
if ($op eq "action") { |
451 |
|
452 |
#my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted; |
453 |
|
454 |
$template->param( |
301 |
$template->param( |
455 |
not_deleted_items => $not_deleted_items, |
302 |
items => $items_table->{items}, |
456 |
deleted_items => $deleted_items, |
303 |
item_header_loop => $items_table->{headers}, |
457 |
delete_records => $del_records, |
|
|
458 |
deleted_records => $deleted_records, |
459 |
not_deleted_loop => \@not_deleted |
460 |
); |
304 |
); |
461 |
} |
305 |
} |
462 |
|
306 |
|
463 |
foreach my $error (@errors) { |
307 |
$template->param( |
464 |
$template->param($error => 1) if $error; |
308 |
op => $nextop, |
465 |
} |
309 |
del => $del, |
466 |
$template->param(src => $src); |
310 |
( $op ? ( $op => 1 ) : () ), |
467 |
$template->param(biblionumber => $biblionumber); |
311 |
src => $src, |
468 |
output_html_with_http_headers $input, $cookie, $template->output; |
312 |
biblionumber => $biblionumber, |
469 |
exit; |
313 |
); |
470 |
|
|
|
471 |
|
472 |
# ---------------- Functions |
473 |
|
474 |
sub BuildItemsData{ |
475 |
my @itemnumbers=@_; |
476 |
# now, build existiing item list |
477 |
my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code |
478 |
my @big_array; |
479 |
#---- finds where items.itemnumber is stored |
480 |
my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField( "items.itemnumber" ); |
481 |
my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField( "items.homebranch" ); |
482 |
foreach my $itemnumber (@itemnumbers){ |
483 |
my $itemdata = Koha::Items->find($itemnumber); |
484 |
next unless $itemdata; # Should have been tested earlier, but just in case... |
485 |
$itemdata = $itemdata->unblessed; |
486 |
my $itemmarc=Item2Marc($itemdata); |
487 |
my %this_row; |
488 |
foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) { |
489 |
# loop through each subfield |
490 |
my $itembranchcode=$field->subfield($branchtagsubfield); |
491 |
if ($itembranchcode && C4::Context->preference("IndependentBranches")) { |
492 |
#verifying rights |
493 |
my $userenv = C4::Context->userenv(); |
494 |
unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){ |
495 |
$this_row{'nomod'}=1; |
496 |
} |
497 |
} |
498 |
my $tag=$field->tag(); |
499 |
foreach my $subfield ($field->subfields) { |
500 |
my ($subfcode,$subfvalue)=@$subfield; |
501 |
next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10 |
502 |
&& $tag ne $itemtagfield |
503 |
&& $subfcode ne $itemtagsubfield); |
504 |
|
505 |
$witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10); |
506 |
if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) { |
507 |
$this_row{$subfcode}=GetAuthorisedValueDesc( $tag, |
508 |
$subfcode, $subfvalue, '', $tagslib) |
509 |
|| $subfvalue; |
510 |
} |
511 |
|
512 |
$this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield); |
513 |
} |
514 |
} |
515 |
|
516 |
# grab title, author, and ISBN to identify bib that the item |
517 |
# belongs to in the display |
518 |
my $biblio = Koha::Biblios->find( $itemdata->{biblionumber} ); |
519 |
$this_row{title} = $biblio->title; |
520 |
$this_row{author} = $biblio->author; |
521 |
$this_row{isbn} = $biblio->biblioitem->isbn; |
522 |
$this_row{biblionumber} = $biblio->biblionumber; |
523 |
$this_row{holds} = $biblio->holds->count; |
524 |
$this_row{item_holds} = Koha::Holds->search( { itemnumber => $itemnumber } )->count; |
525 |
$this_row{item} = Koha::Items->find($itemnumber); |
526 |
|
527 |
if (%this_row) { |
528 |
push(@big_array, \%this_row); |
529 |
} |
530 |
} |
531 |
@big_array = sort {$a->{0} cmp $b->{0}} @big_array; |
532 |
|
533 |
# now, construct template ! |
534 |
# First, the existing items for display |
535 |
my @item_value_loop; |
536 |
my @witnesscodessorted=sort keys %witness; |
537 |
for my $row ( @big_array ) { |
538 |
my %row_data; |
539 |
my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted }; |
540 |
$row_data{item_value} = [ @item_fields ]; |
541 |
$row_data{itemnumber} = $row->{itemnumber}; |
542 |
#reporting this_row values |
543 |
$row_data{'nomod'} = $row->{'nomod'}; |
544 |
$row_data{bibinfo} = $row->{bibinfo}; |
545 |
$row_data{author} = $row->{author}; |
546 |
$row_data{title} = $row->{title}; |
547 |
$row_data{isbn} = $row->{isbn}; |
548 |
$row_data{biblionumber} = $row->{biblionumber}; |
549 |
$row_data{holds} = $row->{holds}; |
550 |
$row_data{item_holds} = $row->{item_holds}; |
551 |
$row_data{item} = $row->{item}; |
552 |
$row_data{safe_to_delete} = $row->{item}->safe_to_delete; |
553 |
my $is_on_loan = C4::Circulation::IsItemIssued( $row->{itemnumber} ); |
554 |
$row_data{onloan} = $is_on_loan ? 1 : 0; |
555 |
push(@item_value_loop,\%row_data); |
556 |
} |
557 |
my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted; |
558 |
|
559 |
my @cannot_be_deleted = map { |
560 |
$_->{safe_to_delete} == 1 ? () : $_->{item}->barcode |
561 |
} @item_value_loop; |
562 |
return { |
563 |
item_loop => \@item_value_loop, |
564 |
cannot_be_deleted => \@cannot_be_deleted, |
565 |
item_header_loop => \@header_loop |
566 |
}; |
567 |
} |
568 |
|
569 |
#BE WARN : it is not the general case |
570 |
# This function can be OK in the item marc record special case |
571 |
# Where subfield is not repeated |
572 |
# And where we are sure that field should correspond |
573 |
# And $tag>10 |
574 |
sub UpdateMarcWith { |
575 |
my ($marcfrom,$marcto)=@_; |
576 |
my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField( "items.itemnumber" ); |
577 |
my $fieldfrom=$marcfrom->field($itemtag); |
578 |
my @fields_to=$marcto->field($itemtag); |
579 |
my $modified = 0; |
580 |
|
581 |
return $modified unless $fieldfrom; |
582 |
|
583 |
foreach my $subfield ( $fieldfrom->subfields() ) { |
584 |
foreach my $field_to_update ( @fields_to ) { |
585 |
if ( $subfield->[1] ) { |
586 |
unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) { |
587 |
$modified++; |
588 |
$field_to_update->update( $subfield->[0] => $subfield->[1] ); |
589 |
} |
590 |
} |
591 |
else { |
592 |
$modified++; |
593 |
$field_to_update->delete_subfield( code => $subfield->[0] ); |
594 |
} |
595 |
} |
596 |
} |
597 |
return $modified; |
598 |
} |
599 |
|
600 |
sub apply_regex { |
601 |
my ($params) = @_; |
602 |
my $search = $params->{search}; |
603 |
my $replace = $params->{replace}; |
604 |
my $modifiers = $params->{modifiers} || []; |
605 |
my $value = $params->{value}; |
606 |
|
607 |
my @available_modifiers = qw( i g ); |
608 |
my $retained_modifiers = q||; |
609 |
for my $modifier ( split //, @$modifiers ) { |
610 |
$retained_modifiers .= $modifier |
611 |
if grep { /$modifier/ } @available_modifiers; |
612 |
} |
613 |
if ( $retained_modifiers =~ m/^(ig|gi)$/ ) { |
614 |
$value =~ s/$search/$replace/ig; |
615 |
} |
616 |
elsif ( $retained_modifiers eq 'i' ) { |
617 |
$value =~ s/$search/$replace/i; |
618 |
} |
619 |
elsif ( $retained_modifiers eq 'g' ) { |
620 |
$value =~ s/$search/$replace/g; |
621 |
} |
622 |
else { |
623 |
$value =~ s/$search/$replace/; |
624 |
} |
625 |
|
314 |
|
626 |
return $value; |
315 |
output_html_with_http_headers $input, $cookie, $template->output; |
627 |
} |
|
|
628 |
- |
|
|