Skip to content

Commit

Permalink
Merge pull request #50 from dmstr/feature/hide-template-in-selection
Browse files Browse the repository at this point in the history
Feature - Hide template in selection
  • Loading branch information
eluhr authored May 23, 2022
2 parents 4f266ef + 0e0f6a7 commit cf1e8e0
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/migrations/m220523_110031_add_hide_template_flag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use yii\db\Migration;

/**
* Class m220523_110031_add_hide_template_flag
*/
class m220523_110031_add_hide_template_flag extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('{{%hrzg_widget_template}}', 'hide_in_list_selection',
$this->boolean()->notNull()->defaultValue(0)->after('twig_template'));
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('{{%hrzg_widget_template}}', 'hide_in_list_selection');
}
}
10 changes: 9 additions & 1 deletion src/models/crud/WidgetTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
*/
class WidgetTemplate extends BaseWidgetTemplate
{

public const IS_VISIBLE_IN_LIST = 0;
public const IS_HIDDEN_IN_LIST = 1;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -58,7 +62,11 @@ function ($attribute, $params) {
}
},
],

[
'hide_in_list_selection',
'in',
'range' => [self::IS_HIDDEN_IN_LIST, self::IS_VISIBLE_IN_LIST]
]
]
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/models/crud/base/WidgetTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @property string $name
* @property string $json_schema
* @property string $twig_template
* @property bool $hide_in_list_selection
*/
abstract class WidgetTemplate extends \yii\db\ActiveRecord
{
Expand Down Expand Up @@ -55,7 +56,8 @@ public function getAliasModel($plural = false)
public function rules()
{
return [
[['name', 'php_class', 'json_schema'], 'required'],
[['name', 'php_class', 'json_schema','hide_in_list_selection'], 'required'],
[['hide_in_list_selection'], 'boolean'],
[['json_schema', 'twig_template'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['name'], 'string', 'max' => 255],
Expand All @@ -72,6 +74,7 @@ public function attributeLabels()
'name' => Yii::t('widgets', 'Name'),
'json_schema' => Yii::t('widgets', 'Json Schema'),
'twig_template' => Yii::t('widgets', 'Twig Template'),
'hide_in_list_selection' => Yii::t('widgets', 'Hide In List Selection'),
'created_at' => Yii::t('widgets', 'Created At'),
'updated_at' => Yii::t('widgets', 'Updated At'),
];
Expand Down
6 changes: 4 additions & 2 deletions src/models/crud/search/WidgetTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function rules()
{
return [
[['id'], 'integer'],
[['name', 'json_schema', 'twig_template', 'created_at', 'updated_at'], 'safe'],
[['name', 'json_schema', 'twig_template', 'created_at', 'updated_at', 'php_class', 'hide_in_list_selection'], 'safe'],
];
}

Expand Down Expand Up @@ -63,7 +63,9 @@ public function search($params)

$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'json_schema', $this->json_schema])
->andFilterWhere(['like', 'twig_template', $this->twig_template]);
->andFilterWhere(['like', 'twig_template', $this->twig_template])
->andFilterWhere(['like', 'php_class', $this->php_class])
->andFilterWhere(['like', 'hide_in_list_selection', $this->hide_in_list_selection]);

$query->orderBy('name');

Expand Down
2 changes: 2 additions & 0 deletions src/views/crud/widget-template/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
['mode' => 'twig', 'containerOptions' => ['style' => 'height: 800px;']]
) ?>
<?php $this->endBlock() ?>

