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

(-)a/C4/Koha.pm (+1 lines)
Lines 28-33 use C4::Branch; # Can be removed? Link Here
28
use Koha::Cache;
28
use Koha::Cache;
29
use Koha::DateUtils qw(dt_from_string);
29
use Koha::DateUtils qw(dt_from_string);
30
use Koha::Libraries;
30
use Koha::Libraries;
31
use Koha::MarcSubfieldStructures;
31
use DateTime::Format::MySQL;
32
use DateTime::Format::MySQL;
32
use Business::ISBN;
33
use Business::ISBN;
33
use autouse 'Data::cselectall_arrayref' => qw(Dumper);
34
use autouse 'Data::cselectall_arrayref' => qw(Dumper);
(-)a/C4/Record.pm (-3 / +8 lines)
Lines 37-42 use Text::CSV::Encoded; #marc2csv Link Here
37
use Koha::SimpleMARC qw(read_field);
37
use Koha::SimpleMARC qw(read_field);
38
use Koha::XSLT_Handler;
38
use Koha::XSLT_Handler;
39
use Koha::CsvProfiles;
39
use Koha::CsvProfiles;
40
use Koha::AuthorisedValues;
40
use Carp;
41
use Carp;
41
42
42
use vars qw(@ISA @EXPORT);
43
use vars qw(@ISA @EXPORT);
Lines 585-602 sub marcrecord2csv { Link Here
585
                # If it is a subfield
586
                # If it is a subfield
586
                my @loop_values;
587
                my @loop_values;
587
                if ( $tag->{subfieldtag} ) {
588
                if ( $tag->{subfieldtag} ) {
589
                    my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, tagsubfield => $tag->{subfieldtag}, });
590
                    $av = $av->count ? $av->unblessed : [];
591
                    my $av_description_mapping = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
588
                    # For each field
592
                    # For each field
589
                    foreach my $field (@fields) {
593
                    foreach my $field (@fields) {
590
                        my @subfields = $field->subfield( $tag->{subfieldtag} );
594
                        my @subfields = $field->subfield( $tag->{subfieldtag} );
591
                        foreach my $subfield (@subfields) {
595
                        foreach my $subfield (@subfields) {
592
                            my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, $tag->{subfieldtag}, $frameworkcode, undef);
596
                            push @loop_values, (defined $av_description_mapping->{$subfield}) ? $av_description_mapping->{$subfield} : $subfield;
593
                            push @loop_values, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield;
594
                        }
597
                        }
595
                    }
598
                    }
596
599
597
                # Or a field
600
                # Or a field
598
                } else {
601
                } else {
599
                    my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, undef, $frameworkcode, undef);
602
                    my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, });
603
                    $av = $av->count ? $av->unblessed : [];
604
                    my $authvalues = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
600
605
601
                    foreach my $field ( @fields ) {
606
                    foreach my $field ( @fields ) {
602
                        my $value;
607
                        my $value;
(-)a/Koha/AuthorisedValues.pm (+18 lines)
Lines 24-29 use Carp; Link Here
24
use Koha::Database;
24
use Koha::Database;
25
25
26
use Koha::AuthorisedValue;
26
use Koha::AuthorisedValue;
27
use Koha::MarcSubfieldStructures;
27
28
28
use base qw(Koha::Objects);
29
use base qw(Koha::Objects);
29
30
Lines 62-67 sub search { Link Here
62
    return $self->SUPER::search( { %$params, %$or, }, $join );
63
    return $self->SUPER::search( { %$params, %$or, }, $join );
63
}
64
}
64
65
66
sub search_by_marc_field {
67
    my ( $self, $params ) = @_;
68
    my $frameworkcode = $params->{frameworkcode} || '';
69
    my $tagfield      = $params->{tagfield};
70
    my $tagsubfield   = $params->{tagsubfield};
71
72
    return unless $tagfield or $tagsubfield;
73
74
    return $self->SUPER::search(
75
        {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
76
            ( defined $tagfield    ? ( 'marc_subfield_structures.tagfield'    => $tagfield )    : () ),
77
            ( defined $tagsubfield ? ( 'marc_subfield_structures.tagsubfield' => $tagsubfield ) : () ),
78
        },
79
        { join => { category => 'marc_subfield_structures' } }
80
    );
81
}
82
65
sub categories {
83
sub categories {
66
    my ( $self ) = @_;
84
    my ( $self ) = @_;
67
    my $rs = $self->_resultset->search(
85
    my $rs = $self->_resultset->search(
(-)a/t/db_dependent/AuthorisedValues.t (-2 / +33 lines)
Lines 1-12 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 14;
4
use Test::More tests => 15;
5
5
6
use C4::Context;
6
use C4::Context;
7
use Koha::AuthorisedValue;
7
use Koha::AuthorisedValue;
8
use Koha::AuthorisedValues;
8
use Koha::AuthorisedValues;
9
use Koha::AuthorisedValueCategories;
9
use Koha::AuthorisedValueCategories;
10
use Koha::MarcSubfieldStructures;
10
11
11
my $dbh = C4::Context->dbh;
12
my $dbh = C4::Context->dbh;
12
$dbh->{AutoCommit} = 0;
13
$dbh->{AutoCommit} = 0;
Lines 99-101 my @categories = Koha::AuthorisedValues->new->categories; Link Here
99
is( @categories, 2, 'There should have 2 categories inserted' );
100
is( @categories, 2, 'There should have 2 categories inserted' );
100
is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
101
is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
101
is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );
102
is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );
102
- 
103
104
subtest 'search_by_*_field' => sub {
105
    plan tests => 1;
106
    my $loc_cat = Koha::AuthorisedValueCategories->find('LOC');
107
    $loc_cat->delete if $loc_cat;
108
    my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
109
    $mss->delete if $mss;
110
    $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } );
111
    $mss->delete if $mss;
112
    Koha::AuthorisedValueCategory->new( { category_name => 'LOC' } )->store;
113
    Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store;
114
    Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
115
    Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
116
    Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store;
117
    Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_1' } )->store;
118
    Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_2' } )->store;
119
    Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_3' } )->store;
120
    Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store;
121
    Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store;
122
    subtest 'search_by_marc_field' => sub {
123
        plan tests => 4;
124
        my $avs;
125
        $avs = Koha::AuthorisedValues->search_by_marc_field();
126
        is ( $avs, undef );
127
        $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' });
128
        is ( $avs, undef );
129
        $avs = Koha::AuthorisedValues->search_by_marc_field({ tagfield => 952, tagsubfield => 'c'});
130
        is( $avs->count, 3, 'default fk');
131
        is( $avs->next->authorised_value, 'location_1', );
132
    };
133
};

Return to bug 17249