Commit 1e375ebeca0d01870c60a59ace5ed21cf4400cc2
1 parent
3fafe556
тест микродаты организации
Showing
5 changed files
with
170 additions
and
9 deletions
Show diff stats
frontend/controllers/ServiceController.php
frontend/microdata/Microdata.php
| @@ -14,5 +14,6 @@ class Microdata extends BaseObject | @@ -14,5 +14,6 @@ class Microdata extends BaseObject | ||
| 14 | public $type=null; | 14 | public $type=null; |
| 15 | public $image=null; | 15 | public $image=null; |
| 16 | public $offers=null; | 16 | public $offers=null; |
| 17 | + public $context=null; | ||
| 17 | 18 | ||
| 18 | } | 19 | } |
| 19 | \ No newline at end of file | 20 | \ No newline at end of file |
frontend/microdata/MicrodataFabric.php
| @@ -23,6 +23,18 @@ class MicrodataFabric extends Microdata | @@ -23,6 +23,18 @@ class MicrodataFabric extends Microdata | ||
| 23 | : | 23 | : |
| 24 | new ArticleMicrodata(array_merge($article,['url'=>123],self::getDefaultSettings())); | 24 | new ArticleMicrodata(array_merge($article,['url'=>123],self::getDefaultSettings())); |
| 25 | } | 25 | } |
| 26 | + public static function createJsonFromOrganization($article=null) | ||
| 27 | + { | ||
| 28 | + | ||
| 29 | + return ($article==null)? | ||
| 30 | + new OrganizationMicrodata(array_merge(self::getDefaultSettings())) | ||
| 31 | + : | ||
| 32 | + new OrganizationMicrodata(array_merge($article,self::getDefaultSettings())); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + | ||
| 36 | + | ||
| 37 | + | ||
| 26 | 38 | ||
| 27 | 39 | ||
| 28 | protected static function getDefaultSettings(){ | 40 | protected static function getDefaultSettings(){ |
| 1 | +<?php | ||
| 2 | +namespace frontend\microdata; | ||
| 3 | + | ||
| 4 | +use yii\base\BaseObject; | ||
| 5 | + | ||
| 6 | + | ||
| 7 | +class OrganizationMicrodata extends Microdata | ||
| 8 | +{ | ||
| 9 | + | ||
| 10 | + | ||
| 11 | + public $brand=null; | ||
| 12 | + public $aggregateRating=null; | ||
| 13 | + public $offers=null; | ||
| 14 | + public $description=null; | ||
| 15 | + public $npm=null; | ||
| 16 | + | ||
| 17 | + public $url=null; | ||
| 18 | + public $logo=null; | ||
| 19 | + public $contactPoint=null; | ||
| 20 | + public $telephone=null; | ||
| 21 | + public $contactType=null; | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + # массив, который составляет из себя реально существующие пары свойство/значение | ||
| 28 | + # !!! так же тут нету пар с пустым значением | ||
| 29 | + public $constructArray=[]; | ||
| 30 | + | ||
| 31 | + public function __construct(array $config = []) | ||
| 32 | + { | ||
| 33 | + | ||
| 34 | + # обрезаю входящие свойства, работаем только с реально существующими | ||
| 35 | + foreach ($config as $key=>$value){ | ||
| 36 | + if(!property_exists(self::class,$key)) | ||
| 37 | + { | ||
| 38 | + unset($config[$key]); | ||
| 39 | + } | ||
| 40 | + elseif($value==null || $value===false || $value=='') | ||
| 41 | + { | ||
| 42 | + unset($config[$key]); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + $this->constructArray=$config; | ||
| 50 | + | ||
| 51 | + parent::__construct($config); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public function toJson(): string | ||
| 55 | + { | ||
| 56 | + $resultJson=" <script type=\"application/ld+json\">"; | ||
| 57 | + | ||
| 58 | + $finalArray=[]; | ||
| 59 | + foreach ($this->constructArray as $key=>$value) | ||
| 60 | + { | ||
| 61 | + if($key=='type' || $key=='context')$key='@'.$key; | ||
| 62 | + $finalArray[$key]=$value; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + $resultJson1=json_encode($finalArray,JSON_UNESCAPED_UNICODE); | ||
| 66 | + $resultJson1=str_replace('\/','/',$resultJson1); | ||
| 67 | + $resultJson.=$resultJson1; | ||
| 68 | + $resultJson.='</script>'; | ||
| 69 | + | ||
| 70 | + return $resultJson; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + | ||
| 74 | + | ||
| 75 | + function add($key,$value) | ||
| 76 | + { | ||
| 77 | + if ($value!==null) | ||
| 78 | + $this->constructArray[$key]=$value; | ||
| 79 | + | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + | ||
| 85 | + | ||
| 86 | + | ||
| 87 | + | ||
| 88 | + | ||
| 89 | + | ||
| 90 | +} | ||
| 0 | \ No newline at end of file | 91 | \ No newline at end of file |
frontend/views/layouts/main.php
| @@ -29,6 +29,11 @@ | @@ -29,6 +29,11 @@ | ||
| 29 | use yii\helpers\Html; | 29 | use yii\helpers\Html; |
| 30 | use yii\web\View; | 30 | use yii\web\View; |
| 31 | use yii\helpers\Url as DefaultUrl; | 31 | use yii\helpers\Url as DefaultUrl; |
| 32 | + use frontend\microdata\MicrodataFabric; | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + | ||
| 36 | + | ||
| 32 | 37 | ||
| 33 | AppAsset::register($this); | 38 | AppAsset::register($this); |
| 34 | SliderAsset::register($this); | 39 | SliderAsset::register($this); |
| @@ -320,7 +325,7 @@ $this->registerMetaTag( | @@ -320,7 +325,7 @@ $this->registerMetaTag( | ||
| 320 | </nav> | 325 | </nav> |
| 321 | <div class="mobile-menu-hidden" style="display: none;"> | 326 | <div class="mobile-menu-hidden" style="display: none;"> |
| 322 | <div class="close-lang-mob"> | 327 | <div class="close-lang-mob"> |
| 323 | -<?php //echo LanguageWidget::widget([ 'mobile' => true ]) ?> | 328 | + |
| 324 | <div class="close-mobile-menu"></div> | 329 | <div class="close-mobile-menu"></div> |
| 325 | </div> | 330 | </div> |
| 326 | <div class="mobi-menu-catalog"> | 331 | <div class="mobi-menu-catalog"> |
| @@ -426,7 +431,12 @@ $this->registerMetaTag( | @@ -426,7 +431,12 @@ $this->registerMetaTag( | ||
| 426 | <div class="col-xs-12"> | 431 | <div class="col-xs-12"> |
| 427 | <div class="map-info-wrapp"> | 432 | <div class="map-info-wrapp"> |
| 428 | <div class="marker-img"><img src="/images/map/marker-1.png" width="68" height="80" alt=""></div> | 433 | <div class="marker-img"><img src="/images/map/marker-1.png" width="68" height="80" alt=""></div> |
| 429 | - <div class="map-adress"><?=$settings->address?></div> | 434 | + |
| 435 | + | ||
| 436 | + | ||
| 437 | + | ||
| 438 | + | ||
| 439 | + <div class="map-adress"><?=$settings->address;?></div> | ||
| 430 | <?php if (!empty($settings->phone) or !empty($settings->phone2)){ | 440 | <?php if (!empty($settings->phone) or !empty($settings->phone2)){ |
| 431 | $phones = array_merge(explode(';', $settings->phone2) , [$settings->phone]); | 441 | $phones = array_merge(explode(';', $settings->phone2) , [$settings->phone]); |
| 432 | ?> | 442 | ?> |
| @@ -501,7 +511,7 @@ $this->registerMetaTag( | @@ -501,7 +511,7 @@ $this->registerMetaTag( | ||
| 501 | <li><a target="_blank" href="<?=$settings->google?>"><img src="/images/s-2.svg" alt=""></a></li> | 511 | <li><a target="_blank" href="<?=$settings->google?>"><img src="/images/s-2.svg" alt=""></a></li> |
| 502 | <?php } ?> | 512 | <?php } ?> |
| 503 | <?php if (!empty($settings->twitter)){?> | 513 | <?php if (!empty($settings->twitter)){?> |
| 504 | - <li><a target="_blank" href="<?=$settings->twitter?>"><img src="/images/s-3.svg" alt=""></a></li> | 514 | + <li><a target="_blank" href="<?=$settings->twitter ?>"><img src="/images/s-3.svg" alt=""></a></li> |
| 505 | <?php }?> | 515 | <?php }?> |
| 506 | </ul> | 516 | </ul> |
| 507 | </div> | 517 | </div> |
| @@ -522,8 +532,7 @@ $this->registerMetaTag( | @@ -522,8 +532,7 @@ $this->registerMetaTag( | ||
| 522 | <div style="display: none" class="btn_call_close"></div> | 532 | <div style="display: none" class="btn_call_close"></div> |
| 523 | </footer> | 533 | </footer> |
| 524 | <?php if (!empty($settings->phone) or !empty($settings->phone2)){ | 534 | <?php if (!empty($settings->phone) or !empty($settings->phone2)){ |
| 525 | - $phones = array_merge(explode(';', $settings->phone2) , [$settings->phone]); | ||
| 526 | - ?> | 535 | + $phones = array_merge(explode(';', $settings->phone2) , [$settings->phone]); ?> |
| 527 | <div class="call-mobile-wr" style="display: none;"> | 536 | <div class="call-mobile-wr" style="display: none;"> |
| 528 | <div class="call-mobile"> | 537 | <div class="call-mobile"> |
| 529 | <div class="call-mobile-td"> | 538 | <div class="call-mobile-td"> |
| @@ -536,10 +545,10 @@ $this->registerMetaTag( | @@ -536,10 +545,10 @@ $this->registerMetaTag( | ||
| 536 | <?php } ?> | 545 | <?php } ?> |
| 537 | 546 | ||
| 538 | <div class="button-call-mob"> | 547 | <div class="button-call-mob"> |
| 539 | - <a class="call-sms" href="sms:<?= $settings->phone ?>"><?=\Yii::t('app','Send SMS')?></a> | 548 | + <a class="call-sms" href="sms:<?= $settings->phone ?>"><?=\Yii::t('app','Send SMS');?></a> |
| 540 | </div> | 549 | </div> |
| 541 | <div class="button-call-mob"> | 550 | <div class="button-call-mob"> |
| 542 | - <a class="call-contact" href="/contacts/abclinic.vcf"><?=\Yii::t('app','Add to contacts')?></a></div> | 551 | + <a class="call-contact" href="/contacts/abclinic.vcf"><?=\Yii::t('app','Add to contacts');?></a></div> |
| 543 | </div> | 552 | </div> |
| 544 | </div> | 553 | </div> |
| 545 | </div> | 554 | </div> |
| @@ -607,7 +616,57 @@ $this->registerMetaTag( | @@ -607,7 +616,57 @@ $this->registerMetaTag( | ||
| 607 | <div class="txt-success"><span><?=\Yii::t('app','Thanks for request')?></span> | 616 | <div class="txt-success"><span><?=\Yii::t('app','Thanks for request')?></span> |
| 608 | <p><?=Yii::t('app','We`ll call you later')?>.</p></div> | 617 | <p><?=Yii::t('app','We`ll call you later')?>.</p></div> |
| 609 | </div> | 618 | </div> |
| 610 | - <?php # выводить если главная страница ?> | 619 | + <?php |
| 620 | + //---------------------------------------------------------------------------------------- | ||
| 621 | + // микроразметка организации | ||
| 622 | + | ||
| 623 | + | ||
| 624 | + /** | ||
| 625 | + * <script type="application/ld+json"> | ||
| 626 | + { | ||
| 627 | + "@context": "http://schema.org", | ||
| 628 | + "@type": "Organization", | ||
| 629 | + "url": "http://www.your-company-site.com", | ||
| 630 | + "logo": "http://www.example.com/logo.png", | ||
| 631 | + "contactPoint": [{ | ||
| 632 | + "@type": "ContactPoint", | ||
| 633 | + "telephone": "+1-401-555-1212", | ||
| 634 | + "contactType": "customer service" | ||
| 635 | + }] | ||
| 636 | + } | ||
| 637 | + </script> | ||
| 638 | + */ | ||
| 639 | + | ||
| 640 | + $layoutMicrodata= | ||
| 641 | + [ | ||
| 642 | + '@context'=> 'http://schema.org/', | ||
| 643 | + '@type'=> 'Organization', | ||
| 644 | + 'url'=>'http://www.your-company-site.com', | ||
| 645 | + #'logo'=>'http://www.example.com/logo.png', | ||
| 646 | + 'logo'=>Url::home(true)."storage/logo/".$settings->logo, | ||
| 647 | + 'contactPoint'=>[ | ||
| 648 | + '@type'=>'ContactPoint', | ||
| 649 | + 'contactType'=>"customer service" | ||
| 650 | + ], | ||
| 651 | + | ||
| 652 | + ]; | ||
| 653 | + if(isset($phones) && !empty($phones)){foreach ($phones as $key =>$phone) | ||
| 654 | + { | ||
| 655 | + $layoutMicrodata['contactPoint']['telephone'.$key]=$phone; | ||
| 656 | + }} | ||
| 657 | + | ||
| 658 | + | ||
| 659 | + $settings->email; | ||
| 660 | + $microdata=new MicrodataFabric(); | ||
| 661 | + $pageMicrodata=$microdata::createJsonFromOrganization($layoutMicrodata)->toJson(); | ||
| 662 | + echo $pageMicrodata | ||
| 663 | + | ||
| 664 | + | ||
| 665 | + | ||
| 666 | + | ||
| 667 | + ?> | ||
| 668 | + | ||
| 669 | + <?php # выводить если главная страница ?> | ||
| 611 | 670 | ||
| 612 | 671 | ||
| 613 | <?php $this->endBody() ?> | 672 | <?php $this->endBody() ?> |