Skip to content

Commit

Permalink
Merge branch 'Bug4247' into ptfs-master
Browse files Browse the repository at this point in the history
Conflicts:

	cataloguing/additem.pl
	koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl
  • Loading branch information
J. David Bavousett committed May 5, 2010
2 parents 73c33d7 + 3a02952 commit d5b3f30
Show file tree
Hide file tree
Showing 11 changed files with 1,013 additions and 78 deletions.
381 changes: 381 additions & 0 deletions C4/Form/AddItem.pm

Large diffs are not rendered by default.

207 changes: 204 additions & 3 deletions C4/ImportBatch.pm
Expand Up @@ -22,6 +22,7 @@ use warnings;

use C4::Context;
use C4::Koha;
use C4::Dates;
use C4::Biblio;
use C4::Items;
use C4::Charset;
Expand Down Expand Up @@ -69,6 +70,11 @@ BEGIN {
SetImportRecordStatus
GetImportRecordMatches
SetImportRecordMatches
AddImportProfile
GetImportProfile
GetImportProfileLoop
GetImportProfileItems
GetImportProfileSubfieldActions
);
}

Expand Down Expand Up @@ -208,6 +214,44 @@ sub AddBiblioToBatch {
my $encoding = shift;
my $z3950random = shift;
my $update_counts = @_ ? shift : 1;
my $subfield_actions = @_ ? shift : [];

foreach my $action (@$subfield_actions) {
my @fields = $marc_record->field($action->{'tag'});
if ( $action->{'action'} eq 'delete' ) {
foreach my $field (@fields) {
$marc_record->delete_field($field);
}
} elsif ( $action->{'action'} eq 'delete_sub' ) {
foreach my $field (@fields) {
$field->delete_subfield(code => $action->{'subfield'});
}
} elsif ( $action->{'action'} eq 'delete_match' ) {
foreach my $field (@fields) {
if ($field->subfield($action->{'subfield'})){
my $cmp1 = _normalize_string($field->subfield($action->{'subfield'}));
my $cmp2 = _normalize_string($action->{'contents'});
if ($cmp1 eq $cmp2){
$field->delete_subfield(code => $action->{'subfield'});
}
}
}
} elsif ( $action->{'action'} eq 'add_always' ) {
$marc_record->insert_grouped_field(MARC::Field->new($action->{'tag'},'','',$action->{'subfield'} => $action->{'contents'}));
} elsif ( $action->{'action'} eq 'add' ) {
my $found =0;
foreach my $field (@fields) {
if ($field->subfield($action->{'subfield'})){
my $cmp1 = _normalize_string($field->subfield($action->{'subfield'}));
my $cmp2 = _normalize_string($action->{'contents'});
$found = 1 if ($cmp1 eq $cmp2);
}
}
if (!$found){
$marc_record->insert_grouped_field(MARC::Field->new($action->{'tag'},'','',$action->{'subfield'} => $action->{'contents'}));
}
}
}

my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random);
_add_biblio_fields($import_record_id, $marc_record);
Expand Down Expand Up @@ -255,6 +299,8 @@ sub BatchStageMarcRecords {
my $branch_code = shift;
my $parse_items = shift;
my $leave_as_staging = shift;
my $added_items = shift;
my $subfield_actions = shift;

# optional callback to monitor status
# of job
Expand All @@ -268,7 +314,8 @@ sub BatchStageMarcRecords {
}

my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
if ($parse_items) {
use Data::Dumper; warn Dumper(@$added_items);
if ($parse_items || @$added_items) {
SetImportBatchItemAction($batch_id, 'always_add');
} else {
SetImportBatchItemAction($batch_id, 'ignore');
Expand All @@ -279,6 +326,8 @@ sub BatchStageMarcRecords {
my $num_items = 0;
# FIXME - for now, we're dealing only with bibs
my $rec_num = 0;
my ($barcode_tag, $barcode_subfield) = &GetMarcFromKohaField( "items.barcode", '' );
my (undef, $dateaccessioned_subfield) = &GetMarcFromKohaField( "items.dateaccessioned", '' );
foreach my $marc_blob (split(/\x1D/, $marc_records)) {
$marc_blob =~ s/^\s+//g;
$marc_blob =~ s/\s+$//g;
Expand All @@ -294,8 +343,17 @@ sub BatchStageMarcRecords {
push @invalid_records, $marc_blob;
} else {
$num_valid++;
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
if ($parse_items) {
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0, $subfield_actions);
if (@$added_items) {
foreach my $item ( @$added_items ) {
my $field = $item->field($barcode_tag);
$field->add_subfields(
$dateaccessioned_subfield => C4::Dates->today()
) if ( !$field->subfield( $dateaccessioned_subfield ) );
$marc_record->append_fields($field);
}
}
if ($parse_items || @$added_items) {
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
$num_items += scalar(@import_items_ids);
}
Expand Down Expand Up @@ -1245,8 +1303,151 @@ sub SetImportRecordMatches {
}
}

sub AddImportProfile {
my ($description, $matcher_id, $template_id, $overlay_action, $nomatch_action, $parse_items, $item_action, $added_items, $subfield_actions) = @_;

my $dbh = C4::Context->dbh;

my $profile_id = $dbh->selectrow_array("SELECT profile_id FROM import_profiles WHERE description = ?", {}, $description);

my $sth;

if ($profile_id) {
$sth = $dbh->prepare("
UPDATE
import_profiles
SET matcher_id = ?, template_id = ?, overlay_action = ?, nomatch_action = ?, parse_items = ?, item_action = ?
WHERE profile_id = ?
");

$sth->execute($matcher_id, $template_id, $overlay_action, $nomatch_action, $parse_items, $item_action, $profile_id);
$dbh->do("
DELETE
FROM import_profile_added_items
WHERE profile_id = ?
", {}, $profile_id);
$dbh->do("
DELETE
FROM import_profile_subfield_actions
WHERE profile_id = ?
", {}, $profile_id);
} else {
$sth = $dbh->prepare("
INSERT
INTO import_profiles(description, matcher_id, template_id, overlay_action, nomatch_action, parse_items, item_action)
VALUES(?, ?, ?, ?, ?, ?, ?)
");

$sth->execute($description, $matcher_id, $template_id, $overlay_action, $nomatch_action, $parse_items, $item_action);
$profile_id = $dbh->{mysql_insertid};
}

$sth = $dbh->prepare("
INSERT
INTO import_profile_added_items(profile_id, marcxml)
VALUES(?, ?)
");

foreach my $item (@$added_items) {
$sth->execute($profile_id, $item->as_xml());
}

$sth = $dbh->prepare("
INSERT
INTO import_profile_subfield_actions(profile_id, tag, subfield, action, contents)
VALUES(?, ?, ?, ?, ?)
");

foreach my $action (@$subfield_actions) {
$sth->execute($profile_id, $action->{'tag'}, $action->{'subfield'}, $action->{'action'}, $action->{'contents'});
}
}

sub GetImportProfile {
my $profile_id = shift;

my $dbh = C4::Context->dbh;

my $profile = $dbh->selectrow_hashref( "
SELECT
description, matcher_id, template_id, overlay_action, nomatch_action, parse_items, item_action
FROM import_profiles
WHERE profile_id = ?
", {}, $profile_id );
}

sub GetImportProfileLoop {
my $dbh = C4::Context->dbh;

my $results = $dbh->selectall_arrayref( "
SELECT
import_profiles.profile_id, description, matcher_id, template_id, overlay_action, nomatch_action, parse_items, item_action, COUNT( marcxml ) AS added_items, ip_actions.tag, ip_actions.subfield
FROM import_profiles
LEFT JOIN import_profile_added_items AS ip_items ON ( import_profiles.profile_id = ip_items.profile_id )
LEFT JOIN import_profile_subfield_actions AS ip_actions ON ( import_profiles.profile_id = ip_actions.profile_id )
GROUP BY import_profiles.profile_id, ip_actions.tag, ip_actions.subfield
", { Slice => {} } );
return [] unless ( @$results );

my @profiles;
my $current_profile;

foreach my $result ( @$results ) {
if ( $current_profile && $current_profile->{'profile_id'} == $result->{'profile_id'} ) {
push @{ $current_profile->{'modified_subfields'} }, { tag => $result->{'tag'}, subfield => $result->{'subfield'} }
} else {
push @profiles, $result;
$current_profile = $result;
$current_profile->{'modified_subfields'} = $result->{'tag'} ? [ { tag => $result->{'tag'}, subfield => $result->{'subfield'} } ] : [];
}
delete $result->{'tag'};
delete $result->{'subfield'};
}

return \@profiles;
}

sub GetImportProfileItems {
my ( $profile_id ) = @_;
my $dbh = C4::Context->dbh;

return map { MARC::Record::new_from_xml( $_, 'UTF-8' ) } @{ $dbh->selectcol_arrayref( "
SELECT
marcxml
FROM import_profile_added_items
WHERE profile_id = ?
", {}, $profile_id ) };
}

sub GetImportProfileSubfieldActions {
my ( $profile_id ) = @_;
my $dbh = C4::Context->dbh;

return map {
+{
"action_type_" . $_->{'action_type'} => 1,
%$_
};
} @{ $dbh->selectall_arrayref( "
SELECT
tag as action_tag, subfield as action_subfield, action as action_type, contents as action_contents
FROM import_profile_subfield_actions
WHERE profile_id = ?
", { Slice => {} }, $profile_id ) };
}

# internal functions
sub _normalize_string {
my ($str) = @_;
$str = uc $str;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
$str =~ s/\W+$//;
$str =~ s/\s+$//;
$str =~ s/\s+/_/g;
$str =~ s/__/_/g;
return $str;
}

sub _create_import_record {
my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
Expand Down
66 changes: 15 additions & 51 deletions cataloguing/additem.pl
Expand Up @@ -20,15 +20,14 @@

use CGI;
use strict;
use warnings;
use C4::Auth;
use C4::Output;
use C4::Biblio;
use C4::Items;
use C4::Context;
use C4::Koha; # XXX subfield_is_koha_internal_p
use C4::Branch; # XXX subfield_is_koha_internal_p
use C4::ClassSource;
use C4::Dates;
use C4::Form::AddItem;

use MARC::File::XML;

Expand Down Expand Up @@ -72,8 +71,8 @@ sub set_item_default_location {
my $dbh = C4::Context->dbh;
my $error = $input->param('error');
my $biblionumber = $input->param('biblionumber');
my $itemnumber = $input->param('itemnumber');
my $op = $input->param('op');
my $itemnumber = $input->param('itemnumber') || '';
my $op = $input->param('op') || '';

my ($template, $loggedinuser, $cookie)
= get_template_and_user({template_name => "cataloguing/additem.tmpl",
Expand All @@ -98,40 +97,14 @@ sub set_item_default_location {
#-------------------------------------------------------------------------------
if ($op eq "additem") {
#-------------------------------------------------------------------------------
# rebuild
my @tags = $input->param('tag');
my @subfields = $input->param('subfield');
my @values = $input->param('field_value');
# build indicator hash.
my @ind_tag = $input->param('ind_tag');
my @indicator = $input->param('indicator');
my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
my ( $record, $barcode_not_unique ) = C4::Form::AddItem::get_item_record( $input, $frameworkcode, 0 );

# type of add
my $add_submit = $input->param('add_submit');
my $add_duplicate_submit = $input->param('add_duplicate_submit');
my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
my $number_of_copies = $input->param('number_of_copies');

# if autoBarcode is set to 'incremental', calculate barcode...
# NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
# NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
if (C4::Context->preference('autoBarcode') eq 'incremental') {
my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
unless ($record->field($tagfield)->subfield($tagsubfield)) {
my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
$sth_barcode->execute;
my ($newbarcode) = $sth_barcode->fetchrow;
$newbarcode++;
# OK, we have the new barcode, now create the entry in MARC record
my $fieldItem = $record->field($tagfield);
$record->delete_field($fieldItem);
$fieldItem->add_subfields($tagsubfield => $newbarcode);
$record->insert_fields_ordered($fieldItem);
}
}

my $addedolditem = TransformMarcToKoha($dbh,$record);

# If we have to add or add & duplicate, we add the item
Expand Down Expand Up @@ -207,7 +180,7 @@ sub set_item_default_location {
}

# Checking if the barcode already exists
$exist_itemnumber = get_item_from_barcode($barcodevalue);
$exist_itemnumber = C4::Form::AddItem::get_item_from_barcode($barcodevalue);
}

# Adding the item
Expand Down Expand Up @@ -268,22 +241,9 @@ sub set_item_default_location {
} elsif ($op eq "saveitem") {
#-------------------------------------------------------------------------------
# rebuild
my @tags = $input->param('tag');
my @subfields = $input->param('subfield');
my @values = $input->param('field_value');
# build indicator hash.
my @ind_tag = $input->param('ind_tag');
my @indicator = $input->param('indicator');
# my $itemnumber = $input->param('itemnumber');
my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
# MARC::Record builded => now, record in DB
# warn "R: ".$record->as_formatted;
# check that the barcode don't exist already
my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
push @errors,"barcode_not_unique";
my ( $itemtosave, $barcode_not_unique ) = C4::Form::AddItem::get_item_record( $input, $frameworkcode, 0, $itemnumber );
if ( $barcode_not_unique ) {
push @errors, 'barcode_not_unique';
} else {
my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
$itemnumber="";
Expand Down Expand Up @@ -323,7 +283,7 @@ sub set_item_default_location {
|| $subf[$i][1];
}

if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
if (($field->tag eq $branchtagfield) && ($subf[$i][0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
#verifying rights
my $userenv = C4::Context->userenv();
unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
Expand Down Expand Up @@ -559,7 +519,11 @@ sub set_item_default_location {
author => $oldrecord->{author},
item_loop => \@item_value_loop,
item_header_loop => \@header_value_loop,
item => \@loop_data,
item => C4::Form::AddItem::get_form_values( $tagslib, 0, {
item => $itemrecord,
biblio => $temp,
frameworkcode => $frameworkcode,
}),
itemnumber => $itemnumber,
itemtagfield => $itemtagfield,
itemtagsubfield => $itemtagsubfield,
Expand Down

0 comments on commit d5b3f30

Please sign in to comment.