View | Details | Raw Unified | Return to bug 26518
Collapse All | Expand All

(-)a/C4/Biblio.pm (-135 / +77 lines)
Lines 216-241 sub AddBiblio { Link Here
216
    # transform the data into koha-table style data
216
    # transform the data into koha-table style data
217
    SetUTF8Flag($record);
217
    SetUTF8Flag($record);
218
    my $olddata = TransformMarcToKoha( $record, $frameworkcode );
218
    my $olddata = TransformMarcToKoha( $record, $frameworkcode );
219
    ( $biblionumber, $error ) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
219
    my $schema = Koha::Database->schema;
220
    $olddata->{'biblionumber'} = $biblionumber;
220
    try {
221
    ( $biblioitemnumber, $error ) = _koha_add_biblioitem( $dbh, $olddata );
221
        $schema->txn_do(sub {
222
223
            my $biblio = Koha::Biblio->new(
224
                {
225
                    frameworkcode => $frameworkcode,
226
                    author        => $olddata->{author},
227
                    title         => $olddata->{title},
228
                    subtitle      => $olddata->{subtitle},
229
                    medium        => $olddata->{medium},
230
                    part_number   => $olddata->{part_number},
231
                    part_name     => $olddata->{part_name},
232
                    unititle      => $olddata->{unititle},
233
                    notes         => $olddata->{notes},
234
                    serial =>
235
                      ( $olddata->{serial} || $olddata->{seriestitle} ? 1 : 0 ),
236
                    seriestitle   => $olddata->{seriestitle},
237
                    copyrightdate => $olddata->{copyrightdate},
238
                    datecreated   => \'NOW()',
239
                    abstract      => $olddata->{abstract},
240
                }
241
            )->store;
242
            $biblionumber = $biblio->biblionumber;
243
244
            my ($cn_sort) = GetClassSort( $olddata->{'biblioitems.cn_source'}, $olddata->{'cn_class'}, $olddata->{'cn_item'} );
245
            my $biblioitem = Koha::Biblioitem->new(
246
                {
247
                    biblionumber          => $biblionumber,
248
                    volume                => $olddata->{volume},
249
                    number                => $olddata->{number},
250
                    itemtype              => $olddata->{itemtype},
251
                    isbn                  => $olddata->{isbn},
252
                    issn                  => $olddata->{issn},
253
                    publicationyear       => $olddata->{publicationyear},
254
                    publishercode         => $olddata->{publishercode},
255
                    volumedate            => $olddata->{volumedate},
256
                    volumedesc            => $olddata->{volumedesc},
257
                    collectiontitle       => $olddata->{collectiontitle},
258
                    collectionissn        => $olddata->{collectionissn},
259
                    collectionvolume      => $olddata->{collectionvolume},
260
                    editionstatement      => $olddata->{editionstatement},
261
                    editionresponsibility => $olddata->{editionresponsibility},
262
                    illus                 => $olddata->{illus},
263
                    pages                 => $olddata->{pages},
264
                    notes                 => $olddata->{bnotes},
265
                    size                  => $olddata->{size},
266
                    place                 => $olddata->{place},
267
                    lccn                  => $olddata->{lccn},
268
                    url                   => $olddata->{url},
269
                    cn_source      => $olddata->{'biblioitems.cn_source'},
270
                    cn_class       => $olddata->{cn_class},
271
                    cn_item        => $olddata->{cn_item},
272
                    cn_suffix      => $olddata->{cn_suff},
273
                    cn_sort        => $cn_sort,
274
                    totalissues    => $olddata->{totalissues},
275
                    ean            => $olddata->{ean},
276
                    agerestriction => $olddata->{agerestriction},
277
                }
278
            )->store;
279
            $biblioitemnumber = $biblioitem->biblioitemnumber;
222
280
223
    _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
281
            _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
224
282
225
    # update MARC subfield that stores biblioitems.cn_sort
283
            # update MARC subfield that stores biblioitems.cn_sort
226
    _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
284
            _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
227
285
228
    # now add the record
286
            # now add the record
229
    ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
287
            ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
230
288
231
    # update OAI-PMH sets
289
            # update OAI-PMH sets
232
    if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
290
            if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
233
        C4::OAI::Sets::UpdateOAISetsBiblio($biblionumber, $record);
291
                C4::OAI::Sets::UpdateOAISetsBiblio($biblionumber, $record);
234
    }
292
            }
235
293
236
    _after_biblio_action_hooks({ action => 'create', biblio_id => $biblionumber });
294
            _after_biblio_action_hooks({ action => 'create', biblio_id => $biblionumber });
237
295
238
    logaction( "CATALOGUING", "ADD", $biblionumber, "biblio" ) if C4::Context->preference("CataloguingLog");
296
            logaction( "CATALOGUING", "ADD", $biblionumber, "biblio" ) if C4::Context->preference("CataloguingLog");
297
        });
