-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathSortableGridAction.php
55 lines (50 loc) · 1.26 KB
/
SortableGridAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* @link https://github.com/himiklab/yii2-sortable-grid-view-widget
* @copyright Copyright (c) 2014-2017 HimikLab
* @license http://opensource.org/licenses/MIT MIT
*/
namespace himiklab\sortablegrid;
use Yii;
use yii\base\Action;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\web\BadRequestHttpException;
/**
* Action for sortable Yii2 GridView widget.
*
* For example:
*
* ```php
* public function actions()
* {
* return [
* 'sort' => [
* 'class' => SortableGridAction::className(),
* 'modelName' => Model::className(),
* ],
* ];
* }
* ```
*
* @author HimikLab
* @package himiklab\sortablegrid
*/
class SortableGridAction extends Action
{
public $modelName;
public function run()
{
if (!$items = Yii::$app->request->post('items')) {
throw new BadRequestHttpException('Don\'t received POST param `items`.');
}
/** @var \yii\db\ActiveRecord $model */
$model = new $this->modelName;
if (!$model->hasMethod('gridSort')) {
throw new InvalidConfigException(
"Not found right `SortableGridBehavior` behavior in `{$this->modelName}`."
);
}
$model->gridSort(Json::decode($items));
}
}