Skip to content

Commit

Permalink
render searchfile only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
eluhr committed Mar 24, 2023
1 parent 059de44 commit 9cdfdeb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
21 changes: 13 additions & 8 deletions src/generators/crud/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,20 @@ public function getControllerID()
public function getModuleId()
{
if (!$this->moduleNs) {
$controllerNs = \yii\helpers\StringHelper::dirname(ltrim($this->controllerClass, '\\'));
$this->moduleNs = \yii\helpers\StringHelper::dirname(ltrim($controllerNs, '\\'));
$controllerNs = StringHelper::dirname(ltrim($this->controllerClass, '\\'));
$this->moduleNs = StringHelper::dirname(ltrim($controllerNs, '\\'));
}

return \yii\helpers\StringHelper::basename($this->moduleNs);
return StringHelper::basename($this->moduleNs);
}

public function generate()
{
$accessDefinitions = require $this->getTemplatePath().'/access_definition.php';

$this->controllerNs = \yii\helpers\StringHelper::dirname(ltrim($this->controllerClass, '\\'));
$this->moduleNs = \yii\helpers\StringHelper::dirname(ltrim($this->controllerNs, '\\'));
$controllerName = substr(\yii\helpers\StringHelper::basename($this->controllerClass), 0, -10);
$this->controllerNs = StringHelper::dirname(ltrim($this->controllerClass, '\\'));
$this->moduleNs = StringHelper::dirname(ltrim($this->controllerNs, '\\'));
$controllerName = substr(StringHelper::basename($this->controllerClass), 0, -10);

if ($this->singularEntities) {
$this->modelClass = Inflector::singularize($this->modelClass);
Expand All @@ -352,7 +352,7 @@ public function generate()
}

$files[] = new CodeFile($baseControllerFile, $this->render('controller.php', ['accessDefinitions' => $accessDefinitions]));
$params['controllerClassName'] = \yii\helpers\StringHelper::basename($this->controllerClass);
$params['controllerClassName'] = StringHelper::basename($this->controllerClass);

if ($this->overwriteControllerClass || !is_file($controllerFile)) {
$files[] = new CodeFile($controllerFile, $this->render('controller-extended.php', $params));
Expand Down Expand Up @@ -389,7 +389,7 @@ public function generate()
}

foreach (scandir($templatePath) as $file) {
if (empty($this->searchModelClass) && $file === '_search.php') {
if ($file === '_search.php' && !$this->getRenderWithSearch()) {
continue;
}
if (is_file($templatePath.'/'.$file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
Expand Down Expand Up @@ -566,4 +566,9 @@ public function getHasTranslationRelation() {
public function getHasTranslationMetaRelation() {
return isset(\Yii::createObject($this->modelClass)->behaviors()['translation_meta']);
}

public function getRenderWithSearch()
{
return $this->indexWidgetType !== 'grid' && $this->searchModelClass !== '';
}
}
2 changes: 1 addition & 1 deletion src/generators/crud/default/views/_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$count = 0;
foreach ($generator->getTableSchema()->getColumnNames() as $attribute) {
if (++$count < 6) {
echo "\t\t<? ".$generator->generateActiveSearchField($attribute)." ?>\n\n";
echo "\t\t<?php echo ".$generator->generateActiveSearchField($attribute)." ?>\n\n";
} else {
echo "\t\t<?php // echo ".$generator->generateActiveSearchField($attribute)." ?>\n\n";
}
Expand Down
45 changes: 21 additions & 24 deletions src/generators/crud/default/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
$baseName = StringHelper::basename($model::class);
$modelName = Inflector::camel2words($baseName);

$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
/** @var \yii\db\ActiveRecord $model */
$model = new $generator->modelClass();
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
$safeAttributes = $model::getTableSchema()->columnNames;
}
}
//$safeAttributes = $model->safeAttributes();
//if (empty($safeAttributes)) {
// /** @var \yii\db\ActiveRecord $model */
// $model = new $generator->modelClass();
// $safeAttributes = $model->safeAttributes();
// if (empty($safeAttributes)) {
// $safeAttributes = $model::getTableSchema()->columnNames;
// }
//}

echo "<?php\n";
?>
Expand All @@ -56,7 +56,7 @@
?>

/**
* create action column template depending acces rights
* create action column template depending on user's access rights
*/
$actionColumnTemplates = [];

Expand Down Expand Up @@ -88,12 +88,9 @@

<div class="giiant-crud <?= Inflector::camel2id(StringHelper::basename($generator->modelClass), '-', true) ?>-index">

<?=
"<?php\n".($generator->indexWidgetType === 'grid' ? '// ' : '') ?>
<?php if ($generator->searchModelClass !== ''): ?>
echo $this->render('_search', ['model' =>$searchModel]);
<?php endif; ?>
?>
<?php if ($generator->indexWidgetType !== 'grid' && $generator->searchModelClass !== '') {
echo "<?php echo \$this->render('_search', ['model' => \$searchModel]); ?>";
} ?>

<?php if ($generator->indexWidgetType === 'grid'): ?>

Expand Down Expand Up @@ -150,10 +147,11 @@
);
$route = $generator->createRelationRoute($relation, 'index');
$label = Inflector::titleize(StringHelper::basename($relation->modelClass), '-');
$i18nMessageLabel = $generator->generateString($label);
$items .= <<<PHP
[
'url' => ['{$route}'],
'label' => '<i class="glyphicon glyphicon-{$iconType}"></i> ' . Yii::t('$generator->modelMessageCategory', '$label'),
'url' => ['$route'],
'label' => '<i class="glyphicon glyphicon-$iconType"></i> ' . $i18nMessageLabel,
],
PHP;
Expand Down Expand Up @@ -187,7 +185,7 @@
<hr />

<div class="table-responsive">
<?= '<?= ' ?>GridView::widget([
<?= '<?php echo ' ?>GridView::widget([
'dataProvider' => $dataProvider,
'pager' => [
'class' => yii\widgets\LinkPager::class,
Expand All @@ -197,19 +195,18 @@
<?php if ($generator->searchModelClass !== ''): ?>
'filterModel' => $searchModel,
<?php endif; ?>
'tableOptions' => ['class' => 'table table-striped table-bordered table-hover'],
'headerRowOptions' => ['class'=>'x'],
'columns' => [
<?php
$i18nMessageView = $generator->generateString('View');
$actionButtonColumn = <<<PHP
[
'class' => '{$generator->actionButtonClass}',
'template' => \$actionColumnTemplateString,
'buttons' => [
'view' => function (\$url, \$model, \$key) {
\$options = [
'title' => Yii::t('{$generator->messageCategory}', 'View'),
'aria-label' => Yii::t('{$generator->messageCategory}', 'View'),
'title' => $i18nMessageView,
'aria-label' => $i18nMessageView,
'data-pjax' => '0',
];
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', \$url, \$options);
Expand Down Expand Up @@ -263,7 +260,7 @@
<?= '<?= ' ?> ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => function ($model, $key, $index, $widget) {
'itemView' => function ($model) {
return Html::a(Html::encode($model-><?= $nameAttribute ?>), ['view', <?= $urlParams ?>]);
},
]); ?>
Expand Down

0 comments on commit 9cdfdeb

Please sign in to comment.