Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 117fefac

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

ActsAsList: Neue Funktion "reorder_list"

Conflicts:
SL/Controller/ProjectType.pm

Unterschiede anzeigen:

SL/Controller/CustomVariableConfig.pm
15 15
sub action_reorder {
16 16
  my ($self) = @_;
17 17

  
18
  my @ids = @{ $::form->{cvarcfg_id} || [] };
19
  my $result = SL::DB::CustomVariableConfig->new->db->do_transaction(sub {
20
    foreach my $idx (0 .. scalar(@ids) - 1) {
21
      SL::DB::CustomVariableConfig->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
22
    }
23
  });
18
  SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
24 19

  
25 20
  $self->render('1;', { type => 'js', inline => 1 });
26 21
}
SL/Controller/PaymentTerm.pm
68 68
sub action_reorder {
69 69
  my ($self) = @_;
70 70

  
71
  my @ids = @{ $::form->{payment_term_id} || [] };
72
  my $result = SL::DB::PaymentTerm->new->db->do_transaction(sub {
73
    foreach my $idx (0 .. scalar(@ids) - 1) {
74
      SL::DB::PaymentTerm->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
75
    }
76
  });
71
  SL::DB::PaymentTerm->reorder_list(@{ $::form->{payment_term_id} || [] });
77 72

  
78 73
  $self->render('1;', { type => 'js', inline => 1 });
79 74
}
SL/Controller/PriceFactor.pm
15 15
sub action_reorder {
16 16
  my ($self) = @_;
17 17

  
18
  my @ids = @{ $::form->{price_factor_id} || [] };
19
  my $result = SL::DB::PriceFactor->new->db->do_transaction(sub {
20
    foreach my $idx (0 .. scalar(@ids) - 1) {
21
      SL::DB::PriceFactor->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
22
    }
23
  });
18
  SL::DB::PriceFactor->reorder_list(@{ $::form->{price_factor_id} || [] });
24 19

  
25 20
  $self->render('1;', { type => 'js', inline => 1 });
26 21
}
SL/Controller/Unit.pm
15 15
sub action_reorder {
16 16
  my ($self) = @_;
17 17

  
18
  my @ids = @{ $::form->{unit_id} || [] };
19
  my $result = SL::DB::Unit->new->db->do_transaction(sub {
20
    foreach my $idx (0 .. scalar(@ids) - 1) {
21
      SL::DB::Unit->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
22
    }
23
  });
18
  SL::DB::Unit->reorder_list(@{ $::form->{unit_id} || [] });
24 19

  
25 20
  $self->render('1;', { type => 'js', inline => 1 });
26 21
}
SL/Controller/Warehouse.pm
15 15
sub action_reorder {
16 16
  my ($self) = @_;
17 17

  
18
  my @ids = @{ $::form->{warehouse_id} || [] };
19
  my $result = SL::DB::Warehouse->new->db->do_transaction(sub {
20
    foreach my $idx (0 .. scalar(@ids) - 1) {
21
      SL::DB::Warehouse->new(id => $ids[$idx])->load->update_attributes(sortkey => $idx + 1);
22
    }
23
  });
18
  SL::DB::Warehouse->reorder_list(@{ $::form->{warehouse_id} || [] });
24 19

  
25 20
  $self->render('1;', { type => 'js', inline => 1 });
26 21
}
SL/DB/CustomVariableConfig.pm
6 6
use strict;
7 7

  
8 8
use SL::DB::MetaSetup::CustomVariableConfig;
9
use SL::DB::Helper::ActsAsList;
9 10

  
10 11
# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
11 12
__PACKAGE__->meta->make_manager_class;
SL/DB/Helper/ActsAsList.pm
3 3
use strict;
4 4

  
5 5
use parent qw(Exporter);
6
our @EXPORT = qw(move_position_up move_position_down);
6
our @EXPORT = qw(move_position_up move_position_down reorder_list);
7 7

  
8 8
use Carp;
9 9

  
......
33 33
  do_move($self, 'down');
34 34
}
35 35

  
36
sub reorder_list {
37
  my ($class_or_self, @ids) = @_;
38

  
39
  return 1 unless @ids;
40

  
41
  my $self   = ref($class_or_self) ? $class_or_self : $class_or_self->new;
42
  my $column = column_name($self);
43
  my $result = $self->db->do_transaction(sub {
44
    my $query = qq|UPDATE | . $self->meta->table . qq| SET ${column} = ? WHERE id = ?|;
45
    my $sth   = $self->db->dbh->prepare($query) || die $self->db->dbh->errstr;
46

  
47
    foreach my $new_position (1 .. scalar(@ids)) {
48
      $sth->execute($new_position, $ids[$new_position - 1]) || die $sth->errstr;
49
    }
50

  
51
    $sth->finish;
52
  });
53

  
54
  return $result;
55
}
56

  
36 57
#
37 58
# Helper functions
38 59
#
......
139 160
Swaps the object with the object one step below the current one
140 161
regarding their sort order by exchanging their C<position> values.
141 162

  
163
=item C<reorder_list @ids>
164

  
165
Re-orders the objects given in C<@ids> by their position in C<@ids> by
166
updating all of their positional columns. Each element in
167
C<@positions> must be the ID of an object. The new position is the
168
ID's index inside C<@ids> plus one (meaning the first element's new
169
position will be 1 and not 0).
170

  
171
This works by executing SQL "UPDATE" statements directly.
172

  
173
Returns the result of the whole transaction (trueish in case of
174
success).
175

  
176
This method can be called both as a class method or an instance
177
method.
178

  
142 179
=back
143 180

  
144 181
=head1 BUGS
SL/DB/PriceFactor.pm
3 3
use strict;
4 4

  
5 5
use SL::DB::MetaSetup::PriceFactor;
6
use SL::DB::Helper::ActsAsList;
6 7

  
7 8
__PACKAGE__->meta->make_manager_class;
8 9

  
SL/DB/Unit.pm
4 4

  
5 5
use SL::DB::MetaSetup::Unit;
6 6
use SL::DB::Manager::Unit;
7
use SL::DB::Helper::ActsAsList;
7 8

  
8 9
__PACKAGE__->meta->add_relationships(
9 10
  base => {
SL/DB/Warehouse.pm
6 6
use strict;
7 7

  
8 8
use SL::DB::MetaSetup::Warehouse;
9
use SL::DB::Helper::ActsAsList;
9 10

  
10 11
__PACKAGE__->meta->make_manager_class;
11 12

  

Auch abrufbar als: Unified diff