1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Controller's action to sort change in inblank\yii2-sortable behavior |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/inblank/yii2-sortable |
6
|
|
|
* @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
namespace inblank\sortable; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Action; |
13
|
|
|
use yii\base\InvalidParamException; |
14
|
|
|
use yii\db\ActiveRecord; |
15
|
|
|
use yii\web\HttpException; |
16
|
|
|
use yii\web\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SortAction |
20
|
|
|
*/ |
21
|
|
|
class SortAction extends Action |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Sorting model class name |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $modelClass; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Redirect URL if request not by ajax |
31
|
|
|
* Callable function signature: function($model){}, where $model is the current sorted model |
32
|
|
|
* @var array|callable |
33
|
|
|
*/ |
34
|
|
|
public $redirectUrl = ['index']; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sort action |
38
|
|
|
* @param int $id model identifier |
39
|
|
|
* @param int|string $position new sort position |
40
|
|
|
* May be set as: |
41
|
|
|
* top - move model to top of sorting |
42
|
|
|
* bottom - move model to bottom of sorting |
43
|
|
|
* u<number> - up to <number> positions |
44
|
|
|
* d<number> - down to <number> positions |
45
|
|
|
* -<number> - down to <number> positions |
46
|
|
|
* <number> - move tto specific position |
47
|
|
|
* @return array|Response |
48
|
|
|
* @throws HttpException |
49
|
|
|
*/ |
50
|
|
|
public function run($id, $position) |
51
|
|
|
{ |
52
|
|
|
if (Yii::$app->request->isAjax) { |
53
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
54
|
|
|
} |
55
|
|
|
if (empty($this->modelClass) || !class_exists($this->modelClass)) { |
56
|
|
|
throw new InvalidParamException('Define model class name for action ' . $this->controller->id . '::' . $this->id); |
57
|
|
|
} |
58
|
|
|
/** @var ActiveRecord $class */ |
59
|
|
|
$class = $this->modelClass; |
60
|
|
|
/** @var SortableBehavior|ActiveRecord $model */ |
61
|
|
|
$model = $class::findOne($id); |
62
|
|
|
if (!$model) { |
63
|
|
|
throw new HttpException(404, 'Model `' . $this->modelClass . '` to change sort not found'); |
64
|
|
|
} |
65
|
|
|
switch ($position) { |
66
|
|
|
case 'top': |
67
|
|
|
// move model to top |
68
|
|
|
$model->moveToTop(); |
|
|
|
|
69
|
|
|
break; |
70
|
|
|
case 'bottom': |
71
|
|
|
// move model to bottom |
72
|
|
|
$model->moveToBottom(); |
|
|
|
|
73
|
|
|
break; |
74
|
|
|
default: |
75
|
|
|
if (strpos('ud-', $position[0]) !== false) { |
76
|
|
|
// relative position |
77
|
|
|
$position = ($position[0] == 'u' ? '' : '-') . substr($position, 1); |
78
|
|
|
if (is_numeric($position)) { |
79
|
|
|
$model->sortChange($position); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} elseif (is_numeric($position)) { |
82
|
|
|
// absolute position |
83
|
|
|
$model->moveToPosition($position); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
$sortField = $model->sortAttribute; |
88
|
|
|
return Yii::$app->response->format == Response::FORMAT_JSON ? |
89
|
|
|
[ |
90
|
|
|
'status' => 200, |
91
|
|
|
'id' => $model->id, |
92
|
|
|
$sortField => $model->$sortField |
93
|
|
|
] : $this->controller->redirect( |
|
|
|
|
94
|
|
|
is_callable($this->redirectUrl) |
95
|
|
|
? call_user_func($this->redirectUrl, $model) : $this->redirectUrl |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: