| @@ 53-72 (lines=20) @@ | ||
| 50 | * @return mixed |
|
| 51 | * @throws \yii\web\NotFoundHttpException |
|
| 52 | */ |
|
| 53 | protected function moveDown($id) |
|
| 54 | { |
|
| 55 | $model = $this->getModel($id); |
|
| 56 | $orderAttrName = $this->positionAttribute; |
|
| 57 | $orderDir = SORT_ASC; |
|
| 58 | ||
| 59 | $swapModel = $model::find() |
|
| 60 | ->where(['>', $orderAttrName, $model->$orderAttrName]) |
|
| 61 | ->orderBy([$orderAttrName => $orderDir]) |
|
| 62 | ->one(); |
|
| 63 | ||
| 64 | if ($swapModel) { |
|
| 65 | $newOrderNumeric = $swapModel->$orderAttrName; |
|
| 66 | $swapModel->$orderAttrName = $model->$orderAttrName; |
|
| 67 | $model->$orderAttrName = $newOrderNumeric; |
|
| 68 | $swapModel->save() && $model->save(); |
|
| 69 | } |
|
| 70 | ||
| 71 | return $this->redirect($model); |
|
| 72 | } |
|
| 73 | ||
| 74 | /** |
|
| 75 | * Move model to up |
|
| @@ 80-99 (lines=20) @@ | ||
| 77 | * @return mixed |
|
| 78 | * @throws \yii\web\NotFoundHttpException |
|
| 79 | */ |
|
| 80 | protected function moveUp($id) |
|
| 81 | { |
|
| 82 | $model = $this->getModel($id); |
|
| 83 | $orderAttrName = $this->positionAttribute; |
|
| 84 | $orderDir = SORT_DESC; |
|
| 85 | ||
| 86 | $swapModel = $model::find() |
|
| 87 | ->where(['<', $orderAttrName, $model->$orderAttrName]) |
|
| 88 | ->orderBy([$orderAttrName => $orderDir]) |
|
| 89 | ->one(); |
|
| 90 | ||
| 91 | if ($swapModel) { |
|
| 92 | $newOrderNumeric = $swapModel->$orderAttrName; |
|
| 93 | $swapModel->$orderAttrName = $model->$orderAttrName; |
|
| 94 | $model->$orderAttrName = $newOrderNumeric; |
|
| 95 | $swapModel->save() && $model->save(); |
|
| 96 | } |
|
| 97 | ||
| 98 | return $this->redirect($model); |
|
| 99 | } |
|
| 100 | } |
|
| 101 | ||