Blame view

backend/components/Sitemap.php 24 KB
9dcc1288   Anastasia   redirect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  <?php
      
      namespace backend\components;
  
      use artbox\catalog\helpers\FilterHelper;
      use artbox\catalog\models\Brand;
      use artbox\catalog\models\Category;
      use artbox\catalog\models\ProductOptionExcl;
      use artbox\catalog\models\VariantOptionExcl;
      use artbox\core\models\Alias;
      use artbox\core\models\Page;
      use artbox\core\models\SitemapStatic;
      use artbox\weblog\models\Article;
      use common\models\Product;
      use common\models\VariantOptionGroupExcl;
      use yii\base\Object;
      use yii\db\Query;
      use yii\helpers\Html;
      use yii\helpers\Url;
      use yii\web\UrlManager;
  
      class Sitemap extends Object
      {
          public $entities = [];
          public $path = '@frontend/web/sitemap.xml';
          public $url = 'sitemap.xml';
      
          /**
           * Get absolute url to sitemap.xml
           *
           * @return string
           */
          public function getUrl(): string
          {
              /**
               * @var UrlManager $urlManager
               */
              $urlManager = \Yii::$app->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 = '<?xml version="1.0" encoding="UTF-8"?>';
              $content .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" >';
              /**
               * @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 .= '</urlset>';
              return $content;
          }
      
      }