From 97c03dbba547f1c049e4a7a7e848f9def929f763 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Fri, 11 Oct 2024 14:45:26 +1300 Subject: [PATCH 1/2] ENH Reports list filtering and pagination. --- code/ReportAdmin.php | 70 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/code/ReportAdmin.php b/code/ReportAdmin.php index 397037ed..46f2a05d 100644 --- a/code/ReportAdmin.php +++ b/code/ReportAdmin.php @@ -7,12 +7,19 @@ use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\GridField\GridField; +use SilverStripe\Forms\GridField\GridFieldButtonRow; use SilverStripe\Forms\GridField\GridFieldConfig; use SilverStripe\Forms\GridField\GridFieldDataColumns; -use SilverStripe\Forms\GridField\GridFieldFooter; +use SilverStripe\Forms\GridField\GridFieldFilterHeader; +use SilverStripe\Forms\GridField\GridFieldPageCount; +use SilverStripe\Forms\GridField\GridFieldPaginator; use SilverStripe\Forms\GridField\GridFieldSortableHeader; +use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig; +use SilverStripe\Forms\TextField; use SilverStripe\ORM\ArrayList; +use SilverStripe\ORM\Filters\PartialMatchFilter; +use SilverStripe\ORM\Search\BasicSearchContext; use SilverStripe\ORM\SS_List; use SilverStripe\Security\Member; use SilverStripe\Security\PermissionProvider; @@ -104,13 +111,18 @@ public function canView($member = null) */ public function Reports() { - $output = new ArrayList(); + $output = ArrayList::create(); foreach (Report::get_reports() as $report) { if ($report->canView()) { $output->push($report); } } - return $output; + + return $output + // Provide a good default for the reports order, otherwise this will be using sort by namespace + ->sort('Title', 'ASC') + // Provide a good default for the data class, otherwise this will use the class of the first item in the list + ->setDataClass(Report::class); } public function handleAction($request, $action) @@ -227,28 +239,60 @@ public function getEditForm($id = null, $fields = null) $fields = $report->getCMSFields(); } else { // List all reports - $fields = new FieldList(); + $fields = FieldList::create(); $gridFieldConfig = GridFieldConfig::create()->addComponents( + // This is a container component that is required by filter header component + GridFieldButtonRow::create('before'), + $filterHeader = GridFieldFilterHeader::create(), GridFieldSortableHeader::create(), - GridFieldDataColumns::create(), - GridFieldFooter::create() + $columns = GridFieldDataColumns::create(), + GridFieldPageCount::create(), + GridFieldPaginator::create() ); + + // Configure the filter header filter search form + $generalField = BasicSearchContext::config()->get('general_search_field_name'); + $searchFieldList = FieldList::create([ + HiddenField::create($generalField), + TextField::create('Title'), + TextField::create('Description'), + ]); + + $searchContext = BasicSearchContext::create(Report::class); + $searchContext->setFields($searchFieldList); + + // Setup filter configuration - partial match with case-insensitive modifier + $filters = [ + 'Title', + 'Description', + ]; + + foreach ($filters as $fieldName) { + $fieldFilter = PartialMatchFilter::create($fieldName); + $fieldFilter->setModifiers([ + // We want case-insensitive match to be consistent with other areas of the CMS + 'nocase', + ]); + $searchContext->addFilter($fieldFilter); + } + + $filterHeader->setSearchContext($searchContext); + $gridField = GridField::create('Reports', false, $this->Reports(), $gridFieldConfig); - $columns = $gridField->getConfig() - ->getComponentByType(GridFieldDataColumns::class); $columns->setDisplayFields(array( 'title' => _t('SilverStripe\\Reports\\ReportAdmin.ReportTitle', 'Title'), + 'description' => _t('SilverStripe\\Reports\\ReportAdmin.ReportDescription', 'Description'), )); - $columns->setFieldFormatting(array( - 'title' => '$value ($CountForOverview)' - )); + $columns->setFieldFormatting([ + 'title' => '$value ($CountForOverview)' + ]); $gridField->addExtraClass('all-reports-gridfield'); $fields->push($gridField); } - $actions = new FieldList(); - $form = new Form($this, "EditForm", $fields, $actions); + $actions = FieldList::create(); + $form = Form::create($this, "EditForm", $fields, $actions); $form->addExtraClass( 'panel panel--padded panel--scrollable cms-edit-form cms-panel-padded' . $this->BaseCSSClasses() ); From 556cb3663879845d3ae454f74bfdf32399b847e5 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Mon, 14 Oct 2024 13:37:13 +1300 Subject: [PATCH 2/2] PR fixes. --- code/ReportAdmin.php | 20 +++++++------------- composer.json | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/code/ReportAdmin.php b/code/ReportAdmin.php index 46f2a05d..8bd38099 100644 --- a/code/ReportAdmin.php +++ b/code/ReportAdmin.php @@ -119,9 +119,7 @@ public function Reports() } return $output - // Provide a good default for the reports order, otherwise this will be using sort by namespace ->sort('Title', 'ASC') - // Provide a good default for the data class, otherwise this will use the class of the first item in the list ->setDataClass(Report::class); } @@ -250,14 +248,16 @@ public function getEditForm($id = null, $fields = null) GridFieldPaginator::create() ); + $titleLabel = _t('SilverStripe\\Reports\\ReportAdmin.ReportTitle', 'Title'); + $descriptionLabel = _t('SilverStripe\\Reports\\ReportAdmin.ReportDescription', 'Description'); + // Configure the filter header filter search form $generalField = BasicSearchContext::config()->get('general_search_field_name'); $searchFieldList = FieldList::create([ HiddenField::create($generalField), - TextField::create('Title'), - TextField::create('Description'), + TextField::create('Title', $titleLabel), + TextField::create('Description', $descriptionLabel), ]); - $searchContext = BasicSearchContext::create(Report::class); $searchContext->setFields($searchFieldList); @@ -266,22 +266,16 @@ public function getEditForm($id = null, $fields = null) 'Title', 'Description', ]; - foreach ($filters as $fieldName) { $fieldFilter = PartialMatchFilter::create($fieldName); - $fieldFilter->setModifiers([ - // We want case-insensitive match to be consistent with other areas of the CMS - 'nocase', - ]); $searchContext->addFilter($fieldFilter); } - $filterHeader->setSearchContext($searchContext); $gridField = GridField::create('Reports', false, $this->Reports(), $gridFieldConfig); $columns->setDisplayFields(array( - 'title' => _t('SilverStripe\\Reports\\ReportAdmin.ReportTitle', 'Title'), - 'description' => _t('SilverStripe\\Reports\\ReportAdmin.ReportDescription', 'Description'), + 'title' => $titleLabel, + 'description' => $descriptionLabel, )); $columns->setFieldFormatting([ diff --git a/composer.json b/composer.json index 4f009e31..c6927d20 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ ], "require": { "php": "^8.1", - "silverstripe/framework": "^5", + "silverstripe/framework": "^5.2", "silverstripe/admin": "^2", "silverstripe/versioned": "^2", "silverstripe/config": "^2",