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

(-)a/Koha/Object.pm (+19 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 schema file).
793
794
=cut
795
796
sub filter {
797
    my ( $self, $params ) = @_;
798
    my $name = $params->{column_set} or return;
799
    return Koha::Object::ColumnSet->new({ rs => $self->_result, name => $name })->apply_filter;
800
}
801
802
=head3 _get_object_class
803
804
=cut
805
787
sub _get_object_class {
806
sub _get_object_class {
788
    my ( $type ) = @_;
807
    my ( $type ) = @_;
789
    return unless $type;
808
    return unless $type;
(-)a/Koha/Object/ColumnSet.pm (+84 lines)
Line 0 Link Here
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 Data::Dumper qw( Dumper );
20
21
use Koha::AllowList;
22
23
=head1 NAME
24
25
Koha::Object::ColumnSet
26
27
=head1 API
28
29
=head2 Class Methods
30
31
=head3 new
32
33
    Constructor.
34
35
=cut
36
37
sub new {
38
    my ( $class, $params ) = @_; # parameter hash keys: rs, name
39
    my $self = bless( $params, $class );
40
    $self->_get_column_set;
41
    return $self;
42
}
43
44
sub _get_column_set {
45
    my ( $self ) = @_;
46
    my $rs = $self->{rs} or return;
47
    my $name = $self->{name} or return;
48
49
    if( $rs->can('get_column_set') ) {
50
        if( my $list = $rs->get_column_set($name) ) {
51
            $self->{_list} = $list;
52
            $self->{_allowlist} = Koha::AllowList->new({ defaults => $list });
53
        }
54
    }
55
}
56
57
=head3 apply_filter
58
59
    $set->apply_filter({ data => $data });
60
61
    Apply column set filter to $data.
62
    If you did not pass $data, it will apply to the resultset data.
63
64
=cut
65
66
sub apply_filter {
67
    my ( $self, $params ) = @_;
68
    return if !$self->{_list};
69
    my $data = $params->{data} // { $self->{rs}->get_columns };
70
    return $self->{_allowlist}->apply({ input => $data });
71
}
72
73
=head3 list_columns
74
75
    $set->list_columns
76
77
=cut
78
79
sub list_columns {
80
    my ( $self ) = @_;
81
    return $self->{_list};
82
}
83
84
1;
(-)a/Koha/Schema/Result/City.pm (+9 lines)
Lines 99-102 sub koha_objects_class { Link Here
99
    'Koha::Cities';
99
    'Koha::Cities';
100
}
100
}
101
101
102
sub get_column_set {
103
    my ( $self, $name ) = @_;
104
    my $sets = {
105
        opac_test => [ 'city_name', 'city_state' ],
106
        staff_test => [ 'city_name', 'city_state', 'city_country', 'city_zipcode' ],
107
    };
108
    return $sets->{$name};
109
}
110
102
1;
111
1;
(-)a/t/db_dependent/Koha/Object/ColumnSet.t (-1 / +41 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
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 Data::Dumper qw/Dumper/;
20
use Test::More tests => 1;
21
22
use t::lib::TestBuilder;
23
24
use Koha::City;
25
use Koha::Object::ColumnSet;
26
27
my $builder = t::lib::TestBuilder->new;
28
29
subtest 'Test ColumnSet on City' => sub {
30
    plan tests => 1;
31
    ok(1); #FIXME
32
33
    my $city = $builder->build_object( { class => 'Koha::Cities' } );
34
    my $set = Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result });
35
36
    print Dumper(
37
        $city->filter({ column_set => 'opac_test' }),
38
        $set->apply_filter({ data => { city_id => 10, city_name => 'Amsterdam' }}),
39
    );
40
41
};

Return to bug 28999