Files
godot-website/plugins/rainlab/blog/components/Posts.php
2017-09-22 21:46:47 +00:00

214 lines
7.0 KiB
PHP

<?php namespace RainLab\Blog\Components;
use Redirect;
use Cms\Classes\Page;
use Cms\Classes\ComponentBase;
use RainLab\Blog\Models\Post as BlogPost;
use RainLab\Blog\Models\Category as BlogCategory;
class Posts extends ComponentBase
{
/**
* A collection of posts to display
* @var Collection
*/
public $posts;
/**
* Parameter to use for the page number
* @var string
*/
public $pageParam;
/**
* If the post list should be filtered by a category, the model to use.
* @var Model
*/
public $category;
/**
* Message to display when there are no messages.
* @var string
*/
public $noPostsMessage;
/**
* Reference to the page name for linking to posts.
* @var string
*/
public $postPage;
/**
* Reference to the page name for linking to categories.
* @var string
*/
public $categoryPage;
/**
* If the post list should be ordered by another attribute.
* @var string
*/
public $sortOrder;
public function componentDetails()
{
return [
'name' => 'rainlab.blog::lang.settings.posts_title',
'description' => 'rainlab.blog::lang.settings.posts_description'
];
}
public function defineProperties()
{
return [
'pageNumber' => [
'title' => 'rainlab.blog::lang.settings.posts_pagination',
'description' => 'rainlab.blog::lang.settings.posts_pagination_description',
'type' => 'string',
'default' => '{{ :page }}',
],
'categoryFilter' => [
'title' => 'rainlab.blog::lang.settings.posts_filter',
'description' => 'rainlab.blog::lang.settings.posts_filter_description',
'type' => 'string',
'default' => ''
],
'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',
],
'noPostsMessage' => [
'title' => 'rainlab.blog::lang.settings.posts_no_posts',
'description' => 'rainlab.blog::lang.settings.posts_no_posts_description',
'type' => 'string',
'default' => 'No posts found',
'showExternalParam' => false
],
'sortOrder' => [
'title' => 'rainlab.blog::lang.settings.posts_order',
'description' => 'rainlab.blog::lang.settings.posts_order_description',
'type' => 'dropdown',
'default' => 'published_at desc'
],
'categoryPage' => [
'title' => 'rainlab.blog::lang.settings.posts_category',
'description' => 'rainlab.blog::lang.settings.posts_category_description',
'type' => 'dropdown',
'default' => 'blog/category',
'group' => 'Links',
],
'postPage' => [
'title' => 'rainlab.blog::lang.settings.posts_post',
'description' => 'rainlab.blog::lang.settings.posts_post_description',
'type' => 'dropdown',
'default' => 'blog/post',
'group' => 'Links',
],
'exceptPost' => [
'title' => 'rainlab.blog::lang.settings.posts_except_post',
'description' => 'rainlab.blog::lang.settings.posts_except_post_description',
'type' => 'string',
'validationPattern' => 'string',
'validationMessage' => 'rainlab.blog::lang.settings.posts_except_post_validation',
'default' => '',
'group' => 'Exceptions',
],
];
}
public function getCategoryPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
public function getPostPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
public function getSortOrderOptions()
{
return BlogPost::$allowedSortingOptions;
}
public function onRun()
{
$this->prepareVars();
$this->category = $this->page['category'] = $this->loadCategory();
$this->posts = $this->page['posts'] = $this->listPosts();
/*
* If the page number is not valid, redirect
*/
if ($pageNumberParam = $this->paramName('pageNumber')) {
$currentPage = $this->property('pageNumber');
if ($currentPage > ($lastPage = $this->posts->lastPage()) && $currentPage > 1)
return Redirect::to($this->currentPageUrl([$pageNumberParam => $lastPage]));
}
}
protected function prepareVars()
{
$this->pageParam = $this->page['pageParam'] = $this->paramName('pageNumber');
$this->noPostsMessage = $this->page['noPostsMessage'] = $this->property('noPostsMessage');
/*
* Page links
*/
$this->postPage = $this->page['postPage'] = $this->property('postPage');
$this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage');
}
protected function listPosts()
{
$category = $this->category ? $this->category->id : null;
/*
* List all the posts, eager load their categories
*/
$posts = BlogPost::with('categories')->listFrontEnd([
'page' => $this->property('pageNumber'),
'sort' => $this->property('sortOrder'),
'perPage' => $this->property('postsPerPage'),
'search' => trim(input('search')),
'category' => $category,
'exceptPost' => $this->property('exceptPost'),
]);
/*
* Add a "url" helper attribute for linking to each post and category
*/
$posts->each(function($post) {
$post->setUrl($this->postPage, $this->controller);
$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
});
return $posts;
}
protected function loadCategory()
{
if (!$slug = $this->property('categoryFilter')) {
return null;
}
$category = new BlogCategory;
$category = $category->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
? $category->transWhere('slug', $slug)
: $category->where('slug', $slug);
$category = $category->first();
return $category ?: null;
}
}