Blame view

console/SiteMapController.php 10.2 KB
8a7e6ecf   Yarik   Namespaces
1
2
  <?php
      
7c3b817e   Administrator   full commit
3
      namespace artweb\artbox\ecommerce\console;
8a7e6ecf   Yarik   Namespaces
4
      
9580e548   Alexey Boroda   -Site map in prog...
5
6
7
      use artweb\artbox\ecommerce\models\Brand;
      use artweb\artbox\ecommerce\models\ProductVariant;
      use artweb\artbox\language\models\Language;
8a7e6ecf   Yarik   Namespaces
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
      use artweb\artbox\seo\models\Seo;
      use artweb\artbox\ecommerce\models\Category;
      use artweb\artbox\ecommerce\models\Product;
      use artweb\artbox\ecommerce\models\ProductFrontendSearch;
      use Yii;
      use artweb\artbox\models\Page;
      use yii\helpers\ArrayHelper;
      use yii\helpers\Url;
      use yii\console\Controller;
      
      /**
       * PageController implements the CRUD actions for Page model.
       */
      class SiteMapController extends Controller
      {
          
9580e548   Alexey Boroda   -Site map in prog...
24
          private $urlList = [ 'https://dev.extremstyle.ua/ru' ];
8a7e6ecf   Yarik   Namespaces
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
          private $count = 1;
          
          public function checkFilter($category, $filter)
          {
              $productModel = new ProductFrontendSearch();
              $productProvider = $productModel->search($category, $filter);
              if (!empty( $productProvider->models )) {
                  return true;
              } else {
                  return false;
              }
          }
          
          public function getAddStatic()
          {
              return [
9580e548   Alexey Boroda   -Site map in prog...
41
                  'https://dev.extremstyle.ua/ru',
8a7e6ecf   Yarik   Namespaces
42
43
44
              ];
          }
          
9580e548   Alexey Boroda   -Site map in prog...
45
          public function getVariants()
8a7e6ecf   Yarik   Namespaces
46
          {
9580e548   Alexey Boroda   -Site map in prog...
47
48
49
50
              return ProductVariant::find()
                                   ->with('lang')
                                   ->with('product.lang')
                                   ->batch(1000);
8a7e6ecf   Yarik   Namespaces
51
52
53
54
55
56
57
58
59
60
61
62
63
64
              
          }
          
          public function getSeoLinks()
          {
              return Seo::find()
                        ->where([ 'meta' => '' ])
                        ->all();
              
          }
          
          public function getStaticPages()
          {
              return Page::find()
9580e548   Alexey Boroda   -Site map in prog...
65
                         ->with('lang')
8a7e6ecf   Yarik   Namespaces
66
67
68
69
70
71
                         ->all();
          }
          
          public function getCategories()
          {
              return Category::find()
9580e548   Alexey Boroda   -Site map in prog...
72
                             ->with('lang')
8a7e6ecf   Yarik   Namespaces
73
74
75
                             ->all();
          }
          
9580e548   Alexey Boroda   -Site map in prog...
76
          public function getCategoriesWithFilters()
8a7e6ecf   Yarik   Namespaces
77
          {
9580e548   Alexey Boroda   -Site map in prog...
78
79
80
81
82
83
84
85
86
87
88
89
90
              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();
8a7e6ecf   Yarik   Namespaces
91
92
93
94
95
96
97
98
99
100
          }
          
          /**
           * @param $category Category;
           *
           * @return mixed
           */
          
          public function getFilters($category)
          {
8a7e6ecf   Yarik   Namespaces
101
              return $category->getActiveFilters()
9580e548   Alexey Boroda   -Site map in prog...
102
103
104
105
106
107
                              ->andWhere(
                                  [
                      
                                      'tax_group.meta_robots' => '',
                                  ]
                              )
8a7e6ecf   Yarik   Namespaces
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                              ->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)) {
9580e548   Alexey Boroda   -Site map in prog...
125
                  print $this->count++ . $url . "\n";
8a7e6ecf   Yarik   Namespaces
126
127
                  $content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
                          'Y-m-d'
9580e548   Alexey Boroda   -Site map in prog...
128
                      ) . '</lastmod>' . '<changefreq>Weekly</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
