-foreach my $error (@errors) {
- $template->param($error => 1) if $error;
-}
-$template->param(src => $src);
-$template->param(biblionumber => $biblionumber);
-output_html_with_http_headers $input, $cookie, $template->output;
-exit;
-
-
-# ---------------- Functions
-
-sub BuildItemsData{
- my @itemnumbers=@_;
- # now, build existiing item list
- my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
- my @big_array;
- #---- finds where items.itemnumber is stored
- my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField( "items.itemnumber" );
- my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField( "items.homebranch" );
- foreach my $itemnumber (@itemnumbers){
- my $itemdata = Koha::Items->find($itemnumber);
- next unless $itemdata; # Should have been tested earlier, but just in case...
- $itemdata = $itemdata->unblessed;
- my $itemmarc=Item2Marc($itemdata);
- my %this_row;
- foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
- # loop through each subfield
- my $itembranchcode=$field->subfield($branchtagsubfield);
- if ($itembranchcode && C4::Context->preference("IndependentBranches")) {
- #verifying rights
- my $userenv = C4::Context->userenv();
- unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){
- $this_row{'nomod'}=1;
- }
- }
- my $tag=$field->tag();
- foreach my $subfield ($field->subfields) {
- my ($subfcode,$subfvalue)=@$subfield;
- next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
- && $tag ne $itemtagfield
- && $subfcode ne $itemtagsubfield);
-
- $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
- if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
- $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
- $subfcode, $subfvalue, '', $tagslib)
- || $subfvalue;
- }
-
- $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
- }
- }
-
- # grab title, author, and ISBN to identify bib that the item
- # belongs to in the display
- my $biblio = Koha::Biblios->find( $itemdata->{biblionumber} );
- $this_row{title} = $biblio->title;
- $this_row{author} = $biblio->author;
- $this_row{isbn} = $biblio->biblioitem->isbn;
- $this_row{biblionumber} = $biblio->biblionumber;
- $this_row{holds} = $biblio->holds->count;
- $this_row{item_holds} = Koha::Holds->search( { itemnumber => $itemnumber } )->count;
- $this_row{item} = Koha::Items->find($itemnumber);
-
- if (%this_row) {
- push(@big_array, \%this_row);
- }
- }
- @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
-
- # now, construct template !
- # First, the existing items for display
- my @item_value_loop;
- my @witnesscodessorted=sort keys %witness;
- for my $row ( @big_array ) {
- my %row_data;
- my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
- $row_data{item_value} = [ @item_fields ];
- $row_data{itemnumber} = $row->{itemnumber};
- #reporting this_row values
- $row_data{'nomod'} = $row->{'nomod'};
- $row_data{bibinfo} = $row->{bibinfo};
- $row_data{author} = $row->{author};
- $row_data{title} = $row->{title};
- $row_data{isbn} = $row->{isbn};
- $row_data{biblionumber} = $row->{biblionumber};
- $row_data{holds} = $row->{holds};
- $row_data{item_holds} = $row->{item_holds};
- $row_data{item} = $row->{item};
- my $is_on_loan = C4::Circulation::IsItemIssued( $row->{itemnumber} );
- $row_data{onloan} = $is_on_loan ? 1 : 0;
- push(@item_value_loop,\%row_data);
- }
- my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
-
- return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
-}
-
-#BE WARN : it is not the general case
-# This function can be OK in the item marc record special case
-# Where subfield is not repeated
-# And where we are sure that field should correspond
-# And $tag>10
-sub UpdateMarcWith {
- my ($marcfrom,$marcto)=@_;
- my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField( "items.itemnumber" );
- my $fieldfrom=$marcfrom->field($itemtag);
- my @fields_to=$marcto->field($itemtag);
- my $modified = 0;
-
- return $modified unless $fieldfrom;
-
- foreach my $subfield ( $fieldfrom->subfields() ) {
- foreach my $field_to_update ( @fields_to ) {
- if ( $subfield->[1] ) {
- unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) {
- $modified++;
- $field_to_update->update( $subfield->[0] => $subfield->[1] );
- }
- }
- else {
- $modified++;
- $field_to_update->delete_subfield( code => $subfield->[0] );
- }
- }
- }
- return $modified;
-}
-
-sub find_value {
- my ($tagfield,$insubfield,$record) = @_;
- my $result;
- my $indicator;
- foreach my $field ($record->field($tagfield)) {
- my @subfields = $field->subfields();
- foreach my $subfield (@subfields) {
- if (@$subfield[0] eq $insubfield) {
- $result .= @$subfield[1];
- $indicator = $field->indicator(1).$field->indicator(2);
- }
- }
- }
- return($indicator,$result);
-}
-
-# ----------------------------
-# Background functions
-
-
-sub add_results_to_template {
- my $template = shift;
- my $results = shift;
- $template->param(map { $_ => $results->{$_} } keys %{ $results });
-}
-
-sub add_saved_job_results_to_template {
- my $template = shift;
- my $completedJobID = shift;
- my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
- my $results = $job->results();
- add_results_to_template($template, $results);
-
- my $fields = $job->get("modified_fields");
- my $items = $job->get("modified_items");
- $template->param(
- modified_items => $items,
- modified_fields => $fields,
- );
-}
-
-sub put_in_background {
- my $job_size = shift;
-
- my $job = C4::BackgroundJob->new($sessionID, "test", '/cgi-bin/koha/tools/batchMod.pl', $job_size);
- my $jobID = $job->id();
-
- # fork off
- if (my $pid = fork) {
- # parent
- # return job ID as JSON
-
- # prevent parent exiting from
- # destroying the kid's database handle
- # FIXME: according to DBI doc, this may not work for Oracle
- $dbh->{InactiveDestroy} = 1;
-
- my $reply = CGI->new("");
- print $reply->header(-type => 'text/html');
- print '{"jobID":"' . $jobID . '"}';
- exit 0;
- } elsif (defined $pid) {
- # child
- # close STDOUT to signal to Apache that
- # we're now running in the background
- close STDOUT;
- close STDERR;
- } else {
- # fork failed, so exit immediately
- warn "fork failed while attempting to run tools/batchMod.pl as a background job";
- exit 0;
- }
- return $job;
-}
-
-