ENHANCEMENT: Refactored to handling of valid report-classes in ReportAdmin. Added new method to retrieve all valid report-class names (getReportClassNames) and updated 'canView', 'Reports', 'EditForm' and 'has_reports'.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@80034 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Rainer Spittel 2009-06-25 05:14:25 +00:00
parent 5cfcf03d11
commit e46075d3fb

View File

@ -37,6 +37,37 @@ class ReportAdmin extends LeftAndMain {
Requirements::block(SAPPHIRE_DIR . '/javascript/HtmlEditorField.js'); Requirements::block(SAPPHIRE_DIR . '/javascript/HtmlEditorField.js');
} }
/**
* Returns an array of all the reports classnames that could be shown on this site
* to any user. Base class is not included in the response.
* It does not perform filtering based on canView().
*
* @return An array of report class-names, i.e.:
* array("AllOrdersReport","CurrentOrdersReport","UnprintedOrderReport")
*/
public function getReportClassNames() {
$baseClass = 'SSReport';
$response = array();
// get all sub-classnames (incl. base classname).
$classNames = ClassInfo::subclassesFor( $baseClass );
// drop base className
$classNames = array_diff($classNames, array($baseClass));
// drop report classes, which are not initiatable.
foreach($classNames as $className) {
// Remove abstract classes
$classReflection = new ReflectionClass($className);
if($classReflection->isInstantiable() ) {
$response[] = $className;
}
}
return $response;
}
/** /**
* Does the parent permission checks, but also * Does the parent permission checks, but also
* makes sure that instantiatable subclasses of * makes sure that instantiatable subclasses of
@ -54,15 +85,15 @@ class ReportAdmin extends LeftAndMain {
if(!parent::canView($member)) return false; if(!parent::canView($member)) return false;
$hasViewableSubclasses = false; $hasViewableSubclasses = false;
$subClasses = array_values(ClassInfo::subclassesFor('SSReport')); $subClasses = array_values( $this->getReportClassNames() );
foreach($subClasses as $subclass) { foreach($subClasses as $subclass) {
// Remove abstract classes and LeftAndMain
$classReflection = new ReflectionClass($subclass); if(singleton($subclass)->canView()) {
if($classReflection->isInstantiable() && $subclass != 'SSReport') { $hasViewableSubclasses = true;
if(singleton($subclass)->canView()) $hasViewableSubclasses = true; }
}
} }
return $hasViewableSubclasses; return $hasViewableSubclasses;
} }
@ -74,13 +105,11 @@ class ReportAdmin extends LeftAndMain {
*/ */
public function Reports() { public function Reports() {
$processedReports = array(); $processedReports = array();
$subClasses = ClassInfo::subclassesFor('SSReport'); $subClasses = $this->getReportClassNames();
if($subClasses) { if($subClasses) {
foreach($subClasses as $subClass) { foreach($subClasses as $subClass) {
if($subClass != 'SSReport') { $processedReports[] = new $subClass();
$processedReports[] = new $subClass();
}
} }
} }
@ -134,14 +163,12 @@ class ReportAdmin extends LeftAndMain {
public function EditForm() { public function EditForm() {
$ids = array(); $ids = array();
$id = isset($_REQUEST['ID']) ? $_REQUEST['ID'] : Session::get('currentPage'); $id = isset($_REQUEST['ID']) ? $_REQUEST['ID'] : Session::get('currentPage');
$subClasses = ClassInfo::subclassesFor('SSReport'); $subClasses = $this->getReportClassNames();
if($subClasses) { if($subClasses) {
foreach($subClasses as $subClass) { foreach($subClasses as $subClass) {
if($subClass != 'SSReport') { $obj = new $subClass();
$obj = new $subClass(); $ids[] = $obj->ID();
$ids[] = $obj->ID();
}
} }
} }
@ -192,16 +219,12 @@ class ReportAdmin extends LeftAndMain {
* @return boolean * @return boolean
*/ */
public static function has_reports() { public static function has_reports() {
$subClasses = ClassInfo::subclassesFor('SSReport'); $subClasses = $this->getReportClassNames();
if($subClasses) { if($subClasses) {
foreach($subClasses as $subClass) { foreach($subClasses as $subClass) {
if($subClass != 'SSReport') { return true;
return true;
}
} }
} }
return false; return false;
} }
} }