silverstripe-blog/widgets/BlogRecentPostsWidget.php

47 lines
1.1 KiB
PHP
Raw Normal View History

2013-08-04 18:38:26 +02:00
<?php
2013-10-10 00:09:28 +02:00
if(class_exists("Widget")) {
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
class BlogRecentPostsWidget extends Widget {
private static $title = "Recent Posts";
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $cmsTitle = "Recent Posts";
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $description = "Displays a list of recent blog posts.";
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $db = array(
"NumberOfPosts" => "Int",
);
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $has_one = array(
"Blog" => "Blog",
);
public function getCMSFields() {
$fields = FieldList::create();
$fields->merge(array(
DropdownField::create("BlogID", _t("BlogRecentPostsWidget.Blog", "Blog"), Blog::get()->map()),
NumericField::create("NumberOfPosts", _t("BlogRecentPostsWidget.NumberOfPosts", "Number of Posts"))
));
$this->extend("updateCMSFields", $fields);
2013-10-10 00:09:28 +02:00
return $fields;
}
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
public function getPosts() {
$blog = $this->Blog();
if($blog) {
return $blog->getBlogPosts()
->sort("PublishDate DESC")
->limit($this->NumberOfPosts);
}
return array();
2013-08-04 18:38:26 +02:00
}
2013-10-10 00:09:28 +02:00
2013-08-04 18:38:26 +02:00
}
2013-10-10 00:09:28 +02:00
class BlogRecentPostsWidget_Controller extends Widget_Controller {
}
2013-08-04 18:38:26 +02:00
}