From b6a2c608acd105f73382033647c6749cd83f996a Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Fri, 25 Jan 2019 15:47:38 +1300 Subject: [PATCH] NEW relocate spam and approve admin actions Previous versions had individual spam and approve buttons for a comment in the admin area on the GridField row. However with the upgrade to SilverStripe 4, and particularly 4.2, these were having layout issues with the new GridField Action Menu component that groups actions together. The solution here is to put them into aforementioned gridfield action menu component, with the other actions for that row. However this requires two separate grid field components (one each for the two comment actions) - previously these were a single component that output two buttons instead of one each. This also reduces coupling, which is nice :) The previous class is still maintained for backwards compatibilty, but is deprecated. --- .travis.yml | 10 +- composer.json | 2 +- src/Admin/CommentsGridFieldAction.php | 3 + src/Admin/CommentsGridFieldApproveAction.php | 140 ++++++++++++++++++ src/Admin/CommentsGridFieldConfig.php | 6 +- src/Admin/CommentsGridFieldSpamAction.php | 141 +++++++++++++++++++ 6 files changed, 296 insertions(+), 6 deletions(-) create mode 100644 src/Admin/CommentsGridFieldApproveAction.php create mode 100644 src/Admin/CommentsGridFieldSpamAction.php diff --git a/.travis.yml b/.travis.yml index 4fdf730..efa691e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,12 +4,14 @@ dist: trusty matrix: include: - php: 5.6 - env: DB=MYSQL INSTALLER_VERSION=4.0.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1 + env: DB=MYSQL INSTALLER_VERSION=4.2.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1 - php: 7.0 - env: DB=PGSQL INSTALLER_VERSION=4.1.x-dev PHPUNIT_TEST=1 + env: DB=PGSQL INSTALLER_VERSION=4.2.x-dev PHPUNIT_TEST=1 - php: 7.1 - env: DB=MYSQL INSTALLER_VERSION=4.2.x-dev PHPUNIT_COVERAGE_TEST=1 + env: DB=MYSQL INSTALLER_VERSION=4.3.x-dev PHPUNIT_COVERAGE_TEST=1 - php: 7.2 + env: DB=MYSQL INSTALLER_VERSION=4.3.x-dev PHPUNIT_TEST=1 + - php: 7.3 env: DB=MYSQL INSTALLER_VERSION=4.x-dev PHPUNIT_TEST=1 before_script: @@ -17,7 +19,7 @@ before_script: - phpenv config-rm xdebug.ini - composer validate - composer require silverstripe/installer:"$INSTALLER_VERSION" ezyang/htmlpurifier:* --no-update - - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.0.x-dev; fi + - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.x-dev; fi - composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile script: diff --git a/composer.json b/composer.json index 9d530b4..f4018f7 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "silverstripe/framework": "^4.0", + "silverstripe/framework": "^4.2", "colymba/gridfield-bulk-editing-tools": "^3.0" }, "suggest": { diff --git a/src/Admin/CommentsGridFieldAction.php b/src/Admin/CommentsGridFieldAction.php index 478472d..dc0d867 100644 --- a/src/Admin/CommentsGridFieldAction.php +++ b/src/Admin/CommentsGridFieldAction.php @@ -9,6 +9,9 @@ use SilverStripe\Forms\GridField\GridField_ActionProvider; use SilverStripe\Forms\GridField\GridField_ColumnProvider; use SilverStripe\Forms\GridField\GridField_FormAction; +/** + * @deprecated 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead + */ class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider { /** diff --git a/src/Admin/CommentsGridFieldApproveAction.php b/src/Admin/CommentsGridFieldApproveAction.php new file mode 100644 index 0000000..e65ff74 --- /dev/null +++ b/src/Admin/CommentsGridFieldApproveAction.php @@ -0,0 +1,140 @@ +getApproveAction($gridField, $record, $columnName); + + if ($field) { + return $field->getAttributes(); + } + + return null; + } + public function getGroup($gridField, $record, $columnName) + { + $field = $this->getApproveAction($gridField, $record, $columnName); + + return $field ? GridField_ActionMenuItem::DEFAULT_GROUP: null; + } + + /** + * {@inheritdoc} + */ + public function getColumnAttributes($gridField, $record, $columnName) + { + return ['class' => 'col-buttons grid-field__col-compact']; + } + + /** + * {@inheritdoc} + */ + public function getColumnMetadata($gridField, $columnName) + { + if ($columnName === 'Actions') { + return ['title' => '']; + } + } + + /** + * {@inheritdoc} + */ + public function getColumnsHandled($gridField) + { + return ['Actions']; + } + + /** + * {@inheritdoc} + */ + public function getColumnContent($gridField, $record, $columnName) + { + if (!$record->canEdit()) { + return; + } + + $field = $this->getApproveAction($gridField, $record, $columnName); + + return $field ? $field->Field() : null; + } + + /** + * Returns the FormAction object, used by other methods to get properties + * + * @return GridField_FormAction + */ + public function getApproveAction($gridField, $record, $columnName) + { + $field = GridField_FormAction::create( + $gridField, + 'CustomAction' . $record->ID . 'Approve', + _t(__CLASS__ . '.APPROVE', 'Approve'), + 'approve', + ['RecordID' => $record->ID] + ) + ->addExtraClass(implode(' ', [ + 'btn', + 'btn-secondary', + 'grid-field__icon-action', + 'action-menu--handled', + 'font-icon-check-mark', + ])) + ->setAttribute('classNames', 'font-icon-check-mark'); + + return ($record->IsSpam || !$record->Moderated) ? $field : null; + } + + /** + * {@inheritdoc} + */ + public function getActions($gridField) + { + return ['approve']; + } + + /** + * {@inheritdoc} + */ + public function handleAction(GridField $gridField, $actionName, $arguments, $data) + { + /** @var Comment $comment */ + $comment = Comment::get()->byID($arguments['RecordID']); + $comment->markApproved(); + + // output a success message to the user + Controller::curr()->getResponse()->setStatusCode( + 200, + _t(__CLASS__ . '.COMMENTAPPROVED', 'Comment approved.') + ); + } +} diff --git a/src/Admin/CommentsGridFieldConfig.php b/src/Admin/CommentsGridFieldConfig.php index fb0c300..9834df7 100644 --- a/src/Admin/CommentsGridFieldConfig.php +++ b/src/Admin/CommentsGridFieldConfig.php @@ -6,6 +6,7 @@ use Colymba\BulkManager\BulkManager; use SilverStripe\Comments\Admin\CommentsGridFieldBulkAction\ApproveHandler; use SilverStripe\Comments\Admin\CommentsGridFieldBulkAction\SpamHandler; use SilverStripe\Core\Convert; +use SilverStripe\Forms\GridField\GridField_ActionMenu; use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; use SilverStripe\Forms\GridField\GridFieldDataColumns; @@ -17,7 +18,10 @@ class CommentsGridFieldConfig extends GridFieldConfig_RecordEditor // $this->addComponent(new GridFieldExportButton()); - $this->addComponent(new CommentsGridFieldAction()); + $this->addComponents([ + new CommentsGridFieldSpamAction(), + new CommentsGridFieldApproveAction(), + ]); // Format column /** @var GridFieldDataColumns $columns */ diff --git a/src/Admin/CommentsGridFieldSpamAction.php b/src/Admin/CommentsGridFieldSpamAction.php new file mode 100644 index 0000000..4cbbba6 --- /dev/null +++ b/src/Admin/CommentsGridFieldSpamAction.php @@ -0,0 +1,141 @@ +getSpamAction($gridField, $record, $columnName); + + if ($field) { + return $field->getAttributes(); + } + + return null; + } + + public function getGroup($gridField, $record, $columnName) + { + $field = $this->getSpamAction($gridField, $record, $columnName); + + return $field ? GridField_ActionMenuItem::DEFAULT_GROUP: null; + } + + + /** + * {@inheritdoc} + */ + public function getColumnAttributes($gridField, $record, $columnName) + { + return ['class' => 'col-buttons grid-field__col-compact']; + } + + /** + * {@inheritdoc} + */ + public function getColumnMetadata($gridField, $columnName) + { + if ($columnName === 'Actions') { + return ['title' => '']; + } + } + + /** + * {@inheritdoc} + */ + public function getColumnsHandled($gridField) + { + return ['Actions']; + } + + /** + * {@inheritdoc} + */ + public function getColumnContent($gridField, $record, $columnName) + { + if (!$record->canEdit()) { + return; + } + + $field = $this->getSpamAction($gridField, $record, $columnName); + + return $field ? $field->Field() : null; + } + + /** + * Returns the FormAction object, used by other methods to get properties + * + * @return GridField_FormAction + */ + public function getSpamAction($gridField, $record, $columnName) + { + $field = GridField_FormAction::create( + $gridField, + 'CustomAction' . $record->ID . 'Spam', + _t(__CLASS__ . '.SPAM', 'Spam'), + 'spam', + ['RecordID' => $record->ID] + ) + ->addExtraClass(implode(' ', [ + 'btn', + 'btn-secondary', + 'grid-field__icon-action', + 'action-menu--handled', + 'font-icon-cross-mark', + ])) + ->setAttribute('classNames', 'font-icon-cross-mark'); + return (!$record->IsSpam || !$record->Moderated) ? $field : null; + } + + /** + * {@inheritdoc} + */ + public function getActions($gridField) + { + return ['spam']; + } + + /** + * {@inheritdoc} + */ + public function handleAction(GridField $gridField, $actionName, $arguments, $data) + { + /** @var Comment $comment */ + $comment = Comment::get()->byID($arguments['RecordID']); + $comment->markSpam(); + + // output a success message to the user + Controller::curr()->getResponse()->setStatusCode( + 200, + _t(__CLASS__ . '.COMMENTMARKEDSPAM', 'Comment marked as spam.') + ); + } +}