silverstripe-reports/code/SideReportsHandler.php
Ingo Schommer f793ffd368 ENHANCEMENT SideReportsHandler class for easier handling and templating of "side reprts" in the CMS interface
API CHANGE Removed CMSMain->ReportFormParameters()/ReportForm()/sidereport(), replaced with new SideReportsHandler class

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@92846 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-11-21 03:20:54 +00:00

94 lines
2.3 KiB
PHP

<?php
/**
* @package cms
* @subpackage reports
*/
class SideReportsHandler extends RequestHandler {
static $url_handlers = array(
'$Action' => 'handleAction'
);
protected $parentController;
/**
* @var String
*/
protected $urlSegment;
/**
* @param string $parentController
* @param string $urlSegment
* @param string $recordClass
*/
function __construct($parentController, $urlSegment, $recordClass = null) {
$this->parentController = $parentController;
$this->urlSegment = $urlSegment;
if($recordClass) $this->recordClass = $recordClass;
parent::__construct();
}
function Link() {
return Controller::join_links($this->parentController->Link(), $this->urlSegment);
}
function handleAction($request) {
// This method can't be called without ajax.
if(!Director::is_ajax()) return Director::redirectBack();
$form = $this->getForm($request->requestVar("ReportClass"), $request->requestVars());
// TODO Accept custom actions
return $form->forTemplate();
}
/**
* @param String $reportClass
* @param Array $data
* @return Form
*/
function getForm($reportClass, $data) {
$report = new $reportClass();
$fields = $report->getParameterFields();
if(!$fields) $fields = new FieldSet();
$fields->push(new LiteralField('ReportHtml', $report->getHTML()));
$fields->push(new HiddenField('ReportClass', null, $reportClass));
$fields->push(new HiddenField('ID', false, (isset($data['ID'])) ? $data['ID'] : null));
$fields->push(new HiddenField('Locale', false, (isset($data['Locale'])) ? $data['Locale'] : null));
$form = new Form(
$this,
'ReportForm',
$fields,
new FieldSet(
new FormAction('sidereport', _t('CMSMain_left.ss.REFRESH','Refresh'))
)
);
$form->setFormAction($this->Link());
$form->unsetValidator();
$form->setFormMethod('GET');
$form->loadDataFrom($data);
return $form;
}
/**
* Returns all viewable subclasses of {@link SideReport}
*
* @return array
*/
function getReportClasses() {
$classes = ClassInfo::subclassesFor("SideReport");
foreach($classes as $i => $class) {
if($class != 'SideReport') $report = singleton($class);
if(
$class == 'SideReport'
|| ($report && !$report->canView())
) {
unset($classes[$i]);
}
}
return $classes;
}
}