X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FActsAsList.pm;h=f48e9967a834546eda7fd10821101bbba0afa7bf;hb=b2f713102369a59726b6fb128dbb0be2fc7dc8e6;hp=566422a9db6d2e2a4754cdc7b3bb36550c6b5bd2;hpb=49556f01cfe6a395cf5554e1a00dc61d6da0493e;p=kivitendo-erp.git diff --git a/SL/DB/Helper/ActsAsList.pm b/SL/DB/Helper/ActsAsList.pm index 566422a9d..f48e9967a 100644 --- a/SL/DB/Helper/ActsAsList.pm +++ b/SL/DB/Helper/ActsAsList.pm @@ -4,7 +4,7 @@ use strict; use parent qw(Exporter); our @EXPORT = qw(move_position_up move_position_down add_to_list remove_from_list reorder_list configure_acts_as_list - get_previous_in_list get_next_in_list); + get_previous_in_list get_next_in_list get_full_list); use Carp; @@ -14,12 +14,13 @@ sub import { my ($class, @params) = @_; my $importing = caller(); + configure_acts_as_list($importing, @params); + $importing->before_save( sub { SL::DB::Helper::ActsAsList::set_position(@_) }); $importing->before_delete(sub { SL::DB::Helper::ActsAsList::remove_position(@_) }); - # Use 'goto' so that Exporter knows which module to import into via - # 'caller()'. - goto &Exporter::import; + # Don't 'goto' to Exporters import, it would try to parse @params + __PACKAGE__->export_to_level(1, $class, @EXPORT); } # @@ -65,6 +66,10 @@ sub add_to_list { croak "Invalid parameter 'position'" unless ($params{position} || '') =~ m/^ (?: before | after | first | last ) $/x; + my $column = column_name($self); + + $self->remove_from_list if ($self->$column // -1) != -1; + if ($params{position} eq 'last') { set_position($self); $self->save; @@ -73,7 +78,6 @@ sub add_to_list { my $table = $self->meta->table; my $primary_key_col = ($self->meta->primary_key)[0]; - my $column = column_name($self); my ($group_by, @values) = get_group_by_where($self); $group_by = " AND ${group_by}" if $group_by; my $new_position; @@ -123,6 +127,16 @@ sub get_previous_in_list { return get_previous_or_next($self, 'previous'); } +sub get_full_list { + my ($self) = @_; + + my $group_by = get_spec(ref $self, 'group_by') || []; + $group_by = [ $group_by ] if $group_by && !ref $group_by; + my @where = map { ($_ => $self->$_) } @{ $group_by }; + + return $self->_get_manager_class->get_all(where => \@where, sort_by => column_name($self) . ' ASC'); +} + sub reorder_list { my ($class_or_self, @ids) = @_; @@ -309,7 +323,7 @@ column =head1 SYNOPSIS package SL::DB::SomeObject; - use SL::DB::Helper::ActsAsList; + use SL::DB::Helper::ActsAsList [ PARAMS ]; package SL::Controller::SomeController; ... @@ -334,7 +348,8 @@ in the table plus one. When the object is deleted all positions greater than the object's old position are decreased by one. -The column name to use can be configured via L. +C will be given to L and can be used to +set the column name. =head1 CLASS FUNCTIONS @@ -342,8 +357,8 @@ The column name to use can be configured via L. =item C -Configures the mixin's behaviour. C<%params> can contain the following -values: +Configures the mixin's behaviour. Will get called automatically with the +include parameters. C<%params> can contain the following values: =over 2 @@ -399,6 +414,10 @@ one. The current item will then be inserted either before or after the referenced item by shifting all the appropriate item positions up by one. +If C<$self>'s positional column is already set when this function is +called then L will be called first before anything +else is done. + After this function C<$self>'s positional column has been set and saved to the database. @@ -417,6 +436,11 @@ already the first one. Fetches the next item in the list. Returns C if C<$self> is already the last one. +=item C + +Fetches all items in the same list as C<$self> and returns them as an +array reference. + =item C Re-orders the objects given in C<@ids> by their position in C<@ids> by