Skip to content

Commit 99f828c

Browse files
committed
Merge pull request #210 from softark/add-extra-fields
Small enhancements on the Assignment pages
2 parents 4599e4f + 4f58dc9 commit 99f828c

File tree

4 files changed

+85
-20
lines changed

4 files changed

+85
-20
lines changed

README.md

+54-9
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ If you use database (class 'yii\rbac\DbManager') to save rbac data, execute the
115115
yii migrate --migrationPath=@yii/rbac/migrations
116116
```
117117

118-
Customizing Controller
119-
----------------------
118+
Customizing Assignment Controller
119+
---------------------------------
120120

121-
Some controller properties may need to be adjusted to the User model of your app.
121+
Assignment controller properties may need to be adjusted to the User model of your app.
122122
To do that, change them via `controllerMap` property. For example:
123123

124124
```php
@@ -128,12 +128,35 @@ To do that, change them via `controllerMap` property. For example:
128128
'controllerMap' => [
129129
'assignment' => [
130130
'class' => 'mdm\admin\controllers\AssignmentController',
131-
/* 'userClassName' => 'app\models\User', */ // fully qualified class name of your User model
132-
// Usually you don't need to specify it explicitly, since the module will detect it automatically
133-
'idField' => 'user_id', // id field of your User model that corresponds to Yii::$app->user->id
134-
'usernameField' => 'username', // username field of your User model
135-
'searchClass' => 'app\models\UserSearch' // fully qualified class name of your User model for searching
136-
]
131+
/* 'userClassName' => 'app\models\User', */
132+
'idField' => 'user_id',
133+
'usernameField' => 'username',
134+
'fullnameField' => 'profile.full_name',
135+
'extraColumns' => [
136+
[
137+
'attribute' => 'full_name',
138+
'label' => 'Full Name',
139+
'value' => function($model, $key, $index, $column) {
140+
return $model->profile->full_name;
141+
},
142+
],
143+
[
144+
'attribute' => 'dept_name',
145+
'label' => 'Department',
146+
'value' => function($model, $key, $index, $column) {
147+
return $model->profile->dept->name;
148+
},
149+
],
150+
[
151+
'attribute' => 'post_name',
152+
'label' => 'Post',
153+
'value' => function($model, $key, $index, $column) {
154+
return $model->profile->post->name;
155+
},
156+
],
157+
],
158+
'searchClass' => 'app\models\UserSearch'
159+
],
137160
],
138161
...
139162
]
@@ -142,6 +165,28 @@ To do that, change them via `controllerMap` property. For example:
142165

