SchoolsController.php 1.93 KB
<?php

namespace frontend\controllers;

use common\models\Fields;
use common\models\Schools;
use common\models\Spots;
use common\models\Trainers;
use Yii;
use frontend\models\Sto;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\data\ArrayDataProvider;
use yii\data\ActiveDataProvider;

class SchoolsController extends Controller
{


    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Schools::find(),
            'pagination' => [
                'pageSize' => 10,
            ],

        ]);


        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionView($translit)
    {

        $model = $this->findModel($translit);

        $video = Fields::getData($model->id,'Schools','video');
        $phone = Fields::getData($model->id,'Schools','phone');
        $price = Fields::getData($model->id,'Schools','price');
        $trainers = $this->findTrainers($model->id);
        $spots_id = explode(',',$model->spots_id);
        $spots = Spots::findAll($spots_id);

        $dataSchools = new ArrayDataProvider([
            'allModels' => $spots,

        ]);


        return $this->render('view', [
            'model' => $model,
            'spots' => $dataSchools,
            'video' => $video,
            'phone' => $phone,
            'price' => $price,
            'trainers' => $trainers,
        ]);
    }



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

    protected function findTrainers($id)
    {
        if (($model = Trainers::find()->where(['school_id'=>$id])->all()) !== null) {

            return $model;

        } else {
            return [new Trainers()];
        }
    }
}