From 7f8928746b5c59458d9a974299dd7037621b8262 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Thu, 21 Jun 2018 16:00:24 +0300 Subject: [PATCH] - blog --- frontend/controllers/ServiceController.php | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------- frontend/views/blog/view.php | 2 +- 2 files changed, 136 insertions(+), 66 deletions(-) diff --git a/frontend/controllers/ServiceController.php b/frontend/controllers/ServiceController.php index 652fc2b..63550fc 100644 --- a/frontend/controllers/ServiceController.php +++ b/frontend/controllers/ServiceController.php @@ -14,83 +14,153 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use frontend\microdata\MicrodataFabric; - + class ServiceController extends Controller { - public function actionView($id){ + 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', 'language.alias'])->all(); - if (empty($others)){ - $others = Service::find()->where(['parent_id' => null, 'status' => true])->with(['services.language.alias', 'language.alias'])->all(); - } - }elseif ($model->level == 1){ - $others = Service::find()->where(['parent_id' => $model->parent_id])->with(['services.language.alias', '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', 'language.alias'])->all(); + if ($model->parent_id == null) { + $others = Service::find() + ->where([ 'parent_id' => $model->id ]) + ->with( + [ + 'services.language.alias', + 'language.alias', + ] + ) + ->all(); + if (empty($others)) { + $others = Service::find() + ->where( + [ + 'parent_id' => null, + 'status' => true, + ] + ) + ->with( + [ + 'services.language.alias', + 'language.alias', + ] + ) + ->all(); + } + } elseif ($model->level == 1) { + $others = Service::find() + ->where([ 'parent_id' => $model->parent_id ]) + ->with( + [ + 'services.language.alias', + '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', + 'language.alias', + ] + ) + ->all(); } - - + # список цен для микроданных - $prices=[]; - foreach ($model->prices as $kry=>$price) - { - foreach ($price as $key2=>$val2){ - if($key2=='price')$prices[]=$price[$key2]; + $prices = []; + foreach ($model->prices as $kry => $price) { + foreach ($price as $key2 => $val2) { + if ($key2 == 'price') { + $prices[] = $price[ $key2 ]; + } } - + } - - $layoutMicrodata=(count($model->prices)>1)? + + $layoutMicrodata = ( count($model->prices) > 1 ) ? [ + '@context' => 'http://schema.org/', + '@type' => 'Product', + 'name' => "'{$model->language->attributes['title']}'", + 'offers' => [ + '@type' => 'AggregateOffer', + 'lowPrice' => "'" . min($prices) . "'", + 'highPrice' => "'" . max($prices) . "'", + 'priceCurrency' => 'UAH', + ], + ] : [ + 'type' => 'Product', + 'name' => "'{$model->language->attributes['title']}'", + 'offers' => [ + '@type' => 'Offer', + 'priceCurrency' => 'UAH', + 'Price' => "'" . max($prices) . "'", + ], + ]; + + $microdata = new MicrodataFabric(); + $pageMicrodata = $microdata::createJsonFromProduct($layoutMicrodata) + ->toJson(); + + $model->body = str_replace( + '[[prices]]', + $this->renderPartial('_prices', [ 'prices' => $model->prices ]), + $model->body + ); + return $this->render( + 'view', [ - '@context'=> 'http://schema.org/', - '@type'=> 'Product', - 'name'=> "'{$model->language->attributes['title']}'", - 'offers' => - [ - '@type'=> 'AggregateOffer', - 'lowPrice'=> "'".min($prices)."'", - 'highPrice'=> "'".max($prices)."'", - 'priceCurrency'=> 'UAH' - ] + 'model' => $model, + 'others' => $others, + 'microdata' => $pageMicrodata, ] - :[ - 'type'=>'Product', - 'name'=> "'{$model->language->attributes['title']}'", - 'offers'=> [ - '@type'=> 'Offer', - 'priceCurrency'=> 'UAH', - 'Price'=> "'".max($prices)."'", - ] - ]; - - - $microdata=new MicrodataFabric(); - $pageMicrodata=$microdata::createJsonFromProduct($layoutMicrodata)->toJson(); - - - - $model->body = str_replace('[[prices]]', $this->renderPartial('_prices', ['prices' => $model->prices]), $model->body); - return $this->render('view', [ - 'model' => $model, - 'others'=> $others, - 'microdata'=>$pageMicrodata - ]); + ); } - public function findModel($id){ + public function findModel($id) + { $model = Service::find() - ->where(['id' => $id, 'status' => true]) - ->with(['language.alias', 'prices' => function (ActiveQuery $query){ - $query->where(['status' => true])->with('language')->orderBy('sort'); - }, 'comments' => function (ActiveQuery $query){ - $query->where(['status' => true]); - }, 'questions' => function (ActiveQuery $query){ - $query->where(['status' => true])->with('doctor'); - },'packages' => function (ActiveQuery $query){ - $query->with(['image', 'language.alias'])->where(['status' => true]); - }])->one(); - if (empty($model)){ + ->where( + [ + 'id' => $id, + 'status' => true, + ] + ) + ->with( + [ + 'language.alias', + 'prices' => function (ActiveQuery $query) { + $query->where([ 'status' => true ]) + ->with('language') + ->orderBy('sort'); + }, + 'comments' => function (ActiveQuery $query) { + $query->where([ 'status' => true ]); + }, + 'questions' => function (ActiveQuery $query) { + $query->where([ 'status' => true ]) + ->with('doctor'); + }, + 'packages' => function (ActiveQuery $query) { + $query->with( + [ + 'image', + 'language.alias', + ] + ) + ->where([ 'status' => true ]); + }, + ] + ) + ->one(); + if (empty($model)) { throw new NotFoundHttpException('Model not found'); } return $model; diff --git a/frontend/views/blog/view.php b/frontend/views/blog/view.php index 4eb63d3..3aea3e6 100755 --- a/frontend/views/blog/view.php +++ b/frontend/views/blog/view.php @@ -63,7 +63,7 @@ image) ? $model->image->getPath() : null) ->cropResize(555, 370) ->quality(84) - ->renderImage(['alt'=>$alt,'title'=>$title])?> + ->renderImage(['alt'=>$alt,'title'=>$title, 'class' => 'blog-view-img'])?>
created_at)?> comments)?> -- libgit2 0.21.4