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

View File

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

View File

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

View File

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