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

(-)a/Koha/Object.pm (+26 lines)
Lines 29-34 use Koha::Database; Link Here
29
use Koha::Exceptions::Object;
29
use Koha::Exceptions::Object;
30
use Koha::DateUtils qw( dt_from_string output_pref );
30
use Koha::DateUtils qw( dt_from_string output_pref );
31
use Koha::Object::Message;
31
use Koha::Object::Message;
32
use Koha::Object::ColumnSet;
32
33
33
=head1 NAME
34
=head1 NAME
34
35
Lines 784-789 sub _columns { Link Here
784
    return $self->{_columns};
785
    return $self->{_columns};
785
}
786
}
786
787
788
=head3 filter
789
790
    my $some_columns = $self->filter({ column_set => $column_set_name });
791
792
    Filters the object data through the specified column set (defined in
793
    schema file).
794
    Allows filtering other input (hashref) by including input => $input.
795
    When you pass deny_mode => 1, the column set is used as a deny list.
796
    Normally, it is considered as an allow list.
797
    Diagnostics via dump => $dump, logging via log_level => $log_level.
798
    Note: $dump contains the blocked/discarded entries.
799
    Log level is debug, warn, etc.
800
801
=cut
802
803
sub filter {
804
    my ( $self, $params ) = @_;
805
    my $set = Koha::Object::ColumnSet->new({ rs => $self->_result, name => $params->{column_set} });
806
    return $set->apply_filter({ input => $params->{input}, deny_mode => $params->{deny_mode}, log_level => $params->{log_level}, dump => $params->{dump} });
807
}
808
809
=head3 _get_object_class
810
811
=cut
812
787
sub _get_object_class {
813
sub _get_object_class {
788
    my ( $type ) = @_;
814
    my ( $type ) = @_;
789
    return unless $type;
815
    return unless $type;
(-)a/Koha/Object/ColumnSet.pm (-1 / +106 lines)
Line 0 Link Here
0
- 
1
package Koha::Object::ColumnSet;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use Carp ();
20
use Data::Dumper ();
21
22
use Koha::Logger ();
23
24
=head1 NAME
25
26
Koha::Object::ColumnSet
27
28
=head1 API
29
30
=head2 Class Methods
31
32
=head3 new
33
34
    Constructor.
35
36
=cut
37
38
sub new {
39
    my ( $class, $params ) = @_; # parameter hash keys: rs, name
40
    my $self = bless( $params, $class );
41
    $self->_get_column_set;
42
    return $self;
43
}
44
45
sub _get_column_set {
46
    my ( $self ) = @_;
47
    my $rs = $self->{rs} or return;
48
    my $name = $self->{name} or return;
49
50
    # We assume now that the caller of apply_filter knows about get_column_set
51
    $self->{_list} = [];
52
    if( $rs->can('get_column_set') ) {
53
        my $list = $rs->get_column_set($name);
54
        $self->{_list} = $list if ref($list) eq 'ARRAY';
55
    }
56
}
57
58
=head3 apply_filter
59
60
    $set->apply_filter({ input => $input, deny_mode => $deny_mode, log_level => $log_level, dump => $dump });
61
62
    Apply column set filter to $input or the passed resultset (see new).
63
    $deny_mode turns the column set into a deny list.
64
    Diagnostics and logging via $dump and $log_level.
65
66
=cut
67
68
sub apply_filter {
69
    my ( $self, $params ) = @_;
70
    my $data = $params->{input} // { $self->{rs}->get_columns };
71
    my $log_level = $params->{log_level};
72
73
    my ( $result, $blocked );
74
    unless( $params->{deny_mode} ) { # as allow list
75
        $result = { map { exists $data->{$_} ? ($_, $data->{$_}) : () } @{$self->{_list}} };
76
    } else { # as deny list
77
        $result = { %$data };
78
        map { delete $result->{$_} } @{$self->{_list}};
79
    }
80
81
    # Handle optional diagnostics, logging
82
    if( $params->{dump} || $log_level ) {
83
        $blocked = { map { !exists $result->{$_} ? ($_, $data->{$_}) : () } keys %$data };
84
        map { $params->{dump}->{$_} = $blocked->{$_} } keys %$blocked if $params->{dump};
85
        if( $log_level && keys %$blocked ) {
86
            my $stack = Carp::longmess; $stack =~ s/\n/, /g;
87
            my $diag = Data::Dumper->new([ $stack, $blocked ])->Indent(0)->Dump;
88
            Koha::Logger->get->$log_level( $diag );
89
        }
90
    }
91
92
    return $result;
93
}
94
95
=head3 list_columns
96
97
    $set->list_columns
98
99
=cut
100
101
sub list_columns {
102
    my ( $self ) = @_;
103
    return [ sort @{$self->{_list}} ];
104
}
105
106
1;

Return to bug 29029