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

(-)a/Koha/Util/Dbrev.pm (-30 / +55 lines)
Lines 22-30 package Koha::Util::Dbrev; Link Here
22
22
23
use Modern::Perl;
23
use Modern::Perl;
24
24
25
use Koha::BiblioFrameworks;
25
use C4::Context;
26
use Koha::Caches;
26
use Koha::Caches;
27
use Koha::MarcSubfieldStructures;
28
27
29
=head1 NAME
28
=head1 NAME
30
29
Lines 54-72 The routines in this modules are called in OO style as class methods. Link Here
54
sub sync_kohafield {
53
sub sync_kohafield {
55
    my ( $class ) = @_;
54
    my ( $class ) = @_;
56
55
56
    my $dbh = C4::Context->dbh;
57
57
    # Clear the destination frameworks first
58
    # Clear the destination frameworks first
58
    Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, kohafield => { '>', q{} } })->update({ kohafield => undef });
59
    $dbh->do(q|
60
        UPDATE marc_subfield_structure
61
        SET kohafield = NULL
62
        WHERE   frameworkcode > ''
63
            AND     Kohafield > ''
64
    |);
59
65
60
    # Now copy from Default
66
    # Now copy from Default
61
    my $rs = Koha::MarcSubfieldStructures->search({ frameworkcode => q{}, kohafield => { '>', q{} } });
67
    my $msss = $dbh->selectall_arrayref(q|
62
    while( my $rec = $rs->next ) {
68
        SELECT kohafield, tagfield, tagsubfield
63
        Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, tagfield => $rec->tagfield, tagsubfield => $rec->tagsubfield })->update({ kohafield => $rec->kohafield });
69
        FROM marc_subfield_structure
70
        WHERE   frameworkcode = ''
71
            AND     kohafield > ''
72
    |, { Slice => {} });
73
    my $sth = $dbh->prepare(q|
74
        UPDATE marc_subfield_structure
75
        SET kohafield = ?
76
        WHERE frameworkcode > ''
77
        AND tagfield = ?
78
        AND tagsubfield = ?
79
    |);
80
    for my $mss ( @$msss ) {
81
        $sth->execute( $mss->{kohafield}, $mss->{tagfield}, $mss->{tagsubfield} );
64
    }
82
    }
65
83
84
    my @frameworkcodes = $dbh->selectall_arrayref(q|
66
    # Clear the cache
85
    # Clear the cache
67
    my @fw = Koha::BiblioFrameworks->search({ frameworkcode => { '>', q{} } })->get_column('frameworkcode');
86
        SELECT frameworkcode FROM biblio_framework WHERE frameworkcode > ''
68
    foreach( @fw ) {
87
    |);
69
        Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$_" );
88
    for my $frameworkcode ( @frameworkcodes ) {
89
        Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$frameworkcode" );
70
    }
90
    }
71
    Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-");
91
    Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-");
72
}
92
}
Lines 84-112 sub sync_kohafield { Link Here
84
=cut
104
=cut
85
105
86
sub get_kohafield_exceptions {
106
sub get_kohafield_exceptions {
87
    my ( $class, $params ) = @_;
107
    my ( $class ) = @_;
88
108
89
    # Too bad that DBIx is not very helpful here
109
    my $dbh = C4::Context->dbh;
90
    my $default = Koha::MarcSubfieldStructures->search({
110
    my $msss = $dbh->selectall_arrayref(q|
91
        frameworkcode => q{},
111
        SELECT kohafield, tagfield, tagsubfield
92
        kohafield => { '>' => '' },
112
        FROM marc_subfield_structure
93
    });
113
        WHERE   frameworkcode != ''
94
    my $other = Koha::MarcSubfieldStructures->search({
114
    |, { Slice => {} });
95
        frameworkcode => { '!=' => q{} },
115
96
    }, { order_by => { -asc => [ qw/tagfield tagsubfield/ ] } });
116
97
117
    my $sth = $dbh->prepare(q|
98
    # Compare
118
        SELECT kohafield
99
    my @return;
119
        FROM marc_subfield_structure
100
    while( my $rec = $other->next ) {
120
        WHERE frameworkcode = ''
101
        my @tags = ( $rec->tagfield, $rec->tagsubfield );
121
        AND tagfield = ?
102
        my $defa = $default->find( q{}, @tags );
122
        AND tagsubfield = ?
103
        if( $rec->kohafield ) {
123
    |);
104
            push @return, [ $rec->frameworkcode, @tags, $rec->kohafield ] if !$defa || !$defa->kohafield || $defa->kohafield ne $rec->kohafield;
124
125
    my @exceptions;
126
    for my $mss ( @$msss ) {
127
        $sth->execute($mss->{tagfield}, $mss->{tagsubfield} );
128
        my ( $default_kohafield ) = $sth->fetchrow_array();
129
        if( $mss->{kohafield} ) {
130
            push @exceptions, [ $mss->{frameworkcode}, $mss->{tagfield}, $mss->{tagsubfield}, $mss->{kohafield} ] if not $default_kohafield or $default_kohafield ne $mss->{kohafield};
105
        } else {
131
        } else {
106
            push @return, [ $rec->frameworkcode, @tags, q{} ] if $defa && $defa->kohafield;
132
            push @exceptions, [ $mss->{frameworkcode}, $mss->{tagfield}, $mss->{tagsubfield}, q{} ] if $default_kohafield;
107
        }
133
        }
108
    }
134
    }
109
    return \@return;
135
    return \@exceptions;
110
}
136
}
111
137
112
=head1 AUTHOR
138
=head1 AUTHOR
113
- 

Return to bug 19096