Use injection where possible and remove redundant code lines

This commit is contained in:
Robbie Averill 2018-09-24 18:09:12 +02:00
parent 9a57c3802c
commit c0ca79090b
2 changed files with 29 additions and 30 deletions

View File

@ -9,10 +9,7 @@ use SilverStripe\Comments\Controllers\CommentingController;
use SilverStripe\Comments\Model\Comment;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
@ -20,9 +17,11 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\View\Requirements;
/**
@ -219,13 +218,17 @@ class CommentsExtension extends DataExtension
{
if ($this->owner->getCommentsOption('require_moderation_cms')) {
return $this->owner->getField('ModerationRequired');
} elseif ($this->owner->getCommentsOption('require_moderation')) {
return 'Required';
} elseif ($this->owner->getCommentsOption('require_moderation_nonmembers')) {
return 'NonMembersOnly';
} else {
return 'None';
}
if ($this->owner->getCommentsOption('require_moderation')) {
return 'Required';
}
if ($this->owner->getCommentsOption('require_moderation_nonmembers')) {
return 'NonMembersOnly';
}
return 'None';
}
/**
@ -237,9 +240,8 @@ class CommentsExtension extends DataExtension
{
if ($this->owner->getCommentsOption('require_login_cms')) {
return (bool) $this->owner->getField('CommentsRequireLogin');
} else {
return (bool) $this->owner->getCommentsOption('require_login');
}
return (bool) $this->owner->getCommentsOption('require_login');
}
/**
@ -314,7 +316,7 @@ class CommentsExtension extends DataExtension
$list = $this->Comments();
// Add pagination
$list = new PaginatedList($list, Controller::curr()->getRequest());
$list = PaginatedList::create($list, Controller::curr()->getRequest());
$list->setPaginationGetVar('commentsstart' . $this->owner->ID);
$list->setPageLength($this->owner->getCommentsOption('comments_per_page'));
@ -388,7 +390,7 @@ class CommentsExtension extends DataExtension
}
// Check member is logged in
$member = $member ?: Member::currentUser();
$member = $member ?: Security::getCurrentUser();
if (!$member) {
return false;
}
@ -482,10 +484,10 @@ class CommentsExtension extends DataExtension
// return back the same variables as previously done in comments
return $this
->owner
->customise(array(
->customise([
'AddCommentForm' => $form,
'ModeratedSubmitted' => $moderatedSubmitted,
))
])
->renderWith('CommentsInterface');
}
@ -533,8 +535,6 @@ class CommentsExtension extends DataExtension
*/
public function getCommentsOptions()
{
$settings = [];
if ($this->owner) {
$settings = $this->owner->config()->get('comments');
} else {
@ -555,7 +555,7 @@ class CommentsExtension extends DataExtension
$newComments = $this->owner->AllComments()->filter('Moderated', 0);
$newGrid = new CommentsGridField(
$newGrid = CommentsGridField::create(
'NewComments',
_t('CommentsAdmin.NewComments', 'New'),
$newComments,
@ -573,7 +573,7 @@ class CommentsExtension extends DataExtension
$spamComments = $this->owner->AllComments()->filter('Moderated', 1)->filter('IsSpam', 1);
$spamGrid = new CommentsGridField(
$spamGrid = CommentsGridField::create(
'SpamComments',
_t('CommentsAdmin.SpamComments', 'Spam'),
$spamComments,
@ -585,19 +585,19 @@ class CommentsExtension extends DataExtension
$spamCount = '(' . count($spamComments) . ')';
if ($fields->hasTabSet()) {
$tabs = new TabSet(
$tabs = TabSet::create(
'Comments',
new Tab(
Tab::create(
'CommentsNewCommentsTab',
_t('SilverStripe\\Comments\\Admin\\CommentAdmin.NewComments', 'New') . ' ' . $newCount,
$newGrid
),
new Tab(
Tab::create(
'CommentsCommentsTab',
_t('SilverStripe\\Comments\\Admin\\CommentAdmin.Comments', 'Approved') . ' ' . $approvedCount,
$approvedGrid
),
new Tab(
Tab::create(
'CommentsSpamCommentsTab',
_t('SilverStripe\\Comments\\Admin\\CommentAdmin.SpamComments', 'Spam') . ' ' . $spamCount,
$spamGrid

View File

@ -17,7 +17,6 @@ use SilverStripe\Forms\ReadonlyField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
class CommentForm extends Form
@ -99,7 +98,7 @@ class CommentForm extends Form
);
}
$required = new RequiredFields(
$required = RequiredFields::create(
$controller->config()->required_fields
);
@ -109,22 +108,22 @@ class CommentForm extends Form
// if the record exists load the extra required data
if ($record = $controller->getOwnerRecord()) {
// Load member data
$member = Member::currentUser();
$member = Security::getCurrentUser();
if (($record->CommentsRequireLogin || $record->PostingRequiredPermission) && $member) {
$fields = $this->Fields();
$fields->removeByName('Name');
$fields->removeByName('Email');
$fields->insertBefore(
new ReadonlyField(
ReadonlyField::create(
'NameView',
_t('CommentInterface.YOURNAME', 'Your name'),
$member->getName()
),
'URL'
);
$fields->push(new HiddenField('Name', '', $member->getName()));
$fields->push(new HiddenField('Email', '', $member->Email));
$fields->push(HiddenField::create('Name', '', $member->getName()));
$fields->push(HiddenField::create('Email', '', $member->Email));
}
// we do not want to read a new URL when the form has already been submitted
@ -220,7 +219,7 @@ class CommentForm extends Form
}
if ($member = Security::getCurrentUser()) {
$form->Fields()->push(new HiddenField('AuthorID', 'Author ID', $member->ID));
$form->Fields()->push(HiddenField::create('AuthorID', 'Author ID', $member->ID));
}
// What kind of moderation is required?