@@ -, +, @@ --- C4/ImportBatch.pm | 115 +++++++++++++++++++- .../prog/en/includes/tools-overlay-action.inc | 21 +++- .../prog/en/modules/tools/manage-marc-import.tt | 1 + tools/manage-marc-import.pl | 12 +- tools/stage-marc-import.pl | 3 +- 5 files changed, 146 insertions(+), 6 deletions(-) --- a/C4/ImportBatch.pm +++ a/C4/ImportBatch.pm @@ -76,6 +76,8 @@ BEGIN { SetImportRecordStatus GetImportRecordMatches SetImportRecordMatches + GetImportMarcAction + SetImportMarcAction ); } @@ -205,7 +207,8 @@ sub AddImportBatch { my (@fields, @vals); foreach (qw( matcher_id template_id branchcode overlay_action nomatch_action item_action - import_status batch_type file_name comments record_type )) { + import_status batch_type file_name comments record_type marctag )) { + if (exists $params->{$_}) { push @fields, $_; push @vals, $params->{$_}; @@ -219,6 +222,34 @@ sub AddImportBatch { return $dbh->{'mysql_insertid'}; } +=head2 GetImportMarcAction + + my $marctag = GetImportMarcAction($batch_id); + +=cut + +sub GetImportMarcAction { + my ($batch_id) = @_; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("SELECT * FROM import_batches WHERE import_batch_id = ?"); + $sth->execute($batch_id); + return $sth->fetchrow_hashref; +} + +=head2 SetImportMarcAction + + SetImportMarcAction($batch_id, $marctag); + +=cut + +sub SetImportMarcAction { + my ($batch_id, $marctag) = @_; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("UPDATE import_batches SET marctag = ? WHERE import_batch_id = ?"); + $sth->execute($marctag, $batch_id); + return; +} + =head2 GetImportBatch my $row = GetImportBatch($batch_id); @@ -323,7 +354,7 @@ sub ModAuthInBatch { =cut -sub BatchStageMarcRecords { +sub BatchStageMarcRecords { my $record_type = shift; my $encoding = shift; my $marc_records = shift; @@ -332,6 +363,7 @@ sub BatchStageMarcRecords { my $branch_code = shift; my $parse_items = shift; my $leave_as_staging = shift; + my $marctag = shift; # optional callback to monitor status # of job @@ -351,6 +383,7 @@ sub BatchStageMarcRecords { file_name => $file_name, comments => $comments, record_type => $record_type, + marctag => $marctag, } ); if ($parse_items) { SetImportBatchItemAction($batch_id, 'always_add'); @@ -611,6 +644,82 @@ sub BatchCommitRecords { $sth->execute($recordid, $rowref->{'import_record_id'}); $sth->finish(); SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); + } elsif ($record_result eq 'appendmarctag') { + $num_updated++; + $recordid = $record_match; + my $tag = GetImportMarcAction($batch_id); + my $marctag = $tag->{'marctag'}; + my $mtag = substr($marctag,0,3); + my $sub = substr($marctag,3); + my $oldxml; + if ($record_type eq 'biblio') { + my ($count, $oldbiblio) = GetBiblio($recordid); + $oldxml = GetXmlBiblio($recordid); + my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'}, $marc_type); + foreach my $item_field ($old_marc->field($item_tag)) { + $old_marc->delete_field($item_field); + } + $oldxml = $old_marc->as_xml($marc_type); + + my $sth=$dbh->prepare("SELECT * FROM marc_tag_structure WHERE tagfield=? AND frameworkcode=?"); + if ($oldbiblio->{'frameworkcode'} eq '') { + $sth->execute($mtag,''); + } else { + $sth->execute($mtag,$oldbiblio->{'frameworkcode'}); + } + my $data=$sth->fetchrow_hashref; + + if ($data->{'repeatable'} == 0) { + my $new_marc = $old_marc->field($mtag); + my $newvalue = $marc_record->field($mtag)->subfield($sub); + my $oldvalue = $old_marc->field($mtag)->subfield($sub); + ##checking value if value is not duplicate update the marc tag. + if ($oldvalue ne $newvalue) { + $new_marc->delete_subfield(code => $sub); + my $new_field = $marc_record->field($mtag); + $new_field->update( $sub => $newvalue ); + $old_marc->insert_fields_ordered($new_field); + } + } elsif($data->{'repeatable'} == 1 ) { + my @value; + my $i =0; + my @oldvalue; + my $j=0; + foreach my $field_marc ($marc_record->field($mtag)) { + @value[$i] = $field_marc->subfield($sub); + $i = $i + 1; + } + foreach my $oldm ($old_marc->field($mtag)) { + @oldvalue[$j] = $oldm->subfield($sub); + $j = $j + 1; + } + for my $index (0 .. $#value) { + #checking for duplicate values + if ($value[$index] ne $oldvalue[$index]) { + my $new = MARC::Field->new($mtag,'','',$sub => $value[$index]); + $old_marc->insert_fields_ordered($new); + } + } + } + ModBiblio($old_marc, $recordid, $oldbiblio->{'frameworkcode'}); + $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; + + if ($item_result eq 'create_new') { + my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid); + $num_items_added = $bib_items_added; + $num_items_errored = $bib_items_errored; + } + } else { + $oldxml = GetAuthorityXML($recordid); + ModAuthority($recordid, $marc_record, GuessAuthTypeCode($marc_record)); + $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?"; + } + my $sth = $dbh->prepare("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?"); + $sth->execute($oldxml, $rowref->{'import_record_id'}); + $sth = $dbh->prepare($query); + $sth->execute($recordid, $rowref->{'import_record_id'}); + SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied'); + SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); } elsif ($record_result eq 'replace') { $num_updated++; $recordid = $record_match; @@ -1466,6 +1575,8 @@ sub _get_commit_action { $bib_result = 'create_new'; } elsif ($overlay_action eq 'ignore') { $bib_result = 'ignore'; + } elsif ($overlay_action eq 'appendmarctag') { + $bib_result = 'appendmarctag'; } $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_matches') ? 'create_new' : 'ignore'; } else { --- a/koha-tmpl/intranet-tmpl/prog/en/includes/tools-overlay-action.inc +++ a/koha-tmpl/intranet-tmpl/prog/en/includes/tools-overlay-action.inc @@ -1,4 +1,16 @@ - [% IF ( overlay_action_replace ) %] + [% IF ( overlay_action_appendmarctag ) %] + + --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt @@ -214,6 +214,7 @@ $(document).ready(function(){ [% END %] [% END %] + [% IF ( marctag ) %]
  • MarcTag:[% marctag %]
  • [% END %]
  • [% IF ( can_commit ) %] --- a/tools/manage-marc-import.pl +++ a/tools/manage-marc-import.pl @@ -128,9 +128,10 @@ if ($op eq "") { my $current_matcher_id = $input->param('current_matcher_id'); my $overlay_action = $input->param('overlay_action'); my $nomatch_action = $input->param('nomatch_action'); + my $marctag = $input->param('marctag'); my $item_action = $input->param('item_action'); redo_matching($template, $import_batch_id, $new_matcher_id, $current_matcher_id, - $overlay_action, $nomatch_action, $item_action); + $overlay_action, $nomatch_action, $item_action, $marctag); import_records_list($template, $import_batch_id, $offset, $results_per_page); } @@ -139,12 +140,14 @@ output_html_with_http_headers $input, $cookie, $template->output; exit 0; sub redo_matching { - my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action) = @_; + my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action, $marctag) = @_; my $rematch_failed = 0; return if not defined $new_matcher_id and not defined $current_matcher_id; my $old_overlay_action = GetImportBatchOverlayAction($import_batch_id); my $old_nomatch_action = GetImportBatchNoMatchAction($import_batch_id); my $old_item_action = GetImportBatchItemAction($import_batch_id); + my $oldtag = GetImportMarcAction($import_batch_id); + my $oldmarctag = $oldtag->{'marctag'}; return if $new_matcher_id eq $current_matcher_id and $old_overlay_action eq $overlay_action and $old_nomatch_action eq $nomatch_action and @@ -162,6 +165,9 @@ sub redo_matching { SetImportBatchItemAction($import_batch_id, $item_action); $template->param('changed_item_action' => 1); } + if ($oldmarctag ne $marctag) { + SetImportMarcAction($import_batch_id, $marctag); + } my $num_with_matches = 0; if (defined $new_matcher_id and $new_matcher_id ne "") { @@ -218,6 +224,7 @@ sub import_batches_list { comments => $batch->{'comments'}, can_clean => ($batch->{'import_status'} ne 'cleaned') ? 1 : 0, record_type => $batch->{'record_type'}, + marctag => $batch->{'marctag'}, }; } $template->param(batch_list => \@list); @@ -419,6 +426,7 @@ sub batch_info { $template->param(batch_info => 1); $template->param(file_name => $batch->{'file_name'}); $template->param(comments => $batch->{'comments'}); + $template->param(marctag => $batch->{'marctag'}); $template->param(import_status => $batch->{'import_status'}); $template->param(upload_timestamp => $batch->{'upload_timestamp'}); $template->{VARS}->{'record_type'} = $batch->{'record_type'}; --- a/tools/stage-marc-import.pl +++ a/tools/stage-marc-import.pl @@ -57,6 +57,7 @@ my $item_action = $input->param('item_action'); my $comments = $input->param('comments'); my $record_type = $input->param('record_type'); my $encoding = $input->param('encoding'); +my $marctag = $input->param('marctag'); my ($template, $loggedinuser, $cookie) = get_template_and_user({template_name => "tools/stage-marc-import.tmpl", query => $input, @@ -131,7 +132,7 @@ if ($completedJobID) { } # FIXME branch code - my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($record_type, $encoding, $marcrecord, $filename, $comments, '', $parse_items, 0, 50, staging_progress_callback($job, $dbh)); + my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($record_type, $encoding, $marcrecord, $filename, $comments, '', $parse_items, 0, $marctag, 50, staging_progress_callback($job, $dbh)); $dbh->commit(); --