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

113 lines
3.0 KiB
PHP

<?php namespace RainLab\Blog\Components;
use Cms\Classes\Page;
use Cms\Classes\ComponentBase;
use RainLab\Blog\Models\Post as BlogPost;
class Post extends ComponentBase
{
/**
* @var RainLab\Blog\Models\Post The post model used for display.
*/
public $post;
/**
* @var string Reference to the page name for linking to categories.
*/
public $categoryPage;
public function componentDetails()
{
return [
'name' => 'rainlab.blog::lang.settings.post_title',
'description' => 'rainlab.blog::lang.settings.post_description'
];
}
public function defineProperties()
{
return [
'slug' => [
'title' => 'rainlab.blog::lang.settings.post_slug',
'description' => 'rainlab.blog::lang.settings.post_slug_description',
'default' => '{{ :slug }}',
'type' => 'string'
],
'categoryPage' => [
'title' => 'rainlab.blog::lang.settings.post_category',
'description' => 'rainlab.blog::lang.settings.post_category_description',
'type' => 'dropdown',
'default' => 'blog/category',
],
];
}
public function getCategoryPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
public function onRun()
{
$this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage');
$this->post = $this->page['post'] = $this->loadPost();
}
protected function loadPost()
{
$slug = $this->property('slug');
$post = new BlogPost;
$post = $post->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
? $post->transWhere('slug', $slug)
: $post->where('slug', $slug);
$post = $post->isPublished()->first();
/*
* Add a "url" helper attribute for linking to each category
*/
if ($post && $post->categories->count()) {
$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
}
return $post;
}
public function previousPost()
{
return $this->getPostSibling(-1);
}
public function nextPost()
{
return $this->getPostSibling(1);
}
protected function getPostSibling($direction = 1)
{
if (!$this->post) {
return;
}
$method = $direction === -1 ? 'previousPost' : 'nextPost';
if (!$post = $this->post->$method()) {
return;
}
$postPage = $this->getPage()->getBaseFileName();
$post->setUrl($postPage, $this->controller);
$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
return $post;
}
}