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\FieldList;
|
2017-05-24 15:26:28 +02:00
|
|
|
use SilverStripe\Forms\TreeMultiselectField;
|
2016-09-22 16:38:29 +02:00
|
|
|
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
|
|
|
|
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.
|
|
|
|
*/
|
2017-05-24 15:26:28 +02:00
|
|
|
class SubsiteReportWrapper extends ReportWrapper
|
|
|
|
{
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
2017-05-29 13:44:19 +02:00
|
|
|
public function parameterFields()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
$subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true);
|
|
|
|
$options = $subsites->toDropdownMap('ID', 'Title');
|
|
|
|
|
|
|
|
$subsiteField = new TreeMultiselectField(
|
|
|
|
'Subsites',
|
|
|
|
_t('SubsiteReportWrapper.ReportDropdown', 'Sites'),
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
$subsiteField->setValue(array_keys($options));
|
|
|
|
|
|
|
|
// We don't need to make the field editable if only one subsite is available
|
|
|
|
if (sizeof($options) <= 1) {
|
|
|
|
$subsiteField = $subsiteField->performReadonlyTransformation();
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = parent::parameterFields();
|
|
|
|
if ($fields) {
|
|
|
|
$fields->insertBefore($subsiteField, $fields->First()->Name());
|
|
|
|
} else {
|
|
|
|
$fields = new FieldList($subsiteField);
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-05-29 13:44:19 +02:00
|
|
|
public function columns()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
$columns = parent::columns();
|
|
|
|
$columns['Subsite.Title'] = Subsite::class;
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Querying
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* @param arary $params
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-05-29 13:44:19 +02:00
|
|
|
public function beforeQuery($params)
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
// The user has select a few specific sites
|
|
|
|
if (!empty($params['Subsites'])) {
|
|
|
|
Subsite::$force_subsite = $params['Subsites'];
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
}
|
2017-04-23 22:23:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-05-29 13:44:19 +02:00
|
|
|
public function afterQuery()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
// Manually manage the subsite filtering
|
|
|
|
Subsite::$force_subsite = null;
|
|
|
|
}
|
2017-05-24 12:32:05 +02:00
|
|
|
|
|
|
|
}
|