X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/3bd03753e22635304bc6ffe385fc60b5229d8c6e..b3420789a7e752f170fb7f433f468a34140028ee:/SL/DB/Helper/LinkedRecords.pm diff --git a/SL/DB/Helper/LinkedRecords.pm b/SL/DB/Helper/LinkedRecords.pm index 1f91718bc..99cb3ed7b 100644 --- a/SL/DB/Helper/LinkedRecords.pm +++ b/SL/DB/Helper/LinkedRecords.pm @@ -4,7 +4,7 @@ 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; @@ -13,6 +13,20 @@ use SL::DB::Helpers::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; @@ -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__ @@ -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