index.php 6.05 KB
<?php
    
    use artbox\catalog\models\Category;
    use artbox\catalog\models\Product;
    use artbox\catalog\models\ProductSearch;
    use kartik\select2\Select2;
    use yii\bootstrap\Html;
    use yii\data\ActiveDataProvider;
    use yii\helpers\Url;
    use yii\web\JsExpression;
    use yii\web\View;
    use yiister\gentelella\widgets\grid\GridView;
    use yiister\gentelella\widgets\Panel;
    
    /**
     * @var View               $this
     * @var ProductSearch      $searchModel
     * @var ActiveDataProvider $dataProvider
     */
    
    $this->title = Yii::t('catalog', 'Products');
    $this->params[ 'breadcrumbs' ][] = $this->title;
    
    $category = Category::find()
                        ->filterWhere(
                            [
                                'id' => $searchModel->category_title,
                            ]
                        )
                        ->with('lang')
                        ->one();
    
    $category_title = $category ? $category->lang->title : '';
?>
<div class="product-index">
    
    <?php
        $xPanel = Panel::begin(
            [
                'header' => Html::encode($this->title),
            ]
        );
    ?>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
  
  <p>
      <?= Html::a(Yii::t('catalog', 'Create Product'), [ 'create' ], [ 'class' => 'btn btn-success' ]) ?>
      <?= Html::button(
          \Yii::t('catalog', 'Delete'),
          [
              'class'        => 'pull-right btn btn-danger artbox-selection-delete',
              'data-message' => \Yii::t('catalog', 'Delete selected rows?'),
              'data-url'     => Url::to([ 'delete-multiple' ]),
          ]
      ) ?>
  </p>
    <?= GridView::widget(
        [
            'dataProvider' => $dataProvider,
            'filterModel'  => $searchModel,
            'columns'      => [
                [
                    'class'    => 'yii\grid\CheckboxColumn',
                    'name'     => 'artbox-selection',
                    'cssClass' => 'artbox-selection',
                ],
                'id',
                [
                    'attribute' => 'title',
                    'value'     => 'lang.title',
                ],
                [
                    'attribute' => 'category_title',
                    'value'     => 'category.lang.title',
                    'filter'    => Select2::widget(
                        [
                            'name'          => 'ProductSearch[category_title]',
                            'value'         => $searchModel->category_title,
                            'initValueText' => $category_title,
                            'options'       => [ 'placeholder' => \Yii::t('catalog', 'Search for a category ...') ],
                            'pluginOptions' => [
                                'allowClear'         => true,
                                'minimumInputLength' => 3,
                                'language'           => [
                                    'errorLoading' => new JsExpression(
                                        "function () { return 'Waiting for results...'; }"
                                    ),
                                ],
                                'ajax'               => [
                                    'url'      => Url::to([ 'category/list' ]),
                                    'dataType' => 'json',
                                    'data'     => new JsExpression(
                                        'function(params) { 
                                                                    return {
                                                                      q:params.term
                                                                        }; 
                                                                    }'
                                    ),
                                ],
                                'escapeMarkup'       => new JsExpression('function (markup) { return markup; }'),
                                'templateResult'     => new JsExpression(
                                    'function (category) { 
                                                                        return category.text; 
                                                                      }'
                                ),
                                'templateSelection'  => new JsExpression(
                                    'function (category) {
                                                                        return category.text; 
                                                                      }'
                                ),
                            ],
                        ]
                    ),
                ],
                [
                    'attribute' => 'brand_title',
                    'value'     => 'brand.lang.title',
                ],
                'created_at:datetime',
                [
                    'attribute' => 'status',
                    'format'    => 'boolean',
                    'filter'    => [
                        0 => \Yii::$app->formatter->asBoolean(0),
                        1 => \Yii::$app->formatter->asBoolean(1),
                    ],
                ],
                [
                    'class'    => 'yii\grid\ActionColumn',
                    'buttons'  => [
                        'variants' => function ($url, $model) {
                            /**
                             * @var Product $model
                             */
                            return Html::a(
                                Html::icon('align-justify'),
                                [
                                    '/variant/index',
                                    'product_id' => $model->id,
                                ]
                            );
                        },
                    ],
                    'template' => '{variants} {view} {update} {delete}',
                ],
            ],
        ]
    ); ?>
    
    <?php
        $xPanel::end();
    ?>
</div>