mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
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
This commit is contained in:
parent
32d6342aff
commit
f793ffd368
104
code/CMSMain.php
104
code/CMSMain.php
@ -41,7 +41,8 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
|
||||
'restore',
|
||||
'revert',
|
||||
'rollback',
|
||||
'sidereport',
|
||||
'sidereports',
|
||||
'SideReportsForm',
|
||||
'submit',
|
||||
'unpublish',
|
||||
'versions',
|
||||
@ -655,82 +656,57 @@ JS;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a dropdown for selecting reports
|
||||
*
|
||||
* @return Form
|
||||
*/
|
||||
function ReportForm() {
|
||||
$reports = ClassInfo::subclassesFor("SideReport");
|
||||
function sidereports() {
|
||||
return new SideReportsHandler($this, 'sidereports');
|
||||
}
|
||||
|
||||
function SideReportsForm() {
|
||||
$record = $this->currentPage();
|
||||
$reports = $this->sidereports()->getReportClasses();
|
||||
$options = array();
|
||||
foreach($reports as $report) {
|
||||
if($report != 'SideReport' && singleton($report)->canView()) {
|
||||
$options[singleton($report)->group()][singleton($report)->sort()][$report] = singleton($report)->title();
|
||||
}
|
||||
}
|
||||
|
||||
$id = $this->request->requestVar('ID');
|
||||
$reportClass = $this->request->requestVar('ReportClass');
|
||||
$report = ClassInfo::exists($reportClass) ? new $reportClass() : false;
|
||||
$reportHtml = ($report) ? $report->getHTML() : false;
|
||||
|
||||
$form = new Form(
|
||||
$this,
|
||||
'ReportForm',
|
||||
new FieldSet(
|
||||
new DropdownField(
|
||||
"ReportClass",
|
||||
_t('CMSMain.REPORT', 'Report'),
|
||||
$options,
|
||||
$reportClass,
|
||||
null,
|
||||
_t('CMSMain.CHOOSEREPORT',"(Choose a report)")
|
||||
),
|
||||
new LiteralField('ReportHtml', $reportHtml),
|
||||
new HiddenField('ID', false, $id),
|
||||
new HiddenField('Locale', false, $this->Locale)
|
||||
),
|
||||
new FieldSet(
|
||||
new FormAction('sidereport', _t('CMSMain_left.ss.GO','Go'))
|
||||
)
|
||||
);
|
||||
$form->unsetValidator();
|
||||
$form->setFormMethod('GET');
|
||||
|
||||
return $form;
|
||||
}
|
||||
function ReportFormParameters() {
|
||||
$reports = ClassInfo::subclassesFor("SideReport");
|
||||
|
||||
$forms = array();
|
||||
foreach($reports as $report) {
|
||||
if ($report != 'SideReport' && singleton($report)->canView()) {
|
||||
if ($fieldset = singleton($report)->getParameterFields()) {
|
||||
$formHtml = '';
|
||||
foreach($fieldset as $field) {
|
||||
$formHtml .= $field->FieldHolder();
|
||||
}
|
||||
$forms[$report] = $formHtml;
|
||||
$finalOptions = array();
|
||||
foreach($options as $group => $weights) {
|
||||
ksort($weights);
|
||||
foreach($weights as $weight => $reports) {
|
||||
foreach($reports as $class => $report) {
|
||||
$finalOptions[$group][$class] = $report;
|
||||
}
|
||||
}
|
||||
}
|
||||
$pageHtml = '';
|
||||
foreach($forms as $class => $html) {
|
||||
$pageHtml .= "<div id=\"SideReportForm_$class\" style=\"display:none\">$html</div>\n\n";
|
||||
}
|
||||
return new LiteralField("ReportFormParameters", '<div id="SideReportForms" style="display:none">'.$pageHtml.'</div>');
|
||||
$selectorField = new GroupedDropdownField(
|
||||
"ReportClass",
|
||||
_t('CMSMain.REPORT', 'Report'),
|
||||
$finalOptions
|
||||
);
|
||||
|
||||
$form = new Form(
|
||||
$this,
|
||||
'SideReportsForm',
|
||||
new FieldSet(
|
||||
$selectorField,
|
||||
new HiddenField('ID', false, ($record) ? $record->ID : null),
|
||||
new HiddenField('Locale', false, $this->Locale)
|
||||
),
|
||||
new FieldSet(
|
||||
new FormAction('doShowSideReport', _t('CMSMain_left.ss.GO','Go'))
|
||||
)
|
||||
);
|
||||
$form->unsetValidator();
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content for a side report.
|
||||
*
|
||||
* @param Array $data
|
||||
* @param Form $form
|
||||
* @return String
|
||||
* @return Form
|
||||
*/
|
||||
function sidereport($data, $form) {
|
||||
$form = $this->ReportForm();
|
||||
return (Director::is_ajax()) ? $form->forTemplate() : $form;
|
||||
function doShowSideReport($data, $form) {
|
||||
$form = $this->sidereports()->getForm($data['ReportClass'], $data);
|
||||
return $form->forTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
|
94
code/SideReportsHandler.php
Normal file
94
code/SideReportsHandler.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
@ -152,10 +152,14 @@
|
||||
* which creates a new page through #Form_EditForm and adds a new tree node.
|
||||
* @name ss.reports_holder
|
||||
*/
|
||||
$('#Form_ReportForm').concrete(/** @lends ss.reports_holder */{
|
||||
$('#Form_SideReportsForm').concrete(/** @lends ss.reports_holder */{
|
||||
ReportContainer: null,
|
||||
|
||||
onmatch: function() {
|
||||
var self = this;
|
||||
|
||||
this.setReportContainer($('#SideReportsHolder'))
|
||||
|
||||
// integrate with sitetree selection changes
|
||||
// TODO Only trigger when report is visible
|
||||
jQuery('#sitetree').bind('selectionchanged', function(e, data) {
|
||||
@ -166,19 +170,6 @@
|
||||
// move submit button to the top
|
||||
this.find('#ReportClass').after(this.find('.Actions'));
|
||||
|
||||
// links in results
|
||||
this.find('ul a').bind('click', function(e) {
|
||||
var $link = $(this);
|
||||
$link.addClass('loading');
|
||||
jQuery('#Form_EditForm').concrete('ss').loadForm(
|
||||
$(this).attr('href'),
|
||||
function(e) {
|
||||
$link.removeClass('loading');
|
||||
}
|
||||
);
|
||||
return false;
|
||||
});
|
||||
|
||||
this._super();
|
||||
},
|
||||
|
||||
@ -198,7 +189,7 @@
|
||||
dataType: 'html',
|
||||
success: function(data, status) {
|
||||
// replace current form
|
||||
self.replaceWith(data);
|
||||
self.getReportContainer().html(data);
|
||||
},
|
||||
complete: function(xmlhttp, status) {
|
||||
button.removeClass('loading');
|
||||
@ -209,6 +200,51 @@
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* All forms loaded via ajax from the Form_SideReports dropdown.
|
||||
*/
|
||||
$("#SideReportsHolder form").concrete({
|
||||
onmatch: function() {
|
||||
// links in results
|
||||
this.find('ul a').live('click', function(e) {
|
||||
var $link = $(this);
|
||||
$link.addClass('loading');
|
||||
jQuery('#Form_EditForm').concrete('ss').loadForm(
|
||||
$(this).attr('href'),
|
||||
function(e) {
|
||||
$link.removeClass('loading');
|
||||
}
|
||||
);
|
||||
return false;
|
||||
});
|
||||
|
||||
this._super();
|
||||
},
|
||||
|
||||
onsubmit: function() {
|
||||
var self = this;
|
||||
|
||||
var button = this.find(':submit:first');
|
||||
button.addClass('loading');
|
||||
|
||||
jQuery.ajax({
|
||||
url: this.attr('action'),
|
||||
data: this.serializeArray(),
|
||||
dataType: 'html',
|
||||
success: function(data, status) {
|
||||
// replace current form
|
||||
self.html(data);
|
||||
},
|
||||
complete: function(xmlhttp, status) {
|
||||
button.removeClass('loading');
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* @class Simple form showing versions of a specific page.
|
||||
* @name ss.Form_VersionsForm
|
||||
|
@ -32,6 +32,7 @@
|
||||
<a href="#"><% _t('SITEREPORTS','Site Reports') %></a>
|
||||
</h3>
|
||||
<div class="listpane" id="reports_holder">
|
||||
$ReportForm
|
||||
$SideReportsForm
|
||||
<div id="SideReportsHolder"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user