Blame view

frontend/controllers/ServiceController.php 3.81 KB
aedc35b5   Anastasia   - load scg to logo
1
2
3
4
5
6
7
8
9
10
11
  <?php
      /**
       * Created by PhpStorm.
       * User: stes
       * Date: 29.05.18
       * Time: 9:51
       */
      
      namespace frontend\controllers;
      
      use common\models\Service;
43a24059   Anastasia   - servives
12
      use yii\db\ActiveQuery;
2032cb18   Anastasia   - services menu
13
      use yii\db\Query;
aedc35b5   Anastasia   - load scg to logo
14
15
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
cfb947e6   alex   Микроданные для у...
16
      use frontend\microdata\MicrodataFabric;
aedc35b5   Anastasia   - load scg to logo
17
18
19
  
      class ServiceController extends Controller
      {
aedc35b5   Anastasia   - load scg to logo
20
21
22
          public function actionView($id){
              $model = $this->findModel($id);
              if ($model->parent_id == null){
727d4d0e   Anastasia   - debug
23
                 $others = Service::find()->where(['parent_id' => $model->id])->with('services.language.alias')->all();
a4320bd1   Anastasia   - bug fix
24
25
26
                 if (empty($others)){
                     $others = Service::find()->where(['parent_id' => null, 'status' => true])->all();
                 }
2032cb18   Anastasia   - services menu
27
              }elseif ($model->level == 1){
727d4d0e   Anastasia   - debug
28
                  $others = Service::find()->where(['parent_id' => $model->parent_id])->with('services.language.alias')->all();
aedc35b5   Anastasia   - load scg to logo
29
              }else{
4d688be7   Anastasia   - bug fix
30
                  $others = Service::find()->where(['parent_id' => (new Query())->select('parent_id')->from('service')->where(['id' => $model->parent_id])])->with('services.language.alias')->all();
aedc35b5   Anastasia   - load scg to logo
31
              }
cfb947e6   alex   Микроданные для у...
32
33
  
  
3fafe556   alex   Микроразметка для...
34
              # список цен для микроданных
cfb947e6   alex   Микроданные для у...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
              $prices=[];
              foreach ($model->prices as $kry=>$price)
              {
                  foreach ($price as $key2=>$val2){
                      if($key2=='price')$prices[]=$price[$key2];
                  }
  
              }
  
              $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']}'",
cfb947e6   alex   Микроданные для у...
60
61
62
63
                      'offers'=> [
                          '@type'=> 'Offer',
                          'priceCurrency'=> 'UAH',
                          'Price'=> "'".max($prices)."'",
3fafe556   alex   Микроразметка для...
64
  
cfb947e6   alex   Микроданные для у...
65
66
67
68
69
70
71
72
73
                      ]
                  ];
  
  
              $microdata=new MicrodataFabric();
              $pageMicrodata=$microdata::createJsonFromProduct($layoutMicrodata)->toJson();
  
  
  
43a24059   Anastasia   - servives
74
              $model->body = str_replace('[[prices]]', $this->renderPartial('_prices', ['prices' => $model->prices]), $model->body);
aedc35b5   Anastasia   - load scg to logo
75
76
              return $this->render('view', [
                  'model' => $model,
cfb947e6   alex   Микроданные для у...
77
78
                  'others'=> $others,
                  'microdata'=>$pageMicrodata
aedc35b5   Anastasia   - load scg to logo
79
80
81
82
83
84
              ]);
          }
          
          public function findModel($id){
              $model = Service::find()
                              ->where(['id' => $id, 'status' => true])
727d4d0e   Anastasia   - debug
85
                              ->with(['language.alias',  'prices' => function (ActiveQuery $query){
43a24059   Anastasia   - servives
86
87
88
                                  $query->where(['status' => true])->orderBy('sort');
                              }, 'comments' => function (ActiveQuery $query){
                                  $query->where(['status' => true]);
b8459872   Anastasia   - forms1
89
                              }, 'questions' => function (ActiveQuery $query){
727d4d0e   Anastasia   - debug
90
                                  $query->where(['status' => true])->with('doctor');
a4320bd1   Anastasia   - bug fix
91
92
                              },'packages' => function (ActiveQuery $query){
                                  $query->with(['image', 'language'])->where(['status' => true]);
43a24059   Anastasia   - servives
93
                              }])->one();
aedc35b5   Anastasia   - load scg to logo
94
95
96
97
98
99
              if (empty($model)){
                  throw new NotFoundHttpException('Model not found');
              }
              return $model;
          }
      }