From 74d06b0fad9fe4ba595d66c7aa6f4d9fbe28f188 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 19 May 2017 15:07:22 +0300 Subject: [PATCH] -Site map generation fixed --- common/modules/rubrication/models/TaxGroup.php | 344 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------- console/config/main.php | 2 +- console/controllers/SiteMapController.php | 492 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 3 files changed, 473 insertions(+), 365 deletions(-) diff --git a/common/modules/rubrication/models/TaxGroup.php b/common/modules/rubrication/models/TaxGroup.php index 857a20d..c298e81 100755 --- a/common/modules/rubrication/models/TaxGroup.php +++ b/common/modules/rubrication/models/TaxGroup.php @@ -1,148 +1,208 @@ [ - 'class' => 'common\behaviors\Slug', - 'in_attribute' => 'name', - 'out_attribute' => 'alias', - 'translit' => true - ], - ]; - } - - /** - * @inheritdoc - */ - public static function tableName() - { - return 'tax_group'; - } - - /** - * @inheritdoc - */ - public function rules() - { - return [ - [['name', 'module'], 'required'], - [['description', 'settings'], 'string'], - [['hierarchical', 'is_filter', 'display','is_menu', 'use_in_name'], 'boolean'], - [['level', 'sort'], 'integer'], - [['alias', 'module'], 'string', 'max' => 50], - [['name'], 'string', 'max' => 255], - [['categories'], 'safe'], - [['use_in_name'], 'default', 'value' => false], - ]; - } - - /** - * @inheritdoc - */ - public function attributeLabels() - { - return [ - 'tax_group_id' => 'Tax Group ID', - 'alias' => 'Alias', - 'name' => 'Name', - 'description' => 'Description', - 'module' => 'Module', - 'hierarchical' => 'Hierarchical', -// 'settings' => 'Settings', - 'is_filter' => 'Use in filter', - 'sort' => 'Sort', - 'display' => 'Display', - 'is_menu' => 'Отображать в меню', - 'use_in_name' => 'Использовать в названии', - ]; - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getTaxGroupToGroups() - { - return $this->hasMany(TaxGroupToGroup::className(), ['tax_group1_id' => 'tax_group_id'])->inverseOf('taxGroup1'); - } - - public function getCategories() - { - return $this->hasMany(Category::className(), ['category_id' => 'category_id']) - ->viaTable('tax_group_to_category', ['tax_group_id' => 'tax_group_id']); - } - - public function setCategories($values) - { - $this->categories = $values; - } - - public function afterSave($insert, $changedAttributes) - { - - $this->unlinkAll('categories',true); - $categories = Category::findAll($this->categories); - foreach($categories as $category){ - $this->link('categories', $category); + public $_options = []; + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'slug' => [ + 'class' => 'common\behaviors\Slug', + 'in_attribute' => 'name', + 'out_attribute' => 'alias', + 'translit' => true, + ], + ]; + } + + /** + * @inheritdoc + */ + public static function tableName() + { + return 'tax_group'; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'name', + 'module', + ], + 'required', + ], + [ + [ + 'description', + 'settings', + ], + 'string', + ], + [ + [ + 'hierarchical', + 'is_filter', + 'display', + 'is_menu', + 'use_in_name', + ], + 'boolean', + ], + [ + [ + 'level', + 'sort', + ], + 'integer', + ], + [ + [ + 'alias', + 'module', + ], + 'string', + 'max' => 50, + ], + [ + [ 'name' ], + 'string', + 'max' => 255, + ], + [ + [ 'categories' ], + 'safe', + ], + [ + [ 'use_in_name' ], + 'default', + 'value' => false, + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'tax_group_id' => 'Tax Group ID', + 'alias' => 'Alias', + 'name' => 'Name', + 'description' => 'Description', + 'module' => 'Module', + 'hierarchical' => 'Hierarchical', + // 'settings' => 'Settings', + 'is_filter' => 'Use in filter', + 'sort' => 'Sort', + 'display' => 'Display', + 'is_menu' => 'Отображать в меню', + 'use_in_name' => 'Использовать в названии', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaxGroupToGroups() + { + return $this->hasMany(TaxGroupToGroup::className(), [ 'tax_group1_id' => 'tax_group_id' ]) + ->inverseOf('taxGroup1'); + } + + public function getCategories() + { + return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ]) + ->viaTable('tax_group_to_category', [ 'tax_group_id' => 'tax_group_id' ]); + } + + /** + * @return ActiveQuery + */ + public function getCategory() + { + return $this->hasOne(Category::className(), [ 'category_id' => 'category_id' ]) + ->viaTable('tax_group_to_category', [ 'tax_group_id' => 'tax_group_id' ]); + } + + public function setCategories($values) + { + $this->categories = $values; + } + + public function afterSave($insert, $changedAttributes) + { + + $this->unlinkAll('categories', true); + $categories = Category::findAll($this->categories); + foreach ($categories as $category) { + $this->link('categories', $category); + } + + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getOptions() + { + return $this->getTaxOptions(); + } + public function getTaxOptions() + { + return $this->hasMany(TaxOption::className(), [ 'tax_group_id' => 'tax_group_id' ]) + ->inverseOf('taxGroup'); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getTaxOptionToGroups() + { + return $this->hasMany(TaxOptionToGroup::className(), [ 'tax_group_id' => 'tax_group_id' ]) + ->inverseOf('taxGroup'); + } + + public function getValueModelName($full_path = true) + { + $valueClass = 'TaxValue' . ucfirst($this->module); + $fullClass = '\common\modules\rubrication\models\\' . $valueClass; + return class_exists($fullClass) ? $full_path ? $fullClass : $valueClass : false; } - - - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getOptions() { - return $this->getTaxOptions(); - } - public function getTaxOptions() - { - return $this->hasMany(TaxOption::className(), ['tax_group_id' => 'tax_group_id'])->inverseOf('taxGroup'); - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getTaxOptionToGroups() - { - return $this->hasMany(TaxOptionToGroup::className(), ['tax_group_id' => 'tax_group_id'])->inverseOf('taxGroup'); - } - - public function getValueModelName($full_path = true) { - $valueClass = 'TaxValue'. ucfirst($this->module); - $fullClass = '\common\modules\rubrication\models\\'. $valueClass; - return class_exists($fullClass) ? $full_path ? $fullClass : $valueClass : FALSE; } -} diff --git a/console/config/main.php b/console/config/main.php index e9d14a5..e2e68ac 100755 --- a/console/config/main.php +++ b/console/config/main.php @@ -13,7 +13,7 @@ return [ 'controllerNamespace' => 'console\controllers', 'components' => [ 'urlManager' => [ - 'baseUrl' => 'http://www.linija-svitla.ua', + 'baseUrl' => 'https://www.linija-svitla.ua', 'enablePrettyUrl' => true, 'showScriptName' => false, diff --git a/console/controllers/SiteMapController.php b/console/controllers/SiteMapController.php index 7966bf2..d1595d4 100755 --- a/console/controllers/SiteMapController.php +++ b/console/controllers/SiteMapController.php @@ -1,239 +1,287 @@ search($category, $filter); - if(!empty($productProvider->models)){ - return true; - } else { - return false; - } - } - - - - public function getAddStatic(){ - return [ - 'http://www.linija-svitla.ua', - 'http://www.linija-svitla.ua/catalog' - ]; - } - - - 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(); - } - - public function getBrands($category){ - - return $category->brands; - } - + + 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 common\modules\rubrication\models\TaxOption; + use frontend\models\ProductFrontendSearch; + use Yii; + use common\models\Page; + use common\models\PageSearch; + use yii\helpers\ArrayHelper; + use yii\helpers\Console; + use yii\helpers\Url; + use yii\console\Controller; + use yii\web\NotFoundHttpException; + use yii\filters\VerbFilter; + use developeruz\db_rbac\behaviors\AccessBehavior; + /** - * @param $category Category; - * @return mixed + * PageController implements the CRUD actions for Page model. */ - - public function getFilters($category){ - - return $category->getActiveFilters(); - - } - - - public function checkUrl($url){ - if(!in_array($url, $this->urlList)){ - $this->urlList[] = $url; - return true; - } else { - return false; + class SiteMapController extends Controller + { + + private $urlList = [ 'http://www.linija-svitla.ua/' ]; + 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 createRow( $url, $priority, &$content ){ -// if($this->checkUrl($url)){ - $this->stdout( '# ' . $this->count++ . "\n", Console::FG_GREEN); - $content .= '' . - '' . $url . '' . - '' . date('Y-m-d') . '' . - 'Weekly' . - '' . $priority .'' . - ''; -// } - } - - - 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 = ''; - $this->stdout('Add static' . "\n", Console::FG_BLUE); - - foreach ($this->getAddStatic() as $page) { - $this->createRow($page , 1,$content); + /** + * @return array + */ + public function getAddStatic() + { + return [ + 'https://www.linija-svitla.ua', + 'https://www.linija-svitla.ua/catalog', + ]; } - $this->stdout('Add static pages' . "\n", Console::FG_BLUE); - - foreach ($this->getStaticPages() as $page) { - $url = Url::to(['text/main','translit' => $page->translit]); - - $this->createRow($url , 1,$content); + + /** + * @return array|\yii\db\ActiveRecord[] + */ + public function getProducts() + { + return Product::find() + ->all(); + } - $this->stdout('Add categories' . "\n", Console::FG_BLUE); - - foreach ($this->getCategories() as $category) { - - $url = Url::to(['catalog/category', 'category' => $category]); - $this->createRow($url , 1,$content); + + /** + * @return array|\yii\db\ActiveRecord[] + */ + public function getSeoLinks() + { + return Seo::find() + ->where( + [ + 'not like', + 'meta', + '%noindex%', + ] + ) + ->all(); + } - - $this->stdout('Add products' . "\n", Console::FG_BLUE); - - foreach ($this->getProducts() as $product) { - - $url = Url::to(['catalog/product', 'product' => $product]); - $this->createRow($url , 0.9, $content); + + /** + * @return array|\yii\db\ActiveRecord[] + */ + public function getStaticPages() + { + return Page::find() + ->all(); } - - $this->stdout('Add articles' . "\n", Console::FG_BLUE); - - foreach ($this->getArticles() as $article) { - - $url = Url::to(['articles/show', 'translit' => $article->translit]); - $this->createRow($url , 0.8,$content); - + + /** + * @return array|\common\modules\product\models\Category[]|\yii\db\ActiveRecord[] + */ + public function getCategories() + { + return Category::find() + ->all(); } - - $this->stdout('Add brands' . "\n", Console::FG_BLUE); - - 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); - } + + /** + * @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', + ] + ) + ->all(); + + } + + public function checkUrl($url) + { + if (!in_array($url, $this->urlList)) { + $this->urlList[] = $url; + return true; + } else { + return false; } } - - $this->stdout('Add filters' . "\n", Console::FG_BLUE); - - 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); + + public function createRow($url, $priority, &$content) + { + // if($this->checkUrl($url)){ + $this->stdout('# ' . $this->count++ . ' ' . $url . "\n", Console::FG_GREEN); + $content .= '' . '' . $url . '' . '' . date( + 'Y-m-d' + ) . '' . 'Weekly' . '' . $priority . '' . ''; + // } + } + + 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 = ''; + + $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 + ); + + $this->createRow($url, 1, $content); + } + $this->stdout('Add categories' . "\n", Console::FG_BLUE); + + foreach ($this->getCategories() as $category) { + + $url = Url::to( + [ + 'catalog/category', + 'category' => $category, + ], + true + ); + $this->createRow($url, 1, $content); + } + + $this->stdout('Add products' . "\n", Console::FG_BLUE); + + foreach ($this->getProducts() as $product) { + + $url = Url::to( + [ + 'catalog/product', + 'product' => $product, + ], + true + ); + $this->createRow($url, 0.9, $content); + } + + $this->stdout('Add articles' . "\n", Console::FG_BLUE); + + foreach ($this->getArticles() as $article) { + + $url = Url::to( + [ + 'articles/show', + 'translit' => $article->translit, + ], + true + ); + $this->createRow($url, 0.8, $content); + + } + + $this->stdout('Add brands' . "\n", Console::FG_BLUE); + + 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 ] ], + ], + true + ); + $this->createRow($url, 0.8, $content); + } } - } + + $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 + ); + $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; + $this->createRow($url, 0.7, $content); + + } + + $content .= ''; + + fwrite($handle, $content); + fclose($handle); + $this->stdout('File name: ' . $dirName . '/' . $filename . "\n", Console::FG_CYAN); + } - - $this->stdout('Add seo links' . "\n", Console::FG_BLUE); - - 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 .= ''; - - fwrite($handle, $content); - fclose($handle); - $this->stdout('File name: ' . $dirName .'/'. $filename . "\n", Console::FG_CYAN); } - -} -- libgit2 0.21.4