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

(-)a/C4/Biblio.pm (-14 / +58 lines)
Lines 689-714 sub GetRecordValue { Link Here
689
        carp 'GetRecordValue called with undefined record';
689
        carp 'GetRecordValue called with undefined record';
690
        return;
690
        return;
691
    }
691
    }
692
    my $dbh = C4::Context->dbh;
693
692
694
    my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?');
693
    # All field mappings are retrieved from cache, or from DB if not already
695
    $sth->execute( $frameworkcode, $field );
694
    # cached.
695
    # The mapping is a data structure that looks like that:
696
    # '' => {
697
    #     subtitle => [
698
    #         [ '200', 'e' ],
699
    #         [ '400', 'e' ],
700
    #     },
701
    #     maintitle => [
702
    #         [ '200', 'a' ],
703
    #         [ '400', 'a' ],
704
    #     },
705
    #  },
706
    #  MAIN = {
707
    #     subtitle => [
708
    #         [ '200', 'e' ],
709
    #     },
710
    #     maintitle => [
711
    #         [ '200', 'a' ],
712
    #     },
713
    #  },
714
715
    my $key = 'FieldMapping';
716
    my $cache = Koha::Cache->get_instance();
717
    my $mapping = $cache->get_from_cache($key);
718
    unless ( $mapping ) {
719
        my $sth = C4::Context->dbh->prepare('SELECT * FROM fieldmapping');
720
        $sth->execute;
721
        $mapping = {};
722
        for my $map ( @{ $sth->fetchall_arrayref({}) } ) {
723
            my ($field, $framework, $tag, $letter) = (
724
                $map->{field},
725
                $map->{frameworkcode},
726
                $map->{fieldcode},
727
                $map->{subfieldcode} );
728
            $mapping->{$framework} ||= {};
729
            $mapping->{$framework}->{$field} ||= [];
730
            push @{ $mapping->{$framework}->{$field} }, [ $tag, $letter ];
731
        }
732
        $cache->set_in_cache($key, $mapping);
733
    }
734
735
    # Does the requested field mapping exists from the framework?
736
    my $maps = $mapping->{$frameworkcode};
737
    return unless $maps;
738
    $maps = $maps->{$field};
739
    return unless $maps;
696
740
697
    my @result = ();
741
    my @result = ();
698
742
    for my $map (@$maps) {
699
    while ( my $row = $sth->fetchrow_hashref ) {
743
        my ($tag, $letter) = @$map;
700
        foreach my $field ( $record->field( $row->{fieldcode} ) ) {
744
        foreach my $field ( $record->field($tag) ) {
701
            if ( ( $row->{subfieldcode} ne "" && $field->subfield( $row->{subfieldcode} ) ) ) {
745
            if ( $letter ) {
702
                foreach my $subfield ( $field->subfield( $row->{subfieldcode} ) ) {
746
                if ( my @values = $field->subfield($letter) ) {
703
                    push @result, { 'subfield' => $subfield };
747
                    push @result, { subfield => $_ } for @values;
704
                }
748
                }
705
749
            }
706
            } elsif ( $row->{subfieldcode} eq "" ) {
750
            else {
707
                push @result, { 'subfield' => $field->as_string() };
751
                push @result, { subfield => $field->as_string() };
708
            }
752
            }
709
        }
753
        }
710
    }
754
    }
711
712
    return \@result;
755
    return \@result;
713
}
756
}
714
757
Lines 732-737 sub SetFieldMapping { Link Here
732
775
733
        $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
776
        $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
734
    }
777
    }
778
    my $cache = Koha::Cache->get_instance()->clear_from_cache('FieldMapping');
735
}
779
}
736
780
737
=head2 DeleteFieldMapping
781
=head2 DeleteFieldMapping
Lines 748-753 sub DeleteFieldMapping { Link Here
748
792
749
    my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
793
    my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
750
    $sth->execute($id);
794
    $sth->execute($id);
795
    my $cache = Koha::Cache->get_instance()->clear_from_cache('FieldMapping');
751
}
796
}
752
797
753
=head2 GetFieldMapping
798
=head2 GetFieldMapping
754
- 

Return to bug 13146