get('urlManagerFrontend'); return $urlManager->createAbsoluteUrl('/' . $this->url); } /** * Check whether sitemap.xml exist * * @return bool */ public function checkFileExist(): bool { return file_exists(\Yii::getAlias($this->path)); } /** * Generate sitemap XML in $path * * @return bool */ public function generateXML(): bool { return $this->saveXML($this->generateOneShot()); } /** * Save generated xml to $path file * * @param string $xml * * @return bool */ protected function saveXML(string $xml): bool { $realpath = \Yii::getAlias($this->path); if (file_put_contents($realpath, $xml)) { return true; } else { return false; } } /** * Generate xml from configs * * @return string */ public function generateOneShot(): string { /** * @var UrlManager $urlManager */ $urlManager = \Yii::$app->get('urlManagerFrontend'); $content = ''; $content .= ''; /** * @var SitemapStatic[] $static */ /** * Main page */ $content .= Html::tag( 'url', Html::tag('loc', Url::to("/", true)) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( 'changefreq', 'Always' ) . Html::tag('priority', 1) ) . PHP_EOL; /** * Products */ $products = Product::find() ->joinWith( [ 'lang.alias', 'image', 'variant', ] ) ->innerJoinWith('category') ->where( [ '!=', 'variant.price', 0, ] ) ->where( [ '!=', 'variant.stock', 0, ] ) ->asArray() ->all(); foreach ($products as $key => $item) { /** * @var \common\models\Product $item */ $content .= Html::tag( 'url', Html::tag('loc', Url::to($item[ 'lang' ][ 'alias' ][ 'value' ], true)) . Html::tag( 'lastmod', date('Y-m-d') ) . Html::tag( 'changefreq', 'Weekly' ) . ( !empty($item[ 'image' ]) ? Html::tag( 'image:image', Html::tag( 'image:loc', Url::to( "/", true ) . 'storage/' . $item[ 'image' ][ 'id' ] . '_' . $item[ 'image' ][ 'fileHash' ] . '.' . pathinfo( $item[ 'image' ][ 'fileName' ], PATHINFO_EXTENSION ) ) ) : '' ) . Html::tag('priority', 0.6) ) . PHP_EOL; } unset($products); /** * Pages */ $pages = Page::find() ->where([ 'in_menu' => true ]) ->with('lang.alias') ->asArray() ->all(); foreach ($pages as $key => $item) { $content .= Html::tag( 'url', Html::tag('loc', Url::to($item[ 'lang' ][ 'alias' ][ 'value' ], true)) . Html::tag( 'lastmod', date('Y-m-d') ) . Html::tag( 'changefreq', 'Weekly' ) . Html::tag('priority', 0.6) ) . PHP_EOL; } unset($pages); /** * Blog articles */ // $blog = Alias::find()->where(['entity' => 'artbox\weblog\models\CategoryLang'])->orWhere(['entity' => 'artbox\weblog\models\TagLang'])->orWhere(['entity'=> 'artbox\weblog\models\ArticleLang'])->all(); // foreach ($blog as $key => $item){ // $content .= Html::tag( // 'url', // Html::tag('loc', Url::to((($item->entity == 'artbox\weblog\models\ArticleLang')? 'articles/' : '').$item->value, true)) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( // 'changefreq', // 'Weekly' // ) . Html::tag('priority', ($item->entity == 'artbox\weblog\models\ArticleLang') ? 0.7 : 0.6) // ); // } $articles = Article::find() ->where([ 'status' => true ]) ->with('lang.alias') ->asArray() ->all(); foreach ($articles as $article) { /** * @var Article $article */ $content .= Html::tag( 'url', Html::tag( 'loc', Url::to('articles/' . $article[ 'lang' ][ 'alias' ][ 'value' ], true) ) . Html::tag( 'lastmod', date('Y-m-d') ) . Html::tag( 'changefreq', 'Weekly' ) . Html::tag('priority', 0.5) ) . PHP_EOL; } unset($articles); /** * Filters */ $open_filters = Alias::find() ->where([ 'entity' => 'artbox\catalog\models\Filter' ]) ->asArray() ->all(); foreach ($open_filters as $key => $item) { $content .= Html::tag( 'url', Html::tag('loc', Url::to($item[ 'value' ], true)) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( 'changefreq', 'daily' ) . Html::tag('priority', 0.8) ) . PHP_EOL; } /** * Brands */ $brands = Brand::find() ->where([ 'status' => 1 ]) ->with( [ 'image', 'lang.alias', ] ) ->asArray() ->all(); foreach ($brands as $item) { $content .= Html::tag( 'url', Html::tag('loc', Url::to($item[ 'lang' ][ 'alias' ][ 'value' ], true)) . Html::tag( 'lastmod', date('Y-m-d') ) . Html::tag( 'changefreq', 'Daily' ) . ( !empty($item[ 'image' ]) ? Html::tag( 'image:image', Html::tag( 'image:loc', Url::to( "/", true ) . 'storage/' . $item[ 'image' ][ 'id' ] . '_' . $item[ 'image' ][ 'fileHash' ] . '.' . pathinfo( $item[ 'image' ][ 'fileName' ], PATHINFO_EXTENSION ) ) ) : '' ) . Html::tag('priority', 0.7) ) . PHP_EOL; } unset($brands); /** * category level 1 */ $category = Category::find() ->with([ 'lang.alias' ]) ->join('INNER JOIN', [ 'c' => 'category' ], 'c.parent_id = category.id') ->andWhere([ 'category.level' => 1 ]) ->asArray() ->all(); foreach ($category as $key => $item) { $content .= Html::tag( 'url', Html::tag( 'loc', Url::to('category/' . $item[ 'lang' ][ 'alias' ][ 'value' ], true) ) . Html::tag( 'lastmod', date('Y-m-d') ) . Html::tag( 'changefreq', 'Daily' ) . Html::tag('priority', 0.9) ) . PHP_EOL; } unset($category); /** * Other filters * * @var FilterHelper $filterHelper */ $filterHelper = \Yii::$app->get('filter'); $category = Category::find() ->with( [ 'lang.alias', 'categories', ] ) ->where( [ 'level' => 2, ] ) ->orWhere([ 'level' => 1 ]) ->asArray() ->all(); $rewritedFilters = $filterHelper->filterObj->getReplacedFilters(); foreach ($category as $key => $item) { if ($item[ 'level' ] == 2 or ( count($item[ 'categories' ]) == 0 and $item[ 'level' ] == 1 )) { $content .= Html::tag( 'url', Html::tag( 'loc', Url::to('catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ], true) ) . Html::tag( 'lastmod', date('Y-m-d') ) . Html::tag( 'changefreq', 'Daily' ) . Html::tag('priority', 0.9) ) . PHP_EOL; } else { continue; } $category_id = $item[ 'id' ]; $option_ids = ( new Query() )->select('product_option_excl.id') ->from('product_option_excl') ->innerJoin( 'product_option_group_excl', 'product_option_excl.product_option_group_excl_id = product_option_group_excl.id' ) ->innerJoin( 'product_option_group_excl_to_category', 'product_option_group_excl.id = product_option_group_excl_to_category.product_option_group_excl_id' ) ->where( [ 'product_option_group_excl_to_category.category_id' => $category_id ] ) ->andWhere([ 'product_option_group_excl_to_category.is_filter' => true ]) ->andWhere( [ 'product_option_excl.id' => ( new Query() )->select( 'product_to_product_option_excl.product_option_excl_id' ) ->from( 'product_to_product_option_excl' ) ->innerJoin( 'product', 'product_to_product_option_excl.product_id = product.id' ) ->innerJoin( 'product_to_category', 'product.id = product_to_category.product_id' ) ->where( [ 'product_to_category.category_id' => $category_id ] ), ] ) ->column(); $options = ProductOptionExcl::find() ->with('lang.alias') ->with('group.lang.alias') ->where([ 'id' => $option_ids ]) ->asArray() ->all(); foreach ($options as $option) { if (strpos($option[ 'group' ][ 'lang' ][ 'alias' ][ 'robots' ], 'noindex') !== false) { continue; } $link = 'catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ] . '/' . $option[ 'lang' ][ 'alias' ][ 'value' ]; if (array_key_exists('/' . $link, $rewritedFilters)) { continue; } $content .= Html::tag( 'url', Html::tag( 'loc', Url::to( $link, true ) ) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( 'changefreq', 'Daily' ) . Html::tag('priority', 0.8) ) . PHP_EOL; } // VARIANTS $option_ids = ( new Query() )->select('variant_option_excl.id') ->from('variant_option_excl') ->innerJoin( 'variant_option_group_excl', 'variant_option_excl.variant_option_group_excl_id = variant_option_group_excl.id' ) ->innerJoin( 'variant_option_group_excl_to_category', 'variant_option_group_excl.id = variant_option_group_excl_to_category.variant_option_group_excl_id' ) ->where( [ 'variant_option_group_excl_to_category.category_id' => $category_id ] ) ->andWhere([ 'variant_option_group_excl_to_category.is_filter' => true ]) ->andWhere( [ 'variant_option_excl.id' => ( new Query() )->select( 'variant_to_variant_option_excl.variant_option_excl_id' ) ->from( 'variant_to_variant_option_excl' ) ->innerJoin( 'variant', 'variant_to_variant_option_excl.variant_id = variant.id' ) ->innerJoin( 'product_to_category', 'variant.product_id = product_to_category.product_id' ) ->where( [ 'product_to_category.category_id' => $category_id ] ), ] ) ->column(); $options = VariantOptionExcl::find() ->with('lang.alias') ->with('group.lang.alias') ->where([ 'id' => $option_ids ]) ->asArray() ->all(); foreach ($options as $option) { if (strpos($option[ 'group' ][ 'lang' ][ 'alias' ][ 'robots' ], 'noindex') !== false) { continue; } $link = 'catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ] . '/' . $option[ 'lang' ][ 'alias' ][ 'value' ]; if (array_key_exists('/' . $link, $rewritedFilters)) { continue; } $content .= Html::tag( 'url', Html::tag( 'loc', Url::to( $link, true ) ) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( 'changefreq', 'Daily' ) . Html::tag('priority', 0.8) ) . PHP_EOL; } $brands_ids = ( new Query() )->select('brand.id') ->from('brand') ->innerJoin('product', 'product.brand_id = brand.id') ->innerJoin( 'product_to_category', 'product.id = product_to_category.product_id' ) ->andWhere([ 'product_to_category.category_id' => $item[ 'id' ] ]) ->groupBy('brand.id') ->column(); $brands = Brand::find() ->with([ 'lang.alias' ]) ->where([ 'status' => true ]) ->andWhere([ 'id' => $brands_ids ]) ->asArray() ->all(); foreach ($brands as $brand) { $link = 'catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ] . '/' . $brand[ 'lang' ][ 'alias' ][ 'value' ]; $content .= Html::tag( 'url', Html::tag( 'loc', Url::to( $link, true ) ) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( 'changefreq', 'Daily' ) . Html::tag('priority', 0.8) ) . PHP_EOL; } } $content .= ''; return $content; } }