From b6a2c608acd105f73382033647c6749cd83f996a Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Fri, 25 Jan 2019 15:47:38 +1300 Subject: [PATCH 1/5] 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.') + ); + } +} From c4cfc446433d2f6f831e028e0a2acd424212b87e Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Sun, 10 Feb 2019 07:01:57 +0300 Subject: [PATCH 2/5] Update Scrutinizer configuration to use new build engine --- .scrutinizer.yml | 68 +++++------------------------------------------- 1 file changed, 7 insertions(+), 61 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index af8f18e..051ef9a 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,69 +1,15 @@ inherit: true +build: + nodes: + analysis: + tests: + override: [php-scrutinizer-run] + checks: php: - verify_property_names: true - verify_argument_usable_as_reference: true - verify_access_scope_valid: true - useless_calls: true - use_statement_alias_conflict: true - variable_existence: true - unused_variables: true - unused_properties: true - unused_parameters: true - unused_methods: true - unreachable_code: true - too_many_arguments: true - sql_injection_vulnerabilities: true - simplify_boolean_return: true - side_effects_or_types: true - security_vulnerabilities: true - return_doc_comments: true - return_doc_comment_if_not_inferrable: true - require_scope_for_properties: true - require_scope_for_methods: true - require_php_tag_first: true - psr2_switch_declaration: true - psr2_class_declaration: true - property_assignments: true - prefer_while_loop_over_for_loop: true - precedence_mistakes: true - precedence_in_conditions: true - phpunit_assertions: true - php5_style_constructor: true - parse_doc_comments: true - parameter_non_unique: true - parameter_doc_comments: true - param_doc_comment_if_not_inferrable: true - optional_parameters_at_the_end: true - one_class_per_file: true - no_unnecessary_if: true - no_trailing_whitespace: true - no_property_on_interface: true - no_non_implemented_abstract_methods: true - no_error_suppression: true - no_duplicate_arguments: true - no_commented_out_code: true - newline_at_end_of_file: true - missing_arguments: true - method_calls_on_non_object: true - instanceof_class_exists: true - foreach_traversable: true - fix_line_ending: true - fix_doc_comments: true - duplication: true - deprecated_code_usage: true - deadlock_detection_in_loops: true code_rating: true - closure_use_not_conflicting: true - catch_class_exists: true - blank_line_after_namespace_declaration: false - avoid_multiple_statements_on_same_line: true - avoid_duplicate_types: true - avoid_conflicting_incrementers: true - avoid_closing_tag: true - assignment_of_null_return: true - argument_type_checks: true + duplication: true filter: paths: [src/*, tests/*] From 18fbdfc94b37d99559ecd11d7b5661c3e18d8117 Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Mon, 11 Feb 2019 09:19:18 +1300 Subject: [PATCH 3/5] Update src/Admin/CommentsGridFieldApproveAction.php Co-Authored-By: NightJar --- src/Admin/CommentsGridFieldApproveAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin/CommentsGridFieldApproveAction.php b/src/Admin/CommentsGridFieldApproveAction.php index e65ff74..1979109 100644 --- a/src/Admin/CommentsGridFieldApproveAction.php +++ b/src/Admin/CommentsGridFieldApproveAction.php @@ -91,7 +91,7 @@ class CommentsGridFieldApproveAction implements /** * Returns the FormAction object, used by other methods to get properties * - * @return GridField_FormAction + * @return GridField_FormAction|null */ public function getApproveAction($gridField, $record, $columnName) { From 7d60fc2fe2e990d532dadc98a6bf94c1cd0dad48 Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Mon, 11 Feb 2019 09:19:31 +1300 Subject: [PATCH 4/5] Update src/Admin/CommentsGridFieldSpamAction.php Co-Authored-By: NightJar --- src/Admin/CommentsGridFieldSpamAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin/CommentsGridFieldSpamAction.php b/src/Admin/CommentsGridFieldSpamAction.php index 4cbbba6..cc8761c 100644 --- a/src/Admin/CommentsGridFieldSpamAction.php +++ b/src/Admin/CommentsGridFieldSpamAction.php @@ -93,7 +93,7 @@ class CommentsGridFieldSpamAction implements /** * Returns the FormAction object, used by other methods to get properties * - * @return GridField_FormAction + * @return GridField_FormAction|null */ public function getSpamAction($gridField, $record, $columnName) { From 647c634dc496121468e11f329cabd14f5b4554b9 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 11 Feb 2019 09:22:02 +1300 Subject: [PATCH 5/5] Update .travis.yml Co-Authored-By: NightJar --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index efa691e..f7f6a7e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,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.x-dev; fi + - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.2.x-dev; fi - composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile script: