SiteSummary::resolveCmsVersion()   A
last analyzed

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.7998
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\Forms\GridField\GridFieldPrintButton;
18
use SilverStripe\Forms\LiteralField;
19
use SilverStripe\ORM\FieldType\DBDatetime;
20
use SilverStripe\ORM\FieldType\DBField;
21
use SilverStripe\Reports\Report;
22
use SilverStripe\View\ArrayData;
23
use SilverStripe\View\Requirements;
24
25
/**
26
 * A report listing all installed modules used in this site (from a cache).
27
 */
28
class SiteSummary extends Report
29
{
30
    public function title()
31
    {
32
        return _t(__CLASS__ . '.TITLE', 'Installed modules');
33
    }
34
35
    public function sourceRecords()
36
    {
37
        $packageList = Package::get();
38
        $typeFilters = Config::inst()->get(UpdatePackageInfoTask::class, 'allowed_types');
39
40
        if (!empty($typeFilters)) {
41
            $packageList = $packageList->filter('Type', $typeFilters);
42
        }
43
44
        $this->extend('updateSourceRecords', $packageList);
45
46
        return $packageList;
47
    }
48
49
    /**
50
     * Provide column selection and formatting rules for the CMS report. You can extend data columns by extending
51
     * {@link Package::summary_fields}, or you can extend this method to adjust the formatting rules, or to provide
52
     * composite fields (such as Summary below) for the CMS report but not the CSV export.
53
     *
54
     * {@inheritDoc}
55
     */
56
    public function columns()
57
    {
58
        $columns = Package::create()->summaryFields();
59
60
        // Remove the default Title and Description and create Summary as a composite of both for the CMS report only
61
        unset($columns['Title'], $columns['Description']);
62
        $columns = ['Summary' => 'Summary'] + $columns;
63
64
        $this->extend('updateColumns', $columns);
65
66
        return $columns;
67
    }
68
69
    /**
70
     * Add a button row, including link out to the SilverStripe addons repository, and export button
71
     *
72
     * {@inheritdoc}
73
     */
74
    public function getReportField()
75
    {
76
        Requirements::css('bringyourownideas/silverstripe-maintenance: client/dist/styles/bundle.css');
77
        Requirements::javascript('bringyourownideas/silverstripe-maintenance: client/dist/js/bundle.js');
78
79
        /** @var GridField $grid */
80
        $grid = parent::getReportField();
81
82
        /** @var GridFieldConfig $config */
83
        $config = $grid->getConfig();
84
85
        $grid->addExtraClass('site-summary');
86
87
        $summaryFields = Package::create()->summaryFields();
88
        /** @var GridFieldExportButton $exportButton */
89
        $exportButton = $config->getComponentByType(GridFieldExportButton::class);
90
        $exportButton->setExportColumns($summaryFields);
91
        /** @var GridFieldPrintButton $printButton */
92
        $printButton = $config->getComponentByType(GridFieldPrintButton::class);
93
        $printButton->setPrintColumns($summaryFields);
94
95
        $versionHtml = ArrayData::create([
96
            'Title' => _t(__CLASS__ . '.VERSION', 'Version'),
97
            'Version' => $this->resolveCmsVersion(),
98
            'LastUpdated' => $this->getLastUpdated(),
99
        ])->renderWith(__CLASS__ . '/VersionHeader');
100
101
        $config->addComponents(
102
            Injector::inst()->create(GridFieldRefreshButton::class, 'buttons-before-left'),
103
            Injector::inst()->create(
104
                GridFieldLinkButton::class,
105
                'https://addons.silverstripe.org',
106
                _t(__CLASS__ . '.LINK_TO_ADDONS', 'Explore Addons'),
107
                'buttons-before-left'
108
            ),
109
            $this->getDropdownFilter(),
110
            $this->getInfoLink(),
111
            Injector::inst()->create(GridFieldHtmlFragment::class, 'header', $versionHtml)
112
        );
113
114
        // Re-order the paginator to ensure it counts correctly, and reorder the buttons
115
        $paginator = $config->getComponentByType(GridFieldPaginator::class);
116
        $config->removeComponent($paginator)->addComponent($paginator);
117
118
        $exportButton = $config->getComponentByType(GridFieldExportButton::class);
119
        $config->removeComponent($exportButton)->addComponent($exportButton);
120
121
        $printButton = $config->getComponentByType(GridFieldPrintButton::class);
122
        $config->removeComponentsByType($printButton)->addComponent($printButton);
0 ignored issues
show
Bug introduced by
$printButton of type SilverStripe\Forms\GridField\GridFieldComponent is incompatible with the type string|string[] expected by parameter $types of SilverStripe\Forms\GridF...emoveComponentsByType(). ( Ignorable by Annotation )

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

122
        $config->removeComponentsByType(/** @scrutinizer ignore-type */ $printButton)->addComponent($printButton);
Loading history...
123
124
        return $grid;
125
    }
126
127
    /**
128
     * Returns a dropdown filter with user configurable options in it
129
     *
130
     * @return GridFieldDropdownFilter
131
     */
132
    protected function getDropdownFilter()
133
    {
134
        /** @var GridFieldDropdownFilter $dropdownFilter */
135
        $dropdownFilter = Injector::inst()->create(
136
            GridFieldDropdownFilter::class,
137
            'addonFilter',
138
            'buttons-before-right',
139
            _t(__CLASS__ . '.ShowAllModules', 'Show all modules')
140
        );
141
142
        $dropdownFilter->addFilterOption(
143
            'supported',
144
            _t(__CLASS__ . '.FilterSupported', 'Supported modules'),
145
            ['Supported' => true]
146
        );
147
        $dropdownFilter->addFilterOption(
148
            'unsupported',
149
            _t(__CLASS__ . '.FilterUnsupported', 'Unsupported modules'),
150
            ['Supported' => false]
151
        );
152
153
        $this->extend('updateDropdownFilterOptions', $dropdownFilter);
154
155
        return $dropdownFilter;
156
    }
157
158
    /**
159
     * Returns a link to more information on this module on the addons site
160
     *
161
     * @return GridFieldHtmlFragment
162
     */
163
    protected function getInfoLink()
164
    {
165
        return Injector::inst()->create(
166
            GridFieldHtmlFragment::class,
167
            'buttons-before-right',
168
            DBField::create_field('HTMLFragment', ArrayData::create([
169
                'Link' => 'https://userhelp.silverstripe.org/en/4/optional_features/modules_report',
170
                'Label' => _t(__CLASS__ . '.MORE_INFORMATION', 'More information'),
171
            ])->renderWith(__CLASS__ . '/MoreInformationLink'))
172
        );
173
    }
174
175
    public function getCMSFields()
176
    {
177
        $fields = parent::getCMSFields();
178
        $alerts = $this->getAlerts();
179
        if ($alerts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alerts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
180
            $summaryInfo = '<div class="site-alert alert alert-warning">' . implode("\n", $alerts) . '</div>';
181
            $fields->unshift(LiteralField::create('AlertSummary', $summaryInfo));
182
        }
183
        return $fields;
184
    }
185
186
    /**
187
     * Return a list of alerts to display in a message box above the report
188
     * A combination of free text fields - combined alerts as opposed to a message box per alert.
189
     *
190
     * @return array
191
     */
192
    protected function getAlerts()
193
    {
194
        $alerts = [];
195
        $this->extend('updateAlerts', $alerts);
196
        return $alerts;
197
    }
198
199
    /**
200
     * Extract CMS and Framework version details from the records in the report
201
     *
202
     * @return string
203
     */
204
    protected function resolveCmsVersion()
205
    {
206
        $versionModules = [
207
            'silverstripe/framework' => 'Framework',
208
            'silverstripe/cms' => 'CMS',
209
        ];
210
        $this->extend('updateVersionModules', $versionModules);
211
212
        $records = $this->sourceRecords()->filter('Name', array_keys($versionModules));
213
        $versionParts = [];
214
215
        foreach ($versionModules as $name => $label) {
216
            $record = $records->find('Name', $name);
217
            if (!$record) {
218
                $version = _t(__CLASS__.'.VersionUnknown', 'Unknown');
219
            } else {
220
                $version = $record->Version;
221
            }
222
223
            $versionParts[] = "$label $version";
224
        }
225
226
        return implode(', ', $versionParts);
227
    }
228
229
    /**
230
     * Get the "last updated" date for the report. This is based on the modified date of any of the records, since
231
     * they are regenerated when the report is generated.
232
     *
233
     * @return string
234
     */
235
    public function getLastUpdated()
236
    {
237
        $packages = Package::get()->limit(1);
238
        if (!$packages->count()) {
239
            return '';
240
        }
241
        /** @var DBDatetime $datetime */
242
        $datetime = $packages->first()->dbObject('LastEdited');
243
        return $datetime->Date() . ' ' . $datetime->Time12();
244
    }
245
}
246