index.php 4.22 KB
<?php
    use artbox\webcomment\models\CommentModel;
    use artbox\webcomment\models\CommentModelSearch;
    use yii\data\ActiveDataProvider;
    use yii\grid\GridView;
    use yii\helpers\Html;
    use yii\helpers\StringHelper;
    use yii\web\View;
    use yii\widgets\Pjax;
    
    /**
     * @var ActiveDataProvider $dataProvider
     * @var CommentModelSearch $searchModel
     * @var string             $commentModel
     * @var View               $this
     */
    $this->title = \Yii::t('artbox-comment', 'Комментарии');
    
    $this->params[ 'breadcrumbs' ][] = $this->title;
    
    $statuses = [
        $searchModel::STATUS_ACTIVE  => \Yii::t('artbox-comment', 'Активный'),
        $searchModel::STATUS_HIDDEN  => \Yii::t('artbox-comment', 'Скрытый'),
        $searchModel::STATUS_DELETED => \Yii::t('artbox-comment', 'Удаленный'),
    ];
    Pjax::begin();
    if (( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != null) {
        echo Html::tag('p', $success);
    }
    echo GridView::widget(
        [
            'dataProvider' => $dataProvider,
            'filterModel'  => $searchModel,
            'columns'      => [
                'id',
                [
                    'attribute' => 'created_at',
                    'format'    => [
                        'date',
                        'php:d.m.Y',
                    ],
                    'filter'    => false,
                ],
                [
                    'label'   => \Yii::t('artbox-comment', 'Комментарий'),
                    'content' => function (CommentModel $model) {
                        return StringHelper::truncate($model->text, 40, '...');
                    },
                ],
                [
                    'attribute' => 'customer_id',
                    'value'     => function ($model) {
                        /**
                         * @var CommentModel $model
                         */
                        if (!empty($model->customer)) {
                            return $model->customer->username . ' (id:' . $model->customer->id . ')';
                        } else {
                            return $model->username . ' ' . $model->email . ' (' . \Yii::t(
                                    'artbox-comment',
                                    'Гость'
                                ) . ')';
                        }
                    },
                ],
                [
                    'attribute' => 'status',
                    'filter'    => $statuses,
                    'value'     => function ($model) use ($statuses) {
                        /**
                         * @var CommentModel $model
                         */
                        if (array_key_exists($model->status, $statuses)) {
                            return $statuses[ $model->status ];
                        } else {
                            return null;
                        }
                    },
                ],
                [
                    'attribute' => 'ratingValue',
                    'value'     => function ($model) {
                        /**
                         * @var CommentModel $model
                         */
                        if (!empty($model->rating)) {
                            return $model->rating->value;
                        }
                        return null;
                    },
                ],
                [
                    'attribute' => 'childrenCount',
                    'value'     => function ($model) {
                        /**
                         * @var CommentModel $model
                         */
                        return count($model->children);
                    },
                ],
                [
                    'class'    => 'yii\grid\ActionColumn',
                    'buttons'  => [
                        'answer' => function (string $url) {
                            return Html::a(Html::tag('i', '', [ 'class' => 'glyphicon glyphicon-bullhorn' ]), $url);
                        },
                    ],
                    'template' => '{update} {answer} {delete}',
                ],
            ],
        ]
    );
    Pjax::end();