SiteMapController.php 7.85 KB
<?php
    
    namespace artweb\artbox\ecommerce\console;
    
    use artweb\artbox\ecommerce\models\Brand;
    use artweb\artbox\ecommerce\models\ProductVariant;
    use artweb\artbox\seo\models\Seo;
    use artweb\artbox\ecommerce\models\Category;
    use Yii;
    use artweb\artbox\models\Page;
    use yii\helpers\ArrayHelper;
    use yii\helpers\Console;
    use yii\helpers\Url;
    use yii\console\Controller;
    
    class SiteMapController extends Controller
    {
        
        private $urlList = [ 'https://dev.extremstyle.ua/ru' ];
        private $count = 1;
        public $fileName;
        public $handle;
        public $mapNumber = 1;
        public $mainMap = '';
        
        public function getAddStatic()
        {
            return [
                'https://dev.extremstyle.ua/ru',
            ];
        }
        
        public function getVariants()
        {
            return ProductVariant::find()
                                 ->with('lang')
                                 ->with('product.lang')
                                 ->batch(10000);
            
        }
        
        public function getSeoLinks()
        {
            return Seo::find()
                      ->where([ 'meta' => '' ])
                      ->all();
            
        }
        
        public function getStaticPages()
        {
            return Page::find()
                       ->with('lang')
                       ->all();
        }
        
        public function getCategories()
        {
            return Category::find()
                           ->with('lang')
                           ->all();
        }
        
        public function getCategoriesWithFilters()
        {
            return Category::find()
                           ->with('lang')
                           ->joinWith('taxGroups.lang')
                           ->with('taxGroups.taxOptions.lang')
                           ->where([ 'tax_group.meta_robots' => '', ])
                           ->all();
        }
        
        public function getBrands()
        {
            return Brand::find()
                        ->joinWith('lang')
                        ->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 . " : ", Console::BOLD);
                $this->stdout($url . "\n", Console::FG_YELLOW);
                $content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
                        'Y-m-d'
                    ) . '</lastmod>' . '<changefreq>Weekly</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
                $this->count++;
                if ($this->count % 40000 == 0) {
                    $content .= '</urlset>';
                    
                    fwrite($this->handle, $content);
                    fclose($this->handle);
                    unset($content);
                    $this->mapNumber++;
                    
                    $this->mainMap .= '<sitemap>'.
                        '<loc>https://dev.extremstyle.ua/ru/' . $this->fileName . '</loc>'.
                        '<lastmod>' . date('Y-m-d') . '</lastmod>'.
                        '</sitemap>';
                    
                    $this->fileName = 'sitemap' . $this->mapNumber . '.xml';
                    $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
                    
                    $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
                }
            }
        }
        
        public function actionProcess()
        {
            $config = ArrayHelper::merge(
                require( \Yii::getAlias('@frontend/config/') . 'main.php' ),
                require( \Yii::getAlias('@common/config/') . 'main.php' )
            );
            
            Yii::$app->urlManager->addRules($config[ 'components' ][ 'urlManager' ][ 'rules' ]);
            $this->mainMap = '<?xml version="1.0" encoding="UTF-8"?>';
            $this->mainMap .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
            $this->fileName = 'test_sitemap' . $this->mapNumber . '.xml';
            setlocale(LC_ALL, 'ru_RU.CP1251');
            $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
            
            $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
            
            foreach ($this->getAddStatic() as $page) {
                $this->createRow($page, 1, $content);
            }
            
            foreach ($this->getStaticPages() as $page) {
                $url = Url::to(
                    [
                        'site/page',
                        'slug' => $page->lang->alias,
                    ]
                );
                $this->createRow($url, 1, $content);
            }
            
            foreach ($this->getCategories() as $category) {
                $url = Url::to(
                    [
                        'catalog/category',
                        'category' => $category->lang->alias,
                    ]
                );
                $this->createRow($url, 0.8, $content);
            }
            
            foreach ($this->getVariants() as $rows) {
                foreach ($rows as $row) {
                    $url = Url::to(
                        [
                            'catalog/product',
                            'product' => $row->product->lang->alias,
                            'variant' => $row->sku,
                        ]
                    );
                    $this->createRow($url, 0.9, $content);
                }
            }
            
            foreach ($this->getBrands() as $brand) {
                
                $url = Url::to(
                    [
                        'brand/view',
                        'slug' => $brand->lang->alias,
                    ]
                );
                $this->createRow($url, 0.7, $content);
                
            }
            
            foreach ($this->getCategoriesWithFilters() as $category) {
                foreach ($category->taxGroups as $group) {
                    foreach ($group->taxOptions as $option) {
                        $url = Url::to(
                            [
                                'catalog/category',
                                'category' => $category,
                                'filters'  => [ $group->lang->alias => [ $option->lang->alias ] ],
                            ]
                        );
                        $this->createRow($url, 0.8, $content);
                    }
                    
                }
            }
            
            foreach ($this->getSeoLinks() as $link) {
                $url = Yii::$app->urlManager->baseUrl . $link->url;
                $this->createRow($url, 0.7, $content);
                
            }
            
            $content .= '</urlset>';
            
            fwrite($this->handle, $content);
            fclose($this->handle);
    
            $this->mainMap .= '<sitemap>'.
                '<loc>https://dev.extremstyle.ua/ru/' . $this->fileName . '</loc>'.
                '<lastmod>' . date('Y-m-d') . '</lastmod>'.
                '</sitemap>'.
                '</sitemapindex>';
            
            $mainHandle = fopen(Yii::getAlias('@frontend') . '/web/sitemap.xml', "w");
            fwrite($mainHandle, $this->mainMap);
            fclose($mainHandle);
            
            print Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName;
        }
        
    }