diff --git a/backend/components/Sitemap.php b/backend/components/Sitemap.php new file mode 100755 index 0000000..4ab610d --- /dev/null +++ b/backend/components/Sitemap.php @@ -0,0 +1,532 @@ +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; + } + + } \ No newline at end of file diff --git a/common/models/LangRedirect.php b/common/models/LangRedirect.php new file mode 100644 index 0000000..f3eceb1 --- /dev/null +++ b/common/models/LangRedirect.php @@ -0,0 +1,33 @@ +request->url == '/'.$language->url and $language->default){ + $this->link = ''; + return true; + }else{ + return false; + } + } + public function getLink(): string + { + return $this->link; + } + } \ No newline at end of file diff --git a/frontend/config/main.php b/frontend/config/main.php index 760794e..5eb55fb 100644 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -2,6 +2,7 @@ use artbox\core\components\LanguageRequest; use artbox\core\components\SeoUrlManager; + use common\models\LangRedirect; $params = array_merge( require( __DIR__ . '/../../common/config/params.php' ), @@ -54,6 +55,7 @@ 'errorAction' => 'site/error', ], 'urlManager' => [ + 'baseUrl' => '/', 'class' => SeoUrlManager::className(), 'enablePrettyUrl' => true, 'forceRedirect' => true, @@ -77,6 +79,9 @@ 'site/contact', ], + 'redirects' => [ + LangRedirect::className(), + ], ], 'assetsAutoCompress' => [ 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent', -- libgit2 0.21.4