ListController.php 2.17 KB
<?php

namespace frontend\modules\news\controllers;

use Yii;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
//
use thread\actions\ListQuery;
//
use frontend\components\BaseController;
use frontend\modules\news\models\{
    Article, Group, search\Articles
};

/**
 * Class ListController
 *
 * @package frontend\modules\news\controllers
 * @author FilamentV <vortex.filament@gmail.com>
 * @copyright (c), Thread
 */
class ListController extends BaseController
{
    public $label = "News";
    public $title = "News";
    public $defaultAction = 'index';
    public $layout = "@app/layouts/base";

    /**
     *
     */
    public function init()
    {
        parent::init();

        $this->breadcrumbs = [
            [
                'label' => Yii::t('news', 'Blog'),
            ]
        ];
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'index' => ['get'],
                ],
            ],
        ];
    }

    /**
     * @return array
     * @throws NotFoundHttpException
     */
    public function actions()
    {
        return [
            'index' => [
                'class' => ListQuery::class,
                'query' => Article::find(),
                'recordOnPage' => 3,
                'layout' => $this->layout
            ],
        ];
    }

    /**
     * @param $alias
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionByGroup($alias)
    {
        $model = Group::getByAlias($alias);
        if ($model == null) {
            throw new NotFoundHttpException;
        }
        //
        $this->breadcrumbs[] = [
            'label' => $model['lang']['title'],
            'url' => $model->getUrl()
        ];
        //SEO
        Yii::$app->metatag->registerModel($model);
        //SEO
        $provider = (new Articles())->search($model);

        return $this->render('list', [
            'group' => $model,
            'models' => $provider->getModels(),
            'pages' => $provider->getPagination()
        ]);
    }
}