Completed
Push — master ( 0ebf95...33622c )
by Damian
12s
created

SubsiteReportWrapper::columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace SilverStripe\Subsites\Reports;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\TreeMultiselectField;
7
use SilverStripe\Reports\ReportWrapper;
8
use SilverStripe\Subsites\Model\Subsite;
9
10
/**
11
 * Creates a subsite-aware version of another report.
12
 * Pass another report (or its classname) into the constructor.
13
 */
14
class SubsiteReportWrapper extends ReportWrapper
15
{
16
17
    /**
18
     * @return FieldList
19
     */
20
    public function parameterFields()
21
    {
22
        $subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true);
23
        $options = $subsites->toDropdownMap('ID', 'Title');
24
25
        $subsiteField = TreeMultiselectField::create(
26
            'Subsites',
0 ignored issues
show
Bug introduced by
'Subsites' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
            /** @scrutinizer ignore-type */ 'Subsites',
Loading history...
27
            _t(__CLASS__ . '.ReportDropdown', 'Sites'),
28
            $options
29
        );
30
        $subsiteField->setValue(array_keys($options));
31
32
        // We don't need to make the field editable if only one subsite is available
33
        if (sizeof($options) <= 1) {
0 ignored issues
show
Bug introduced by
The call to sizeof() has too few arguments starting with mode. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        if (/** @scrutinizer ignore-call */ sizeof($options) <= 1) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
34
            $subsiteField = $subsiteField->performReadonlyTransformation();
35
        }
36
37
        $fields = parent::parameterFields();
38
        if ($fields) {
39
            $fields->insertBefore($subsiteField, $fields->First()->Name());
40
        } else {
41
            $fields = FieldList::create($subsiteField);
42
        }
43
        return $fields;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function columns()
50
    {
51
        $columns = parent::columns();
52
        $columns['Subsite.Title'] = Subsite::class;
53
        return $columns;
54
    }
55
56
    ///////////////////////////////////////////////////////////////////////////////////////////
57
    // Querying
58
59
    /**
60
     * @param arary $params
0 ignored issues
show
Bug introduced by
The type SilverStripe\Subsites\Reports\arary was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
     * @return void
62
     */
63
    public function beforeQuery($params)
64
    {
65
        // The user has select a few specific sites
66
        if (!empty($params['Subsites'])) {
67
            Subsite::$force_subsite = $params['Subsites'];
68
69
            // Default: restrict to all accessible sites
70
        } else {
71
            $subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain');
72
            $options = $subsites->toDropdownMap('ID', 'Title');
73
            Subsite::$force_subsite = join(',', array_keys($options));
74
        }
75
    }
76
77
    /**
78
     * @return void
79
     */
80
    public function afterQuery()
81
    {
82
        // Manually manage the subsite filtering
83
        Subsite::$force_subsite = null;
84
    }
85
}
86