mirror of
https://github.com/silverstripe/silverstripe-reports
synced 2024-10-22 11:05:53 +02:00
Merge pull request #88 from creative-commoners/pulls/dynamic-link
NEW Add support for callable link formatting
This commit is contained in:
commit
a9db6dee3a
@ -3,11 +3,15 @@
|
|||||||
namespace SilverStripe\Reports;
|
namespace SilverStripe\Reports;
|
||||||
|
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
use SilverStripe\CMS\Model\SiteTree;
|
||||||
use SilverStripe\Control\Controller;
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Core\ClassInfo;
|
use SilverStripe\Core\ClassInfo;
|
||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Core\Convert;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\Forms\FieldList;
|
use SilverStripe\Forms\FieldList;
|
||||||
use SilverStripe\Forms\FormAction;
|
use SilverStripe\Forms\FormAction;
|
||||||
|
use SilverStripe\Forms\FormField;
|
||||||
use SilverStripe\Forms\GridField\GridField;
|
use SilverStripe\Forms\GridField\GridField;
|
||||||
use SilverStripe\Forms\GridField\GridFieldButtonRow;
|
use SilverStripe\Forms\GridField\GridFieldButtonRow;
|
||||||
use SilverStripe\Forms\GridField\GridFieldConfig;
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
||||||
@ -20,6 +24,8 @@ use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
|||||||
use SilverStripe\Forms\LiteralField;
|
use SilverStripe\Forms\LiteralField;
|
||||||
use SilverStripe\ORM\ArrayList;
|
use SilverStripe\ORM\ArrayList;
|
||||||
use SilverStripe\ORM\CMSPreviewable;
|
use SilverStripe\ORM\CMSPreviewable;
|
||||||
|
use SilverStripe\ORM\DataList;
|
||||||
|
use SilverStripe\ORM\DataQuery;
|
||||||
use SilverStripe\ORM\SS_List;
|
use SilverStripe\ORM\SS_List;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use SilverStripe\Security\Permission;
|
use SilverStripe\Security\Permission;
|
||||||
@ -53,6 +59,8 @@ use SilverStripe\View\ViewableData;
|
|||||||
*
|
*
|
||||||
* Right now, all subclasses of SS_Report will be shown in the ReportAdmin. In SS3 there is only
|
* Right now, all subclasses of SS_Report will be shown in the ReportAdmin. In SS3 there is only
|
||||||
* one place where reports can go, so this class is greatly simplifed from its version in SS2.
|
* one place where reports can go, so this class is greatly simplifed from its version in SS2.
|
||||||
|
*
|
||||||
|
* @method SS_List|DataList sourceRecords($params = [], $sort = null, $limit = null) List of records to show for this report
|
||||||
*/
|
*/
|
||||||
class Report extends ViewableData
|
class Report extends ViewableData
|
||||||
{
|
{
|
||||||
@ -77,7 +85,7 @@ class Report extends ViewableData
|
|||||||
* The class of object being managed by this report.
|
* The class of object being managed by this report.
|
||||||
* Set by overriding in your subclass.
|
* Set by overriding in your subclass.
|
||||||
*/
|
*/
|
||||||
protected $dataClass = 'SilverStripe\\CMS\\Model\\SiteTree';
|
protected $dataClass = SiteTree::class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A field that specifies the sort order of this report
|
* A field that specifies the sort order of this report
|
||||||
@ -132,7 +140,10 @@ class Report extends ViewableData
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the {@link SilverStripe\ORM\Queries\SQLSelect} that provides your report data.
|
* Return the {@link DataQuery} that provides your report data.
|
||||||
|
*
|
||||||
|
* @param array $params
|
||||||
|
* @return DataQuery
|
||||||
*/
|
*/
|
||||||
public function sourceQuery($params)
|
public function sourceQuery($params)
|
||||||
{
|
{
|
||||||
@ -145,6 +156,9 @@ class Report extends ViewableData
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a SS_List records for this report.
|
* Return a SS_List records for this report.
|
||||||
|
*
|
||||||
|
* @param array $params
|
||||||
|
* @return SS_List
|
||||||
*/
|
*/
|
||||||
public function records($params)
|
public function records($params)
|
||||||
{
|
{
|
||||||
@ -162,6 +176,11 @@ class Report extends ViewableData
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function columns()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the data class for this report
|
* Return the data class for this report
|
||||||
*/
|
*/
|
||||||
@ -220,7 +239,8 @@ class 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 Report[] Array of Report objects
|
||||||
*/
|
*/
|
||||||
public static function get_reports()
|
public static function get_reports()
|
||||||
{
|
{
|
||||||
@ -241,6 +261,7 @@ class Report extends ViewableData
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var Report $reportObj */
|
||||||
$reportObj = $report::create();
|
$reportObj = $report::create();
|
||||||
if ($reportObj->hasMethod('sort')) {
|
if ($reportObj->hasMethod('sort')) {
|
||||||
// Use the sort method to specify the sort field
|
// Use the sort method to specify the sort field
|
||||||
@ -284,6 +305,7 @@ class Report extends ViewableData
|
|||||||
|
|
||||||
// Add search fields is available
|
// Add search fields is available
|
||||||
if ($this->hasMethod('parameterFields') && $parameterFields = $this->parameterFields()) {
|
if ($this->hasMethod('parameterFields') && $parameterFields = $this->parameterFields()) {
|
||||||
|
/** @var FormField $field */
|
||||||
foreach ($parameterFields as $field) {
|
foreach ($parameterFields as $field) {
|
||||||
// Namespace fields for easier handling in form submissions
|
// Namespace fields for easier handling in form submissions
|
||||||
$field->setName(sprintf('filters[%s]', $field->getName()));
|
$field->setName(sprintf('filters[%s]', $field->getName()));
|
||||||
@ -324,8 +346,12 @@ class Report extends ViewableData
|
|||||||
*/
|
*/
|
||||||
public function getReportField()
|
public function getReportField()
|
||||||
{
|
{
|
||||||
// TODO Remove coupling with global state
|
$params = [];
|
||||||
$params = isset($_REQUEST['filters']) ? $_REQUEST['filters'] : array();
|
if (Injector::inst()->has(HTTPRequest::class)) {
|
||||||
|
/** @var HTTPRequest $request */
|
||||||
|
$request = Injector::inst()->get(HTTPRequest::class);
|
||||||
|
$params = $request->param('filters') ?: [];
|
||||||
|
}
|
||||||
$items = $this->sourceRecords($params, null, null);
|
$items = $this->sourceRecords($params, null, null);
|
||||||
|
|
||||||
$gridFieldConfig = GridFieldConfig::create()->addComponents(
|
$gridFieldConfig = GridFieldConfig::create()->addComponents(
|
||||||
@ -339,15 +365,15 @@ class Report extends ViewableData
|
|||||||
new GridFieldPaginator()
|
new GridFieldPaginator()
|
||||||
);
|
);
|
||||||
$gridField = new GridField('Report', null, $items, $gridFieldConfig);
|
$gridField = new GridField('Report', null, $items, $gridFieldConfig);
|
||||||
$columns = $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns');
|
$columns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);
|
||||||
$displayFields = array();
|
$displayFields = [];
|
||||||
$fieldCasting = array();
|
$fieldCasting = [];
|
||||||
$fieldFormatting = array();
|
$fieldFormatting = [];
|
||||||
|
|
||||||
// Parse the column information
|
// Parse the column information
|
||||||
foreach ($this->columns() as $source => $info) {
|
foreach ($this->columns() as $source => $info) {
|
||||||
if (is_string($info)) {
|
if (is_string($info)) {
|
||||||
$info = array('title' => $info);
|
$info = ['title' => $info];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($info['formatting'])) {
|
if (isset($info['formatting'])) {
|
||||||
@ -361,18 +387,22 @@ class Report extends ViewableData
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isset($info['link']) && $info['link']) {
|
if (isset($info['link']) && $info['link']) {
|
||||||
$fieldFormatting[$source] = function ($value, $item) {
|
if (is_callable($info['link'])) {
|
||||||
if ($item instanceof CMSPreviewable) {
|
$fieldFormatting[$source] = $info['link'];
|
||||||
/** @var CMSPreviewable $item */
|
} else {
|
||||||
return sprintf(
|
$fieldFormatting[$source] = function ($value, $item) {
|
||||||
'<a class="grid-field__link-block" href="%s" title="%s">%s</a>',
|
if ($item instanceof CMSPreviewable) {
|
||||||
Convert::raw2att($item->CMSEditLink()),
|
/** @var CMSPreviewable $item */
|
||||||
Convert::raw2att($value),
|
return sprintf(
|
||||||
Convert::raw2xml($value)
|
'<a class="grid-field__link-block" href="%s" title="%s">%s</a>',
|
||||||
);
|
Convert::raw2att($item->CMSEditLink()),
|
||||||
}
|
Convert::raw2att($value),
|
||||||
return $value;
|
Convert::raw2xml($value)
|
||||||
};
|
);
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$displayFields[$source] = isset($info['title']) ? $info['title'] : $source;
|
$displayFields[$source] = isset($info['title']) ? $info['title'] : $source;
|
||||||
|
Loading…
Reference in New Issue
Block a user