From 117fefacdfaf457a502b32bae2f0e10fd0e11414 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 30 Jan 2013 11:15:23 +0100 Subject: [PATCH] ActsAsList: Neue Funktion "reorder_list" Conflicts: SL/Controller/ProjectType.pm --- SL/Controller/CustomVariableConfig.pm | 7 +---- SL/Controller/PaymentTerm.pm | 7 +---- SL/Controller/PriceFactor.pm | 7 +---- SL/Controller/Unit.pm | 7 +---- SL/Controller/Warehouse.pm | 7 +---- SL/DB/CustomVariableConfig.pm | 1 + SL/DB/Helper/ActsAsList.pm | 39 ++++++++++++++++++++++++++- SL/DB/PriceFactor.pm | 1 + SL/DB/Unit.pm | 1 + SL/DB/Warehouse.pm | 1 + 10 files changed, 47 insertions(+), 31 deletions(-) diff --git a/SL/Controller/CustomVariableConfig.pm b/SL/Controller/CustomVariableConfig.pm index a2842a0ec..9a151da0e 100644 --- a/SL/Controller/CustomVariableConfig.pm +++ b/SL/Controller/CustomVariableConfig.pm @@ -15,12 +15,7 @@ __PACKAGE__->run_before('check_auth'); 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 }); } diff --git a/SL/Controller/PaymentTerm.pm b/SL/Controller/PaymentTerm.pm index 3a768a2d4..df86252fe 100644 --- a/SL/Controller/PaymentTerm.pm +++ b/SL/Controller/PaymentTerm.pm @@ -68,12 +68,7 @@ sub action_destroy { 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 }); } diff --git a/SL/Controller/PriceFactor.pm b/SL/Controller/PriceFactor.pm index 31b376d2d..5def8aabf 100644 --- a/SL/Controller/PriceFactor.pm +++ b/SL/Controller/PriceFactor.pm @@ -15,12 +15,7 @@ __PACKAGE__->run_before('check_auth'); 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 }); } diff --git a/SL/Controller/Unit.pm b/SL/Controller/Unit.pm index 72cd5ce71..ae6e31305 100644 --- a/SL/Controller/Unit.pm +++ b/SL/Controller/Unit.pm @@ -15,12 +15,7 @@ __PACKAGE__->run_before('check_auth'); 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 }); } diff --git a/SL/Controller/Warehouse.pm b/SL/Controller/Warehouse.pm index cfc84a667..33c8bfcdc 100644 --- a/SL/Controller/Warehouse.pm +++ b/SL/Controller/Warehouse.pm @@ -15,12 +15,7 @@ __PACKAGE__->run_before('check_auth'); 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 }); } diff --git a/SL/DB/CustomVariableConfig.pm b/SL/DB/CustomVariableConfig.pm index 3410a4966..5823e9b7c 100644 --- a/SL/DB/CustomVariableConfig.pm +++ b/SL/DB/CustomVariableConfig.pm @@ -6,6 +6,7 @@ package SL::DB::CustomVariableConfig; 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; 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 diff --git a/SL/DB/PriceFactor.pm b/SL/DB/PriceFactor.pm index 6680a12f3..cf07b52aa 100644 --- a/SL/DB/PriceFactor.pm +++ b/SL/DB/PriceFactor.pm @@ -3,6 +3,7 @@ package SL::DB::PriceFactor; use strict; use SL::DB::MetaSetup::PriceFactor; +use SL::DB::Helper::ActsAsList; __PACKAGE__->meta->make_manager_class; diff --git a/SL/DB/Unit.pm b/SL/DB/Unit.pm index 59740e789..927e31b2e 100644 --- a/SL/DB/Unit.pm +++ b/SL/DB/Unit.pm @@ -4,6 +4,7 @@ use strict; use SL::DB::MetaSetup::Unit; use SL::DB::Manager::Unit; +use SL::DB::Helper::ActsAsList; __PACKAGE__->meta->add_relationships( base => { diff --git a/SL/DB/Warehouse.pm b/SL/DB/Warehouse.pm index 938c31e91..0557976f3 100644 --- a/SL/DB/Warehouse.pm +++ b/SL/DB/Warehouse.pm @@ -6,6 +6,7 @@ package SL::DB::Warehouse; use strict; use SL::DB::MetaSetup::Warehouse; +use SL::DB::Helper::ActsAsList; __PACKAGE__->meta->make_manager_class; -- 2.20.1