ServiceController.php
6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?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;
    use frontend\microdata\MicrodataFabric;
    
    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',
                                         '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 ];
                    }
                }
                
            }
            $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',
                    ]
                ];
	        if (count($model->prices) <= 1 && isset($prices)) {
		        if (!empty($prices)) $layoutMicrodata['offers']['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)
        {
            $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)) {
                throw new NotFoundHttpException('Model not found');
            }
            return $model;
        }
    } 
