Completed
Push — master ( e2e2e6...f57935 )
by
unknown
14s
created

CwpStatsReport::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace CWP\CWP\Report;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Assets\Folder;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldConfig;
10
use SilverStripe\Forms\GridField\GridFieldExportButton;
11
use SilverStripe\Forms\GridField\GridFieldPrintButton;
12
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\Reports\Report;
15
use SilverStripe\Subsites\Model\Subsite;
16
use SilverStripe\Versioned\Versioned;
17
18
/**
19
 * Summary report on the page and file counts managed by this CMS.
20
 */
21
class CwpStatsReport extends Report
22
{
23
    public function title()
24
    {
25
        return _t(__CLASS__ . '.Title', 'Summary statistics');
26
    }
27
28
    public function description()
29
    {
30
        return _t(
31
            __CLASS__ . '.Description',
32
            'This report provides various statistics for this site. The "total live page count" is the number that ' .
33
                'can be compared against the instance size specifications.'
34
        );
35
    }
36
37
    public function columns()
38
    {
39
        return [
40
            'Name' => _t(__CLASS__ . '.Name', 'Name'),
41
            'Count' => _t(__CLASS__ . '.Count', 'Count'),
42
        ];
43
    }
44
45
    /**
46
     * Manually create source records for the report. Agreggates cannot be provided as a column of a DataQuery result.
47
     *
48
     * {@inheritDoc}
49
     */
50
    public function sourceRecords($params = [], $sort = null, $limit = null)
51
    {
52
        $records = [];
53
54
        // Get the query to apply across all variants: looks at all subsites, translations, live stage only.
55
        $crossVariant = (function ($dataQuery) {
56
            $params = [
57
                'Subsite.filter' => false,
58
                'Versioned.mode' => 'stage',
59
                'Versioned.stage' => Versioned::LIVE,
60
            ];
61
62
            return $dataQuery->setDataQueryParam($params);
63
        });
64
65
        // Total.
66
        $records[] = [
67
            'Name' => _t(
68
                __CLASS__ . '.TotalPageCount',
69
                'Total live page count, across all translations and subsites'
70
            ),
71
            'Count' => $crossVariant(SiteTree::get())->count(),
72
        ];
73
74
        if (class_exists(Subsite::class)) {
75
            // Main site.
76
            $records[] = [
77
                'Name' => _t(__CLASS__ . '.PagesForMainSite', '- in the main site'),
78
                'Count' => $crossVariant(SiteTree::get())
79
                    ->filter(['SubsiteID' => 0])
80
                    ->count(),
81
            ];
82
83
            // Per subsite.
84
            $subsites = Subsite::get();
85
            foreach ($subsites as $subsite) {
86
                $records[] = [
87
                    'Name' => _t(
88
                        __CLASS__ . '.PagesForSubsite',
89
                        "- in the subsite '{SubsiteTitle}'",
90
                        ['SubsiteTitle' => $subsite->Title]
91
                    ),
92
                    'Count' => $crossVariant(SiteTree::get())
93
                        ->filter(['SubsiteID' => $subsite->ID])
94
                        ->count(),
95
                ];
96
            }
97
        }
98
99
        // Files.
100
        $records[] = [
101
            'Name' => _t(__CLASS__ . '.FileCount', 'File count'),
102
            'Count' => File::get()
103
                ->setDataQueryParam('Subsite.filter', false)
104
                ->filter(['ClassName:not' => Folder::class])
105
                ->count(),
106
        ];
107
108
        return ArrayList::create($records);
109
    }
110
111
    /**
112
     * @return GridField
113
     */
114
    public function getReportField()
115
    {
116
        /** @var GridField $gridField */
117
        $gridField = parent::getReportField();
118
119
        /** @var GridFieldConfig $gridConfig */
120
        $gridConfig = $gridField->getConfig();
121
        $gridConfig->removeComponentsByType(GridFieldPrintButton::class);
122
        $gridConfig->removeComponentsByType(GridFieldExportButton::class);
123
        $gridConfig->removeComponentsByType(GridFieldSortableHeader::class);
124
125
        return $gridField;
126
    }
127
}
128