sub action_reorder {
my ($self) = @_;
- my @ids = @{ $::form->{cvarcfg_id} || [] };
- my $result = SL::DB::CustomVariableConfig->new->db->do_transaction(sub {
- foreach my $idx (0 .. scalar(@ids) - 1) {
- SL::DB::CustomVariableConfig->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
- }
- });
+ SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
$self->render('1;', { type => 'js', inline => 1 });
}
sub action_reorder {
my ($self) = @_;
- my @ids = @{ $::form->{payment_term_id} || [] };
- my $result = SL::DB::PaymentTerm->new->db->do_transaction(sub {
- foreach my $idx (0 .. scalar(@ids) - 1) {
- SL::DB::PaymentTerm->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
- }
- });
+ SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
$self->render('1;', { type => 'js', inline => 1 });
}
sub action_reorder {
my ($self) = @_;
- my @ids = @{ $::form->{price_factor_id} || [] };
- my $result = SL::DB::PriceFactor->new->db->do_transaction(sub {
- foreach my $idx (0 .. scalar(@ids) - 1) {
- SL::DB::PriceFactor->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
- }
- });
+ SL::DB::PriceFactor->reorder_list(@{ $::form->{price_factor_id} || [] });
$self->render('1;', { type => 'js', inline => 1 });
}
sub action_reorder {
my ($self) = @_;
- my @ids = @{ $::form->{unit_id} || [] };
- my $result = SL::DB::Unit->new->db->do_transaction(sub {
- foreach my $idx (0 .. scalar(@ids) - 1) {
- SL::DB::Unit->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
- }
- });
+ SL::DB::Unit->reorder_list(@{ $::form->{unit_id} || [] });
$self->render('1;', { type => 'js', inline => 1 });
}
sub action_reorder {
my ($self) = @_;
- my @ids = @{ $::form->{warehouse_id} || [] };
- my $result = SL::DB::Warehouse->new->db->do_transaction(sub {
- foreach my $idx (0 .. scalar(@ids) - 1) {
- SL::DB::Warehouse->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
- }
- });
+ SL::DB::Warehouse->reorder_list(@{ $::form->{warehouse_id} || [] });
$self->render('1;', { type => 'js', inline => 1 });
}
use strict;
use SL::DB::MetaSetup::CustomVariableConfig;
+use SL::DB::Helper::ActsAsList;
# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
__PACKAGE__->meta->make_manager_class;
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;
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
#
Swaps the object with the object one step below the current one
regarding their sort order by exchanging their C<position> values.
+=item C<reorder_list @ids>
+
+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
use strict;
use SL::DB::MetaSetup::PriceFactor;
+use SL::DB::Helper::ActsAsList;
__PACKAGE__->meta->make_manager_class;
use SL::DB::MetaSetup::Unit;
use SL::DB::Manager::Unit;
+use SL::DB::Helper::ActsAsList;
__PACKAGE__->meta->add_relationships(
base => {
use strict;
use SL::DB::MetaSetup::Warehouse;
+use SL::DB::Helper::ActsAsList;
__PACKAGE__->meta->make_manager_class;