ServiceController.php 2.07 KB
<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 29.05.18
     * Time: 9:51
     */
    
    namespace frontend\controllers;
    
    use common\models\Service;
    use yii\db\ActiveQuery;
    use yii\db\Query;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;

    class ServiceController extends Controller
    {
        public function actionView($id){
            $model = $this->findModel($id);
            if ($model->parent_id == null){
               $others = Service::find()->where(['parent_id' => $model->id])->with('services.language.alias')->all();
            }elseif ($model->level == 1){
                $others = Service::find()->where(['parent_id' => $model->parent_id])->with('services.language.alias')->all();
            }else{
                $others = Service::find()->where(['parent_id' => (new Query())->select('parent_id')->from('service')->where(['id' => $model->parent_id])])->with('services.language.alias')->all();
            }
            
            $model->body = str_replace('[[prices]]', $this->renderPartial('_prices', ['prices' => $model->prices]), $model->body);
            return $this->render('view', [
                'model' => $model,
                'others'=> $others
            ]);
        }
        
        public function findModel($id){
            $model = Service::find()
                            ->where(['id' => $id, 'status' => true])
                            ->with(['language.alias',  'prices' => function (ActiveQuery $query){
                                $query->where(['status' => true])->orderBy('sort');
                            }, 'comments' => function (ActiveQuery $query){
                                $query->where(['status' => true]);
                            }, 'questions' => function (ActiveQuery $query){
                                $query->where(['status' => true])->with('doctor');
                            }])->one();
            if (empty($model)){
                throw new NotFoundHttpException('Model not found');
            }
            return $model;
        }
    }