<?php echo $form->field($model,'hide_in_list_selection')->checkbox() ?>
</p>
<?php $this->endBlock(); ?>
<?= $this->blocks['main'] ?>
Expand Down
26 changes: 20 additions & 6 deletions src/views/crud/widget-template/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use hrzg\widget\models\crud\WidgetTemplate;
use insolita\wgadminlte\Box;
use yii\grid\GridView;
Expand All @@ -19,9 +20,9 @@
<?php Box::begin() ?>
<?php Pjax::begin(
[
'id' => 'widget-template-main',
'id' => 'widget-template-main',
'enableReplaceState' => false,
'linkSelector' => '#pjax-main ul.pagination a, th a',
'linkSelector' => '#pjax-main ul.pagination a, th a',
]
) ?>
<h1>
Expand All @@ -30,7 +31,7 @@
</h1>
<div class="clearfix crud-navigation">
<div class="pull-left">
<?= Html::a('<span class="glyphicon glyphicon-plus"></span> '.\Yii::t('widgets', 'New'), ['create'],
<?= Html::a('<span class="glyphicon glyphicon-plus"></span> ' . \Yii::t('widgets', 'New'), ['create'],
['class' => 'btn btn-success']) ?>
</div>
</div>
Expand All @@ -52,15 +53,28 @@
'template' => '{view} {update} {delete}',
'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] = \Yii::$app->controller->id ? \Yii::$app->controller->id.'/'.$action : $action;
$params = is_array($key) ? $key : [$model->primaryKey()[0] => (string)$key];
$params[0] = \Yii::$app->controller->id ? \Yii::$app->controller->id . '/' . $action : $action;

return Url::toRoute($params);
},
'contentOptions' => ['nowrap' => 'nowrap'],
],
'name',
'php_class',
[
'attribute' => 'php_class',
'filter' => $searchModel->optPhpClass()
],
[
'attribute' => 'hide_in_list_selection',
'filter' => [
WidgetTemplate::IS_VISIBLE_IN_LIST => Yii::t('widgets', 'Visible'),
WidgetTemplate::IS_HIDDEN_IN_LIST => Yii::t('widgets', 'Hidden'),
],
'value' => function ($model) {
return $model->hide_in_list_selection === WidgetTemplate::IS_VISIBLE_IN_LIST ? Yii::t('widgets', 'Visible') : Yii::t('widgets', 'Hidden');
}
]
],
]); ?>
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/views/crud/widget-template/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @var hrzg\widget\models\crud\WidgetTemplate $model
*/
use dmstr\bootstrap\Tabs;
use hrzg\widget\models\crud\WidgetTemplate;
use insolita\wgadminlte\Box;
use yii\helpers\Html;
use yii\widgets\DetailView;
Expand Down Expand Up @@ -79,6 +80,12 @@
'format' => 'html',
'value' => '<pre>' . Html::encode($model->twig_template) . '</pre>'
],
[
'attribute' => 'hide_in_list_selection',
'value' => function ($model) {
return $model->hide_in_list_selection === WidgetTemplate::IS_VISIBLE_IN_LIST ? Yii::t('widgets', 'Visible') : Yii::t('widgets', 'Hidden');
}
],
'created_at',
'updated_at',
],
Expand Down
9 changes: 7 additions & 2 deletions src/widgets/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function getMenuItems()
$linkTarget = \Yii::$app->params['backend.iframe.name'] ?? '_self';
return [
[
'label' => ' ' . $this->id . ' <span class="label label-info">Cell</span>',
'label' => ' ' . $this->id . ' <span class="label label-info">' . \Yii::t('widgets', 'Cell') . '</span>',
'items' => [[
'label' => FA::icon(FA::_PLUS),
'url' => [
Expand Down Expand Up @@ -302,7 +302,12 @@ private function generateCellControls()
['label' => $this->id]
];

foreach (WidgetTemplate::find()->where(['php_class' => TwigTemplate::class])->orderBy('name')->all() as $template) {
$templates = WidgetTemplate::find()
->where(['php_class' => TwigTemplate::class])
->andWhere(['hide_in_list_selection' => WidgetTemplate::IS_VISIBLE_IN_LIST])
->orderBy('name')
->all();
foreach ($templates as $template) {
$items[] = [
'label' => $template->name,
'url' => [
Expand Down

0 comments on commit cf1e8e0

Please sign in to comment.