From b3420789a7e752f170fb7f433f468a34140028ee Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 12 Jan 2011 16:05:39 +0100 Subject: [PATCH] =?utf8?q?linked=5Frecords:=20Parameter=20'filter'=20f?= =?utf8?q?=C3=BCr=20Filtern=20nach=20"darf=20von=20aktueller=20Benutzerin?= =?utf8?q?=20gesehen=20werden"?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/DB/Helper/LinkedRecords.pm | 36 +++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/SL/DB/Helper/LinkedRecords.pm b/SL/DB/Helper/LinkedRecords.pm index 30b08f417..99cb3ed7b 100644 --- a/SL/DB/Helper/LinkedRecords.pm +++ b/SL/DB/Helper/LinkedRecords.pm @@ -17,14 +17,16 @@ sub linked_records { 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); + 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 { +sub linked_records_implementation { my $self = shift; my %params = @_; @@ -35,8 +37,8 @@ sub _linked_records_implementation { my %from_to = ( from => delete($params{from}) || $both, to => delete($params{to}) || $both); - 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 @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; @@ -171,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__ @@ -218,6 +233,19 @@ 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 -- 2.20.1