diff --git a/phpcs.xml.dist b/phpcs.xml.dist index fa589da..d834f9a 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -6,17 +6,5 @@ - - - - - - - - - - - - - \ No newline at end of file + diff --git a/src/Admin/CommentAdmin.php b/src/Admin/CommentAdmin.php index 2cb4a8e..7cbd599 100644 --- a/src/Admin/CommentAdmin.php +++ b/src/Admin/CommentAdmin.php @@ -3,22 +3,19 @@ namespace SilverStripe\Comments\Admin; use SilverStripe\Admin\LeftAndMain; -use SilverStripe\Comments\Admin\CommentsGridField; use SilverStripe\Comments\Model\Comment; -use SilverStripe\Forms\Tab; -use SilverStripe\Forms\TabSet; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; -use SilverStripe\Security\PermissionProvider; +use SilverStripe\Forms\Tab; +use SilverStripe\Forms\TabSet; use SilverStripe\Security\Security; -use SilverStripe\View\SSViewer; /** * Comment administration system within the CMS * * @package comments */ -class CommentAdmin extends LeftAndMain implements PermissionProvider +class CommentAdmin extends LeftAndMain { private static $url_segment = 'comments'; @@ -28,7 +25,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider private static $menu_icon_class = 'font-icon-comment'; - private static $allowed_actions = array( + private static $allowed_actions = [ 'approvedmarked', 'deleteall', 'deletemarked', @@ -37,18 +34,18 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider 'spammarked', 'EditForm', 'unmoderated' - ); + ]; private static $required_permission_codes = 'CMS_ACCESS_CommentAdmin'; public function providePermissions() { - return array( - "CMS_ACCESS_CommentAdmin" => array( + return [ + 'CMS_ACCESS_CommentAdmin' => [ 'name' => _t(__CLASS__ . '.ADMIN_PERMISSION', "Access to 'Comments' section"), 'category' => _t('SilverStripe\\Security\\Permission.CMS_ACCESS_CATEGORY', 'CMS Access') - ) - ); + ], + ]; } /** @@ -69,7 +66,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $newComments = Comment::get()->filter('Moderated', 0); - $newGrid = new CommentsGridField( + $newGrid = CommentsGridField::create( 'NewComments', '', $newComments, @@ -78,7 +75,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $approvedComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 0); - $approvedGrid = new CommentsGridField( + $approvedGrid = CommentsGridField::create( 'ApprovedComments', '', $approvedComments, @@ -87,7 +84,7 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $spamComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 1); - $spamGrid = new CommentsGridField( + $spamGrid = CommentsGridField::create( 'SpamComments', '', $spamComments, @@ -136,11 +133,11 @@ class CommentAdmin extends LeftAndMain implements PermissionProvider $actions ); - $form->addExtraClass('cms-edit-form'); + $form->addExtraClass('cms-edit-form fill-height'); $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); if ($form->Fields()->hasTabset()) { - $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); + $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses()); } diff --git a/src/Admin/CommentsGridField.php b/src/Admin/CommentsGridField.php index 732c036..cde1eed 100644 --- a/src/Admin/CommentsGridField.php +++ b/src/Admin/CommentsGridField.php @@ -2,7 +2,6 @@ namespace SilverStripe\Comments\Admin; -use SilverStripe\Forms\FormField; use SilverStripe\Forms\GridField\GridField; use SilverStripe\View\HTML; diff --git a/src/Admin/CommentsGridFieldAction.php b/src/Admin/CommentsGridFieldAction.php index d602bf4..478472d 100644 --- a/src/Admin/CommentsGridFieldAction.php +++ b/src/Admin/CommentsGridFieldAction.php @@ -26,7 +26,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getColumnAttributes($gridField, $record, $columnName) { - return array('class' => 'col-buttons'); + return ['class' => 'col-buttons']; } /** @@ -34,8 +34,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getColumnMetadata($gridField, $columnName) { - if ($columnName == 'Actions') { - return array('title' => ''); + if ($columnName === 'Actions') { + return ['title' => '']; } } @@ -44,7 +44,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getColumnsHandled($gridField) { - return array('Actions'); + return ['Actions']; } /** @@ -64,7 +64,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act 'CustomAction' . $record->ID . 'Spam', _t(__CLASS__ . '.SPAM', 'Spam'), 'spam', - array('RecordID' => $record->ID) + ['RecordID' => $record->ID] ) ->addExtraClass('btn btn-secondary grid-field__icon-action') ->Field(); @@ -76,7 +76,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act 'CustomAction' . $record->ID . 'Approve', _t(__CLASS__ . '.APPROVE', 'Approve'), 'approve', - array('RecordID' => $record->ID) + ['RecordID' => $record->ID] ) ->addExtraClass('btn btn-secondary grid-field__icon-action') ->Field(); @@ -90,7 +90,7 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function getActions($gridField) { - return array('spam', 'approve'); + return ['spam', 'approve']; } /** @@ -98,7 +98,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { - if ($actionName == 'spam') { + if ($actionName === 'spam') { + /** @var Comment $comment */ $comment = Comment::get()->byID($arguments['RecordID']); $comment->markSpam(); @@ -109,7 +110,8 @@ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_Act ); } - if ($actionName == 'approve') { + if ($actionName === 'approve') { + /** @var Comment $comment */ $comment = Comment::get()->byID($arguments['RecordID']); $comment->markApproved(); diff --git a/src/Admin/CommentsGridFieldConfig.php b/src/Admin/CommentsGridFieldConfig.php index 4225fec..fb0c300 100644 --- a/src/Admin/CommentsGridFieldConfig.php +++ b/src/Admin/CommentsGridFieldConfig.php @@ -20,8 +20,9 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor $this->addComponent(new CommentsGridFieldAction()); // Format column + /** @var GridFieldDataColumns $columns */ $columns = $this->getComponentByType(GridFieldDataColumns::class); - $columns->setFieldFormatting(array( + $columns->setFieldFormatting([ 'ParentTitle' => function ($value, &$item) { return sprintf( '%s', @@ -29,10 +30,10 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor $item->obj('ParentTitle')->forTemplate() ); } - )); + ]); // Add bulk option - $manager = new BulkManager(null, false); + $manager = BulkManager::create(null, false); $spamAction = SpamHandler::create()->setLabel(_t(__CLASS__ . '.SPAM', 'Spam')); $approveAction = ApproveHandler::create()->setLabel(_t(__CLASS__ . '.APPROVE', 'Approve')); diff --git a/src/Controllers/CommentingController.php b/src/Controllers/CommentingController.php index f69afc7..50c5fcd 100644 --- a/src/Controllers/CommentingController.php +++ b/src/Controllers/CommentingController.php @@ -4,20 +4,21 @@ namespace SilverStripe\Comments\Controllers; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Comments\Extensions\CommentsExtension; +use SilverStripe\Comments\Forms\CommentForm; use SilverStripe\Comments\Model\Comment; -use SilverStripe\Control\Director; use SilverStripe\Control\Controller; -use SilverStripe\Control\Email\Email; +use SilverStripe\Control\Director; use SilverStripe\Control\HTTP; use SilverStripe\Control\HTTPRequest; +use SilverStripe\Control\HTTPResponse; +use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Control\RSS\RSSFeed; -use SilverStripe\Control\Session; -use SilverStripe\ORM\DataObject; -use SilverStripe\ORM\PaginatedList; -use SilverStripe\Security\Member; -use SilverStripe\Security\Security; use SilverStripe\Core\Injector\Injector; -use SilverStripe\Comments\Forms\CommentForm; +use SilverStripe\Forms\Form; +use SilverStripe\ORM\DataObject; +use SilverStripe\ORM\FieldType\DBHTMLText; +use SilverStripe\ORM\PaginatedList; +use SilverStripe\Security\Security; /** * @package comments @@ -27,7 +28,7 @@ class CommentingController extends Controller /** * {@inheritDoc} */ - private static $allowed_actions = array( + private static $allowed_actions = [ 'delete', 'spam', 'ham', @@ -36,15 +37,15 @@ class CommentingController extends Controller 'CommentsForm', 'reply', 'doPostComment', - 'doPreviewComment' - ); + 'doPreviewComment', + ]; /** * {@inheritDoc} */ - private static $url_handlers = array( + private static $url_handlers = [ 'reply/$ParentCommentID//$ID/$OtherID' => 'reply', - ); + ]; /** * Fields required for this form @@ -52,11 +53,11 @@ class CommentingController extends Controller * @var array * @config */ - private static $required_fields = array( + private static $required_fields = [ 'Name', 'Email', - 'Comment' - ); + 'Comment', + ]; /** * Parent class this commenting form is for @@ -70,21 +71,21 @@ class CommentingController extends Controller * * @var DataObject */ - private $ownerRecord = null; + private $ownerRecord; /** * Parent controller record * * @var Controller */ - private $ownerController = null; + private $ownerController; /** * Backup url to return to * * @var string */ - protected $fallbackReturnURL = null; + protected $fallbackReturnURL; /** * Set the parent class name to use @@ -178,6 +179,7 @@ class CommentingController extends Controller { // If possible use the current record if ($record = $this->getOwnerRecord()) { + /** @var DataObject|CommentsExtension $record */ return $record->getCommentsOption($key); } @@ -198,6 +200,7 @@ class CommentingController extends Controller public function getOptions() { if ($record = $this->getOwnerRecord()) { + /** @var DataObject|CommentsExtension $record */ return $record->getCommentsOptions(); } @@ -226,7 +229,7 @@ class CommentingController extends Controller /** * Outputs the RSS feed of comments * - * @return HTMLText + * @return DBHTMLText */ public function rss() { @@ -252,10 +255,10 @@ class CommentingController extends Controller $class = SiteTree::class; } - $comments = Comment::get()->filter(array( + $comments = Comment::get()->filter([ 'Moderated' => 1, 'IsSpam' => 0, - )); + ]); // Check if class filter if ($class) { @@ -275,10 +278,10 @@ class CommentingController extends Controller } $title = _t(__CLASS__ . '.RSSTITLE', "Comments RSS Feed"); - $comments = new PaginatedList($comments, $request); + $comments = PaginatedList::create($comments, $request); $comments->setPageLength($this->getOption('comments_per_page')); - return new RSSFeed( + return RSSFeed::create( $comments, $link, $title, @@ -375,6 +378,9 @@ class CommentingController extends Controller * Redirect back to referer if available, ensuring that only site URLs * are allowed to avoid phishing. If it's an AJAX request render the * comment in it's new state + * + * @param Comment $comment + * @return DBHTMLText|HTTPResponse|false */ private function renderChangedCommentState($comment) { @@ -383,21 +389,21 @@ class CommentingController extends Controller // Render comment using AJAX if ($this->request->isAjax()) { return $comment->renderWith('Includes/CommentsInterface_singlecomment'); - } else { - // Redirect to either the comment or start of the page - if (empty($referer)) { - return $this->redirectBack(); - } else { - // Redirect to the comment, but check for phishing - $url = $referer . '#comment-' . $comment->ID; - // absolute redirection URLs not located on this site may cause phishing - if (Director::is_site_url($url)) { - return $this->redirect($url); - } else { - return false; - } - } } + + // Redirect to either the comment or start of the page + if (empty($referer)) { + return $this->redirectBack(); + } + + // Redirect to the comment, but check for phishing + $url = $referer . '#comment-' . $comment->ID; + // absolute redirection URLs not located on this site may cause phishing + if (Director::is_site_url($url)) { + return $this->redirect($url); + } + + return false; } /** @@ -411,6 +417,7 @@ class CommentingController extends Controller $id = isset($this->urlParams['ID']) ? $this->urlParams['ID'] : false; if ($id) { + /** @var Comment $comment */ $comment = Comment::get()->byId($id); if ($comment) { $this->fallbackReturnURL = $comment->Link(); @@ -436,9 +443,9 @@ class CommentingController extends Controller $form->addExtraClass('reply-form'); // Load parent into reply form - $form->loadDataFrom(array( + $form->loadDataFrom([ 'ParentCommentID' => $comment->ID - )); + ]); // Customise action $form->setFormAction($this->Link('reply', $comment->ID)); @@ -461,6 +468,7 @@ class CommentingController extends Controller { // Extract parent comment from reply and build this way if ($parentID = $request->param('ParentCommentID')) { + /** @var Comment $comment */ $comment = DataObject::get_by_id(Comment::class, $parentID, true); if ($comment) { return $this->ReplyForm($comment); @@ -518,8 +526,8 @@ class CommentingController extends Controller // absolute redirection URLs not located on this site may cause phishing if (Director::is_site_url($url)) { return $this->redirect($url); - } else { - return false; } + + return false; } } diff --git a/src/Extensions/CommentsExtension.php b/src/Extensions/CommentsExtension.php index ed827fb..ce0ec93 100644 --- a/src/Extensions/CommentsExtension.php +++ b/src/Extensions/CommentsExtension.php @@ -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; /** @@ -157,13 +156,16 @@ class CommentsExtension extends DataExtension // Check if enabled setting should be cms configurable if ($this->owner->getCommentsOption('enabled_cms')) { - $options->push(new CheckboxField('ProvideComments', _t('SilverStripe\\Comments\\Model\\Comment.ALLOWCOMMENTS', 'Allow Comments'))); + $options->push(CheckboxField::create('ProvideComments', _t( + 'SilverStripe\\Comments\\Model\\Comment.ALLOWCOMMENTS', + 'Allow Comments' + ))); } // Check if we should require users to login to comment if ($this->owner->getCommentsOption('require_login_cms')) { $options->push( - new CheckboxField( + CheckboxField::create( 'CommentsRequireLogin', _t('Comments.COMMENTSREQUIRELOGIN', 'Require login to comment') ) @@ -180,16 +182,23 @@ class CommentsExtension extends DataExtension // Check if moderation should be enabled via cms configurable if ($this->owner->getCommentsOption('require_moderation_cms')) { - $moderationField = new DropdownField('ModerationRequired', _t(__CLASS__ . '.COMMENTMODERATION', 'Comment Moderation'), array( - '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' + $moderationField = DropdownField::create( + 'ModerationRequired', + _t( + __CLASS__ . '.COMMENTMODERATION', + 'Comment Moderation' ), - )); + [ + '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' + ), + ] + ); if ($fields->hasTabSet()) { - $fields->addFieldsToTab('Root.Settings', $moderationField); + $fields->addFieldToTab('Root.Settings', $moderationField); } else { $fields->push($moderationField); } @@ -209,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'; } /** @@ -227,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'); } /** @@ -304,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')); @@ -378,7 +390,7 @@ class CommentsExtension extends DataExtension } // Check member is logged in - $member = $member ?: Member::currentUser(); + $member = $member ?: Security::getCurrentUser(); if (!$member) { return false; } @@ -472,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'); } @@ -523,8 +535,6 @@ class CommentsExtension extends DataExtension */ public function getCommentsOptions() { - $settings = []; - if ($this->owner) { $settings = $this->owner->config()->get('comments'); } else { @@ -545,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, @@ -563,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, @@ -575,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 diff --git a/src/Forms/CommentForm.php b/src/Forms/CommentForm.php index edee7de..04c1a68 100644 --- a/src/Forms/CommentForm.php +++ b/src/Forms/CommentForm.php @@ -2,7 +2,11 @@ namespace SilverStripe\Comments\Forms; +use SilverStripe\Comments\Controllers\CommentingController; +use SilverStripe\Comments\Model\Comment; +use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPResponse; +use SilverStripe\Core\Convert; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\EmailField; use SilverStripe\Forms\FieldList; @@ -13,14 +17,7 @@ use SilverStripe\Forms\ReadonlyField; use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\TextareaField; use SilverStripe\Forms\TextField; -use SilverStripe\Security\Member; -use SilverStripe\Control\Cookie; -use SilverStripe\Core\Convert; use SilverStripe\Security\Security; -use SilverStripe\Comments\Model\Comment; -use SilverStripe\Control\Controller; -use SilverStripe\Comments\Controllers\CommentingController; -use SilverStripe\Core\Config\Config; class CommentForm extends Form { @@ -46,18 +43,27 @@ class CommentForm extends Form // Email EmailField::create( 'Email', - _t('SilverStripe\\Comments\\Controllers\\CommentingController.EMAILADDRESS', 'Your email address (will not be published)') + _t( + 'SilverStripe\\Comments\\Controllers\\CommentingController.EMAILADDRESS', + 'Your email address (will not be published)' + ) ) ->setCustomValidationMessage($emailRequired) ->setAttribute('data-msg-required', $emailRequired) ->setAttribute('data-msg-email', $emailInvalid) ->setAttribute('data-rule-email', true), // Url - TextField::create('URL', _t('SilverStripe\\Comments\\Controllers\\CommentingController.WEBSITEURL', 'Your website URL')) + TextField::create('URL', _t( + 'SilverStripe\\Comments\\Controllers\\CommentingController.WEBSITEURL', + 'Your website URL' + )) ->setAttribute('data-msg-url', $urlInvalid) ->setAttribute('data-rule-url', true), // Comment - TextareaField::create('Comment', _t('SilverStripe\\Comments\\Controllers\\CommentingController.COMMENTS', 'Comments')) + TextareaField::create('Comment', _t( + 'SilverStripe\\Comments\\Controllers\\CommentingController.COMMENTS', + 'Comments' + )) ->setCustomValidationMessage($commentRequired) ->setAttribute('data-msg-required', $commentRequired) ), @@ -92,7 +98,7 @@ class CommentForm extends Form ); } - $required = new RequiredFields( + $required = RequiredFields::create( $controller->config()->required_fields ); @@ -102,51 +108,59 @@ 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 // which in here, it hasn't been. - $this->loadDataFrom(array( + $this->loadDataFrom([ 'ParentID' => $record->ID, 'ReturnURL' => $controller->getRequest()->getURL(), 'ParentClassName' => $controller->getParentClass() - )); + ]); } // Set it so the user gets redirected back down to the form upon form fail $this->setRedirectToFormOnValidationError(true); - // load any data from the cookies - if ($data = Cookie::get('CommentsForm_UserData')) { - $data = Convert::json2array($data); + // load any data from the session + $data = $this->getSessionData(); + if (!is_array($data)) { + return; + } - $this->loadDataFrom(array( - 'Name' => isset($data['Name']) ? $data['Name'] : '', - 'URL' => isset($data['URL']) ? $data['URL'] : '', - 'Email' => isset($data['Email']) ? $data['Email'] : '' - )); + // load user data from previous form request back into form. + if (array_key_exists('UserData', $data)) { + $formData = Convert::json2array($data['UserData']); - // allow previous value to fill if comment not stored in cookie (i.e. validation error) - $prevComment = Cookie::get('CommentsForm_Comment'); + $this->loadDataFrom([ + 'Name' => isset($formData['Name']) ? $formData['Name'] : '', + 'URL' => isset($formData['URL']) ? $formData['URL'] : '', + 'Email' => isset($formData['Email']) ? $formData['Email'] : '' + ]); + } + + // allow previous value to fill if comment + if (array_key_exists('Comment', $data)) { + $prevComment = $data['Comment']; if ($prevComment && $prevComment != '') { - $this->loadDataFrom(array('Comment' => $prevComment)); + $this->loadDataFrom(['Comment' => $prevComment]); } } } @@ -184,8 +198,10 @@ class CommentForm extends Form } // cache users data - Cookie::set('CommentsForm_UserData', Convert::raw2json($data)); - Cookie::set('CommentsForm_Comment', $data['Comment']); + $form->setSessionData([ + 'UserData' => Convert::raw2json($data), + 'Comment' => $data['Comment'] + ]); // extend hook to allow extensions. Also see onAfterPostComment $this->controller->extend('onBeforePostComment', $form); @@ -203,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? @@ -246,8 +262,16 @@ class CommentForm extends Form $this->getRequest()->getSession()->set('CommentsModerated', 1); } - // clear the users comment since it passed validation - Cookie::set('CommentsForm_Comment', false); + // clear the users comment since the comment was successful. + if ($comment->exists()) { + // Remove the comment data as it's been saved already. + unset($data['Comment']); + } + + // cache users data (name, email, etc to prepopulate on other forms). + $form->setSessionData([ + 'UserData' => Convert::raw2json($data), + ]); // Find parent link if (!empty($data['ReturnURL'])) {