Completed
Push — master ( 3da775...1e6bd3 )
by Robbie
9s
created

SiteSummary::resolveCmsVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Reports;
4
5
use BringYourOwnIdeas\Maintenance\Model\Package;
6
use SilverStripe\Core\Config\Config;
7
use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldConfig;
10
use SilverStripe\View\Requirements;
11
use SilverStripe\Forms\GridField\GridFieldExportButton;
12
use SilverStripe\View\ArrayData;
13
use SilverStripe\Core\Injector\Injector;
14
use BringYourOwnIdeas\Maintenance\Forms\GridFieldDropdownFilter;
15
use BringYourOwnIdeas\Maintenance\Forms\GridFieldRefreshButton;
16
use BringYourOwnIdeas\Maintenance\Forms\GridFieldLinkButton;
17
use BringYourOwnIdeas\Maintenance\Forms\GridFieldHtmlFragment;
18
use SilverStripe\Forms\GridField\GridFieldPaginator;
19
use SilverStripe\Reports\Report;
20
21
/**
22
 * A report listing all installed modules used in this site (from a cache).
23
 */
24
class SiteSummary extends Report
25
{
26
    public function title()
27
    {
28
        return _t(__CLASS__ . '.TITLE', 'Installed modules');
29
    }
30
31
    public function sourceRecords()
32
    {
33
        $packageList = Package::get();
34
        $typeFilters = Config::inst()->get(UpdatePackageInfoTask::class, 'allowed_types');
35
36
        if (!empty($typeFilters)) {
37
            $packageList = $packageList->filter('Type', $typeFilters);
38
        }
39
40
        $this->extend('updateSourceRecords', $packageList);
41
42
        return $packageList;
43
    }
44
45
    /**
46
     * Provide column selection and formatting rules for the CMS report. You can extend data columns by extending
47
     * {@link Package::summary_fields}, or you can extend this method to adjust the formatting rules, or to provide
48
     * composite fields (such as Summary below) for the CMS report but not the CSV export.
49
     *
50
     * {@inheritDoc}
51
     */
52
    public function columns()
53
    {
54
        $columns = Package::create()->summaryFields();
55
56
        // Remove the default Title and Description and create Summary as a composite of both for the CMS report only
57
        unset($columns['Title'], $columns['Description']);
58
        $columns = ['Summary' => 'Summary'] + $columns;
59
60
        $this->extend('updateColumns', $columns);
61
62
        return $columns;
63
    }
64
65
    /**
66
     * Add a button row, including link out to the SilverStripe addons repository, and export button
67
     *
68
     * {@inheritdoc}
69
     */
70
    public function getReportField()
71
    {
72
        Requirements::css('bringyourownideas/silverstripe-maintenance: css/sitesummary.css');
73
74
        /** @var GridField $grid */
75
        $grid = parent::getReportField();
76
77
        /** @var GridFieldConfig $config */
78
        $config = $grid->getConfig();
79
80
        $grid->addExtraClass('package-summary');
81
82
        /** @var GridFieldExportButton $exportButton */
83
        $exportButton = $config->getComponentByType(GridFieldExportButton::class);
84
        $exportButton->setExportColumns(Package::create()->summaryFields());
85
86
        $versionHtml = ArrayData::create([
87
            'Title' => _t(__CLASS__ . '.VERSION', 'Version'),
88
            'Version' => $this->resolveCmsVersion(),
89
        ])->renderWith('SiteSummary_VersionHeader');
90
91
        /** @var GridFieldDropdownFilter $dropdownFilter */
92
        $dropdownFilter = Injector::inst()->create(
93
            GridFieldDropdownFilter::class,
94
            'addonFilter',
95
            'buttons-before-right',
96
            _t(__CLASS__ . '.ShowAllModules', 'Show all modules')
97
        );
98
99
        $dropdownFilter->addFilterOption(
100
            'supported',
101
            _t(__CLASS__ . '.FilterSupported', 'Supported modules'),
102
            ['Supported' => true]
103
        );
104
        $dropdownFilter->addFilterOption(
105
            'unsupported',
106
            _t(__CLASS__ . '.FilterUnsupported', 'Unsupported modules'),
107
            ['Supported' => false]
108
        );
109
110
        $this->extend('updateDropdownFilterOptions', $dropdownFilter);
111
112
        $config->addComponents(
113
            Injector::inst()->create(GridFieldRefreshButton::class, 'buttons-before-left'),
114
            Injector::inst()->create(
115
                GridFieldLinkButton::class,
116
                'https://addons.silverstripe.org',
117
                _t(__CLASS__ . '.LINK_TO_ADDONS', 'Explore Addons'),
118
                'buttons-before-left'
119
            ),
120
            $dropdownFilter,
121
            Injector::inst()->create(GridFieldHtmlFragment::class, 'header', $versionHtml)
122
        );
123
124
        // Re-order the paginator to ensure it counts correctly.
125
        $paginator = $config->getComponentByType(GridFieldPaginator::class);
126
        $config->removeComponent($paginator);
127
        $config->addComponent($paginator);
128
129
        return $grid;
130
    }
131
132
    /**
133
     * Extract CMS and Framework version details from the records in the report
134
     *
135
     * @return string
136
     */
137
    protected function resolveCmsVersion()
138
    {
139
        $versionModules = [
140
            'silverstripe/framework' => 'Framework',
141
            'silverstripe/cms' => 'CMS',
142
        ];
143
        $this->extend('updateVersionModules', $versionModules);
144
145
        $records = $this->sourceRecords()->filter('Name', array_keys($versionModules));
146
        $versionParts = [];
147
148
        foreach ($versionModules as $name => $label) {
149
            $record = $records->find('Name', $name);
150
            if (!$record) {
151
                $version = _t(__CLASS__.'.VersionUnknown', 'Unknown');
152
            } else {
153
                $version = $record->Version;
154
            }
155
156
            $versionParts[] = "$label $version";
157
        }
158
159
        return implode(', ', $versionParts);
160
    }
161
}
162