e6357e22
Administrator
big commti
|
1
|
<?php
|
74d06b0f
Alexey Boroda
-Site map generat...
|
2
3
4
5
6
|
namespace console\controllers;
use common\models\Articles;
use common\models\Seo;
|
74d06b0f
Alexey Boroda
-Site map generat...
|
7
8
9
10
11
12
|
use common\modules\product\models\Category;
use common\modules\product\models\Product;
use common\modules\rubrication\models\TaxOption;
use frontend\models\ProductFrontendSearch;
use Yii;
use common\models\Page;
|
66510176
Alexey Boroda
-Sitemap excluded...
|
13
|
use yii\db\Query;
|
74d06b0f
Alexey Boroda
-Site map generat...
|
14
15
16
17
|
use yii\helpers\ArrayHelper;
use yii\helpers\Console;
use yii\helpers\Url;
use yii\console\Controller;
|
74d06b0f
Alexey Boroda
-Site map generat...
|
18
|
|
e6357e22
Administrator
big commti
|
19
|
/**
|
74d06b0f
Alexey Boroda
-Site map generat...
|
20
|
* PageController implements the CRUD actions for Page model.
|
e6357e22
Administrator
big commti
|
21
|
*/
|
74d06b0f
Alexey Boroda
-Site map generat...
|
22
23
24
25
26
27
|
class SiteMapController extends Controller
{
private $urlList = [ 'http://www.linija-svitla.ua/' ];
private $count = 1;
|
66510176
Alexey Boroda
-Sitemap excluded...
|
28
29
|
private $addedFilters = [];
|
74d06b0f
Alexey Boroda
-Site map generat...
|
30
31
32
33
34
35
36
37
38
|
public function checkFilter($category, $filter)
{
$productModel = new ProductFrontendSearch();
$productProvider = $productModel->search($category, $filter);
if (!empty($productProvider->models)) {
return true;
} else {
return false;
}
|
e6357e22
Administrator
big commti
|
39
|
}
|
6c673f5d
Alexey Boroda
-Sitemap generati...
|
40
|
|
74d06b0f
Alexey Boroda
-Site map generat...
|
41
42
43
44
45
46
47
|
/**
* @return array
*/
public function getAddStatic()
{
return [
'https://www.linija-svitla.ua',
|
74d06b0f
Alexey Boroda
-Site map generat...
|
48
|
];
|
e6357e22
Administrator
big commti
|
49
|
}
|
74d06b0f
Alexey Boroda
-Site map generat...
|
50
51
52
53
54
55
56
57
58
|
/**
* @return array|\yii\db\ActiveRecord[]
*/
public function getProducts()
{
return Product::find()
->all();
|
e6357e22
Administrator
big commti
|
59
|
}
|
74d06b0f
Alexey Boroda
-Site map generat...
|
60
61
62
63
64
65
66
67
68
69
70
|
/**
* @return array|\yii\db\ActiveRecord[]
*/
public function getSeoLinks()
{
return Seo::find()
->where(
[
'not like',
'meta',
|
66510176
Alexey Boroda
-Sitemap excluded...
|
71
72
73
74
75
76
77
78
|
'noindex',
]
)
->andWhere(
[
'!=',
'url',
'/',
|
74d06b0f
Alexey Boroda
-Site map generat...
|
79
80
81
82
|
]
)
->all();
|
e6357e22
Administrator
big commti
|
83
|
}
|
74d06b0f
Alexey Boroda
-Site map generat...
|
84
85
86
87
88
89
90
91
|
/**
* @return array|\yii\db\ActiveRecord[]
*/
public function getStaticPages()
{
return Page::find()
->all();
|
e6357e22
Administrator
big commti
|
92
|
}
|
74d06b0f
Alexey Boroda
-Site map generat...
|
93
94
95
96
97
98
99
|
/**
* @return array|\common\modules\product\models\Category[]|\yii\db\ActiveRecord[]
*/
public function getCategories()
{
return Category::find()
|
66510176
Alexey Boroda
-Sitemap excluded...
|
100
|
->with('brands')
|
74d06b0f
Alexey Boroda
-Site map generat...
|
101
|
->all();
|
e6357e22
Administrator
big commti
|
102
|
}
|
74d06b0f
Alexey Boroda
-Site map generat...
|
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
|
/**
* @return array|\yii\db\ActiveRecord[]
*/
public function getArticles()
{
return Articles::find()
->all();
}
/**
* @param $category
*
* @return mixed
*/
public function getBrands($category)
{
return $category->brands;
}
/**
* @return array|\yii\db\ActiveRecord[]
*/
public function getOptions()
{
return TaxOption::find()
->innerJoinWith(
[
'taxGroup.category',
]
)
|
170bb70d
Alexey Boroda
-Sitemap updated
|
136
137
138
139
140
|
->where(
[
'tax_group.is_filter' => true,
]
)
|
66510176
Alexey Boroda
-Sitemap excluded...
|
141
142
143
144
145
146
147
148
149
150
|
->joinWith('products')
->andWhere(
[
'product.product_id' => ( new Query() )->select(['pid' => 'product.product_id'])
->from('product')->join('INNER JOIN', 'product_variant', 'product.product_id = product_variant.product_id')
->where(['!=','product_variant.stock',0])
]
)
|
74d06b0f
Alexey Boroda
-Site map generat...
|
151
152
153
154
155
156
157
158
159
160
161
|
->all();
}
public function checkUrl($url)
{
if (!in_array($url, $this->urlList)) {
$this->urlList[] = $url;
return true;
} else {
return false;
|
e6357e22
Administrator
big commti
|
162
163
|
}
}
|
74d06b0f
Alexey Boroda
-Site map generat...
|
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
|
public function createRow($url, $priority, &$content)
{
// if($this->checkUrl($url)){
$this->stdout('# ' . $this->count++ . ' ' . $url . "\n", Console::FG_GREEN);
$content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
'Y-m-d'
) . '</lastmod>' . '<changefreq>Weekly</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
// }
}
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' ]);
$this->stdout('Start!' . "\n", Console::FG_RED);
$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">';
$this->stdout('Add static' . "\n", Console::FG_BLUE);
foreach ($this->getAddStatic() as $page) {
$this->createRow($page, 1, $content);
}
$this->stdout('Add static pages' . "\n", Console::FG_BLUE);
foreach ($this->getStaticPages() as $page) {
$url = Url::to(
[
'text/main',
'translit' => $page->translit,
],
true
);
|
07082ed1
Alexey Boroda
-Sitemap generati...
|
213
|
$this->createRow($url, 0.5, $content);
|
74d06b0f
Alexey Boroda
-Site map generat...
|
214
215
216
217
218
219
220
221
222
223
224
225
|
}
$this->stdout('Add categories' . "\n", Console::FG_BLUE);
foreach ($this->getCategories() as $category) {
$url = Url::to(
[
'catalog/category',
'category' => $category,
],
true
);
|
886cf4d7
Alexey Boroda
-Sitemap started ...
|
226
|
$this->createRow($url, 0.9, $content);
|
74d06b0f
Alexey Boroda
-Site map generat...
|
227
228
229
230
231
232
233
234
235
236
237
238
239
|
}
$this->stdout('Add products' . "\n", Console::FG_BLUE);
foreach ($this->getProducts() as $product) {
$url = Url::to(
[
'catalog/product',
'product' => $product,
],
true
);
|
886cf4d7
Alexey Boroda
-Sitemap started ...
|
240
|
$this->createRow($url, 0.7, $content);
|
74d06b0f
Alexey Boroda
-Site map generat...
|
241
242
243
244
245
246
247
248
249
250
251
252
253
|
}
$this->stdout('Add articles' . "\n", Console::FG_BLUE);
foreach ($this->getArticles() as $article) {
$url = Url::to(
[
'articles/show',
'translit' => $article->translit,
],
true
);
|
886cf4d7
Alexey Boroda
-Sitemap started ...
|
254
|
$this->createRow($url, 0.7, $content);
|
74d06b0f
Alexey Boroda
-Site map generat...
|
255
256
257
258
259
260
|
}
$this->stdout('Add brands' . "\n", Console::FG_BLUE);
foreach ($this->getCategories() as $category) {
|
66510176
Alexey Boroda
-Sitemap excluded...
|
261
262
263
264
265
266
267
268
269
270
|
foreach ($category->brands as $brand) {
$url = Url::to(
[
'catalog/category',
'category' => $category,
'filters' => [ 'brands' => [ $brand->alias ] ],
],
true
);
$this->createRow($url, 0.8, $content);
|
e6357e22
Administrator
big commti
|
271
|
}
|
e6357e22
Administrator
big commti
|
272
|
}
|
74d06b0f
Alexey Boroda
-Site map generat...
|
273
274
275
276
277
278
279
280
281
282
283
284
|
$this->stdout('Add filters' . "\n", Console::FG_BLUE);
foreach ($this->getOptions() as $option) {
$url = Url::to(
[
'catalog/category',
'category' => $option->taxGroup->category,
'filters' => [ $option->taxGroup->alias => [ $option->alias ] ],
],
true
);
|
66510176
Alexey Boroda
-Sitemap excluded...
|
285
|
$this->addedFilters[] = $url;
|
74d06b0f
Alexey Boroda
-Site map generat...
|
286
287
288
289
290
291
292
|
$this->createRow($url, 0.8, $content);
}
$this->stdout('Add seo links' . "\n", Console::FG_BLUE);
foreach ($this->getSeoLinks() as $link) {
$url = Yii::$app->urlManager->baseUrl . $link->url;
|
66510176
Alexey Boroda
-Sitemap excluded...
|
293
294
295
|
if (in_array($url, $this->addedFilters)) {
continue;
}
|
07082ed1
Alexey Boroda
-Sitemap generati...
|
296
|
$this->createRow($url, 0.8, $content);
|
74d06b0f
Alexey Boroda
-Site map generat...
|
297
298
299
300
301
302
303
304
305
|
}
$content .= '</urlset>';
fwrite($handle, $content);
fclose($handle);
$this->stdout('File name: ' . $dirName . '/' . $filename . "\n", Console::FG_CYAN);
|
e6357e22
Administrator
big commti
|
306
|
}
|
6c673f5d
Alexey Boroda
-Sitemap generati...
|
307
|
|
e6357e22
Administrator
big commti
|
308
|
}
|