Commit e6357e22c1a852de75c3ea0efded31b1e72b9ae2
1 parent
50e38c82
big commti
Showing
2 changed files
with
232 additions
and
0 deletions
Show diff stats
console/config/main.php
| ... | ... | @@ -12,6 +12,12 @@ return [ |
| 12 | 12 | 'bootstrap' => ['log'], |
| 13 | 13 | 'controllerNamespace' => 'console\controllers', |
| 14 | 14 | 'components' => [ |
| 15 | + 'urlManager' => [ | |
| 16 | + 'baseUrl' => 'http://www.linija-svitla.ua', | |
| 17 | + 'enablePrettyUrl' => true, | |
| 18 | + 'showScriptName' => false, | |
| 19 | + | |
| 20 | + ], | |
| 15 | 21 | 'log' => [ |
| 16 | 22 | 'targets' => [ |
| 17 | 23 | [ | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace console\controllers; | |
| 4 | + | |
| 5 | +use common\models\Articles; | |
| 6 | +use common\models\Seo; | |
| 7 | +use common\modules\product\helpers\FilterHelper; | |
| 8 | +use common\modules\product\models\Brand; | |
| 9 | +use common\modules\product\models\Category; | |
| 10 | +use common\modules\product\models\Product; | |
| 11 | +use frontend\models\ProductFrontendSearch; | |
| 12 | +use Yii; | |
| 13 | +use common\models\Page; | |
| 14 | +use common\models\PageSearch; | |
| 15 | +use yii\helpers\ArrayHelper; | |
| 16 | +use yii\helpers\Url; | |
| 17 | +use yii\console\Controller; | |
| 18 | +use yii\web\NotFoundHttpException; | |
| 19 | +use yii\filters\VerbFilter; | |
| 20 | +use developeruz\db_rbac\behaviors\AccessBehavior; | |
| 21 | +/** | |
| 22 | + * PageController implements the CRUD actions for Page model. | |
| 23 | + */ | |
| 24 | +class SiteMapController extends Controller | |
| 25 | +{ | |
| 26 | + | |
| 27 | + private $urlList = ['http://www.rukzachok.com.ua/']; | |
| 28 | + private $count = 1; | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + public function checkFilter($category, $filter){ | |
| 33 | + $productModel = new ProductFrontendSearch(); | |
| 34 | + $productProvider = $productModel->search($category, $filter); | |
| 35 | + if(!empty($productProvider->models)){ | |
| 36 | + return true; | |
| 37 | + } else { | |
| 38 | + return false; | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + public function getAddStatic(){ | |
| 45 | + return [ | |
| 46 | + 'http://www.rukzachok.com.ua', | |
| 47 | + 'http://www.rukzachok.com.ua/catalog' | |
| 48 | + ]; | |
| 49 | + } | |
| 50 | + | |
| 51 | + | |
| 52 | + public function getProducts() { | |
| 53 | + return Product::find()->all(); | |
| 54 | + | |
| 55 | + } | |
| 56 | + | |
| 57 | + | |
| 58 | + public function getSeoLinks() { | |
| 59 | + return Seo::find()->where(['meta' => ''])->all(); | |
| 60 | + | |
| 61 | + } | |
| 62 | + | |
| 63 | + public function getStaticPages(){ | |
| 64 | + return Page::find()->all(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + | |
| 68 | + public function getCategories(){ | |
| 69 | + return Category::find()->all(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + | |
| 73 | + public function getArticles(){ | |
| 74 | + return Articles::find()->all(); | |
| 75 | + } | |
| 76 | + | |
| 77 | + public function getBrands($category){ | |
| 78 | + | |
| 79 | + return $category->brands; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * @param $category Category; | |
| 84 | + * @return mixed | |
| 85 | + */ | |
| 86 | + | |
| 87 | + public function getFilters($category){ | |
| 88 | + | |
| 89 | + return $category->getActiveFilters()->all(); | |
| 90 | + | |
| 91 | + } | |
| 92 | + | |
| 93 | + | |
| 94 | + public function checkUrl($url){ | |
| 95 | + if(!in_array($url, $this->urlList)){ | |
| 96 | + $this->urlList[] = $url; | |
| 97 | + return true; | |
| 98 | + } else { | |
| 99 | + return false; | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + | |
| 104 | + public function createRow( $url, $priority, &$content ){ | |
| 105 | + if($this->checkUrl($url)){ | |
| 106 | + print $this->count++ . "\n"; | |
| 107 | + $content .= '<url>' . | |
| 108 | + '<loc>' . $url . '</loc>' . | |
| 109 | + '<lastmod>' . date('Y-m-d') . '</lastmod>' . | |
| 110 | + '<changefreq>Daily</changefreq>' . | |
| 111 | + '<priority>' . $priority .'</priority>' . | |
| 112 | + '</url>'; | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + | |
| 117 | + public function actionProcess() { | |
| 118 | + | |
| 119 | + $config = ArrayHelper::merge( | |
| 120 | + require(__DIR__ . '/../../frontend/config/main.php'), | |
| 121 | + require(__DIR__ . '/../../common/config/main.php') | |
| 122 | + | |
| 123 | + ); | |
| 124 | + | |
| 125 | + Yii::$app->urlManager->addRules($config['components']['urlManager']['rules']); | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + $dirName = Yii::getAlias('@frontend').'/web'; | |
| 130 | + | |
| 131 | + $filename = 'sitemap.xml'; | |
| 132 | + | |
| 133 | + setlocale(LC_ALL, 'ru_RU.CP1251'); | |
| 134 | + $handle = fopen($dirName .'/'. $filename, "w"); | |
| 135 | + | |
| 136 | + $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; | |
| 137 | + | |
| 138 | + foreach ($this->getAddStatic() as $page) { | |
| 139 | + $this->createRow($page , 1,$content); | |
| 140 | + } | |
| 141 | + | |
| 142 | + foreach ($this->getStaticPages() as $page) { | |
| 143 | + $url = Url::to(['text/index','translit' => $page->translit]); | |
| 144 | + $this->createRow($url , 1,$content); | |
| 145 | + } | |
| 146 | + | |
| 147 | + foreach ($this->getCategories() as $category) { | |
| 148 | + $url = Url::to(['catalog/category', 'category' => $category]); | |
| 149 | + $this->createRow($url , 1,$content); | |
| 150 | + } | |
| 151 | + | |
| 152 | + | |
| 153 | + foreach ($this->getProducts() as $product) { | |
| 154 | + | |
| 155 | + $url = Url::to(['catalog/product', 'product' => $product]); | |
| 156 | + $this->createRow($url , 0.9, $content); | |
| 157 | + } | |
| 158 | + | |
| 159 | + | |
| 160 | + foreach ($this->getArticles() as $article) { | |
| 161 | + | |
| 162 | + $url = Url::to(['articles/show', 'translit' => $article->translit, 'id' => $article->id,]); | |
| 163 | + $this->createRow($url , 0.8,$content); | |
| 164 | + | |
| 165 | + } | |
| 166 | + | |
| 167 | + | |
| 168 | + foreach($this->getCategories() as $category){ | |
| 169 | + foreach ($this->getBrands($category) as $brand) { | |
| 170 | + if($this->checkFilter($category, ['brands' => [$brand->brand_id]])){ | |
| 171 | + $url = Url::to(['catalog/category', 'category' => $category, 'filters' => ['brands' => [$brand->alias]]]) ; | |
| 172 | + $this->createRow($url , 0.8, $content); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + } | |
| 176 | + | |
| 177 | + | |
| 178 | + foreach($this->getCategories() as $category){ | |
| 179 | + foreach ($this->getFilters($category) as $filter) { | |
| 180 | + if($this->checkFilter($category, [$filter['group_alias'] => [$filter['option_alias']]])){ | |
| 181 | + $url = Url::to(['catalog/category', 'category' => $category, 'filters' => [$filter['group_alias'] => [$filter['option_alias']]] ]); | |
| 182 | + $this->createRow($url , 0.8, $content); | |
| 183 | + } | |
| 184 | + | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + foreach($this->getSeoLinks() as $link){ | |
| 189 | + $url = Yii::$app->urlManager->baseUrl.$link->url; | |
| 190 | + $this->createRow($url , 0.7, $content); | |
| 191 | + | |
| 192 | + } | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | +// foreach($this->getCategories() as $category){ | |
| 197 | +// foreach ($this->getFilters($category) as $filter1) { | |
| 198 | +// foreach ($this->getFilters($category) as $filter2) { | |
| 199 | +// if($this->checkFilter($category, [$filter1['group_alias'] => [$filter1['option_alias']],$filter2['group_alias'] => [$filter2['option_alias']]] )){ | |
| 200 | +// $url = Url::to(['catalog/category', 'category' => $category, 'filters' => [$filter1['group_alias'] => [$filter1['option_alias']],$filter2['group_alias'] => [$filter2['option_alias']]] ]); | |
| 201 | +// $this->createRow($url , 0.7, $content); | |
| 202 | +// } | |
| 203 | +// | |
| 204 | +// } | |
| 205 | +// | |
| 206 | +// foreach ($this->getBrands($category) as $brand) { | |
| 207 | +// if($this->checkFilter($category, ['brands' => [$brand->brand_id], $filter1['group_alias'] => [$filter1['option_alias']]] )){ | |
| 208 | +// $url = Url::to(['catalog/category', 'category' => $category, 'filters' => ['brands' => [$brand->alias],$filter1['group_alias'] => [$filter1['option_alias']]]]); | |
| 209 | +// $this->createRow($url , 0.7,$content); | |
| 210 | +// } | |
| 211 | +// | |
| 212 | +// } | |
| 213 | +// } | |
| 214 | +// } | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + $content .= '</urlset>'; | |
| 219 | + | |
| 220 | + fwrite($handle, $content); | |
| 221 | + fclose($handle); | |
| 222 | + | |
| 223 | + print $dirName .'/'. $filename; | |
| 224 | + } | |
| 225 | + | |
| 226 | +} | ... | ... |