2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Model;
|
|
|
|
|
|
|
|
use Page;
|
2018-01-29 04:13:19 +01:00
|
|
|
use SilverStripe\AssetAdmin\Forms\UploadField;
|
2017-01-26 09:28:42 +01:00
|
|
|
use SilverStripe\Assets\Image;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-11-02 20:32:03 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Forms\DatetimeField;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
|
|
|
use SilverStripe\Forms\ListboxField;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\ToggleCompositeField;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2019-02-07 10:05:32 +01:00
|
|
|
use SilverStripe\ORM\ManyManyList;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\ORM\UnsavedRelationList;
|
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2017-09-13 23:53:29 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\TagField\TagField;
|
2017-11-03 03:39:57 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* An individual blog post.
|
|
|
|
*
|
|
|
|
* @method ManyManyList Categories()
|
|
|
|
* @method ManyManyList Tags()
|
|
|
|
* @method ManyManyList Authors()
|
|
|
|
* @method Blog Parent()
|
|
|
|
*
|
|
|
|
* @property string $PublishDate
|
|
|
|
* @property string $AuthorNames
|
|
|
|
* @property int $ParentID
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $table_name = 'BlogPost';
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $db = [
|
2016-12-15 04:41:49 +01:00
|
|
|
'PublishDate' => 'Datetime',
|
2015-11-21 07:17:29 +01:00
|
|
|
'AuthorNames' => 'Varchar(1024)',
|
2016-12-19 01:56:53 +01:00
|
|
|
'Summary' => 'HTMLText'
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $has_one = [
|
2017-01-26 09:28:42 +01:00
|
|
|
'FeaturedImage' => Image::class
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2017-06-20 22:07:30 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $owns = [
|
2017-06-20 22:07:30 +02:00
|
|
|
'FeaturedImage',
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2017-06-20 22:07:30 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $many_many = [
|
2017-01-26 09:28:42 +01:00
|
|
|
'Categories' => BlogCategory::class,
|
|
|
|
'Tags' => BlogTag::class,
|
2017-01-13 03:33:53 +01:00
|
|
|
'Authors' => Member::class
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $defaults = [
|
2016-12-15 04:41:49 +01:00
|
|
|
'ShowInMenus' => false,
|
|
|
|
'InheritSideBar' => true,
|
2016-12-19 01:56:53 +01:00
|
|
|
'ProvideComments' => true
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $extensions = [
|
2017-01-26 09:28:42 +01:00
|
|
|
BlogPostFilter::class
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $searchable_fields = [
|
2016-12-15 04:41:49 +01:00
|
|
|
'Title'
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $summary_fields = [
|
2016-12-15 04:41:49 +01:00
|
|
|
'Title'
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $casting = [
|
2016-09-08 12:00:15 +02:00
|
|
|
'Excerpt' => 'HTMLText',
|
2016-12-19 01:56:53 +01:00
|
|
|
'Date' => 'DBDatetime'
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $allowed_children = [];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default sorting lists BlogPosts with an empty PublishDate at the top.
|
2016-01-21 17:46:10 +01:00
|
|
|
*
|
2015-11-21 07:17:29 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $can_be_root = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will display or hide the current class from the SiteTree. This variable can be
|
|
|
|
* configured using YAML.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $show_in_sitetree = false;
|
|
|
|
|
2018-03-14 09:01:09 +01:00
|
|
|
/**
|
|
|
|
* This helps estimate how long an article will take to read, if your target audience
|
|
|
|
* is elderly then you should lower this value. See {@link getMinutesToRead()}
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $minutes_to_read_wpm = 200;
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* Determine the role of the given member.
|
|
|
|
*
|
|
|
|
* Call be called via template to determine the current user.
|
|
|
|
*
|
|
|
|
* @example "Hello $RoleOf($CurrentMember.ID)"
|
|
|
|
*
|
|
|
|
* @param null|int|Member $member
|
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function RoleOf($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
if (!$member) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isAuthor($member)) {
|
2017-09-14 00:27:40 +02:00
|
|
|
return _t(__CLASS__ . '.AUTHOR', 'Author');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$parent = $this->Parent();
|
|
|
|
|
|
|
|
if ($parent instanceof Blog) {
|
|
|
|
return $parent->RoleOf($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the given member is an author of this post.
|
|
|
|
*
|
|
|
|
* @param null|Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isAuthor($member = null)
|
|
|
|
{
|
|
|
|
if (!$member || !$member->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = $this->Authors();
|
|
|
|
|
|
|
|
if ($list instanceof UnsavedRelationList) {
|
|
|
|
return in_array($member->ID, $list->getIDList());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list->byID($member->ID) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2018-01-28 23:50:39 +01:00
|
|
|
Requirements::css('silverstripe/blog:client/dist/styles/main.css');
|
|
|
|
Requirements::javascript('silverstripe/blog:client/dist/js/main.bundle.js');
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2016-06-02 07:11:56 +02:00
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
2017-09-14 00:27:40 +02:00
|
|
|
$uploadField = UploadField::create('FeaturedImage', _t(__CLASS__ . '.FeaturedImage', 'Featured Image'));
|
2017-09-14 00:59:01 +02:00
|
|
|
$uploadField->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FieldList $fields
|
|
|
|
*/
|
2018-04-06 00:21:43 +02:00
|
|
|
$fields->insertAfter('Content', $uploadField);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$summary = HtmlEditorField::create('Summary', false);
|
|
|
|
$summary->setRows(5);
|
|
|
|
$summary->setDescription(_t(
|
2017-09-14 00:27:40 +02:00
|
|
|
__CLASS__ . '.SUMMARY_DESCRIPTION',
|
2015-11-21 07:17:29 +01:00
|
|
|
'If no summary is specified the first 30 words will be used.'
|
|
|
|
));
|
|
|
|
|
|
|
|
$summaryHolder = ToggleCompositeField::create(
|
|
|
|
'CustomSummary',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.CUSTOMSUMMARY', 'Add A Custom Summary'),
|
2017-09-14 00:59:01 +02:00
|
|
|
[
|
2015-11-21 07:17:29 +01:00
|
|
|
$summary,
|
2017-09-14 00:59:01 +02:00
|
|
|
]
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
$summaryHolder->setHeadingLevel(4);
|
|
|
|
$summaryHolder->addExtraClass('custom-summary');
|
|
|
|
|
2018-04-06 00:21:43 +02:00
|
|
|
$fields->insertAfter('FeaturedImage', $summaryHolder);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$authorField = ListboxField::create(
|
|
|
|
'Authors',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Authors', 'Authors'),
|
2016-06-02 07:11:56 +02:00
|
|
|
$this->getCandidateAuthors()->map()->toArray()
|
2016-06-01 07:28:59 +02:00
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$authorNames = TextField::create(
|
|
|
|
'AuthorNames',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.AdditionalCredits', 'Additional Credits'),
|
2015-11-21 07:17:29 +01:00
|
|
|
null,
|
|
|
|
1024
|
2016-12-18 23:50:42 +01:00
|
|
|
)->setDescription(
|
|
|
|
_t(
|
2017-09-14 00:27:40 +02:00
|
|
|
__CLASS__ . '.AdditionalCredits_Description',
|
2018-01-29 04:10:45 +01:00
|
|
|
'If some authors of this post don\'t have CMS access, enter their name(s) here. '.
|
|
|
|
'You can separate multiple names with a comma.'
|
2016-12-18 23:50:42 +01:00
|
|
|
)
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
|
2016-06-02 07:11:56 +02:00
|
|
|
if (!$this->canEditAuthors()) {
|
2015-11-21 07:17:29 +01:00
|
|
|
$authorField = $authorField->performDisabledTransformation();
|
|
|
|
$authorNames = $authorNames->performDisabledTransformation();
|
|
|
|
}
|
|
|
|
|
2017-09-14 00:27:40 +02:00
|
|
|
$publishDate = DatetimeField::create('PublishDate', _t(__CLASS__ . '.PublishDate', 'Publish Date'));
|
2017-04-18 09:02:27 +02:00
|
|
|
|
2016-06-02 07:11:56 +02:00
|
|
|
if (!$this->PublishDate) {
|
2016-12-15 23:57:25 +01:00
|
|
|
$publishDate->setDescription(
|
|
|
|
_t(
|
2017-09-14 00:27:40 +02:00
|
|
|
__CLASS__ . '.PublishDate_Description',
|
2016-12-15 23:57:25 +01:00
|
|
|
'Will be set to "now" if published without a value.'
|
|
|
|
)
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get categories and tags
|
2016-06-02 07:11:56 +02:00
|
|
|
$parent = $this->Parent();
|
2015-11-21 07:17:29 +01:00
|
|
|
$categories = $parent instanceof Blog
|
|
|
|
? $parent->Categories()
|
|
|
|
: BlogCategory::get();
|
|
|
|
$tags = $parent instanceof Blog
|
|
|
|
? $parent->Tags()
|
|
|
|
: BlogTag::get();
|
|
|
|
|
2017-01-13 03:55:48 +01:00
|
|
|
// @todo: Reimplement the sidebar
|
|
|
|
// $options = BlogAdminSidebar::create(
|
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.PostOptions',
|
|
|
|
[
|
|
|
|
$publishDate,
|
|
|
|
TagField::create(
|
|
|
|
'Categories',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Categories', 'Categories'),
|
2017-01-13 03:55:48 +01:00
|
|
|
$categories,
|
|
|
|
$this->Categories()
|
|
|
|
)
|
|
|
|
->setCanCreate($this->canCreateCategories())
|
|
|
|
->setShouldLazyLoad(true),
|
|
|
|
TagField::create(
|
|
|
|
'Tags',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Tags', 'Tags'),
|
2017-01-13 03:55:48 +01:00
|
|
|
$tags,
|
|
|
|
$this->Tags()
|
|
|
|
)
|
|
|
|
->setCanCreate($this->canCreateTags())
|
|
|
|
->setShouldLazyLoad(true),
|
|
|
|
$authorField,
|
|
|
|
$authorNames
|
|
|
|
]
|
|
|
|
);
|
|
|
|
// )->setTitle('Post Options');
|
|
|
|
// $options->setName('blog-admin-sidebar');
|
|
|
|
// $fields->insertBefore($options, 'Root');
|
2018-07-09 11:58:10 +02:00
|
|
|
|
|
|
|
$fields->fieldByName('Root.PostOptions')
|
|
|
|
->setTitle(_t(__CLASS__ . '.PostOptions', 'Post Options'));
|
2015-11-21 07:17:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
|
|
|
$fields->fieldByName('Root')->setTemplate('TabSet_holder');
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of author candidates to be assigned as authors of this blog post.
|
|
|
|
*
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
|
|
|
public function getCandidateAuthors()
|
|
|
|
{
|
2017-09-14 01:36:33 +02:00
|
|
|
if ($this->config()->get('restrict_authors_to_group')) {
|
|
|
|
return Group::get()->filter('Code', $this->config()->get('restrict_authors_to_group'))->first()->Members();
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
2016-12-15 23:57:25 +01:00
|
|
|
|
|
|
|
$list = Member::get();
|
|
|
|
$this->extend('updateCandidateAuthors', $list);
|
|
|
|
return $list;
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this user can edit the authors list.
|
|
|
|
*
|
|
|
|
* @param null|int|Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canEditAuthors($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
$extended = $this->extendedCan('canEditAuthors', $member);
|
|
|
|
|
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parent = $this->Parent();
|
|
|
|
|
|
|
|
if ($parent instanceof Blog && $parent->exists()) {
|
|
|
|
if ($parent->isEditor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent->isWriter($member) && $this->isAuthor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Permission::checkMember($member, Blog::MANAGE_USERS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|int|Member $member
|
|
|
|
*
|
|
|
|
* @return null|Member
|
|
|
|
*/
|
|
|
|
protected function getMember($member = null)
|
|
|
|
{
|
|
|
|
if (!$member) {
|
2017-09-13 23:53:29 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($member)) {
|
|
|
|
$member = Member::get()->byID($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $member;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether user can create new categories.
|
|
|
|
*
|
|
|
|
* @param null|int|Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canCreateCategories($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($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 null|int|Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canCreateTags($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* Update the PublishDate to now if the BlogPost would otherwise be published without a date.
|
|
|
|
*/
|
|
|
|
public function onBeforePublish()
|
|
|
|
{
|
|
|
|
/**
|
2016-12-15 04:41:49 +01:00
|
|
|
* @var DBDatetime $publishDate
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
|
|
|
$publishDate = $this->dbObject('PublishDate');
|
|
|
|
|
|
|
|
if (!$publishDate->getValue()) {
|
2016-12-15 04:41:49 +01:00
|
|
|
$this->PublishDate = DBDatetime::now()->getValue();
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* Sets blog relationship on all categories and tags assigned to this post.
|
|
|
|
*/
|
|
|
|
public function onAfterWrite()
|
|
|
|
{
|
|
|
|
parent::onAfterWrite();
|
|
|
|
|
|
|
|
foreach ($this->Categories() as $category) {
|
|
|
|
/**
|
|
|
|
* @var BlogCategory $category
|
|
|
|
*/
|
|
|
|
$category->BlogID = $this->ParentID;
|
|
|
|
$category->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->Tags() as $tag) {
|
|
|
|
/**
|
|
|
|
* @var BlogTag $tag
|
|
|
|
*/
|
|
|
|
$tag->BlogID = $this->ParentID;
|
|
|
|
$tag->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function canView($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
if (!parent::canView($member)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:57:25 +01:00
|
|
|
if ($this->canEdit($member)) {
|
2016-02-05 03:28:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-02 23:42:23 +01:00
|
|
|
// If on draft stage, user has permission to view draft, so show it
|
2017-11-03 03:39:57 +01:00
|
|
|
if (Versioned::get_stage() === Versioned::DRAFT) {
|
2016-02-05 03:28:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
2016-12-15 04:41:49 +01:00
|
|
|
* @var DBDatetime $publishDate
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
|
|
|
$publishDate = $this->dbObject('PublishDate');
|
2016-12-15 23:57:25 +01:00
|
|
|
if (!$publishDate->exists()) {
|
2016-02-05 03:28:09 +01:00
|
|
|
return false;
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
2016-02-05 03:28:09 +01:00
|
|
|
return !$publishDate->InFuture();
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function canPublish($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
if (Permission::checkMember($member, 'ADMIN')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$extended = $this->extendedCan('canPublish', $member);
|
|
|
|
|
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parent = $this->Parent();
|
|
|
|
|
|
|
|
if ($parent instanceof Blog && $parent->exists()) {
|
|
|
|
if ($parent->isEditor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent->isWriter($member) && $this->isAuthor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent->isContributor($member)) {
|
|
|
|
return parent::canEdit($member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
if (parent::canEdit($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parent = $this->Parent();
|
|
|
|
|
|
|
|
if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent->isEditor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$parent->isWriter($member) && !$parent->isContributor($member)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isAuthor($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the post excerpt.
|
|
|
|
*
|
|
|
|
* @param int $wordsToDisplay
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Excerpt($wordsToDisplay = 30)
|
|
|
|
{
|
2018-04-06 00:21:26 +02:00
|
|
|
/** @var DBHTMLText $content */
|
2015-11-21 07:17:29 +01:00
|
|
|
$content = $this->dbObject('Content');
|
|
|
|
|
|
|
|
return $content->Summary($wordsToDisplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a monthly archive link for the current blog post.
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMonthlyArchiveLink($type = 'day')
|
|
|
|
{
|
|
|
|
/**
|
2016-12-15 04:41:49 +01:00
|
|
|
* @var DBDatetime $date
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
|
|
|
$date = $this->dbObject('PublishDate');
|
|
|
|
|
|
|
|
if ($type != 'year') {
|
|
|
|
if ($type == 'day') {
|
|
|
|
return Controller::join_links(
|
|
|
|
$this->Parent()->Link('archive'),
|
|
|
|
$date->format('Y'),
|
2018-03-20 01:54:08 +01:00
|
|
|
$date->format('M'),
|
2015-11-21 07:17:29 +01:00
|
|
|
$date->format('d')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-20 01:54:08 +01:00
|
|
|
return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'), $date->format('M'));
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a yearly archive link for the current blog post.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getYearlyArchiveLink()
|
|
|
|
{
|
|
|
|
/**
|
2016-12-15 04:41:49 +01:00
|
|
|
* @var DBDatetime $date
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
|
|
|
$date = $this->dbObject('PublishDate');
|
|
|
|
|
|
|
|
return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves static and dynamic authors linked to this post.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getCredits()
|
|
|
|
{
|
2016-12-19 00:12:08 +01:00
|
|
|
$list = ArrayList::create();
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$list->merge($this->getDynamicCredits());
|
|
|
|
$list->merge($this->getStaticCredits());
|
|
|
|
|
|
|
|
return $list->sort('Name');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves dynamic authors linked to this post.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
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();
|
2017-09-14 01:49:27 +02:00
|
|
|
return ArrayList::create($items);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update all authors
|
2017-09-14 01:49:27 +02:00
|
|
|
$items = ArrayList::create();
|
2015-11-21 07:17:29 +01:00
|
|
|
foreach ($this->Authors() as $author) {
|
|
|
|
// Add link for each author
|
2017-09-14 00:59:01 +02:00
|
|
|
$author = $author->customise([
|
2015-11-21 07:17:29 +01:00
|
|
|
'URL' => $parent->ProfileLink($author->URLSegment),
|
2017-09-14 00:59:01 +02:00
|
|
|
]);
|
2015-11-21 07:17:29 +01:00
|
|
|
$items->push($author);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves static authors linked to this post.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
protected function getStaticCredits()
|
|
|
|
{
|
2017-09-14 01:49:27 +02:00
|
|
|
$items = ArrayList::create();
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames));
|
|
|
|
|
|
|
|
foreach ($authors as $author) {
|
2017-09-14 01:49:27 +02:00
|
|
|
$item = ArrayData::create([
|
2015-11-21 07:17:29 +01:00
|
|
|
'Name' => $author,
|
2017-09-14 00:59:01 +02:00
|
|
|
]);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$items->push($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:32:03 +01:00
|
|
|
/**
|
|
|
|
* Checks to see if User Profiles has been disabled via config
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getProfilesDisabled()
|
|
|
|
{
|
|
|
|
return Config::inst()->get(BlogController::class, 'disable_profiles');
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name').
|
|
|
|
*
|
|
|
|
* @param bool $includeRelations
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fieldLabels($includeRelations = true)
|
|
|
|
{
|
|
|
|
$labels = parent::fieldLabels($includeRelations);
|
|
|
|
|
2017-09-14 00:27:40 +02:00
|
|
|
$labels['Title'] = _t(__CLASS__ . '.PageTitleLabel', 'Post Title');
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
2016-05-31 04:23:45 +02:00
|
|
|
/**
|
|
|
|
* Proxy method for displaying the publish date in rss feeds.
|
|
|
|
* @see https://github.com/silverstripe/silverstripe-blog/issues/394
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getDate()
|
|
|
|
{
|
2017-07-26 16:22:36 +02:00
|
|
|
if ($this->hasDatabaseField('Date')) {
|
|
|
|
return $this->getField('Date');
|
|
|
|
}
|
2016-05-31 04:23:45 +02:00
|
|
|
return !empty($this->PublishDate) ? $this->PublishDate : null;
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:01:09 +01:00
|
|
|
/**
|
|
|
|
* Provides a rough estimate of how long this post will take to read based on wikipedias answer to "How fast can a
|
|
|
|
* human read" of 200wpm. Source https://en.wikipedia.org/wiki/Speed_reading
|
|
|
|
*
|
|
|
|
* @param null|integer $wpm
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function MinutesToRead($wpm = null)
|
|
|
|
{
|
|
|
|
$wpm = $wpm ?: $this->config()->get('minutes_to_read_wpm');
|
|
|
|
|
|
|
|
if (!is_numeric($wpm)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf("Expecting integer but got %s instead", gettype($wpm)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$wordCount = str_word_count(strip_tags($this->Content));
|
|
|
|
|
|
|
|
if ($wordCount < $wpm) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return round($wordCount / $wpm, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function onBeforeWrite()
|
|
|
|
{
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
2017-09-13 23:53:29 +02:00
|
|
|
if (!$this->exists() && ($member = Security::getCurrentUser())) {
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->Authors()->add($member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|