Blame view

console/controllers/SiteMapController.php 7.07 KB
e6357e22   Administrator   big commti
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <?php
  
  namespace console\controllers;
  
  use common\models\Articles;
  use common\models\Seo;
  use common\modules\product\helpers\FilterHelper;
  use common\modules\product\models\Brand;
  use common\modules\product\models\Category;
  use common\modules\product\models\Product;
  use frontend\models\ProductFrontendSearch;
  use Yii;
  use common\models\Page;
  use common\models\PageSearch;
  use yii\helpers\ArrayHelper;
6c673f5d   Alexey Boroda   -Sitemap generati...
16
  use yii\helpers\Console;
e6357e22   Administrator   big commti
17
18
19
20
21
22
23
24
25
26
27
  use yii\helpers\Url;
  use yii\console\Controller;
  use yii\web\NotFoundHttpException;
  use yii\filters\VerbFilter;
  use developeruz\db_rbac\behaviors\AccessBehavior;
  /**
   * PageController implements the CRUD actions for Page model.
   */
  class SiteMapController extends Controller
  {
  
7a686bc3   Administrator   big commti
28
      private $urlList = ['http://www.linija-svitla.ua/'];
e6357e22   Administrator   big commti
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
      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 [
7a686bc3   Administrator   big commti
47
48
              'http://www.linija-svitla.ua',
              'http://www.linija-svitla.ua/catalog'
e6357e22   Administrator   big commti
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
          ];
      }
  
  
      public function getProducts() {
          return Product::find()->all();
  
      }
  
  
      public function getSeoLinks() {
          return Seo::find()->where(['meta' => ''])->all();
  
      }
  
      public function getStaticPages(){
          return Page::find()->all();
      }
  
  
      public function getCategories(){
          return Category::find()->all();
      }
  
  
      public function getArticles(){
          return Articles::find()->all();
      }
  
