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
frontend/microdata/MicrodataFabric.php
... | ... | @@ -23,6 +23,18 @@ class MicrodataFabric extends Microdata |
23 | 23 | : |
24 | 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 | 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 | 91 | \ No newline at end of file | ... | ... |
frontend/views/layouts/main.php
... | ... | @@ -29,6 +29,11 @@ |
29 | 29 | use yii\helpers\Html; |
30 | 30 | use yii\web\View; |
31 | 31 | use yii\helpers\Url as DefaultUrl; |
32 | + use frontend\microdata\MicrodataFabric; | |
33 | + | |
34 | + | |
35 | + | |
36 | + | |
32 | 37 | |
33 | 38 | AppAsset::register($this); |
34 | 39 | SliderAsset::register($this); |
... | ... | @@ -320,7 +325,7 @@ $this->registerMetaTag( |
320 | 325 | </nav> |
321 | 326 | <div class="mobile-menu-hidden" style="display: none;"> |
322 | 327 | <div class="close-lang-mob"> |
323 | -<?php //echo LanguageWidget::widget([ 'mobile' => true ]) ?> | |
328 | + | |
324 | 329 | <div class="close-mobile-menu"></div> |
325 | 330 | </div> |
326 | 331 | <div class="mobi-menu-catalog"> |
... | ... | @@ -426,7 +431,12 @@ $this->registerMetaTag( |
426 | 431 | <div class="col-xs-12"> |
427 | 432 | <div class="map-info-wrapp"> |
428 | 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 | 440 | <?php if (!empty($settings->phone) or !empty($settings->phone2)){ |
431 | 441 | $phones = array_merge(explode(';', $settings->phone2) , [$settings->phone]); |
432 | 442 | ?> |
... | ... | @@ -501,7 +511,7 @@ $this->registerMetaTag( |
501 | 511 | <li><a target="_blank" href="<?=$settings->google?>"><img src="/images/s-2.svg" alt=""></a></li> |
502 | 512 | <?php } ?> |
503 | 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 | 515 | <?php }?> |
506 | 516 | </ul> |
507 | 517 | </div> |
... | ... | @@ -522,8 +532,7 @@ $this->registerMetaTag( |
522 | 532 | <div style="display: none" class="btn_call_close"></div> |
523 | 533 | </footer> |
524 | 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 | 536 | <div class="call-mobile-wr" style="display: none;"> |
528 | 537 | <div class="call-mobile"> |
529 | 538 | <div class="call-mobile-td"> |
... | ... | @@ -536,10 +545,10 @@ $this->registerMetaTag( |
536 | 545 | <?php } ?> |
537 | 546 | |
538 | 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 | 549 | </div> |
541 | 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 | 552 | </div> |
544 | 553 | </div> |
545 | 554 | </div> |
... | ... | @@ -607,7 +616,57 @@ $this->registerMetaTag( |
607 | 616 | <div class="txt-success"><span><?=\Yii::t('app','Thanks for request')?></span> |
608 | 617 | <p><?=Yii::t('app','We`ll call you later')?>.</p></div> |
609 | 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 | 672 | <?php $this->endBody() ?> | ... | ... |