X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FLinkedRecords.pm;h=6dad81f2ea2a1d89436e90555343666a079705dc;hb=89c9ff022d3f13e27ba6bda085df15707fcfb0eb;hp=1f91718bcb9670ab03c52deab6b1f2e1f024d108;hpb=3bd03753e22635304bc6ffe385fc60b5229d8c6e;p=kivitendo-erp.git diff --git a/SL/DB/Helper/LinkedRecords.pm b/SL/DB/Helper/LinkedRecords.pm index 1f91718bc..6dad81f2e 100644 --- a/SL/DB/Helper/LinkedRecords.pm +++ b/SL/DB/Helper/LinkedRecords.pm @@ -1,18 +1,32 @@ -package SL::DB::Helpers::LinkedRecords; +package SL::DB::Helper::LinkedRecords; use strict; require Exporter; our @ISA = qw(Exporter); -our @EXPORT = qw(linked_records link_to_record linked_records_sorted); +our @EXPORT = qw(linked_records link_to_record); use Carp; use Sort::Naturally; -use SL::DB::Helpers::Mappings; +use SL::DB::Helper::Mappings; use SL::DB::RecordLink; sub linked_records { + my ($self, %params) = @_; + + my %sort_spec = ( by => delete($params{sort_by}), + dir => delete($params{sort_dir}) ); + my $filter = delete $params{filter}; + + my $records = linked_records_implementation($self, %params); + $records = filter_linked_records($self, $filter, @{ $records }) if $filter; + $records = sort_linked_records($self, $sort_spec{by}, $sort_spec{dir}, @{ $records }) if $sort_spec{by}; + + return $records; +} + +sub linked_records_implementation { my $self = shift; my %params = @_; @@ -23,8 +37,8 @@ sub linked_records { my %from_to = ( from => delete($params{from}) || $both, to => delete($params{to}) || $both); - my @records = (@{ $self->linked_records(%params, direction => 'from', from => $from_to{from}) }, - @{ $self->linked_records(%params, direction => 'to', to => $from_to{to} ) }); + my @records = (@{ linked_records_implementation($self, %params, direction => 'from', from => $from_to{from}) }, + @{ linked_records_implementation($self, %params, direction => 'to', to => $from_to{to} ) }); my %record_map = map { ( ref($_) . $_->id => $_ ) } @records; @@ -33,14 +47,14 @@ sub linked_records { my $myself = $wanted eq 'from' ? 'to' : $wanted eq 'to' ? 'from' : croak("Invalid parameter `direction'"); - my $my_table = SL::DB::Helpers::Mappings::get_table_for_package(ref($self)); + my $my_table = SL::DB::Helper::Mappings::get_table_for_package(ref($self)); my @query = ( "${myself}_table" => $my_table, "${myself}_id" => $self->id ); if ($params{$wanted}) { my $wanted_classes = ref($params{$wanted}) eq 'ARRAY' ? $params{$wanted} : [ $params{$wanted} ]; - my $wanted_tables = [ map { SL::DB::Helpers::Mappings::get_table_for_package($_) || croak("Invalid parameter `${wanted}'") } @{ $wanted_classes } ]; + my $wanted_tables = [ map { SL::DB::Helper::Mappings::get_table_for_package($_) || croak("Invalid parameter `${wanted}'") } @{ $wanted_classes } ]; push @query, ("${wanted}_table" => $wanted_tables); } @@ -53,8 +67,8 @@ sub linked_records { @query = ref($params{query}) eq 'ARRAY' ? @{ $params{query} } : (); foreach my $link (@{ $links }) { - my $manager_class = SL::DB::Helpers::Mappings::get_manager_package_for_table($link->$sub_wanted_table); - my $object_class = SL::DB::Helpers::Mappings::get_package_for_table($link->$sub_wanted_table); + my $manager_class = SL::DB::Helper::Mappings::get_manager_package_for_table($link->$sub_wanted_table); + my $object_class = SL::DB::Helper::Mappings::get_package_for_table($link->$sub_wanted_table); eval "require " . $object_class . "; 1;"; push @{ $records }, @{ $manager_class->get_all(query => [ id => $link->$sub_wanted_id, @query ]) }; } @@ -88,12 +102,6 @@ sub link_to_record { return wantarray ? @links : $links[0]; } -sub linked_records_sorted { - my ($self, $sort_by, $sort_dir, %params) = @_; - - return sort_linked_records($self, $sort_by, $sort_dir, $self->linked_records(%params)); -} - sub sort_linked_records { my ($self_or_class, $sort_by, $sort_dir, @records) = @_; @@ -165,6 +173,19 @@ sub sort_linked_records { return [ sort($comparator @records) ]; } +sub filter_linked_records { + my ($self_or_class, $filter, @records) = @_; + + if ($filter eq 'accessible') { + my $employee = SL::DB::Manager::Employee->current; + @records = grep { !$_->can('may_be_accessed') || $_->may_be_accessed($employee) } @records; + } else { + croak "Unsupported filter parameter '${filter}'"; + } + + return \@records; +} + 1; __END__ @@ -173,7 +194,7 @@ __END__ =head1 NAME -SL::DB::Helpers::LinkedRecords - Mixin for retrieving linked records via the table C +SL::DB::Helper::LinkedRecords - Mixin for retrieving linked records via the table C =head1 FUNCTIONS @@ -208,6 +229,23 @@ created today: to => 'SL::DB::Invoice', query => [ transdate => DateTime->today_local ]); +The optional parameters C<$params{sort_by}> and C<$params{sort_dir}> +can be used in order to sort the result. If C<$params{sort_by}> is +trueish then the result is sorted by calling L. + +The optional parameter C<$params{filter}> controls whether or not the +result is filtered. Supported values are: + +=over 2 + +=item C + +Removes all objects for which the function C from the +mixin L exists and returns falsish for +the current employee. + +=back + Returns an array reference. =item C @@ -259,19 +297,12 @@ Can be called both as a class or as an instance function. This function is not exported. -=item C - -Returns the result of L sorted by -L. C<%params> is passed to -L. C<$sort_by> and C<$sort_dir> are passed to -L. - =back =head1 EXPORTS -This mixin exports the functions L, -L and L. +This mixin exports the functions L and +L. =head1 BUGS