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

(-)a/C4/XSLT.pm (-76 / +15 lines)
Lines 28-49 use C4::Koha qw( xml_escape ); Link Here
28
use C4::Biblio qw( GetAuthorisedValueDesc GetFrameworkCode GetMarcStructure );
28
use C4::Biblio qw( GetAuthorisedValueDesc GetFrameworkCode GetMarcStructure );
29
use Koha::AuthorisedValues;
29
use Koha::AuthorisedValues;
30
use Koha::ItemTypes;
30
use Koha::ItemTypes;
31
use Koha::RecordProcessor;
31
use Koha::XSLT::Base;
32
use Koha::XSLT::Base;
32
use Koha::Libraries;
33
use Koha::Libraries;
33
use Koha::Recalls;
34
use Koha::Recalls;
34
35
35
my $engine; #XSLT Handler object
36
my $engine; #XSLT Handler object
36
my %authval_per_framework;
37
    # Cache for tagfield-tagsubfield to decode per framework.
38
    # Should be preferably be placed in Koha-core...
39
37
40
our (@ISA, @EXPORT_OK);
38
our (@ISA, @EXPORT_OK);
41
BEGIN {
39
BEGIN {
42
    require Exporter;
40
    require Exporter;
43
    @ISA = qw(Exporter);
41
    @ISA = qw(Exporter);
44
    @EXPORT_OK = qw(
42
    @EXPORT_OK = qw(
45
        transformMARCXML4XSLT
46
        getAuthorisedValues4MARCSubfields
47
        buildKohaItemsNamespace
43
        buildKohaItemsNamespace
48
        XSLTParse4Display
44
        XSLTParse4Display
49
    );
45
    );
Lines 56-130 C4::XSLT - Functions for displaying XSLT-generated content Link Here
56
52
57
=head1 FUNCTIONS
53
=head1 FUNCTIONS
58
54
59
=head2 transformMARCXML4XSLT
60
61
Replaces codes with authorized values in a MARC::Record object
62
63
=cut
64
65
sub transformMARCXML4XSLT {
66
    my ($biblionumber, $record, $opac) = @_;
67
    my $frameworkcode = GetFrameworkCode($biblionumber) || '';
68
    my $tagslib = &GetMarcStructure(1, $frameworkcode, { unsafe => 1 });
69
    my @fields;
70
    # FIXME: wish there was a better way to handle exceptions
71
    eval {
72
        @fields = $record->fields();
73
    };
74
    if ($@) { warn "PROBLEM WITH RECORD"; next; }
75
    my $marcflavour = C4::Context->preference('marcflavour');
76
    my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
77
    foreach my $tag ( keys %$av ) {
78
        foreach my $field ( $record->field( $tag ) ) {
79
            if ( $av->{ $tag } ) {
80
                my @new_subfields = ();
81
                for my $subfield ( $field->subfields() ) {
82
                    my ( $letter, $value ) = @$subfield;
83
                    # Replace the field value with the authorised value *except* for MARC21 field 942$n (suppression in opac)
84
                    if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) || $marcflavour eq 'UNIMARC' ) {
85
                        $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib, undef, $opac )
86
                            if $av->{ $tag }->{ $letter };
87
                    }
88
                    push( @new_subfields, $letter, $value );
89
                } 
90
                $field ->replace_with( MARC::Field->new(
91
                    $tag,
92
                    $field->indicator(1),
93
                    $field->indicator(2),
94
                    @new_subfields
95
                ) );
96
            }
97
        }
98
    }
99
    return $record;
100
}
101
102
=head2 getAuthorisedValues4MARCSubfields
103
104
Returns a ref of hash of ref of hash for tag -> letter controlled by authorised values
105
Is only used in this module currently.
106
107
=cut
108
109
sub getAuthorisedValues4MARCSubfields {
110
    my ($frameworkcode) = @_;
111
    unless ( $authval_per_framework{ $frameworkcode } ) {
112
        my $dbh = C4::Context->dbh;
113
        my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
114
                                 FROM marc_subfield_structure
115
                                 WHERE authorised_value IS NOT NULL
116
                                   AND authorised_value!=''
117
                                   AND frameworkcode=?");
118
        $sth->execute( $frameworkcode );
119
        my $av = { };
120
        while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
121
            $av->{ $tag }->{ $letter } = 1;
122
        }
123
        $authval_per_framework{ $frameworkcode } = $av;
124
    }
125
    return $authval_per_framework{ $frameworkcode };
