Skip to content

Commit 2fc9aea

Browse files
add hierarchical categories
1 parent 039ba2b commit 2fc9aea

File tree

15 files changed

+142
-1296
lines changed

15 files changed

+142
-1296
lines changed

Diff for: CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
4+
## Unreleased
5+
### Added
6+
- Hierarchical category tree: made change to FancytreeWidget, Fancytree.js. [See more](https://github.com/mar10/fancytree/pull/342)

Diff for: backend/controllers/CategoryController.php

+33-15
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace backend\controllers;
44

5-
use Yii;
6-
use common\models\Category;
75
use backend\models\CategorySearch;
6+
use common\models\Category;
7+
use Yii;
8+
use yii\filters\VerbFilter;
89
use yii\web\Controller;
910
use yii\web\NotFoundHttpException;
10-
use yii\filters\VerbFilter;
1111

1212
/**
1313
* CategoryController implements the CRUD actions for Category model.
@@ -62,13 +62,22 @@ public function actionCreate()
6262
{
6363
$model = new Category();
6464

65-
if ($model->load(Yii::$app->request->post()) && $model->save()) {
66-
return $this->redirect(['view', 'id' => $model->id]);
67-
} else {
68-
return $this->render('create', [
69-
'model' => $model,
70-
]);
65+
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
66+
if (!isset($model->parentId)) {
67+
$result = $model->saveNode();
68+
} else {
69+
$parent = Category::findOne($model->parentId);
70+
$result = $model->appendTo($parent);
71+
}
72+
73+
if ($result) {
74+
return $this->redirect(['view', 'id' => $model->id]);
75+
}
7176
}
77+
78+
return $this->render('create', [
79+
'model' => $model,
80+
]);
7281
}
7382

7483
/**
@@ -81,13 +90,22 @@ public function actionUpdate($id)
8190
{
8291
$model = $this->findModel($id);
8392

84-
if ($model->load(Yii::$app->request->post()) && $model->save()) {
85-
return $this->redirect(['view', 'id' => $model->id]);
86-
} else {
87-
return $this->render('update', [
88-
'model' => $model,
89-
]);
93+
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
94+
if (!isset($model->parentId)) {
95+
$result = $model->saveNode();
96+
} else {
97+
$parent = Category::findOne($model->parentId);
98+
$result = $model->appendTo($parent);
99+
}
100+
101+
if ($result) {
102+
return $this->redirect(['view', 'id' => $model->id]);
103+
}
90104
}
105+
106+
return $this->render('update', [
107+
'model' => $model,
108+
]);
91109
}
92110

93111
/**

Diff for: backend/views/category/_form.php

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<?php
22

3-
use common\models\Category;
4-
use kartik\widgets\Select2;
3+
use wbraganca\fancytree\FancytreeWidget;
54
use yii\helpers\Html;
65
use yii\widgets\ActiveForm;
76

87
/* @var $this yii\web\View */
9-
/* @var $model common\models\Category */
108
/* @var $form yii\widgets\ActiveForm */
11-
12-
$query = Category::find();
13-
if (!$model->isNewRecord)
14-
$query->where(['!=', 'id', $model->id]);
15-
$categories = \yii\helpers\ArrayHelper::map($query->all(), 'id', 'name')
9+
/* @var $model \common\models\Category */
10+
$query = \common\models\Category::find();
11+
$data = $query->dataFancytree();
1612
?>
1713

1814
<div class="category-form">
@@ -21,16 +17,15 @@
2117

2218
<?= $form->field($model, 'name')->textInput(['maxlength' => 255]) ?>
2319

24-
<?= $form->field($model, 'parent_id')->widget(Select2::className(), [
25-
'data' => $categories,
26-
'options' => ['placeholder' => 'Select a parent category ...'],
27-
'pluginOptions' => [
28-
'allowClear' => true,
29-
],
20+
<?= $form->field($model, 'parentId')->widget(FancytreeWidget::className(), [
21+
'options' => [
22+
'source' => $data,
23+
]
3024
]) ?>
3125

3226
<div class="form-group">
33-
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
27+
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update',
28+
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
3429
</div>
3530

3631
<?php ActiveForm::end(); ?>

Diff for: common/components/Catalog.php

+1-21
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,7 @@ class Catalog extends Component
1313
*/
1414
public function getCategories()
1515
{
16-
$categories = Category::find()->orderBy(['parent_id' => SORT_ASC, 'id' => SORT_ASC])->all();
17-
$tree = $this->buildTree($categories, null);
18-
19-
return $tree;
20-
}
21-
22-
protected function buildTree(array $elements, $parentId = 0)
23-
{
24-
$branch = [];
25-
26-
foreach ($elements as $element) {
27-
if ($element['parent_id'] == $parentId) {
28-
$children = $this->buildTree($elements, $element['id']);
29-
if ($children) {
30-
$element['children'] = $children;
31-
}
32-
$branch[] = $element;
33-
}
34-
}
35-
36-
return $branch;
16+
return Category::find()->dataFancytree();
3717
}
3818

3919
}

Diff for: common/components/CategoryQuery.php

-20
This file was deleted.

0 commit comments

Comments
 (0)