X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FActsAsList.pm;h=ce927bba41390be1149bf9f041c80215846a2246;hb=117fefacdfaf457a502b32bae2f0e10fd0e11414;hp=a6450533d32fb9c793eda902c52359962c36ae04;hpb=de0f9532013c861dae78aa01b9633284d1ceee7c;p=kivitendo-erp.git diff --git a/SL/DB/Helper/ActsAsList.pm b/SL/DB/Helper/ActsAsList.pm index a6450533d..ce927bba4 100644 --- a/SL/DB/Helper/ActsAsList.pm +++ b/SL/DB/Helper/ActsAsList.pm @@ -3,7 +3,7 @@ package SL::DB::Helper::ActsAsList; use strict; use parent qw(Exporter); -our @EXPORT = qw(move_position_up move_position_down); +our @EXPORT = qw(move_position_up move_position_down reorder_list); use Carp; @@ -33,6 +33,27 @@ sub move_position_down { do_move($self, 'down'); } +sub reorder_list { + my ($class_or_self, @ids) = @_; + + return 1 unless @ids; + + 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 $query = qq|UPDATE | . $self->meta->table . qq| SET ${column} = ? WHERE id = ?|; + my $sth = $self->db->dbh->prepare($query) || die $self->db->dbh->errstr; + + foreach my $new_position (1 .. scalar(@ids)) { + $sth->execute($new_position, $ids[$new_position - 1]) || die $sth->errstr; + } + + $sth->finish; + }); + + return $result; +} + # # Helper functions # @@ -139,6 +160,22 @@ regarding their sort order by exchanging their C values. Swaps the object with the object one step below the current one regarding their sort order by exchanging their C values. +=item C + +Re-orders the objects given in C<@ids> by their position in C<@ids> by +updating all of their positional columns. Each element in +C<@positions> must be the ID of an object. The new position is the +ID's index inside C<@ids> plus one (meaning the first element's new +position will be 1 and not 0). + +This works by executing SQL "UPDATE" statements directly. + +Returns the result of the whole transaction (trueish in case of +success). + +This method can be called both as a class method or an instance +method. + =back =head1 BUGS