diff --git a/extensions/BlogFilter.php b/extensions/BlogFilter.php index 507f7c3..00e4613 100755 --- a/extensions/BlogFilter.php +++ b/extensions/BlogFilter.php @@ -32,7 +32,7 @@ class BlogFilter extends Hierarchy { $dataQuery = $staged->dataQuery() ->innerJoin("BlogPost", "BlogPost" . $stage . ".ID = SiteTree" . $stage . ".ID") - ->where("PublishDate < NOW()"); + ->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'"); $staged = $staged->setDataQuery($dataQuery); } @@ -56,7 +56,7 @@ class BlogFilter extends Hierarchy { } else if(in_array($this->owner->ClassName, ClassInfo::subClassesFor("Blog")) && !Permission::check("VIEW_DRAFT_CONTENT")) { $dataQuery = $staged->dataQuery() ->innerJoin("BlogPost", "BlogPost_Live.ID = SiteTree_Live.ID") - ->where("PublishDate < NOW()"); + ->where("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'"); $staged = $staged->setDataQuery($dataQuery); } return $staged; diff --git a/extensions/BlogPostFilter.php b/extensions/BlogPostFilter.php index a548980..8ee25dc 100755 --- a/extensions/BlogPostFilter.php +++ b/extensions/BlogPostFilter.php @@ -12,8 +12,8 @@ class BlogPostFilter extends DataExtension { if($stage == "Stage") $stage = ""; else $stage = "_" . Convert::raw2sql($stage); - $query->addWhere("PublishDate < NOW()"); - } + $query->addWhere("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'"); + } } diff --git a/model/Blog.php b/model/Blog.php index 7b1e476..dd2e2b4 100755 --- a/model/Blog.php +++ b/model/Blog.php @@ -74,7 +74,8 @@ class Blog extends Page { public function getSettingsFields() { $fields = parent::getSettingsFields(); - $fields->addFieldToTab("Root.Settings", NumericField::create("PostsPerPage", _t("Blog.PostsPerPage", "Posts Per Page"))); + $fields->addFieldToTab("Root.Settings", + NumericField::create("PostsPerPage", _t("Blog.PostsPerPage", "Posts Per Page"))); return $fields; } @@ -111,6 +112,35 @@ class Blog extends Page { return $blogPosts; } + + + /** + * 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); + } + } @@ -176,21 +206,7 @@ class Blog_Controller extends Page_Controller { } if($year) { - $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) . "'"); - } - } - - $this->blogPosts = $this->getBlogPosts()->setDataQuery($query); + $this->blogPosts = $this->getArchivedBlogPosts($year, $month, $day); return $this->render(); } return $this->httpError(404, "Not Found"); diff --git a/model/BlogPost.php b/model/BlogPost.php index d3fe628..545f27f 100755 --- a/model/BlogPost.php +++ b/model/BlogPost.php @@ -63,8 +63,10 @@ class BlogPost extends Page { public function getCMSFields() { // Assign to variable & pass for PHP <= 5.4 closure compatibility - $data['TagsMap'] = $this->Parent()->Tags()->map()->toArray(); - $data['CategoryMap'] = $this->Parent()->Categories()->map()->toArray(); + $data = array( + "TagsMap" => $this->Parent()->Tags()->map()->toArray(), + "CategoryMap" => $this->Parent()->Categories()->map()->toArray() + ); $this->beforeUpdateCMSFields(function($fields) use ($data) { // Add Publish date fields @@ -108,7 +110,7 @@ class BlogPost extends Page { /** - * Checks the publish date to see if the blog post as actually been published. + * Checks the publish date to see if the blog post has actually been published. * * @param $member Member|null * diff --git a/tests/BlogPostFilterTest.php b/tests/BlogPostFilterTest.php new file mode 100755 index 0000000..5dfe667 --- /dev/null +++ b/tests/BlogPostFilterTest.php @@ -0,0 +1,24 @@ +logout(); + + $count = BlogPost::get()->count(); + $this->assertEquals(3, $count, "Filtered blog posts"); + + SS_Datetime::set_mock_now("2020-01-01 00:00:00"); + $count = BlogPost::get()->count(); + $this->assertEquals(5, $count, "Unfiltered blog posts"); + } + +} diff --git a/tests/BlogTagTest.php b/tests/BlogTagTest.php new file mode 100755 index 0000000..73c20eb --- /dev/null +++ b/tests/BlogTagTest.php @@ -0,0 +1,22 @@ +logout(); + + $post = $this->objFromFixture("BlogPost", "blogpost1"); + $tag = $this->objFromFixture("BlogTag", "firsttag"); + $posts = $tag->BlogPosts(); + $this->assertEquals(1, $posts->count(), "Tag blog post count"); + } + +} diff --git a/tests/BlogTest.php b/tests/BlogTest.php new file mode 100755 index 0000000..34f73bc --- /dev/null +++ b/tests/BlogTest.php @@ -0,0 +1,50 @@ +logout(); + + $blog = $this->objFromFixture("Blog", 'firstblog'); + + Config::inst()->update("BlogPost", "show_in_sitetree", true); + $classes = $blog->getExcludedSiteTreeClassNames(); + $this->assertEquals(0, count($classes), "No classes should be hidden."); + + Config::inst()->update("BlogPost", "show_in_sitetree", false); + $classes = $blog->getExcludedSiteTreeClassNames(); + $this->assertEquals(1, count($classes), "BlogPost class should be hidden."); + } + + + + public function testGetArchivedBlogPosts() { + $member = Member::currentUser(); + if($member) $member->logout(); + + $blog = $this->objFromFixture("Blog", "firstblog"); + + // Test yearly + $archive = $blog->getArchivedBlogPosts(2013); + $this->assertEquals(2, $archive->count(), "Incorrect Yearly Archive count for 2013"); + $this->assertEquals("First post", $archive->first()->Title, "Incorrect First Blog post"); + $this->assertEquals("Second post", $archive->last()->Title, "Incorrect Last Blog post"); + + // Test monthly + $archive = $blog->getArchivedBlogPosts(2013, 10); + $this->assertEquals(1, $archive->count(), "Incorrect monthly acrhive count."); + + // Test daily + $archive = $blog->getArchivedBlogPosts(2013, 10, 01); + $this->assertEquals(1, $archive->count(), "Incorrect daily archive count."); + } + +} \ No newline at end of file diff --git a/tests/blog.yml b/tests/blog.yml new file mode 100755 index 0000000..c4f6c71 --- /dev/null +++ b/tests/blog.yml @@ -0,0 +1,47 @@ +##################################################### +# Mock date is set to 2013-10-01 20:00:00 +##################################################### + +Blog: + firstblog: + Title: 'First Blog' + +BlogTag: + firsttag: + Title: 'First Tag' + URLSegment: 'first-tag'; + Blog: =>Blog.firstblog + +BlogCategory: + firstcategory: + Title: 'First Category' + URLSegment: 'first-category' + Blog: =>Blog.firstblog + +BlogPost: + blogpost1: + Title: 'First post' + URLSegment: first-post + PublishDate: '2013-10-01 15:00:00' + Parent: =>Blog.firstblog + Tags: =>BlogTag.firsttag + blogpost2: + Title: 'Second post' + URLSegment: second-post + PublishDate: '2013-09-01 15:00:00' + Parent: =>Blog.firstblog + blogpost3: + Title: 'Old post' + URLSegment: old-post + PublishDate: '2012-01-09 15:00:00' + Parent: =>Blog.firstblog + futurepost: + Title: 'Future Post' + URLSegment: future-post + PublishDate: '2015-01-01 00:00:00' + Tags: =>BlogTag.firsttag + futurepost2: + Title: 'Future Post 2' + URLSegment: future-post-2 + PublishDate: '2013-11-01 00:00:00' + Tags: =>BlogTag.firsttag diff --git a/widgets/BlogArchiveWidget.php b/widgets/BlogArchiveWidget.php index 1598899..68e129c 100755 --- a/widgets/BlogArchiveWidget.php +++ b/widgets/BlogArchiveWidget.php @@ -1,81 +1,85 @@ "Int", - "Type" => "Enum('Monthly, Yearly', 'Monthly')" - ); + private static $description = "Displays an archive list of posts."; - private static $defaults = array( - "NumberOfMonths" => 12 - ); + private static $db = array( + "NumberToDisplay" => "Int", + "Type" => "Enum('Monthly, Yearly', 'Monthly')" + ); - private static $has_one = array( - "Blog" => "Blog", - ); + private static $defaults = array( + "NumberOfMonths" => 12 + ); - public function getCMSFields() { - $fields = parent::getCMSFields(); + private static $has_one = array( + "Blog" => "Blog", + ); - $type = $this->dbObject("Type")->enumValues(); - foreach($type as $k => $v) { - $type[$k] = _t("BlogArchiveWidget." . ucfirst(strtolower($v)), $v); + public function getCMSFields() { + $fields = parent::getCMSFields(); + + $type = $this->dbObject("Type")->enumValues(); + foreach($type as $k => $v) { + $type[$k] = _t("BlogArchiveWidget." . ucfirst(strtolower($v)), $v); + } + + $fields->push(DropdownField::create("BlogID", _t("BlogArchiveWidget.Blog", "Blog"), Blog::get()->map())); + $fields->push(DropdownField::create("Type", _t("BlogArchiveWidget.Type", "Type"), $type)); + $fields->push(NumericField::create("NumberToDisplay", _t("BlogArchiveWidget.NumberToDisplay", "No. to Display"))); + return $fields; + } + + + /** + * Returns a list of months where blog posts are present. + * + * @return DataList + **/ + public function getArchive() { + $query = $this->Blog()->getBlogPosts()->dataQuery(); + + if($this->Type == "Yearly") { + $query->groupBy("DATE_FORMAT(PublishDate, '%Y')"); + } else { + $query->groupBy("DATE_FORMAT(PublishDate, '%Y-%M')"); + } + + $articles = $this->Blog()->getBlogPosts()->setDataQuery($query); + if($this->NumberToDisplay > 0) $articles = $articles->limit($this->NumberToDisplay); + + $archive = new ArrayList(); + if($articles->count() > 0) { + foreach($articles as $article) { + if($this->Type == "Yearly") { + $year = date('Y', strtotime($article->PublishDate)); + $month = null; + $title = $year; + } else { + $year = date('Y', strtotime($article->PublishDate)); + $month = date('m', strtotime($article->PublishDate)); + $title = date('F Y', strtotime($article->PublishDate)); + } + $archive->push(new ArrayData(array( + "Title" => $title, + "Link" => Controller::join_links($this->Blog()->Link("archive"), $year, $month) + ))); + } + } + return $archive; } - $fields->push(DropdownField::create("BlogID", _t("BlogArchiveWidget.Blog", "Blog"), Blog::get()->map())); - $fields->push(DropdownField::create("Type", _t("BlogArchiveWidget.Type", "Type"), $type)); - $fields->push(NumericField::create("NumberToDisplay", _t("BlogArchiveWidget.NumberToDisplay", "No. to Display"))); - return $fields; } - - /** - * Returns a list of months where blog posts are present. - * - * @return DataList - **/ - public function getArchive() { - $query = $this->Blog()->getBlogPosts()->dataQuery(); - - if($this->Type == "Yearly") { - $query->groupBy("DATE_FORMAT(PublishDate, '%Y')"); - } else { - $query->groupBy("DATE_FORMAT(PublishDate, '%Y-%M')"); - } - - $articles = $this->Blog()->getBlogPosts()->setDataQuery($query); - if($this->NumberToDisplay > 0) $articles = $articles->limit($this->NumberToDisplay); + class BlogArchiveWidget_Controller extends Widget_Controller { - $archive = new ArrayList(); - if($articles->count() > 0) { - foreach($articles as $article) { - if($this->Type == "Yearly") { - $year = date('Y', strtotime($article->PublishDate)); - $month = null; - $title = $year; - } else { - $year = date('Y', strtotime($article->PublishDate)); - $month = date('m', strtotime($article->PublishDate)); - $title = date('F Y', strtotime($article->PublishDate)); - } - $archive->push(new ArrayData(array( - "Title" => $title, - "Link" => Controller::join_links($this->Blog()->Link("archive"), $year, $month) - ))); - } - } - return $archive; } } - -class BlogArchiveWidget_Controller extends Widget_Controller { - -} \ No newline at end of file diff --git a/widgets/BlogCategoriesWIdget.php b/widgets/BlogCategoriesWIdget.php index cf26726..cdc1aee 100755 --- a/widgets/BlogCategoriesWIdget.php +++ b/widgets/BlogCategoriesWIdget.php @@ -1,35 +1,39 @@ "Blog", - ); + private static $db = array(); + + private static $has_one = array( + "Blog" => "Blog", + ); + + public function getCMSFields() { + $fields = parent::getCMSFields(); + $fields->push(DropdownField::create("BlogID", _t("BlogCategoriesWidget.Blog", "Blog"), Blog::get()->map())); + return $fields; + } + + public function getCategories() { + $blog = $this->Blog(); + if($blog) { + return $blog->Categories(); + } + return array(); + } - public function getCMSFields() { - $fields = parent::getCMSFields(); - $fields->push(DropdownField::create("BlogID", _t("BlogCategoriesWidget.Blog", "Blog"), Blog::get()->map())); - return $fields; } - public function getCategories() { - $blog = $this->Blog(); - if($blog) { - return $blog->Categories(); - } - return array(); + class BlogCategoriesWidget_Controller extends Widget_Controller { + } } - -class BlogCategoriesWidget_Controller extends Widget_Controller { - -} \ No newline at end of file diff --git a/widgets/BlogRecentPostsWidget.php b/widgets/BlogRecentPostsWidget.php index cda733e..021f9c1 100755 --- a/widgets/BlogRecentPostsWidget.php +++ b/widgets/BlogRecentPostsWidget.php @@ -1,40 +1,44 @@ "Int", - ); + private static $description = "Displays a list of recent blog posts."; - private static $has_one = array( - "Blog" => "Blog", - ); + private static $db = array( + "NumberOfPosts" => "Int", + ); - public function getCMSFields() { - $fields = parent::getCMSFields(); - $fields->push(DropdownField::create("BlogID", _t("BlogRecentPostsWidget.Blog", "Blog"), Blog::get()->map())); - $fields->push(NumericField::create("NumberOfPosts", _t("BlogRecentPostsWidget.NumberOfPosts", "Number of Posts"))); - return $fields; - } + private static $has_one = array( + "Blog" => "Blog", + ); - public function getPosts() { - $blog = $this->Blog(); - if($blog) { - return $blog->getBlogPosts() - ->sort("PublishDate DESC") - ->limit($this->NumberOfPosts); + public function getCMSFields() { + $fields = parent::getCMSFields(); + $fields->push(DropdownField::create("BlogID", _t("BlogRecentPostsWidget.Blog", "Blog"), Blog::get()->map())); + $fields->push(NumericField::create("NumberOfPosts", _t("BlogRecentPostsWidget.NumberOfPosts", "Number of Posts"))); + return $fields; } - return array(); + + public function getPosts() { + $blog = $this->Blog(); + if($blog) { + return $blog->getBlogPosts() + ->sort("PublishDate DESC") + ->limit($this->NumberOfPosts); + } + return array(); + } + } -} + class BlogRecentPostsWidget_Controller extends Widget_Controller { + + } -class BlogRecentPostsWidget_Controller extends Widget_Controller { - } \ No newline at end of file diff --git a/widgets/BlogTagsWidget.php b/widgets/BlogTagsWidget.php index a3f4251..d977351 100755 --- a/widgets/BlogTagsWidget.php +++ b/widgets/BlogTagsWidget.php @@ -1,35 +1,39 @@ "Blog", - ); + private static $db = array(); - public function getCMSFields() { - $fields = parent::getCMSFields(); - $fields->push(DropdownField::create("BlogID", _t("BlogTagsWidget.Blog", "Blog"), Blog::get()->map())); - return $fields; - } + private static $has_one = array( + "Blog" => "Blog", + ); - public function getTags() { - $blog = $this->Blog(); - if($blog) { - return $blog->Tags(); + public function getCMSFields() { + $fields = parent::getCMSFields(); + $fields->push(DropdownField::create("BlogID", _t("BlogTagsWidget.Blog", "Blog"), Blog::get()->map())); + return $fields; } - return array(); + + public function getTags() { + $blog = $this->Blog(); + if($blog) { + return $blog->Tags(); + } + return array(); + } + } -} + class BlogTagsWidget_Controller extends Widget_Controller { + + } -class BlogTagsWidget_Controller extends Widget_Controller { - } \ No newline at end of file