MicroDataWidget.php 2.97 KB
<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 04.07.18
     * Time: 15:33
     */
    
    namespace frontend\widgets;
    
    use artbox\core\helpers\ImageHelper;
    use frontend\helpers\Url;
    use yii\base\Widget;
    use yii\helpers\Json;
    
    class MicroDataWidget extends Widget
    {
        public $model = null;
        
        public $seo;
        
        public $logoUrl;
        
        public $type = 'Organization';
        
        public $width;
        
        public $height;
        
        public function run()
        {
            $this->seo = \Yii::$app->get('seo');
            $data = [];
            $this->logoUrl = \Yii::$app->request->hostInfo . '/storage/logo/logo.png';
            if ($this->type == 'NewsArticle'){
                $data = [
                    '@context'         => "http://schema.org",
                    '@type'            => $this->type,
                    "mainEntityOfPage" => [
                        "@type" => "WebPage",
                        "@id"   => \Yii::$app->request->absoluteUrl,
                    ],
                    'headline'         => ( !empty($this->seo->h1) ) ? $this->seo->h1 : $this->model->language->title,
                    'image'            => [
                        \Yii::$app->request->hostInfo . ImageHelper::set((!empty($this->model->image)) ? $this->model->image->getUrl() : null)
                                                                               ->render(),
                    ],
                    'datePublished'    => date('c', $this->model->created_at),
                    'dateModified'     => date('c', $this->model->updated_at),
                    'author'           => [
                        '@type' => 'Person',
                        'name'  => 'admin',
                    ],
                    'publisher'        => [
                        '@type' => 'Organization',
                        'name'  => 'admin',
                        'logo'  => [
                            '@type'  => "ImageObject",
                            'url'    => $this->logoUrl,
                        ],
                    ],
                    'description'      => $this->seo->desc,
                ];
            }elseif ($this->type == 'Organization'){
                  $data = [
                       '@context' => 'http://schema.org',
                       "@type"=> $this->type,
                       'url' => \Yii::$app->request->absoluteUrl,
                       'logo' => $this->logoUrl,
                       'contactPoint' => [
                           "@type" => "ContactPoint",
                            "url" => Url::home(true),
                            "contactType" => "customer service",
                       ]
                  ];
            }
           
            
            return $this->render(
                'microdata',
                [
                    'data' => Json::encode($data),
                ]
            );
        }
    }