Converted to PSR-2

This commit is contained in:
helpfulrobot 2015-12-16 11:06:45 +13:00
parent a09795b3a2
commit 93fced94f1
4 changed files with 796 additions and 705 deletions

View File

@ -29,7 +29,8 @@
*
* @package reports
*/
class SS_Report extends ViewableData {
class SS_Report extends ViewableData
{
/**
* This is the title of the report,
* used by the ReportAdmin templates.
@ -76,7 +77,8 @@ class SS_Report extends ViewableData {
* - overriding description(), which lets you support i18n
* - defining the $description property
*/
public function title() {
public function title()
{
return $this->title;
}
@ -85,7 +87,8 @@ class SS_Report extends ViewableData {
*
* @return string
*/
public function getTitle() {
public function getTitle()
{
return $this->title();
}
@ -96,14 +99,16 @@ class SS_Report extends ViewableData {
* - overriding description(), which lets you support i18n
* - defining the $description property
*/
public function description() {
public function description()
{
return $this->description;
}
/**
* Return the {@link SQLQuery} that provides your report data.
*/
public function sourceQuery($params) {
public function sourceQuery($params)
{
if ($this->hasMethod('sourceRecords')) {
return $this->sourceRecords()->dataQuery();
} else {
@ -114,7 +119,8 @@ class SS_Report extends ViewableData {
/**
* Return a SS_List records for this report.
*/
public function records($params) {
public function records($params)
{
if ($this->hasMethod('sourceRecords')) {
return $this->sourceRecords($params, null, null);
} else {
@ -132,12 +138,14 @@ class SS_Report extends ViewableData {
/**
* Return the data class for this report
*/
public function dataClass() {
public function dataClass()
{
return $this->dataClass;
}
public function getLink($action = null) {
public function getLink($action = null)
{
return Controller::join_links(
'admin/reports/',
"$this->class",
@ -152,9 +160,10 @@ class SS_Report extends ViewableData {
* @param Array $params - any parameters for the sourceRecords
* @return Int
*/
public function getCount($params = array()){
public function getCount($params = array())
{
$sourceRecords = $this->sourceRecords($params, null, null);
if(!$sourceRecords instanceOf SS_List){
if (!$sourceRecords instanceof SS_List) {
user_error($this->class."::sourceRecords does not return an SS_List", E_USER_NOTICE);
return "-1";
}
@ -165,7 +174,8 @@ class SS_Report extends ViewableData {
* Exclude certain reports classes from the list of Reports in the CMS
* @param $reportClass Can be either a string with the report classname or an array of reports classnames
*/
static public function add_excluded_reports($reportClass) {
public static function add_excluded_reports($reportClass)
{
if (is_array($reportClass)) {
self::$excluded_reports = array_merge(self::$excluded_reports, $reportClass);
} else {
@ -181,7 +191,8 @@ class SS_Report extends ViewableData {
* the list of reports in report admin in the CMS.
* @return array
*/
static public function get_excluded_reports() {
public static function get_excluded_reports()
{
return self::$excluded_reports;
}
@ -189,26 +200,36 @@ class SS_Report extends ViewableData {
* Return the SS_Report objects making up the given list.
* @return Array of SS_Report objects
*/
static public function get_reports() {
public static function get_reports()
{
$reports = ClassInfo::subclassesFor(get_called_class());
$reportsArray = array();
if ($reports && count($reports) > 0) {
//collect reports into array with an attribute for 'sort'
foreach ($reports as $report) {
if (in_array($report, self::$excluded_reports)) continue; //don't use the SS_Report superclass
if (in_array($report, self::$excluded_reports)) {
continue;
} //don't use the SS_Report superclass
$reflectionClass = new ReflectionClass($report);
if ($reflectionClass->isAbstract()) continue; //don't use abstract classes
if ($reflectionClass->isAbstract()) {
continue;
} //don't use abstract classes
$reportObj = new $report;
if (method_exists($reportObj,'sort')) $reportObj->sort = $reportObj->sort(); //use the sort method to specify the sort field
if (method_exists($reportObj, 'sort')) {
$reportObj->sort = $reportObj->sort();
} //use the sort method to specify the sort field
$reportsArray[$report] = $reportObj;
}
}
uasort($reportsArray, function ($a, $b) {
if($a->sort == $b->sort) return 0;
else return ($a->sort < $b->sort) ? -1 : 1;
if ($a->sort == $b->sort) {
return 0;
} else {
return ($a->sort < $b->sort) ? -1 : 1;
}
});
return $reportsArray;
@ -227,7 +248,8 @@ class SS_Report extends ViewableData {
*
* @return FieldList
*/
public function getCMSFields() {
public function getCMSFields()
{
$fields = new FieldList();
if ($title = $this->title()) {
@ -258,7 +280,8 @@ class SS_Report extends ViewableData {
return $fields;
}
public function getCMSActions() {
public function getCMSActions()
{
// getCMSActions() can be extended with updateCMSActions() on a extension
$actions = new FieldList();
$this->extend('updateCMSActions', $actions);
@ -274,7 +297,8 @@ class SS_Report extends ViewableData {
*
* @return FormField subclass
*/
public function getReportField() {
public function getReportField()
{
// TODO Remove coupling with global state
$params = isset($_REQUEST['filters']) ? $_REQUEST['filters'] : array();
$items = $this->sourceRecords($params, null, null);
@ -296,11 +320,19 @@ class SS_Report extends ViewableData {
// Parse the column information
foreach ($this->columns() as $source => $info) {
if(is_string($info)) $info = array('title' => $info);
if (is_string($info)) {
$info = array('title' => $info);
}
if(isset($info['formatting'])) $fieldFormatting[$source] = $info['formatting'];
if(isset($info['csvFormatting'])) $csvFieldFormatting[$source] = $info['csvFormatting'];
if(isset($info['casting'])) $fieldCasting[$source] = $info['casting'];
if (isset($info['formatting'])) {
$fieldFormatting[$source] = $info['formatting'];
}
if (isset($info['csvFormatting'])) {
$csvFieldFormatting[$source] = $info['csvFormatting'];
}
if (isset($info['casting'])) {
$fieldCasting[$source] = $info['casting'];
}
if (isset($info['link']) && $info['link']) {
$link = singleton('CMSPageEditController')->Link('show');
@ -320,8 +352,9 @@ class SS_Report extends ViewableData {
* @param Member $member
* @return boolean
*/
public function canView($member = null) {
if(!$member && $member !== FALSE) {
public function canView($member = null)
{
if (!$member && $member !== false) {
$member = Member::currentUser();
}
@ -337,10 +370,10 @@ class SS_Report extends ViewableData {
*
* @return string
*/
public function TreeTitle() {
public function TreeTitle()
{
return $this->title();
}
}
/**
@ -358,30 +391,35 @@ class SS_Report extends ViewableData {
*
* @package reports
*/
abstract class SS_ReportWrapper extends SS_Report {
abstract class SS_ReportWrapper extends SS_Report
{
protected $baseReport;
public function __construct($baseReport) {
public function __construct($baseReport)
{
$this->baseReport = is_string($baseReport) ? new $baseReport : $baseReport;
$this->dataClass = $this->baseReport->dataClass();
parent::__construct();
}
public function ID() {
public function ID()
{
return get_class($this->baseReport) . '_' . get_class($this);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Filtering
public function parameterFields() {
public function parameterFields()
{
return $this->baseReport->parameterFields();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Columns
public function columns() {
public function columns()
{
return $this->baseReport->columns();
}
@ -391,32 +429,34 @@ abstract class SS_ReportWrapper extends SS_Report {
/**
* Override this method to perform some actions prior to querying.
*/
public function beforeQuery($params) {
public function beforeQuery($params)
{
}
/**
* Override this method to perform some actions after querying.
*/
public function afterQuery() {}
public function afterQuery()
{
}
public function sourceQuery($params) {
public function sourceQuery($params)
{
if ($this->baseReport->hasMethod('sourceRecords')) {
// The default implementation will create a fake query from our sourceRecords() method
return parent::sourceQuery($params);
} elseif ($this->baseReport->hasMethod('sourceQuery')) {
$this->beforeQuery($params);
$query = $this->baseReport->sourceQuery($params);
$this->afterQuery();
return $query;
} else {
user_error("Please override sourceQuery()/sourceRecords() and columns() in your base report", E_USER_ERROR);
}
}
public function sourceRecords($params = array(), $sort = null, $limit = null) {
public function sourceRecords($params = array(), $sort = null, $limit = null)
{
$this->beforeQuery($params);
$records = $this->baseReport->sourceRecords($params, $sort, $limit);
$this->afterQuery();
@ -427,23 +467,28 @@ abstract class SS_ReportWrapper extends SS_Report {
///////////////////////////////////////////////////////////////////////////////////////////
// Pass-through
public function title() {
public function title()
{
return $this->baseReport->title();
}
public function group() {
public function group()
{
return $this->baseReport->hasMethod('group') ? $this->baseReport->group() : 'Group';
}
public function sort() {
public function sort()
{
return $this->baseReport->hasMethod('sort') ? $this->baseReport->sort() : 0;
}
public function description() {
public function description()
{
return $this->baseReport->description();
}
public function canView($member = null) {
public function canView($member = null)
{
return $this->baseReport->canView($member);
}
}

View File

@ -10,7 +10,8 @@
*
* @package reports
*/
class ReportAdmin extends LeftAndMain implements PermissionProvider {
class ReportAdmin extends LeftAndMain implements PermissionProvider
{
private static $url_segment = 'reports';
@ -36,7 +37,8 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
protected $reportObject;
public function init() {
public function init()
{
parent::init();
//set the report we are currently viewing from the URL
@ -64,14 +66,21 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
* @param Member $member
* @return boolean
*/
public function canView($member = null) {
if(!$member && $member !== FALSE) $member = Member::currentUser();
public function canView($member = null)
{
if (!$member && $member !== false) {
$member = Member::currentUser();
}
if(!parent::canView($member)) return false;
if (!parent::canView($member)) {
return false;
}
$hasViewableSubclasses = false;
foreach ($this->Reports() as $report) {
if($report->canView($member)) return true;
if ($report->canView($member)) {
return true;
}
}
return false;
@ -83,10 +92,13 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
*
* @return SS_List
*/
public function Reports() {
public function Reports()
{
$output = new ArrayList();
foreach (SS_Report::get_reports() as $report) {
if($report->canView()) $output->push($report);
if ($report->canView()) {
$output->push($report);
}
}
return $output;
}
@ -102,7 +114,8 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
*
* @return boolean
*/
public static function has_reports() {
public static function has_reports()
{
return sizeof(SS_Report::get_reports()) > 0;
}
@ -110,7 +123,8 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
* Returns the Breadcrumbs for the ReportAdmin
* @return ArrayList
*/
public function Breadcrumbs($unlinked = false) {
public function Breadcrumbs($unlinked = false)
{
$items = parent::Breadcrumbs($unlinked);
// The root element should explicitly point to the root node.
@ -132,7 +146,8 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
* Returns the link to the report admin section, or the specific report that is currently displayed
* @return String
*/
public function Link($action = null) {
public function Link($action = null)
{
if ($this->reportObject) {
$link = $this->reportObject->getLink($action);
} else {
@ -141,7 +156,8 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
return $link;
}
public function providePermissions() {
public function providePermissions()
{
$title = _t("ReportAdmin.MENUTITLE", LeftAndMain::menu_title_for_class($this->class));
return array(
"CMS_ACCESS_ReportAdmin" => array(
@ -151,7 +167,8 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider {
);
}
public function getEditForm($id = null, $fields = null) {
public function getEditForm($id = null, $fields = null)
{
$report = $this->reportObject;
if ($report) {
$fields = $report->getCMSFields();

View File

@ -5,30 +5,36 @@
*
* @package reports
*/
class SideReportView extends ViewableData {
class SideReportView extends ViewableData
{
protected $controller, $report;
protected $parameters;
public function __construct($controller, $report) {
public function __construct($controller, $report)
{
$this->controller = $controller;
$this->report = $report;
parent::__construct();
}
public function group() {
public function group()
{
return _t('SideReport.OtherGroupTitle', "Other");
}
public function sort() {
public function sort()
{
return 0;
}
public function setParameters($parameters) {
public function setParameters($parameters)
{
$this->parameters = $parameters;
}
public function forTemplate() {
public function forTemplate()
{
$records = $this->report->records($this->parameters);
$columns = $this->report->columns();
@ -38,7 +44,9 @@ class SideReportView extends ViewableData {
foreach ($records as $record) {
$result .= "<li>\n";
foreach ($columns as $source => $info) {
if(is_string($info)) $info = array('title' => $info);
if (is_string($info)) {
$info = array('title' => $info);
}
$result .= $this->formatValue($record, $source, $info);
}
$result .= "\n</li>\n";
@ -56,7 +64,8 @@ class SideReportView extends ViewableData {
return $result;
}
protected function formatValue($record, $source, $info) {
protected function formatValue($record, $source, $info)
{
// Field sources
//if(is_string($source)) {
$val = Convert::raw2xml($record->$source);
@ -104,8 +113,10 @@ class SideReportView extends ViewableData {
*
* @package reports
*/
class SideReportWrapper extends SS_ReportWrapper {
public function columns() {
class SideReportWrapper extends SS_ReportWrapper
{
public function columns()
{
if ($this->baseReport->hasMethod('sideReportColumns')) {
return $this->baseReport->sideReportColumns();
} else {

View File

@ -4,9 +4,11 @@
* @package reports
* @subpackage tests
*/
class ReportTest extends SapphireTest {
class ReportTest extends SapphireTest
{
public function testGetReports() {
public function testGetReports()
{
$reports = SS_Report::get_reports();
$this->assertNotNull($reports, "Reports returned");
$previousSort = 0;
@ -16,7 +18,8 @@ class ReportTest extends SapphireTest {
}
}
public function testExcludeReport() {
public function testExcludeReport()
{
$reports = SS_Report::get_reports();
$reportNames = array();
foreach ($reports as $report) {
@ -46,7 +49,8 @@ class ReportTest extends SapphireTest {
$this->assertNotContains('ReportTest_FakeTest2', $reportNames, 'ReportTest_FakeTest2 is NOT in reports list');
}
public function testAbstractClassesAreExcluded() {
public function testAbstractClassesAreExcluded()
{
$reports = SS_Report::get_reports();
$reportNames = array();
foreach ($reports as $report) {
@ -62,22 +66,27 @@ class ReportTest extends SapphireTest {
* @package reports
* @subpackage tests
*/
class ReportTest_FakeTest extends SS_Report implements TestOnly {
public function title() {
class ReportTest_FakeTest extends SS_Report implements TestOnly
{
public function title()
{
return 'Report title';
}
public function columns() {
public function columns()
{
return array(
"Title" => array(
"title" => "Page Title"
)
);
}
public function sourceRecords($params, $sort, $limit) {
public function sourceRecords($params, $sort, $limit)
{
return new ArrayList();
}
public function sort() {
public function sort()
{
return 100;
}
}
@ -86,22 +95,27 @@ class ReportTest_FakeTest extends SS_Report implements TestOnly {
* @package reports
* @subpackage tests
*/
class ReportTest_FakeTest2 extends SS_Report implements TestOnly {
public function title() {
class ReportTest_FakeTest2 extends SS_Report implements TestOnly
{
public function title()
{
return 'Report title 2';
}
public function columns() {
public function columns()
{
return array(
"Title" => array(
"title" => "Page Title 2"
)
);
}
public function sourceRecords($params, $sort, $limit) {
public function sourceRecords($params, $sort, $limit)
{
return new ArrayList();
}
public function sort() {
public function sort()
{
return 98;
}
}
@ -110,25 +124,29 @@ class ReportTest_FakeTest2 extends SS_Report implements TestOnly {
* @package reports
* @subpackage tests
*/
abstract class ReportTest_FakeTest_Abstract extends SS_Report implements TestOnly {
abstract class ReportTest_FakeTest_Abstract extends SS_Report implements TestOnly
{
public function title() {
public function title()
{
return 'Report title Abstract';
}
public function columns() {
public function columns()
{
return array(
"Title" => array(
"title" => "Page Title Abstract"
)
);
}
public function sourceRecords($params, $sort, $limit) {
public function sourceRecords($params, $sort, $limit)
{
return new ArrayList();
}
public function sort() {
public function sort()
{
return 5;
}
}