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

303 lines
11 KiB
PHP
Raw Normal View History

<?php namespace RainLab\Blog\Components;
use Lang;
use Redirect;
use BackendAuth;
use Cms\Classes\Page;
use Cms\Classes\ComponentBase;
use October\Rain\Database\Model;
use October\Rain\Database\Collection;
use RainLab\Blog\Models\Post as BlogPost;
use RainLab\Blog\Models\Category as BlogCategory;
use RainLab\Blog\Models\Settings as BlogSettings;
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' => '',
],
2022-12-18 11:13:59 +00:00
'categoryGroup' => [
'title' => 'categoryGroup',
'description' => 'categoryGroup',
'type' => 'string',
'default' => '',
],
2022-12-14 15:55:13 +00:00
'typePost' => [
'title' => 'typePost',
'description' => 'typePost',
'type' => 'string',
'default' => '',
],
2021-05-21 13:27:57 +00:00
'featured' => [
'title' => 'Featured posts',
'description' => 'Filter featured posts',
2021-06-17 10:32:19 +00:00
'type' => 'dropdown',
'options' => ['yes' => 'Yes', 'not' => 'Not']
2021-05-21 13:27:57 +00:00
2022-12-23 09:55:31 +00:00
],
'morque' => [
'title' => 'Morque posts',
'description' => 'Filter morque posts',
'type' => 'dropdown',
'options' => ['yes' => 'Yes', 'not' => 'Not']
2021-05-21 13:27:57 +00:00
],
'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' => Lang::get('rainlab.blog::lang.settings.posts_no_posts_default'),
'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' => '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',
],
'exceptPost' => [
'title' => 'rainlab.blog::lang.settings.posts_except_post',
'description' => 'rainlab.blog::lang.settings.posts_except_post_description',
'type' => 'string',
'validationPattern' => '^[a-z0-9\-_,\s]+$',
'validationMessage' => 'rainlab.blog::lang.settings.posts_except_post_validation',
'default' => '',
'group' => 'rainlab.blog::lang.settings.group_exceptions',
],
'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',
],
];
}
public function getCategoryPageOptions()
{
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();
$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;
$categorySlug = $this->category ? $this->category->slug : null;
/*
* List all the posts, eager load their categories
*/
$isPublished = !$this->checkEditor();
2021-06-17 10:32:19 +00:00
$featured = $this->property('featured') ? $this->property('featured')=='yes':null;
2022-12-23 09:55:31 +00:00
$morque = $this->property('morque') ? $this->property('morque')=='yes':null;
2023-02-17 06:39:53 +00:00
// dump($this->property('typePost'));
$filters = [
'page' => $this->property('pageNumber'),
'sort' => $this->property('sortOrder'),
'perPage' => $this->property('postsPerPage'),
2021-06-17 10:32:19 +00:00
'featured' => $featured,
2022-12-23 09:55:31 +00:00
'morque' => $morque,
2023-02-17 06:39:53 +00:00
// 'typePost' => empty($this->property('typePost')) ? null : $this->property('typePost'),
// 'typePost' => empty($this->property('typePost')) ? null : is_array($this->property('typePost'))
// ? $this->property('typePost')
// : preg_split('/,\s*/', $this->property('typePost'), -1, PREG_SPLIT_NO_EMPTY),
2022-12-19 09:40:00 +00:00
'postGroup' => empty($this->property('categoryGroup')) ? null : $this->property('categoryGroup'),
2021-04-08 08:08:59 +00:00
'search' => trim(input('q')),
'category' => $category,
2021-04-02 09:22:25 +00:00
'date' => input('date'),
'published' => $isPublished,
'exceptPost' => is_array($this->property('exceptPost'))
? $this->property('exceptPost')
: preg_split('/,\s*/', $this->property('exceptPost'), -1, PREG_SPLIT_NO_EMPTY),
'exceptCategories' => is_array($this->property('exceptCategories'))
? $this->property('exceptCategories')
: preg_split('/,\s*/', $this->property('exceptCategories'), -1, PREG_SPLIT_NO_EMPTY),
2023-02-17 06:39:53 +00:00
];
if(!empty($this->property('typePost'))){
$filters["typePost"] = is_array($this->property('typePost'))
? $this->property('typePost')
: preg_split('/,\s*/', $this->property('typePost'), -1, PREG_SPLIT_NO_EMPTY);
}
$posts = BlogPost::with(['categories'])->listFrontEnd($filters);
2022-12-18 11:13:59 +00:00
//dd($this->property('categoryGroup'));
/*
* Add a "url" helper attribute for linking to each post and category
*/
$posts->each(function($post) use ($categorySlug) {
$post->setUrl($this->postPage, $this->controller, ['category' => $categorySlug]);
$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;
}
protected function checkEditor()
{
$backendUser = BackendAuth::getUser();
return $backendUser && $backendUser->hasAccess('rainlab.blog.access_posts') && BlogSettings::get('show_all_posts', true);
}
}