Completed
Push — master ( 0133ac...41a847 )
by
unknown
9s
created

SiteSummary::getInfoLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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