silverstripe-blog/code/widgets/BlogRecentPostsWidget.php

78 lines
1.3 KiB
PHP
Raw Normal View History

2013-08-04 18:38:26 +02:00
<?php
2015-05-09 16:33:12 +02:00
if(!class_exists("Widget")) {
return;
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @method Blog Blog()
*
* @property int $NumberOfPosts
*/
class BlogRecentPostsWidget extends Widget {
/**
* @var string
*/
private static $title = 'Recent Posts';
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var string
*/
private static $cmsTitle = 'Recent Posts';
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var string
*/
private static $description = 'Displays a list of recent blog posts.';
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
private static $db = array(
'NumberOfPosts' => 'Int',
);
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
private static $has_one = array(
'Blog' => 'Blog',
);
2013-10-10 00:09:28 +02:00
2015-05-09 16:33:12 +02:00
/**
* {@inheritdoc}
*/
public function getCMSFields() {
$this->beforeUpdateCMSFields(function ($fields) {
2015-05-13 23:12:48 +02:00
/**
* @var FieldList $fields
*/
2015-05-09 16:33:12 +02:00
$fields->merge(array(
DropdownField::create('BlogID', _t('BlogRecentPostsWidget.Blog', 'Blog'), Blog::get()->map()),
NumericField::create('NumberOfPosts', _t('BlogRecentPostsWidget.NumberOfPosts', 'Number of Posts'))
));
});
return parent::getCMSFields();
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @return array
*/
public function getPosts() {
$blog = $this->Blog();
if($blog) {
return $blog->getBlogPosts()
2015-05-14 01:11:50 +02:00
->sort('"PublishDate" DESC')
2015-05-09 16:33:12 +02:00
->limit($this->NumberOfPosts);
2013-08-04 18:38:26 +02:00
}
2013-10-10 00:09:28 +02:00
2015-05-09 16:33:12 +02:00
return array();
2013-08-04 18:38:26 +02:00
}
2015-05-09 16:33:12 +02:00
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
class BlogRecentPostsWidget_Controller extends Widget_Controller {
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
}