Completed
Pull Request — master (#85)
by Robbie
02:19
created

SiteSummary::getLastUpdated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Reports;
4
5
use BringYourOwnIdeas\Maintenance\Forms\GridFieldDropdownFilter;
6
use BringYourOwnIdeas\Maintenance\Forms\GridFieldHtmlFragment;
7
use BringYourOwnIdeas\Maintenance\Forms\GridFieldLinkButton;
8
use BringYourOwnIdeas\Maintenance\Forms\GridFieldRefreshButton;
9
use BringYourOwnIdeas\Maintenance\Model\Package;
10
use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Forms\GridField\GridField;
14
use SilverStripe\Forms\GridField\GridFieldConfig;
15
use SilverStripe\Forms\GridField\GridFieldExportButton;
16
use SilverStripe\Forms\GridField\GridFieldPaginator;
17
use SilverStripe\Reports\Report;
18
use SilverStripe\View\ArrayData;
19
use SilverStripe\View\Requirements;
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: client/dist/styles/bundle.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
            'LastUpdated' => $this->getLastUpdated(),
90
        ])->renderWith('SiteSummary_VersionHeader');
91
92
        /** @var GridFieldDropdownFilter $dropdownFilter */
93
        $dropdownFilter = Injector::inst()->create(
94
            GridFieldDropdownFilter::class,
95
            'addonFilter',
96
            'buttons-before-right',
97
            _t(__CLASS__ . '.ShowAllModules', 'Show all modules')
98
        );
99
100
        $dropdownFilter->addFilterOption(
101
            'supported',
102
            _t(__CLASS__ . '.FilterSupported', 'Supported modules'),
103
            ['Supported' => true]
104
        );
105
        $dropdownFilter->addFilterOption(
106
            'unsupported',
107
            _t(__CLASS__ . '.FilterUnsupported', 'Unsupported modules'),
108
            ['Supported' => false]
109
        );
110
111
        $this->extend('updateDropdownFilterOptions', $dropdownFilter);
112
113
        $config->addComponents(
114
            Injector::inst()->create(GridFieldRefreshButton::class, 'buttons-before-left'),
115
            Injector::inst()->create(
116
                GridFieldLinkButton::class,
117
                'https://addons.silverstripe.org',
118
                _t(__CLASS__ . '.LINK_TO_ADDONS', 'Explore Addons'),
119
                'buttons-before-left'
120
            ),
121
            $dropdownFilter,
122
            Injector::inst()->create(GridFieldHtmlFragment::class, 'header', $versionHtml)
123
        );
124
125
        // Re-order the paginator to ensure it counts correctly.
126
        $paginator = $config->getComponentByType(GridFieldPaginator::class);
127
        $config->removeComponent($paginator);
128
        $config->addComponent($paginator);
129
130
        return $grid;
131
    }
132
133
    /**
134
     * Extract CMS and Framework version details from the records in the report
135
     *
136
     * @return string
137
     */
138
    protected function resolveCmsVersion()
139
    {
140
        $versionModules = [
141
            'silverstripe/framework' => 'Framework',
142
            'silverstripe/cms' => 'CMS',
143
        ];
144
        $this->extend('updateVersionModules', $versionModules);
145
146
        $records = $this->sourceRecords()->filter('Name', array_keys($versionModules));
147
        $versionParts = [];
148
149
        foreach ($versionModules as $name => $label) {
150
            $record = $records->find('Name', $name);
151
            if (!$record) {
152
                $version = _t(__CLASS__.'.VersionUnknown', 'Unknown');
153
            } else {
154
                $version = $record->Version;
155
            }
156
157
            $versionParts[] = "$label $version";
158
        }
159
160
        return implode(', ', $versionParts);
161
    }
162
163
    /**
164
     * Get the "last updated" date for the report. This is based on the modified date of any of the records, since
165
     * they are regenerated when the report is generated.
166
     *
167
     * @return string
168
     */
169
    public function getLastUpdated()
170
    {
171
        $packages = Package::get()->limit(1);
172
        if (!$packages->count()) {
173
            return '';
174
        }
175
        return $packages->first()->dbObject('LastEdited')->Nice();
176
    }
177
}
178