public function relationGrid($name, $relation, $showAllRecords = false)
{
$model = new $relation->modelClass();
// column counter
$counter = 0;
$columns = '';
if (!$this->generator->isPivotRelation($relation)) {
// hasMany relations
$template = '{view} {update}';
$deleteButtonPivot = '';
} else {
// manyMany relations
$template = '{view} {delete}';
$deleteButtonPivot = <<<EOS
'delete' => function (\$url, \$model) {
return Html::a('<span class="glyphicon glyphicon-remove"></span>', \$url, [
'class' => 'text-danger',
'title' => {$this->generator->generateString('Remove')},
'data-confirm' => {$this->generator->generateString('Are you sure you want to delete the related item?')},
'data-method' => 'post',
'data-pjax' => '0',
]);
},
'view' => function (\$url, \$model) {
return Html::a(
'<span class="glyphicon glyphicon-cog"></span>',
\$url,
[
'data-title' => {$this->generator->generateString('View Pivot Record')},
'data-toggle' => 'tooltip',
'data-pjax' => '0',
'class' => 'text-muted',
]
);
},
EOS;
}
$reflection = new \ReflectionClass($relation->modelClass);
$controller = $this->generator->pathPrefix . Inflector::camel2id($reflection->getShortName(), '-', true);
$relKey = key($relation->link);
$actionColumn = <<<EOS
[
'class' => '{$this->generator->actionButtonClass}',
'template' => '{$template}',
'contentOptions' => ['nowrap'=>'nowrap'],
'urlCreator' => function (\$action, \$model, \$key, \$index) {
// using the column name as key, not mapping to 'id' like the standard generator
\$params = is_array(\$key) ? \$key : [\$model->primaryKey()[0] => (string) \$key];
\$params[0] = '{$controller}' . '/' . \$action;
\$params['{$model->formName()}'] = ['{$relKey}' => \$model->primaryKey()[0]];
return \$params;
},
'buttons' => [
{$deleteButtonPivot}
],
'controller' => '{$controller}'
]
EOS;
// add action column
$columns .= $actionColumn . ",\n";
// prepare grid column formatters
$model->setScenario('crud');
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
$safeAttributes = $model->getTableSchema()->columnNames;
}
foreach ($safeAttributes as $attr) {
// max seven columns
if ($counter > $this->generator->gridRelationMaxColumns) {
continue;
}
// skip virtual attributes
if ($this->skipVirtualAttributes && !isset($model->tableSchema->columns[$attr])) {
continue;
}
// don't show current model
if (key($relation->link) == $attr) {
continue;
}
$code = $this->generator->columnFormat($attr, $model);
if ($code == false) {
continue;
}
$columns .= $code . ",\n";
++$counter;
}
$query = $showAllRecords ? "'query' => \\{$relation->modelClass}::find()" : "'query' => \$model->get{$name}()";
$pageParam = Inflector::slug("page-{$name}");
$firstPageLabel = $this->generator->generateString('First');
$lastPageLabel = $this->generator->generateString('Last');
$code = "'<div class=\"table-responsive\">'\n . ";
$code .= <<<EOS
\\yii\\grid\\GridView::widget([
'layout' => '{summary}{pager}<br/>{items}{pager}',
'dataProvider' => new \\yii\\data\\ActiveDataProvider([
{$query},
'pagination' => [
'pageSize' => 20,
'pageParam'=>'{$pageParam}',
]
]),
'pager' => [
'class' => yii\\widgets\\LinkPager::className(),
'firstPageLabel' => {$firstPageLabel},
'lastPageLabel' => {$lastPageLabel}
],
'columns' => [
{$columns}]
])
EOS;
$code .= "\n . '</div>' ";
return $code;
}