SiteMapController.php 8.72 KB
<?php
    
    namespace console\controllers;
    
    use common\models\Articles;
    use common\models\Seo;
    use common\modules\product\models\Category;
    use common\modules\product\models\Product;
    use common\modules\rubrication\models\TaxOption;
    use frontend\models\ProductFrontendSearch;
    use Yii;
    use common\models\Page;
    use yii\helpers\ArrayHelper;
    use yii\helpers\Console;
    use yii\helpers\Url;
    use yii\console\Controller;
    
    /**
     * PageController implements the CRUD actions for Page model.
     */
    class SiteMapController extends Controller
    {
        
        private $urlList = [ 'http://www.linija-svitla.ua/' ];
        private $count = 1;
        
        public function checkFilter($category, $filter)
        {
            $productModel = new ProductFrontendSearch();
            $productProvider = $productModel->search($category, $filter);
            if (!empty($productProvider->models)) {
                return true;
            } else {
                return false;
            }
        }
        
        /**
         * @return array
         */
        public function getAddStatic()
        {
            return [
                'https://www.linija-svitla.ua',
            ];
        }
        
        /**
         * @return array|\yii\db\ActiveRecord[]
         */
        public function getProducts()
        {
            return Product::find()
                          ->all();
            
        }
        
        /**
         * @return array|\yii\db\ActiveRecord[]
         */
        public function getSeoLinks()
        {
            return Seo::find()
                      ->where(
                          [
                              'not like',
                              'meta',
                              '%noindex%',
                          ]
                      )
                      ->all();
            
        }
        
        /**
         * @return array|\yii\db\ActiveRecord[]
         */
        public function getStaticPages()
        {
            return Page::find()
                       ->all();
        }
        
        /**
         * @return array|\common\modules\product\models\Category[]|\yii\db\ActiveRecord[]
         */
        public function getCategories()
        {
            return Category::find()
                           ->all();
        }
        
        /**
         * @return array|\yii\db\ActiveRecord[]
         */
        public function getArticles()
        {
            return Articles::find()
                           ->all();
        }
        
        /**
         * @param $category
         *
         * @return mixed
         */
        public function getBrands($category)
        {
            
            return $category->brands;
        }
        
        /**
         * @return array|\yii\db\ActiveRecord[]
         */
        public function getOptions()
        {
            
            return TaxOption::find()
                            ->innerJoinWith(
                                [
                                    'taxGroup.category',
                                ]
                            )
                            ->where(
                                [
                                    'tax_group.is_filter' => true,
                                ]
                            )
                            ->all();
            
        }
        
        public function checkUrl($url)
        {
            if (!in_array($url, $this->urlList)) {
                $this->urlList[] = $url;
                return true;
            } else {
                return false;
            }
        }
        
        public function createRow($url, $priority, &$content)
        {
            //        if($this->checkUrl($url)){
            $this->stdout('# ' . $this->count++ . ' ' . $url . "\n", Console::FG_GREEN);
            $content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
                    'Y-m-d'
                ) . '</lastmod>' . '<changefreq>Weekly</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
            //        }
        }
        
        public function actionProcess()
        {
            
            $config = ArrayHelper::merge(
                require( __DIR__ . '/../../frontend/config/main.php' ),
                require( __DIR__ . '/../../common/config/main.php' )
            
            );
            
            Yii::$app->urlManager->addRules($config[ 'components' ][ 'urlManager' ][ 'rules' ]);
            
            $this->stdout('Start!' . "\n", Console::FG_RED);
            
            $dirName = Yii::getAlias('@frontend') . '/web';
            
            $filename = 'sitemap.xml';
            
            setlocale(LC_ALL, 'ru_RU.CP1251');
            $handle = fopen($dirName . '/' . $filename, "w");
            
            $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
            
            $this->stdout('Add static' . "\n", Console::FG_BLUE);
            
            foreach ($this->getAddStatic() as $page) {
                $this->createRow($page, 1, $content);
            }
            $this->stdout('Add static pages' . "\n", Console::FG_BLUE);
            
            foreach ($this->getStaticPages() as $page) {
                $url = Url::to(
                    [
                        'text/main',
                        'translit' => $page->translit,
                    ],
                    true
                );
                
                $this->createRow($url, 0.5, $content);
            }
            $this->stdout('Add categories' . "\n", Console::FG_BLUE);
            
            foreach ($this->getCategories() as $category) {
                
                $url = Url::to(
                    [
                        'catalog/category',
                        'category' => $category,
                    ],
                    true
                );
                $this->createRow($url, 0.9, $content);
            }
            
            $this->stdout('Add products' . "\n", Console::FG_BLUE);
            
            foreach ($this->getProducts() as $product) {
                
                $url = Url::to(
                    [
                        'catalog/product',
                        'product' => $product,
                    ],
                    true
                );
                $this->createRow($url, 0.7, $content);
            }
            
            $this->stdout('Add articles' . "\n", Console::FG_BLUE);
            
            foreach ($this->getArticles() as $article) {
                
                $url = Url::to(
                    [
                        'articles/show',
                        'translit' => $article->translit,
                    ],
                    true
                );
                $this->createRow($url, 0.7, $content);
                
            }
            
            $this->stdout('Add brands' . "\n", Console::FG_BLUE);
            
            foreach ($this->getCategories() as $category) {
                foreach ($this->getBrands($category) as $brand) {
                    if ($this->checkFilter($category, [ 'brands' => [ $brand->brand_id ] ])) {
                        $url = Url::to(
                            [
                                'catalog/category',
                                'category' => $category,
                                'filters'  => [ 'brands' => [ $brand->alias ] ],
                            ],
                            true
                        );
                        $this->createRow($url, 0.8, $content);
                    }
                }
            }
            
            $this->stdout('Add filters' . "\n", Console::FG_BLUE);
            
            foreach ($this->getOptions() as $option) {
                $url = Url::to(
                    [
                        'catalog/category',
                        'category' => $option->taxGroup->category,
                        'filters'  => [ $option->taxGroup->alias => [ $option->alias ] ],
                    ],
                    true
                );
                $this->createRow($url, 0.8, $content);
            }
            
            $this->stdout('Add seo links' . "\n", Console::FG_BLUE);
            
            foreach ($this->getSeoLinks() as $link) {
                $url = Yii::$app->urlManager->baseUrl . $link->url;
                $this->createRow($url, 0.8, $content);
                
            }
            
            $content .= '</urlset>';
            
            fwrite($handle, $content);
            fclose($handle);
            $this->stdout('File name: ' . $dirName . '/' . $filename . "\n", Console::FG_CYAN);
            
        }
        
    }