silverstripe-blog/code/model/BlogPost.php

685 lines
15 KiB
PHP
Raw Normal View History

2013-07-21 12:23:35 +02:00
<?php
/**
2015-05-09 16:33:12 +02:00
* An individual blog post.
2013-07-21 12:23:35 +02:00
*
* @package silverstripe
* @subpackage blog
*
* @method ManyManyList Categories()
* @method ManyManyList Tags()
* @method ManyManyList Authors()
2015-05-09 16:33:12 +02:00
* @method Blog Parent()
*
2015-05-09 16:33:12 +02:00
* @property string $PublishDate
* @property string $AuthorNames
* @property int $ParentID
*/
2013-07-21 12:23:35 +02:00
class BlogPost extends Page {
/**
* Same as above, but for list of users that can be
* given credit in the author field for blog posts
* @var string|bool false or group code
*/
private static $restrict_authors_to_group = false;
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
2013-07-21 12:23:35 +02:00
private static $db = array(
2015-05-09 16:33:12 +02:00
'PublishDate' => 'SS_Datetime',
'AuthorNames' => 'Varchar(1024)',
'Summary' => 'HTMLText',
2013-07-21 12:23:35 +02:00
);
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
2013-08-04 18:38:26 +02:00
private static $has_one = array(
2015-05-09 16:33:12 +02:00
'FeaturedImage' => 'Image',
2013-08-04 18:38:26 +02:00
);
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
2013-08-04 18:38:26 +02:00
private static $many_many = array(
2015-05-09 16:33:12 +02:00
'Categories' => 'BlogCategory',
'Tags' => 'BlogTag',
'Authors' => 'Member',
2013-08-04 18:38:26 +02:00
);
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
2013-08-04 18:38:26 +02:00
private static $defaults = array(
2015-05-09 16:33:12 +02:00
'ShowInMenus' => false,
'InheritSideBar' => true,
'ProvideComments' => true,
2013-08-04 18:38:26 +02:00
);
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
2013-08-04 18:38:26 +02:00
private static $extensions = array(
2015-05-09 16:33:12 +02:00
'BlogPostFilter',
2013-08-04 18:38:26 +02:00
);
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
private static $searchable_fields = array(
2015-05-09 16:33:12 +02:00
'Title',
);
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
private static $summary_fields = array(
2015-05-09 16:33:12 +02:00
'Title',
);
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
2015-03-04 23:20:47 +01:00
private static $casting = array(
2015-05-09 16:33:12 +02:00
'Excerpt' => 'Text',
2015-03-04 23:20:47 +01:00
);
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
private static $allowed_children = array();
2015-05-09 16:33:12 +02:00
/**
* The default sorting lists BlogPosts with an empty PublishDate at the top.
*
2015-05-09 16:33:12 +02:00
* @var string
*/
private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC';
2013-07-21 12:23:35 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var bool
*/
private static $can_be_root = false;
2013-08-04 18:38:26 +02:00
2013-07-21 12:23:35 +02:00
/**
2015-05-09 16:33:12 +02:00
* This will display or hide the current class from the SiteTree. This variable can be
* configured using YAML.
2013-07-21 12:23:35 +02:00
*
2015-05-09 16:33:12 +02:00
* @var bool
*/
private static $show_in_sitetree = false;
2013-07-21 12:23:35 +02:00
/**
2015-05-09 16:33:12 +02:00
* Determine the role of the given member.
*
2015-05-09 16:33:12 +02:00
* Call be called via template to determine the current user.
*
* @example "Hello $RoleOf($CurrentMember.ID)"
*
* @param null|int|Member $member
*
* @return null|string
*/
2015-05-09 16:33:12 +02:00
public function RoleOf($member = null) {
$member = $this->getMember($member);
2015-05-09 16:33:12 +02:00
if(!$member) {
return null;
}
2015-05-09 16:33:12 +02:00
if($this->isAuthor($member)) {
return _t('BlogPost.AUTHOR', 'Author');
}
$parent = $this->Parent();
if($parent instanceof Blog) {
return $parent->RoleOf($member);
}
return null;
}
2015-04-09 05:45:12 +02:00
/**
2015-05-09 16:33:12 +02:00
* Determine if the given member is an author of this post.
2015-04-09 05:45:12 +02:00
*
2015-05-09 16:33:12 +02:00
* @param null|Member $member
2015-04-09 05:45:12 +02:00
*
2015-05-09 16:33:12 +02:00
* @return bool
2015-04-09 05:45:12 +02:00
*/
2015-05-09 16:33:12 +02:00
public function isAuthor($member = null) {
if(!$member || !$member->exists()) {
return false;
}
2015-04-09 05:45:12 +02:00
2015-05-09 16:33:12 +02:00
$list = $this->Authors();
2015-04-09 05:45:12 +02:00
2015-05-09 16:33:12 +02:00
if($list instanceof UnsavedRelationList) {
return in_array($member->ID, $list->getIDList());
}
2015-04-09 05:45:12 +02:00
2015-05-09 16:33:12 +02:00
return $list->byID($member->ID) !== null;
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* {@inheritdoc}
*/
2013-07-21 12:23:35 +02:00
public function getCMSFields() {
Requirements::css(BLOGGER_DIR . '/css/cms.css');
2015-04-23 04:27:30 +02:00
Requirements::javascript(BLOGGER_DIR . '/js/cms.js');
2013-08-04 18:38:26 +02:00
$self =& $this;
2015-05-09 16:33:12 +02:00
$this->beforeUpdateCMSFields(function ($fields) use ($self) {
2015-05-12 07:46:19 +02:00
$uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Featured Image'));
$uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
/**
* @var FieldList $fields
*/
2015-05-18 00:55:18 +02:00
$fields->insertAfter($uploadField, 'Content');
2015-05-12 07:46:19 +02:00
2015-05-09 16:33:12 +02:00
$summary = HtmlEditorField::create('Summary', false);
$summary->setRows(5);
$summary->setDescription(_t(
'BlogPost.SUMMARY_DESCRIPTION',
'If no summary is specified the first 30 words will be used.'
));
$summaryHolder = ToggleCompositeField::create(
'CustomSummary',
_t('BlogPost.CUSTOMSUMMARY', 'Add A Custom Summary'),
array(
2015-05-09 16:33:12 +02:00
$summary,
)
2015-05-09 16:33:12 +02:00
);
$summaryHolder->setHeadingLevel(4);
$summaryHolder->addExtraClass('custom-summary');
2015-05-12 07:46:19 +02:00
$fields->insertAfter($summaryHolder, 'FeaturedImage');
2015-02-02 11:32:40 +01:00
$fields->push(HiddenField::create('MenuTitle'));
$urlSegment = $fields->dataFieldByName('URLSegment');
2015-03-04 23:20:47 +01:00
$urlSegment->setURLPrefix($self->Parent()->RelativeLink());
2015-02-02 11:32:40 +01:00
$fields->removeFieldsFromTab('Root.Main', array(
'MenuTitle',
'URLSegment',
));
2015-02-02 11:32:40 +01:00
$authorField = ListboxField::create(
2015-05-09 16:33:12 +02:00
'Authors',
_t('BlogPost.Authors', 'Authors'),
$this->getCandidateAuthors()->map()->toArray()
)->setMultiple(true);
$authorNames = TextField::create(
2015-05-09 16:33:12 +02:00
'AuthorNames',
_t('BlogPost.AdditionalCredits', 'Additional Credits'),
null,
1024
)->setDescription(_t(
'BlogPost.PublishDate_Description',
'If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.')
2015-05-09 16:33:12 +02:00
);
if(!$self->canEditAuthors()) {
$authorField = $authorField->performDisabledTransformation();
$authorNames = $authorNames->performDisabledTransformation();
}
2015-05-09 16:33:12 +02:00
$publishDate = DatetimeField::create('PublishDate', _t('BlogPost.PublishDate', 'Publish Date'));
$publishDate->getDateField()->setConfig('showcalendar', true);
if(!$self->PublishDate) {
$publishDate->setDescription(_t(
'BlogPost.PublishDate_Description',
'Will be set to "now" if published without a value.')
);
}
2015-05-09 16:33:12 +02:00
// Get categories and tags
$parent = $self->Parent();
$categories = $parent instanceof Blog
? $parent->Categories()
: BlogCategory::get();
$tags = $parent instanceof Blog
? $parent->Tags()
: BlogTag::get();
$options = BlogAdminSidebar::create(
2015-05-09 16:33:12 +02:00
$publishDate,
2015-02-02 11:32:40 +01:00
$urlSegment,
2015-04-08 01:48:00 +02:00
TagField::create(
'Categories',
_t('BlogPost.Categories', 'Categories'),
$categories,
2015-05-15 13:17:58 +02:00
$self->Categories()
)
->setCanCreate($self->canCreateCategories())
->setShouldLazyLoad(true),
2015-04-08 01:48:00 +02:00
TagField::create(
'Tags',
_t('BlogPost.Tags', 'Tags'),
$tags,
2015-05-15 13:17:58 +02:00
$self->Tags()
)
->setCanCreate($self->canCreateTags())
->setShouldLazyLoad(true),
$authorField,
$authorNames
)->setTitle('Post Options');
2015-02-02 11:32:40 +01:00
$fields->insertBefore($options, 'Root');
});
2013-08-04 18:38:26 +02:00
$fields = parent::getCMSFields();
2015-02-02 11:32:40 +01:00
$fields->fieldByName('Root')->setTemplate('TabSet_holder');
2013-07-21 12:23:35 +02:00
return $fields;
}
/**
* Gets the list of author candidates to be assigned as authors of this blog post.
*
* @return SS_List
*/
public function getCandidateAuthors() {
if($this->config()->restrict_authors_to_group) {
return Group::get()->filter('Code', $this->config()->restrict_authors_to_group)->first()->Members();
} else {
$list = Member::get();
$this->extend('updateCandidateAuthors', $list);
return $list;
}
}
2015-04-08 01:48:00 +02:00
/**
2015-05-09 16:33:12 +02:00
* Determine if this user can edit the authors list.
2015-04-08 01:48:00 +02:00
*
2015-05-09 16:33:12 +02:00
* @param null|int|Member $member
2015-04-08 01:48:00 +02:00
*
* @return bool
*/
2015-05-09 16:33:12 +02:00
public function canEditAuthors($member = null) {
$member = $this->getMember($member);
$extended = $this->extendedCan('canEditAuthors', $member);
if($extended !== null) {
return $extended;
}
2015-04-08 01:48:00 +02:00
$parent = $this->Parent();
2015-05-09 16:33:12 +02:00
if($parent instanceof Blog && $parent->exists()) {
if($parent->isEditor($member)) {
return true;
}
2015-04-08 01:48:00 +02:00
2015-05-09 16:33:12 +02:00
if($parent->isWriter($member) && $this->isAuthor($member)) {
return true;
}
}
2015-04-08 01:48:00 +02:00
2015-05-09 16:33:12 +02:00
return Permission::checkMember($member, Blog::MANAGE_USERS);
2015-04-08 01:48:00 +02:00
}
/**
2015-05-09 16:33:12 +02:00
* @param null|int|Member $member
*
* @return null|Member
*/
protected function getMember($member = null) {
if(!$member) {
$member = Member::currentUser();
}
if(is_numeric($member)) {
2015-05-09 16:33:12 +02:00
$member = Member::get()->byID($member);
}
return $member;
}
/**
* Determine whether user can create new categories.
2015-04-08 01:48:00 +02:00
*
2015-05-09 16:33:12 +02:00
* @param null|int|Member $member
2015-04-08 01:48:00 +02:00
*
* @return bool
*/
2015-05-09 16:33:12 +02:00
public function canCreateCategories($member = null) {
$member = $this->getMember($member);
2015-04-08 01:48:00 +02:00
$parent = $this->Parent();
2015-05-09 16:33:12 +02:00
if(!$parent || !$parent->exists() || !($parent instanceof Blog)) {
return false;
}
2015-04-08 01:48:00 +02:00
2015-05-09 16:33:12 +02:00
if($parent->isEditor($member)) {
return true;
}
2015-04-08 01:48:00 +02:00
return Permission::checkMember($member, 'ADMIN');
}
2015-05-09 16:33:12 +02:00
/**
* Determine whether user can create new tags.
*
* @param null|int|Member $member
*
* @return bool
*/
public function canCreateTags($member = null) {
$member = $this->getMember($member);
2015-05-09 16:33:12 +02:00
$parent = $this->Parent();
2015-05-09 16:33:12 +02:00
if(!$parent || !$parent->exists() || !($parent instanceof Blog)) {
return false;
}
2013-07-21 12:23:35 +02:00
2015-05-09 16:33:12 +02:00
if($parent->isEditor($member)) {
return true;
}
2013-07-21 12:23:35 +02:00
2015-05-09 16:33:12 +02:00
if($parent->isWriter($member)) {
return true;
}
return Permission::checkMember($member, 'ADMIN');
}
2013-07-21 12:23:35 +02:00
/**
2015-05-09 16:33:12 +02:00
* {@inheritdoc}
*
* Update the PublishDate to now if the BlogPost would otherwise be published without a date.
2015-05-09 16:33:12 +02:00
*/
public function onBeforePublish() {
2015-05-09 16:33:12 +02:00
/**
* @var SS_Datetime $publishDate
*/
$publishDate = $this->dbObject('PublishDate');
if(!$publishDate->getValue()) {
$this->PublishDate = SS_Datetime::now()->getValue();
$this->write();
}
}
2015-04-08 01:48:00 +02:00
/**
2015-05-09 16:33:12 +02:00
* {@inheritdoc}
2015-04-08 01:48:00 +02:00
*
2015-05-09 16:33:12 +02:00
* Sets blog relationship on all categories and tags assigned to this post.
2015-04-08 01:48:00 +02:00
*/
2015-05-09 16:33:12 +02:00
public function onAfterWrite() {
2015-04-08 01:48:00 +02:00
parent::onAfterWrite();
2015-05-09 16:33:12 +02:00
foreach($this->Categories() as $category) {
/**
* @var BlogCategory $category
*/
2015-04-08 01:48:00 +02:00
$category->BlogID = $this->ParentID;
$category->write();
}
2015-05-09 16:33:12 +02:00
foreach($this->Tags() as $tag) {
/**
* @var BlogTag $tag
*/
2015-04-08 01:48:00 +02:00
$tag->BlogID = $this->ParentID;
$tag->write();
}
}
2013-07-21 12:23:35 +02:00
/**
2015-05-09 16:33:12 +02:00
* {@inheritdoc}
*/
2013-07-21 12:23:35 +02:00
public function canView($member = null) {
2015-07-08 07:42:00 +02:00
$member = $this->getMember($member);
2015-05-09 16:33:12 +02:00
if(!parent::canView($member)) {
return false;
}
2015-07-08 07:42:00 +02:00
/**
* @var SS_Datetime $publishDate
*/
$publishDate = $this->dbObject('PublishDate');
2013-07-21 12:23:35 +02:00
2015-07-08 07:42:00 +02:00
// Show past posts
if(!$publishDate->exists() || !$publishDate->InFuture()) {
return true;
2013-07-21 12:23:35 +02:00
}
2015-05-09 16:33:12 +02:00
2015-07-08 07:42:00 +02:00
// Anyone that can edit this page can view it
return $this->canEdit($member);
2013-07-21 12:23:35 +02:00
}
2015-05-09 16:33:12 +02:00
/**
* {@inheritdoc}
*/
public function canPublish($member = null) {
$member = $this->getMember($member);
2015-05-09 16:33:12 +02:00
if(Permission::checkMember($member, 'ADMIN')) {
return true;
}
$extended = $this->extendedCan('canPublish', $member);
if($extended !== null) {
return $extended;
}
$parent = $this->Parent();
2015-05-09 16:33:12 +02:00
if($parent instanceof Blog && $parent->exists()) {
if($parent->isEditor($member)) {
return true;
}
2015-05-09 16:33:12 +02:00
if($parent->isWriter($member) && $this->isAuthor($member)) {
return true;
}
2015-05-09 16:33:12 +02:00
if($parent->isContributor($member)) {
return parent::canEdit($member);
}
}
return $this->canEdit($member);
}
/**
2015-05-09 16:33:12 +02:00
* {@inheritdoc}
*/
2015-05-09 16:33:12 +02:00
public function canEdit($member = null) {
$member = $this->getMember($member);
2015-05-09 16:33:12 +02:00
if(parent::canEdit($member)) {
return true;
}
$parent = $this->Parent();
2015-05-09 16:33:12 +02:00
if(!$parent || !$parent->exists() || !($parent instanceof Blog)) {
return false;
}
2015-05-09 16:33:12 +02:00
if($parent->isEditor($member)) {
return true;
}
2015-05-09 16:33:12 +02:00
if(!$parent->isWriter($member) && !$parent->isContributor($member)) {
return false;
}
2015-05-09 16:33:12 +02:00
return $this->isAuthor($member);
}
2013-08-04 18:38:26 +02:00
/**
* Returns the post excerpt.
*
2015-05-09 16:33:12 +02:00
* @param int $wordsToDisplay
2013-08-04 18:38:26 +02:00
*
* @return string
2015-05-09 16:33:12 +02:00
*/
public function Excerpt($wordsToDisplay = 30) {
/**
* @var Text $content
*/
$content = $this->dbObject('Content');
2013-08-04 18:38:26 +02:00
2015-05-14 02:11:09 +02:00
return $content->Summary($wordsToDisplay);
2015-05-09 16:33:12 +02:00
}
2013-08-04 18:38:26 +02:00
/**
* Returns a monthly archive link for the current blog post.
*
2015-05-09 16:33:12 +02:00
* @param string $type
*
2015-05-09 16:33:12 +02:00
* @return string
*/
public function getMonthlyArchiveLink($type = 'day') {
/**
* @var SS_Datetime $date
*/
$date = $this->dbObject('PublishDate');
if($type != 'year') {
if($type == 'day') {
return Controller::join_links(
2015-05-09 16:33:12 +02:00
$this->Parent()->Link('archive'),
$date->format('Y'),
$date->format('m'),
$date->format('d')
);
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'), $date->format('m'));
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
}
2013-08-04 18:38:26 +02:00
/**
* Returns a yearly archive link for the current blog post.
*
2015-05-09 16:33:12 +02:00
* @return string
*/
2013-08-04 18:38:26 +02:00
public function getYearlyArchiveLink() {
2015-05-09 16:33:12 +02:00
/**
* @var SS_Datetime $date
*/
$date = $this->dbObject('PublishDate');
return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
2013-08-04 18:38:26 +02:00
}
2015-04-01 03:52:46 +02:00
/**
* Resolves static and dynamic authors linked to this post.
*
* @return ArrayList
*/
2015-05-09 16:33:12 +02:00
public function getCredits() {
2015-04-01 03:52:46 +02:00
$list = new ArrayList();
$list->merge($this->getDynamicCredits());
$list->merge($this->getStaticCredits());
return $list->sort('Name');
}
/**
* Resolves dynamic authors linked to this post.
*
* @return ArrayList
*/
2015-05-09 16:33:12 +02:00
protected function getDynamicCredits() {
// Find best page to host user profiles
$parent = $this->Parent();
if(! ($parent instanceof Blog) ) {
$parent = Blog::get()->first();
}
// If there is no parent blog, return list undecorated
if(!$parent) {
$items = $this->Authors()->toArray();
return new ArrayList($items);
}
2015-04-01 03:52:46 +02:00
// Update all authors
$items = new ArrayList();
2015-04-01 03:52:46 +02:00
foreach($this->Authors() as $author) {
// Add link for each author
$author = $author->customise(array(
'URL' => $parent->ProfileLink($author->URLSegment),
));
$items->push($author);
2015-04-01 03:52:46 +02:00
}
2015-04-01 03:52:46 +02:00
return $items;
}
/**
* Resolves static authors linked to this post.
*
* @return ArrayList
*/
2015-05-09 16:33:12 +02:00
protected function getStaticCredits() {
2015-04-01 03:52:46 +02:00
$items = new ArrayList();
$authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames));
2015-05-09 16:33:12 +02:00
foreach($authors as $author) {
2015-04-01 03:52:46 +02:00
$item = new ArrayData(array(
'Name' => $author,
));
$items->push($item);
}
return $items;
}
/**
2015-05-09 16:33:12 +02:00
* Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name').
*
* @param bool $includeRelations
*
* @return array
2015-05-09 16:33:12 +02:00
*/
public function fieldLabels($includeRelations = true) {
$labels = parent::fieldLabels($includeRelations);
$labels['Title'] = _t('BlogPost.PageTitleLabel', "Post Title");
return $labels;
}
2015-05-09 16:33:12 +02:00
/**
* {@inheritdoc}
*/
protected function onBeforeWrite() {
parent::onBeforeWrite();
if(!$this->exists() && ($member = Member::currentUser())) {
$this->Authors()->add($member);
}
}
}
2013-07-21 12:23:35 +02:00
/**
* @package silverstripe
* @subpackage blog
2015-05-09 16:33:12 +02:00
*/
2013-07-21 12:23:35 +02:00
class BlogPost_Controller extends Page_Controller {
2015-05-09 16:33:12 +02:00
2013-08-04 18:38:26 +02:00
}