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