4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
32 use C4
::BackgroundJob
;
37 use List
::MoreUtils qw
/uniq/;
40 use Koha
::Exceptions
::Exception
;
41 use Koha
::AuthorisedValues
;
49 my $dbh = C4
::Context
->dbh;
50 my $error = $input->param('error');
51 my @itemnumbers = $input->multi_param('itemnumber');
52 my $biblionumber = $input->param('biblionumber');
53 my $op = $input->param('op');
54 my $del = $input->param('del');
55 my $del_records = $input->param('del_records');
56 my $completedJobID = $input->param('completedJobID');
57 my $runinbackground = $input->param('runinbackground');
58 my $src = $input->param('src');
59 my $use_default_values = $input->param('use_default_values');
60 my $exclude_from_local_holds_priority = $input->param('exclude_from_local_holds_priority');
65 $template_name = "tools/batchMod.tt";
66 $template_flag = { tools
=> '*' };
69 $template_name = ($del) ?
"tools/batchMod-del.tt" : "tools/batchMod-edit.tt";
70 $template_flag = ($del) ?
{ tools
=> 'items_batchdel' } : { tools
=> 'items_batchmod' };
73 my ($template, $loggedinuser, $cookie)
74 = get_template_and_user
({template_name
=> $template_name,
77 flagsrequired
=> $template_flag,
80 $template->param( searchid
=> scalar $input->param('searchid'), );
82 # Does the user have a restricted item edition permission?
83 my $uid = $loggedinuser ? Koha
::Patrons
->find( $loggedinuser )->userid : undef;
84 my $restrictededition = $uid ? haspermission
($uid, {'tools' => 'items_batchmod_restricted'}) : undef;
85 # In case user is a superlibrarian, edition is not restricted
86 $restrictededition = 0 if ($restrictededition != 0 && C4
::Context
->IsSuperLibrarian());
88 $template->param(del
=> $del);
91 my @errors; # store errors found while checking data BEFORE saving item.
92 my $items_display_hashref;
93 our $tagslib = &GetMarcStructure
(1);
95 my $deleted_items = 0; # Number of deleted items
96 my $deleted_records = 0; # Number of deleted records ( with no items attached )
97 my $not_deleted_items = 0; # Number of items that could not be deleted
98 my @not_deleted; # List of the itemnumbers that could not be deleted
99 my $modified_items = 0; # Numbers of modified items
100 my $modified_fields = 0; # Numbers of modified fields
102 my %cookies = parse CGI
::Cookie
($cookie);
103 my $sessionID = $cookies{'CGISESSID'}->value;
106 #--- ----------------------------------------------------------------------------
107 if ($op eq "action") {
108 #-------------------------------------------------------------------------------
109 my @tags = $input->multi_param('tag');
110 my @subfields = $input->multi_param('subfield');
111 my @values = $input->multi_param('field_value');
112 my @searches = $input->multi_param('regex_search');
113 my @replaces = $input->multi_param('regex_replace');
114 my @modifiers = $input->multi_param('regex_modifiers');
115 my @disabled = $input->multi_param('disable_input');
116 # build indicator hash.
117 my @ind_tag = $input->multi_param('ind_tag');
118 my @indicator = $input->multi_param('indicator');
120 # Is there something to modify ?
121 # TODO : We shall use this var to warn the user in case no modification was done to the items
122 my $values_to_modify = scalar(grep {!/^$/} @values) || scalar(grep {!/^$/} @searches);
123 my $values_to_blank = scalar(@disabled);
127 # Once the job is done
128 if ($completedJobID) {
129 # If we have a reasonable amount of items, we display them
130 my $max_items = $del ? C4
::Context
->preference("MaxItemsToDisplayForBatchDel") : C4
::Context
->preference("MaxItemsToDisplayForBatchMod");
131 if (scalar(@itemnumbers) <= $max_items ){
132 if (scalar(@itemnumbers) <= 1000 ) {
133 $items_display_hashref=BuildItemsData
(@itemnumbers);
135 # Else, we only display the barcode
136 my @simple_items_display = map {
138 my $item = Koha
::Items
->find($itemnumber);
140 itemnumber
=> $itemnumber,
141 barcode
=> $item ?
( $item->barcode // q{} ) : q{},
142 biblionumber
=> $item ?
$item->biblio->biblionumber : q{},
145 $template->param("simple_items_display" => \
@simple_items_display);
148 $template->param( "too_many_items_display" => scalar(@itemnumbers) );
149 $template->param( "job_completed" => 1 );
152 # Setting the job as done
153 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
155 # Calling the template
156 add_saved_job_results_to_template
($template, $completedJobID, $items_display_hashref);
159 # While the job is getting done
161 # Job size is the number of items we have to process
162 my $job_size = scalar(@itemnumbers);
165 # If we asked for background processing
166 if ($runinbackground) {
167 $job = put_in_background
($job_size);
170 #initializing values for updates
171 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
( "items.itemnumber" );
172 if ($values_to_modify){
173 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag, 'ITEM');
174 $marcitem = MARC
::Record
::new_from_xml
($xml, 'UTF-8');
176 if ($values_to_blank){
177 foreach my $disabledsubf (@disabled){
178 if ($marcitem && $marcitem->field($itemtagfield)){
179 $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
182 $marcitem = MARC
::Record
->new();
183 $marcitem->append_fields( MARC
::Field
->new( $itemtagfield, '', '', $disabledsubf => "" ) );
188 my $yesno = Koha
::AuthorisedValues
->search({category
=> 'YES_NO'});
191 while(my $yn = $yesno->next) {
192 $ynhash->{'av'.$yn->authorised_value} = $yn->lib;
196 my $schema = Koha
::Database
->new->schema;
201 my $extra_headers = {};
202 foreach my $itemnumber (@itemnumbers) {
203 $job->progress($i) if $runinbackground;
204 my $item = Koha
::Items
->find($itemnumber);
207 ; # Should have been tested earlier, but just in case...
208 my $itemdata = $item->unblessed;
210 my $return = $item->safe_delete;
211 if ( ref( $return ) ) {
215 $not_deleted_items++;
218 biblionumber
=> $itemdata->{'biblionumber'},
219 itemnumber
=> $itemdata->{'itemnumber'},
220 barcode
=> $itemdata->{'barcode'},
221 title
=> $itemdata->{'title'},
226 # If there are no items left, delete the biblio
228 my $itemscount = Koha
::Biblios
->find( $itemdata->{'biblionumber'} )->items->count;
229 if ( $itemscount == 0 ) {
230 my $error = DelBiblio
( $itemdata->{'biblionumber'} );
233 if ( $src eq 'CATALOGUING' ) {
234 # We are coming catalogue/detail.pl, there were items from a single bib record
235 $template->param( biblio_deleted
=> 1 );
242 my $modified_holds_priority = 0;
243 if ( defined $exclude_from_local_holds_priority && $exclude_from_local_holds_priority ne "" ) {
244 if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) {
245 $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store;
246 $modified_holds_priority = 1;
248 $extra_headers->{exclude_from_local_holds_priority
} = {name
=> 'Exclude from local holds priority', items
=> {}} unless defined $extra_headers->{exclude_from_local_holds_priority
};
249 $extra_headers->{exclude_from_local_holds_priority
}->{items
}->{$item->itemnumber} = $ynhash->{'av'.$item->exclude_from_local_holds_priority};
252 if ( $values_to_modify || $values_to_blank ) {
253 my $localmarcitem = Item2Marc
($itemdata);
255 for ( my $i = 0 ; $i < @tags ; $i++ ) {
256 my $search = $searches[$i];
260 my $subfield = $subfields[$i];
261 my $replace = $replaces[$i];
263 my $value = $localmarcitem->field( $tag )->subfield( $subfield );
264 my $old_value = $value;
266 my @available_modifiers = qw( i g );
267 my $retained_modifiers = q
||;
268 for my $modifier ( split //, $modifiers[$i] ) {
269 $retained_modifiers .= $modifier
270 if grep {/$modifier/} @available_modifiers;
272 if ( $retained_modifiers =~ m/^(ig|gi)$/ ) {
273 $value =~ s/$search/$replace/ig;
275 elsif ( $retained_modifiers eq 'i' ) {
276 $value =~ s/$search/$replace/i;
278 elsif ( $retained_modifiers eq 'g' ) {
279 $value =~ s/$search/$replace/g;
282 $value =~ s/$search/$replace/;
285 my @fields_to = $localmarcitem->field($tag);
286 foreach my $field_to_update ( @fields_to ) {
287 unless ( $old_value eq $value ) {
289 $field_to_update->update( $subfield => $value );
294 $modified += UpdateMarcWith
( $marcitem, $localmarcitem );
298 my $item = ModItemFromMarc
(
300 $itemdata->{biblionumber
},
305 LostItem
( $itemnumber, 'batchmod' )
307 and not $itemdata->{itemlost
};
312 if ($runinbackground) {
313 $modified_items++ if $modified || $modified_holds_priority;
314 $modified_fields += $modified + $modified_holds_priority;
317 modified_items
=> $modified_items,
318 modified_fields
=> $modified_fields,
319 extra_headers
=> $extra_headers,
327 Koha
::Exceptions
::Exception
->throw(
328 'Some items have not been deleted, rolling back');
334 if ( $_->isa('Koha::Exceptions::Exception') ) {
335 $template->param( deletion_failed
=> 1 );
337 die "Something terrible has happened!"
338 if ($_ =~ /Rollback failed/); # Rollback failed
343 #-------------------------------------------------------------------------------
344 # build screen with existing items. and "new" one
345 #-------------------------------------------------------------------------------
348 my $filefh = $input->upload('uploadfile');
349 my $filecontent = $input->param('filecontent');
350 my ( @notfoundbarcodes, @notfounditemnumbers);
352 my $split_chars = C4
::Context
->preference('BarcodeSeparators');
354 binmode $filefh, ':encoding(UTF-8)';
356 while (my $content=<$filefh>){
357 $content =~ s/[\r\n]*$//;
358 push @contentlist, $content if $content;
361 if ($filecontent eq 'barcode_file') {
362 @contentlist = grep /\S/, ( map { split /[$split_chars]/ } @contentlist );
363 @contentlist = uniq
@contentlist;
364 # Note: adding lc for case insensitivity
365 my %itemdata = map { lc($_->{barcode
}) => $_->{itemnumber
} } @
{ Koha
::Items
->search({ barcode
=> \
@contentlist }, { columns
=> [ 'itemnumber', 'barcode' ] } )->unblessed };
366 @itemnumbers = map { exists $itemdata{lc $_} ?
$itemdata{lc $_} : () } @contentlist;
367 @notfoundbarcodes = grep { !exists $itemdata{lc $_} } @contentlist;
369 elsif ( $filecontent eq 'itemid_file') {
370 @contentlist = uniq
@contentlist;
371 my %itemdata = map { $_->{itemnumber
} => 1 } @
{ Koha
::Items
->search({ itemnumber
=> \
@contentlist }, { columns
=> [ 'itemnumber' ] } )->unblessed };
372 @itemnumbers = grep { exists $itemdata{$_} } @contentlist;
373 @notfounditemnumbers = grep { !exists $itemdata{$_} } @contentlist;
376 if (defined $biblionumber && !@itemnumbers){
377 my @all_items = GetItemsInfo
( $biblionumber );
378 foreach my $itm (@all_items) {
379 push @itemnumbers, $itm->{itemnumber
};
382 if ( my $list = $input->param('barcodelist') ) {
383 my @barcodelist = grep /\S/, ( split /[$split_chars]/, $list );
384 @barcodelist = uniq
@barcodelist;
385 # Note: adding lc for case insensitivity
386 my %itemdata = map { lc($_->{barcode
}) => $_->{itemnumber
} } @
{ Koha
::Items
->search({ barcode
=> \
@barcodelist }, { columns
=> [ 'itemnumber', 'barcode' ] } )->unblessed };
387 @itemnumbers = map { exists $itemdata{lc $_} ?
$itemdata{lc $_} : () } @barcodelist;
388 @notfoundbarcodes = grep { !exists $itemdata{lc $_} } @barcodelist;
392 # Flag to tell the template there are valid results, hidden or not
393 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
394 # Only display the items if there are no more than pref MaxItemsToProcessForBatchMod or MaxItemsToDisplayForBatchDel
395 my $max_display_items = $del
396 ? C4
::Context
->preference("MaxItemsToDisplayForBatchDel")
397 : C4
::Context
->preference("MaxItemsToDisplayForBatchMod");
398 $template->param("too_many_items_process" => scalar(@itemnumbers)) if !$del && scalar(@itemnumbers) >= C4
::Context
->preference("MaxItemsToProcessForBatchMod");
399 if (scalar(@itemnumbers) <= ( $max_display_items // 1000 ) ) {
400 $items_display_hashref=BuildItemsData
(@itemnumbers);
402 $template->param("too_many_items_display" => scalar(@itemnumbers));
403 # Even if we do not display the items, we need the itemnumbers
404 $template->param(itemnumbers_array
=> \
@itemnumbers);
406 # now, build the item form for entering a new item
409 my $branch_limit = C4
::Context
->userenv ? C4
::Context
->userenv->{"branch"} : "";
411 my $libraries = Koha
::Libraries
->search({}, { order_by
=> ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
413 # Adding a default choice, in case the user does not want to modify the branch
414 my $nochange_branch = { branchname
=> '', value
=> '', selected
=> 1 };
415 unshift (@
$libraries, $nochange_branch);
417 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
419 # Getting list of subfields to keep when restricted batchmod edit is enabled
420 my $subfieldsToAllowForBatchmod = C4
::Context
->preference('SubfieldsToAllowForRestrictedBatchmod');
421 my $allowAllSubfields = (
422 not defined $subfieldsToAllowForBatchmod
423 or $subfieldsToAllowForBatchmod eq q
||
425 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForBatchmod);
427 foreach my $tag (sort keys %{$tagslib}) {
428 # loop through each subfield
429 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
430 next if IsMarcStructureInternal
( $tagslib->{$tag}{$subfield} );
431 next if (not $allowAllSubfields and $restrictededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow );
432 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
433 # barcode and stocknumber are not meant to be batch-modified
434 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
435 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
438 my $index_subfield = int(rand(1000000));
439 if ($subfield eq '@'){
440 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_subfield;
442 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
444 $subfield_data{tag
} = $tag;
445 $subfield_data{subfield
} = $subfield;
446 $subfield_data{marc_lib
} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib
}."\">".$tagslib->{$tag}->{$subfield}->{lib
}."</span>";
447 $subfield_data{mandatory
} = $tagslib->{$tag}->{$subfield}->{mandatory
};
448 $subfield_data{repeatable
} = $tagslib->{$tag}->{$subfield}->{repeatable
};
450 if ( $use_default_values) {
451 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue
};
452 # get today date & replace YYYY, MM, DD if provided in the default value
453 my $today = dt_from_string
;
454 my $year = $today->year;
455 my $month = $today->month;
456 my $day = $today->day;
457 $value =~ s/YYYY/$year/g;
458 $value =~ s/MM/$month/g;
459 $value =~ s/DD/$day/g;
461 $subfield_data{visibility
} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden
} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden
} < -4));
462 # testing branch value if IndependentBranches.
464 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
465 my @authorised_values;
467 # builds list, depending on authorised value...
469 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "branches" ) {
470 foreach my $library (@
$libraries) {
471 push @authorised_values, $library->{branchcode
};
472 $authorised_lib{$library->{branchcode
}} = $library->{branchname
};
476 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
477 push @authorised_values, "";
478 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
479 while ( my $itemtype = $itemtypes->next ) {
480 push @authorised_values, $itemtype->itemtype;
481 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
487 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "cn_source" ) {
488 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
490 my $class_sources = GetClassSources
();
491 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
493 foreach my $class_source (sort keys %$class_sources) {
494 next unless $class_sources->{$class_source}->{'used'} or
495 ($value and $class_source eq $value) or
496 ($class_source eq $default_source);
497 push @authorised_values, $class_source;
498 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
502 #---- "true" authorised value
505 push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
507 my @avs = Koha
::AuthorisedValues
->search({ category
=> $tagslib->{$tag}->{$subfield}->{authorised_value
}, branchcode
=> $branch_limit },{order_by
=>'lib'});
508 for my $av ( @avs ) {
509 push @authorised_values, $av->authorised_value;
510 $authorised_lib{$av->authorised_value} = $av->lib;
514 $subfield_data{marc_value
} = {
516 id
=> "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
517 name
=> "field_value",
518 values => \
@authorised_values,
519 labels
=> \
%authorised_lib,
522 # it's a thesaurus / authority field
524 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
525 $subfield_data{marc_value
} = {
527 id
=> $subfield_data{id
},
529 authtypecode
=> $tagslib->{$tag}->{$subfield}->{authtypecode
},
532 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder
} ) { # plugin
533 require Koha
::FrameworkPlugin
;
534 my $plugin = Koha
::FrameworkPlugin
->new( {
535 name
=> $tagslib->{$tag}->{$subfield}->{'value_builder'},
539 my $pars= { dbh
=> $dbh, record
=> $temp, tagslib
=> $tagslib,
540 id
=> $subfield_data{id
}, tabloop
=> \
@loop_data };
541 $plugin->build( $pars );
542 if( !$plugin->errstr ) {
543 $subfield_data{marc_value
} = {
545 id
=> $subfield_data{id
},
547 javascript
=> $plugin->javascript,
548 noclick
=> $plugin->noclick,
551 warn $plugin->errstr;
552 $subfield_data{marc_value
} = { # supply default input form
554 id
=> $subfield_data{id
},
559 elsif ( $tag eq '' ) { # it's an hidden field
560 $subfield_data{marc_value
} = {
562 id
=> $subfield_data{id
},
566 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
567 $subfield_data{marc_value
} = {
569 id
=> $subfield_data{id
},
573 elsif ( length($value) > 100
574 or (C4
::Context
->preference("marcflavour") eq "UNIMARC" and
575 300 <= $tag && $tag < 400 && $subfield eq 'a' )
576 or (C4
::Context
->preference("marcflavour") eq "MARC21" and
577 500 <= $tag && $tag < 600 )
579 # oversize field (textarea)
580 $subfield_data{marc_value
} = {
582 id
=> $subfield_data{id
},
586 # it's a standard field
587 $subfield_data{marc_value
} = {
589 id
=> $subfield_data{id
},
593 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
594 push (@loop_data, \
%subfield_data);
597 } # -- End foreach tag
601 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
604 notfoundbarcodes
=> \
@notfoundbarcodes,
605 notfounditemnumbers
=> \
@notfounditemnumbers
608 } # -- End action="show"
610 $template->param(%$items_display_hashref) if $items_display_hashref;
614 $template->param( $op => 1 ) if $op;
616 if ($op eq "action") {
618 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
621 not_deleted_items
=> $not_deleted_items,
622 deleted_items
=> $deleted_items,
623 delete_records
=> $del_records,
624 deleted_records
=> $deleted_records,
625 not_deleted_loop
=> \
@not_deleted
629 foreach my $error (@errors) {
630 $template->param($error => 1) if $error;
632 $template->param(src
=> $src);
633 $template->param(biblionumber
=> $biblionumber);
634 output_html_with_http_headers
$input, $cookie, $template->output;
638 # ---------------- Functions
642 # now, build existiing item list
643 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
645 #---- finds where items.itemnumber is stored
646 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
( "items.itemnumber" );
647 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
( "items.homebranch" );
648 foreach my $itemnumber (@itemnumbers){
649 my $itemdata = Koha
::Items
->find($itemnumber);
650 next unless $itemdata; # Should have been tested earlier, but just in case...
651 $itemdata = $itemdata->unblessed;
652 my $itemmarc=Item2Marc
($itemdata);
654 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
655 # loop through each subfield
656 my $itembranchcode=$field->subfield($branchtagsubfield);
657 if ($itembranchcode && C4
::Context
->preference("IndependentBranches")) {
659 my $userenv = C4
::Context
->userenv();
660 unless (C4
::Context
->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){
661 $this_row{'nomod'}=1;
664 my $tag=$field->tag();
665 foreach my $subfield ($field->subfields) {
666 my ($subfcode,$subfvalue)=@
$subfield;
667 next if ($tagslib->{$tag}->{$subfcode}->{tab
} ne 10
668 && $tag ne $itemtagfield
669 && $subfcode ne $itemtagsubfield);
671 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib
} if ($tagslib->{$tag}->{$subfcode}->{tab
} eq 10);
672 if ($tagslib->{$tag}->{$subfcode}->{tab
} eq 10) {
673 $this_row{$subfcode}=GetAuthorisedValueDesc
( $tag,
674 $subfcode, $subfvalue, '', $tagslib)
678 $this_row{itemnumber
} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
682 # grab title, author, and ISBN to identify bib that the item
683 # belongs to in the display
684 my $biblio = Koha
::Biblios
->find( $itemdata->{biblionumber
} );
685 $this_row{title
} = $biblio->title;
686 $this_row{author
} = $biblio->author;
687 $this_row{isbn
} = $biblio->biblioitem->isbn;
688 $this_row{biblionumber
} = $biblio->biblionumber;
689 $this_row{holds
} = $biblio->holds->count;
690 $this_row{item_holds
} = Koha
::Holds
->search( { itemnumber
=> $itemnumber } )->count;
691 $this_row{item
} = Koha
::Items
->find($itemnumber);
694 push(@big_array, \
%this_row);
697 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
699 # now, construct template !
700 # First, the existing items for display
702 my @witnesscodessorted=sort keys %witness;
703 for my $row ( @big_array ) {
705 my @item_fields = map +{ field
=> $_ || '' }, @
$row{ @witnesscodessorted };
706 $row_data{item_value
} = [ @item_fields ];
707 $row_data{itemnumber
} = $row->{itemnumber
};
708 #reporting this_row values
709 $row_data{'nomod'} = $row->{'nomod'};
710 $row_data{bibinfo
} = $row->{bibinfo
};
711 $row_data{author
} = $row->{author
};
712 $row_data{title
} = $row->{title
};
713 $row_data{isbn
} = $row->{isbn
};
714 $row_data{biblionumber
} = $row->{biblionumber
};
715 $row_data{holds
} = $row->{holds
};
716 $row_data{item_holds
} = $row->{item_holds
};
717 $row_data{item
} = $row->{item
};
718 my $is_on_loan = C4
::Circulation
::IsItemIssued
( $row->{itemnumber
} );
719 $row_data{onloan
} = $is_on_loan ?
1 : 0;
720 push(@item_value_loop,\
%row_data);
722 my @header_loop=map { { header_value
=> $witness{$_}} } @witnesscodessorted;
724 return { item_loop
=> \
@item_value_loop, item_header_loop
=> \
@header_loop };
727 #BE WARN : it is not the general case
728 # This function can be OK in the item marc record special case
729 # Where subfield is not repeated
730 # And where we are sure that field should correspond
733 my ($marcfrom,$marcto)=@_;
734 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField
( "items.itemnumber" );
735 my $fieldfrom=$marcfrom->field($itemtag);
736 my @fields_to=$marcto->field($itemtag);
739 return $modified unless $fieldfrom;
741 foreach my $subfield ( $fieldfrom->subfields() ) {
742 foreach my $field_to_update ( @fields_to ) {
743 if ( $subfield->[1] ) {
744 unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) {
746 $field_to_update->update( $subfield->[0] => $subfield->[1] );
751 $field_to_update->delete_subfield( code
=> $subfield->[0] );
759 my ($tagfield,$insubfield,$record) = @_;
762 foreach my $field ($record->field($tagfield)) {
763 my @subfields = $field->subfields();
764 foreach my $subfield (@subfields) {
765 if (@
$subfield[0] eq $insubfield) {
766 $result .= @
$subfield[1];
767 $indicator = $field->indicator(1).$field->indicator(2);
771 return($indicator,$result);
774 # ----------------------------
775 # Background functions
778 sub add_results_to_template
{
779 my $template = shift;
781 $template->param(map { $_ => $results->{$_} } keys %{ $results });
784 sub add_saved_job_results_to_template
{
785 my $template = shift;
786 my $completedJobID = shift;
787 my $items_display_hashref= shift;
788 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
789 my $results = $job->results();
790 add_results_to_template
($template, $results);
792 my $fields = $job->get("modified_fields");
793 my $items = $job->get("modified_items");
794 my $extra_headers = $job->get("extra_headers");
796 foreach my $header (keys %{$extra_headers}) {
797 push @
{$items_display_hashref->{item_header_loop
}}, {header_value
=> $extra_headers->{$header}->{name
}};
798 foreach my $row (@
{$items_display_hashref->{item_loop
}}) {
799 push @
{$row->{item_value
}}, {field
=> $extra_headers->{$header}->{items
}->{$row->{itemnumber
}}};
804 modified_items
=> $items,
805 modified_fields
=> $fields,
809 sub put_in_background
{
810 my $job_size = shift;
812 my $job = C4
::BackgroundJob
->new($sessionID, "test", '/cgi-bin/koha/tools/batchMod.pl', $job_size);
813 my $jobID = $job->id();
816 if (my $pid = fork) {
818 # return job ID as JSON
820 # prevent parent exiting from
821 # destroying the kid's database handle
822 # FIXME: according to DBI doc, this may not work for Oracle
823 $dbh->{InactiveDestroy
} = 1;
825 my $reply = CGI
->new("");
826 print $reply->header(-type
=> 'text/html');
827 print '{"jobID":"' . $jobID . '"}';
829 } elsif (defined $pid) {
831 # close STDOUT to signal to Apache that
832 # we're now running in the background
836 # fork failed, so exit immediately
837 warn "fork failed while attempting to run tools/batchMod.pl as a background job";