Passed
Push — master ( ecccb9...b91912 )
by Robbie
08:46
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 10
c 0
b 0
f 0
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\ArrayData;
10
use SilverStripe\View\Requirements;
11
12
class ElementsInUseReport extends Report
13
{
14
    /**
15
     * The string used in GET params to filter the records in this report by element type
16
     *
17
     * @var string
18
     */
19
    const CLASS_NAME_FILTER_KEY = 'ClassName';
20
21
    public function title()
22
    {
23
        return _t(__CLASS__ . '.ReportTitle', 'Content blocks in use');
24
    }
25
26
    public function sourceRecords($params = [])
27
    {
28
        /** @var DataList $elements */
29
        $elements = BaseElement::get()->exclude(['ClassName' => BaseElement::class]);
30
31
        if (isset($params[static::CLASS_NAME_FILTER_KEY])) {
32
            $className = $this->unsanitiseClassName($params[static::CLASS_NAME_FILTER_KEY]);
33
            $elements = $elements->filter(['ClassName' => $className]);
34
        }
35
36
        return $elements;
37
    }
38
39
    public function columns()
40
    {
41
        return [
42
            'Icon' => [
43
                'title' => '',
44
                'formatting' => function ($value, BaseElement $item) {
45
                    return $item->getIcon();
46
                },
47
            ],
48
            'Title' => [
49
                'title' => _t(__CLASS__ . '.Title', 'Title'),
50
                'formatting' => function ($value, BaseElement $item) {
51
                    $value = $item->Title;
52
53
                    if (!empty($value)) {
54
                        if ($item->CMSEditLink()) {
55
                            return $this->getEditLink($value, $item);
56
                        }
57
                        return $value;
58
                    }
59
                    return '<span class="element__note">' . _t(__CLASS__ . '.None', 'None') . '</span>';
60
                },
61
            ],
62
            'Summary' => [
63
                'title' => _t(__CLASS__ . '.Summary', 'Summary'),
64
                'casting' => 'HTMLText->RAW',
65
                'formatting' => function ($value, BaseElement $item) {
66
                    return $item->getSummary();
67
                },
68
            ],
69
            'Type' => [
70
                'title' => _t(__CLASS__ . '.Type', 'Type'),
71
                'formatting' => function ($value, BaseElement $item) {
72
                    return $item->getTypeNice();
73
                },
74
            ],
75
            'Page.Title' => [
76
                'title' => _t(__CLASS__ . '.Page', 'Page'),
77
                'formatting' => function ($value, BaseElement $item) {
78
                    if ($value) {
79
                        return $this->getEditLink($value, $item);
80
                    }
81
                    return $item->getPageTitle();
82
                },
83
            ],
84
        ];
85
    }
86
87
    /**
88
     * Helper method to return the link to edit an element
89
     *
90
     * @param string $value
91
     * @param BaseElement $item
92
     * @return string
93
     */
94
    protected function getEditLink($value, BaseElement $item)
95
    {
96
        return sprintf(
97
            '<a class="grid-field__link" href="%s" title="%s">%s</a>',
98
            $item->CMSEditLink(),
99
            $value,
100
            $value
101
        );
102
    }
103
104
    /**
105
     * Add elemental CSS and a unique class name to the GridField
106
     *
107
     * @return GridField
108
     */
109
    public function getReportField()
110
    {
111
        Requirements::css('dnadesign/silverstripe-elemental:client/dist/styles/bundle.css');
112
113
        /** @var GridField $field */
114
        $field = parent::getReportField();
115
        $field->addExtraClass('elemental-report__grid-field');
116
117
        return $field;
118
    }
119
120
    /**
121
     * When used with silverstripe/reports >= 4.4, this method will automatically be added as breadcrumbs
122
     * leading up to this report.
123
     *
124
     * @return ArrayData[]
125
     */
126
    public function getBreadcrumbs()
127
    {
128
        $params = $this->getSourceParams();
0 ignored issues
show
Bug introduced by
The method getSourceParams() does not exist on DNADesign\Elemental\Reports\ElementsInUseReport. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

128
        /** @scrutinizer ignore-call */ 
129
        $params = $this->getSourceParams();
Loading history...
129
130
        // Only apply breadcrumbs if a "ClassName" filter is applied. This implies that we came from the
131
        // "element type report".
132
        if (!isset($params[static::CLASS_NAME_FILTER_KEY])) {
133
            return [];
134
        }
135
136
        $report = ElementTypeReport::create();
137
138
        return [ArrayData::create([
139
            'Title' => $report->title(),
140
            'Link' => $report->getLink(),
141
        ])];
142
    }
143
144
    /**
145
     * Unsanitise a model class' name from a URL param
146
     *
147
     * See {@link \SilverStripe\Admin\ModelAdmin::unsanitiseClassName}
148
     *
149
     * @param string $class
150
     * @return string
151
     */
152
    protected function unsanitiseClassName($class)
153
    {
154
        return str_replace('-', '\\', $class);
155
    }
156
}
157