298
    } catch {
299
        warn $_;
300
        ( $biblionumber, $biblioitemnumber ) = ( undef, undef );
301
    };
239
    return ( $biblionumber, $biblioitemnumber );
302
    return ( $biblionumber, $biblioitemnumber );
240
}
303
}
241
304
Lines 2670-2730 sub _koha_marc_update_biblioitem_cn_sort { Link Here
2670
    }
2733
    }
2671
}
2734
}
2672
2735
2673
=head2 _koha_add_biblio
2674
2675
  my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2676
2677
Internal function to add a biblio ($biblio is a hash with the values)
2678
2679
=cut
2680
2681
sub _koha_add_biblio {
2682
    my ( $dbh, $biblio, $frameworkcode ) = @_;
2683
2684
    my $error;
2685
2686
    # set the series flag
2687
    unless (defined $biblio->{'serial'}){
2688
    	$biblio->{'serial'} = 0;
2689
    	if ( $biblio->{'seriestitle'} ) { $biblio->{'serial'} = 1 }
2690
    }
2691
2692
    my $query = "INSERT INTO biblio
2693
        SET frameworkcode = ?,
2694
            author = ?,
2695
            title = ?,
2696
            subtitle = ?,
2697
            medium = ?,
2698
            part_number = ?,
2699
            part_name = ?,
2700
            unititle =?,
2701
            notes = ?,
2702
            serial = ?,
2703
            seriestitle = ?,
2704
            copyrightdate = ?,
2705
            datecreated=NOW(),
2706
            abstract = ?
2707
        ";
2708
    my $sth = $dbh->prepare($query);
2709
    $sth->execute(
2710
        $frameworkcode,        $biblio->{'author'},      $biblio->{'title'},       $biblio->{'subtitle'},
2711
        $biblio->{'medium'},   $biblio->{'part_number'}, $biblio->{'part_name'},   $biblio->{'unititle'},
2712
        $biblio->{'notes'},    $biblio->{'serial'},      $biblio->{'seriestitle'}, $biblio->{'copyrightdate'},
2713
        $biblio->{'abstract'}
2714
    );
2715
2716
    my $biblionumber = $dbh->{'mysql_insertid'};
2717
    if ( $dbh->errstr ) {
2718
        $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
2719
        warn $error;
2720
    }
2721
2722
    $sth->finish();
2723
2724
    #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2725
    return ( $biblionumber, $error );
2726
}
2727
2728
=head2 _koha_modify_biblio
2736
=head2 _koha_modify_biblio
2729
2737
2730
  my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2738
  my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
Lines 2835-2906 sub _koha_modify_biblioitem_nonmarc { Link Here
2835
    return ( $biblioitem->{'biblioitemnumber'}, $error );
2843
    return ( $biblioitem->{'biblioitemnumber'}, $error );
2836
}
2844
}
2837
2845
2838
=head2 _koha_add_biblioitem
2839
2840
  my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2841
