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

(-)a/Koha/MarcOverlayRules.pm (-6 / +75 lines)
Lines 16-28 package Koha::MarcOverlayRules; Link Here
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use List::Util qw(first);
19
use List::Util qw(first any);
20
use Koha::MarcOverlayRule;
20
use Koha::MarcOverlayRule;
21
use Carp;
21
use Carp;
22
22
23
use Koha::Exceptions::MarcOverlayRule;
23
use Koha::Exceptions::MarcOverlayRule;
24
use Try::Tiny;
24
use Try::Tiny;
25
use Scalar::Util qw(looks_like_number);
25
use Scalar::Util qw(looks_like_number);
26
use Clone qw(clone);
26
27
27
use parent qw(Koha::Objects);
28
use parent qw(Koha::Objects);
28
29
Lines 46-51 sub operations { Link Here
46
    return ('add', 'append', 'remove', 'delete');
47
    return ('add', 'append', 'remove', 'delete');
47
}
48
}
48
49
50
=head3 modules
51
52
Returns a list of all modules in order of priority.
53
54
=cut
55
56
sub modules {
57
    return ('userid', 'categorycode', 'source');
58
}
59
60
49
=head3 context_rules
61
=head3 context_rules
50
62
51
    my $rules = Koha::MarcOverlayRules->context_rules($context);
63
    my $rules = Koha::MarcOverlayRules->context_rules($context);
Lines 96-114 sub context_rules { Link Here
96
        $cache->set_in_cache('marc_overlay_rules', $rules);
108
        $cache->set_in_cache('marc_overlay_rules', $rules);
97
    }
109
    }
98
110
99
    my $context_rules = undef;
111
    my $context_rules;
100
    foreach my $module_name (keys %{$context}) {
112
    my @context_modules = grep { exists $context->{$_} } $self->modules();
113
114
    foreach my $module_name (@context_modules) {
101
        if (
115
        if (
102
            exists $rules->{$module_name} &&
116
            exists $rules->{$module_name} &&
103
            exists $rules->{$module_name}->{$context->{$module_name}}
117
            exists $rules->{$module_name}->{$context->{$module_name}}
104
        ) {
118
        ) {
105
            $context_rules = $rules->{$module_name}->{$context->{$module_name}};
119
            $context_rules = $rules->{$module_name}->{$context->{$module_name}};
120
121
            unless (exists $rules->{$module_name}->{'*'}) {
122
                # No wildcard filter rules defined
123
                last;
124
            }
125
126
            # Merge in all wildcard filter rules which has not been overriden
127
            # by the matching context
128
            if ($context_rules->{'*'}) {
129
                # Wildcard tag for matching context will override all rules,
130
                # nothing left to do
131
                last;
132
            }
133
134
            # Clone since we will potentially be modified cached value
135
            # fetched with unsafe => 1
136
            $context_rules = clone($context_rules);
137
138
            if (exists $rules->{$module_name}->{'*'}->{'*'}) {
139
                # Merge in wildcard filter wildcard tag rule if exists
140
                $context_rules->{'*'} = $rules->{$module_name}->{'*'}->{'*'};
141
            }
142
143
            if (exists $rules->{$module_name}->{'*'}->{tags}) {
144
                if (exists $context_rules->{regexps}) {
145
                    # If the current context has regexp rules, we have to make sure
146
                    # to not only to skip tags already present but also those matching
147
                    # any of those rules
148
149
                    my @regexps = map { qr/^$_->[0]$/ } @{$context_rules->{regexps}};
150
151
                    foreach my $tag (keys %{$rules->{$module_name}->{'*'}->{tags}}) {
152
                        unless ((any { $tag =~ $_ } @regexps) || exists $context_rules->{tags}->{$tag}) {
153
                            $context_rules->{tags}->{$tag} = $rules->{$module_name}->{'*'}->{tags}->{$tag};
154
                        }
155
                    }
156
                }
157
                else {
158
                    # Merge in wildcard filter tag rules not already present
159
                    # in the matching context
160
                    foreach my $tag (keys %{$rules->{$module_name}->{'*'}->{tags}}) {
161
                        unless (exists $context_rules->{tags}->{$tag}) {
162
                            $context_rules->{tags}->{$tag} = $rules->{$module_name}->{'*'}->{tags}->{$tag};
163
                        }
164
                    }
165
                }
166
            }
167
168
            if (exists $rules->{$module_name}->{'*'}->{regexps}) {
169
                # Merge in wildcard filter regexp rules last, making sure rules from the
170
                # matching context have precidence
171
                $context_rules->{regexps} //= [];
172
                push @{$context_rules->{regexps}}, @{$rules->{$module_name}->{'*'}->{regexps}};
173
            }
174
106
            last;
175
            last;
107
        }
176
        }
108
    }
177
    }
178
109
    if (!$context_rules) {
179
    if (!$context_rules) {
110
        # No perms matching specific context conditions found, try wildcard value for each active context
180
        # No rules matching specific context conditions found, try wildcard value for each active context
111
        foreach my $module_name (keys %{$context}) {
181
        foreach my $module_name (@context_modules) {
112
            if (exists $rules->{$module_name}->{'*'}) {
182
            if (exists $rules->{$module_name}->{'*'}) {
113
                $context_rules = $rules->{$module_name}->{'*'};
183
                $context_rules = $rules->{$module_name}->{'*'};
114
                last;
184
                last;
115
- 

Return to bug 33268