Skip to content

Commit 9b420a2

Browse files
committed
Made saving state optional
1 parent 0e0589e commit 9b420a2

File tree

4 files changed

+81
-68
lines changed

4 files changed

+81
-68
lines changed

src/behaviors/NestedSetsManagementBehavior.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace arogachev\tree\behaviors;
44

5+
use arogachev\tree\helpers\ArrayHelper;
56
use yii\base\Behavior;
67

78
class NestedSetsManagementBehavior extends Behavior
@@ -10,4 +11,64 @@ class NestedSetsManagementBehavior extends Behavior
1011
* @var string
1112
*/
1213
public $isOpenedAttribute = 'is_opened';
14+
15+
/**
16+
* @var boolean
17+
*/
18+
public $saveState = false;
19+
20+
21+
/**
22+
* @return array
23+
*/
24+
public function getHierarchicalArray()
25+
{
26+
/* @var $modelClass \yii\db\ActiveRecord */
27+
$modelClass = $this->owner->className();
28+
/* @var $baseModel \creocoder\nestedsets\NestedSetsBehavior */
29+
$baseModel = new $this->owner;
30+
$models = $modelClass::find()
31+
->orderBy($baseModel->leftAttribute)
32+
->all();
33+
34+
$depth = 0;
35+
$c = 0;
36+
$result = [];
37+
$path = [];
38+
39+
foreach ($models as $model) {
40+
$depthValue = $model->{$baseModel->depthAttribute};
41+
42+
if ($depthValue > $depth) {
43+
$c = 0;
44+
} elseif ($depthValue < $depth) {
45+
for ($i = $depth - $depthValue; $i; $i--) {
46+
unset($path[count($path) - 1]);
47+
}
48+
49+
$c = $path[count($path) - 1] + 1;
50+
}
51+
52+
$path[$depthValue] = $c;
53+
54+
$value = [
55+
'id' => $model->primaryKey,
56+
'text' => $model->name,
57+
];
58+
59+
if ($this->saveState) {
60+
$value['state'] = [
61+
'opened' => $model->{$model->isOpenedAttribute},
62+
];
63+
}
64+
65+
ArrayHelper::saveByPath($result, $path, $value);
66+
67+
$depth = $depthValue;
68+
69+
$c++;
70+
}
71+
72+
return $result;
73+
}
1374
}

src/controllers/TreeController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace arogachev\tree\controllers;
44

5-
use arogachev\tree\helpers\NestedSetsHelper;
5+
use arogachev\tree\behaviors\NestedSetsManagementBehavior;
6+
use creocoder\nestedsets\NestedSetsBehavior;
67
use Yii;
78
use yii\db\ActiveRecord;
89
use yii\filters\ContentNegotiator;
@@ -50,7 +51,7 @@ public function beforeAction($action)
5051
*/
5152
public function actionGetTree()
5253
{
53-
return NestedSetsHelper::getHierarchicalArray(Yii::$app->request->get('modelClass'));
54+
return $this->getModel(null, true)->getHierarchicalArray();
5455
}
5556

5657
/**
@@ -149,7 +150,7 @@ public function actionInsertAfter()
149150
/**
150151
* @param null|string $paramName
151152
* @param boolean $createIfNotFound
152-
* @return \yii\db\ActiveRecord|\creocoder\nestedsets\NestedSetsBehavior
153+
* @return \yii\db\ActiveRecord|NestedSetsBehavior|NestedSetsManagementBehavior
153154
* @throws BadRequestHttpException
154155
* @throws NotFoundHttpException
155156
*/

src/helpers/NestedSetsHelper.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/widgets/NestedSets.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ public function init()
4545
$this->options['id'] = $this->getId();
4646
}
4747

48+
$clientEvents = [
49+
'create_node' => 'yii.tree.createNode',
50+
'move_node' => 'yii.tree.moveNode',
51+
'rename_node' => 'yii.tree.renameNode',
52+
'delete_node' => 'yii.tree.deleteNode',
53+
];
54+
55+
$model = new $this->modelClass;
56+
if ($model->saveState) {
57+
$clientEvents = array_merge($clientEvents, [
58+
'open_node' => 'yii.tree.openNode',
59+
'close_node' => 'yii.tree.closeNode',
60+
]);
61+
}
62+
4863
$this->jsTreeOptions = ArrayHelper::merge([
4964
'clientOptions' => [
5065
'core' => [
@@ -65,14 +80,7 @@ public function init()
6580
],
6681
],
6782
],
68-
'clientEvents' => [
69-
'open_node' => 'yii.tree.openNode',
70-
'close_node' => 'yii.tree.closeNode',
71-
'create_node' => 'yii.tree.createNode',
72-
'move_node' => 'yii.tree.moveNode',
73-
'rename_node' => 'yii.tree.renameNode',
74-
'delete_node' => 'yii.tree.deleteNode',
75-
],
83+
'clientEvents' => $clientEvents,
7684
], $this->jsTreeOptions);
7785
}
7886

0 commit comments

Comments
 (0)