<?php

namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use common\models\Articles;
use yii\web\HttpException;
use yii\data\Pagination;

class ArticlesController extends Controller
{

    public function actionMain()
    {
            
                $query = Articles::find()->groupBy('id')->orderBy('id DESC') ;
                $countQuery = clone $query;
                $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize'=>18]);
                $pages->forcePageParam = false;
                $pages->pageSizeParam = false;
                $news = $query->offset($pages->offset)
                    ->with(['comments.rating', 'averageRating'])
                    ->limit($pages->limit)
                    ->all();
                
            return $this->render('index', [
                'pages'=>$pages,
                'news'=>$news,
            ]);
    }
    public function actionShow($translit){
        $news = $this->findModel($translit);


        return $this->render('show', [
            'news'=>$news,
        ]);
    }
    protected function findModel($translit)
    {
        if (($model = Articles::findOne(["translit"=>$translit])) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
}