From 382e9b3a0b13a464cffeb8568da1fc7b2885d38d Mon Sep 17 00:00:00 2001 From: Paul Poulain Date: Wed, 15 Dec 2010 20:47:21 +0100 Subject: [PATCH] Bug 5725 : MT #2991 (Feature) Batch biblio modifications This patch add a new feature of batch biblio modifications, it permit to select biblios from basket, and modify fields of them setting Rebased to fix whitespace issues. --- C4/Biblio.pm | 171 ++++++++++++++- koha-tmpl/intranet-tmpl/prog/en/js/basket.js | 6 + .../prog/en/modules/basket/basket.tmpl | 5 + .../prog/en/modules/tools/batchedit.tmpl | 231 ++++++++++++++++++++ tools/batchedit.pl | 222 +++++++++++++++++++ 5 files changed, 623 insertions(+), 12 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchedit.tmpl create mode 100755 tools/batchedit.pl diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 8ea5da5..bafc2db 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -93,6 +93,8 @@ BEGIN { &ModBiblio &ModBiblioframework &ModZebra + + &BatchModField ); # To delete something @@ -376,7 +378,7 @@ sub ModBiblioframework { =head2 DelBiblio - my $error = &DelBiblio($dbh,$biblionumber); + my $error = &DelBiblio($biblionumber); Exported function (core API) for deleting a biblio in koha. Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items) @@ -1046,9 +1048,14 @@ The MARC record contains both biblio & item data. sub GetMarcBiblio { my $biblionumber = shift; + my $deletedtable = shift; my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? "); - $sth->execute($biblionumber); + my $strsth = qq{SELECT marcxml FROM biblioitems WHERE biblionumber=?}; + $strsth .= qq{UNION SELECT marcxml FROM deletedbiblioitems WHERE biblionumber=?} if $deletedtable; + my $sth = $dbh->prepare($strsth); + my @params=($biblionumber); + push @params, $biblionumber if ($deletedtable); + $sth->execute(@params); my $row = $sth->fetchrow_hashref; my $marcxml = StripNonXmlChars( $row->{'marcxml'} ); MARC::File::XML->default_record_format( C4::Context->preference('marcflavour') ); @@ -1113,6 +1120,8 @@ sub GetCOinSBiblio { my $isbn = ''; my $issn = ''; my $publisher = ''; + my $place = ''; + my $tpages = ''; if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) { my $fmts6; @@ -1159,24 +1168,70 @@ sub GetCOinSBiblio { $genre = ( $mtx eq 'dc' ) ? "&rft.type=$genre" : "&rft.genre=$genre"; # Setting datas - $aulast = $record->subfield( '700', 'a' ); - $aufirst = $record->subfield( '700', 'b' ); - $oauthors = "&rft.au=$aufirst $aulast"; + + # authors + $aulast = $record->subfield( '700', 'a' ) || ''; + $aufirst = $record->subfield( '700', 'b' ) || ''; + + foreach my $field (qw/700 701/) { + my $author = ''; + $author = $record->subfield($field, 'a') || ''; + $author .= ", " . $record->subfield($field, 'b') if ($author and $record->subfield($field, 'b')); + $author .= " - " . join(' - ', $record->subfield($field, '4')) if ($record->subfield($field, '4')); + $author .= " (" . $record->subfield($field, 'f') . ")" if ($record->subfield($field, 'f')); + + $oauthors .= "&rft.au=$author" if ($author); + } + + foreach my $field (qw/710 711/) { + my $author = ''; + $author = $record->subfield($field, 'a') || ''; + $author .= ", " . $record->subfield($field, 'b') if ($author and $record->subfield($field, 'b')); + $author .= " - " . join(' - ', $record->subfield($field, '4')) if ($record->subfield($field, '4')); + $author .= " (" . $record->subfield($field, 'c') . ")" if ($record->subfield($field, 'c')); + + $oauthors .= "&rft.au=$author" if ($author); + } + # others authors if ( $record->field('200') ) { + for my $au ( $record->field('200')->subfield('f') ) { + $oauthors .= "&rft.au=$au"; + } for my $au ( $record->field('200')->subfield('g') ) { $oauthors .= "&rft.au=$au"; } + } + + # place + $place = join(" - ", $record->subfield('210', 'a')); + $place = "&rtf.place=$place" if ($place); + + # tpages + my $i = 0; + foreach my $field ($record->field('215')) { + $tpages .= " | " if ($i > 0); + $tpages .= join(" - ", $field->subfield('a')); + $tpages .= join(" ; ", $field->subfield('d')); + $tpages .= join(" + ", $field->subfield('e')); + $i++; + } + $tpages = "&rtf.tpages=$tpages" if ($tpages); + + # title + my $btitle = join(' ; ', $record->subfield('200', 'a')); + $btitle .= " : " . join(' : ', $record->subfield('200', 'e')) if ($record->subfield('200', 'e')); + $title = ( $mtx eq 'dc' ) ? "&rft.title=" . $record->subfield( '200', 'a' ) - : "&rft.title=" . $record->subfield( '200', 'a' ) . "&rft.btitle=" . $record->subfield( '200', 'a' ); - $pubyear = $record->subfield( '210', 'd' ); - $publisher = $record->subfield( '210', 'c' ); - $isbn = $record->subfield( '010', 'a' ); - $issn = $record->subfield( '011', 'a' ); + : "&rft.title=" . $record->subfield( '200', 'a' ) . "&rft.btitle=" . $btitle; + $pubyear = $record->subfield( '210', 'd' ) || ''; + $publisher = $record->subfield( '210', 'c' ) || ''; + $isbn = $record->subfield( '010', 'a' ) || ''; + $issn = $record->subfield( '011', 'a' ) || ''; } else { # MARC21 need some improve @@ -1205,7 +1260,7 @@ sub GetCOinSBiblio { } my $coins_value = -"ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3A$mtx$genre$title&rft.isbn=$isbn&rft.issn=$issn&rft.aulast=$aulast&rft.aufirst=$aufirst$oauthors&rft.pub=$publisher&rft.date=$pubyear"; +"ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3A$mtx$genre$title&rft.isbn=$isbn&rft.issn=$issn&rft.aulast=$aulast&rft.aufirst=$aufirst$oauthors&rft.pub=$publisher&rft.date=$pubyear$place$tpages"; $coins_value =~ s/(\ |&[^a])/\+/g; $coins_value =~ s/\"/\"\;/g; @@ -1750,6 +1805,7 @@ sub TransformHtmlToXml { @$values[$i] =~ s/>/>/g; @$values[$i] =~ s/"/"/g; @$values[$i] =~ s/'/'/g; + @$values[$i] = NormalizeString(@$values[$i]); # if ( !utf8::is_utf8( @$values[$i] ) ) { # utf8::decode( @$values[$i] ); @@ -2313,7 +2369,9 @@ sub PrepareItemrecordDisplay { push @authorised_values, $branchcode; $authorised_lib{$branchcode} = $branchname; } + $defaultvalue = C4::Context->userenv->{branch}; } + $defaultvalue = C4::Context->userenv->{branch}; #----- itemtypes } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { @@ -3461,6 +3519,95 @@ sub get_biblio_authorised_values { return $authorised_values; } +=head3 BatchModField + + Mod subfields in field record + + returns 1,$record if succeed + returns 0,$record if Action was not processed + returns -1 if no record + returns -2 if no action done on record + +=cut + +sub BatchModField { + my ( $record, $field, $subfield, $action, $condval, $nocond, $repval ) = @_; + + return -1 unless $record; + $condval=NormalizeString($condval); + my $condition=qr/$condval/; + + if($action eq "add"){ + for my $rfield ($record->field($field)){ + $rfield->add_subfields( $subfield => $repval ); + } + return 1; + }elsif($action eq "addfield"){ + my $new_field = MARC::Field->new($field,'','', + $subfield => $repval); + $record->insert_fields_ordered($new_field); + return 1; + } else { + my $done=0; + for my $rfield ($record->field($field)) { + if ($subfield && $subfield ne "@"){ + my @subfields = $rfield->subfields(); + my @subfields_to_add; + foreach my $subf (@subfields) { + if ($subf->[0] eq $subfield){ + $subf->[1]=NormalizeString($subf->[1]); + if ( $action eq "mod" ) { + if ( $nocond ne "true" && $subf->[1] =~ s/$condition/$repval/) { + $done=1; + } + if ($nocond eq "true"){ + $subf->[1] = $repval; + $done=1; + } + } elsif ( $action eq "del" ) { + if ( $subf->[1] =~ m/$condition/ || $nocond eq "true" ) { + $done=1; + next; + } + } + } + push @subfields_to_add,@$subf; + } + if ($done){ + if (@subfields_to_add){ + $rfield->replace_with(MARC::Field->new($rfield->tag,$rfield->indicator(1),$rfield->indicator(2),@subfields_to_add)); + } + else { + my $count= $record->delete_field($rfield); + } + } + } + else { + if ($action eq "del"){ + my $count=$record->delete_field($rfield); + $done=1; + } + else { + if ($field < 10){ + my $value=$record->field($field)->data(); + if ($value=~ s/$condition/$repval/){ + $record->field($field)->update($value); + $done=1; + + } + if ( $nocond eq 'true'){ + $record->field($field)->update($repval); + $done=1; + } + } + } + } + } + return ($done,$record); + } + return -2; +} + 1; __END__ diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/basket.js b/koha-tmpl/intranet-tmpl/prog/en/js/basket.js index 0ab6711..0991f42 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/js/basket.js +++ b/koha-tmpl/intranet-tmpl/prog/en/js/basket.js @@ -433,4 +433,10 @@ $(document).ready(function(){ if(basketcount){ updateBasket(basketcount); } }); +function batchEdit(){ + var valCookie = readCookie(nameCookie); + var strCookie = nameParam + "=" + valCookie; + var loc = CGIBIN + "tools/batchedit.pl?" + strCookie; + window.opener.location = loc; +} diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tmpl index d3d83ac..6699bde 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tmpl @@ -86,6 +86,11 @@ function placeHold () { Brief DisplayMore Details +
  • + + Batch Edit + +
  • Send diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchedit.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchedit.tmpl new file mode 100644 index 0000000..f7b7589 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchedit.tmpl @@ -0,0 +1,231 @@ + +Koha › Tools › Batch Deletion of Items + + + + + + + + + + + +
    +
    +
    +
    + +
    + +

    Batch records modification

    +
    + Use a file +
      +
    1. +
    +
    +
    + Or enter records one by one +
      +
    1. + + +
    2. +
    +
    + +

    List of records:

    + + + + + + + + + + + + + + + + +
    BiblionumberTitleAuthor Status
    + + + + OK + Nothing done + Some Actions failed. List Follow :
    +
    + + + +
    All operations processed
    + + +
    +

    Modification rules:

    + + +" /> + + + + + + + + + + + + + + + + + + + +
    FieldSubfieldActionCondition ValueValue 
    + + + + + + + + All + + +
    + +
    +
    + +
    +
    +
    +
    + + diff --git a/tools/batchedit.pl b/tools/batchedit.pl new file mode 100755 index 0000000..06fcd0f --- /dev/null +++ b/tools/batchedit.pl @@ -0,0 +1,222 @@ +#!/usr/bin/perl + + +# Copyright 2010 SARL BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +use CGI; +use strict; +use C4::Output; +use C4::Auth; +use C4::Branch; +use C4::Koha; +use C4::Biblio; +use C4::Context; +use C4::Debug; +use JSON; + +my $input = new CGI; +my $dbh = C4::Context->dbh; + +my $filefh = $input->param('uploadfile'); +my $recordslist = $input->param('recordslist'); +my $bib_list = $input->param('bib_list'); +my @biblionumbers; + +if ($filefh) { + while ( my $biblionumber = <$filefh> ) { + $biblionumber =~ s/[\r\n]*$//g; + push @biblionumbers, $biblionumber if $biblionumber; + } +} elsif ($recordslist) { + push @biblionumbers, split( /\s\n/, $recordslist ); +} elsif ($bib_list) { + push @biblionumbers, split('/', $bib_list); +} + +my $op = $input->param('op'); +my ($template, $loggedinuser, $cookie); + +my $frameworkcode=""; +my $tagslib = &GetMarcStructure(1,$frameworkcode); +my %report_actions; + +if($input->param('field') and not defined $op){ + ($template, $loggedinuser, $cookie) + = get_template_and_user({template_name => "acqui/ajax.tmpl", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => "batchedit", + }); + + + my $tag = $input->param('field'); + my $subfield = $input->param('subfield'); + + if($input->param('subfield')){ + my $branches = GetBranchesLoop(); + + my @authorised_values; + if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) { + if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) { + foreach my $thisbranch (@$branches) { + push @authorised_values, { + code => $thisbranch->{value}, + value => $thisbranch->{branchname}, + }; + # $value = $thisbranch->{value} if $thisbranch->{selected}; + } + }elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { + my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description"); + $sth->execute(); + while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { + push @authorised_values, { + code => $itemtype, + value => $description, + }; + } + + }else { + # Getting the fields where the item location is + my ($location_field, $location_subfield) = GetMarcFromKohaField('items.location', $frameworkcode); + + # Getting the name of the authorised values' category for item location + my $item_location_category = $tagslib->{$location_field}->{$location_subfield}->{'authorised_value'}; + # Are we dealing with item location ? + my $item_location = ($tagslib->{$tag}->{$subfield}->{authorised_value} eq $item_location_category) ? 1 : 0; + + # If so, we sort by authorised_value, else by libelle + my $orderby = $item_location ? 'authorised_value' : 'lib'; + + my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY $orderby"); + + $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}); + + + while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) { + push @authorised_values, { + code => $value, + value => ($item_location) ? $value . " - " . $lib : $lib, + }; + + } + } + } + $template->param('return' => to_json(\@authorised_values)); + }else{ + my @modifiablesubf; + + foreach my $subfield (sort keys %{$tagslib->{$tag}}) { + next if subfield_is_koha_internal_p($subfield); + next if $subfield eq "@"; + next if ($tagslib->{$tag}->{$subfield}->{'tab'} eq "10"); + my %subfield_data; + $subfield_data{subfield} = $subfield; + push @modifiablesubf, \%subfield_data; + } + $template->param('return' => to_json(\@modifiablesubf)); + } + + + output_html_with_http_headers $input, $cookie, $template->output; + exit; +}else{ + ($template, $loggedinuser, $cookie) + = get_template_and_user({template_name => "tools/batchedit.tmpl", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => "batchedit", + }); + + $template->param( inputform => 1, ) unless @biblionumbers; + + if(!defined $op) { + my @modifiablefields; + + foreach my $tag (sort keys %{$tagslib}) { + my %subfield_data; + foreach my $subfield (sort keys %{$tagslib->{$tag}}) { + next if $subfield_data{tag}; + next if subfield_is_koha_internal_p($subfield); + next if ($tagslib->{$tag}->{$subfield}->{'tab'} eq "10"); + + $subfield_data{tag} = $tag; + + push @modifiablefields, \%subfield_data; + } + } + + $template->param( marcfields => \@modifiablefields, + bib_list => $input->param('bib_list'), + ); + + }else{ + my @fields = $input->param('field'); + my @subfields = $input->param('subfield'); + my @actions = $input->param('action'); + my @condvals = $input->param('condval'); + my @nocondvals = $input->param('nocondval'); + my @repvals = $input->param('repval'); + foreach my $biblionumber ( @biblionumbers ){ + my $record = GetMarcBiblio($biblionumber); + my $biblio = GetBiblio($biblionumber); + my $report = 0; + my @failed_actions; + for(my $i = 0 ; $i < scalar(@fields) ; $i++ ){ + my $field = $fields[$i]; + my $subfield = $subfields[$i]; + my $action = $actions[$i]; + my $condval = $condvals[$i]; + my $nocond = $nocondvals[$i]; + my $repval = $repvals[$i]; + + my ($result,$record) = BatchModField($record, $field, $subfield, $action, $condval, $nocond, $repval); + push @failed_actions, {action=>"$field $subfield $action ".($nocond eq "true"?"all":$condval)." $repval"} if ($result<=0); + } + if (@failed_actions == scalar(@fields)){ + $report_actions{$biblionumber}->{status}="No_Actions"; + } + elsif (@failed_actions>0 and @failed_actions < scalar(@fields)){ + $report_actions{$biblionumber}->{status}="Actions_Failed"; + $report_actions{$biblionumber}->{failed_actions}=\@failed_actions; + } + elsif (@failed_actions == 0){ + $report_actions{$biblionumber}->{status}="OK"; + } + ModBiblio($record, $biblionumber, $biblio->{frameworkcode}) unless ($report); + } + $template->param('moddone' => 1); + } + +} + +my @biblioinfos; + +for my $biblionumber (@biblionumbers){ + my $biblio = GetBiblio($biblionumber); + if (defined $op){ + $biblio->{$report_actions{$biblionumber}->{status}}=1; + $biblio->{failed_actions}=$report_actions{$biblionumber}->{failed_actions}; + } + push @biblioinfos, $biblio; +} + +$template->param(biblioinfos => \@biblioinfos); +output_html_with_http_headers $input, $cookie, $template->output; +exit; -- 1.7.1