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 2676-2736 sub _koha_marc_update_biblioitem_cn_sort { Link Here
2676
    }
2739
    }
2677
}
2740
}
2678
2741
2679
=head2 _koha_add_biblio
2680
2681
  my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2682
2683
Internal function to add a biblio ($biblio is a hash with the values)
2684
2685
=cut
2686
2687
sub _koha_add_biblio {
2688
    my ( $dbh, $biblio, $frameworkcode ) = @_;
2689
2690
    my $error;
2691
2692
    # set the series flag
2693
    unless (defined $biblio->{'serial'}){
2694
    	$biblio->{'serial'} = 0;
2695
    	if ( $biblio->{'seriestitle'} ) { $biblio->{'serial'} = 1 }
2696
    }
2697
2698
    my $query = "INSERT INTO biblio
2699
        SET frameworkcode = ?,
2700
            author = ?,
2701
            title = ?,
2702
            subtitle = ?,
2703
            medium = ?,
2704
            part_number = ?,
2705
            part_name = ?,
2706
            unititle =?,
2707
            notes = ?,
2708
            serial = ?,
2709
            seriestitle = ?,
2710
            copyrightdate = ?,
2711
            datecreated=NOW(),
2712
            abstract = ?
2713
        ";
2714
    my $sth = $dbh->prepare($query);
2715
    $sth->execute(
2716
        $frameworkcode,        $biblio->{'author'},      $biblio->{'title'},       $biblio->{'subtitle'},
2717
        $biblio->{'medium'},   $biblio->{'part_number'}, $biblio->{'part_name'},   $biblio->{'unititle'},
2718
        $biblio->{'notes'},    $biblio->{'serial'},      $biblio->{'seriestitle'}, $biblio->{'copyrightdate'},
2719
        $biblio->{'abstract'}
2720
    );
2721
2722
    my $biblionumber = $dbh->{'mysql_insertid'};
2723
    if ( $dbh->errstr ) {
2724
        $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
2725
        warn $error;
2726
    }
2727
2728
    $sth->finish();
2729
2730
    #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2731
    return ( $biblionumber, $error );
2732
}
2733
2734
=head2 _koha_modify_biblio
2742
=head2 _koha_modify_biblio
2735
2743
2736
  my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2744
  my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
Lines 2841-2912 sub _koha_modify_biblioitem_nonmarc { Link Here
2841
    return ( $biblioitem->{'biblioitemnumber'}, $error );
2849
    return ( $biblioitem->{'biblioitemnumber'}, $error );
2842
}
2850
}
2843
2851
2844
=head2 _koha_add_biblioitem
2845
2846
  my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2847
2848
Internal function to add a biblioitem
2849
2850
=cut
2851
2852
sub _koha_add_biblioitem {
2853
    my ( $dbh, $biblioitem ) = @_;
2854
    my $error;
2855
2856
    my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2857
    my $query = "INSERT INTO biblioitems SET
2858
        biblionumber    = ?,
2859
        volume          = ?,
2860
        number          = ?,
2861
        itemtype        = ?,
2862
        isbn            = ?,
2863
        issn            = ?,
2864
        publicationyear = ?,
2865
        publishercode   = ?,
2866
        volumedate      = ?,
2867
        volumedesc      = ?,
2868
        collectiontitle = ?,
2869
        collectionissn  = ?,
2870
        collectionvolume= ?,
2871
        editionstatement= ?,
2872
        editionresponsibility = ?,
2873
        illus           = ?,
2874
        pages           = ?,
2875
        notes           = ?,
2876
        size            = ?,
2877
        place           = ?,
2878
        lccn            = ?,
2879
        url             = ?,
2880
        cn_source       = ?,
2881
        cn_class        = ?,
2882
        cn_item         = ?,
2883
        cn_suffix       = ?,
2884
        cn_sort         = ?,
2885
        totalissues     = ?,
2886
        ean             = ?,
2887
        agerestriction  = ?
2888
        ";
2889
    my $sth = $dbh->prepare($query);
2890
    $sth->execute(
2891
        $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
2892
        $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
2893
        $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
2894
        $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
2895
        $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
2896
        $biblioitem->{'lccn'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
2897
        $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
2898
        $biblioitem->{'totalissues'},      $biblioitem->{'ean'},              $biblioitem->{'agerestriction'}
2899
    );
2900
    my $bibitemnum = $dbh->{'mysql_insertid'};
2901
2902
    if ( $dbh->errstr ) {
2903
        $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
2904
        warn $error;
2905
    }
2906
    $sth->finish();
2907
    return ( $bibitemnum, $error );
2908
}
2909
2910
=head2 _koha_delete_biblio
2852
=head2 _koha_delete_biblio
2911
2853
2912
  $error = _koha_delete_biblio($dbh,$biblionumber);
2854
  $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