Passed
Pull Request — master (#135)
by Robbie
02:01
created

ElementsInUseReport::getReportField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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__ . '.Title', 'Content blocks in use');
18
    }
19
20
    public function description()
21
    {
22
        return _t(__CLASS__ . '.Description', 'Show which content blocks are in use');
23
    }
24
25
    public function sourceRecords($params = [])
26
    {
27
        /** @var BaseElement[] $elements */
28
        $elements = BaseElement::get();
29
30
        $output = ArrayList::create();
31
32
        foreach ($elements as $element) {
33
            // Skip if filtering and looking at a different record
34
            if (isset($params['ClassName']) && $params['ClassName'] !== $element->sanitiseClassName($element)) {
35
                continue;
36
            }
37
38
            $output->push(ArrayData::create([
39
                'Icon' => $element->getIcon(),
40
                'Title' => $element->Title,
0 ignored issues
show
Bug Best Practice introduced by
The property Title does not exist on DNADesign\Elemental\Models\BaseElement. Since you implemented __get, consider adding a @property annotation.
Loading history...
41
                'EditLink' => $element->CMSEditLink(),
42
                'Summary' => $element->ElementSummary(),
0 ignored issues
show
Bug introduced by
The method ElementSummary() does not exist on DNADesign\Elemental\Models\BaseElement. 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

42
                'Summary' => $element->/** @scrutinizer ignore-call */ ElementSummary(),
Loading history...
43
                'Type' => $element->getTypeNice(),
44
                'ClassName' => $element->sanitiseClassName(get_class($element)),
45
                'Page' => $element->getPageTitle(),
0 ignored issues
show
Bug introduced by
The method getPageTitle() does not exist on DNADesign\Elemental\Models\BaseElement. 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

45
                'Page' => $element->/** @scrutinizer ignore-call */ getPageTitle(),
Loading history...
46
            ]));
47
        }
48
49
        return $output;
50
    }
51
52
    public function columns()
53
    {
54
        return [
55
            'Icon' => [
56
                'title' => '',
57
            ],
58
            'Title' => [
59
                'title' => _t(__CLASS__ . '.Title', 'Title'),
60
                'formatting' => function ($value, $item) {
61
                    if (!empty($value)) {
62
                        if ($item->EditLink) {
63
                            return $this->getEditLink($value, $item);
64
                        }
65
                        return $value;
66
                    }
67
                    return '<span class="element__note">' . _t(__CLASS__ . '.None', 'None') . '</span>';
68
                },
69
            ],
70
            'Summary' => [
71
                'title' => _t(__CLASS__ . '.Summary', 'Summary'),
72
            ],
73
            'Type' => [
74
                'title' => _t(__CLASS__ . '.Type', 'Type'),
75
            ],
76
            'Page' => [
77
                'title' => _t(__CLASS__ . '.Page', 'Page'),
78
                'formatting' => function ($value, $item) {
79
                    if ($value) {
80
                        return $this->getEditLink($value, $item);
81
                    }
82
                },
83
            ],
84
        ];
85
    }
86
87
    /**
88
     * Helper method to return the link to edit an element
89
     *
90
     * @param string $value
91
     * @param ArrayData $item
92
     * @return string
93
     */
94
    protected function getEditLink($value, $item)
95
    {
96
        return sprintf(
97
            '<a href="grid-field__link" href="%s" title="%s">%s</a>',
98
            $item->EditLink,
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:css/elemental-admin.css');
112
113
        /** @var GridField $field */
114
        $field = parent::getReportField();
115
        $field->addExtraClass('elemental-report__grid-field');
116
117
        return $field;
118
    }
119
}
120