From 9bebc7cb5fbcbcf618afdf718df9cf86f6378d1a Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 13 Sep 2021 08:05:11 +0000 Subject: [PATCH] Bug 28999: {CONCEPT] Introduce Koha::Object::ColumnSet Content-Type: text/plain; charset=utf-8 --- Koha/Object.pm | 19 ++++++ Koha/Object/ColumnSet.pm | 84 ++++++++++++++++++++++++++ Koha/Schema/Result/City.pm | 9 +++ t/db_dependent/Koha/Object/ColumnSet.t | 41 +++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 Koha/Object/ColumnSet.pm create mode 100755 t/db_dependent/Koha/Object/ColumnSet.t diff --git a/Koha/Object.pm b/Koha/Object.pm index 6f55c3b486..d2c00fcfb2 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -29,6 +29,7 @@ use Koha::Database; use Koha::Exceptions::Object; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Object::Message; +use Koha::Object::ColumnSet; =head1 NAME @@ -784,6 +785,24 @@ sub _columns { return $self->{_columns}; } +=head3 filter + + my $some_columns = $self->filter({ column_set => $column_set_name }); + + Filters the object data through the specified column set (defined in schema file). + +=cut + +sub filter { + my ( $self, $params ) = @_; + my $name = $params->{column_set} or return; + return Koha::Object::ColumnSet->new({ rs => $self->_result, name => $name })->apply_filter; +} + +=head3 _get_object_class + +=cut + sub _get_object_class { my ( $type ) = @_; return unless $type; diff --git a/Koha/Object/ColumnSet.pm b/Koha/Object/ColumnSet.pm new file mode 100644 index 0000000000..bb0e8f5255 --- /dev/null +++ b/Koha/Object/ColumnSet.pm @@ -0,0 +1,84 @@ +package Koha::Object::ColumnSet; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Data::Dumper qw( Dumper ); + +use Koha::AllowList; + +=head1 NAME + +Koha::Object::ColumnSet + +=head1 API + +=head2 Class Methods + +=head3 new + + Constructor. + +=cut + +sub new { + my ( $class, $params ) = @_; # parameter hash keys: rs, name + my $self = bless( $params, $class ); + $self->_get_column_set; + return $self; +} + +sub _get_column_set { + my ( $self ) = @_; + my $rs = $self->{rs} or return; + my $name = $self->{name} or return; + + if( $rs->can('get_column_set') ) { + if( my $list = $rs->get_column_set($name) ) { + $self->{_list} = $list; + $self->{_allowlist} = Koha::AllowList->new({ defaults => $list }); + } + } +} + +=head3 apply_filter + + $set->apply_filter({ data => $data }); + + Apply column set filter to $data. + If you did not pass $data, it will apply to the resultset data. + +=cut + +sub apply_filter { + my ( $self, $params ) = @_; + return if !$self->{_list}; + my $data = $params->{data} // { $self->{rs}->get_columns }; + return $self->{_allowlist}->apply({ input => $data }); +} + +=head3 list_columns + + $set->list_columns + +=cut + +sub list_columns { + my ( $self ) = @_; + return $self->{_list}; +} + +1; diff --git a/Koha/Schema/Result/City.pm b/Koha/Schema/Result/City.pm index 5855dd21ec..ff170c2cd3 100644 --- a/Koha/Schema/Result/City.pm +++ b/Koha/Schema/Result/City.pm @@ -99,4 +99,13 @@ sub koha_objects_class { 'Koha::Cities'; } +sub get_column_set { + my ( $self, $name ) = @_; + my $sets = { + opac_test => [ 'city_name', 'city_state' ], + staff_test => [ 'city_name', 'city_state', 'city_country', 'city_zipcode' ], + }; + return $sets->{$name}; +} + 1; diff --git a/t/db_dependent/Koha/Object/ColumnSet.t b/t/db_dependent/Koha/Object/ColumnSet.t new file mode 100755 index 0000000000..807def6d1a --- /dev/null +++ b/t/db_dependent/Koha/Object/ColumnSet.t @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Data::Dumper qw/Dumper/; +use Test::More tests => 1; + +use t::lib::TestBuilder; + +use Koha::City; +use Koha::Object::ColumnSet; + +my $builder = t::lib::TestBuilder->new; + +subtest 'Test ColumnSet on City' => sub { + plan tests => 1; + ok(1); #FIXME + + my $city = $builder->build_object( { class => 'Koha::Cities' } ); + my $set = Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result }); + + print Dumper( + $city->filter({ column_set => 'opac_test' }), + $set->apply_filter({ data => { city_id => 10, city_name => 'Amsterdam' }}), + ); + +}; -- 2.20.1