From d9d12dd62122954797273b52abcbefe181963871 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Mon, 24 May 2010 04:22:56 +0000 Subject: [PATCH] ENHANCEMENT: display only members of Administrators and Blog Management groups in the blog holder's blog owners dropdown field FEATURE: allow blog owner customisation --- code/BlogHolder.php | 22 +++++++++++++++++++++- tests/BlogHolderTest.php | 15 +++++++++++++++ tests/BlogTest.yml | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/code/BlogHolder.php b/code/BlogHolder.php index ee825f7..775ea45 100644 --- a/code/BlogHolder.php +++ b/code/BlogHolder.php @@ -28,18 +28,38 @@ class BlogHolder extends BlogTree implements PermissionProvider { ); function getCMSFields() { + $blogOwners = $this->blogOwners(); + SiteTree::disableCMSFieldsExtensions(); $fields = parent::getCMSFields(); SiteTree::enableCMSFieldsExtensions(); $fields->addFieldToTab('Root.Content.Main', new CheckboxField('TrackBacksEnabled', 'Enable TrackBacks')); - $fields->addFieldToTab('Root.Content.Main', new DropdownField('OwnerID', 'Blog owner', DataObject::get('Member')->toDropDownMap('ID', 'Name', 'None'))); + $fields->addFieldToTab('Root.Content.Main', new DropdownField('OwnerID', 'Blog owner', $blogOwners->toDropDownMap('ID', 'Name', 'None'))); $fields->addFieldToTab('Root.Content.Main', new CheckboxField('AllowCustomAuthors', 'Allow non-admins to have a custom author field')); $this->extend('updateCMSFields', $fields); return $fields; } + + /** + * Get members who have BLOGMANAGEMENT and ADMIN permission + */ + function blogOwners($sort = 'Name', $direction = "ASC") { + $adminMembers = Permission::get_members_by_permission('ADMIN'); + $blogOwners = Permission::get_members_by_permission('BLOGMANAGEMENT'); + + if(!$adminMembers) $adminMembers = new DataObjectSet(); + if(!$blogOwners) $blogOwners = new DataObjectSet(); + + $blogOwners->merge($adminMembers); + $blogOwners->sort($sort, $direction); + + $this->extend('extendBlogOwners', $blogOwners); + + return $blogOwners; + } public function BlogHolderIDs() { return array( $this->ID ); diff --git a/tests/BlogHolderTest.php b/tests/BlogHolderTest.php index 9d6b653..05938a6 100644 --- a/tests/BlogHolderTest.php +++ b/tests/BlogHolderTest.php @@ -53,6 +53,21 @@ class BlogHolderTest extends SapphireTest { $this->assertContains($entry->URLSegment, $expectedEntries); } } + + function testBlogOwners() { + $mainblog = $this->objFromFixture('BlogHolder', 'mainblog'); + $actualMembers = array_values($mainblog->blogOwners()->toDropDownMap('ID', 'Name')); + $expectedMembers = array( + 'ADMIN User', // test default admin + 'Admin One', + 'Admin Two', + 'Blog Owner One', + 'Blog Owner Three', + 'Blog Owner Two', + ); + + $this->assertEquals($expectedMembers, $actualMembers); + } } diff --git a/tests/BlogTest.yml b/tests/BlogTest.yml index 1d49fe6..c21bf5a 100644 --- a/tests/BlogTest.yml +++ b/tests/BlogTest.yml @@ -23,6 +23,43 @@ BlogEntry: Date: 2008-01-17 18:45:00 Parent: =>BlogHolder.mainblog Tags: tag1,tag2,tag3 - +Permission: + admin: + Code: ADMIN + blogOwners: + Code: BLOGMANAGEMENT + +Group: + admin: + Title: Admin + Permissions: =>Permission.admin + blogOwners: + Title: Blog Owners + Permissions: =>Permission.blogOwners + +Member: + admin1: + Name: Admin One + Groups: =>Group.admin + admin2: + Name: Admin Two + Groups: =>Group.admin + blogOwner1: + Name: Blog Owner One + Groups: =>Group.blogOwners + blogOwner2: + Name: Blog Owner Two + Groups: =>Group.blogOwners + blogOwner3: + Name: Blog Owner Three + Groups: =>Group.admin, =>Group.blogOwners + noBody: + Name: No Body + + + + + +