Version: current

     1 <?php
     2
     3 /**
     4  * Creates a subsite-aware version of another report.
     5  * Pass another report (or its classname) into the constructor.
     6  */
     7 class SubsiteReportWrapper extends SS_ReportWrapper {
     8     ///////////////////////////////////////////////////////////////////////////////////////////
     9     // Filtering
    10
    11     function parameterFields() {
    12         $subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain');
    13         $options = array_merge(array( '0' => _t('Subsite.MainSiteTitle', 'Main')), $subsites->toDropdownMap('ID', 'Title'));
    14
    15         $subsiteField = new TreeMultiselectField('Subsites', _t('SubsiteReportWrapper.Subsites', 'Subsites'), $options);
    16         $subsiteField->setValue(array_keys($options));
    17
    18         // We don't need to make the field editable if only one subsite is available
    19         if(sizeof($options) <= 1) {
    20             $subsiteField = $subsiteField->performReadonlyTransformation();
    21         }
    22
    23         $fields = parent::parameterFields();
    24         if($fields) {
    25             $fields->insertBefore($subsiteField, $fields->First()->Name());
    26         } else {
    27             $fields = new FieldSet($subsiteField);
    28         }
    29         return $fields;
    30     }
    31
    32     ///////////////////////////////////////////////////////////////////////////////////////////
    33     // Columns
    34
    35     function columns() {
    36         $columns = parent::columns();
    37         $columns['Subsite.Title'] = _t('SubsiteReportWrapper.Subsite', 'Subsite');
    38         return $columns;
    39     }
    40
    41     ///////////////////////////////////////////////////////////////////////////////////////////
    42     // Querying
    43
    44     function beforeQuery($params) {
    45         // The user has select a few specific sites
    46         if($params['Subsites'] != '') {
    47             Subsite::$force_subsite = $params['Subsites'];
    48
    49         // Default: restrict to all accessible sites
    50         } else {
    51             $subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain');
    52             $options = $subsites->toDropdownMap('ID', 'Title');
    53             $options[0] = 'Main';
    54             Subsite::$force_subsite = join(',', array_keys($options));
    55         }
    56     }
    57     function afterQuery() {
    58         // Manually manage the subsite filtering
    59         Subsite::$force_subsite = null;
    60     }
    61
    62 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -