|
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
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
public $redirectUrl = ['index']; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Sort action |
|
37
|
|
|
* @param int $id model identifier |
|
38
|
|
|
* @param int|string $position new sort position |
|
39
|
|
|
* May be set as: |
|
40
|
|
|
* top - move model to top of sorting |
|
41
|
|
|
* bottom - move model to bottom of sorting |
|
42
|
|
|
* u<number> - up to <number> positions |
|
43
|
|
|
* d<number> - down to <number> positions |
|
44
|
|
|
* -<number> - down to <number> positions |
|
45
|
|
|
* <number> - move tto specific position |
|
46
|
|
|
* @return array|Response |
|
47
|
|
|
* @throws HttpException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function run($id, $position) |
|
50
|
|
|
{ |
|
51
|
|
|
if (empty($this->modelClass) || !class_exists($this->modelClass)) { |
|
52
|
|
|
throw new InvalidParamException('Define model class name for action ' . $this->controller->id . '::' . $this->id); |
|
53
|
|
|
} |
|
54
|
|
|
/** @var ActiveRecord $class */ |
|
55
|
|
|
$class = $this->modelClass; |
|
56
|
|
|
/** @var SortableBehavior $model */ |
|
57
|
|
|
$model = $class::findOne($id); |
|
58
|
|
|
if (!$model) { |
|
59
|
|
|
throw new HttpException(404, 'Model `' . $this->modelClass . '` to change sort not found'); |
|
60
|
|
|
} |
|
61
|
|
|
switch ($position) { |
|
62
|
|
|
case 'top': |
|
63
|
|
|
// move model to top |
|
64
|
|
|
$model->moveToTop(); |
|
65
|
|
|
break; |
|
66
|
|
|
case 'bottom': |
|
67
|
|
|
// move model to bottom |
|
68
|
|
|
$model->moveToBottom(); |
|
69
|
|
|
break; |
|
70
|
|
|
default: |
|
71
|
|
|
if (strpos('ud-', $position[0]) !== false) { |
|
72
|
|
|
// relative position |
|
73
|
|
|
$position = ($position[0] == 'd' ? '-' : '') . substr($position, 1); |
|
74
|
|
|
if (is_numeric($position)) { |
|
75
|
|
|
$model->sortChange($position); |
|
76
|
|
|
} |
|
77
|
|
|
} elseif (is_numeric($position)) { |
|
78
|
|
|
// absolute position |
|
79
|
|
|
$model->moveToPosition($position); |
|
80
|
|
|
} |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
return Yii::$app->response->format == Response::FORMAT_JSON ? |
|
84
|
|
|
['status' => 200] : |
|
85
|
|
|
$this->controller->redirect($this->redirectUrl); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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: