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": [
{
"name": "Michael Strong",
"email": "micmania@hotmail.co.uk"
"email": "github@michaelstrong.co.uk"
}
],
"suggest": {
"silverstripe/widgets": "Some widgets come with the blog which are compatible with the widgets module.",
"silverstripe/comments": "This module adds comments to your blog."
},
"prefer-stable": true
}
}

View File

@ -1,12 +1,13 @@
<?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
* @subpackage blog
*
* @author Michael Strong <micmania@hotmail.co.uk>
* @author Michael Strong <github@michaelstrong.co.uk>
*
**/
class BlogFilter extends Hierarchy {
@ -21,15 +22,22 @@ class BlogFilter extends Hierarchy {
$staged = parent::stageChildren($showAll);
$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());
} 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.
$stage = Versioned::current_stage();
if($stage == "Stage") $stage = "";
else $stage = "_" . Convert::raw2sql($stage);
// Filter published posts
$dataQuery = $staged->dataQuery()
->innerJoin("BlogPost", "BlogPost" . $stage . ".ID = SiteTree" . $stage . ".ID")
->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");
@ -51,9 +59,16 @@ class BlogFilter extends Hierarchy {
$staged = parent::liveChildren($showAll, $onlyDeletedFromStage);
$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());
} 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()
->innerJoin("BlogPost", "BlogPost_Live.ID = SiteTree_Live.ID")
->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");

View File

@ -1,5 +1,14 @@
<?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 {
/**
@ -21,6 +30,8 @@ class BlogPostFilter extends DataExtension {
* 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
* 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) {

View File

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

View File

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

View File

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

View File

@ -1,5 +1,14 @@
<?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 {
public function augmentColumns($gridField, &$columns) {
@ -22,7 +31,8 @@ class GridFieldBlogPostState implements GridField_ColumnProvider {
if($record->hasMethod("isPublished")) {
$published = $record->isPublished();
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()) {
return _t(
"GridFieldBlogPostState.Timer",

View File

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

View File

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

View File

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

View File

@ -6,13 +6,10 @@
* @package silverstripe
* @subpackage blog
*
* @author Michael Strong <micmania@hotmail.co.uk>
* @author Michael Strong <github@michaelstrong.co.uk>
**/
class BlogPost extends Page {
/**
* @var array
**/
private static $db = array(
"PublishDate" => "SS_Datetime",
);
@ -32,8 +29,6 @@ class BlogPost extends Page {
"ProvideComments" => true, // Support for comments
);
private static $allowed_children = array();
private static $extensions = array(
"BlogPostFilter",
);
@ -46,9 +41,11 @@ class BlogPost extends Page {
"Title",
);
private static $allowed_children = array();
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
@ -155,7 +152,12 @@ class BlogPost extends Page {
$year = $date->format("Y");
if($type != "year") {
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"));
}
@ -183,7 +185,7 @@ class BlogPost extends Page {
* @package silverstripe
* @subpackage blog
*
* @author Michael Strong <micmania@hotmail.co.uk>
* @author Michael Strong <github@michaelstrong.co.uk>
**/
class BlogPost_Controller extends Page_Controller {

View File

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