ORIENT/plugins/rainlab/blog/components/RssFeed.php

186 lines
6.0 KiB
PHP
Raw Normal View History

<?php namespace RainLab\Blog\Components;
use Lang;
use Response;
use Cms\Classes\Page;
use Cms\Classes\ComponentBase;
use RainLab\Blog\Models\Post as BlogPost;
use RainLab\Blog\Models\Category as BlogCategory;
2021-10-09 06:25:40 +00:00
use Carbon\Carbon;
class RssFeed extends ComponentBase
{
/**
* A collection of posts to display
* @var Collection
*/
public $posts;
/**
* If the post list should be filtered by a category, the model to use.
* @var Model
*/
public $category;
/**
* Reference to the page name for the main blog page.
* @var string
*/
public $blogPage;
/**
* Reference to the page name for linking to posts.
* @var string
*/
public $postPage;
public function componentDetails()
{
return [
'name' => 'rainlab.blog::lang.settings.rssfeed_title',
'description' => 'rainlab.blog::lang.settings.rssfeed_description'
];
}
public function defineProperties()
{
return [
'categoryFilter' => [
'title' => 'rainlab.blog::lang.settings.posts_filter',
'description' => 'rainlab.blog::lang.settings.posts_filter_description',
'type' => 'string',
'default' => '',
],
'sortOrder' => [
'title' => 'rainlab.blog::lang.settings.posts_order',
'description' => 'rainlab.blog::lang.settings.posts_order_description',
'type' => 'dropdown',
'default' => 'created_at desc',
],
'postsPerPage' => [
'title' => 'rainlab.blog::lang.settings.posts_per_page',
'type' => 'string',
'validationPattern' => '^[0-9]+$',
'validationMessage' => 'rainlab.blog::lang.settings.posts_per_page_validation',
'default' => '10',
],
'blogPage' => [
'title' => 'rainlab.blog::lang.settings.rssfeed_blog',
'description' => 'rainlab.blog::lang.settings.rssfeed_blog_description',
'type' => 'dropdown',
'default' => 'blog/post',
'group' => 'rainlab.blog::lang.settings.group_links',
],
'postPage' => [
'title' => 'rainlab.blog::lang.settings.posts_post',
'description' => 'rainlab.blog::lang.settings.posts_post_description',
'type' => 'dropdown',
'default' => 'blog/post',
'group' => 'rainlab.blog::lang.settings.group_links',
],
2021-10-09 06:25:40 +00:00
'source' => [
'title' => 'Source',
'description' => 'Source google, yandex',
'type' => 'dropdown',
2021-10-11 08:16:01 +00:00
'default' => 'google',
],
'exceptCategories' => [
'title' => 'rainlab.blog::lang.settings.posts_except_categories',
'description' => 'rainlab.blog::lang.settings.posts_except_categories_description',
'type' => 'string',
'validationPattern' => '^[a-z0-9\-_,\s]+$',
'validationMessage' => 'rainlab.blog::lang.settings.posts_except_categories_validation',
'default' => '',
'group' => 'rainlab.blog::lang.settings.group_exceptions',
2021-10-09 06:25:40 +00:00
]
];
}
2021-10-09 06:25:40 +00:00
public function getSourceOptions(){
2021-10-11 08:16:01 +00:00
return ['google'=>'google','yandex'=>'yandex'];
2021-10-09 06:25:40 +00:00
}
2021-10-11 08:16:01 +00:00
public function getBlogPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
public function getPostPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
public function getSortOrderOptions()
{
$options = BlogPost::$allowedSortingOptions;
foreach ($options as $key => $value) {
$options[$key] = Lang::get($value);
}
return $options;
}
public function onRun()
{
$this->prepareVars();
2021-10-09 06:25:40 +00:00
$xmlFeed = $this->renderPartial('@'.($this->property('source')=='yandex'?'yandex':'default'));
return Response::make($xmlFeed, '200')->header('Content-Type', 'text/xml');
}
protected function prepareVars()
{
$this->blogPage = $this->page['blogPage'] = $this->property('blogPage');
$this->postPage = $this->page['postPage'] = $this->property('postPage');
$this->category = $this->page['category'] = $this->loadCategory();
$this->posts = $this->page['posts'] = $this->listPosts();
2021-10-09 06:25:40 +00:00
$this->page['language'] = \App::getLocale();
$this->page['link'] = $this->pageUrl($this->blogPage);
$this->page['rssLink'] = $this->currentPageUrl();
2021-10-09 06:25:40 +00:00
//Carbon::setLocale('en');
2021-10-11 08:16:01 +00:00
}
protected function listPosts()
{
$category = $this->category ? $this->category->id : null;
/*
* List all the posts, eager load their categories
*/
2021-10-11 08:16:01 +00:00
$posts = BlogPost::listFrontEnd([
'sort' => $this->property('sortOrder'),
'perPage' => $this->property('postsPerPage'),
2021-10-11 08:16:01 +00:00
'category' => $category,
'exceptCategories' => is_array($this->property('exceptCategories'))
? $this->property('exceptCategories')
: preg_split('/,\s*/', $this->property('exceptCategories'), -1, PREG_SPLIT_NO_EMPTY),
]);
/*
* Add a "url" helper attribute for linking to each post and category
*/
$posts->each(function($post) {
$post->setUrl($this->postPage, $this->controller);
});
return $posts;
}
protected function loadCategory()
{
if (!$categoryId = $this->property('categoryFilter')) {
return null;
}
if (!$category = BlogCategory::whereSlug($categoryId)->first()) {
return null;
}
return $category;
}
}