API Remove Report::add_excluded_report() and make excluded_reports configurable

This commit is contained in:
Robbie Averill 2017-07-16 16:36:12 +12:00
parent bf4e7224e0
commit f4af1fab77
3 changed files with 37 additions and 39 deletions

View File

@ -87,13 +87,15 @@ class Report extends ViewableData
/** /**
* Reports which should not be collected and returned in get_reports * Reports which should not be collected and returned in get_reports
*
* @config
* @var array * @var array
*/ */
public static $excluded_reports = array( private static $excluded_reports = [
'SilverStripe\\Reports\\Report', self::class,
'SilverStripe\\Reports\\ReportWrapper', ReportWrapper::class,
'SilverStripe\\Reports\\SideReportWrapper', SideReportWrapper::class,
); ];
/** /**
* Return the title of this report. * Return the title of this report.
@ -205,30 +207,15 @@ class Report extends ViewableData
return $sourceRecords->count(); 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 * 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. * the list of reports in report admin in the CMS.
*
* @return array * @return array
*/ */
public static function get_excluded_reports() 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()); $reports = ClassInfo::subclassesFor(get_called_class());
$reportsArray = array(); $reportsArray = [];
if ($reports && count($reports) > 0) { 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) { 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; continue;
} //don't use the SS_Report superclass }
$reflectionClass = new ReflectionClass($report); $reflectionClass = new ReflectionClass($report);
// Don't use abstract classes
if ($reflectionClass->isAbstract()) { if ($reflectionClass->isAbstract()) {
continue; continue;
} //don't use abstract classes }
$reportObj = new $report; $reportObj = $report::create();
if (method_exists($reportObj, 'sort')) { if ($reportObj->hasMethod('sort')) {
// Use the sort method to specify the sort field
$reportObj->sort = $reportObj->sort(); $reportObj->sort = $reportObj->sort();
} //use the sort method to specify the sort field }
$reportsArray[$report] = $reportObj; $reportsArray[$report] = $reportObj;
} }
} }

View File

@ -37,7 +37,7 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider
private static $template_path = null; // defaults to (project)/templates/email 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( private static $url_handlers = array(
'show/$ReportClass/$Action' => 'handleAction' 'show/$ReportClass/$Action' => 'handleAction'

View File

@ -2,12 +2,13 @@
namespace SilverStripe\Reports\Tests; namespace SilverStripe\Reports\Tests;
use SilverStripe\Reports\Report; use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\GridField\GridFieldDataColumns; use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Reports\Tests\ReportTest\FakeObject; use SilverStripe\Reports\Tests\ReportTest\FakeObject;
use SilverStripe\Reports\Tests\ReportTest\FakeTest; use SilverStripe\Reports\Tests\ReportTest\FakeTest;
use SilverStripe\Reports\Tests\ReportTest\FakeTest2; use SilverStripe\Reports\Tests\ReportTest\FakeTest2;
use SilverStripe\Reports\Report;
class ReportTest extends SapphireTest class ReportTest extends SapphireTest
{ {
@ -29,14 +30,14 @@ class ReportTest extends SapphireTest
public function testExcludeReport() public function testExcludeReport()
{ {
$reports = Report::get_reports(); $reports = Report::get_reports();
$reportNames = array(); $reportNames = [];
foreach ($reports as $report) { foreach ($reports as $report) {
$reportNames[] = get_class($report); $reportNames[] = get_class($report);
} }
$this->assertContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is in reports list'); $this->assertContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is in reports list');
//exclude one report // Exclude one report
Report::add_excluded_reports(FakeTest::class); Config::modify()->merge(Report::class, 'excluded_reports', [FakeTest::class]);
$reports = Report::get_reports(); $reports = Report::get_reports();
$reportNames = array(); $reportNames = array();
@ -45,11 +46,14 @@ class ReportTest extends SapphireTest
} }
$this->assertNotContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is NOT in reports list'); $this->assertNotContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is NOT in reports list');
//exclude two reports // Exclude two reports
Report::add_excluded_reports(array(FakeTest::class, FakeTest2::class)); Config::modify()->merge(Report::class, 'excluded_reports', [
FakeTest::class,
FakeTest2::class
]);
$reports = Report::get_reports(); $reports = Report::get_reports();
$reportNames = array(); $reportNames = [];
foreach ($reports as $report) { foreach ($reports as $report) {
$reportNames[] = get_class($report); $reportNames[] = get_class($report);
} }
@ -105,6 +109,9 @@ class ReportTest extends SapphireTest
$page->ID = 959547; $page->ID = 959547;
$titleContent = $columns->getColumnContent($gridField, $page, 'Title'); $titleContent = $columns->getColumnContent($gridField, $page, 'Title');
$this->assertEquals('<a class="grid-field__link-block" href="dummy-edit-link/959547" title="My Object">My Object</a>', $titleContent); $this->assertEquals(
'<a class="grid-field__link-block" href="dummy-edit-link/959547" title="My Object">My Object</a>',
$titleContent
);
} }
} }