silverstripe-reports/code/ReportAdmin.php

264 lines
7.8 KiB
PHP
Raw Normal View History

2013-01-17 01:22:13 +01:00
<?php
2016-06-16 07:06:51 +02:00
namespace SilverStripe\Reports;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
2017-06-21 06:30:14 +02:00
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldFooter;
2017-06-21 06:30:14 +02:00
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
2016-06-16 07:06:51 +02:00
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\SS_List;
use SilverStripe\Security\Member;
use SilverStripe\Security\PermissionProvider;
2017-06-09 06:48:30 +02:00
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
2016-06-16 07:06:51 +02:00
2013-01-17 01:22:13 +01:00
/**
* Reports section of the CMS.
2016-06-16 07:06:51 +02:00
*
2013-01-17 01:22:13 +01:00
* All reports that should show in the ReportAdmin section
2017-06-27 05:52:13 +02:00
* of the CMS need to subclass {@link SilverStripe\Reports\Report}, and implement
2013-01-17 01:22:13 +01:00
* the appropriate methods and variables that are required.
*/
2015-12-15 23:06:45 +01:00
class ReportAdmin extends LeftAndMain implements PermissionProvider
{
private static $url_segment = 'reports';
2016-06-16 07:06:51 +02:00
2015-12-15 23:06:45 +01:00
private static $menu_title = 'Reports';
2016-06-16 07:06:51 +02:00
2016-12-20 04:13:46 +01:00
private static $menu_icon_class = 'font-icon-chart-line';
2016-12-20 01:30:50 +01:00
2015-12-15 23:06:45 +01:00
private static $template_path = null; // defaults to (project)/templates/email
private static $tree_class = Report::class;
2015-12-15 23:06:45 +01:00
private static $url_handlers = array(
'show/$ReportClass/$Action' => 'handleAction'
2015-12-15 23:06:45 +01:00
);
/**
2016-06-16 07:06:51 +02:00
* Variable that describes which report we are currently viewing based on
2015-12-15 23:06:45 +01:00
* the URL (gets set in init method).
*
* @var string
*/
protected $reportClass;
/**
2016-09-09 08:11:38 +02:00
* @var Report
*/
2015-12-15 23:06:45 +01:00
protected $reportObject;
2016-06-16 07:06:51 +02:00
private static $required_permission_codes = 'CMS_ACCESS_ReportAdmin';
2015-12-15 23:06:45 +01:00
public function init()
{
parent::init();
// Set custom options for TinyMCE specific to ReportAdmin
HTMLEditorConfig::get('cms')->setOption('content_css', project() . '/css/editor.css');
2015-12-15 23:06:45 +01:00
Requirements::javascript('silverstripe/reports: javascript/ReportAdmin.js');
2015-12-15 23:06:45 +01:00
}
/**
* Does the parent permission checks, but also
* makes sure that instantiatable subclasses of
2017-06-27 05:52:13 +02:00
* {@link SilverStripe\Reports\Report} exist. By default, the CMS doesn't
2015-12-15 23:06:45 +01:00
* include any Reports, so there's no point in showing
*
* @param Member $member
* @return boolean
*/
public function canView($member = null)
{
if (!$member && $member !== false) {
2017-06-09 06:48:30 +02:00
$member = Security::getCurrentUser();
2015-12-15 23:06:45 +01:00
}
if (!parent::canView($member)) {
return false;
}
foreach ($this->Reports() as $report) {
if ($report->canView($member)) {
return true;
}
}
return false;
}
/**
* Return a SS_List of SS_Report subclasses
* that are available for use.
*
* @return SS_List
*/
public function Reports()
{
$output = new ArrayList();
2016-09-09 08:11:38 +02:00
/** @var Report $report */
foreach (Report::get_reports() as $report) {
2015-12-15 23:06:45 +01:00
if ($report->canView()) {
$output->push($report);
}
}
return $output;
}
2016-07-29 00:44:00 +02:00
public function handleAction($request, $action)
{
$this->reportClass = $this->unsanitiseClassName($request->param('ReportClass'));
// Check report
if ($this->reportClass) {
2016-09-09 08:11:38 +02:00
$allReports = Report::get_reports();
if (empty($allReports[$this->reportClass])) {
return $this->httpError(404);
}
$this->reportObject = $allReports[$this->reportClass];
}
// Delegate to sub-form
return parent::handleAction($request, $action);
}
2017-06-21 06:30:14 +02:00
/**
* Unsanitise a model class' name from a URL param
*
* @param string $class
* @return string
*/
protected function unsanitiseClassName($class)
{
2022-04-13 07:39:46 +02:00
return str_replace('-', '\\', $class ?? '');
2017-06-21 06:30:14 +02:00
}
2015-12-15 23:06:45 +01:00
/**
* Determine if we have reports and need
* to display the "Reports" main menu item
* in the CMS.
*
* The test for an existance of a report
* is done by checking for a subclass of
* "SS_Report" that exists.
*
* @return boolean
*/
public static function has_reports()
{
2022-04-13 07:39:46 +02:00
return sizeof(Report::get_reports() ?? []) > 0;
2015-12-15 23:06:45 +01:00
}
/**
* Returns the Breadcrumbs for the ReportAdmin
2016-08-11 04:29:53 +02:00
*
* @param bool $unlinked
2015-12-15 23:06:45 +01:00
* @return ArrayList
*/
public function Breadcrumbs($unlinked = false)
{
$items = parent::Breadcrumbs($unlinked);
2016-06-16 07:06:51 +02:00
2015-12-15 23:06:45 +01:00
// The root element should explicitly point to the root node.
// Uses session state for current record otherwise.
$items[0]->Link = singleton('SilverStripe\\Reports\\ReportAdmin')->Link();
2015-12-15 23:06:45 +01:00
if ($report = $this->reportObject) {
$breadcrumbs = $report->getBreadcrumbs();
if (!empty($breadcrumbs)) {
foreach ($breadcrumbs as $crumb) {
$items->push($crumb);
}
}
2015-12-15 23:06:45 +01:00
//build breadcrumb trail to the current report
2019-01-09 20:59:28 +01:00
$items->push(ArrayData::create([
'Title' => $report->title(),
'Link' => Controller::join_links(
$this->Link(),
2019-01-09 20:59:28 +01:00
'?' . http_build_query(['q' => $this->request->requestVar('q')])
)
2019-01-09 20:59:28 +01:00
]));
2015-12-15 23:06:45 +01:00
}
return $items;
}
/**
* Returns the link to the report admin section, or the specific report that is currently displayed
*
* @param string $action
* @return string
2015-12-15 23:06:45 +01:00
*/
public function Link($action = null)
{
if ($this->reportObject) {
return $this->reportObject->getLink($action);
2015-12-15 23:06:45 +01:00
}
// Basic link to this cms section
return parent::Link($action);
2015-12-15 23:06:45 +01:00
}
public function providePermissions()
{
return array(
"CMS_ACCESS_ReportAdmin" => array(
2017-05-08 07:56:43 +02:00
'name' => _t('SilverStripe\\CMS\\Controllers\\CMSMain.ACCESS', "Access to '{title}' section", array(
2016-08-11 04:29:53 +02:00
'title' => static::menu_title()
)),
2017-05-08 07:56:43 +02:00
'category' => _t('SilverStripe\\Security\\Permission.CMS_ACCESS_CATEGORY', 'CMS Access')
2015-12-15 23:06:45 +01:00
)
);
}
public function getEditForm($id = null, $fields = null)
{
$report = $this->reportObject;
if ($report) {
$fields = $report->getCMSFields();
} else {
// List all reports
$fields = new FieldList();
$gridFieldConfig = GridFieldConfig::create()->addComponents(
GridFieldSortableHeader::create(),
GridFieldDataColumns::create(),
GridFieldFooter::create()
2015-12-15 23:06:45 +01:00
);
$gridField = GridField::create('Reports', false, $this->Reports(), $gridFieldConfig);
2016-08-11 04:29:53 +02:00
/** @var GridFieldDataColumns $columns */
2019-01-09 20:59:28 +01:00
$columns = $gridField->getConfig()
->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns');
2015-12-15 23:06:45 +01:00
$columns->setDisplayFields(array(
2017-05-08 07:56:43 +02:00
'title' => _t('SilverStripe\\Reports\\ReportAdmin.ReportTitle', 'Title'),
2015-12-15 23:06:45 +01:00
));
$columns->setFieldFormatting(array(
'title' => '<a href=\"$Link\" class=\"grid-field__link-block\">$value ($CountForOverview)</a>'
2015-12-15 23:06:45 +01:00
));
$gridField->addExtraClass('all-reports-gridfield');
$fields->push($gridField);
}
$actions = new FieldList();
$form = new Form($this, "EditForm", $fields, $actions);
2019-01-09 20:59:28 +01:00
$form->addExtraClass(
'panel panel--padded panel--scrollable cms-edit-form cms-panel-padded' . $this->BaseCSSClasses()
);
2015-12-15 23:06:45 +01:00
$form->loadDataFrom($this->request->getVars());
$this->extend('updateEditForm', $form);
return $form;
}
}