Completed
Push — master ( c30dd6...2c9ce4 )
by Robbie
13s
created

ElementsInUseReport   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A title() 0 3 1
A getEditLink() 0 7 1
A getReportField() 0 9 1
B sourceRecords() 0 25 4
B columns() 0 29 4
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 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,
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...
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
            ],
68
            'Type' => [
69
                'title' => _t(__CLASS__ . '.Type', 'Type'),
70
            ],
71
            'Page' => [
72
                'title' => _t(__CLASS__ . '.Page', 'Page'),
73
                'formatting' => function ($value, $item) {
74
                    if ($value) {
75
                        return $this->getEditLink($value, $item);
76
                    }
77
                },
78
            ],
79
        ];
80
    }
81
82
    /**
83
     * Helper method to return the link to edit an element
84
     *
85
     * @param string $value
86
     * @param ArrayData $item
87
     * @return string
88
     */
89
    protected function getEditLink($value, $item)
90
    {
91
        return sprintf(
92
            '<a class="grid-field__link" href="%s" title="%s">%s</a>',
93
            $item->EditLink,
94
            $value,
95
            $value
96
        );
97
    }
98
99
    /**
100
     * Add elemental CSS and a unique class name to the GridField
101
     *
102
     * @return GridField
103
     */
104
    public function getReportField()
105
    {
106
        Requirements::css('dnadesign/silverstripe-elemental:css/elemental-admin.css');
107
108
        /** @var GridField $field */
109
        $field = parent::getReportField();
110
        $field->addExtraClass('elemental-report__grid-field');
111
112
        return $field;
113
    }
114
}
115