Seo.php 6.66 KB
<?php
namespace frontend\widgets;

use common\models\SeoDynamic;

use common\modules\product\models\Brand;
use common\modules\rubrication\models\TaxOption;
use yii\base\Widget;
use yii\helpers\Html;

class Seo extends Widget
{
    private $url;
    public $row;
    public $own_attr;
    public $fields;
    public $category_name;
    public $description;
    public $title;
    public $meta;
    public $seo_text;
    public $h1;
    public $key;
    public $name;
    public $project_name;


    const SEO_TEXT = 'seo_text';
    const DESCRIPTION = 'description';
    const META = 'meta';
    const H1 = 'h1';
    const TITLE = 'title';

    public function init()
    {
        $this->url = \Yii::$app->request->url;
        $this->project_name = \Yii::$app->name;
        parent::init();

    }


    public function run()
    {

        $seoData = $this->getViewData();
        foreach ($seoData as $key => $value) {
            $this->$key = $value;
        }
 

        switch ($this->row) {
            case self::SEO_TEXT:
                return $this->selectSeoData(self::SEO_TEXT);

                break;
            case self::H1:
                return $this->selectSeoData(self::H1);
                break;
            case self::TITLE:

                $filter = \Yii::$app->request->get('filter', []);
                //        var_dump($filter );die();
//                if(!empty($filter)){
//                    $filter_row = '';
//                    foreach($filter as $sub_filter_name => $sub_filter_array){
//                        if($sub_filter_name=='options'){
//                            foreach($sub_filter_array as $f_name=>$f_values){
//                                $filter_row .= $f_name.':'.implode(',',$f_values).'|';
//                            }
//                        }
//
//                    }
//                    $this->fields['name'] = $filter_row;
//                }

                $title = $this->selectSeoData(self::TITLE);


//                if(!empty($filter_row)){
//                    return $filter_row;
//                }

                 if (!empty($title)) {
                    return $title;
                } else  {
                    return $this->project_name;
                }

                break;
            case self::DESCRIPTION:
                $description = $this->selectSeoData(self::DESCRIPTION);

                if (!empty($description)) {

                    $this->getView()->registerMetaTag([
                        'name' => 'description',
                        'content' => $description
                    ]);

                }

                break;
            case self::META:
                $meta = $this->selectSeoData(self::META);

                $filter = \Yii::$app->request->get('filter', []);
                $sort = \Yii::$app->request->get('sort', []);
                $paginate = \Yii::$app->request->get('page', []);
                if (!empty($meta)) {

                    $this->getView()->registerMetaTag([
                        'name' => 'robots',
                        'content' => $meta
                    ]);

                } else if (
                    isset($filter['brands']) && count($filter['brands']) > 1
                    || isset($filter['options']["pol"]) && count($filter['options']["pol"]) > 1
                    || isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) > 1
                    || isset($filter['options']["god"]) && count($filter['options']["god"]) > 1

                ) {
                    $this->getView()->registerMetaTag([
                        'name' => 'robots',
                        'content' => 'noindex,nofollow'
                    ]);

                } else if (
                    isset($filter['brands']) && count($filter['brands']) <= 1 && isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) >= 4
                    || isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) > 4
                    || !empty($sort) ||  !empty($paginate) || isset($filter['prices'])
                ) {
                    $this->getView()->registerMetaTag([
                        'name' => 'robots',
                        'content' => 'noindex,follow'
                    ]);
                } else {

                    $this->getView()->registerMetaTag([
                        'name' => 'robots',
                        'content' => 'index,follow'
                    ]);
                }


                break;
        }


    }

    protected function replaceData($str)
    {

        if (!empty($this->fields)) {
            foreach ($this->fields as $field_name => $field_value) {
                $str = str_replace('{' . $field_name . '}', $field_value, $str);
            }
        }
        $str = str_replace('{project_name}', $this->project_name, $str);
        return $str;
    }

    protected function findSeoByUrl()
    {
        return \common\models\Seo::findOne(['url' => $this->url]);
    }

    protected function findSeoByDynamic()
    {

        if(!empty($this->key)){

            $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller' => \Yii::$app->controller->id, 'action' => \Yii::$app->controller->action->id, 'key' => $this->key]);
        } else {

            $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller' => \Yii::$app->controller->id, 'action' => \Yii::$app->controller->action->id]);
        }

        return $query->one();
    }


    protected function getViewData()
    {
        $params = $this->getView()->params;
        if (isset($params['seo'])) {
            return $params['seo'];
        } else {
            return [];
        }
    }

    protected function selectSeoData($param)
    {
        $result = '';

        $widgetData = $this->findSeoByUrl();

        if ($widgetData instanceof \common\models\Seo) {

            $result = $widgetData->$param;

        } else if (!empty($this->$param)) {

            $result = $this->$param;
        } else {

            $widgetData = $this->findSeoByDynamic();

            if ($widgetData instanceof SeoDynamic) {

                $result = $widgetData->$param;

            }

        }

        return $this->replaceData($result);

    }

    public function getTitleString($array){
        $template = "{category} {naz} {brand}  {sex} {year} купить в Украине, Киев, Харькове - цены, фото, отзывы | Rukzachok.com.ua";
        foreach ($array as $field_name => $field_value) {
            $template = str_replace('{' . $field_name . '}', $field_value, $template);
        }
        $template =  preg_replace('/\{.[^\}]*\}\s/','',$template);
        return $template;

    }


}