38ed47e1
Administrator
20.07.16
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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;
use yii\helpers\Url;
use yii\console\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use developeruz\db_rbac\behaviors\AccessBehavior;
|
315ebf90
Alex Savenko
sitemap controlle...
|
21
22
|
|
38ed47e1
Administrator
20.07.16
|
23
24
25
26
27
28
29
30
31
|
/**
* PageController implements the CRUD actions for Page model.
*/
class SiteMapController extends Controller
{
private $urlList = ['http://www.rukzachok.com.ua/'];
private $count = 1;
|
315ebf90
Alex Savenko
sitemap controlle...
|
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public function getAddStatic() {
return [
'http://www.rukzachok.com.ua',
'http://www.rukzachok.com.ua/catalog'
];
}
public function getStaticPages() {
return Page::find()->all();
}
public function getCategories() {
return Category::find()->andWhere(['meta_robots' => ''])->all();
}
|
38ed47e1
Administrator
20.07.16
|
46
|
|
d57c2c62
Alex Savenko
sitemap controlle...
|
47
|
public function checkFilter($category, $filter) {
|
38ed47e1
Administrator
20.07.16
|
48
49
|
$productModel = new ProductFrontendSearch();
$productProvider = $productModel->search($category, $filter);
|
a54d86cf
Alex Savenko
sitemapcontroller...
|
50
|
if(!empty($productProvider->models)){
|
38ed47e1
Administrator
20.07.16
|
51
|
return true;
|
a54d86cf
Alex Savenko
sitemapcontroller...
|
52
|
} else {
|
38ed47e1
Administrator
20.07.16
|
53
|
return false;
|
a54d86cf
Alex Savenko
sitemapcontroller...
|
54
|
}
|
38ed47e1
Administrator
20.07.16
|
55
56
|
}
|
38ed47e1
Administrator
20.07.16
|
57
58
59
60
61
|
public function getProducts() {
return Product::find()->all();
}
|
38ed47e1
Administrator
20.07.16
|
62
63
|
public function getSeoLinks() {
return Seo::find()->where(['meta' => ''])->all();
|
38ed47e1
Administrator
20.07.16
|
64
65
|
}
|
d57c2c62
Alex Savenko
sitemap controlle...
|
66
|
public function getArticles() {
|
38ed47e1
Administrator
20.07.16
|
67
68
69
|
return Articles::find()->all();
}
|
d57c2c62
Alex Savenko
sitemap controlle...
|
70
|
public function getBrands($category) {
|
38ed47e1
Administrator
20.07.16
|
71
72
73
74
|
return $category->brands;
}
|
f9849059
Administrator
06.09.16
|
75
76
77
78
|
/**
* @param $category Category;
* @return mixed
*/
|
d57c2c62
Alex Savenko
sitemap controlle...
|
79
|
public function getFilters($category) {
|
38ed47e1
Administrator
20.07.16
|
80
81
82
83
84
|
return $category->getActiveFilters()->all();
}
|
d57c2c62
Alex Savenko
sitemap controlle...
|
85
|
public function checkUrl($url) {
|
a54d86cf
Alex Savenko
sitemapcontroller...
|
86
87
88
89
90
|
if (preg_match('/filters:[^=]+=[^=]+;[^=]+=[^=]+/', $url)) {
$reverse_url = preg_replace('/filters:([^=]+=[^=]+);([^=]+=[^=]+)/', 'filters:$2;$1', $url);
}
|
11ffb77e
Alex Savenko
sitemapcontroller...
|
91
|
if(in_array($url, $this->urlList) || (isset($reverse_url) && in_array($reverse_url, $this->urlList))) {
|
a54d86cf
Alex Savenko
sitemapcontroller...
|
92
93
|
return false;
} else {
|
38ed47e1
Administrator
20.07.16
|
94
95
|
$this->urlList[] = $url;
return true;
|
38ed47e1
Administrator
20.07.16
|
96
97
98
|
}
}
|
d57c2c62
Alex Savenko
sitemap controlle...
|
99
|
public function createRow( $url, $priority, &$content ) {
|
38ed47e1
Administrator
20.07.16
|
100
101
102
103
104
105
106
107
108
109
110
|
if($this->checkUrl($url)){
print $this->count++ . "\n";
$content .= '<url>' .
'<loc>' . $url . '</loc>' .
'<lastmod>' . date('Y-m-d') . '</lastmod>' .
'<changefreq>Daily</changefreq>' .
'<priority>' . $priority .'</priority>' .
'</url>';
}
}
|
38ed47e1
Administrator
20.07.16
|
111
112
113
114
115
116
117
118
119
120
|
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']);
|
dbceaf12
Administrator
20.07.16
|
121
|
$dirName = Yii::getAlias('@frontend').'/web';
|
38ed47e1
Administrator
20.07.16
|
122
|
|
dbceaf12
Administrator
20.07.16
|
123
|
$filename = 'sitemap.xml';
|
38ed47e1
Administrator
20.07.16
|
124
125
126
127
128
129
130
|
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) {
|
dbceaf12
Administrator
20.07.16
|
131
|
$this->createRow($page , 1,$content);
|
38ed47e1
Administrator
20.07.16
|
132
133
134
135
136
137
138
139
140
141
142
143
|
}
foreach ($this->getStaticPages() as $page) {
$url = Url::to(['text/index','translit' => $page->translit]);
$this->createRow($url , 1,$content);
}
foreach ($this->getCategories() as $category) {
$url = Url::to(['catalog/category', 'category' => $category]);
$this->createRow($url , 1,$content);
}
|
315ebf90
Alex Savenko
sitemap controlle...
|
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
|
// foreach ($this->getProducts() as $product) {
//
// $url = Url::to(['catalog/product', 'product' => $product]);
// $this->createRow($url , 0.9, $content);
// }
//
// foreach ($this->getArticles() as $article) {
//
// $url = Url::to(['articles/show', 'translit' => $article->translit, 'id' => $article->id,]);
// $this->createRow($url , 0.8,$content);
//
// }
//
// 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);
// }
// }
// }
//
// 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);
// }
//
// }
// }
//
// 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);
// }
//
// }
// }
// }
|
38ed47e1
Administrator
20.07.16
|
201
202
203
204
205
206
207
208
209
210
211
212
|
$content .= '</urlset>';
fwrite($handle, $content);
fclose($handle);
print $dirName .'/'. $filename;
}
}
|