Commit cfb947e6faac20bdda5a8077becb598055ddf648
1 parent
ab9c91d2
Микроданные для услуг
Showing
12 changed files
with
545 additions
and
5 deletions
Show diff stats
1 | +<?php | |
2 | +namespace frontend\controllers; | |
3 | + | |
4 | +use phpDocumentor\Reflection\Types\Object_; | |
5 | +use yii\web\Controller; | |
6 | +use frontend\microdata\MicrodataFabric; | |
7 | +use yii\helpers\VarDumper as d; | |
8 | +use frontend\microdata\test2; | |
9 | + | |
10 | +class NewController extends Controller | |
11 | +{ | |
12 | + public function actionIndex() | |
13 | + { | |
14 | + | |
15 | + | |
16 | + | |
17 | + | |
18 | + $microdata=new MicrodataFabric(); | |
19 | + print_r($microdata::createJsonFromArticle($productArray)->toJson()); | |
20 | + die; | |
21 | + } | |
22 | +} | |
0 | 23 | \ No newline at end of file | ... | ... |
frontend/controllers/ServiceController.php
... | ... | @@ -13,6 +13,7 @@ |
13 | 13 | use yii\db\Query; |
14 | 14 | use yii\web\Controller; |
15 | 15 | use yii\web\NotFoundHttpException; |
16 | + use frontend\microdata\MicrodataFabric; | |
16 | 17 | |
17 | 18 | class ServiceController extends Controller |
18 | 19 | { |
... | ... | @@ -28,11 +29,62 @@ |
28 | 29 | }else{ |
29 | 30 | $others = Service::find()->where(['parent_id' => (new Query())->select('parent_id')->from('service')->where(['id' => $model->parent_id])])->with('services.language.alias')->all(); |
30 | 31 | } |
31 | - | |
32 | + | |
33 | + | |
34 | + $prices=[]; | |
35 | + foreach ($model->prices as $kry=>$price) | |
36 | + { | |
37 | + foreach ($price as $key2=>$val2){ | |
38 | + if($key2=='price')$prices[]=$price[$key2]; | |
39 | + } | |
40 | + | |
41 | + } | |
42 | + | |
43 | + $layoutMicrodata=(count($model->prices)>1)? | |
44 | + [ | |
45 | + '@context'=> 'http://schema.org/', | |
46 | + '@type'=> 'Product', | |
47 | + 'name'=> "'{$model->language->attributes['title']}'", | |
48 | + 'offers' => | |
49 | + [ | |
50 | + '@type'=> 'AggregateOffer', | |
51 | + 'lowPrice'=> "'".min($prices)."'", | |
52 | + 'highPrice'=> "'".max($prices)."'", | |
53 | + 'priceCurrency'=> 'UAH' | |
54 | + ] | |
55 | + ] | |
56 | + :[ | |
57 | + 'type'=>'Product', | |
58 | + 'name'=> "'{$model->language->attributes['title']}'", | |
59 | + 'image'=> [ | |
60 | + 'https://example.com/photos/1x1/photo.jpg', | |
61 | + 'https://example.com/photos/4x3/photo.jpg', | |
62 | + 'https://example.com/photos/16x9/photo.jpg' | |
63 | + ], | |
64 | + 'offers'=> [ | |
65 | + '@type'=> 'Offer', | |
66 | + 'priceCurrency'=> 'UAH', | |
67 | + 'Price'=> "'".max($prices)."'", | |
68 | + 'priceValidUntil'=> '2020-11-05', | |
69 | + 'itemCondition'=> 'http://schema.org/UsedCondition', | |
70 | + 'availability'=> 'http://schema.org/InStock', | |
71 | + 'seller'=> [ | |
72 | + 'type'=> 'Organization', | |
73 | + 'name'=>'Executive Objects'] | |
74 | + ] | |
75 | + ]; | |
76 | + | |
77 | + | |
78 | + $microdata=new MicrodataFabric(); | |
79 | + $pageMicrodata=$microdata::createJsonFromProduct($layoutMicrodata)->toJson(); | |
80 | + | |
81 | + | |
82 | + | |
32 | 83 | $model->body = str_replace('[[prices]]', $this->renderPartial('_prices', ['prices' => $model->prices]), $model->body); |
33 | 84 | return $this->render('view', [ |
34 | 85 | 'model' => $model, |
35 | - 'others'=> $others | |
86 | + 'others'=> $others, | |
87 | + 'microdata'=>$pageMicrodata | |
36 | 88 | ]); |
37 | 89 | } |
38 | 90 | ... | ... |
1 | +<?php | |
2 | + | |
3 | +namespace frontend\microdata; | |
4 | + | |
5 | +use yii\base\BaseObject; | |
6 | +use frontend\microdata\Microdata; | |
7 | + | |
8 | + | |
9 | +class ArticleMicrodata extends Microdata | |
10 | +{ | |
11 | + | |
12 | + | |
13 | + # массив, который составляет из себя реально существующие пары свойство/значение | |
14 | + # !!! так же тут нету пар с пустым значением | |
15 | + public $constructArray=[]; | |
16 | + | |
17 | + public function __construct(array $config = []) | |
18 | + { | |
19 | + | |
20 | + # обрезаю входящие свойства, работаем только с реально существующими | |
21 | + foreach ($config as $key=>$value){ | |
22 | + if(!property_exists(self::class,$key)) | |
23 | + { | |
24 | + unset($config[$key]); | |
25 | + } | |
26 | + elseif($value==null || $value===false || $value=='') | |
27 | + { | |
28 | + unset($config[$key]); | |
29 | + } | |
30 | + | |
31 | + | |
32 | + | |
33 | + } | |
34 | + | |
35 | + $this->constructArray=$config; | |
36 | + | |
37 | + parent::__construct($config); | |
38 | + } | |
39 | + | |
40 | + | |
41 | + public function createJson() | |
42 | + { | |
43 | + | |
44 | + } | |
45 | + | |
46 | + | |
47 | + public function toJson(): string | |
48 | + { | |
49 | + $resultJson=" <script type=\"application/ld+json\">"; | |
50 | + | |
51 | + $test=[]; | |
52 | + foreach ($this->constructArray as $key=>$value) | |
53 | + { | |
54 | + if($key=='type')$key='@'.$key; | |
55 | + $test[$key]=$value; | |
56 | + } | |
57 | + $resultJson1=json_encode($test); | |
58 | + $resultJson1=str_replace('\/','/',$resultJson1); | |
59 | + $resultJson.=$resultJson1; | |
60 | + $resultJson.='</script>'; | |
61 | + return $resultJson; | |
62 | + } | |
63 | + | |
64 | + | |
65 | + function add($key,$value) | |
66 | + { | |
67 | + if ($value!==null) | |
68 | + $this->constructArray[$key]=$value; | |
69 | + | |
70 | + } | |
71 | + | |
72 | + | |
73 | + | |
74 | + /** | |
75 | + | |
76 | + <script type="application/ld+json"> | |
77 | + { | |
78 | + "@context": "http://schema.org", | |
79 | + "@type": "Article", | |
80 | + "author": "SEO-специалист", | |
81 | + "interactionStatistic": [ | |
82 | + { | |
83 | + "@type": "InteractionCounter", | |
84 | + "interactionService": | |
85 | + { | |
86 | + "@type": "Website", | |
87 | + "name": "Twitter", | |
88 | + "url": "http://www.twitter.com" | |
89 | + }, | |
90 | + "interactionType": "http://schema.org/ShareAction", | |
91 | + "userInteractionCount": "20" | |
92 | + }, | |
93 | + { | |
94 | + "@type": "InteractionCounter", | |
95 | + "interactionType": "http://schema.org/CommentAction", | |
96 | + "userInteractionCount": "10" | |
97 | + } | |
98 | + ], | |
99 | + "name": "JSON-LD" | |
100 | + } | |
101 | + </script> | |
102 | + | |
103 | + | |
104 | + | |
105 | + | |
106 | + | |
107 | + | |
108 | + <script type="application/ld+json"> | |
109 | + { | |
110 | + "@context": "http://schema.org", | |
111 | + "@type": "NewsArticle", | |
112 | + "mainEntityOfPage": { | |
113 | + "@type": "WebPage", | |
114 | + "@id": "https://google.com/article" | |
115 | + }, | |
116 | + "headline": "Article headline", | |
117 | + "image": [ | |
118 | + "https://example.com/photos/1x1/photo.jpg", | |
119 | + "https://example.com/photos/4x3/photo.jpg", | |
120 | + "https://example.com/photos/16x9/photo.jpg" | |
121 | + ], | |
122 | + "datePublished": "2015-02-05T08:00:00+08:00", | |
123 | + "dateModified": "2015-02-05T09:20:00+08:00", | |
124 | + "author": { | |
125 | + "@type": "Person", | |
126 | + "name": "John Doe" | |
127 | + }, | |
128 | + "publisher": { | |
129 | + "@type": "Organization", | |
130 | + "name": "Google", | |
131 | + "logo": { | |
132 | + "@type": "ImageObject", | |
133 | + "url": "https://google.com/logo.jpg" | |
134 | + } | |
135 | + }, | |
136 | + "description": "A most wonderful article" | |
137 | + } | |
138 | + </script> | |
139 | + | |
140 | + | |
141 | + | |
142 | + */ | |
143 | + | |
144 | + | |
145 | +} | |
146 | + | |
147 | + | ... | ... |
1 | +<?php | |
2 | +//---------------------------------------------------------------------------------------------------------------------- | |
3 | +// Класс, который будет подтягивать общие настройки | |
4 | +//---------------------------------------------------------------------------------------------------------------------- | |
5 | +namespace frontend\microdata; | |
6 | + | |
7 | +use yii\base\BaseObject; | |
8 | + | |
9 | + | |
10 | + | |
11 | +class Microdata extends BaseObject | |
12 | +{ | |
13 | + public $name=null; | |
14 | + public $type=null; | |
15 | + public $image=null; | |
16 | + public $test=null; | |
17 | + public $offers=null; | |
18 | + | |
19 | +} | |
0 | 20 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | +namespace frontend\microdata; | |
3 | + | |
4 | +use frontend\microdata\Microdata; | |
5 | + | |
6 | + | |
7 | + | |
8 | + | |
9 | +class MicrodataFabric extends Microdata | |
10 | +{ | |
11 | + public static function createJsonFromProduct($product) | |
12 | + { | |
13 | + return ($product==null)? | |
14 | + new ProductMicrodata(array_merge(['url'=>123],self::getDefaultSettings())) | |
15 | + : | |
16 | + new ProductMicrodata(array_merge($product,['url'=>123],self::getDefaultSettings())); | |
17 | + } | |
18 | + public static function createJsonFromArticle($article=null) | |
19 | + { | |
20 | + | |
21 | + return ($article==null)? | |
22 | + new ArticleMicrodata(array_merge(['url'=>123],self::getDefaultSettings())) | |
23 | + : | |
24 | + new ArticleMicrodata(array_merge($article,['url'=>123],self::getDefaultSettings())); | |
25 | + } | |
26 | + | |
27 | + | |
28 | + protected static function getDefaultSettings(){ | |
29 | + return require_once (realpath(__DIR__.'/settings/settings.php')); | |
30 | + } | |
31 | + | |
32 | +} | |
0 | 33 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | +namespace frontend\microdata; | |
3 | + | |
4 | +use yii\base\BaseObject; | |
5 | + | |
6 | + | |
7 | +class ProductMicrodata extends Microdata | |
8 | +{ | |
9 | + | |
10 | + public $type=null; | |
11 | + public $name=null; | |
12 | + public $image=null; | |
13 | + public $brand=null; | |
14 | + public $aggregateRating=null; | |
15 | + public $offers=null; | |
16 | + public $description=null; | |
17 | + public $npm=null; | |
18 | + | |
19 | + | |
20 | + | |
21 | + # массив, который составляет из себя реально существующие пары свойство/значение | |
22 | + # !!! так же тут нету пар с пустым значением | |
23 | + public $constructArray=[]; | |
24 | + | |
25 | + public function __construct(array $config = []) | |
26 | + { | |
27 | + | |
28 | + # обрезаю входящие свойства, работаем только с реально существующими | |
29 | + foreach ($config as $key=>$value){ | |
30 | + if(!property_exists(self::class,$key)) | |
31 | + { | |
32 | + unset($config[$key]); | |
33 | + } | |
34 | + elseif($value==null || $value===false || $value=='') | |
35 | + { | |
36 | + unset($config[$key]); | |
37 | + } | |
38 | + | |
39 | + | |
40 | + | |
41 | + } | |
42 | + | |
43 | + $this->constructArray=$config; | |
44 | + | |
45 | + parent::__construct($config); | |
46 | + } | |
47 | + | |
48 | + | |
49 | + public function createJson() | |
50 | + { | |
51 | + | |
52 | + } | |
53 | + | |
54 | + | |
55 | + public function toJson(): string | |
56 | + { | |
57 | + $resultJson=" <script type=\"application/ld+json\">"; | |
58 | + | |
59 | + $test=[]; | |
60 | + foreach ($this->constructArray as $key=>$value) | |
61 | + { | |
62 | + if($key=='type')$key='@'.$key; | |
63 | + $test[$key]=$value; | |
64 | + } | |
65 | + | |
66 | + $resultJson1=json_encode($test,JSON_UNESCAPED_UNICODE); | |
67 | + $resultJson1=str_replace('\/','/',$resultJson1); | |
68 | + $resultJson.=$resultJson1; | |
69 | + $resultJson.='</script>'; | |
70 | + return $resultJson; | |
71 | + } | |
72 | + | |
73 | + | |
74 | + function add($key,$value) | |
75 | + { | |
76 | + if ($value!==null) | |
77 | + $this->constructArray[$key]=$value; | |
78 | + | |
79 | + } | |
80 | + | |
81 | + | |
82 | + public function test() | |
83 | + { | |
84 | + | |
85 | + | |
86 | + #Для страниц услуг где представлена только одна цена | |
87 | + $productArray=[ | |
88 | + 'type'=>'Product', | |
89 | + 'name'=> 'Executive Anvil', | |
90 | + 'image'=> [ | |
91 | + 'https://example.com/photos/1x1/photo.jpg', | |
92 | + 'https://example.com/photos/4x3/photo.jpg', | |
93 | + 'https://example.com/photos/16x9/photo.jpg' | |
94 | + ], | |
95 | + 'description'=>'Sleeker than ACME\'s Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.', | |
96 | + 'mpn'=>'925872', | |
97 | + 'brand'=>[ | |
98 | + 'type'=> 'Thing', | |
99 | + 'name'=> 'ACME' | |
100 | + ], | |
101 | + 'aggregateRating'=> | |
102 | + [ | |
103 | + 'type'=> 'AggregateRating', | |
104 | + 'ratingValue'=> '4.4', | |
105 | + 'reviewCount'=> '89' | |
106 | + ], | |
107 | + 'offers'=> [ | |
108 | + '@type'=> 'Offer', | |
109 | + 'priceCurrency'=> 'USD', | |
110 | + 'Price'=> '199.99', | |
111 | + 'priceValidUntil'=> '2020-11-05', | |
112 | + 'itemCondition'=> 'http://schema.org/UsedCondition', | |
113 | + 'availability'=> 'http://schema.org/InStock', | |
114 | + 'seller'=> [ | |
115 | + 'type'=> 'Organization', | |
116 | + 'name'=>'Executive Objects'] | |
117 | + ] | |
118 | + ]; | |
119 | + | |
120 | + | |
121 | + #Для страниц услуг где представлены 2 и больше цен | |
122 | + $layout2= | |
123 | + [ | |
124 | + '@context'=> 'http://schema.org/', | |
125 | + '@type'=> 'Product', | |
126 | + 'name'=> 'Executive Anvil', | |
127 | + 'image'=> | |
128 | + [ | |
129 | + 'https://example.com/photos/1x1/photo.jpg', | |
130 | + 'https://example.com/photos/4x3/photo.jpg', | |
131 | + 'https://example.com/photos/16x9/photo.jpg' | |
132 | + ], | |
133 | + 'brand'=> | |
134 | + [ | |
135 | + '@type'=> 'Thing', | |
136 | + 'name'=> 'ACME' | |
137 | + ], | |
138 | + 'aggregateRating'=> | |
139 | + [ | |
140 | + '@type'=> 'AggregateRating', | |
141 | + 'ratingValue'=> '4.4', | |
142 | + 'ratingCount'=> '89' | |
143 | + ], | |
144 | + 'offers' => | |
145 | + [ | |
146 | + '@type'=> 'AggregateOffer', | |
147 | + 'lowPrice'=> '119.99', | |
148 | + 'highPrice'=> '199.99', | |
149 | + 'priceCurrency'=> 'USD' | |
150 | + ] | |
151 | + ]; | |
152 | + | |
153 | + | |
154 | + } | |
155 | + | |
156 | + | |
157 | + | |
158 | + | |
159 | + | |
160 | + | |
161 | + | |
162 | + | |
163 | + | |
164 | +} | |
0 | 165 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | + | |
3 | +use yii\base\BaseObject; | |
4 | + | |
5 | +class Microdata extends BaseObject | |
6 | +{ | |
7 | + public function toJSON(): string | |
8 | + { | |
9 | + return ''; | |
10 | + } | |
11 | +} | |
12 | + | |
13 | +class MircodataFabric | |
14 | +{ | |
15 | + public static function createFromProduct($product): Microdata | |
16 | + { | |
17 | + return new ProductMicrodata(); | |
18 | + } | |
19 | + | |
20 | + public static function createFromArticle($article): Microdata | |
21 | + { | |
22 | + /* | |
23 | + * Generate array for config | |
24 | + */ | |
25 | + | |
26 | + return new ArticleMicrodata(array_merge(['title' => 'article',], self::getCommonConfig())); | |
27 | + } | |
28 | + | |
29 | + protected static function getCommonConfig():array | |
30 | + { | |
31 | + return require('config.php'); | |
32 | + } | |
33 | +} | |
34 | + | |
35 | +class ArticleMicrodata extends Microdata | |
36 | +{ | |
37 | + public $title; | |
38 | + | |
39 | + public function toJSON(): string | |
40 | + { | |
41 | + return 'article json'; | |
42 | + } | |
43 | +} | |
44 | + | |
45 | +class ProductMicrodata extends Microdata | |
46 | +{ | |
47 | + public $price; | |
48 | + | |
49 | + public function toJSON(): string | |
50 | + { | |
51 | + return 'product json'; | |
52 | + } | |
53 | +} | |
54 | + | |
55 | + | |
56 | +/* | |
57 | +* Implementation | |
58 | +* | |
59 | + * MircodataFabric::createFromProduct($product)->toJSON(); | |
60 | +* MircodataFabric::createFromArticle($article)->toJSON(); | |
61 | +*/ | |
0 | 62 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | +namespace frontend\microdata; | |
3 | +/** | |
4 | + * Created by PhpStorm. | |
5 | + * User: alex | |
6 | + * Date: 15.06.18 | |
7 | + * Time: 14:38 | |
8 | + */ | |
9 | + | |
10 | + | |
11 | +class test2 | |
12 | +{ | |
13 | + public $name2='Jack'; | |
14 | + public $sorname2='Daniels'; | |
15 | + | |
16 | + | |
17 | + public function getData2() | |
18 | + { | |
19 | + return ['name2'=>$this->name2,'sorname2'=>$this->sorname2]; | |
20 | + } | |
21 | +} | |
0 | 22 | \ No newline at end of file | ... | ... |
frontend/views/layouts/main.php
1 | 1 | <?php |
2 | - | |
2 | + | |
3 | 3 | /** |
4 | 4 | * @var View $this |
5 | 5 | * @var string $content |
... | ... | @@ -73,12 +73,15 @@ |
73 | 73 | ); |
74 | 74 | $module = \Yii::$app->getModule('feedback'); |
75 | 75 | $aliases = Alias::find()->where(['route' => '{"0":"site/questions"}'])->indexBy('route')->andWhere(['language_id' => Language::getCurrent()->id])->asArray()->all(); |
76 | + | |
76 | 77 | ?> |
77 | 78 | |
78 | 79 | <?php $this->beginPage() ?> |
79 | 80 | <!DOCTYPE html > |
80 | 81 | <html xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html" lang="<?= \Yii::$app->language ?>"> |
81 | 82 | <head> |
83 | + | |
84 | + <meta charset="utf-8"> | |
82 | 85 | <!-- Global site tag (gtag.js) - Google Analytics --> |
83 | 86 | <script async src="https://www.googletagmanager.com/gtag/js?id=UA-120861020-1"></script> |
84 | 87 | <script> | ... | ... |
frontend/views/service/view.php
... | ... | @@ -52,6 +52,7 @@ |
52 | 52 | $aliases = Alias::find()->where(['route' => '{"0":"site/questions"}'])->indexBy('route')->andWhere(['language_id' => Language::getCurrent()->id])->asArray()->all(); |
53 | 53 | $this->params[ 'breadcrumbs' ][] = (!empty($seo->h1)) ? $seo->h1 :$model->title; |
54 | 54 | ?> |
55 | + <?=$microdata;?> | |
55 | 56 | <section class="section-service-page"> |
56 | 57 | <div class="container"> |
57 | 58 | <div class="row"> |
... | ... | @@ -74,10 +75,9 @@ |
74 | 75 | if (($model->id == $service->id or $model->parent_id == $service->id) and !empty($service->services)) {?> |
75 | 76 | <ul> |
76 | 77 | <?php foreach ($service->services as $item){?> |
78 | + | |
77 | 79 | <?php if($model->id == $item->id) : ?> |
78 | 80 | <li class="active"><a ><?=$item->title?></a></li> |
79 | - | |
80 | - | |
81 | 81 | <?php else: ?> |
82 | 82 | <li class=""><a href="<?=Url::to(['alias' => $item->language->alias])?>"><?=$item->title?></a></li> |
83 | 83 | <?php endif;?> |
... | ... | @@ -128,6 +128,7 @@ |
128 | 128 | </div> |
129 | 129 | <div class="row"> |
130 | 130 | <div class="col-xs-12 service-text-table-wrapp editor-text"> |
131 | + | |
131 | 132 | <?=$model->body?> |
132 | 133 | </div> |
133 | 134 | </div> | ... | ... |