2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Model;
|
|
|
|
|
|
|
|
use Page;
|
|
|
|
use SilverStripe\Blog\Admin\GridFieldCategorisationConfig;
|
2018-01-29 03:57:17 +01:00
|
|
|
use SilverStripe\Blog\Forms\GridField\GridFieldConfigBlogPost;
|
2016-12-19 00:12:08 +01:00
|
|
|
use SilverStripe\CMS\Controllers\RootURLController;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Core\Convert;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Forms\ListboxField;
|
2017-09-22 02:20:41 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Forms\NumericField;
|
2018-04-06 00:21:26 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
2019-02-07 10:05:32 +01:00
|
|
|
use SilverStripe\ORM\HasManyList;
|
|
|
|
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;
|
|
|
|
use SilverStripe\Security\PermissionProvider;
|
2017-09-13 23:53:29 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\View\Requirements;
|
2016-06-01 07:28:59 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* Blog Holder
|
|
|
|
*
|
|
|
|
* @method HasManyList Tags() List of tags in this blog
|
|
|
|
* @method HasManyList Categories() List of categories in this blog
|
|
|
|
* @method ManyManyList Editors() List of editors
|
|
|
|
* @method ManyManyList Writers() List of writers
|
|
|
|
* @method ManyManyList Contributors() List of contributors
|
|
|
|
*/
|
|
|
|
class Blog extends Page implements PermissionProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Permission for user management.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const MANAGE_USERS = 'BLOG_MANAGE_USERS';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, users assigned as editor, writer, or contributor will be automatically granted
|
|
|
|
* CMS_ACCESS_CMSMain permission. If false, only users with this permission already may be
|
|
|
|
* assigned.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $grant_user_access = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permission to either require, or grant to users assigned to work on this blog.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $grant_user_permission = 'CMS_ACCESS_CMSMain';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Group code to assign newly granted users to.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $grant_user_group = 'blog-users';
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $table_name = 'Blog';
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $db = [
|
2015-11-21 07:17:29 +01:00
|
|
|
'PostsPerPage' => 'Int',
|
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_many = [
|
2017-01-26 09:28:42 +01:00
|
|
|
'Tags' => BlogTag::class,
|
|
|
|
'Categories' => BlogCategory::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 $many_many = [
|
2017-01-26 09:28:42 +01:00
|
|
|
'Editors' => Member::class,
|
|
|
|
'Writers' => Member::class,
|
|
|
|
'Contributors' => 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 $allowed_children = [
|
2017-01-26 09:28:42 +01:00
|
|
|
BlogPost::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 $extensions = [
|
2017-01-26 09:28:42 +01:00
|
|
|
BlogFilter::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 = [
|
2015-11-21 07:17:29 +01:00
|
|
|
'ProvideComments' => false,
|
2016-12-15 04:41:49 +01:00
|
|
|
'PostsPerPage' => 10
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $description = 'Adds a blog to your website.';
|
|
|
|
|
2019-01-14 01:25:52 +01:00
|
|
|
private static $icon_class = 'font-icon-p-posts';
|
2016-01-19 02:59:34 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-09-22 02:20:41 +02:00
|
|
|
$this->addCMSRequirements();
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2016-12-19 01:56:53 +01:00
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
|
|
|
if (!$this->canEdit()) {
|
2015-11-21 07:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$categories = GridField::create(
|
|
|
|
'Categories',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Categories', 'Categories'),
|
2016-12-19 01:56:53 +01:00
|
|
|
$this->Categories(),
|
2016-12-15 23:57:25 +01:00
|
|
|
GridFieldCategorisationConfig::create(
|
|
|
|
15,
|
2016-12-19 01:56:53 +01:00
|
|
|
$this->Categories()->sort('Title'),
|
2016-12-15 23:57:25 +01:00
|
|
|
BlogCategory::class,
|
|
|
|
'Categories',
|
|
|
|
'BlogPosts'
|
|
|
|
)
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$tags = GridField::create(
|
|
|
|
'Tags',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Tags', 'Tags'),
|
2016-12-19 01:56:53 +01:00
|
|
|
$this->Tags(),
|
2016-12-15 23:57:25 +01:00
|
|
|
GridFieldCategorisationConfig::create(
|
|
|
|
15,
|
2016-12-19 01:56:53 +01:00
|
|
|
$this->Tags()->sort('Title'),
|
2016-12-15 23:57:25 +01:00
|
|
|
BlogTag::class,
|
|
|
|
'Tags',
|
|
|
|
'BlogPosts'
|
|
|
|
)
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FieldList $fields
|
|
|
|
*/
|
2016-12-15 23:57:25 +01:00
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.Categorisation',
|
2017-09-14 00:59:01 +02:00
|
|
|
[
|
2016-12-15 23:57:25 +01:00
|
|
|
$categories,
|
|
|
|
$tags
|
2017-09-14 00:59:01 +02:00
|
|
|
]
|
2016-12-15 23:57:25 +01:00
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2018-01-16 18:17:41 +01:00
|
|
|
$fields->fieldByName('Root.Categorisation')
|
|
|
|
->addExtraClass('blog-cms-categorisation')
|
|
|
|
->setTitle(_t(__CLASS__ . '.Categorisation', 'Categorisation'));
|
2015-11-21 07:17:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
2017-09-22 02:20:41 +02:00
|
|
|
/**
|
|
|
|
* Adds CMS related css and js overrides
|
|
|
|
*/
|
|
|
|
protected function addCMSRequirements()
|
|
|
|
{
|
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');
|
2017-09-22 02:20:41 +02:00
|
|
|
}
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
if ($this->isEditor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this member is an editor of the blog.
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isEditor($member)
|
|
|
|
{
|
|
|
|
$isEditor = $this->isMemberOf($member, $this->Editors());
|
|
|
|
$this->extend('updateIsEditor', $isEditor, $member);
|
|
|
|
|
|
|
|
return $isEditor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the given member belongs to the given relation.
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @param DataList $relation
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isMemberOf($member, $relation)
|
|
|
|
{
|
|
|
|
if (!$member || !$member->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($relation instanceof UnsavedRelationList) {
|
|
|
|
return in_array($member->ID, $relation->getIDList());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $relation->byID($member->ID) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the role of the given member.
|
|
|
|
*
|
|
|
|
* Call be called via template to determine the current user.
|
|
|
|
*
|
|
|
|
* @example "Hello $RoleOf($CurrentMember.ID)"
|
|
|
|
*
|
|
|
|
* @param int|Member $member
|
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function RoleOf($member)
|
|
|
|
{
|
|
|
|
if (is_numeric($member)) {
|
2017-01-13 03:33:53 +01:00
|
|
|
$member = Member::get()->byId($member);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$member) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isEditor($member)) {
|
2017-09-14 00:27:40 +02:00
|
|
|
return _t(__CLASS__ . '.EDITOR', 'Editor');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isWriter($member)) {
|
2017-09-14 00:27:40 +02:00
|
|
|
return _t(__CLASS__ . '.WRITER', 'Writer');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isContributor($member)) {
|
2017-09-14 00:27:40 +02:00
|
|
|
return _t(__CLASS__ . '.CONTRIBUTOR', 'Contributor');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this member is a writer of the blog.
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isWriter($member)
|
|
|
|
{
|
|
|
|
$isWriter = $this->isMemberOf($member, $this->Writers());
|
|
|
|
$this->extend('updateIsWriter', $isWriter, $member);
|
|
|
|
|
|
|
|
return $isWriter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this member is a contributor of the blog.
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isContributor($member)
|
|
|
|
{
|
|
|
|
$isContributor = $this->isMemberOf($member, $this->Contributors());
|
|
|
|
$this->extend('updateIsContributor', $isContributor, $member);
|
|
|
|
|
|
|
|
return $isContributor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function canAddChildren($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
if ($this->isEditor($member) || $this->isWriter($member) || $this->isContributor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::canAddChildren($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getSettingsFields()
|
|
|
|
{
|
2017-09-22 02:20:41 +02:00
|
|
|
$this->addCMSRequirements();
|
2015-11-21 07:17:29 +01:00
|
|
|
$fields = parent::getSettingsFields();
|
|
|
|
|
2016-12-15 23:57:25 +01:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Settings',
|
2017-09-14 00:27:40 +02:00
|
|
|
NumericField::create('PostsPerPage', _t(__CLASS__ . '.PostsPerPage', 'Posts Per Page'))
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$members = $this->getCandidateUsers()->map()->toArray();
|
2018-01-29 04:10:45 +01:00
|
|
|
$toggleButton = LiteralField::create(
|
|
|
|
'ToggleButton',
|
|
|
|
'<a class="font-icon-info-circled toggle-description"></a>'
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$editorField = ListboxField::create('Editors', 'Editors', $members)
|
2017-09-22 02:20:41 +02:00
|
|
|
->setRightTitle($toggleButton)
|
|
|
|
->setDescription(
|
|
|
|
_t(
|
|
|
|
__CLASS__ . '.UsersEditorsFieldDescription',
|
2019-01-14 01:25:52 +01:00
|
|
|
'An editor has control over specific Blogs, and all posts included within it.
|
2017-09-22 02:20:41 +02:00
|
|
|
Short of being able to assign other editors to a blog, they are able to handle most changes to
|
|
|
|
their assigned blog. <br /><br />
|
|
|
|
Editors have these permissions:<br />
|
|
|
|
<br />
|
|
|
|
Update or publish any BlogPost in their Blog<br />
|
|
|
|
Update or publish their Blog<br />
|
|
|
|
Assign/unassign writers to their Blog<br />
|
|
|
|
Assign/unassign contributors to their Blog<br />
|
|
|
|
Assign/unassign any member as an author of a particular BlogPost'
|
|
|
|
)
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
if (!$this->canEditEditors()) {
|
|
|
|
$editorField = $editorField->performDisabledTransformation();
|
|
|
|
}
|
|
|
|
$writerField = ListboxField::create('Writers', 'Writers', $members)
|
2017-09-22 02:20:41 +02:00
|
|
|
->setRightTitle($toggleButton)
|
|
|
|
->setDescription(
|
|
|
|
_t(
|
|
|
|
__CLASS__ . '.UsersWritersFieldDescription',
|
|
|
|
'A writer has full control over creating, editing and publishing BlogPosts they have authored
|
|
|
|
or have been assigned to. Writers are unable to edit BlogPosts to which they are not assigned.
|
|
|
|
<br /><br />
|
|
|
|
Writers have these permissions:<br />
|
|
|
|
<br />
|
|
|
|
Update or publish any BlogPost they have authored or have been assigned to<br />
|
2019-01-14 01:25:52 +01:00
|
|
|
Assign/unassign any member as an author of a particular BlogPost they have authored or have been
|
2017-09-22 02:20:41 +02:00
|
|
|
assigned to'
|
|
|
|
)
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
if (!$this->canEditWriters()) {
|
|
|
|
$writerField = $writerField->performDisabledTransformation();
|
|
|
|
}
|
|
|
|
|
|
|
|
$contributorField = ListboxField::create('Contributors', 'Contributors', $members)
|
2016-12-16 05:10:38 +01:00
|
|
|
// ->setMultiple(true)
|
2017-09-22 02:20:41 +02:00
|
|
|
->setRightTitle($toggleButton)
|
|
|
|
->setDescription(
|
|
|
|
_t(
|
|
|
|
__CLASS__ . '.UsersContributorsFieldDescription',
|
2019-01-14 01:25:52 +01:00
|
|
|
'Contributors have the ability to create or edit BlogPosts, but are unable to publish without
|
2017-09-22 02:20:41 +02:00
|
|
|
authorisation of an editor. They are also unable to assign other contributing authors to any of
|
|
|
|
their BlogPosts.<br />
|
|
|
|
<br />
|
|
|
|
Contributors have these permissions:<br />
|
|
|
|
<br />
|
|
|
|
Update any BlogPost they have authored or have been assigned to'
|
|
|
|
)
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
if (!$this->canEditContributors()) {
|
|
|
|
$contributorField = $contributorField->performDisabledTransformation();
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:57:25 +01:00
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.Users',
|
2017-09-14 00:59:01 +02:00
|
|
|
[
|
2016-12-15 23:57:25 +01:00
|
|
|
$editorField,
|
|
|
|
$writerField,
|
|
|
|
$contributorField
|
2017-09-14 00:59:01 +02:00
|
|
|
]
|
2016-12-15 23:57:25 +01:00
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of user candidates to be assigned to assist with this blog.
|
|
|
|
*
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
|
|
|
protected function getCandidateUsers()
|
|
|
|
{
|
2017-09-14 01:36:33 +02:00
|
|
|
if ($this->config()->get('grant_user_access')) {
|
2015-11-21 07:17:29 +01:00
|
|
|
$list = Member::get();
|
|
|
|
$this->extend('updateCandidateUsers', $list);
|
|
|
|
return $list;
|
|
|
|
}
|
2018-04-06 00:21:26 +02:00
|
|
|
|
|
|
|
return Permission::get_members_by_permission(
|
|
|
|
$this->config()->get('grant_user_permission')
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this user can edit the editors list.
|
|
|
|
*
|
|
|
|
* @param int|Member $member
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canEditEditors($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
$extended = $this->extendedCan('canEditEditors', $member);
|
|
|
|
|
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Permission::checkMember($member, self::MANAGE_USERS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this user can edit writers list.
|
|
|
|
*
|
|
|
|
* @param int|Member $member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEditWriters($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
$extended = $this->extendedCan('canEditWriters', $member);
|
|
|
|
|
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isEditor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Permission::checkMember($member, self::MANAGE_USERS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if this user can edit the contributors list.
|
|
|
|
*
|
|
|
|
* @param int|Member $member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEditContributors($member = null)
|
|
|
|
{
|
|
|
|
$member = $this->getMember($member);
|
|
|
|
|
|
|
|
$extended = $this->extendedCan('canEditContributors', $member);
|
|
|
|
|
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isEditor($member)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Permission::checkMember($member, self::MANAGE_USERS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns BlogPosts for a given date period.
|
|
|
|
*
|
|
|
|
* @param int $year
|
|
|
|
* @param null|int $month
|
|
|
|
* @param null|int $day
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function getArchivedBlogPosts($year, $month = null, $day = null)
|
|
|
|
{
|
|
|
|
$query = $this->getBlogPosts()->dataQuery();
|
|
|
|
|
|
|
|
$stage = $query->getQueryParam('Versioned.stage');
|
|
|
|
|
|
|
|
if ($stage) {
|
|
|
|
$stage = '_' . $stage;
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:57:25 +01:00
|
|
|
$query->innerJoin(
|
|
|
|
DataObject::getSchema()->tableName(BlogPost::class),
|
|
|
|
sprintf('"SiteTree%s"."ID" = "BlogPost%s"."ID"', $stage, $stage)
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2017-09-14 01:37:51 +02:00
|
|
|
$conn = DB::get_conn();
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2016-01-24 23:57:26 +01:00
|
|
|
// Filter by year
|
|
|
|
$yearCond = $conn->formattedDatetimeClause('"BlogPost"."PublishDate"', '%Y');
|
|
|
|
$query->where(sprintf('%s = \'%04d\'', $yearCond, Convert::raw2sql($year)));
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2016-01-24 23:57:26 +01:00
|
|
|
// Filter by month (if given)
|
|
|
|
if ($month) {
|
|
|
|
$monthCond = $conn->formattedDatetimeClause('"BlogPost"."PublishDate"', '%m');
|
|
|
|
$query->where(sprintf('%s = \'%02d\'', $monthCond, Convert::raw2sql($month)));
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2016-01-24 23:57:26 +01:00
|
|
|
if ($day) {
|
|
|
|
$dayCond = $conn->formattedDatetimeClause('"BlogPost"."PublishDate"', '%d');
|
|
|
|
$query->where(sprintf('%s = \'%02d\'', $dayCond, Convert::raw2sql($day)));
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:46:10 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
return $this->getBlogPosts()->setDataQuery($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return blog posts.
|
|
|
|
*
|
|
|
|
* @return DataList of BlogPost objects
|
|
|
|
*/
|
|
|
|
public function getBlogPosts()
|
|
|
|
{
|
|
|
|
$blogPosts = BlogPost::get()->filter('ParentID', $this->ID);
|
|
|
|
|
|
|
|
$this->extend('updateGetBlogPosts', $blogPosts);
|
|
|
|
|
|
|
|
return $blogPosts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a link to a Member profile.
|
|
|
|
*
|
|
|
|
* @param string $urlSegment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function ProfileLink($urlSegment)
|
|
|
|
{
|
2016-12-19 00:12:08 +01:00
|
|
|
$baseLink = $this->Link();
|
|
|
|
if ($baseLink === '/') {
|
|
|
|
// Handle homepage blogs
|
|
|
|
$baseLink = RootURLController::get_homepage_link();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Controller::join_links($baseLink, 'profile', $urlSegment);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This sets the title for our gridfield.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLumberjackTitle()
|
|
|
|
{
|
2017-09-14 00:27:40 +02:00
|
|
|
return _t(__CLASS__ . '.LumberjackTitle', 'Blog Posts');
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This overwrites lumberjacks default gridfield config.
|
|
|
|
*
|
|
|
|
* @return GridFieldConfig
|
|
|
|
*/
|
|
|
|
public function getLumberjackGridFieldConfig()
|
|
|
|
{
|
2018-01-29 03:57:17 +01:00
|
|
|
return GridFieldConfigBlogPost::create();
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function providePermissions()
|
|
|
|
{
|
2017-09-14 00:59:01 +02:00
|
|
|
return [
|
|
|
|
Blog::MANAGE_USERS => [
|
2015-11-21 07:17:29 +01:00
|
|
|
'name' => _t(
|
2017-09-14 00:27:40 +02:00
|
|
|
__CLASS__ . '.PERMISSION_MANAGE_USERS_DESCRIPTION',
|
2015-11-21 07:17:29 +01:00
|
|
|
'Manage users for individual blogs'
|
|
|
|
),
|
|
|
|
'help' => _t(
|
2017-09-14 00:27:40 +02:00
|
|
|
__CLASS__ . '.PERMISSION_MANAGE_USERS_HELP',
|
2015-11-21 07:17:29 +01:00
|
|
|
'Allow assignment of Editors, Writers, or Contributors to blogs'
|
|
|
|
),
|
2017-09-14 00:27:40 +02:00
|
|
|
'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'Blog permissions'),
|
2015-11-21 07:17:29 +01:00
|
|
|
'sort' => 100
|
2017-09-14 00:59:01 +02:00
|
|
|
]
|
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function onBeforeWrite()
|
|
|
|
{
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
$this->assignGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign users as necessary to the blog group.
|
|
|
|
*/
|
|
|
|
protected function assignGroup()
|
|
|
|
{
|
2017-09-14 01:36:33 +02:00
|
|
|
if (!$this->config()->get('grant_user_access')) {
|
2015-11-21 07:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$group = $this->getUserGroup();
|
|
|
|
|
|
|
|
// Must check if the method exists or else an error occurs when changing page type
|
|
|
|
if ($this->hasMethod('Editors')) {
|
2017-09-14 00:59:01 +02:00
|
|
|
foreach ([$this->Editors(), $this->Writers(), $this->Contributors()] as $levels) {
|
2015-11-21 07:17:29 +01:00
|
|
|
foreach ($levels as $user) {
|
|
|
|
if (!$user->inGroup($group)) {
|
|
|
|
$user->Groups()->add($group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets or creates the group used to assign CMS access.
|
|
|
|
*
|
|
|
|
* @return Group
|
|
|
|
*/
|
|
|
|
protected function getUserGroup()
|
|
|
|
{
|
2017-09-14 01:36:33 +02:00
|
|
|
$code = $this->config()->get('grant_user_group');
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$group = Group::get()->filter('Code', $code)->first();
|
|
|
|
|
|
|
|
if ($group) {
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:57:25 +01:00
|
|
|
$group = Group::create();
|
2015-11-21 07:17:29 +01:00
|
|
|
$group->Title = 'Blog users';
|
|
|
|
$group->Code = $code;
|
|
|
|
|
|
|
|
$group->write();
|
|
|
|
|
2016-12-15 23:57:25 +01:00
|
|
|
$permission = Permission::create();
|
2017-09-14 01:36:33 +02:00
|
|
|
$permission->Code = $this->config()->get('grant_user_permission');
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$group->Permissions()->add($permission);
|
|
|
|
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
}
|