9ffea880   Administrator   big commti
78
      public function getBrands($category){
e6357e22   Administrator   big commti
79
  
9ffea880   Administrator   big commti
80
          return $category->brands;
e6357e22   Administrator   big commti
81
82
83
84
85
86
87
88
89
      }
  
      /**
       * @param $category Category;
       * @return mixed
       */
  
      public function getFilters($category){
  
0bfe5701   Administrator   big commti
90
          return $category->getActiveFilters();
e6357e22   Administrator   big commti
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  
      }
  
  
      public function checkUrl($url){
          if(!in_array($url, $this->urlList)){
              $this->urlList[] = $url;
              return true;
          } else {
              return false;
          }
      }
  
  
      public function createRow( $url, $priority, &$content ){
6c673f5d   Alexey Boroda   -Sitemap generati...
106
107
  //        if($this->checkUrl($url)){
              $this->stdout( '# ' . $this->count++ . "\n", Console::FG_GREEN);
e6357e22   Administrator   big commti
108
109
110
              $content .= '<url>' .
                  '<loc>' . $url . '</loc>' .
                  '<lastmod>' . date('Y-m-d') . '</lastmod>' .
950470c1   Administrator   big commti
111
                  '<changefreq>Weekly</changefreq>' .
e6357e22   Administrator   big commti
112
113
                  '<priority>' . $priority .'</priority>' .
                  '</url>';
6c673f5d   Alexey Boroda   -Sitemap generati...
114
  //        }
e6357e22   Administrator   big commti
115
116
117
118
119
120
121
122
123
124
125
126
127
      }
  
  
      public function actionProcess() {
  
          $config = ArrayHelper::merge(
              require(__DIR__ . '/../../frontend/config/main.php'),
              require(__DIR__ . '/../../common/config/main.php')
  
          );
  
          Yii::$app->urlManager->addRules($config['components']['urlManager']['rules']);
  
6c673f5d   Alexey Boroda   -Sitemap generati...
128
          $this->stdout('Start!' . "\n", Console::FG_RED);
e6357e22   Administrator   big commti
129
130
131
132
133
134
135
136
137
  
          $dirName = Yii::getAlias('@frontend').'/web';
  
          $filename = 'sitemap.xml';
  
          setlocale(LC_ALL, 'ru_RU.CP1251');
          $handle = fopen($dirName .'/'. $filename, "w");
  
          $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
6c673f5d   Alexey Boroda   -Sitemap generati...
138
139
          
          $this->stdout('Add static' . "\n", Console::FG_BLUE);
e6357e22   Administrator   big commti
140
141
142
143
  
          foreach ($this->getAddStatic() as $page) {
              $this->createRow($page , 1,$content);
          }
6c673f5d   Alexey Boroda   -Sitemap generati...
144
          $this->stdout('Add static pages' . "\n", Console::FG_BLUE);
e6357e22   Administrator   big commti
145
146
  
          foreach ($this->getStaticPages() as $page) {
950470c1   Administrator   big commti
147
              $url = Url::to(['text/main','translit' => $page->translit]);
6c673f5d   Alexey Boroda   -Sitemap generati...
148
      
e6357e22   Administrator   big commti
149
150
              $this->createRow($url , 1,$content);
          }
6c673f5d   Alexey Boroda   -Sitemap generati...
151
          $this->stdout('Add categories' . "\n", Console::FG_BLUE);
e6357e22   Administrator   big commti
152
153
  
          foreach ($this->getCategories() as $category) {
6c673f5d   Alexey Boroda   -Sitemap generati...
154
      
e6357e22   Administrator   big commti
155
156
157
              $url = Url::to(['catalog/category', 'category' => $category]);
              $this->createRow($url , 1,$content);
          }
6c673f5d   Alexey Boroda   -Sitemap generati...
158
159
      
          $this->stdout('Add products' . "\n", Console::FG_BLUE);
e6357e22   Administrator   big commti
160
161
  
          foreach ($this->getProducts() as $product) {
6c673f5d   Alexey Boroda   -Sitemap generati...
162
      
e6357e22   Administrator   big commti
163
164
165
              $url = Url::to(['catalog/product', 'product' => $product]);
              $this->createRow($url , 0.9, $content);
          }
6c673f5d   Alexey Boroda   -Sitemap generati...
166
167
      
          $this->stdout('Add articles' . "\n", Console::FG_BLUE);
e6357e22   Administrator   big commti
168
169
  
          foreach ($this->getArticles() as $article) {
6c673f5d   Alexey Boroda   -Sitemap generati...
170
      
950470c1   Administrator   big commti
171
              $url = Url::to(['articles/show', 'translit' => $article->translit]);
e6357e22   Administrator   big commti
172
173
174
              $this->createRow($url , 0.8,$content);
  
          }
6c673f5d   Alexey Boroda   -Sitemap generati...
175
176
      
          $this->stdout('Add brands' . "\n", Console::FG_BLUE);
e6357e22   Administrator   big commti
177
178
179
180
181
182
183
184
185
  
          foreach($this->getCategories() as $category){
              foreach ($this->getBrands($category) as $brand) {
                  if($this->checkFilter($category, ['brands' => [$brand->brand_id]])){
                      $url = Url::to(['catalog/category', 'category' => $category, 'filters' => ['brands' => [$brand->alias]]]) ;
                      $this->createRow($url , 0.8, $content);
                  }
              }
          }
6c673f5d   Alexey Boroda   -Sitemap generati...
186
187
      
          $this->stdout('Add filters' . "\n", Console::FG_BLUE);
e6357e22   Administrator   big commti
188
189
190
191
192
193
194
195
196
197
  
          foreach($this->getCategories() as $category){
              foreach ($this->getFilters($category) as $filter) {
                  if($this->checkFilter($category, [$filter['group_alias'] => [$filter['option_alias']]])){
                      $url = Url::to(['catalog/category', 'category' => $category, 'filters' => [$filter['group_alias'] => [$filter['option_alias']]] ]);
                      $this->createRow($url , 0.8, $content);
                  }
  
              }
          }
6c673f5d   Alexey Boroda   -Sitemap generati...
198
199
200
      
          $this->stdout('Add seo links' . "\n", Console::FG_BLUE);
      
e6357e22   Administrator   big commti
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
          foreach($this->getSeoLinks() as $link){
              $url = Yii::$app->urlManager->baseUrl.$link->url;
              $this->createRow($url , 0.7, $content);
  
          }
  
  
  
  //        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);
  //                    }
  //
  //                }
  //
  //                foreach ($this->getBrands($category) as $brand) {
  //                    if($this->checkFilter($category, ['brands' => [$brand->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);
  //                    }
  //
  //                }
  //            }
  //        }
  
  
  
          $content .= '</urlset>';
  
          fwrite($handle, $content);
          fclose($handle);
6c673f5d   Alexey Boroda   -Sitemap generati...
235
236
          $this->stdout('File name: ' . $dirName .'/'. $filename . "\n", Console::FG_CYAN);
          
e6357e22   Administrator   big commti
237
238
239
      }
  
  }