From 28f1a1e836653701a2271dc7fe00132d81ff5717 Mon Sep 17 00:00:00 2001 From: Timofeev Ivan Date: Sat, 26 Nov 2016 16:49:21 +0700 Subject: [PATCH] tree and filter attribute --- src/ChildrenAction.php | 32 ++++++++++++++++++++++++-------- src/NestedSetsGrid.php | 9 ++++++--- src/NodeMoveAction.php | 5 +++++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/ChildrenAction.php b/src/ChildrenAction.php index 4fe4b86..fe0bd8f 100644 --- a/src/ChildrenAction.php +++ b/src/ChildrenAction.php @@ -9,20 +9,36 @@ class ChildrenAction extends Action { public $modelName; + public $filterAttribute; - public function run($id = 1) + public function run($id = null) { Yii::$app->response->format = Response::FORMAT_JSON; - $models = $this->modelName::findOne($id)->children(1)->all(); + if ($this->modelName === null) { + throw new \yii\base\InvalidConfigException("No 'modelName' supplied on action initialization."); + } $children = []; - foreach ($models as $key => $model) { - $children[] = [ - 'key' => $model->id, - 'title' => $model->name, - 'lazy' => !$model->isLeaf(), - ]; + + $query = $this->modelName::find(); + + $query->where($id ? ['id' => $id] : ['depth' => 0]); + + if ($this->filterAttribute) { + $query->andWhere([$this->filterAttribute => Yii::$app->request->get($this->filterAttribute) ? Yii::$app->request->get($this->filterAttribute) : null]); + } + + if (($parent = $query->one()) !== null) { + $models = $parent->children(1)->all(); + + foreach ($models as $key => $model) { + $children[] = [ + 'key' => $model->id, + 'title' => $model->name, + 'lazy' => !$model->isLeaf(), + ]; + } } return $children; diff --git a/src/NestedSetsGrid.php b/src/NestedSetsGrid.php index c4b736a..cf57db3 100644 --- a/src/NestedSetsGrid.php +++ b/src/NestedSetsGrid.php @@ -13,12 +13,15 @@ class NestedSetsGrid extends \yii\base\Widget { public $model; + public $filterAttribute; + public $actions = []; + public $options = [ 'class' => 'table table-condensed table-hover table-striped fancytree-fade-expander', ]; - public $actions = []; - public $pluginEvents = []; + public $pluginOptions = []; + public $pluginEvents = []; private $_defaultPluginOptions = [ 'extensions' => ['dnd', 'edit', 'glyph', 'table'], @@ -55,7 +58,7 @@ public function init() } if (!isset($this->actions['children'])) { - $this->actions['children'] = Url::to(['children']); + $this->actions['children'] = Url::to(['children', $this->filterAttribute => Yii::$app->request->get($this->filterAttribute)]); } if (!isset($this->actions['nodeMove'])) { diff --git a/src/NodeMoveAction.php b/src/NodeMoveAction.php index 0590075..3dcb42f 100644 --- a/src/NodeMoveAction.php +++ b/src/NodeMoveAction.php @@ -9,6 +9,7 @@ class NodeMoveAction extends Action { public $modelName; + public $treeAttribute; public function run($id, $target, $action) { @@ -21,6 +22,10 @@ public function run($id, $target, $action) $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)];