2010-11-30 01:33:19 +01:00
|
|
|
<?php
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
namespace SilverStripe\Comments\Extensions;
|
|
|
|
|
2017-12-18 21:14:47 +01:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-01-16 20:57:37 +01:00
|
|
|
use SilverStripe\Comments\Admin\CommentsGridField;
|
|
|
|
use SilverStripe\Comments\Admin\CommentsGridFieldConfig;
|
|
|
|
use SilverStripe\Comments\Controllers\CommentingController;
|
|
|
|
use SilverStripe\Comments\Model\Comment;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldGroup;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Tab;
|
|
|
|
use SilverStripe\Forms\TabSet;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
2018-09-24 18:09:12 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2017-01-16 20:57:37 +01:00
|
|
|
use SilverStripe\ORM\PaginatedList;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2018-09-24 18:09:12 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2017-01-16 20:57:37 +01:00
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
|
2010-11-30 01:33:19 +01:00
|
|
|
/**
|
|
|
|
* Extension to {@link DataObject} to enable tracking comments.
|
|
|
|
*
|
|
|
|
* @package comments
|
|
|
|
*/
|
2016-02-19 01:48:25 +01:00
|
|
|
class CommentsExtension extends DataExtension
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Default configuration values
|
|
|
|
*
|
|
|
|
* enabled: Allows commenting to be disabled even if the extension is present
|
|
|
|
* enabled_cms: Allows commenting to be enabled or disabled via the CMS
|
|
|
|
* require_login: Boolean, whether a user needs to login (required for required_permission)
|
|
|
|
* require_login_cms: Allows require_login to be set via the CMS
|
|
|
|
* required_permission: Permission (or array of permissions) required to comment
|
|
|
|
* include_js: Enhance operation by ajax behaviour on moderation links (required for use_preview)
|
|
|
|
* use_gravatar: Set to true to show gravatar icons
|
|
|
|
* gravatar_default: Theme for 'not found' gravatar {@see http://gravatar.com/site/implement/images}
|
|
|
|
* gravatar_rating: Gravatar rating (same as the standard default)
|
|
|
|
* show_comments_when_disabled: Show older comments when commenting has been disabled.
|
|
|
|
* order_comments_by: Default sort order.
|
|
|
|
* order_replies_by: Sort order for replies.
|
|
|
|
* comments_holder_id: ID for the comments holder
|
|
|
|
* comment_permalink_prefix: ID prefix for each comment
|
|
|
|
* require_moderation: Require moderation for all comments
|
|
|
|
* require_moderation_cms: Ignore other comment moderation config settings and set via CMS
|
|
|
|
* frontend_moderation: Display unmoderated comments in the frontend, if the user can moderate them.
|
|
|
|
* frontend_spam: Display spam comments in the frontend, if the user can moderate them.
|
|
|
|
* html_allowed: Allow for sanitized HTML in comments
|
|
|
|
* use_preview: Preview formatted comment (when allowing HTML)
|
|
|
|
* nested_comments: Enable nested comments
|
|
|
|
* nested_depth: Max depth of nested comments in levels (where root is 1 depth) 0 means no limit.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*/
|
2017-09-18 04:16:24 +02:00
|
|
|
private static $comments = [
|
2016-02-19 01:48:25 +01:00
|
|
|
'enabled' => true,
|
2017-10-09 06:26:07 +02:00
|
|
|
'enabled_cms' => false,
|
|
|
|
'require_login' => false,
|
|
|
|
'require_login_cms' => false,
|
|
|
|
'required_permission' => false,
|
2016-02-19 01:48:25 +01:00
|
|
|
'include_js' => true,
|
2017-10-09 06:26:07 +02:00
|
|
|
'use_gravatar' => false,
|
2016-02-19 01:48:25 +01:00
|
|
|
'gravatar_size' => 80,
|
|
|
|
'gravatar_default' => 'identicon',
|
|
|
|
'gravatar_rating' => 'g',
|
2017-10-09 06:26:07 +02:00
|
|
|
'show_comments_when_disabled' => false,
|
2016-02-19 01:48:25 +01:00
|
|
|
'order_comments_by' => '"Created" DESC',
|
2017-10-09 06:26:07 +02:00
|
|
|
'order_replies_by' => false,
|
2016-02-19 01:48:25 +01:00
|
|
|
'comments_per_page' => 10,
|
|
|
|
'comments_holder_id' => 'comments-holder',
|
|
|
|
'comment_permalink_prefix' => 'comment-',
|
2017-10-09 06:26:07 +02:00
|
|
|
'require_moderation' => false,
|
|
|
|
'require_moderation_nonmembers' => false,
|
|
|
|
'require_moderation_cms' => false,
|
|
|
|
'frontend_moderation' => false,
|
|
|
|
'frontend_spam' => false,
|
|
|
|
'html_allowed' => false,
|
2017-09-18 04:16:24 +02:00
|
|
|
'html_allowed_elements' => ['a', 'img', 'i', 'b'],
|
2017-10-09 06:26:07 +02:00
|
|
|
'use_preview' => false,
|
|
|
|
'nested_comments' => false,
|
2016-02-19 01:48:25 +01:00
|
|
|
'nested_depth' => 2,
|
2017-09-18 04:16:24 +02:00
|
|
|
];
|
2016-02-19 01:48:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-18 04:16:24 +02:00
|
|
|
private static $db = [
|
2016-02-19 01:48:25 +01:00
|
|
|
'ProvideComments' => 'Boolean',
|
|
|
|
'ModerationRequired' => 'Enum(\'None,Required,NonMembersOnly\',\'None\')',
|
|
|
|
'CommentsRequireLogin' => 'Boolean',
|
2017-09-18 04:16:24 +02:00
|
|
|
];
|
2016-02-19 01:48:25 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
private static $has_many = [
|
2017-10-09 06:26:07 +02:00
|
|
|
'Commments' => Comment::class . '.Parent'
|
2017-01-16 20:57:37 +01:00
|
|
|
];
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* CMS configurable options should default to the config values, but respect
|
|
|
|
* default values specified by the object
|
|
|
|
*/
|
|
|
|
public function populateDefaults()
|
|
|
|
{
|
2017-10-09 06:26:07 +02:00
|
|
|
$defaults = $this->owner->config()->get('defaults');
|
2016-02-19 01:48:25 +01:00
|
|
|
|
|
|
|
// Set if comments should be enabled by default
|
|
|
|
if (isset($defaults['ProvideComments'])) {
|
|
|
|
$this->owner->ProvideComments = $defaults['ProvideComments'];
|
|
|
|
} else {
|
|
|
|
$this->owner->ProvideComments = $this->owner->getCommentsOption('enabled') ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If moderation options should be configurable via the CMS then
|
|
|
|
if (isset($defaults['ModerationRequired'])) {
|
|
|
|
$this->owner->ModerationRequired = $defaults['ModerationRequired'];
|
|
|
|
} elseif ($this->owner->getCommentsOption('require_moderation')) {
|
|
|
|
$this->owner->ModerationRequired = 'Required';
|
|
|
|
} elseif ($this->owner->getCommentsOption('require_moderation_nonmembers')) {
|
|
|
|
$this->owner->ModerationRequired = 'NonMembersOnly';
|
|
|
|
} else {
|
|
|
|
$this->owner->ModerationRequired = 'None';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set login required
|
|
|
|
if (isset($defaults['CommentsRequireLogin'])) {
|
|
|
|
$this->owner->CommentsRequireLogin = $defaults['CommentsRequireLogin'];
|
|
|
|
} else {
|
|
|
|
$this->owner->CommentsRequireLogin = $this->owner->getCommentsOption('require_login') ? 1 : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this extension is applied to a {@link SiteTree} record then
|
|
|
|
* append a Provide Comments checkbox to allow authors to trigger
|
|
|
|
* whether or not to display comments
|
|
|
|
*
|
|
|
|
* @todo Allow customization of other {@link Commenting} configuration
|
|
|
|
*
|
|
|
|
* @param FieldList $fields
|
|
|
|
*/
|
|
|
|
public function updateSettingsFields(FieldList $fields)
|
|
|
|
{
|
2017-12-18 21:14:47 +01:00
|
|
|
$options = FieldGroup::create()->setTitle(_t(__CLASS__ . '.COMMENTOPTIONS', 'Comments'));
|
2016-02-19 01:48:25 +01:00
|
|
|
|
|
|
|
// Check if enabled setting should be cms configurable
|
|
|
|
if ($this->owner->getCommentsOption('enabled_cms')) {
|
2018-09-24 18:04:17 +02:00
|
|
|
$options->push(CheckboxField::create('ProvideComments', _t(
|
|
|
|
'SilverStripe\\Comments\\Model\\Comment.ALLOWCOMMENTS',
|
2019-05-02 01:31:32 +02:00
|
|
|
'Allow comments'
|
2018-09-24 18:04:17 +02:00
|
|
|
)));
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we should require users to login to comment
|
|
|
|
if ($this->owner->getCommentsOption('require_login_cms')) {
|
|
|
|
$options->push(
|
2018-09-24 18:04:17 +02:00
|
|
|
CheckboxField::create(
|
2016-02-19 01:48:25 +01:00
|
|
|
'CommentsRequireLogin',
|
|
|
|
_t('Comments.COMMENTSREQUIRELOGIN', 'Require login to comment')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options->FieldList()->count()) {
|
|
|
|
if ($fields->hasTabSet()) {
|
|
|
|
$fields->addFieldsToTab('Root.Settings', $options);
|
|
|
|
} else {
|
|
|
|
$fields->push($options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if moderation should be enabled via cms configurable
|
|
|
|
if ($this->owner->getCommentsOption('require_moderation_cms')) {
|
2018-09-24 18:04:17 +02:00
|
|
|
$moderationField = DropdownField::create(
|
|
|
|
'ModerationRequired',
|
|
|
|
_t(
|
|
|
|
__CLASS__ . '.COMMENTMODERATION',
|
|
|
|
'Comment Moderation'
|
2016-02-19 01:48:25 +01:00
|
|
|
),
|
2018-09-24 18:04:17 +02:00
|
|
|
[
|
|
|
|
'None' => _t(__CLASS__ . '.MODERATIONREQUIRED_NONE', 'No moderation required'),
|
|
|
|
'Required' => _t(__CLASS__ . '.MODERATIONREQUIRED_REQUIRED', 'Moderate all comments'),
|
|
|
|
'NonMembersOnly' => _t(
|
|
|
|
__CLASS__ . '.MODERATIONREQUIRED_NONMEMBERSONLY',
|
|
|
|
'Only moderate non-members'
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
2016-02-19 01:48:25 +01:00
|
|
|
if ($fields->hasTabSet()) {
|
2018-09-24 18:04:17 +02:00
|
|
|
$fields->addFieldToTab('Root.Settings', $moderationField);
|
2016-02-19 01:48:25 +01:00
|
|
|
} else {
|
|
|
|
$fields->push($moderationField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get comment moderation rules for this parent
|
|
|
|
*
|
|
|
|
* None: No moderation required
|
|
|
|
* Required: All comments
|
|
|
|
* NonMembersOnly: Only anonymous users
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getModerationRequired()
|
|
|
|
{
|
|
|
|
if ($this->owner->getCommentsOption('require_moderation_cms')) {
|
|
|
|
return $this->owner->getField('ModerationRequired');
|
2018-09-24 18:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->owner->getCommentsOption('require_moderation')) {
|
2016-02-19 01:48:25 +01:00
|
|
|
return 'Required';
|
2018-09-24 18:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->owner->getCommentsOption('require_moderation_nonmembers')) {
|
2016-02-19 01:48:25 +01:00
|
|
|
return 'NonMembersOnly';
|
|
|
|
}
|
2018-09-24 18:09:12 +02:00
|
|
|
|
|
|
|
return 'None';
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if users must be logged in to post comments
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getCommentsRequireLogin()
|
|
|
|
{
|
|
|
|
if ($this->owner->getCommentsOption('require_login_cms')) {
|
|
|
|
return (bool) $this->owner->getField('CommentsRequireLogin');
|
|
|
|
}
|
2018-09-24 18:09:12 +02:00
|
|
|
return (bool) $this->owner->getCommentsOption('require_login');
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the RelationList of all comments against this object. Can be used as a data source
|
|
|
|
* for a gridfield with write access.
|
|
|
|
*
|
2017-01-16 20:57:37 +01:00
|
|
|
* @return DataList
|
2016-02-19 01:48:25 +01:00
|
|
|
*/
|
|
|
|
public function AllComments()
|
|
|
|
{
|
|
|
|
$order = $this->owner->getCommentsOption('order_comments_by');
|
2017-01-16 20:57:37 +01:00
|
|
|
$comments = Comment::get()
|
|
|
|
->filter('ParentID', $this->owner->ID)
|
2016-02-19 01:48:25 +01:00
|
|
|
->sort($order);
|
|
|
|
$this->owner->extend('updateAllComments', $comments);
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all comments against this object, with with spam and unmoderated items excluded, for use in the frontend
|
|
|
|
*
|
2017-01-16 20:57:37 +01:00
|
|
|
* @return DataList
|
2016-02-19 01:48:25 +01:00
|
|
|
*/
|
|
|
|
public function AllVisibleComments()
|
|
|
|
{
|
|
|
|
$list = $this->AllComments();
|
|
|
|
|
|
|
|
// Filter spam comments for non-administrators if configured
|
|
|
|
$showSpam = $this->owner->getCommentsOption('frontend_spam') && $this->owner->canModerateComments();
|
2017-09-14 01:12:07 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
if (!$showSpam) {
|
|
|
|
$list = $list->filter('IsSpam', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter un-moderated comments for non-administrators if moderation is enabled
|
|
|
|
$showUnmoderated = ($this->owner->ModerationRequired === 'None')
|
|
|
|
|| ($this->owner->getCommentsOption('frontend_moderation') && $this->owner->canModerateComments());
|
|
|
|
if (!$showUnmoderated) {
|
|
|
|
$list = $list->filter('Moderated', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->owner->extend('updateAllVisibleComments', $list);
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the root level comments, with spam and unmoderated items excluded, for use in the frontend
|
|
|
|
*
|
2017-01-16 20:57:37 +01:00
|
|
|
* @return DataList
|
2016-02-19 01:48:25 +01:00
|
|
|
*/
|
|
|
|
public function Comments()
|
|
|
|
{
|
|
|
|
$list = $this->AllVisibleComments();
|
|
|
|
|
|
|
|
// If nesting comments, only show root level
|
|
|
|
if ($this->owner->getCommentsOption('nested_comments')) {
|
|
|
|
$list = $list->filter('ParentCommentID', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->owner->extend('updateComments', $list);
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a paged list of the root level comments, with spam and unmoderated items excluded,
|
|
|
|
* for use in the frontend
|
|
|
|
*
|
|
|
|
* @return PaginatedList
|
|
|
|
*/
|
|
|
|
public function PagedComments()
|
|
|
|
{
|
|
|
|
$list = $this->Comments();
|
|
|
|
|
|
|
|
// Add pagination
|
2018-09-24 18:09:12 +02:00
|
|
|
$list = PaginatedList::create($list, Controller::curr()->getRequest());
|
2016-02-19 01:48:25 +01:00
|
|
|
$list->setPaginationGetVar('commentsstart' . $this->owner->ID);
|
|
|
|
$list->setPageLength($this->owner->getCommentsOption('comments_per_page'));
|
|
|
|
|
|
|
|
$this->owner->extend('updatePagedComments', $list);
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if comments are enabled for this instance
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getCommentsEnabled()
|
|
|
|
{
|
|
|
|
// Don't display comments form for pseudo-pages (such as the login form)
|
|
|
|
if (!$this->owner->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine which flag should be used to determine if this is enabled
|
|
|
|
if ($this->owner->getCommentsOption('enabled_cms')) {
|
2017-10-09 06:26:07 +02:00
|
|
|
return (bool) $this->owner->ProvideComments;
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2017-10-09 06:26:07 +02:00
|
|
|
|
|
|
|
return (bool) $this->owner->getCommentsOption('enabled');
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the HTML ID for the comment holder in the template
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCommentHolderID()
|
|
|
|
{
|
|
|
|
return $this->owner->getCommentsOption('comments_holder_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permission codes required in order to post (or empty if none required)
|
|
|
|
*
|
|
|
|
* @return string|array Permission or list of permissions, if required
|
|
|
|
*/
|
|
|
|
public function getPostingRequiredPermission()
|
|
|
|
{
|
|
|
|
return $this->owner->getCommentsOption('required_permission');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a user can post comments on this item
|
|
|
|
*
|
|
|
|
* @param Member $member Member to check
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canPostComment($member = null)
|
|
|
|
{
|
|
|
|
// Deny if not enabled for this object
|
|
|
|
if (!$this->owner->CommentsEnabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-14 01:12:07 +02:00
|
|
|
|
2017-07-28 07:12:01 +02:00
|
|
|
if (!$this->owner->canView($member)) {
|
|
|
|
// deny if current user cannot view the underlying record.
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-19 01:48:25 +01:00
|
|
|
|
|
|
|
// Check if member is required
|
|
|
|
$requireLogin = $this->owner->CommentsRequireLogin;
|
|
|
|
if (!$requireLogin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check member is logged in
|
2018-09-24 18:09:12 +02:00
|
|
|
$member = $member ?: Security::getCurrentUser();
|
2016-02-19 01:48:25 +01:00
|
|
|
if (!$member) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If member required check permissions
|
|
|
|
$requiredPermission = $this->owner->PostingRequiredPermission;
|
|
|
|
if ($requiredPermission && !Permission::checkMember($member, $requiredPermission)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this member can moderate comments in the CMS
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canModerateComments($member = null)
|
|
|
|
{
|
|
|
|
// Deny if not enabled for this object
|
|
|
|
if (!$this->owner->CommentsEnabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to can-edit
|
|
|
|
return $this->owner->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the RSS link to all comments
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCommentRSSLink()
|
|
|
|
{
|
2017-01-17 05:33:39 +01:00
|
|
|
return Director::absoluteURL('comments/rss');
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the RSS link to all comments on this page
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCommentRSSLinkPage()
|
|
|
|
{
|
|
|
|
return Controller::join_links(
|
2017-01-16 20:57:37 +01:00
|
|
|
$this->getCommentRSSLink(),
|
2018-11-27 13:19:55 +01:00
|
|
|
str_replace('\\', '-', get_class($this->owner)),
|
2017-01-16 20:57:37 +01:00
|
|
|
$this->owner->ID
|
2016-02-19 01:48:25 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Comments interface for the front end. Includes the CommentAddForm and the composition
|
|
|
|
* of the comments display.
|
|
|
|
*
|
|
|
|
* To customize the html see templates/CommentInterface.ss or extend this function with
|
|
|
|
* your own extension.
|
|
|
|
*
|
|
|
|
* @todo Cleanup the passing of all this configuration based functionality
|
|
|
|
*
|
|
|
|
* @see docs/en/Extending
|
|
|
|
*/
|
|
|
|
public function CommentsForm()
|
|
|
|
{
|
|
|
|
// Check if enabled
|
|
|
|
$enabled = $this->getCommentsEnabled();
|
|
|
|
if ($enabled && $this->owner->getCommentsOption('include_js')) {
|
2018-04-26 07:42:08 +02:00
|
|
|
Requirements::javascript('//code.jquery.com/jquery-3.3.1.min.js');
|
2017-10-09 06:26:07 +02:00
|
|
|
Requirements::javascript('silverstripe/comments:thirdparty/jquery-validate/jquery.validate.min.js');
|
2018-01-29 01:04:34 +01:00
|
|
|
Requirements::javascript('silverstripe/admin:client/dist/js/i18n.js');
|
2017-10-09 06:26:07 +02:00
|
|
|
Requirements::add_i18n_javascript('silverstripe/comments:javascript/lang');
|
|
|
|
Requirements::javascript('silverstripe/comments:javascript/CommentsInterface.js');
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$controller = CommentingController::create();
|
|
|
|
$controller->setOwnerRecord($this->owner);
|
2017-01-16 20:57:37 +01:00
|
|
|
$controller->setParentClass($this->owner->getClassName());
|
2016-02-19 01:48:25 +01:00
|
|
|
$controller->setOwnerController(Controller::curr());
|
|
|
|
|
2017-07-04 16:53:38 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$moderatedSubmitted = $session->get('CommentsModerated');
|
|
|
|
$session->clear('CommentsModerated');
|
2016-02-19 01:48:25 +01:00
|
|
|
|
|
|
|
$form = ($enabled) ? $controller->CommentsForm() : false;
|
|
|
|
|
|
|
|
// a little bit all over the show but to ensure a slightly easier upgrade for users
|
|
|
|
// return back the same variables as previously done in comments
|
|
|
|
return $this
|
|
|
|
->owner
|
2018-09-24 18:09:12 +02:00
|
|
|
->customise([
|
2016-02-19 01:48:25 +01:00
|
|
|
'AddCommentForm' => $form,
|
|
|
|
'ModeratedSubmitted' => $moderatedSubmitted,
|
2018-09-24 18:09:12 +02:00
|
|
|
])
|
2016-02-19 01:48:25 +01:00
|
|
|
->renderWith('CommentsInterface');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this extension instance is attached to a {@link SiteTree} object
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function attachedToSiteTree()
|
|
|
|
{
|
2017-10-09 06:26:07 +02:00
|
|
|
$class = $this->owner->baseClass();
|
2016-02-19 01:48:25 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
return (is_subclass_of($class, SiteTree::class)) || ($class == SiteTree::class);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-18 04:16:24 +02:00
|
|
|
* Get the commenting option for this object.
|
2016-02-19 01:48:25 +01:00
|
|
|
*
|
2017-09-18 04:16:24 +02:00
|
|
|
* This can be overridden in any instance or extension to customise the
|
|
|
|
* option available.
|
2016-02-19 01:48:25 +01:00
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return mixed Result if the setting is available, or null otherwise
|
|
|
|
*/
|
|
|
|
public function getCommentsOption($key)
|
|
|
|
{
|
2017-09-18 04:16:24 +02:00
|
|
|
$settings = $this->getCommentsOptions();
|
2016-02-19 01:48:25 +01:00
|
|
|
$value = null;
|
2017-09-18 04:16:24 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
if (isset($settings[$key])) {
|
|
|
|
$value = $settings[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// To allow other extensions to customise this option
|
|
|
|
if ($this->owner) {
|
|
|
|
$this->owner->extend('updateCommentsOption', $key, $value);
|
|
|
|
}
|
2017-09-18 04:16:24 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2017-09-18 04:16:24 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCommentsOptions()
|
|
|
|
{
|
|
|
|
if ($this->owner) {
|
2017-10-09 06:26:07 +02:00
|
|
|
$settings = $this->owner->config()->get('comments');
|
2017-09-18 04:16:24 +02:00
|
|
|
} else {
|
|
|
|
$settings = Config::inst()->get(__CLASS__, 'comments');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
/**
|
|
|
|
* Add moderation functions to the current fieldlist
|
|
|
|
*
|
|
|
|
* @param FieldList $fields
|
|
|
|
*/
|
|
|
|
protected function updateModerationFields(FieldList $fields)
|
|
|
|
{
|
2017-10-09 06:26:07 +02:00
|
|
|
Requirements::css('silverstripe/comments:css/cms.css');
|
2016-02-19 01:48:25 +01:00
|
|
|
|
|
|
|
$newComments = $this->owner->AllComments()->filter('Moderated', 0);
|
|
|
|
|
2018-09-24 18:09:12 +02:00
|
|
|
$newGrid = CommentsGridField::create(
|
2016-02-19 01:48:25 +01:00
|
|
|
'NewComments',
|
|
|
|
_t('CommentsAdmin.NewComments', 'New'),
|
|
|
|
$newComments,
|
|
|
|
CommentsGridFieldConfig::create()
|
|
|
|
);
|
|
|
|
|
|
|
|
$approvedComments = $this->owner->AllComments()->filter('Moderated', 1)->filter('IsSpam', 0);
|
|
|
|
|
|
|
|
$approvedGrid = new CommentsGridField(
|
|
|
|
'ApprovedComments',
|
|
|
|
_t('CommentsAdmin.Comments', 'Approved'),
|
|
|
|
$approvedComments,
|
|
|
|
CommentsGridFieldConfig::create()
|
|
|
|
);
|
|
|
|
|
|
|
|
$spamComments = $this->owner->AllComments()->filter('Moderated', 1)->filter('IsSpam', 1);
|
|
|
|
|
2018-09-24 18:09:12 +02:00
|
|
|
$spamGrid = CommentsGridField::create(
|
2016-02-19 01:48:25 +01:00
|
|
|
'SpamComments',
|
|
|
|
_t('CommentsAdmin.SpamComments', 'Spam'),
|
|
|
|
$spamComments,
|
|
|
|
CommentsGridFieldConfig::create()
|
|
|
|
);
|
|
|
|
|
|
|
|
$newCount = '(' . count($newComments) . ')';
|
|
|
|
$approvedCount = '(' . count($approvedComments) . ')';
|
|
|
|
$spamCount = '(' . count($spamComments) . ')';
|
|
|
|
|
|
|
|
if ($fields->hasTabSet()) {
|
2018-09-24 18:09:12 +02:00
|
|
|
$tabs = TabSet::create(
|
2016-02-19 01:48:25 +01:00
|
|
|
'Comments',
|
2018-09-24 18:09:12 +02:00
|
|
|
Tab::create(
|
2017-09-18 04:16:24 +02:00
|
|
|
'CommentsNewCommentsTab',
|
|
|
|
_t('SilverStripe\\Comments\\Admin\\CommentAdmin.NewComments', 'New') . ' ' . $newCount,
|
2016-02-19 01:48:25 +01:00
|
|
|
$newGrid
|
|
|
|
),
|
2018-09-24 18:09:12 +02:00
|
|
|
Tab::create(
|
2017-09-18 04:16:24 +02:00
|
|
|
'CommentsCommentsTab',
|
|
|
|
_t('SilverStripe\\Comments\\Admin\\CommentAdmin.Comments', 'Approved') . ' ' . $approvedCount,
|
2016-02-19 01:48:25 +01:00
|
|
|
$approvedGrid
|
|
|
|
),
|
2018-09-24 18:09:12 +02:00
|
|
|
Tab::create(
|
2017-09-18 04:16:24 +02:00
|
|
|
'CommentsSpamCommentsTab',
|
|
|
|
_t('SilverStripe\\Comments\\Admin\\CommentAdmin.SpamComments', 'Spam') . ' ' . $spamCount,
|
2016-02-19 01:48:25 +01:00
|
|
|
$spamGrid
|
|
|
|
)
|
|
|
|
);
|
2017-12-18 21:14:47 +01:00
|
|
|
$tabs->setTitle(_t(__CLASS__ . '.COMMENTSTABSET', 'Comments'));
|
2017-04-14 17:09:33 +02:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
$fields->addFieldToTab('Root', $tabs);
|
|
|
|
} else {
|
|
|
|
$fields->push($newGrid);
|
|
|
|
$fields->push($approvedGrid);
|
|
|
|
$fields->push($spamGrid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
// Disable moderation if not permitted
|
|
|
|
if ($this->owner->canModerateComments()) {
|
|
|
|
$this->updateModerationFields($fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't a page we should merge the settings into the CMS fields
|
|
|
|
if (!$this->attachedToSiteTree()) {
|
|
|
|
$this->updateSettingsFields($fields);
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 02:51:14 +02:00
|
|
|
}
|