2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Widgets;
|
|
|
|
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Blog\Model\Blog;
|
2019-04-10 13:22:15 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
2018-01-29 04:13:19 +01:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Forms\NumericField;
|
2018-04-06 00:22:58 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Widgets\Model\Widget;
|
|
|
|
|
2018-04-06 00:22:58 +02:00
|
|
|
if (!class_exists(Widget::class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @method Blog Blog()
|
|
|
|
*
|
|
|
|
* @property int $NumberOfPosts
|
|
|
|
*/
|
|
|
|
class BlogRecentPostsWidget extends Widget
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $title = 'Recent Posts';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cmsTitle = 'Recent Posts';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $description = 'Displays a list of recent blog posts.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $db = [
|
2015-11-21 07:17:29 +01:00
|
|
|
'NumberOfPosts' => 'Int',
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $has_one = [
|
2017-01-18 05:12:06 +01:00
|
|
|
'Blog' => Blog::class,
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2017-12-15 05:24:20 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $table_name = 'BlogRecentPostsWidget';
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
|
|
|
/**
|
|
|
|
* @var FieldList $fields
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
$fields->merge([
|
2017-09-14 00:27:40 +02:00
|
|
|
DropdownField::create('BlogID', _t(__CLASS__ . '.Blog', 'Blog'), Blog::get()->map()),
|
|
|
|
NumericField::create('NumberOfPosts', _t(__CLASS__ . '.NumberOfPosts', 'Number of Posts'))
|
2017-09-14 00:59:01 +02:00
|
|
|
]);
|
2015-11-21 07:17:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-06 00:22:58 +02:00
|
|
|
* @return array|DataList
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
|
|
|
public function getPosts()
|
|
|
|
{
|
|
|
|
$blog = $this->Blog();
|
|
|
|
|
|
|
|
if ($blog) {
|
|
|
|
return $blog->getBlogPosts()
|
2019-04-10 13:22:15 +02:00
|
|
|
->filter('ID:not', Director::get_current_page()->ID)
|
2015-11-21 07:17:29 +01:00
|
|
|
->sort('"PublishDate" DESC')
|
|
|
|
->limit($this->NumberOfPosts);
|
|
|
|
}
|
|
|
|
|
2017-09-14 00:59:01 +02:00
|
|
|
return [];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
}
|