From b355286bd3c95fb2a7d039fa9bceeca0504c7d2a Mon Sep 17 00:00:00 2001 From: micmania1 Date: Sun, 16 Feb 2014 05:19:26 +0000 Subject: [PATCH] ENHANCEMENT: Updated Blog to allow for better subclassing. You can now set different show_in_sitetree config options on subclasses. Also done some cleanup --- .gitignore | 0 composer.json | 5 ++-- extensions/BlogFilter.php | 27 ++++++++++++++---- extensions/BlogPostFilter.php | 11 +++++++ extensions/URLSegmentExtension.php | 2 +- forms/gridfield/GridFieldAddByDBField.php | 12 +++++--- .../GridFieldBlogPostAddNewButton.php | 7 ++++- forms/gridfield/GridFieldBlogPostState.php | 12 +++++++- forms/gridfield/GridFieldConfig_BlogPost.php | 4 +-- .../gridfield/GridFieldSiteTreeEditButton.php | 4 +-- images/blog-icon-s1d712fffa2.png | Bin lang/_manifest_exclude | 0 model/Blog.php | 21 ++++++++------ model/BlogCategory.php | 2 +- model/BlogPost.php | 20 +++++++------ model/BlogTag.php | 3 +- 16 files changed, 91 insertions(+), 39 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 images/blog-icon-s1d712fffa2.png mode change 100644 => 100755 lang/_manifest_exclude diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json index 30322a8..541e82e 100755 --- a/composer.json +++ b/composer.json @@ -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 + } } diff --git a/extensions/BlogFilter.php b/extensions/BlogFilter.php index 00e4613..fbd95f8 100755 --- a/extensions/BlogFilter.php +++ b/extensions/BlogFilter.php @@ -1,12 +1,13 @@ + * @author Michael Strong * **/ 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()) . "'"); diff --git a/extensions/BlogPostFilter.php b/extensions/BlogPostFilter.php index 8ee25dc..33bbcbe 100755 --- a/extensions/BlogPostFilter.php +++ b/extensions/BlogPostFilter.php @@ -1,5 +1,14 @@ +**/ 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) { diff --git a/extensions/URLSegmentExtension.php b/extensions/URLSegmentExtension.php index aced3fa..83f148e 100755 --- a/extensions/URLSegmentExtension.php +++ b/extensions/URLSegmentExtension.php @@ -6,7 +6,7 @@ * @package silverstripe * @subpackage blog * - * @author Michael Strong + * @author Michael Strong **/ class URLSegmentExtension extends DataExtension { diff --git a/forms/gridfield/GridFieldAddByDBField.php b/forms/gridfield/GridFieldAddByDBField.php index 9bdd351..5fffa9e 100755 --- a/forms/gridfield/GridFieldAddByDBField.php +++ b/forms/gridfield/GridFieldAddByDBField.php @@ -6,7 +6,7 @@ * @package silverstripe * @subpackage blog * - * @author Michael Strong + * @author Michael Strong **/ 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' ); diff --git a/forms/gridfield/GridFieldBlogPostAddNewButton.php b/forms/gridfield/GridFieldBlogPostAddNewButton.php index 0a66efd..10067e7 100755 --- a/forms/gridfield/GridFieldBlogPostAddNewButton.php +++ b/forms/gridfield/GridFieldBlogPostAddNewButton.php @@ -1,4 +1,5 @@ */ class GridFieldBlogPostAddNewButton extends GridFieldAddNewButton implements GridField_ActionProvider { @@ -42,7 +45,9 @@ class GridFieldBlogPostAddNewButton extends GridFieldAddNewButton $allowedChildren = $parent->allowedChildren(); $children = array(); 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( diff --git a/forms/gridfield/GridFieldBlogPostState.php b/forms/gridfield/GridFieldBlogPostState.php index 8a52128..98f976a 100755 --- a/forms/gridfield/GridFieldBlogPostState.php +++ b/forms/gridfield/GridFieldBlogPostState.php @@ -1,5 +1,14 @@ +**/ 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", ' Saved as Draft'); + return _t("GridFieldBlogPostState.Draft", + ' Saved as Draft'); } else if (strtotime($record->PublishDate) > time()) { return _t( "GridFieldBlogPostState.Timer", diff --git a/forms/gridfield/GridFieldConfig_BlogPost.php b/forms/gridfield/GridFieldConfig_BlogPost.php index 0e1fd6a..c0ac542 100755 --- a/forms/gridfield/GridFieldConfig_BlogPost.php +++ b/forms/gridfield/GridFieldConfig_BlogPost.php @@ -1,12 +1,12 @@ + * @author Michael String **/ class GridFieldConfig_BlogPost extends GridFieldConfig { diff --git a/forms/gridfield/GridFieldSiteTreeEditButton.php b/forms/gridfield/GridFieldSiteTreeEditButton.php index 1b05f0d..5efbd59 100755 --- a/forms/gridfield/GridFieldSiteTreeEditButton.php +++ b/forms/gridfield/GridFieldSiteTreeEditButton.php @@ -6,7 +6,7 @@ * @package silverstripe * @subpackage blog * - * @author Michael String + * @author Michael String **/ 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( diff --git a/images/blog-icon-s1d712fffa2.png b/images/blog-icon-s1d712fffa2.png old mode 100644 new mode 100755 diff --git a/lang/_manifest_exclude b/lang/_manifest_exclude old mode 100644 new mode 100755 diff --git a/model/Blog.php b/model/Blog.php index dd2e2b4..8e173f7 100755 --- a/model/Blog.php +++ b/model/Blog.php @@ -6,7 +6,7 @@ * @package silverstripe * @subpackage blog * - * @author Michael String + * @author Michael String **/ 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")) { - foreach($tmpClasses as $class) { + $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 + * @author Michael String **/ class Blog_Controller extends Page_Controller { diff --git a/model/BlogCategory.php b/model/BlogCategory.php index dbb843f..d948be7 100755 --- a/model/BlogCategory.php +++ b/model/BlogCategory.php @@ -6,7 +6,7 @@ * @package silverstripe * @subpackage blog * - * @author Michael Strong + * @author Michael Strong **/ class BlogCategory extends DataObject { diff --git a/model/BlogPost.php b/model/BlogPost.php index 545f27f..21a0757 100755 --- a/model/BlogPost.php +++ b/model/BlogPost.php @@ -6,13 +6,10 @@ * @package silverstripe * @subpackage blog * - * @author Michael Strong + * @author Michael Strong **/ 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 + * @author Michael Strong **/ class BlogPost_Controller extends Page_Controller { diff --git a/model/BlogTag.php b/model/BlogTag.php index cd97706..7c24177 100755 --- a/model/BlogTag.php +++ b/model/BlogTag.php @@ -6,7 +6,7 @@ * @package silverstripe * @subpackage blog * - * @author Michael Strong + * @author Michael Strong **/ 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"))