143166
```
144167

168+
- Required properties
169+
- **userClassName** Fully qualified class name of your User model
170+
Usually you don't need to specify it explicitly, since the module will detect it automatically
171+
- **idField** ID field of your User model
172+
The field that corresponds to Yii::$app->user->id.
173+
The default value is 'id'.
174+
- **usernameField** User name field of your User model
175+
The default value is 'username'.
176+
- Optional properties
177+
- **fullnameField** The field that specifies the full name of the user used in "view" page.
178+
This can either be a field of the user model or of a related model (e.g. profile model).
179+
When the field is of a related model, the name should be specified with a dot-separated notation (e.g. 'profile.full_name').
180+
The default value is null.
181+
- **extraColumns** The definition of the extra columns used in the "index" page
182+
This should be an array of the definitions of the grid view columns.
183+
You may include the attributes of the related models as you see in the example above.
184+
The default value is an empty array.
185+
- **searchClass** Fully qualified class name of your model for searching the user model
186+
You have to supply the proper search model in order to enable the filtering and the sorting by the extra columns.
187+
The default value is null.
188+
189+
145190
Customizing Layout
146191
------------------
147192

controllers/AssignmentController.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class AssignmentController extends Controller
2424
public $userClassName;
2525
public $idField = 'id';
2626
public $usernameField = 'username';
27+
public $fullnameField;
2728
public $searchClass;
29+
public $extraColumns = [];
2830

2931
/**
3032
* @inheritdoc
@@ -62,18 +64,19 @@ public function actionIndex()
6264

6365
if ($this->searchClass === null) {
6466
$searchModel = new AssignmentSearch;
67+
$dataProvider = $searchModel->search(\Yii::$app->request->getQueryParams(), $this->userClassName, $this->usernameField);
6568
} else {
6669
$class = $this->searchClass;
6770
$searchModel = new $class;
71+
$dataProvider = $searchModel->search(\Yii::$app->request->getQueryParams());
6872
}
6973

70-
$dataProvider = $searchModel->search(\Yii::$app->request->getQueryParams(), $this->userClassName, $this->usernameField);
71-
7274
return $this->render('index', [
7375
'dataProvider' => $dataProvider,
7476
'searchModel' => $searchModel,
7577
'idField' => $this->idField,
7678
'usernameField' => $this->usernameField,
79+
'extraColumns' => $this->extraColumns,
7780
]);
7881
}
7982

@@ -90,6 +93,7 @@ public function actionView($id)
9093
'model' => $model,
9194
'idField' => $this->idField,
9295
'usernameField' => $this->usernameField,
96+
'fullnameField' => $this->fullnameField,
9397
]);
9498
}
9599

views/assignment/index.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
/* @var $this yii\web\View */
88
/* @var $dataProvider yii\data\ActiveDataProvider */
99
/* @var $searchModel mdm\admin\models\searchs\Assignment */
10+
/* @var $usernameField string */
11+
/* @var $extraColumns string[] */
1012

1113
$this->title = Yii::t('rbac-admin', 'Assignments');
1214
$this->params['breadcrumbs'][] = $this->title;
@@ -19,20 +21,26 @@
1921
Pjax::begin([
2022
'enablePushState'=>false,
2123
]);
22-
echo GridView::widget([
23-
'dataProvider' => $dataProvider,
24-
'filterModel' => $searchModel,
25-
'columns' => [
24+
$columns = array_merge(
25+
[
2626
['class' => 'yii\grid\SerialColumn'],
2727
[
2828
'class' => 'yii\grid\DataColumn',
2929
'attribute' => $usernameField,
3030
],
31+
],
32+
$extraColumns,
33+
[
3134
[
3235
'class' => 'yii\grid\ActionColumn',
3336
'template'=>'{view}'
3437
],
35-
],
38+
]
39+
);
40+
echo GridView::widget([
41+
'dataProvider' => $dataProvider,
42+
'filterModel' => $searchModel,
43+
'columns' => $columns,
3644
]);
3745
Pjax::end();
3846
?>

views/assignment/view.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
<?php
22

33
use yii\helpers\Html;
4+
use yii\helpers\ArrayHelper;
45
use mdm\admin\AdminAsset;
56
use yii\helpers\Json;
67
use yii\helpers\Url;
78

89
/* @var $this yii\web\View */
910
/* @var $model yii\web\IdentityInterface */
11+
/* @var $fullnameField string */
1012

11-
$this->title = Yii::t('rbac-admin', 'Assignments');
12-
$this->params['breadcrumbs'][] = $this->title;
13+
$userName = $model->{$usernameField};
14+
if (!empty($fullnameField)) {
15+
$userName .= ' (' . ArrayHelper::getValue($model, $fullnameField) . ')';
16+
}
17+
$userName = Html::encode($userName);
18+
19+
$this->title = Yii::t('rbac-admin', 'Assignments') . ' : ' . $userName;;
20+
$this->params['breadcrumbs'][] = ['label' => Yii::t('rbac-admin', 'Assignments'), 'url' => ['index']];
21+
$this->params['breadcrumbs'][] = $userName;
1322
?>
1423
<div class="assignment-index">
15-
<?= Html::a(Yii::t('rbac-admin', 'Users'), ['index'], ['class' => 'btn btn-success']) ?>
16-
<h1><?= Yii::t('rbac-admin', 'User') ?>: <?= Html::encode($model->{$usernameField}) ?></h1>
24+
<h1><?= $this->title ?></h1>
1725

1826
<div class="row">
1927
<div class="col-lg-5">

0 commit comments

Comments
 (0)