126
}
127
128
=head2 XSLTParse4Display
55
=head2 XSLTParse4Display
129
56
130
Returns xml for biblionumber and requested XSLT transformation.
57
Returns xml for biblionumber and requested XSLT transformation.
Lines 245-252 sub XSLTParse4Display { Link Here
245
    my ( $params ) = @_;
172
    my ( $params ) = @_;
246
173
247
    my $biblionumber = $params->{biblionumber};
174
    my $biblionumber = $params->{biblionumber};
248
    my $orig_record  = $params->{record};
175
    my $record       = $params->{record};
249
    my $xslsyspref   = $params->{xsl_syspref};
176
    my $xslsyspref   = $params->{xsl_syspref};
177
    my $interface    = ( $xslsyspref =~ /OPAC/ ) ? 'opac' : 'intranet' ;
250
    my $fixamps      = $params->{fix_amps};
178
    my $fixamps      = $params->{fix_amps};
251
    my $hidden_items = $params->{hidden_items} || [];
179
    my $hidden_items = $params->{hidden_items} || [];
252
    my $variables    = $params->{xslt_variables};
180
    my $variables    = $params->{xslt_variables};
Lines 257-264 sub XSLTParse4Display { Link Here
257
185
258
    my $xslfilename = get_xsl_filename( $xslsyspref);
186
    my $xslfilename = get_xsl_filename( $xslsyspref);
259
187
188
    my $frameworkcode = GetFrameworkCode($biblionumber) || '';
189
    my $record_processor = Koha::RecordProcessor->new(
190
        {
191
            filters => [ 'ExpandCodedFields' ],
192
            options => {
193
                interface     => $interface,
194
                frameworkcode => $frameworkcode
195
            }
196
        }
197
    );
198
    $record_processor->process($record);
199
260
    # grab the XML, run it through our stylesheet, push it out to the browser
200
    # grab the XML, run it through our stylesheet, push it out to the browser
261
    my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
262
    my $itemsxml;
201
    my $itemsxml;
263
    if ( $xslsyspref eq "OPACXSLTDetailsDisplay" || $xslsyspref eq "XSLTDetailsDisplay" || $xslsyspref eq "XSLTResultsDisplay" ) {
202
    if ( $xslsyspref eq "OPACXSLTDetailsDisplay" || $xslsyspref eq "XSLTDetailsDisplay" || $xslsyspref eq "XSLTResultsDisplay" ) {
264
        $itemsxml = ""; #We don't use XSLT for items display on these pages
203
        $itemsxml = ""; #We don't use XSLT for items display on these pages
(-)a/Koha/OAI/Server/Repository.pm (-4 / +15 lines)
Lines 25-30 use HTTP::OAI::Repository qw( validate_request ); Link Here
25
25
26
use base ("HTTP::OAI::Repository");
26
use base ("HTTP::OAI::Repository");
27
27
28
use Koha::RecordProcessor;
28
use Koha::OAI::Server::Identify;
29
use Koha::OAI::Server::Identify;
29
use Koha::OAI::Server::ListSets;
30
use Koha::OAI::Server::ListSets;
30
use Koha::OAI::Server::ListMetadataFormats;
31
use Koha::OAI::Server::ListMetadataFormats;
Lines 35-42 use XML::SAX::Writer; Link Here
35
use YAML::XS;
36
use YAML::XS;
36
use CGI qw/:standard -oldstyle_urls/;
37
use CGI qw/:standard -oldstyle_urls/;
37
use C4::Context;
38
use C4::Context;
38
use C4::Biblio qw( GetMarcBiblio );
39
use C4::Biblio qw( GetMarcBiblio GetFrameworkCode );
39
use C4::XSLT qw( transformMARCXML4XSLT );
40
use Koha::XSLT::Base;
40
use Koha::XSLT::Base;
41
41
42
=head1 NAME
42
=head1 NAME
Lines 183-190 sub get_biblio_marcxml { Link Here
183
            opac         => 1
183
            opac         => 1
184
        }
184
        }
185
    );
185
    );
186
    $record = transformMARCXML4XSLT( $biblionumber, $record )
186
    if ( $expanded_avs ) {
187
        if $expanded_avs;
187
        my $frameworkcode = GetFrameworkCode($biblionumber) || '';
188
        my $record_processor = Koha::RecordProcessor->new(
189
            {
190
                filters => [ 'ExpandCodedFields' ],
191
                options => {
192
                    interface     => 'opac',
193
                    frameworkcode => $frameworkcode
194
                }
195
            }
196
        );
197
        $record_processor->process($record) if $expanded_avs;
198
    }
188
199
189
    return $record ? $record->as_xml_record() : undef;
200
    return $record ? $record->as_xml_record() : undef;
190
}
201
}
(-)a/t/db_dependent/XSLT.t (-14 / +2 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use MARC::Record;
20
use MARC::Record;
21
use Test::More tests => 4;
21
use Test::More tests => 3;
22
use Test::Warn;
22
use Test::Warn;
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
24
use t::lib::Mocks;
Lines 28-34 use Koha::Libraries; Link Here
28
use Koha::ItemTypes;
28
use Koha::ItemTypes;
29
29
30
BEGIN {
30
BEGIN {
31
    use_ok('C4::XSLT', qw( transformMARCXML4XSLT getAuthorisedValues4MARCSubfields buildKohaItemsNamespace ));
31
    use_ok('C4::XSLT', qw( buildKohaItemsNamespace ));
32
}
32
}
33
33
34
my $schema  = Koha::Database->new->schema;
34
my $schema  = Koha::Database->new->schema;
Lines 36-52 my $builder = t::lib::TestBuilder->new; Link Here
36
36
37
$schema->storage->txn_begin;
37
$schema->storage->txn_begin;
38
38
39
subtest 'transformMARCXML4XSLT tests' => sub {
40
    plan tests => 1;
41
    my $mock_xslt =  Test::MockModule->new("C4::XSLT");
42
    $mock_xslt->mock( getAuthorisedValues4MARCSubfields => sub { return { 942 => { 'n' => 1 } } } );
43
    $mock_xslt->mock( GetAuthorisedValueDesc => sub { warn "called"; });
44
    my $record = MARC::Record->new();
45
    my $suppress_field = MARC::Field->new( 942, ' ', ' ', n => '1' );
46
    $record->append_fields($suppress_field);
47
    warning_is { C4::XSLT::transformMARCXML4XSLT( 3,$record ) } undef, "942n auth value not translated";
48
};
49
50
subtest 'buildKohaItemsNamespace status tests' => sub {
39
subtest 'buildKohaItemsNamespace status tests' => sub {
51
    plan tests => 17;
40
    plan tests => 17;
52
41
53
- 

Return to bug 30921