Completed
Push — master ( 3c0a16...506e36 )
by Robbie
10s
created

ElementsInUseReport::getReportField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\Elemental\Reports;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\Reports\Report;
10
use SilverStripe\View\ArrayData;
11
use SilverStripe\View\Requirements;
12
13
class ElementsInUseReport extends Report
14
{
15
    public function title()
16
    {
17
        return _t(__CLASS__ . '.ReportTitle', 'Content blocks in use');
18
    }
19
20
    public function sourceRecords($params = [])
21
    {
22
        /** @var BaseElement[] $elements */
23
        $elements = BaseElement::get();
24
25
        $output = ArrayList::create();
26
27
        foreach ($elements as $element) {
28
            // Skip if filtering and looking at a different record
29
            if (isset($params['ClassName']) && $params['ClassName'] !== $element->sanitiseClassName($element)) {
30
                continue;
31
            }
32
33
            $output->push(ArrayData::create([
34
                'Icon' => $element->getIcon(),
35
                'Title' => $element->Title,
36
                'EditLink' => $element->CMSEditLink(),
37
                'Summary' => $element->getSummary(),
38
                'Type' => $element->getTypeNice(),
39
                'ClassName' => $element->sanitiseClassName(get_class($element)),
40
                'Page' => $element->getPageTitle(),
41
            ]));
42
        }
43
44
        return $output;
45
    }
46
47
    public function columns()
48
    {
49
        return [
50
            'Icon' => [
51
                'title' => '',
52
            ],
53
            'Title' => [
54
                'title' => _t(__CLASS__ . '.Title', 'Title'),
55
                'formatting' => function ($value, $item) {
56
                    if (!empty($value)) {
57
                        if ($item->EditLink) {
58
                            return $this->getEditLink($value, $item);
59
                        }
60
                        return $value;
61
                    }
62
                    return '<span class="element__note">' . _t(__CLASS__ . '.None', 'None') . '</span>';
63
                },
64
            ],
65
            'Summary' => [
66
                'title' => _t(__CLASS__ . '.Summary', 'Summary'),
67
                'casting' => 'HTMLText->RAW',
68
            ],
69
            'Type' => [
70
                'title' => _t(__CLASS__ . '.Type', 'Type'),
71
            ],
72
            'Page' => [
73
                'title' => _t(__CLASS__ . '.Page', 'Page'),
74
                'formatting' => function ($value, $item) {
75
                    if ($value) {
76
                        return $this->getEditLink($value, $item);
77
                    }
78
                },
79
            ],
80
        ];
81
    }
82
83
    /**
84
     * Helper method to return the link to edit an element
85
     *
86
     * @param string $value
87
     * @param ArrayData $item
88
     * @return string
89
     */
90
    protected function getEditLink($value, $item)
91
    {
92
        return sprintf(
93
            '<a class="grid-field__link" href="%s" title="%s">%s</a>',
94
            $item->EditLink,
95
            $value,
96
            $value
97
        );
98
    }
99
100
    /**
101
     * Add elemental CSS and a unique class name to the GridField
102
     *
103
     * @return GridField
104
     */
105
    public function getReportField()
106
    {
107
        Requirements::css('dnadesign/silverstripe-elemental:client/dist/styles/bundle.css');
108
109
        /** @var GridField $field */
110
        $field = parent::getReportField();
111
        $field->addExtraClass('elemental-report__grid-field');
112
113
        return $field;
114
    }
115
}
116