Added tag field

This commit is contained in:
Christopher Pitt 2015-04-08 11:48:00 +12:00
parent c03ec88467
commit 3e29c378ed
2 changed files with 98 additions and 29 deletions

View File

@ -168,16 +168,20 @@ class BlogPost extends Page {
$options = BlogAdminSidebar::create( $options = BlogAdminSidebar::create(
$publishDate = DatetimeField::create("PublishDate", _t("BlogPost.PublishDate", "Publish Date")), $publishDate = DatetimeField::create("PublishDate", _t("BlogPost.PublishDate", "Publish Date")),
$urlSegment, $urlSegment,
ListboxField::create( TagField::create(
"Categories", 'Categories',
_t("BlogPost.Categories", "Categories"), _t('BlogPost.Categories', 'Categories'),
$self->Parent()->Categories()->map()->toArray() $self->Parent()->Categories()->map(),
)->setMultiple(true), $self->Categories()->map(),
ListboxField::create( !$this->canCreateCategories()
"Tags", ),
_t("BlogPost.Tags", "Tags"), TagField::create(
$self->Parent()->Tags()->map()->toArray() 'Tags',
)->setMultiple(true), _t('BlogPost.Tags', 'Tags'),
$self->Parent()->Tags()->map(),
$self->Tags()->map(),
!$this->canCreateTags()
),
$authorField, $authorField,
$authorNames $authorNames
)->setTitle('Post Options'); )->setTitle('Post Options');
@ -195,6 +199,48 @@ class BlogPost extends Page {
return $fields; return $fields;
} }
/**
* Determine whether user can create new categories.
*
* @param int|Member|null $member
*
* @return bool
*/
public function canCreateCategories($member = null) {
$member = $member ?: Member::currentUser();
if(is_numeric($member)) $member = Member::get()->byID($member);
$parent = $this->Parent();
if(!$parent || !$parent->exists() || !($parent instanceof Blog)) return false;
if($parent->isEditor($member)) return true;
return Permission::checkMember($member, 'ADMIN');
}
/**
* Determine whether user can create new tags.
*
* @param int|Member|null $member
*
* @return bool
*/
public function canCreateTags($member = null) {
$member = $member ?: Member::currentUser();
if(is_numeric($member)) $member = Member::get()->byID($member);
$parent = $this->Parent();
if(!$parent || !$parent->exists() || !($parent instanceof Blog)) return false;
if($parent->isEditor($member)) return true;
if($parent->isWriter($member)) return true;
return Permission::checkMember($member, 'ADMIN');
}
protected function onBeforeWrite() { protected function onBeforeWrite() {
parent::onBeforeWrite(); parent::onBeforeWrite();
@ -220,7 +266,25 @@ class BlogPost extends Page {
} }
} }
/**
* Sets blog relationship on all categories and tags assigned to this post.
*
* @throws ValidationException
*/
public function onAfterWrite()
{
parent::onAfterWrite();
foreach ($this->Categories() as $category) {
$category->BlogID = $this->ParentID;
$category->write();
}
foreach ($this->Tags() as $tag) {
$tag->BlogID = $this->ParentID;
$tag->write();
}
}
/** /**
* Checks the publish date to see if the blog post has actually been published. * Checks the publish date to see if the blog post has actually been published.

View File

@ -1,12 +1,17 @@
{ {
"name": "silverstripe/blog", "name": "silverstripe/blog",
"description": "A fresh take on blogging in Silverstripe set out to tackle the issue of a cluttered Site Tree.", "description": "A fresh take on blogging in Silverstripe set out to tackle the issue of a cluttered Site Tree.",
"keywords": ["silverstripe", "blog", "news"], "keywords": [
"type": "silverstripe-module", "silverstripe",
"require": { "blog",
"silverstripe/cms": ">=3.1.0", "news"
"silverstripe/lumberjack": "~1.1" ],
}, "type": "silverstripe-module",
"require": {
"silverstripe/cms": ">=3.1.0",
"silverstripe/lumberjack": "~1.1",
"silverstripe/tagfield": "dev-develop"
},
"require-dev": { "require-dev": {
"phpunit/PHPUnit": "~3.7@stable" "phpunit/PHPUnit": "~3.7@stable"
}, },
@ -15,15 +20,15 @@
"dev-master": "2.0.x-dev" "dev-master": "2.0.x-dev"
} }
}, },
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"authors": [ "authors": [
{ {
"name": "Michael Strong", "name": "Michael Strong",
"email": "github@michaelstrong.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."
} }
} }