Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 3a56098f

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID 3a56098f9b80397fe27922f1661b2069183aa2d6
  • Vorgänger dbd7b379
  • Nachfolger c73b2658

Unterstützung für Tabellen mit Spalte "sortkey" anstelle von "position"

Unterschiede anzeigen:

SL/DB/Helper/ActsAsList.pm
39 39

  
40 40
sub set_position {
41 41
  my ($self) = @_;
42
  if (!defined $self->position) {
43
    my $max_position = $self->db->dbh->selectrow_arrayref(qq|SELECT COALESCE(max(position), 0) FROM | . $self->meta->table)->[0];
44
    $self->position($max_position + 1);
42
  my $column = column_name($self);
43

  
44
  if (!defined $self->$column) {
45
    my $max_position = $self->db->dbh->selectrow_arrayref(qq|SELECT COALESCE(max(${column}), 0) FROM | . $self->meta->table)->[0];
46
    $self->$column($max_position + 1);
45 47
  }
46 48

  
47 49
  return 1;
......
49 51

  
50 52
sub remove_position {
51 53
  my ($self) = @_;
54
  my $column = column_name($self);
52 55

  
53 56
  $self->load;
54
  if (defined $self->position) {
55
    $self->_get_manager_class->update_all(set   => { position => \'position - 1' },
56
                                          where => [ position => { gt => $self->position } ]);
57
  if (defined $self->$column) {
58
    $self->_get_manager_class->update_all(set   => { $column => \"${column} - 1" },
59
                                          where => [ $column => { gt => $self->$column } ]);
57 60
  }
58 61

  
59 62
  return 1;
......
61 64

  
62 65
sub do_move {
63 66
  my ($self, $direction) = @_;
67
  my $column             = column_name($self);
64 68

  
65 69
  croak "Object has not been saved yet" unless $self->id;
66
  croak "No position set yet"           unless defined $self->position;
70
  croak "No position set yet"           unless defined $self->$column;
67 71

  
68 72
  my ($comp_sql, $comp_rdbo, $min_max, $plus_minus) = $direction eq 'up' ? ('<', 'ge', 'max', '+') : ('>', 'le', 'min', '-');
69 73

  
70
  my $new_position = $self->db->dbh->selectrow_arrayref(qq|SELECT ${min_max}(position) FROM | . $self->meta->table . qq| WHERE position ${comp_sql} | . $self->position)->[0];
74
  my $new_position = $self->db->dbh->selectrow_arrayref(qq|SELECT ${min_max}(${column}) FROM | . $self->meta->table . qq| WHERE ${column} ${comp_sql} | . $self->$column)->[0];
71 75

  
72 76
  return undef unless defined $new_position;
73 77

  
74
  $self->_get_manager_class->update_all(set   => { position => $self->position },
75
                                        where => [ position => $new_position ]);
76
  $self->update_attributes(position => $new_position);
78
  $self->_get_manager_class->update_all(set   => { $column => $self->$column },
79
                                        where => [ $column => $new_position ]);
80
  $self->update_attributes($column => $new_position);
81
}
82

  
83
sub column_name {
84
  my ($self) = @_;
85
  return $self->can('sortkey') ? 'sortkey' : 'position';
77 86
}
78 87

  
79 88
1;
......
85 94

  
86 95
=head1 NAME
87 96

  
88
SL::DB::Helper::ActsAsList - Mixin for managing ordered items by a column I<position>
97
SL::DB::Helper::ActsAsList - Mixin for managing ordered items by a
98
column I<position> or I<sortkey>
89 99

  
90 100
=head1 SYNOPSIS
91 101

  
......
107 117
  $obj->delete
108 118

  
109 119
This mixin assumes that the mixing package's table contains a column
110
called C<position>. This column is set automatically upon saving the
111
object if it hasn't been set already. If it hasn't then it will be set
112
to the maximum position used in the table plus one.
120
called C<position> or C<sortkey> (for legacy tables). This column is
121
set automatically upon saving the object if it hasn't been set
122
already. If it hasn't then it will be set to the maximum position used
123
in the table plus one.
113 124

  
114 125
When the object is deleted all positions greater than the object's old
115 126
position are decreased by one.

Auch abrufbar als: Unified diff