private function shufflePositionsOnIntermediateItems($old_position, $new_position, $avoid_id = NULL)
{
if ($old_position == $new_position) {
return;
}
$avoid_id_condition = $avoid_id ? $this->primaryKey() . " != " . $avoid_id : '1 = 1';
if ($old_position < $new_position) {
// Decrement position of intermediate items
// e.g., if moving an item from 2 to 5,
// move [3, 4, 5] to [2, 3, 4]
$this->listifyList()->where($this->positionColumn(), '>', $old_position)->where($this->positionColumn(), '<=', $new_position)->whereRaw($avoid_id_condition)->decrement($this->positionColumn());
} else {
// Increment position of intermediate items
// e.g., if moving an item from 5 to 2,
// move [2, 3, 4] to [3, 4, 5]
$this->listifyList()->where($this->positionColumn(), '>=', $new_position)->where($this->positionColumn(), '<', $old_position)->whereRaw($avoid_id_condition)->increment($this->positionColumn());
}
}