Commit 14bb41a79a62803b881957d378c99a8ed2420b1b

Authored by Timur Kastemirov
2 parents bd07fbee 1fd00901

Merge remote-tracking branch 'origin/master'

backend/components/Sitemap.php 0 → 100755
  1 +<?php
  2 +
  3 + namespace backend\components;
  4 +
  5 + use artbox\catalog\helpers\FilterHelper;
  6 + use artbox\catalog\models\Brand;
  7 + use artbox\catalog\models\Category;
  8 + use artbox\catalog\models\ProductOptionExcl;
  9 + use artbox\catalog\models\VariantOptionExcl;
  10 + use artbox\core\models\Alias;
  11 + use artbox\core\models\Page;
  12 + use artbox\core\models\SitemapStatic;
  13 + use artbox\weblog\models\Article;
  14 + use common\models\Product;
  15 + use common\models\VariantOptionGroupExcl;
  16 + use yii\base\Object;
  17 + use yii\db\Query;
  18 + use yii\helpers\Html;
  19 + use yii\helpers\Url;
  20 + use yii\web\UrlManager;
  21 +
  22 + class Sitemap extends Object
  23 + {
  24 + public $entities = [];
  25 + public $path = '@frontend/web/sitemap.xml';
  26 + public $url = 'sitemap.xml';
  27 +
  28 + /**
  29 + * Get absolute url to sitemap.xml
  30 + *
  31 + * @return string
  32 + */
  33 + public function getUrl(): string
  34 + {
  35 + /**
  36 + * @var UrlManager $urlManager
  37 + */
  38 + $urlManager = \Yii::$app->get('urlManagerFrontend');
  39 + return $urlManager->createAbsoluteUrl('/' . $this->url);
  40 + }
  41 +
  42 + /**
  43 + * Check whether sitemap.xml exist
  44 + *
  45 + * @return bool
  46 + */
  47 + public function checkFileExist(): bool
  48 + {
  49 + return file_exists(\Yii::getAlias($this->path));
  50 + }
  51 + /**
  52 + * Generate sitemap XML in $path
  53 + *
  54 + * @return bool
  55 + */
  56 + public function generateXML(): bool
  57 + {
  58 + return $this->saveXML($this->generateOneShot());
  59 + }
  60 +
  61 + /**
  62 + * Save generated xml to $path file
  63 + *
  64 + * @param string $xml
  65 + *
  66 + * @return bool
  67 + */
  68 + protected function saveXML(string $xml): bool
  69 + {
  70 + $realpath = \Yii::getAlias($this->path);
  71 + if (file_put_contents($realpath, $xml)) {
  72 + return true;
  73 + } else {
  74 + return false;
  75 + }
  76 + }
  77 +
  78 + /**
  79 + * Generate xml from configs
  80 + *
  81 + * @return string
  82 + */
  83 + public function generateOneShot(): string
  84 + {
  85 + /**
  86 + * @var UrlManager $urlManager
  87 + */
  88 + $urlManager = \Yii::$app->get('urlManagerFrontend');
  89 + $content = '<?xml version="1.0" encoding="UTF-8"?>';
  90 + $content .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" >';
  91 + /**
  92 + * @var SitemapStatic[] $static
  93 + */
  94 +
  95 + /**
  96 + * Main page
  97 + */
  98 + $content .= Html::tag(
  99 + 'url',
  100 + Html::tag('loc', Url::to("/", true)) . Html::tag('lastmod', date('Y-m-d')) . Html::tag(
  101 + 'changefreq',
  102 + 'Always'
  103 + ) . Html::tag('priority', 1)
  104 + ) . PHP_EOL;
  105 +
  106 + /**
  107 + * Products
  108 + */
  109 + $products = Product::find()
  110 + ->joinWith(
  111 + [
  112 + 'lang.alias',
  113 + 'image',
  114 + 'variant',
  115 + ]
  116 + )
  117 + ->innerJoinWith('category')
  118 + ->where(
  119 + [
  120 + '!=',
  121 + 'variant.price',
  122 + 0,
  123 + ]
  124 + )
  125 + ->where(
  126 + [
  127 + '!=',
  128 + 'variant.stock',
  129 + 0,
  130 + ]
  131 + )
  132 + ->asArray()
  133 + ->all();
  134 + foreach ($products as $key => $item) {
  135 + /**
  136 + * @var \common\models\Product $item
  137 + */
  138 + $content .= Html::tag(
  139 + 'url',
  140 + Html::tag('loc', Url::to($item[ 'lang' ][ 'alias' ][ 'value' ], true)) . Html::tag(
  141 + 'lastmod',
  142 + date('Y-m-d')
  143 + ) . Html::tag(
  144 + 'changefreq',
  145 + 'Weekly'
  146 + ) . ( !empty($item[ 'image' ]) ? Html::tag(
  147 + 'image:image',
  148 + Html::tag(
  149 + 'image:loc',
  150 + Url::to(
  151 + "/",
  152 + true
  153 + ) . 'storage/' . $item[ 'image' ][ 'id' ] . '_' . $item[ 'image' ][ 'fileHash' ] . '.' . pathinfo(
  154 + $item[ 'image' ][ 'fileName' ],
  155 + PATHINFO_EXTENSION
  156 + )
  157 + )
  158 + ) : '' ) . Html::tag('priority', 0.6)
  159 + ) . PHP_EOL;
  160 + }
  161 +
  162 + unset($products);
  163 +
  164 + /**
  165 + * Pages
  166 + */
  167 + $pages = Page::find()
  168 + ->where([ 'in_menu' => true ])
  169 + ->with('lang.alias')
  170 + ->asArray()
  171 + ->all();
  172 + foreach ($pages as $key => $item) {
  173 + $content .= Html::tag(
  174 + 'url',
  175 + Html::tag('loc', Url::to($item[ 'lang' ][ 'alias' ][ 'value' ], true)) . Html::tag(
  176 + 'lastmod',
  177 + date('Y-m-d')
  178 + ) . Html::tag(
  179 + 'changefreq',
  180 + 'Weekly'
  181 + ) . Html::tag('priority', 0.6)
  182 + ) . PHP_EOL;
  183 + }
  184 +
  185 + unset($pages);
  186 +
  187 + /**
  188 + * Blog articles
  189 + */
  190 + // $blog = Alias::find()->where(['entity' => 'artbox\weblog\models\CategoryLang'])->orWhere(['entity' => 'artbox\weblog\models\TagLang'])->orWhere(['entity'=> 'artbox\weblog\models\ArticleLang'])->all();
  191 + // foreach ($blog as $key => $item){
  192 + // $content .= Html::tag(
  193 + // 'url',
  194 + // Html::tag('loc', Url::to((($item->entity == 'artbox\weblog\models\ArticleLang')? 'articles/' : '').$item->value, true)) . Html::tag('lastmod', date('Y-m-d')) . Html::tag(
  195 + // 'changefreq',
  196 + // 'Weekly'
  197 + // ) . Html::tag('priority', ($item->entity == 'artbox\weblog\models\ArticleLang') ? 0.7 : 0.6)
  198 + // );
  199 + // }
  200 +
  201 + $articles = Article::find()
  202 + ->where([ 'status' => true ])
  203 + ->with('lang.alias')
  204 + ->asArray()
  205 + ->all();
  206 + foreach ($articles as $article) {
  207 + /**
  208 + * @var Article $article
  209 + */
  210 + $content .= Html::tag(
  211 + 'url',
  212 + Html::tag(
  213 + 'loc',
  214 + Url::to('articles/' . $article[ 'lang' ][ 'alias' ][ 'value' ], true)
  215 + ) . Html::tag(
  216 + 'lastmod',
  217 + date('Y-m-d')
  218 + ) . Html::tag(
  219 + 'changefreq',
  220 + 'Weekly'
  221 + ) . Html::tag('priority', 0.5)
  222 + ) . PHP_EOL;
  223 + }
  224 +
  225 + unset($articles);
  226 +
  227 + /**
  228 + * Filters
  229 + */
  230 + $open_filters = Alias::find()
  231 + ->where([ 'entity' => 'artbox\catalog\models\Filter' ])
  232 + ->asArray()
  233 + ->all();
  234 + foreach ($open_filters as $key => $item) {
  235 + $content .= Html::tag(
  236 + 'url',
  237 + Html::tag('loc', Url::to($item[ 'value' ], true)) . Html::tag('lastmod', date('Y-m-d')) . Html::tag(
  238 + 'changefreq',
  239 + 'daily'
  240 + ) . Html::tag('priority', 0.8)
  241 + ) . PHP_EOL;
  242 + }
  243 +
  244 + /**
  245 + * Brands
  246 + */
  247 +
  248 + $brands = Brand::find()
  249 + ->where([ 'status' => 1 ])
  250 + ->with(
  251 + [
  252 + 'image',
  253 + 'lang.alias',
  254 + ]
  255 + )
  256 + ->asArray()
  257 + ->all();
  258 + foreach ($brands as $item) {
  259 + $content .= Html::tag(
  260 + 'url',
  261 + Html::tag('loc', Url::to($item[ 'lang' ][ 'alias' ][ 'value' ], true)) . Html::tag(
  262 + 'lastmod',
  263 + date('Y-m-d')
  264 + ) . Html::tag(
  265 + 'changefreq',
  266 + 'Daily'
  267 + ) . ( !empty($item[ 'image' ]) ? Html::tag(
  268 + 'image:image',
  269 + Html::tag(
  270 + 'image:loc',
  271 + Url::to(
  272 + "/",
  273 + true
  274 + ) . 'storage/' . $item[ 'image' ][ 'id' ] . '_' . $item[ 'image' ][ 'fileHash' ] . '.' . pathinfo(
  275 + $item[ 'image' ][ 'fileName' ],
  276 + PATHINFO_EXTENSION
  277 + )
  278 + )
  279 + ) : '' ) . Html::tag('priority', 0.7)
  280 + ) . PHP_EOL;
  281 + }
  282 + unset($brands);
  283 + /**
  284 + * category level 1
  285 + */
  286 + $category = Category::find()
  287 + ->with([ 'lang.alias' ])
  288 + ->join('INNER JOIN', [ 'c' => 'category' ], 'c.parent_id = category.id')
  289 + ->andWhere([ 'category.level' => 1 ])
  290 + ->asArray()
  291 + ->all();
  292 + foreach ($category as $key => $item) {
  293 + $content .= Html::tag(
  294 + 'url',
  295 + Html::tag(
  296 + 'loc',
  297 + Url::to('category/' . $item[ 'lang' ][ 'alias' ][ 'value' ], true)
  298 + ) . Html::tag(
  299 + 'lastmod',
  300 + date('Y-m-d')
  301 + ) . Html::tag(
  302 + 'changefreq',
  303 + 'Daily'
  304 + ) . Html::tag('priority', 0.9)
  305 + ) . PHP_EOL;
  306 +
  307 + }
  308 + unset($category);
  309 + /**
  310 + * Other filters
  311 + *
  312 + * @var FilterHelper $filterHelper
  313 + */
  314 + $filterHelper = \Yii::$app->get('filter');
  315 +
  316 + $category = Category::find()
  317 + ->with(
  318 + [
  319 + 'lang.alias',
  320 + 'categories',
  321 + ]
  322 + )
  323 + ->where(
  324 + [
  325 + 'level' => 2,
  326 + ]
  327 + )
  328 + ->orWhere([ 'level' => 1 ])
  329 + ->asArray()
  330 + ->all();
  331 +
  332 + $rewritedFilters = $filterHelper->filterObj->getReplacedFilters();
  333 + foreach ($category as $key => $item) {
  334 + if ($item[ 'level' ] == 2 or ( count($item[ 'categories' ]) == 0 and $item[ 'level' ] == 1 )) {
  335 + $content .= Html::tag(
  336 + 'url',
  337 + Html::tag(
  338 + 'loc',
  339 + Url::to('catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ], true)
  340 + ) . Html::tag(
  341 + 'lastmod',
  342 + date('Y-m-d')
  343 + ) . Html::tag(
  344 + 'changefreq',
  345 + 'Daily'
  346 + ) . Html::tag('priority', 0.9)
  347 + ) . PHP_EOL;
  348 + } else {
  349 + continue;
  350 + }
  351 + $category_id = $item[ 'id' ];
  352 + $option_ids = ( new Query() )->select('product_option_excl.id')
  353 + ->from('product_option_excl')
  354 + ->innerJoin(
  355 + 'product_option_group_excl',
  356 + 'product_option_excl.product_option_group_excl_id = product_option_group_excl.id'
  357 + )
  358 + ->innerJoin(
  359 + 'product_option_group_excl_to_category',
  360 + 'product_option_group_excl.id = product_option_group_excl_to_category.product_option_group_excl_id'
  361 + )
  362 + ->where(
  363 + [ 'product_option_group_excl_to_category.category_id' => $category_id ]
  364 + )
  365 + ->andWhere([ 'product_option_group_excl_to_category.is_filter' => true ])
  366 + ->andWhere(
  367 + [
  368 + 'product_option_excl.id' => ( new Query() )->select(
  369 + 'product_to_product_option_excl.product_option_excl_id'
  370 + )
  371 + ->from(
  372 + 'product_to_product_option_excl'
  373 + )
  374 + ->innerJoin(
  375 + 'product',
  376 + 'product_to_product_option_excl.product_id = product.id'
  377 + )
  378 + ->innerJoin(
  379 + 'product_to_category',
  380 + 'product.id = product_to_category.product_id'
  381 + )
  382 + ->where(
  383 + [ 'product_to_category.category_id' => $category_id ]
  384 + ),
  385 + ]
  386 + )
  387 + ->column();
  388 +
  389 + $options = ProductOptionExcl::find()
  390 + ->with('lang.alias')
  391 + ->with('group.lang.alias')
  392 + ->where([ 'id' => $option_ids ])
  393 + ->asArray()
  394 + ->all();
  395 +
  396 + foreach ($options as $option) {
  397 + if (strpos($option[ 'group' ][ 'lang' ][ 'alias' ][ 'robots' ], 'noindex') !== false) {
  398 + continue;
  399 + }
  400 + $link = 'catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ] . '/' . $option[ 'lang' ][ 'alias' ][ 'value' ];
  401 + if (array_key_exists('/' . $link, $rewritedFilters)) {
  402 + continue;
  403 + }
  404 + $content .= Html::tag(
  405 + 'url',
  406 + Html::tag(
  407 + 'loc',
  408 + Url::to(
  409 + $link,
  410 + true
  411 + )
  412 + ) . Html::tag('lastmod', date('Y-m-d')) . Html::tag(
  413 + 'changefreq',
  414 + 'Daily'
  415 + ) . Html::tag('priority', 0.8)
  416 + ) . PHP_EOL;
  417 + }
  418 +
  419 + // VARIANTS
  420 + $option_ids = ( new Query() )->select('variant_option_excl.id')
  421 + ->from('variant_option_excl')
  422 + ->innerJoin(
  423 + 'variant_option_group_excl',
  424 + 'variant_option_excl.variant_option_group_excl_id = variant_option_group_excl.id'
  425 + )
  426 + ->innerJoin(
  427 + 'variant_option_group_excl_to_category',
  428 + 'variant_option_group_excl.id = variant_option_group_excl_to_category.variant_option_group_excl_id'
  429 + )
  430 + ->where(
  431 + [ 'variant_option_group_excl_to_category.category_id' => $category_id ]
  432 + )
  433 + ->andWhere([ 'variant_option_group_excl_to_category.is_filter' => true ])
  434 + ->andWhere(
  435 + [
  436 + 'variant_option_excl.id' => ( new Query() )->select(
  437 + 'variant_to_variant_option_excl.variant_option_excl_id'
  438 + )
  439 + ->from(
  440 + 'variant_to_variant_option_excl'
  441 + )
  442 + ->innerJoin(
  443 + 'variant',
  444 + 'variant_to_variant_option_excl.variant_id = variant.id'
  445 + )
  446 + ->innerJoin(
  447 + 'product_to_category',
  448 + 'variant.product_id = product_to_category.product_id'
  449 + )
  450 + ->where(
  451 + [ 'product_to_category.category_id' => $category_id ]
  452 + ),
  453 + ]
  454 + )
  455 + ->column();
  456 +
  457 + $options = VariantOptionExcl::find()
  458 + ->with('lang.alias')
  459 + ->with('group.lang.alias')
  460 + ->where([ 'id' => $option_ids ])
  461 + ->asArray()
  462 + ->all();
  463 +
  464 + foreach ($options as $option) {
  465 + if (strpos($option[ 'group' ][ 'lang' ][ 'alias' ][ 'robots' ], 'noindex') !== false) {
  466 + continue;
  467 + }
  468 + $link = 'catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ] . '/' . $option[ 'lang' ][ 'alias' ][ 'value' ];
  469 + if (array_key_exists('/' . $link, $rewritedFilters)) {
  470 + continue;
  471 + }
  472 + $content .= Html::tag(
  473 + 'url',
  474 + Html::tag(
  475 + 'loc',
  476 + Url::to(
  477 + $link,
  478 + true
  479 + )
  480 + ) . Html::tag('lastmod', date('Y-m-d')) . Html::tag(
  481 + 'changefreq',
  482 + 'Daily'
  483 + ) . Html::tag('priority', 0.8)
  484 + ) . PHP_EOL;
  485 + }
  486 +
  487 +
  488 +
  489 +
  490 +
  491 +
  492 + $brands_ids = ( new Query() )->select('brand.id')
  493 + ->from('brand')
  494 + ->innerJoin('product', 'product.brand_id = brand.id')
  495 + ->innerJoin(
  496 + 'product_to_category',
  497 + 'product.id = product_to_category.product_id'
  498 + )
  499 + ->andWhere([ 'product_to_category.category_id' => $item[ 'id' ] ])
  500 + ->groupBy('brand.id')
  501 + ->column();
  502 +
  503 + $brands = Brand::find()
  504 + ->with([ 'lang.alias' ])
  505 + ->where([ 'status' => true ])
  506 + ->andWhere([ 'id' => $brands_ids ])
  507 + ->asArray()
  508 + ->all();
  509 +
  510 + foreach ($brands as $brand) {
  511 + $link = 'catalog/' . $item[ 'lang' ][ 'alias' ][ 'value' ] . '/' . $brand[ 'lang' ][ 'alias' ][ 'value' ];
  512 + $content .= Html::tag(
  513 + 'url',
  514 + Html::tag(
  515 + 'loc',
  516 + Url::to(
  517 + $link,
  518 + true
  519 + )
  520 + ) . Html::tag('lastmod', date('Y-m-d')) . Html::tag(
  521 + 'changefreq',
  522 + 'Daily'
  523 + ) . Html::tag('priority', 0.8)
  524 + ) . PHP_EOL;
  525 + }
  526 +
  527 + }
  528 + $content .= '</urlset>';
  529 + return $content;
  530 + }
  531 +
  532 + }
0 533 \ No newline at end of file
... ...
common/models/LangRedirect.php 0 → 100644
  1 +<?php
  2 + /**
  3 + * Created by PhpStorm.
  4 + * User: stes
  5 + * Date: 01.03.18
  6 + * Time: 16:00
  7 + */
  8 +
  9 + namespace common\models;
  10 +
  11 + use artbox\core\models\interfaces\RedirectInterface;
  12 + use artbox\core\models\Language;
  13 + use yii\base\Object;
  14 +
  15 + class LangRedirect extends Object implements RedirectInterface
  16 + {
  17 + protected $link = '';
  18 +
  19 + public function doRedirect(string $url): bool
  20 + {
  21 + $language = Language::getCurrent();
  22 + if(\Yii::$app->request->url == '/'.$language->url and $language->default){
  23 + $this->link = '';
  24 + return true;
  25 + }else{
  26 + return false;
  27 + }
  28 + }
  29 + public function getLink(): string
  30 + {
  31 + return $this->link;
  32 + }
  33 + }
0 34 \ No newline at end of file
... ...
frontend/config/main.php
... ... @@ -2,6 +2,7 @@
2 2  
3 3 use artbox\core\components\LanguageRequest;
4 4 use artbox\core\components\SeoUrlManager;
  5 + use common\models\LangRedirect;
5 6  
6 7 $params = array_merge(
7 8 require( __DIR__ . '/../../common/config/params.php' ),
... ... @@ -54,6 +55,7 @@
54 55 'errorAction' => 'site/error',
55 56 ],
56 57 'urlManager' => [
  58 + 'baseUrl' => '/',
57 59 'class' => SeoUrlManager::className(),
58 60 'enablePrettyUrl' => true,
59 61 'forceRedirect' => true,
... ... @@ -77,6 +79,9 @@
77 79 'site/contact',
78 80  
79 81 ],
  82 + 'redirects' => [
  83 + LangRedirect::className(),
  84 + ],
80 85 ],
81 86 'assetsAutoCompress' => [
82 87 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent',
... ...
frontend/views/site/index.php
... ... @@ -73,7 +73,7 @@ JS;
73 73 <section class="blue-fon why-us-wr no-mg">
74 74 <div class="container">
75 75 <div class="heading text-center">
76   - <div class="h2 big-text"><?=\Yii::t('app', 'index_why_us')?></div>
  76 + <h1 class="h2 big-text"><?=\Yii::t('app', 'index_why_us')?></h1>
77 77 </div>
78 78 <div class="why-us container">
79 79 <div class="col-md-12 row">
... ...