2842
Internal function to add a biblioitem
2843
2844
=cut
2845
2846
sub _koha_add_biblioitem {
2847
    my ( $dbh, $biblioitem ) = @_;
2848
    my $error;
2849
2850
    my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2851
    my $query = "INSERT INTO biblioitems SET
2852
        biblionumber    = ?,
2853
        volume          = ?,
2854
        number          = ?,
2855
        itemtype        = ?,
2856
        isbn            = ?,
2857
        issn            = ?,
2858
        publicationyear = ?,
2859
        publishercode   = ?,
2860
        volumedate      = ?,
2861
        volumedesc      = ?,
2862
        collectiontitle = ?,
2863
        collectionissn  = ?,
2864
        collectionvolume= ?,
2865
        editionstatement= ?,
2866
        editionresponsibility = ?,
2867
        illus           = ?,
2868
        pages           = ?,
2869
        notes           = ?,
2870
        size            = ?,
2871
        place           = ?,
2872
        lccn            = ?,
2873
        url             = ?,
2874
        cn_source       = ?,
2875
        cn_class        = ?,
2876
        cn_item         = ?,
2877
        cn_suffix       = ?,
2878
        cn_sort         = ?,
2879
        totalissues     = ?,
2880
        ean             = ?,
2881
        agerestriction  = ?
2882
        ";
2883
    my $sth = $dbh->prepare($query);
2884
    $sth->execute(
2885
        $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
2886
        $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
2887
        $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
2888
        $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
2889
        $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
2890
        $biblioitem->{'lccn'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
2891
        $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
2892
        $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
2893
    );
2894
    my $bibitemnum = $dbh->{'mysql_insertid'};
2895
2896
    if ( $dbh->errstr ) {
2897
        $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
2898
        warn $error;
2899
    }
2900
    $sth->finish();
2901
    return ( $bibitemnum, $error );
2902
}
2903
2904
=head2 _koha_delete_biblio
2846
=head2 _koha_delete_biblio
2905
2847
2906
  $error = _koha_delete_biblio($dbh,$biblionumber);
2848
  $error = _koha_delete_biblio($dbh,$biblionumber);
(-)a/t/db_dependent/Biblio.t (-5 / +25 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 14;
20
use Test::More tests => 15;
21
use Test::MockModule;
21
use Test::MockModule;
22
use List::MoreUtils qw( uniq );
22
use List::MoreUtils qw( uniq );
23
use MARC::Record;
23
use MARC::Record;
Lines 42-47 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" ); Link Here
42
42
43
my $builder = t::lib::TestBuilder->new;
43
my $builder = t::lib::TestBuilder->new;
44
44
45
subtest 'AddBiblio' => sub {
46
    plan tests => 3;
47
48
    my $marcflavour = 'MARC21';
49
    t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour );
50
    my $record = MARC::Record->new();
51
52
    my ( $f, $sf ) = GetMarcFromKohaField('biblioitems.lccn');
53
    my $lccn_field = MARC::Field->new( $f, ' ', ' ',
54
        $sf => 'ThisisgoingtobetoomanycharactersfortheLCCNfield' );
55
    $record->append_fields($lccn_field);
56
57
    my $nb_biblios = Koha::Biblios->count;
58
    my ( $biblionumber, $biblioitemnumber ) =
59
      C4::Biblio::AddBiblio( $record, '' );
60
    is( $biblionumber, undef,
61
        'AddBiblio returns undef for biblionumber if something went wrong' );
62
    is( $biblioitemnumber, undef,
63
        'AddBiblio returns undef for biblioitemnumber if something went wrong'
64
    );
65
    is( Koha::Biblios->count, $nb_biblios,
66
        'No biblio should have been added if something went wrong' );
67
};
68
45
subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
69
subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
46
    plan tests => 25;
70
    plan tests => 25;
47
71
Lines 758-766 subtest "LinkBibHeadingsToAuthorities record generation tests" => sub { Link Here
758
        "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
782
        "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
759
        "The generated record contains the correct subfields"
783
        "The generated record contains the correct subfields"
760
    );
784
    );
761
762
763
764
};
785
};
765
786
766
# Cleanup
787
# Cleanup
767
- 

Return to bug 26518