2010-03-01 22:56:32 +01:00
|
|
|
<?php
|
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
namespace SilverStripe\Subsites\Reports;
|
|
|
|
|
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\Forms\TreeMultiselectField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Reports\ReportWrapper;
|
2017-05-24 12:32:05 +02:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2010-03-01 22:56:32 +01:00
|
|
|
/**
|
|
|
|
* Creates a subsite-aware version of another report.
|
|
|
|
* Pass another report (or its classname) into the constructor.
|
|
|
|
*/
|
2016-09-22 16:38:29 +02:00
|
|
|
class SubsiteReportWrapper extends ReportWrapper {
|
2010-03-01 22:56:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Filtering
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2010-03-01 22:56:32 +01:00
|
|
|
function parameterFields() {
|
2011-09-12 14:01:13 +02:00
|
|
|
$subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true);
|
2010-03-01 22:56:32 +01:00
|
|
|
$options = $subsites->toDropdownMap('ID', 'Title');
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2013-10-30 13:43:59 +01:00
|
|
|
$subsiteField = new TreeMultiselectField(
|
2017-05-24 12:32:05 +02:00
|
|
|
'Subsites',
|
|
|
|
_t('SubsiteReportWrapper.ReportDropdown', 'Sites'),
|
2013-10-30 13:43:59 +01:00
|
|
|
$options
|
|
|
|
);
|
2010-03-01 22:56:37 +01:00
|
|
|
$subsiteField->setValue(array_keys($options));
|
2010-03-01 22:56:32 +01:00
|
|
|
|
2010-03-01 22:56:37 +01:00
|
|
|
// We don't need to make the field editable if only one subsite is available
|
|
|
|
if(sizeof($options) <= 1) {
|
|
|
|
$subsiteField = $subsiteField->performReadonlyTransformation();
|
|
|
|
}
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2010-03-01 22:56:49 +01:00
|
|
|
$fields = parent::parameterFields();
|
2010-03-01 22:56:32 +01:00
|
|
|
if($fields) {
|
|
|
|
$fields->insertBefore($subsiteField, $fields->First()->Name());
|
|
|
|
} else {
|
2012-03-25 18:35:01 +02:00
|
|
|
$fields = new FieldList($subsiteField);
|
2010-03-01 22:56:32 +01:00
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Columns
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2010-03-01 22:56:32 +01:00
|
|
|
function columns() {
|
2010-03-01 22:56:49 +01:00
|
|
|
$columns = parent::columns();
|
2017-05-24 13:36:04 +02:00
|
|
|
$columns['Subsite.Title'] = Subsite::class;
|
2010-03-01 22:56:32 +01:00
|
|
|
return $columns;
|
|
|
|
}
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2010-03-01 22:56:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Querying
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2010-03-01 22:56:32 +01:00
|
|
|
function beforeQuery($params) {
|
2010-03-01 22:56:37 +01:00
|
|
|
// The user has select a few specific sites
|
2010-03-01 22:56:32 +01:00
|
|
|
if(!empty($params['Subsites'])) {
|
2010-03-01 22:56:37 +01:00
|
|
|
Subsite::$force_subsite = $params['Subsites'];
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2010-03-01 22:56:37 +01:00
|
|
|
// Default: restrict to all accessible sites
|
|
|
|
} else {
|
|
|
|
$subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain');
|
|
|
|
$options = $subsites->toDropdownMap('ID', 'Title');
|
|
|
|
Subsite::$force_subsite = join(',', array_keys($options));
|
2010-03-01 22:56:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function afterQuery() {
|
|
|
|
// Manually manage the subsite filtering
|
|
|
|
Subsite::$force_subsite = null;
|
|
|
|
}
|
2017-05-24 12:32:05 +02:00
|
|
|
|
|
|
|
}
|