From cfb947e6faac20bdda5a8077becb598055ddf648 Mon Sep 17 00:00:00 2001 From: dozer111 Date: Fri, 15 Jun 2018 17:01:47 +0300 Subject: [PATCH] Микроданные для услуг --- frontend/controllers/NewController.php | 22 ++++++++++++++++++++++ frontend/controllers/ServiceController.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- frontend/microdata/ArticleMicrodata.php | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/microdata/Microdata.php | 19 +++++++++++++++++++ frontend/microdata/MicrodataFabric.php | 32 ++++++++++++++++++++++++++++++++ frontend/microdata/MicrodataInterface.php | 11 +++++++++++ frontend/microdata/ProductMicrodata.php | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/microdata/settings/settings.php | 7 +++++++ frontend/microdata/test.php | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/microdata/test2.php | 21 +++++++++++++++++++++ frontend/views/layouts/main.php | 5 ++++- frontend/views/service/view.php | 5 +++-- 12 files changed, 545 insertions(+), 5 deletions(-) create mode 100644 frontend/controllers/NewController.php create mode 100644 frontend/microdata/ArticleMicrodata.php create mode 100644 frontend/microdata/Microdata.php create mode 100644 frontend/microdata/MicrodataFabric.php create mode 100644 frontend/microdata/MicrodataInterface.php create mode 100644 frontend/microdata/ProductMicrodata.php create mode 100644 frontend/microdata/settings/settings.php create mode 100644 frontend/microdata/test.php create mode 100644 frontend/microdata/test2.php diff --git a/frontend/controllers/NewController.php b/frontend/controllers/NewController.php new file mode 100644 index 0000000..5373264 --- /dev/null +++ b/frontend/controllers/NewController.php @@ -0,0 +1,22 @@ +toJson()); + die; + } +} \ No newline at end of file diff --git a/frontend/controllers/ServiceController.php b/frontend/controllers/ServiceController.php index d1bcee5..ecae167 100644 --- a/frontend/controllers/ServiceController.php +++ b/frontend/controllers/ServiceController.php @@ -13,6 +13,7 @@ use yii\db\Query; use yii\web\Controller; use yii\web\NotFoundHttpException; + use frontend\microdata\MicrodataFabric; class ServiceController extends Controller { @@ -28,11 +29,62 @@ }else{ $others = Service::find()->where(['parent_id' => (new Query())->select('parent_id')->from('service')->where(['id' => $model->parent_id])])->with('services.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']}'", + 'image'=> [ + 'https://example.com/photos/1x1/photo.jpg', + 'https://example.com/photos/4x3/photo.jpg', + 'https://example.com/photos/16x9/photo.jpg' + ], + 'offers'=> [ + '@type'=> 'Offer', + 'priceCurrency'=> 'UAH', + 'Price'=> "'".max($prices)."'", + 'priceValidUntil'=> '2020-11-05', + 'itemCondition'=> 'http://schema.org/UsedCondition', + 'availability'=> 'http://schema.org/InStock', + 'seller'=> [ + 'type'=> 'Organization', + 'name'=>'Executive Objects'] + ] + ]; + + + $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 + 'others'=> $others, + 'microdata'=>$pageMicrodata ]); } diff --git a/frontend/microdata/ArticleMicrodata.php b/frontend/microdata/ArticleMicrodata.php new file mode 100644 index 0000000..d357ed6 --- /dev/null +++ b/frontend/microdata/ArticleMicrodata.php @@ -0,0 +1,147 @@ +$value){ + if(!property_exists(self::class,$key)) + { + unset($config[$key]); + } + elseif($value==null || $value===false || $value=='') + { + unset($config[$key]); + } + + + + } + + $this->constructArray=$config; + + parent::__construct($config); + } + + + public function createJson() + { + + } + + + public function toJson(): string + { + $resultJson=" '; + return $resultJson; + } + + + function add($key,$value) + { + if ($value!==null) + $this->constructArray[$key]=$value; + + } + + + + /** + + + + + + + + + + + + + */ + + +} + + diff --git a/frontend/microdata/Microdata.php b/frontend/microdata/Microdata.php new file mode 100644 index 0000000..7c7b421 --- /dev/null +++ b/frontend/microdata/Microdata.php @@ -0,0 +1,19 @@ +123],self::getDefaultSettings())) + : + new ProductMicrodata(array_merge($product,['url'=>123],self::getDefaultSettings())); + } + public static function createJsonFromArticle($article=null) + { + + return ($article==null)? + new ArticleMicrodata(array_merge(['url'=>123],self::getDefaultSettings())) + : + new ArticleMicrodata(array_merge($article,['url'=>123],self::getDefaultSettings())); + } + + + protected static function getDefaultSettings(){ + return require_once (realpath(__DIR__.'/settings/settings.php')); + } + +} \ No newline at end of file diff --git a/frontend/microdata/MicrodataInterface.php b/frontend/microdata/MicrodataInterface.php new file mode 100644 index 0000000..4883d10 --- /dev/null +++ b/frontend/microdata/MicrodataInterface.php @@ -0,0 +1,11 @@ +$value){ + if(!property_exists(self::class,$key)) + { + unset($config[$key]); + } + elseif($value==null || $value===false || $value=='') + { + unset($config[$key]); + } + + + + } + + $this->constructArray=$config; + + parent::__construct($config); + } + + + public function createJson() + { + + } + + + public function toJson(): string + { + $resultJson=" '; + return $resultJson; + } + + + function add($key,$value) + { + if ($value!==null) + $this->constructArray[$key]=$value; + + } + + + public function test() + { + + + #Для страниц услуг где представлена только одна цена + $productArray=[ + 'type'=>'Product', + 'name'=> 'Executive Anvil', + 'image'=> [ + 'https://example.com/photos/1x1/photo.jpg', + 'https://example.com/photos/4x3/photo.jpg', + 'https://example.com/photos/16x9/photo.jpg' + ], + 'description'=>'Sleeker than ACME\'s Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.', + 'mpn'=>'925872', + 'brand'=>[ + 'type'=> 'Thing', + 'name'=> 'ACME' + ], + 'aggregateRating'=> + [ + 'type'=> 'AggregateRating', + 'ratingValue'=> '4.4', + 'reviewCount'=> '89' + ], + 'offers'=> [ + '@type'=> 'Offer', + 'priceCurrency'=> 'USD', + 'Price'=> '199.99', + 'priceValidUntil'=> '2020-11-05', + 'itemCondition'=> 'http://schema.org/UsedCondition', + 'availability'=> 'http://schema.org/InStock', + 'seller'=> [ + 'type'=> 'Organization', + 'name'=>'Executive Objects'] + ] + ]; + + + #Для страниц услуг где представлены 2 и больше цен + $layout2= + [ + '@context'=> 'http://schema.org/', + '@type'=> 'Product', + 'name'=> 'Executive Anvil', + 'image'=> + [ + 'https://example.com/photos/1x1/photo.jpg', + 'https://example.com/photos/4x3/photo.jpg', + 'https://example.com/photos/16x9/photo.jpg' + ], + 'brand'=> + [ + '@type'=> 'Thing', + 'name'=> 'ACME' + ], + 'aggregateRating'=> + [ + '@type'=> 'AggregateRating', + 'ratingValue'=> '4.4', + 'ratingCount'=> '89' + ], + 'offers' => + [ + '@type'=> 'AggregateOffer', + 'lowPrice'=> '119.99', + 'highPrice'=> '199.99', + 'priceCurrency'=> 'USD' + ] + ]; + + + } + + + + + + + + + +} \ No newline at end of file diff --git a/frontend/microdata/settings/settings.php b/frontend/microdata/settings/settings.php new file mode 100644 index 0000000..829fcd0 --- /dev/null +++ b/frontend/microdata/settings/settings.php @@ -0,0 +1,7 @@ +"http://schema.org", +]; \ No newline at end of file diff --git a/frontend/microdata/test.php b/frontend/microdata/test.php new file mode 100644 index 0000000..fc9a72f --- /dev/null +++ b/frontend/microdata/test.php @@ -0,0 +1,61 @@ + 'article',], self::getCommonConfig())); + } + + protected static function getCommonConfig():array + { + return require('config.php'); + } +} + +class ArticleMicrodata extends Microdata +{ + public $title; + + public function toJSON(): string + { + return 'article json'; + } +} + +class ProductMicrodata extends Microdata +{ + public $price; + + public function toJSON(): string + { + return 'product json'; + } +} + + +/* +* Implementation +* + * MircodataFabric::createFromProduct($product)->toJSON(); +* MircodataFabric::createFromArticle($article)->toJSON(); +*/ \ No newline at end of file diff --git a/frontend/microdata/test2.php b/frontend/microdata/test2.php new file mode 100644 index 0000000..bebfc2e --- /dev/null +++ b/frontend/microdata/test2.php @@ -0,0 +1,21 @@ +$this->name2,'sorname2'=>$this->sorname2]; + } +} \ No newline at end of file diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index 5b2b224..fcd6d19 100755 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -1,5 +1,5 @@ getModule('feedback'); $aliases = Alias::find()->where(['route' => '{"0":"site/questions"}'])->indexBy('route')->andWhere(['language_id' => Language::getCurrent()->id])->asArray()->all(); + ?> beginPage() ?> + +