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

View File

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