diff --git a/code/Report.php b/code/Report.php index 8c88b6a2..ae1f9636 100644 --- a/code/Report.php +++ b/code/Report.php @@ -87,13 +87,15 @@ class Report extends ViewableData /** * Reports which should not be collected and returned in get_reports + * + * @config * @var array */ - public static $excluded_reports = array( - 'SilverStripe\\Reports\\Report', - 'SilverStripe\\Reports\\ReportWrapper', - 'SilverStripe\\Reports\\SideReportWrapper', - ); + private static $excluded_reports = [ + self::class, + ReportWrapper::class, + SideReportWrapper::class, + ]; /** * Return the title of this report. @@ -205,30 +207,15 @@ class Report extends ViewableData return $sourceRecords->count(); } - /** - * Exclude certain reports classes from the list of Reports in the CMS - * @param $reportClass string|array A string with the Report classname or an array of Report classnames - */ - public static function add_excluded_reports($reportClass) - { - if (is_array($reportClass)) { - self::$excluded_reports = array_merge(self::$excluded_reports, $reportClass); - } else { - if (is_string($reportClass)) { - //add to the excluded reports, so this report doesn't get used - self::$excluded_reports[] = $reportClass; - } - } - } - /** * Return an array of excluded reports. That is, reports that will not be included in * the list of reports in report admin in the CMS. + * * @return array */ public static function get_excluded_reports() { - return self::$excluded_reports; + return (array) self::config()->get('excluded_reports'); } /** @@ -239,22 +226,26 @@ class Report extends ViewableData { $reports = ClassInfo::subclassesFor(get_called_class()); - $reportsArray = array(); + $reportsArray = []; if ($reports && count($reports) > 0) { - //collect reports into array with an attribute for 'sort' + $excludedReports = static::get_excluded_reports(); + // Collect reports into array with an attribute for 'sort' foreach ($reports as $report) { - if (in_array($report, self::$excluded_reports)) { + // Don't use the Report superclass, or any excluded report classes + if (in_array($report, $excludedReports)) { continue; - } //don't use the SS_Report superclass + } $reflectionClass = new ReflectionClass($report); + // Don't use abstract classes if ($reflectionClass->isAbstract()) { continue; - } //don't use abstract classes + } - $reportObj = new $report; - if (method_exists($reportObj, 'sort')) { + $reportObj = $report::create(); + if ($reportObj->hasMethod('sort')) { + // Use the sort method to specify the sort field $reportObj->sort = $reportObj->sort(); - } //use the sort method to specify the sort field + } $reportsArray[$report] = $reportObj; } } diff --git a/code/ReportAdmin.php b/code/ReportAdmin.php index 33ae2925..8173681e 100644 --- a/code/ReportAdmin.php +++ b/code/ReportAdmin.php @@ -37,7 +37,7 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider private static $template_path = null; // defaults to (project)/templates/email - private static $tree_class = 'SilverStripe\\Reports\\Report'; + private static $tree_class = Report::class; private static $url_handlers = array( 'show/$ReportClass/$Action' => 'handleAction' diff --git a/tests/ReportTest.php b/tests/ReportTest.php index a98e82f2..2a04ffb2 100644 --- a/tests/ReportTest.php +++ b/tests/ReportTest.php @@ -2,12 +2,13 @@ namespace SilverStripe\Reports\Tests; -use SilverStripe\Reports\Report; +use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\GridField\GridFieldDataColumns; use SilverStripe\Reports\Tests\ReportTest\FakeObject; use SilverStripe\Reports\Tests\ReportTest\FakeTest; use SilverStripe\Reports\Tests\ReportTest\FakeTest2; +use SilverStripe\Reports\Report; class ReportTest extends SapphireTest { @@ -29,14 +30,14 @@ class ReportTest extends SapphireTest public function testExcludeReport() { $reports = Report::get_reports(); - $reportNames = array(); + $reportNames = []; foreach ($reports as $report) { $reportNames[] = get_class($report); } $this->assertContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is in reports list'); - //exclude one report - Report::add_excluded_reports(FakeTest::class); + // Exclude one report + Config::modify()->merge(Report::class, 'excluded_reports', [FakeTest::class]); $reports = Report::get_reports(); $reportNames = array(); @@ -45,11 +46,14 @@ class ReportTest extends SapphireTest } $this->assertNotContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is NOT in reports list'); - //exclude two reports - Report::add_excluded_reports(array(FakeTest::class, FakeTest2::class)); + // Exclude two reports + Config::modify()->merge(Report::class, 'excluded_reports', [ + FakeTest::class, + FakeTest2::class + ]); $reports = Report::get_reports(); - $reportNames = array(); + $reportNames = []; foreach ($reports as $report) { $reportNames[] = get_class($report); } @@ -105,6 +109,9 @@ class ReportTest extends SapphireTest $page->ID = 959547; $titleContent = $columns->getColumnContent($gridField, $page, 'Title'); - $this->assertEquals('My Object', $titleContent); + $this->assertEquals( + 'My Object', + $titleContent + ); } }