Passed
Push — master ( c5d5b8...0bcd4c )
by Robbie
04:01
created

ElementsInUseReport   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getEditLink() 0 7 1
A getReportField() 0 9 1
A sourceRecords() 0 11 2
A columns() 0 43 4
A unsanitiseClassName() 0 3 1
A title() 0 3 1
1
<?php
2
3
namespace DNADesign\Elemental\Reports;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Forms\GridField\GridField;
7
use SilverStripe\ORM\DataList;
8
use SilverStripe\Reports\Report;
9
use SilverStripe\View\Requirements;
10
11
class ElementsInUseReport extends Report
12
{
13
    public function title()
14
    {
15
        return _t(__CLASS__ . '.ReportTitle', 'Content blocks in use');
16
    }
17
18
    public function sourceRecords($params = [])
19
    {
20
        /** @var DataList $elements */
21
        $elements = BaseElement::get()->exclude(['ClassName' => BaseElement::class]);
22
23
        if (isset($params['ClassName'])) {
24
            $className = $this->unsanitiseClassName($params['ClassName']);
25
            $elements = $elements->filter(['ClassName' => $className]);
26
        }
27
28
        return $elements;
29
    }
30
31
    public function columns()
32
    {
33
        return [
34
            'Icon' => [
35
                'title' => '',
36
                'formatting' => function ($value, BaseElement $item) {
37
                    return $item->getIcon();
38
                },
39
            ],
40
            'Title' => [
41
                'title' => _t(__CLASS__ . '.Title', 'Title'),
42
                'formatting' => function ($value, BaseElement $item) {
43
                    $value = $item->Title;
44
45
                    if (!empty($value)) {
46
                        if ($item->CMSEditLink()) {
47
                            return $this->getEditLink($value, $item);
48
                        }
49
                        return $value;
50
                    }
51
                    return '<span class="element__note">' . _t(__CLASS__ . '.None', 'None') . '</span>';
52
                },
53
            ],
54
            'Summary' => [
55
                'title' => _t(__CLASS__ . '.Summary', 'Summary'),
56
                'casting' => 'HTMLText->RAW',
57
                'formatting' => function ($value, BaseElement $item) {
58
                    return $item->getSummary();
59
                },
60
            ],
61
            'Type' => [
62
                'title' => _t(__CLASS__ . '.Type', 'Type'),
63
                'formatting' => function ($value, BaseElement $item) {
64
                    return $item->getTypeNice();
65
                },
66
            ],
67
            'Page.Title' => [
68
                'title' => _t(__CLASS__ . '.Page', 'Page'),
69
                'formatting' => function ($value, BaseElement $item) {
70
                    if ($value) {
71
                        return $this->getEditLink($value, $item);
72
                    }
73
                    return $item->getPageTitle();
74
                },
75
            ],
76
        ];
77
    }
78
79
    /**
80
     * Helper method to return the link to edit an element
81
     *
82
     * @param string $value
83
     * @param BaseElement $item
84
     * @return string
85
     */
86
    protected function getEditLink($value, $item)
87
    {
88
        return sprintf(
89
            '<a class="grid-field__link" href="%s" title="%s">%s</a>',
90
            $item->CMSEditLink(),
91
            $value,
92
            $value
93
        );
94
    }
95
96
    /**
97
     * Add elemental CSS and a unique class name to the GridField
98
     *
99
     * @return GridField
100
     */
101
    public function getReportField()
102
    {
103
        Requirements::css('dnadesign/silverstripe-elemental:client/dist/styles/bundle.css');
104
105
        /** @var GridField $field */
106
        $field = parent::getReportField();
107
        $field->addExtraClass('elemental-report__grid-field');
108
109
        return $field;
110
    }
111
112
    /**
113
     * Unsanitise a model class' name from a URL param
114
     *
115
     * See {@link \SilverStripe\Admin\ModelAdmin::unsanitiseClassName}
116
     *
117
     * @param string $class
118
     * @return string
119
     */
120
    protected function unsanitiseClassName($class)
121
    {
122
        return str_replace('-', '\\', $class);
123
    }
124
}
125