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 / +110 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
    # If the schema does not contain this sub, _list will not be set.
51
    # If the sub exists but the name of the set is not found, _list will be undef.
52
    # See further apply_filter.
53
    if( $rs->can('get_column_set') ) {
54
        $self->{_list} = $rs->get_column_set($name);
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
    NOTE: Returns all data if schema does not support get_column_set yet. Returns undef if set not found.
67
68
=cut
69
70
sub apply_filter {
71
    my ( $self, $params ) = @_;
72
    my $data = $params->{input} // { $self->{rs}->get_columns };
73
    my $log_level = $params->{log_level};
74
    return $data if !exists( $self->{_list} ); # no get_column_set in schema, just return all data
75
    return unless $self->{_list}; # no results if the specific column set is not found
76
77
    my ( $result, $blocked );
78
    unless( $params->{deny_mode} ) { # as allow list
79
        $result = { map { exists $data->{$_} ? ($_, $data->{$_}) : () } @{$self->{_list}} };
80
    } else { # as deny list
81
        $result = { %$data };
82
        map { delete $result->{$_} } @{$self->{_list}};
83
    }
84
85
    # Handle optional diagnostics, logging
86
    if( $params->{dump} || $log_level ) {
87
        $blocked = { map { !exists $result->{$_} ? ($_, $data->{$_}) : () } keys %$data };
88
        map { $params->{dump}->{$_} = $blocked->{$_} } keys %$blocked if $params->{dump};
89
        if( $log_level && keys %$blocked ) {
90
            my $stack = Carp::longmess; $stack =~ s/\n/, /g;
91
            my $diag = Data::Dumper->new([ $stack, $blocked ])->Indent(0)->Dump;
92
            Koha::Logger->get->$log_level( $diag );
93
        }
94
    }
95
96
    return $result;
97
}
98
99
=head3 list_columns
100
101
    $set->list_columns
102
103
=cut
104
105
sub list_columns {
106
    my ( $self ) = @_;
107
    return [ sort @{$self->{_list}} ] if $self->{_list};
108
}
109
110
1;

Return to bug 29029