ENHANCEMENT: Updated Blog to allow for better subclassing. You can now set different show_in_sitetree config options on subclasses. Also done some cleanup

This commit is contained in:
micmania1 2014-02-16 05:19:26 +00:00
parent f6e9d187ed
commit b355286bd3
16 changed files with 91 additions and 39 deletions

0
.gitignore vendored Normal file → Executable file
View File

View File

@ -10,12 +10,11 @@
"authors": [ "authors": [
{ {
"name": "Michael Strong", "name": "Michael Strong",
"email": "micmania@hotmail.co.uk" "email": "github@michaelstrong.co.uk"
} }
], ],
"suggest": { "suggest": {
"silverstripe/widgets": "Some widgets come with the blog which are compatible with the widgets module.", "silverstripe/widgets": "Some widgets come with the blog which are compatible with the widgets module.",
"silverstripe/comments": "This module adds comments to your blog." "silverstripe/comments": "This module adds comments to your blog."
}, }
"prefer-stable": true
} }

View File

@ -1,12 +1,13 @@
<?php <?php
/** /**
* Base extension of BlogPost. * This class is responsible for filtering the SiteTree when necessary and also
* overlaps into filtering only published posts.
* *
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <micmania@hotmail.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
* *
**/ **/
class BlogFilter extends Hierarchy { class BlogFilter extends Hierarchy {
@ -21,15 +22,22 @@ class BlogFilter extends Hierarchy {
$staged = parent::stageChildren($showAll); $staged = parent::stageChildren($showAll);
$controller = Controller::curr(); $controller = Controller::curr();
if($controller->class == "CMSPagesController" && in_array($controller->getAction(), array("treeview", "listview", "getsubtree"))) { if($controller->class == "CMSPagesController"
&& in_array($controller->getAction(), array("treeview", "listview", "getsubtree"))
) {
// Filter the SiteTree
return $staged->exclude("ClassName", $this->owner->getExcludedSiteTreeClassNames()); return $staged->exclude("ClassName", $this->owner->getExcludedSiteTreeClassNames());
} else if(in_array($this->owner->ClassName, ClassInfo::subClassesFor("Blog")) && !Permission::check("VIEW_DRAFT_CONTENT")) {
} else if(in_array($this->owner->ClassName, ClassInfo::subClassesFor("Blog"))
&& !Permission::check("VIEW_DRAFT_CONTENT")
) {
// Get the current stage. // Get the current stage.
$stage = Versioned::current_stage(); $stage = Versioned::current_stage();
if($stage == "Stage") $stage = ""; if($stage == "Stage") $stage = "";
else $stage = "_" . Convert::raw2sql($stage); else $stage = "_" . Convert::raw2sql($stage);
// Filter published posts
$dataQuery = $staged->dataQuery() $dataQuery = $staged->dataQuery()
->innerJoin("BlogPost", "BlogPost" . $stage . ".ID = SiteTree" . $stage . ".ID") ->innerJoin("BlogPost", "BlogPost" . $stage . ".ID = SiteTree" . $stage . ".ID")
->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'"); ->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");
@ -51,9 +59,16 @@ class BlogFilter extends Hierarchy {
$staged = parent::liveChildren($showAll, $onlyDeletedFromStage); $staged = parent::liveChildren($showAll, $onlyDeletedFromStage);
$controller = Controller::curr(); $controller = Controller::curr();
if($controller->class == "CMSPagesController" && in_array($controller->getAction(), array("treeview", "listview", "getsubtree"))) { if($controller->class == "CMSPagesController"
&& in_array($controller->getAction(), array("treeview", "listview", "getsubtree"))
) {
// Filter the SiteTree
return $staged->exclude("ClassName", $this->owner->getExcludedSiteTreeClassNames()); return $staged->exclude("ClassName", $this->owner->getExcludedSiteTreeClassNames());
} else if(in_array($this->owner->ClassName, ClassInfo::subClassesFor("Blog")) && !Permission::check("VIEW_DRAFT_CONTENT")) {
} else if(in_array($this->owner->ClassName, ClassInfo::subClassesFor("Blog"))
&& !Permission::check("VIEW_DRAFT_CONTENT")
) {
// Filter publish posts
$dataQuery = $staged->dataQuery() $dataQuery = $staged->dataQuery()
->innerJoin("BlogPost", "BlogPost_Live.ID = SiteTree_Live.ID") ->innerJoin("BlogPost", "BlogPost_Live.ID = SiteTree_Live.ID")
->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'"); ->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");

View File

@ -1,5 +1,14 @@
<?php <?php
/**
* This is responsible for filtering only published posts to users who do not have
* permission to view non-published posts.
*
* @package silverstripe
* @subpackage blog
*
* @author Michael Strong <github@michaelstrong.co.uk>
**/
class BlogPostFilter extends DataExtension { class BlogPostFilter extends DataExtension {
/** /**
@ -21,6 +30,8 @@ class BlogPostFilter extends DataExtension {
* This is a fix so that when we try to fetch subclasses of BlogPost, * This is a fix so that when we try to fetch subclasses of BlogPost,
* lazy loading includes the BlogPost table in its query. Leaving this table out * lazy loading includes the BlogPost table in its query. Leaving this table out
* means the default sort order column PublishDate causes an error. * means the default sort order column PublishDate causes an error.
*
* @see https://github.com/silverstripe/silverstripe-framework/issues/1682
**/ **/
public function augmentLoadLazyFields(SQLQuery &$query, &$dataQuery, $parent) { public function augmentLoadLazyFields(SQLQuery &$query, &$dataQuery, $parent) {

View File

@ -6,7 +6,7 @@
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <micmania@hotmail.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class URLSegmentExtension extends DataExtension { class URLSegmentExtension extends DataExtension {

View File

@ -6,7 +6,7 @@
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <micmania@hotmail.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class GridFieldAddByDBField implements GridField_ActionProvider, GridField_HTMLProvider { class GridFieldAddByDBField implements GridField_ActionProvider, GridField_HTMLProvider {
@ -103,13 +103,17 @@ class GridFieldAddByDBField implements GridField_ActionProvider, GridField_HTMLP
$obj = singleton($dataClass); $obj = singleton($dataClass);
$dbField = $this->getDataObjectField(); $dbField = $this->getDataObjectField();
$textField = TextField::create("gridfieldaddbydbfield[" . $obj->ClassName . "][" . Convert::raw2htmlatt($dbField) . "]") $textField = TextField::create(
->setAttribute("placeholder", $obj->fieldLabel($dbField)) "gridfieldaddbydbfield[" . $obj->ClassName . "][" . Convert::raw2htmlatt($dbField) . "]"
)->setAttribute("placeholder", $obj->fieldLabel($dbField))
->addExtraClass("no-change-track"); ->addExtraClass("no-change-track");
$addAction = new GridField_FormAction($gridField, $addAction = new GridField_FormAction($gridField,
'add', 'add',
_t('GridFieldAddByDBField.Add', "Add {name}", "Add button text", array("name" => $obj->i18n_singular_name())), _t('GridFieldAddByDBField.Add',
"Add {name}", "Add button text",
array("name" => $obj->i18n_singular_name())
),
'add', 'add',
'add' 'add'
); );

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* This component provides a button for opening the add new form provided by * This component provides a button for opening the add new form provided by
* {@link GridFieldDetailForm}. * {@link GridFieldDetailForm}.
@ -8,6 +9,8 @@
* *
* @package framework * @package framework
* @subpackage fields-gridfield * @subpackage fields-gridfield
*
* @author Michael Strong <github@michaelstrong.co.uk>
*/ */
class GridFieldBlogPostAddNewButton extends GridFieldAddNewButton class GridFieldBlogPostAddNewButton extends GridFieldAddNewButton
implements GridField_ActionProvider { implements GridField_ActionProvider {
@ -42,7 +45,9 @@ class GridFieldBlogPostAddNewButton extends GridFieldAddNewButton
$allowedChildren = $parent->allowedChildren(); $allowedChildren = $parent->allowedChildren();
$children = array(); $children = array();
foreach($allowedChildren as $class) { foreach($allowedChildren as $class) {
$children[$class] = singleton($class)->i18n_singular_name(); if(!Config::inst()->get($class, "show_in_sitetree")) {
$children[$class] = singleton($class)->i18n_singular_name();
}
} }
$pageTypes = DropdownField::create( $pageTypes = DropdownField::create(

View File

@ -1,5 +1,14 @@
<?php <?php
/**
* Provides a component to the {@link GridField} which tells the user whether or not a
* blog post has been published and when.
*
* @package silverstripe
* @subpackage blog
*
* @author Michael Strong <github@michaelstrong.co.uk>
**/
class GridFieldBlogPostState implements GridField_ColumnProvider { class GridFieldBlogPostState implements GridField_ColumnProvider {
public function augmentColumns($gridField, &$columns) { public function augmentColumns($gridField, &$columns) {
@ -22,7 +31,8 @@ class GridFieldBlogPostState implements GridField_ColumnProvider {
if($record->hasMethod("isPublished")) { if($record->hasMethod("isPublished")) {
$published = $record->isPublished(); $published = $record->isPublished();
if(!$published) { if(!$published) {
return _t("GridFieldBlogPostState.Draft", '<i class="btn-icon blog-icon btn-icon-pencil"></i> Saved as Draft'); return _t("GridFieldBlogPostState.Draft",
'<i class="btn-icon blog-icon btn-icon-pencil"></i> Saved as Draft');
} else if (strtotime($record->PublishDate) > time()) { } else if (strtotime($record->PublishDate) > time()) {
return _t( return _t(
"GridFieldBlogPostState.Timer", "GridFieldBlogPostState.Timer",

View File

@ -1,12 +1,12 @@
<?php <?php
/** /**
* GirdField config necessary for managing a SiteTree object. * GridField config necessary for managing a SiteTree object.
* *
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael String <micmania@hotmail.co.uk> * @author Michael String <github@michaelstrong.co.uk>
**/ **/
class GridFieldConfig_BlogPost extends GridFieldConfig { class GridFieldConfig_BlogPost extends GridFieldConfig {

View File

@ -6,7 +6,7 @@
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael String <micmania@hotmail.co.uk> * @author Michael String <github@michaelstrong.co.uk>
**/ **/
class GridFieldSiteTreeEditButton extends GridFieldEditButton { class GridFieldSiteTreeEditButton extends GridFieldEditButton {
@ -18,7 +18,7 @@ class GridFieldSiteTreeEditButton extends GridFieldEditButton {
* @return string - the HTML for the column * @return string - the HTML for the column
*/ */
public function getColumnContent($gridField, $record, $columnName) { public function getColumnContent($gridField, $record, $columnName) {
// No permission checks, handled through GridFieldDetailForm, // No permission checks - handled through GridFieldDetailForm
// which can make the form readonly if no edit permissions are available. // which can make the form readonly if no edit permissions are available.
$data = new ArrayData(array( $data = new ArrayData(array(

0
images/blog-icon-s1d712fffa2.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 800 B

After

Width:  |  Height:  |  Size: 800 B

0
lang/_manifest_exclude Normal file → Executable file
View File

View File

@ -6,7 +6,7 @@
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael String <micmania@hotmail.co.uk> * @author Michael String <github@michaelstrong.co.uk>
**/ **/
class Blog extends Page { class Blog extends Page {
@ -34,11 +34,15 @@ class Blog extends Page {
public function getCMSFields() { public function getCMSFields() {
$fields = parent::getCMSFields(); $fields = parent::getCMSFields();
if(!Config::inst()->get("BlogPost", "show_in_sitetree")) {
$posts = $this->getBlogPosts();
$excluded = $this->getExcludedSiteTreeClassNames();
if(!empty($excluded)) {
$posts = $posts->filter("ClassName", $excluded);
$gridField = new GridField( $gridField = new GridField(
"BlogPost", "BlogPost",
_t("Blog.BlogPosts", "Blog Posts"), _t("Blog.BlogPosts", "Blog Posts"),
$this->getBlogPosts(), $posts,
GridFieldConfig_BlogPost::create() GridFieldConfig_BlogPost::create()
); );
$fields->addFieldToTab("Root.BlogPosts", $gridField); $fields->addFieldToTab("Root.BlogPosts", $gridField);
@ -75,7 +79,8 @@ class Blog extends Page {
public function getSettingsFields() { public function getSettingsFields() {
$fields = parent::getSettingsFields(); $fields = parent::getSettingsFields();
$fields->addFieldToTab("Root.Settings", $fields->addFieldToTab("Root.Settings",
NumericField::create("PostsPerPage", _t("Blog.PostsPerPage", "Posts Per Page"))); NumericField::create("PostsPerPage", _t("Blog.PostsPerPage", "Posts Per Page"))
);
return $fields; return $fields;
} }
@ -89,9 +94,9 @@ class Blog extends Page {
**/ **/
public function getExcludedSiteTreeClassNames() { public function getExcludedSiteTreeClassNames() {
$classes = array(); $classes = array();
$tmpClasses = ClassInfo::subClassesFor("BlogPost"); $tmpClasses = $this->allowedChildren();
if(!Config::inst()->get("BlogPost", "show_in_sitetree")) { foreach($tmpClasses as $class) {
foreach($tmpClasses as $class) { if(!Config::inst()->get($class, "show_in_sitetree")) {
$classes[$class] = $class; $classes[$class] = $class;
} }
} }
@ -151,7 +156,7 @@ class Blog extends Page {
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael String <micmania@hotmail.co.uk> * @author Michael String <github@michaelstrong.co.uk>
**/ **/
class Blog_Controller extends Page_Controller { class Blog_Controller extends Page_Controller {

View File

@ -6,7 +6,7 @@
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <micmania@hotmail.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class BlogCategory extends DataObject { class BlogCategory extends DataObject {

View File

@ -6,13 +6,10 @@
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <micmania@hotmail.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class BlogPost extends Page { class BlogPost extends Page {
/**
* @var array
**/
private static $db = array( private static $db = array(
"PublishDate" => "SS_Datetime", "PublishDate" => "SS_Datetime",
); );
@ -32,8 +29,6 @@ class BlogPost extends Page {
"ProvideComments" => true, // Support for comments "ProvideComments" => true, // Support for comments
); );
private static $allowed_children = array();
private static $extensions = array( private static $extensions = array(
"BlogPostFilter", "BlogPostFilter",
); );
@ -46,9 +41,11 @@ class BlogPost extends Page {
"Title", "Title",
); );
private static $allowed_children = array();
private static $default_sort = "PublishDate DESC"; private static $default_sort = "PublishDate DESC";
public static $can_be_root = false; private static $can_be_root = false;
/** /**
* This will display or hide the current class from the SiteTree. This * This will display or hide the current class from the SiteTree. This
@ -155,7 +152,12 @@ class BlogPost extends Page {
$year = $date->format("Y"); $year = $date->format("Y");
if($type != "year") { if($type != "year") {
if($type == "day") { if($type == "day") {
return Controller::join_links($this->Parent()->Link("archive"), $date->format("Y"), $date->format("m"), $date->format("d")); return Controller::join_links(
$this->Parent()->Link("archive"),
$date->format("Y"),
$date->format("m"),
$date->format("d")
);
} }
return Controller::join_links($this->Parent()->Link("archive"), $date->format("Y"), $date->format("m")); return Controller::join_links($this->Parent()->Link("archive"), $date->format("Y"), $date->format("m"));
} }
@ -183,7 +185,7 @@ class BlogPost extends Page {
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <micmania@hotmail.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class BlogPost_Controller extends Page_Controller { class BlogPost_Controller extends Page_Controller {

View File

@ -6,7 +6,7 @@
* @package silverstripe * @package silverstripe
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <micmania@hotmail.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class BlogTag extends DataObject { class BlogTag extends DataObject {
@ -26,6 +26,7 @@ class BlogTag extends DataObject {
"URLSegmentExtension", "URLSegmentExtension",
); );
public function getCMSFields() { public function getCMSFields() {
$fields = new FieldList( $fields = new FieldList(
TextField::create("Title", _t("BlogTag.Title", "Title")) TextField::create("Title", _t("BlogTag.Title", "Title"))