8a7e6ecf   Yarik   Namespaces
129
130
131
132
133
              }
          }
          
          public function actionProcess()
          {
8a7e6ecf   Yarik   Namespaces
134
135
136
137
138
139
140
141
142
              $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' ]);
              
              $dirName = Yii::getAlias('@frontend') . '/web';
              
9580e548   Alexey Boroda   -Site map in prog...
143
              $filename = 'test_sitemap.xml';
8a7e6ecf   Yarik   Namespaces
144
145
146
147
148
149
150
151
152
153
154
155
              setlocale(LC_ALL, 'ru_RU.CP1251');
              $handle = fopen($dirName . '/' . $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(
                      [
9580e548   Alexey Boroda   -Site map in prog...
156
157
                          'site/page',
                          'slug' => $page->lang->alias,
8a7e6ecf   Yarik   Namespaces
158
159
160
161
162
163
164
165
166
                      ]
                  );
                  $this->createRow($url, 1, $content);
              }
              
              foreach ($this->getCategories() as $category) {
                  $url = Url::to(
                      [
                          'catalog/category',
9580e548   Alexey Boroda   -Site map in prog...
167
                          'category' => $category->lang->alias,
8a7e6ecf   Yarik   Namespaces
168
169
                      ]
                  );
9580e548   Alexey Boroda   -Site map in prog...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
                  $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);
                  }
8a7e6ecf   Yarik   Namespaces
184
185
              }
              
9580e548   Alexey Boroda   -Site map in prog...
186
187
              foreach ($this->getBrands() as $brand) {
  
8a7e6ecf   Yarik   Namespaces
188
189
                  $url = Url::to(
                      [
9580e548   Alexey Boroda   -Site map in prog...
190
191
                          'brand/view',
                          'slug' => $brand->lang->alias,
8a7e6ecf   Yarik   Namespaces
192
193
                      ]
                  );
9580e548   Alexey Boroda   -Site map in prog...
194
195
                  $this->createRow($url, 0.7, $content);
  
8a7e6ecf   Yarik   Namespaces
196
197
              }
              
9580e548   Alexey Boroda   -Site map in prog...
198
199
200
              foreach ($this->getCategoriesWithFilters() as $category) {
                  foreach ($category->taxGroups as $group) {
                      foreach ($group->taxOptions as $option) {
8a7e6ecf   Yarik   Namespaces
201
202
203
204
                          $url = Url::to(
                              [
                                  'catalog/category',
                                  'category' => $category,
9580e548   Alexey Boroda   -Site map in prog...
205
                                  'filters'  => [ $group->lang->alias => [ $option->lang->alias ] ],
8a7e6ecf   Yarik   Namespaces
206
207
208
209
210
211
212
213
214
215
216
                              ]
                          );
                          $this->createRow($url, 0.8, $content);
                      }
                      
                  }
              }
              
              foreach ($this->getSeoLinks() as $link) {
                  $url = Yii::$app->urlManager->baseUrl . $link->url;
                  $this->createRow($url, 0.7, $content);
9580e548   Alexey Boroda   -Site map in prog...
217
  
8a7e6ecf   Yarik   Namespaces
218
219
              }
              
9580e548   Alexey Boroda   -Site map in prog...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
              //            foreach ($this->getCategories() as $category) {
              //                foreach ($this->getFilters($category) as $filter1) {
              //                    foreach ($this->getFilters($category) as $filter2) {
              //                        if ($this->checkFilter(
              //                            $category,
              //                            [
              //                                $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
              //                                $filter2[ 'group_alias' ] => [ $filter2[ 'option_alias' ] ],
              //                            ]
              //                        )
              //                        ) {
              //                            $url = Url::to(
              //                                [
              //                                    'catalog/category',
              //                                    'category' => $category,
              //                                    'filters'  => [
              //                                        $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
              //                                        $filter2[ 'group_alias' ] => [ $filter2[ 'option_alias' ] ],
              //                                    ],
              //                                ]
              //                            );
              //                            $this->createRow($url, 0.7, $content);
              //                        }
              //
8a7e6ecf   Yarik   Namespaces
244
245
              //                    }
              //
9580e548   Alexey Boroda   -Site map in prog...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
              //                    foreach ($this->getBrands($category) as $brand) {
              //                        if ($this->checkFilter(
              //                            $category,
              //                            [
              //                                'brands'                  => [ $brand->id ],
              //                                $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
              //                            ]
              //                        )
              //                        ) {
              //                            $url = Url::to(
              //                                [
              //                                    'catalog/category',
              //                                    'category' => $category,
              //                                    'filters'  => [
              //                                        'brands'                  => [ $brand->alias ],
              //                                        $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
              //                                    ],
              //                                ]
              //                            );
              //                            $this->createRow($url, 0.7, $content);
              //                        }
8a7e6ecf   Yarik   Namespaces
267
              //
8a7e6ecf   Yarik   Namespaces
268
              //                    }
8a7e6ecf   Yarik   Namespaces
269
270
              //                }
              //            }
8a7e6ecf   Yarik   Namespaces
271
272
273
274
275
276
277
278
279
280
              
              $content .= '</urlset>';
              
              fwrite($handle, $content);
              fclose($handle);
              
              print $dirName . '/' . $filename;
          }
          
      }