Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activity pages and elements. Add type page #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
101 changes: 90 additions & 11 deletions src/ExportSitemapHandler.php
Expand Up @@ -41,6 +41,7 @@
use yii\helpers\Url;
use yii\web\UrlNormalizer;
use yii\widgets\ActiveForm;
use yii\web\View;

/**
* @property string $rootSitemapsDir
Expand All @@ -60,6 +61,11 @@ class ExportSitemapHandler extends ExportHandler
*/
public $tree_ids = [];

/**
* @var null учет типов разделов дерева
*/
public $tree_type_ids = [];

/**
* @var null базовый путь сайта
*/
Expand All @@ -70,6 +76,16 @@ class ExportSitemapHandler extends ExportHandler
*/
public $sitemaps_dir = null;

/**
* @var bool
*/
public $active_tree = true; //Учитывать активность разделов

/**
* @var bool
*/
public $active_content_elem = true; //Учитывать активность элемента

/**
* @var string путь к результирующему файлу
*/
Expand Down Expand Up @@ -124,15 +140,15 @@ public function rules()
['content_ids' , 'required'],
['content_ids' , 'safe'],

['tree_ids' , 'safe'],
[['tree_ids', 'tree_type_ids'] , 'safe'],

['base_url' , 'required'],
['base_url' , 'url'],

['site_id' , 'string'],
['sitemaps_dir' , 'string'],

['max_urlsets' , 'integer'],
[['max_urlsets', 'active_content_elem', 'active_tree'], 'integer'],

]);
}
Expand All @@ -146,6 +162,9 @@ public function attributeLabels()
'sitemaps_dir' => \Yii::t('skeeks/exportShopYandexMarket', 'Папка частей sitemap'),
'site_id' => \Yii::t('skeeks/exportShopYandexMarket', 'Сайт'),
'max_urlsets' => \Yii::t('skeeks/exportShopYandexMarket', 'Максимальное количество urlsets в одном файле'),
'active_tree' => \Yii::t('skeeks/exportSitemap', 'Active flag to tree'),
'active_content_elem' => \Yii::t('skeeks/exportSitemap', 'Active flag to contents element'),
'tree_type_ids' => \Yii::t('skeeks/exportSitemap', 'Types of tree'),
]);
}
public function attributeHints()
Expand Down Expand Up @@ -182,6 +201,11 @@ public function renderConfigForm(ActiveForm $form)
'mode' => SelectTree::MOD_MULTI
]
);
echo $form->fieldSelectMulti($this, 'tree_type_ids', ArrayHelper::map(
\skeeks\cms\models\CmsTreeType::find()->all(), 'id', 'name'
));
echo $form->field($this, 'active_content_elem')->checkbox(\Yii::$app->formatter->booleanFormat);
echo $form->field($this, 'active_tree')->checkbox(\Yii::$app->formatter->booleanFormat);

echo $form->field($this, 'max_urlsets');
}
Expand All @@ -205,7 +229,21 @@ public function export()
throw new Exception("Не удалось создать директорию для файла");
}
}
$result = [];
$sitemap = [];

$this->result->stdout("\tСоздание файла siemap для разделов\n");
$result = $this->_addTrees($result);

if ($result)
{
$publicUrl = $this->generateSitemapFile('tree.xml', $result);
$this->result->stdout("\tФайл успешно сгенерирован: {$publicUrl}\n");

$sitemap[] = $publicUrl;
}

/*
$query = Tree::find()
->orderBy(['level' => SORT_ASC, 'priority' => SORT_ASC]);
if ($this->site_id)
Expand All @@ -214,17 +252,13 @@ public function export()
}
$trees = $query->all();

$result = [];

$this->result->stdout("\tСоздание файла siemap для разделов\n");

$sitemap = [];
if ($trees)
{
/**
* @var Tree $tree
*/
foreach ($trees as $tree)
/* foreach ($trees as $tree)
{
if (!$tree->redirect)
{
Expand All @@ -236,14 +270,12 @@ public function export()
}
}

$publicUrl = $this->generateSitemapFile('tree.xml', $result);
$this->result->stdout("\tФайл успешно сгенерирован: {$publicUrl}\n");

$sitemap[] = $publicUrl;
}

*/
if ($this->content_ids)
{

$this->result->stdout("\tЭкспорт контента\n");

foreach ($this->content_ids as $contentId)
Expand Down Expand Up @@ -289,6 +321,49 @@ public function export()
return $this->result;
}

/**
* @param array $data
* @return array
*/
protected function _addTrees(&$data = [])
{
$query = Tree::find();

if (!empty($this->site_id))
{
$query->where(['cms_site_id' => $this->site_id]);
}

if ($this->active_tree) {
$query->andWhere(['active' => 'Y']);
}

if ($this->tree_type_ids) {
$query->andWhere(['tree_type_id' => $this->tree_type_ids]);
}

$trees = $query->orderBy(['level' => SORT_ASC, 'priority' => SORT_ASC])->all();

$result = [];

if ($trees) {
/**
* @var Tree $tree
*/
foreach ($trees as $tree) {
if (!$tree->redirect && !$tree->redirect_tree_id) {
$result[] =
[
"loc" => $tree->absoluteUrl,
"lastmod" => $this->_lastMod($tree),
];
}
}
}

return $result;
}

/**
* @param CmsContent $cmsContent
* @return array
Expand All @@ -303,6 +378,10 @@ protected function _exportContent(CmsContent $cmsContent)
->where(['content_id' => $cmsContent->id])
->orderBy(['published_at' => SORT_DESC]);

if ($this->active_content_elem) {
$query->andWhere([CmsContentElement::tableName() . '.active' => 'Y']);
}

$countQuery = clone $query;
$total = $countQuery->count();

Expand Down
3 changes: 3 additions & 0 deletions src/messages/ru/main.php
@@ -1,4 +1,7 @@
<?php
return [
'Sitemap.xml export' => 'Генерация sitemap.xml',
'Active flag to contents element' => 'Учитывать флаг активности для элементов контента',
'Active flag to tree' => 'Учитывать флаг активности для разделов',
'Types of tree' => 'Укажите типы разделов',
];