X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FActsAsList.pm;h=7ca63a353fde47542a4a3740076a770fa488190a;hb=2a0cbd885790174fa0f212e6661b30362650a42c;hp=f5bc8c293eae4365145c309e72fa2a5d71f74516;hpb=a341d959ab798890746200efd0d5983c66f67db7;p=kivitendo-erp.git diff --git a/SL/DB/Helper/ActsAsList.pm b/SL/DB/Helper/ActsAsList.pm index f5bc8c293..7ca63a353 100644 --- a/SL/DB/Helper/ActsAsList.pm +++ b/SL/DB/Helper/ActsAsList.pm @@ -3,9 +3,11 @@ package SL::DB::Helper::ActsAsList; 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); +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_full_list); use Carp; +use SL::X; my %list_spec; @@ -13,12 +15,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); } # @@ -38,7 +41,7 @@ sub move_position_down { sub remove_from_list { my ($self) = @_; - my $worker = sub { + return $self->db->with_transaction(sub { remove_position($self); # Set to -1 manually because $self->update_attributes() would @@ -54,9 +57,7 @@ sub remove_from_list { SQL $self->db->dbh->do($sql, undef, $self->$primary_key_col); $self->$column(undef); - }; - - return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker); + }); } sub add_to_list { @@ -64,6 +65,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; @@ -72,7 +77,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; @@ -104,12 +108,30 @@ sub add_to_list { ${group_by} SQL - my $worker = sub { + return $self->db->with_transaction(sub { $self->db->dbh->do($query, undef, $new_position - 1, @values); $self->update_attributes($column => $new_position); - }; + }); +} + +sub get_next_in_list { + my ($self) = @_; + return get_previous_or_next($self, 'next'); +} + +sub get_previous_in_list { + my ($self) = @_; + 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->db->in_transaction ? $worker->() : $self->db->do_transaction($worker); + return $self->_get_manager_class->get_all(where => \@where, sort_by => column_name($self) . ' ASC'); } sub reorder_list { @@ -119,15 +141,17 @@ sub reorder_list { my $self = ref($class_or_self) ? $class_or_self : $class_or_self->new; my $column = column_name($self); - my $result = $self->db->do_transaction(sub { + my $result = $self->db->with_transaction(sub { my $query = qq|UPDATE | . $self->meta->table . qq| SET ${column} = ? WHERE id = ?|; - my $sth = $self->db->dbh->prepare($query) || die $self->db->dbh->errstr; + my $sth = $self->db->dbh->prepare($query) || SL::X::DBUtilsError->throw(msg => 'reorder_list error', db_error => $self->db->dbh->errstr); foreach my $new_position (1 .. scalar(@ids)) { - $sth->execute($new_position, $ids[$new_position - 1]) || die $sth->errstr; + $sth->execute($new_position, $ids[$new_position - 1]) || SL::X::DBUtilsError->throw(msg => 'reorder_list error', db_error => $sth->errstr); } $sth->finish; + + 1; }); return $result; @@ -245,6 +269,30 @@ SQL $self->update_attributes($column => $new_position); } +sub get_previous_or_next { + my ($self, $direction) = @_; + + my $asc_desc = $direction eq 'next' ? 'ASC' : 'DESC'; + my $comparator = $direction eq 'next' ? '>' : '<'; + my $table = $self->meta->table; + my $column = column_name($self); + my $primary_key_col = ($self->meta->primary_key)[0]; + my ($group_by, @values) = get_group_by_where($self); + $group_by = " AND ${group_by}" if $group_by; + my $sql = <db->dbh->selectrow_arrayref($sql, undef, $self->$column, @values) || [])->[0]; + + return $id ? $self->_get_manager_class->find_by(id => $id) : undef; +} + sub column_name { my ($self) = @_; my $column = get_spec(ref $self, 'column_name'); @@ -274,7 +322,7 @@ column =head1 SYNOPSIS package SL::DB::SomeObject; - use SL::DB::Helper::ActsAsList; + use SL::DB::Helper::ActsAsList [ PARAMS ]; package SL::Controller::SomeController; ... @@ -299,7 +347,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 @@ -307,8 +356,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 @@ -364,6 +413,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. @@ -372,6 +425,21 @@ saved to the database. Sets this items positional column to C<-1>, saves it and moves all following items up by 1. +=item C + +Fetches the previous item in the list. Returns C if C<$self> is +already the first one. + +=item C + +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