3 |
3 |
use strict;
|
4 |
4 |
|
5 |
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);
|
|
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);
|
7 |
8 |
|
8 |
9 |
use Carp;
|
9 |
10 |
|
... | ... | |
112 |
113 |
return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
|
113 |
114 |
}
|
114 |
115 |
|
|
116 |
sub get_next_in_list {
|
|
117 |
my ($self) = @_;
|
|
118 |
return get_previous_or_next($self, 'next');
|
|
119 |
}
|
|
120 |
|
|
121 |
sub get_previous_in_list {
|
|
122 |
my ($self) = @_;
|
|
123 |
return get_previous_or_next($self, 'previous');
|
|
124 |
}
|
|
125 |
|
115 |
126 |
sub reorder_list {
|
116 |
127 |
my ($class_or_self, @ids) = @_;
|
117 |
128 |
|
... | ... | |
245 |
256 |
$self->update_attributes($column => $new_position);
|
246 |
257 |
}
|
247 |
258 |
|
|
259 |
sub get_previous_or_next {
|
|
260 |
my ($self, $direction) = @_;
|
|
261 |
|
|
262 |
my $asc_desc = $direction eq 'next' ? 'ASC' : 'DESC';
|
|
263 |
my $comparator = $direction eq 'next' ? '>' : '<';
|
|
264 |
my $table = $self->meta->table;
|
|
265 |
my $column = column_name($self);
|
|
266 |
my $primary_key_col = ($self->meta->primary_key)[0];
|
|
267 |
my ($group_by, @values) = get_group_by_where($self);
|
|
268 |
$group_by = " AND ${group_by}" if $group_by;
|
|
269 |
my $sql = <<SQL;
|
|
270 |
SELECT ${primary_key_col}
|
|
271 |
FROM ${table}
|
|
272 |
WHERE (${column} ${comparator} ?)
|
|
273 |
${group_by}
|
|
274 |
ORDER BY ${column} ${asc_desc}
|
|
275 |
LIMIT 1
|
|
276 |
SQL
|
|
277 |
|
|
278 |
my $id = ($self->db->dbh->selectrow_arrayref($sql, undef, $self->$column, @values) || [])->[0];
|
|
279 |
|
|
280 |
return $id ? $self->_get_manager_class->find_by(id => $id) : undef;
|
|
281 |
}
|
|
282 |
|
248 |
283 |
sub column_name {
|
249 |
284 |
my ($self) = @_;
|
250 |
285 |
my $column = get_spec(ref $self, 'column_name');
|
... | ... | |
372 |
407 |
Sets this items positional column to C<-1>, saves it and moves all
|
373 |
408 |
following items up by 1.
|
374 |
409 |
|
|
410 |
=item C<get_previous_in_list>
|
|
411 |
|
|
412 |
Fetches the previous item in the list. Returns C<undef> if C<$self> is
|
|
413 |
already the first one.
|
|
414 |
|
|
415 |
=item C<get_next_in_list>
|
|
416 |
|
|
417 |
Fetches the next item in the list. Returns C<undef> if C<$self> is
|
|
418 |
already the last one.
|
|
419 |
|
375 |
420 |
=item C<reorder_list @ids>
|
376 |
421 |
|
377 |
422 |
Re-orders the objects given in C<@ids> by their position in C<@ids> by
|
ActsAsList: get_next_in_list() und get_previous_in_list()