Skip to content

Commit

Permalink
'bug'
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Feb 22, 2017
1 parent dd5bbba commit 97d359b
Show file tree
Hide file tree
Showing 11 changed files with 785 additions and 821 deletions.
16 changes: 8 additions & 8 deletions backend/modules/rbac/controllers/MenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function actionIndex()
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
'model' => $this->findModel($id),
]);
}

Expand All @@ -88,12 +88,13 @@ public function actionView($id)
public function actionCreate($id = null)
{
$model = new Menu();
$model->parent_name = $id ? Menu::findOne($id)->name : null;
$model->parent = $id ? : null;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', '操作成功');
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
'model' => $model,
]);
}
}
Expand All @@ -109,14 +110,12 @@ public function actionCreate($id = null)
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->menuParent) {
$model->parent_name = $model->menuParent->name;
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', '操作成功');
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
'model' => $model,
]);
}
}
Expand All @@ -131,11 +130,12 @@ public function actionUpdate($id)
*/
public function actionDelete($id)
{
if (Menu::find()->where(['parent' => $id])->all() > 0) {
if (Menu::find()->where(['parent' => $id])->count() > 0) {
Yii::$app->session->setFlash('error', '请先删除该菜单下的所有子菜单');
return $this->redirect(['index']);
}
$this->findModel($id)->delete();
Yii::$app->session->setFlash('success', '操作成功');
return $this->redirect(['index']);
}

Expand Down
42 changes: 14 additions & 28 deletions backend/modules/rbac/models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ public function rules()
return [
[['name'], 'required'],
['name', 'unique'],
[['parent_name'], 'filterParent'],
[['parent_name'], 'in',
'range' => static::find()->select(['name'])->column(),
'message' => 'Menu "{value}" not found.', ],
[['parent', 'route', 'data', 'order'], 'default'],
[['parent'], 'in', 'range' => static::find()->select(['id'])->column(), 'message' => 'Menu "{value}" not found.', ],
[['data', 'parent'], 'default'],
['route', function($attribute){
if (!empty($this->$attribute)) {
$this->addError('route', '一级菜单不能有地址');
Expand All @@ -97,28 +94,6 @@ public function rules()
];
}

/**
* Use to loop detected.
*/
public function filterParent()
{
$value = $this->parent_name;
$parent = self::findOne(['name' => $value]);
if ($parent) {
$id = $this->id;
$parent_id = $parent->id;
while ($parent) {
if ($parent->id == $id) {
$this->addError('parent_name', 'Loop detected.');

return;
}
$parent = $parent->menuParent;
}
$this->parent = $parent_id;
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -128,7 +103,6 @@ public function attributeLabels()
'id' => Yii::t('rbac', 'ID'),
'name' => Yii::t('rbac', 'Name'),
'parent' => Yii::t('rbac', 'Parent'),
'parent_name' => Yii::t('rbac', 'Parent Name'),
'route' => Yii::t('rbac', 'Route'),
'icon' => Yii::t('rbac', 'Icon'),
'order' => Yii::t('rbac', 'Order'),
Expand Down Expand Up @@ -172,4 +146,16 @@ public static function getSavedRoutes()

return $result;
}

public static function getDropDownList($tree = [], &$result = [], $deep = 0, $separator = '    ')
{
$deep++;
foreach($tree as $list) {
$result[$list['id']] = str_repeat($separator, $deep-1) . $list['name'];
if (isset($list['children'])) {
self::getDropDownList($list['children'], $result, $deep);
}
}
return $result;
}
}
37 changes: 16 additions & 21 deletions backend/modules/rbac/views/menu/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,33 @@

?>

<div class="box box-primary">
<div class="box-body">
<?php $form = ActiveForm::begin(); ?>
<div class="box box-primary">
<div class="box-body">
<?php $form = ActiveForm::begin(); ?>

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

<?= $form->field($model, 'parent_name')->textInput(['id' => 'parent_name']) ?>
<?= $form->field($model, 'parent')->dropDownList($model::getDropDownList(\common\helpers\Tree::build($model::find()->asArray()->all(), 'id', 'parent', 'children', null)), ['encode' => false]) ?>

<?= $form->field($model, 'route')->textInput(['id' => 'route']) ?>
<?= $form->field($model, 'route')->textInput(['id' => 'route']) ?>

<?= $form->field($model, 'icon')->widget(\backend\widgets\iconpicker\IconPickerWidget::className()) ?>
<?= $form->field($model, 'icon')->widget(\backend\widgets\iconpicker\IconPickerWidget::className()) ?>

<?= $form->field($model, 'order')->input('number') ?>
<?= $form->field($model, 'order')->input('number') ?>

<?= $form->field($model, 'data')->textarea(['rows' => 4]) ?>
<?= $form->field($model, 'data')->textarea(['rows' => 4]) ?>

<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('rbac', 'Create') : Yii::t('rbac', 'Update'), ['class' => 'btn btn-flat btn-block bg-maroon']) ?>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('rbac', 'Create') : Yii::t('rbac', 'Update'), ['class' => 'btn btn-flat btn-block bg-maroon']) ?>
</div>

<?php ActiveForm::end(); ?>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
<?php
AutocompleteAsset::register($this);

$options1 = Json::htmlEncode([
'source' => Menu::find()->select(['name'])->column(),
]);
$this->registerJs("$('#parent_name').autocomplete($options1);");

$options2 = Json::htmlEncode([
$options = Json::htmlEncode([
'source' => Menu::getSavedRoutes(),
]);
$this->registerJs("$('#route').autocomplete($options2);");
$this->registerJs("$('#route').autocomplete($options);");
2 changes: 1 addition & 1 deletion backend/static/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ td{word-break: break-all}
}

/*选项卡导航*/
.Hui-tabNav, .Hui-tabNav .acrossTab li, .Hui-tabNav .acrossTab li em {
.tab-nav, .tab-nav .acrossTab li, .tab-nav .acrossTab li em {
background-image: url(../images/acrossTab-2.png);
}
.acrossTab li {
Expand Down
Loading

0 comments on commit 97d359b

Please sign in to comment.