Lines 117-164
if ($op eq "action") {
Link Here
|
117 |
my @searches = $input->multi_param('regex_search'); |
117 |
my @searches = $input->multi_param('regex_search'); |
118 |
my @replaces = $input->multi_param('regex_replace'); |
118 |
my @replaces = $input->multi_param('regex_replace'); |
119 |
my @modifiers = $input->multi_param('regex_modifiers'); |
119 |
my @modifiers = $input->multi_param('regex_modifiers'); |
120 |
my @disabled = $input->multi_param('disable_input'); |
|
|
121 |
|
122 |
# Is there something to modify ? |
123 |
# TODO : We shall use this var to warn the user in case no modification was done to the items |
124 |
my $values_to_modify = scalar(grep {!/^$/} @values) || scalar(grep {!/^$/} @searches); |
125 |
my $values_to_blank = scalar(@disabled); |
126 |
|
127 |
my $marcitem; |
128 |
|
129 |
#initializing values for updates |
130 |
my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField( "items.itemnumber" ); |
131 |
if ($values_to_modify){ |
132 |
my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,undef,undef, 'ITEM'); |
133 |
$marcitem = MARC::Record::new_from_xml($xml, 'UTF-8'); |
134 |
} |
135 |
if ($values_to_blank){ |
136 |
foreach my $disabledsubf (@disabled){ |
137 |
if ($marcitem && $marcitem->field($itemtagfield)){ |
138 |
$marcitem->field($itemtagfield)->update( $disabledsubf => "" ); |
139 |
} |
140 |
else { |
141 |
$marcitem = MARC::Record->new(); |
142 |
$marcitem->append_fields( MARC::Field->new( $itemtagfield, '', '', $disabledsubf => "" ) ); |
143 |
} |
144 |
} |
145 |
} |
146 |
|
120 |
|
147 |
my $upd_biblionumbers; |
121 |
my $upd_biblionumbers; |
148 |
my $del_biblionumbers; |
122 |
my $del_biblionumbers; |
149 |
try { |
123 |
if ( $del ) { |
150 |
my $schema = Koha::Database->new->schema; |
124 |
try { |
151 |
$schema->txn_do( |
125 |
my $schema = Koha::Database->new->schema; |
152 |
sub { |
126 |
$schema->txn_do( |
153 |
# For each item |
127 |
sub { |
154 |
my $i = 1; |
128 |
foreach my $itemnumber (@itemnumbers) { |
155 |
foreach my $itemnumber (@itemnumbers) { |
129 |
my $item = Koha::Items->find($itemnumber); |
156 |
my $item = Koha::Items->find($itemnumber); |
130 |
next |
157 |
next |
131 |
unless $item |
158 |
unless $item |
132 |
; # Should have been tested earlier, but just in case... |
159 |
; # Should have been tested earlier, but just in case... |
133 |
my $itemdata = $item->unblessed; |
160 |
my $itemdata = $item->unblessed; |
|
|
161 |
if ($del) { |
162 |
my $return = $item->safe_delete; |
134 |
my $return = $item->safe_delete; |
163 |
if ( ref( $return ) ) { |
135 |
if ( ref( $return ) ) { |
164 |
$deleted_items++; |
136 |
$deleted_items++; |
Lines 192-291
if ($op eq "action") {
Link Here
|
192 |
} |
164 |
} |
193 |
} |
165 |
} |
194 |
} |
166 |
} |
195 |
else { |
167 |
if (@not_deleted) { |
196 |
my $modified_holds_priority = 0; |
168 |
Koha::Exceptions::Exception->throw( |
197 |
if ( defined $exclude_from_local_holds_priority && $exclude_from_local_holds_priority ne "" ) { |
169 |
'Some items have not been deleted, rolling back'); |
198 |
if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) { |
170 |
} |
199 |
$item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store; |
171 |
} |
200 |
$modified_holds_priority = 1; |
172 |
); |
201 |
} |
173 |
} |
202 |
} |
174 |
catch { |
203 |
my $modified = 0; |
175 |
warn $_; |
204 |
if ( $values_to_modify || $values_to_blank ) { |
176 |
if ( $_->isa('Koha::Exceptions::Exception') ) { |
205 |
my $localmarcitem = Item2Marc($itemdata); |
177 |
$template->param( deletion_failed => 1 ); |
|
|
178 |
} |
179 |
die "Something terrible has happened!" |
180 |
if ($_ =~ /Rollback failed/); # Rollback failed |
181 |
}; |
182 |
} |
206 |
|
183 |
|
207 |
for ( my $i = 0 ; $i < @tags ; $i++ ) { |
184 |
else { # modification |
208 |
my $search = $searches[$i]; |
|
|
209 |
next unless $search; |
210 |
|
185 |
|
211 |
my $tag = $tags[$i]; |
186 |
my @columns = Koha::Items->columns; |
212 |
my $subfield = $subfields[$i]; |
|
|
213 |
my $replace = $replaces[$i]; |
214 |
|
187 |
|
215 |
my $value = $localmarcitem->field( $tag )->subfield( $subfield ); |
188 |
my $new_item_data; |
216 |
my $old_value = $value; |
189 |
my @columns_with_regex; |
|
|
190 |
for my $c ( @columns ) { |
191 |
if ( $c eq 'more_subfields_xml' ) { |
192 |
my @more_subfields_xml = $input->multi_param("items.more_subfields_xml"); |
193 |
my @unlinked_item_subfields; |
194 |
for my $subfield ( @more_subfields_xml ) { |
195 |
my $v = $input->param('items.more_subfields_xml_' . $subfield); |
196 |
push @unlinked_item_subfields, $subfield, $v; |
197 |
} |
198 |
if ( @unlinked_item_subfields ) { |
199 |
my $marc = MARC::Record->new(); |
200 |
# use of tag 999 is arbitrary, and doesn't need to match the item tag |
201 |
# used in the framework |
202 |
$marc->append_fields(MARC::Field->new('999', ' ', ' ', @unlinked_item_subfields)); |
203 |
$marc->encoding("UTF-8"); |
204 |
# FIXME This is WRONG! We need to use the values that haven't been modified by the batch tool! |
205 |
$new_item_data->{more_subfields_xml} = $marc->as_xml("USMARC"); |
206 |
next; |
207 |
} |
208 |
$new_item_data->{more_subfields_xml} = undef; |
209 |
# FIXME deal with more_subfields_xml and @subfields_to_blank |
210 |
} elsif ( grep { $c eq $_ } @subfields_to_blank ) { |
211 |
# Empty this column |
212 |
$new_item_data->{$c} = undef |
213 |
} else { |
217 |
|
214 |
|
218 |
my @available_modifiers = qw( i g ); |
215 |
my @v = grep { $_ ne "" } |
219 |
my $retained_modifiers = q||; |
216 |
uniq $input->multi_param( "items." . $c ); |
220 |
for my $modifier ( split //, $modifiers[$i] ) { |
|
|
221 |
$retained_modifiers .= $modifier |
222 |
if grep {/$modifier/} @available_modifiers; |
223 |
} |
224 |
if ( $retained_modifiers =~ m/^(ig|gi)$/ ) { |
225 |
$value =~ s/$search/$replace/ig; |
226 |
} |
227 |
elsif ( $retained_modifiers eq 'i' ) { |
228 |
$value =~ s/$search/$replace/i; |
229 |
} |
230 |
elsif ( $retained_modifiers eq 'g' ) { |
231 |
$value =~ s/$search/$replace/g; |
232 |
} |
233 |
else { |
234 |
$value =~ s/$search/$replace/; |
235 |
} |
236 |
|
217 |
|
237 |
my @fields_to = $localmarcitem->field($tag); |
218 |
next unless @v; |
238 |
foreach my $field_to_update ( @fields_to ) { |
219 |
|
239 |
unless ( $old_value eq $value ) { |
220 |
$new_item_data->{$c} = join ' | ', @v; |
240 |
$modified++; |
221 |
} |
241 |
$field_to_update->update( $subfield => $value ); |
222 |
|
242 |
} |
223 |
if ( my $regex_search = $input->param('items.'.$c.'_regex_search') ) { |
243 |
} |
224 |
push @columns_with_regex, $c; |
|
|
225 |
} |
226 |
} |
227 |
|
228 |
try { |
229 |
my $schema = Koha::Database->new->schema; |
230 |
$schema->txn_do( |
231 |
sub { |
232 |
|
233 |
foreach my $itemnumber (@itemnumbers) { |
234 |
my $item = Koha::Items->find($itemnumber); |
235 |
next |
236 |
unless $item |
237 |
; # Should have been tested earlier, but just in case... |
238 |
my $itemdata = $item->unblessed; |
239 |
|
240 |
my $modified_holds_priority = 0; |
241 |
if ( defined $exclude_from_local_holds_priority && $exclude_from_local_holds_priority ne "" ) { |
242 |
if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) { |
243 |
$item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store; |
244 |
$modified_holds_priority = 1; |
244 |
} |
245 |
} |
|
|
246 |
} |
245 |
|
247 |
|
246 |
$modified += UpdateMarcWith( $marcitem, $localmarcitem ); |
248 |
my $modified = 0; |
247 |
if ($modified) { |
249 |
for my $c ( @columns_with_regex ) { |
248 |
eval { |
250 |
my $regex_search = $input->param('items.'.$c.'_regex_search'); |
249 |
if ( |
251 |
my $old_value = $item->$c; |
250 |
my $item = ModItemFromMarc( |
252 |
|
251 |
$localmarcitem, |
253 |
my $value = apply_regex( |
252 |
$itemdata->{biblionumber}, |
254 |
{ |
253 |
$itemnumber, |
255 |
search => $regex_search, |
254 |
{ skip_record_index => 1 }, |
256 |
replace => $input->param( |
255 |
) |
257 |
'items' . $c . '_regex_replace' |
256 |
) |
258 |
), |
257 |
{ |
259 |
modifiers => $input->param( |
258 |
LostItem( |
260 |
'items' . $c . '_regex_modifiers' |
259 |
$itemnumber, |
261 |
), |
260 |
'batchmod', |
262 |
value => $old_value, |
261 |
undef, |
263 |
} |
262 |
{ skip_record_index => 1 } |
264 |
); |
263 |
) if $item->{itemlost} |
265 |
unless ( $old_value eq $value ) { |
264 |
and not $itemdata->{itemlost}; |
266 |
$modified++; |
265 |
} |
267 |
$item->$c($value); |
266 |
}; |
|
|
267 |
push @$upd_biblionumbers, $itemdata->{'biblionumber'}; |
268 |
} |
268 |
} |
269 |
} |
269 |
} |
|
|
270 |
|
271 |
$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? |
272 |
if ( $modified) { |
273 |
my $itemlost_pre = $item->itemlost; |
274 |
$item->set($new_item_data)->store({skip_record_index => 1}); |
275 |
|
276 |
push @$upd_biblionumbers, $itemdata->{'biblionumber'}; |
277 |
|
278 |
LostItem( |
279 |
$item->itemnumber, 'batchmod', undef, |
280 |
{ skip_record_index => 1 } |
281 |
) if $item->itemlost |
282 |
and not $itemlost_pre; |
283 |
} |
284 |
|
270 |
$modified_items++ if $modified || $modified_holds_priority; |
285 |
$modified_items++ if $modified || $modified_holds_priority; |
271 |
$modified_fields += $modified + $modified_holds_priority; |
286 |
$modified_fields += $modified + $modified_holds_priority; |
272 |
} |
287 |
} |
273 |
$i++; |
|
|
274 |
} |
275 |
if (@not_deleted) { |
276 |
Koha::Exceptions::Exception->throw( |
277 |
'Some items have not been deleted, rolling back'); |
278 |
} |
288 |
} |
279 |
} |
289 |
); |
280 |
); |
|
|
281 |
} |
282 |
catch { |
283 |
if ( $_->isa('Koha::Exceptions::Exception') ) { |
284 |
$template->param( deletion_failed => 1 ); |
285 |
} |
290 |
} |
286 |
die "Something terrible has happened!" |
291 |
catch { |
287 |
if ($_ =~ /Rollback failed/); # Rollback failed |
292 |
warn $_; |
288 |
}; |
293 |
die "Something terrible has happened!" |
|
|
294 |
if ($_ =~ /Rollback failed/); # Rollback failed |
295 |
}; |
296 |
} |
297 |
|
289 |
$upd_biblionumbers = [ uniq @$upd_biblionumbers ]; # Only update each bib once |
298 |
$upd_biblionumbers = [ uniq @$upd_biblionumbers ]; # Only update each bib once |
290 |
|
299 |
|
291 |
# Don't send specialUpdate for records we are going to delete |
300 |
# Don't send specialUpdate for records we are going to delete |
Lines 320-325
if ($op eq "action") {
Link Here
|
320 |
$template->param( "job_completed" => 1 ); |
329 |
$template->param( "job_completed" => 1 ); |
321 |
} |
330 |
} |
322 |
|
331 |
|
|
|
332 |
|
323 |
# Calling the template |
333 |
# Calling the template |
324 |
$template->param( |
334 |
$template->param( |
325 |
modified_items => $modified_items, |
335 |
modified_items => $modified_items, |
Lines 588-590
sub UpdateMarcWith {
Link Here
|
588 |
} |
598 |
} |
589 |
return $modified; |
599 |
return $modified; |
590 |
} |
600 |
} |
591 |
- |
601 |
|
|
|
602 |
sub apply_regex { |
603 |
my ($params) = @_; |
604 |
my $search = $params->{search}; |
605 |
my $replace = $params->{replace}; |
606 |
my $modifiers = $params->{modifiers} || []; |
607 |
my $value = $params->{value}; |
608 |
|
609 |
my @available_modifiers = qw( i g ); |
610 |
my $retained_modifiers = q||; |
611 |
for my $modifier ( split //, @$modifiers ) { |
612 |
$retained_modifiers .= $modifier |
613 |
if grep { /$modifier/ } @available_modifiers; |
614 |
} |
615 |
if ( $retained_modifiers =~ m/^(ig|gi)$/ ) { |
616 |
$value =~ s/$search/$replace/ig; |
617 |
} |
618 |
elsif ( $retained_modifiers eq 'i' ) { |
619 |
$value =~ s/$search/$replace/i; |
620 |
} |
621 |
elsif ( $retained_modifiers eq 'g' ) { |
622 |
$value =~ s/$search/$replace/g; |
623 |
} |
624 |
else { |
625 |
$value =~ s/$search/$replace/; |
626 |
} |
627 |
|
628 |
return $value; |
629 |
} |