-
Notifications
You must be signed in to change notification settings - Fork 1
/
NodeMoveAction.php
46 lines (34 loc) · 1.12 KB
/
NodeMoveAction.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
<?php
namespace nighthtr\nestedsets;
use Yii;
use yii\base\Action;
use yii\web\Response;
class NodeMoveAction extends Action
{
public $modelName;
public $treeAttribute;
public function run($id, $target, $action)
{
Yii::$app->response->format = Response::FORMAT_JSON;
if ($this->modelName === null) {
throw new \yii\base\InvalidConfigException("No 'modelName' supplied on action initialization.");
}
$model = $this->modelName::findOne($id);
$target = $this->modelName::findOne($target);
if ($this->treeAttribute && ($model->$this->treeAttribute != $target->$this->treeAttribute)) {
return ['status' => false];
}
switch ($action) {
case 'over':
return ['status' => $model->appendTo($target)];
break;
case 'before':
return ['status' => $model->insertBefore($target)];
break;
case 'after':
return ['status' => $model->insertAfter($target)];
break;
}
return ['status' => false];
}
}