1 package SL::DB::Helper::ActsAsList;
5 use parent qw(Exporter);
6 our @EXPORT = qw(move_position_up move_position_down add_to_list remove_from_list reorder_list configure_acts_as_list
7 get_previous_in_list get_next_in_list get_full_list);
14 my ($class, @params) = @_;
15 my $importing = caller();
17 $importing->before_save( sub { SL::DB::Helper::ActsAsList::set_position(@_) });
18 $importing->before_delete(sub { SL::DB::Helper::ActsAsList::remove_position(@_) });
20 # Use 'goto' so that Exporter knows which module to import into via
22 goto &Exporter::import;
29 sub move_position_up {
34 sub move_position_down {
36 do_move($self, 'down');
39 sub remove_from_list {
43 remove_position($self);
45 # Set to -1 manually because $self->update_attributes() would
46 # trigger the before_save() hook from this very plugin assigning a
47 # number at the end of the list again.
48 my $table = $self->meta->table;
49 my $column = column_name($self);
50 my $primary_key_col = ($self->meta->primary_key)[0];
54 WHERE ${primary_key_col} = ?
56 $self->db->dbh->do($sql, undef, $self->$primary_key_col);
57 $self->$column(undef);
60 return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
64 my ($self, %params) = @_;
66 croak "Invalid parameter 'position'" unless ($params{position} || '') =~ m/^ (?: before | after | first | last ) $/x;
68 my $column = column_name($self);
70 $self->remove_from_list if ($self->$column // -1) != -1;
72 if ($params{position} eq 'last') {
78 my $table = $self->meta->table;
79 my $primary_key_col = ($self->meta->primary_key)[0];
80 my ($group_by, @values) = get_group_by_where($self);
81 $group_by = " AND ${group_by}" if $group_by;
84 if ($params{position} eq 'first') {
88 # Can only be 'before' or 'after' -- 'last' has been checked above
91 my $reference = $params{reference};
92 croak "Missing parameter 'reference'" if !$reference;
96 $reference_pos = $reference->$column;
98 ($reference_pos) = $self->db->dbh->selectrow_array(qq|SELECT ${column} FROM ${table} WHERE ${primary_key_col} = ?|, undef, $reference);
101 $new_position = $params{position} eq 'before' ? $reference_pos : $reference_pos + 1;
106 SET ${column} = ${column} + 1
107 WHERE (${column} > ?)
112 $self->db->dbh->do($query, undef, $new_position - 1, @values);
113 $self->update_attributes($column => $new_position);
116 return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
119 sub get_next_in_list {
121 return get_previous_or_next($self, 'next');
124 sub get_previous_in_list {
126 return get_previous_or_next($self, 'previous');
132 my $group_by = get_spec(ref $self, 'group_by') || [];
133 $group_by = [ $group_by ] if $group_by && !ref $group_by;
134 my @where = map { ($_ => $self->$_) } @{ $group_by };
136 return $self->_get_manager_class->get_all(where => \@where, sort_by => column_name($self) . ' ASC');
140 my ($class_or_self, @ids) = @_;
142 return 1 unless @ids;
144 my $self = ref($class_or_self) ? $class_or_self : $class_or_self->new;
145 my $column = column_name($self);
146 my $result = $self->db->do_transaction(sub {
147 my $query = qq|UPDATE | . $self->meta->table . qq| SET ${column} = ? WHERE id = ?|;
148 my $sth = $self->db->dbh->prepare($query) || die $self->db->dbh->errstr;
150 foreach my $new_position (1 .. scalar(@ids)) {
151 $sth->execute($new_position, $ids[$new_position - 1]) || die $sth->errstr;
160 sub configure_acts_as_list {
161 my ($class, %params) = @_;
163 $list_spec{$class} = {
164 group_by => $params{group_by},
165 column_name => $params{column_name},
173 sub get_group_by_where {
176 my $group_by = get_spec(ref $self, 'group_by') || [];
177 $group_by = [ $group_by ] if $group_by && !ref $group_by;
179 my (@where, @values);
180 foreach my $column (@{ $group_by }) {
181 my $value = $self->$column;
182 push @values, $value if defined $value;
183 push @where, defined($value) ? "(${column} = ?)" : "(${column} IS NULL)";
186 return (join(' AND ', @where), @values);
191 my $column = column_name($self);
192 my $value = $self->$column;
194 return 1 if defined($value) && ($value != -1);
196 my $table = $self->meta->table;
197 my ($group_by, @values) = get_group_by_where($self);
198 $group_by = " AND ${group_by}" if $group_by;
200 SELECT COALESCE(MAX(${column}), 0)
202 WHERE (${column} <> -1)
206 my $max_position = $self->db->dbh->selectrow_arrayref($sql, undef, @values)->[0];
207 $self->$column($max_position + 1);
212 sub remove_position {
214 my $column = column_name($self);
217 my $value = $self->$column;
218 return 1 unless defined($value) && ($value != -1);
220 my $table = $self->meta->table;
221 my ($group_by, @values) = get_group_by_where($self);
222 $group_by = ' AND ' . $group_by if $group_by;
225 SET ${column} = ${column} - 1
226 WHERE (${column} > ?)
230 $self->db->dbh->do($sql, undef, $value, @values);
236 my ($self, $direction) = @_;
238 croak "Object has not been saved yet" unless $self->id;
240 my $column = column_name($self);
241 my $old_position = $self->$column;
242 croak "No position set yet" unless defined($old_position) && ($old_position != -1);
244 my $table = $self->meta->table;
245 my ($comp_sel, $comp_upd, $min_max, $plus_minus) = $direction eq 'up' ? ('<', '>=', 'MAX', '+') : ('>', '<=', 'MIN', '-');
246 my ($group_by, @values) = get_group_by_where($self);
247 $group_by = ' AND ' . $group_by if $group_by;
249 SELECT ${min_max}(${column})
251 WHERE (${column} <> -1)
252 AND (${column} ${comp_sel} ?)
256 my $new_position = $self->db->dbh->selectrow_arrayref($sql, undef, $old_position, @values)->[0];
258 return undef unless defined $new_position;
263 WHERE (${column} = ?)
267 $self->db->dbh->do($sql, undef, $old_position, $new_position, @values);
269 $self->update_attributes($column => $new_position);
272 sub get_previous_or_next {
273 my ($self, $direction) = @_;
275 my $asc_desc = $direction eq 'next' ? 'ASC' : 'DESC';
276 my $comparator = $direction eq 'next' ? '>' : '<';
277 my $table = $self->meta->table;
278 my $column = column_name($self);
279 my $primary_key_col = ($self->meta->primary_key)[0];
280 my ($group_by, @values) = get_group_by_where($self);
281 $group_by = " AND ${group_by}" if $group_by;
283 SELECT ${primary_key_col}
285 WHERE (${column} ${comparator} ?)
287 ORDER BY ${column} ${asc_desc}
291 my $id = ($self->db->dbh->selectrow_arrayref($sql, undef, $self->$column, @values) || [])->[0];
293 return $id ? $self->_get_manager_class->find_by(id => $id) : undef;
298 my $column = get_spec(ref $self, 'column_name');
299 return $column if $column;
300 return $self->can('sortkey') ? 'sortkey' : 'position';
304 my ($class, $key) = @_;
306 return undef unless $list_spec{$class};
307 return $list_spec{$class}->{$key};
319 SL::DB::Helper::ActsAsList - Mixin for managing ordered items by a
324 package SL::DB::SomeObject;
325 use SL::DB::Helper::ActsAsList;
327 package SL::Controller::SomeController;
329 # Assign a position automatically
330 $obj = SL::DB::SomeObject->new(description => 'bla');
333 # Move items up and down
334 $obj = SL::DB::SomeOBject->new(id => 1)->load;
335 $obj->move_position_up;
336 $obj->move_position_down;
338 # Adjust all remaining positions automatically
341 This mixin assumes that the mixing package's table contains a column
342 called C<position> or C<sortkey> (for legacy tables). This column is
343 set automatically upon saving the object if it hasn't been set
344 already. If it hasn't then it will be set to the maximum position used
345 in the table plus one.
347 When the object is deleted all positions greater than the object's old
348 position are decreased by one.
350 The column name to use can be configured via L<configure_acts_as_list>.
352 =head1 CLASS FUNCTIONS
356 =item C<configure_acts_as_list %params>
358 Configures the mixin's behaviour. C<%params> can contain the following
365 The name of the column containing the position. If not set explicitly
366 then the mixin will use C<sortkey> if the model contains such a column
367 (only for legacy tables) and C<position> otherwise.
371 An optional column name (or array reference of column names) by which
372 to group. If a table contains items for several distinct sets and each
373 set has its own sorting then this can be used.
375 An example would be requirement spec text blocks. They have a column
376 called C<output_position> that selects where to output the text blocks
377 (either before or after the sections). Furthermore these text blocks
378 each belong to a single requirement spec document. So each combination
379 of C<requirement_spec_id> and C<output_position> should have its own
380 set of C<position> values, which can be achieved by configuring this
381 mixin with C<group_by = [qw(requirement_spec_id output_position)]>.
387 =head1 INSTANCE FUNCTIONS
391 =item C<move_position_up>
393 Swaps the object with the object one step above the current one
394 regarding their sort order by exchanging their C<position> values.
396 =item C<move_position_down>
398 Swaps the object with the object one step below the current one
399 regarding their sort order by exchanging their C<position> values.
401 =item C<add_to_list %params>
403 Adds this item to the list. The parameter C<position> is required and
404 can be one of C<first>, C<last>, C<before> and C<after>. With C<first>
405 the item is inserted as the first item in the list and all other
406 item's positions are shifted up by one. For C<position = last> the
407 item is inserted at the end of the list.
409 For C<before> and C<after> an additional parameter C<reference> is
410 required. This is either a Rose model instance or the primary key of
411 one. The current item will then be inserted either before or after the
412 referenced item by shifting all the appropriate item positions up by
415 If C<$self>'s positional column is already set when this function is
416 called then L</remove_from_list> will be called first before anything
419 After this function C<$self>'s positional column has been set and
420 saved to the database.
422 =item C<remove_from_list>
424 Sets this items positional column to C<-1>, saves it and moves all
425 following items up by 1.
427 =item C<get_previous_in_list>
429 Fetches the previous item in the list. Returns C<undef> if C<$self> is
430 already the first one.
432 =item C<get_next_in_list>
434 Fetches the next item in the list. Returns C<undef> if C<$self> is
435 already the last one.
437 =item C<get_full_list>
439 Fetches all items in the same list as C<$self> and returns them as an
442 =item C<reorder_list @ids>
444 Re-orders the objects given in C<@ids> by their position in C<@ids> by
445 updating all of their positional columns. Each element in
446 C<@positions> must be the ID of an object. The new position is the
447 ID's index inside C<@ids> plus one (meaning the first element's new
448 position will be 1 and not 0).
450 This works by executing SQL "UPDATE" statements directly.
452 Returns the result of the whole transaction (trueish in case of
455 This method can be called both as a class method or an instance
466 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>