2013-07-21 12:23:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blog Holder
|
|
|
|
*
|
|
|
|
* @package silverstripe
|
|
|
|
* @subpackage blog
|
|
|
|
*
|
2014-02-20 17:37:55 +01:00
|
|
|
* @author Michael Strong <github@michaelstrong.co.uk>
|
2013-07-21 12:23:35 +02:00
|
|
|
**/
|
|
|
|
class Blog extends Page {
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
private static $db = array(
|
|
|
|
"PostsPerPage" => "Int",
|
|
|
|
);
|
|
|
|
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
private static $has_many = array(
|
|
|
|
"Tags" => "BlogTag",
|
2013-08-04 18:38:26 +02:00
|
|
|
"Categories" => "BlogCategory",
|
2013-07-21 12:23:35 +02:00
|
|
|
);
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
|
|
|
|
private static $allowed_children = array(
|
2013-08-04 18:38:26 +02:00
|
|
|
"BlogPost",
|
2013-07-21 12:23:35 +02:00
|
|
|
);
|
|
|
|
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
private static $extensions = array(
|
|
|
|
"BlogFilter",
|
|
|
|
);
|
2013-07-21 12:23:35 +02:00
|
|
|
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
private static $defaults = array(
|
|
|
|
"ProvideComments" => false,
|
|
|
|
);
|
2013-07-21 12:23:35 +02:00
|
|
|
|
2014-12-11 11:42:36 +01:00
|
|
|
private static $description = 'Adds a blog to your website.';
|
2013-07-21 12:23:35 +02:00
|
|
|
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
public function getCMSFields() {
|
2014-07-27 07:34:43 +02:00
|
|
|
$self =& $this;
|
|
|
|
$this->beforeUpdateCMSFields(function($fields) use ($self) {
|
2013-07-21 12:23:35 +02:00
|
|
|
|
2014-06-13 17:19:15 +02:00
|
|
|
// Create categories and tag config
|
|
|
|
$config = GridFieldConfig_RecordEditor::create();
|
|
|
|
$config->removeComponentsByType("GridFieldAddNewButton");
|
|
|
|
$config->addComponent(new GridFieldAddByDBField("buttons-before-left"));
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2014-06-13 17:19:15 +02:00
|
|
|
$categories = GridField::create(
|
|
|
|
"Categories",
|
|
|
|
_t("Blog.Categories", "Categories"),
|
2014-07-27 07:34:43 +02:00
|
|
|
$self->Categories(),
|
2014-06-13 17:19:15 +02:00
|
|
|
$config
|
|
|
|
);
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2014-06-13 17:19:15 +02:00
|
|
|
$tags = GridField::create(
|
|
|
|
"Tags",
|
|
|
|
_t("Blog.Tags", "Tags"),
|
2014-07-27 07:34:43 +02:00
|
|
|
$self->Tags(),
|
2014-06-13 17:19:15 +02:00
|
|
|
$config
|
|
|
|
);
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2014-06-13 17:19:15 +02:00
|
|
|
$fields->addFieldsToTab("Root.BlogOptions", array(
|
|
|
|
$categories,
|
|
|
|
$tags
|
|
|
|
));
|
2013-07-21 12:23:35 +02:00
|
|
|
|
2014-06-13 17:19:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$fields = parent::getCMSFields();
|
2013-08-04 18:38:26 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-27 10:40:08 +02:00
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
public function getSettingsFields() {
|
|
|
|
$fields = parent::getSettingsFields();
|
2013-10-10 00:09:28 +02:00
|
|
|
$fields->addFieldToTab("Root.Settings",
|
2014-02-16 06:19:26 +01:00
|
|
|
NumericField::create("PostsPerPage", _t("Blog.PostsPerPage", "Posts Per Page"))
|
|
|
|
);
|
2013-07-21 12:23:35 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-11 11:42:36 +01:00
|
|
|
* Return blog posts
|
2013-07-21 12:23:35 +02:00
|
|
|
*
|
|
|
|
* @return DataList of BlogPost objects
|
|
|
|
**/
|
|
|
|
public function getBlogPosts() {
|
2013-10-09 16:20:46 +02:00
|
|
|
$blogPosts = BlogPost::get()->filter("ParentID", $this->ID);
|
|
|
|
//Allow decorators to manipulate list
|
|
|
|
$this->extend('updateGetBlogPosts', $blogPosts);
|
|
|
|
return $blogPosts;
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 00:09:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns blogs posts for a given date period.
|
|
|
|
*
|
|
|
|
* @param $year int
|
|
|
|
* @param $month int
|
|
|
|
* @param $dat int
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
**/
|
|
|
|
public function getArchivedBlogPosts($year, $month = null, $day = null) {
|
|
|
|
$query = $this->getBlogPosts()->dataQuery();
|
|
|
|
|
|
|
|
$stage = $query->getQueryParam("Versioned.stage");
|
|
|
|
if($stage) $stage = '_' . Convert::raw2sql($stage);
|
|
|
|
|
|
|
|
$query->innerJoin("BlogPost", "`SiteTree" . $stage . "`.`ID` = `BlogPost" . $stage . "`.`ID`");
|
|
|
|
$query->where("YEAR(PublishDate) = '" . Convert::raw2sql($year) . "'");
|
|
|
|
if($month) {
|
|
|
|
$query->where("MONTH(PublishDate) = '" . Convert::raw2sql($month) . "'");
|
|
|
|
if($day) {
|
|
|
|
$query->where("DAY(PublishDate) = '" . Convert::raw2sql($day) . "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getBlogPosts()->setDataQuery($query);
|
|
|
|
}
|
|
|
|
|
2014-12-11 11:42:36 +01:00
|
|
|
|
2015-02-05 05:30:27 +01:00
|
|
|
|
2014-12-11 11:42:36 +01:00
|
|
|
/**
|
|
|
|
* This sets the title for our gridfield
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLumberjackTitle() {
|
|
|
|
return _t('Blog.LumberjackTitle', 'Blog Posts');
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:30:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This overwrites lumberjacks default gridfield config.
|
|
|
|
*
|
|
|
|
* @return GridFieldConfig
|
|
|
|
*/
|
|
|
|
public function getLumberjackGridFieldConfig() {
|
2015-02-07 00:52:45 +01:00
|
|
|
return GridFieldConfig_BlogPost::create();
|
2015-02-05 05:30:27 +01:00
|
|
|
}
|
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blog Controller
|
|
|
|
*
|
|
|
|
* @package silverstripe
|
|
|
|
* @subpackage blog
|
|
|
|
*
|
2014-03-24 01:19:27 +01:00
|
|
|
* @author Michael Strong <github@michaelstrong.co.uk>
|
2013-07-21 12:23:35 +02:00
|
|
|
**/
|
|
|
|
class Blog_Controller extends Page_Controller {
|
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'archive',
|
|
|
|
'tag',
|
|
|
|
'category',
|
2013-08-11 00:34:46 +02:00
|
|
|
'rss',
|
2013-07-21 12:23:35 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $url_handlers = array(
|
2013-08-04 18:38:26 +02:00
|
|
|
'tag/$Tag!' => 'tag',
|
|
|
|
'category/$Category!' => 'category',
|
2013-08-11 00:34:46 +02:00
|
|
|
'archive/$Year!/$Month/$Day' => 'archive',
|
2013-07-21 12:23:35 +02:00
|
|
|
);
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current Blog Post DataList query.
|
|
|
|
*
|
|
|
|
* @var DataList
|
|
|
|
**/
|
2013-07-21 12:23:35 +02:00
|
|
|
protected $blogPosts;
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
public function index() {
|
2013-08-04 18:38:26 +02:00
|
|
|
$this->blogPosts = $this->getBlogPosts();
|
2013-07-21 12:23:35 +02:00
|
|
|
return $this->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an archive for a specificed date. This can be by year or year/month
|
|
|
|
*
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
**/
|
2013-07-21 12:23:35 +02:00
|
|
|
public function archive() {
|
2013-08-04 18:38:26 +02:00
|
|
|
$year = $this->getArchiveYear();
|
|
|
|
$month = $this->getArchiveMonth();
|
2013-08-11 00:34:46 +02:00
|
|
|
$day = $this->getArchiveDay();
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
// If an invalid month has been passed, we can return a 404.
|
|
|
|
if($this->request->param("Month") && !$month) {
|
|
|
|
return $this->httpError(404, "Not Found");
|
2014-04-10 14:44:17 +02:00
|
|
|
}
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2014-04-10 14:44:17 +02:00
|
|
|
// Check for valid day
|
|
|
|
if($month && $this->request->param("Day") && !$day) {
|
|
|
|
return $this->httpError(404, "Not Found");
|
2013-08-11 00:34:46 +02:00
|
|
|
}
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
if($year) {
|
2013-10-10 00:09:28 +02:00
|
|
|
$this->blogPosts = $this->getArchivedBlogPosts($year, $month, $day);
|
2013-08-04 18:38:26 +02:00
|
|
|
return $this->render();
|
|
|
|
}
|
|
|
|
return $this->httpError(404, "Not Found");
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the blog posts for a given tag.
|
|
|
|
*
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
**/
|
2013-07-21 12:23:35 +02:00
|
|
|
public function tag() {
|
2013-08-04 18:38:26 +02:00
|
|
|
$tag = $this->getCurrentTag();
|
2013-07-21 12:23:35 +02:00
|
|
|
if($tag) {
|
2013-08-04 18:38:26 +02:00
|
|
|
$this->blogPosts = $tag->BlogPosts();
|
|
|
|
return $this->render();
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
|
|
|
return $this->httpError(404, "Not Found");
|
|
|
|
}
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the blog posts for a given category
|
|
|
|
*
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
**/
|
2013-07-21 12:23:35 +02:00
|
|
|
public function category() {
|
2013-08-04 18:38:26 +02:00
|
|
|
$category = $this->getCurrentCategory();
|
2013-07-21 12:23:35 +02:00
|
|
|
if($category) {
|
2013-08-04 18:38:26 +02:00
|
|
|
$this->blogPosts = $category->BlogPosts();
|
|
|
|
return $this->render();
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
|
|
|
return $this->httpError(404, "Not Found");
|
|
|
|
}
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
/**
|
|
|
|
* Displays an RSS feed of blog posts
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
**/
|
|
|
|
public function rss() {
|
2014-09-29 10:47:23 +02:00
|
|
|
$rss = new RSSFeed($this->getBlogPosts(), $this->Link(), $this->MetaTitle, $this->MetaDescription);
|
2014-10-02 11:19:13 +02:00
|
|
|
$this->extend('updateRss', $rss);
|
2014-07-27 07:34:43 +02:00
|
|
|
return $rss->outputToBrowser();
|
2013-08-11 00:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
/**
|
|
|
|
* Returns a list of paginated blog posts based on the blogPost dataList
|
|
|
|
*
|
|
|
|
* @return PaginatedList
|
|
|
|
**/
|
|
|
|
public function PaginatedList() {
|
|
|
|
$posts = new PaginatedList($this->blogPosts);
|
|
|
|
|
|
|
|
// If pagination is set to '0' then no pagination will be shown.
|
|
|
|
if($this->PostsPerPage > 0) $posts->setPageLength($this->PostsPerPage);
|
|
|
|
else $posts->setPageLength($this->getBlogPosts()->count());
|
|
|
|
|
|
|
|
$start = $this->request->getVar($posts->getPaginationGetVar());
|
|
|
|
$posts->setPageStart($start);
|
|
|
|
|
|
|
|
return $posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tag Getter for use in templates.
|
|
|
|
*
|
|
|
|
* @return BlogTag|null
|
|
|
|
**/
|
|
|
|
public function getCurrentTag() {
|
|
|
|
$tag = $this->request->param("Tag");
|
|
|
|
if($tag) {
|
|
|
|
return $this->dataRecord->Tags()
|
|
|
|
->filter("URLSegment", $tag)
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Category Getter for use in templates.
|
|
|
|
*
|
|
|
|
* @return BlogCategory|null
|
|
|
|
**/
|
|
|
|
public function getCurrentCategory() {
|
|
|
|
$category = $this->request->param("Category");
|
|
|
|
if($category) {
|
|
|
|
return $this->dataRecord->Categories()
|
|
|
|
->filter("URLSegment", $category)
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the archive year from the url
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
**/
|
|
|
|
public function getArchiveYear() {
|
|
|
|
$year = $this->request->param("Year");
|
|
|
|
if(preg_match("/^[0-9]{4}$/", $year)) {
|
2013-08-11 00:34:46 +02:00
|
|
|
return (int) $year;
|
2013-08-04 18:38:26 +02:00
|
|
|
}
|
|
|
|
return null;
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the archive money from the url.
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
**/
|
|
|
|
public function getArchiveMonth() {
|
|
|
|
$month = $this->request->param("Month");
|
|
|
|
if(preg_match("/^[0-9]{1,2}$/", $month)) {
|
2013-08-11 00:34:46 +02:00
|
|
|
if($month > 0 && $month < 13) {
|
|
|
|
// Check that we have a valid date.
|
|
|
|
if(checkdate($month, 01, $this->getArchiveYear())) {
|
|
|
|
return (int) $month;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the archive day from the url
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
**/
|
|
|
|
public function getArchiveDay() {
|
|
|
|
$day = $this->request->param("Day");
|
|
|
|
if(preg_match("/^[0-9]{1,2}$/", $day)) {
|
|
|
|
|
|
|
|
// Check that we have a valid date
|
|
|
|
if(checkdate($this->getArchiveMonth(), $day, $this->getArchiveYear())) {
|
|
|
|
return (int) $day;
|
|
|
|
}
|
2013-08-04 18:38:26 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
/**
|
|
|
|
* Returns the current archive date.
|
|
|
|
*
|
|
|
|
* @return Date
|
|
|
|
**/
|
2013-08-04 18:38:26 +02:00
|
|
|
public function getArchiveDate() {
|
|
|
|
$year = $this->getArchiveYear();
|
|
|
|
$month = $this->getArchiveMonth();
|
2013-08-11 00:34:46 +02:00
|
|
|
$day = $this->getArchiveDay();
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
if($year) {
|
|
|
|
if($month) {
|
2013-08-11 00:34:46 +02:00
|
|
|
$date = $year . '-' . $month . '-01';
|
|
|
|
if($day) {
|
|
|
|
$date = $year . '-' . $month . '-' . $day;
|
|
|
|
}
|
2013-08-04 18:38:26 +02:00
|
|
|
} else {
|
2013-08-11 00:34:46 +02:00
|
|
|
$date = $year . '-01-01';
|
2013-08-04 18:38:26 +02:00
|
|
|
}
|
2013-08-11 00:34:46 +02:00
|
|
|
return DBField::create_field("Date", $date);
|
2013-08-04 18:38:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a link to the RSS feed.
|
|
|
|
*
|
|
|
|
* @return string URL
|
|
|
|
**/
|
|
|
|
public function getRSSLink() {
|
|
|
|
return $this->Link("rss");
|
|
|
|
}
|
|
|
|
|
2014-09-29 10:47:23 +02:00
|
|
|
}
|