1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Reports; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\Forms\GridField\GridField; |
9
|
|
|
use SilverStripe\ORM\ArrayList; |
10
|
|
|
use SilverStripe\Reports\Report; |
11
|
|
|
use SilverStripe\View\ArrayData; |
12
|
|
|
use SilverStripe\View\Requirements; |
13
|
|
|
|
14
|
|
|
class ElementTypeReport extends Report |
15
|
|
|
{ |
16
|
|
|
public function title() |
17
|
|
|
{ |
18
|
|
|
return _t(__CLASS__ . '.Title', 'Content block types'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function sourceRecords($params = []) |
22
|
|
|
{ |
23
|
|
|
$classes = $this->getElementTypes(); |
24
|
|
|
$output = ArrayList::create(); |
25
|
|
|
|
26
|
|
|
foreach ($classes as $class) { |
27
|
|
|
/** @var BaseElement $element */ |
28
|
|
|
$element = Injector::inst()->create($class); |
29
|
|
|
|
30
|
|
|
$output->push(ArrayData::create([ |
31
|
|
|
'Icon' => $element->getIcon(), |
32
|
|
|
'Type' => $element->getType(), |
33
|
|
|
'TypeNice' => $element->getTypeNice(), |
34
|
|
|
'ClassName' => $element->sanitiseClassName($class), |
35
|
|
|
'Total' => $class::get()->count(), |
36
|
|
|
])); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $output; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return an array of all valid classes that extend BaseElement |
44
|
|
|
* |
45
|
|
|
* @return string[] |
46
|
|
|
*/ |
47
|
|
|
protected function getElementTypes() |
48
|
|
|
{ |
49
|
|
|
$classes = ClassInfo::subclassesFor(BaseElement::class); |
50
|
|
|
$output = []; |
51
|
|
|
|
52
|
|
|
foreach ($classes as $className) { |
53
|
|
|
if ($className === BaseElement::class) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$output[] = $className; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $output; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function columns() |
64
|
|
|
{ |
65
|
|
|
// Get from Injector so substitutions are used... |
66
|
|
|
$inUseReport = ElementsInUseReport::singleton(); |
67
|
|
|
|
68
|
|
|
return [ |
69
|
|
|
'Icon' => [ |
70
|
|
|
'title' => '', |
71
|
|
|
], |
72
|
|
|
'Type' => [ |
73
|
|
|
'title' => _t(__CLASS__ . '.Type', 'Content Type'), |
74
|
|
|
'formatting' => function ($value, $item) use ($inUseReport) { |
75
|
|
|
return sprintf( |
76
|
|
|
'<a class="grid-field__link" href="%s" title="%s">%s</a>', |
77
|
|
|
$inUseReport->getLink( |
78
|
|
|
'?filters[' . $inUseReport::CLASS_NAME_FILTER_KEY . ']=' . $item->ClassName |
79
|
|
|
), |
80
|
|
|
$item->Type, |
81
|
|
|
$item->TypeNice |
82
|
|
|
); |
83
|
|
|
}, |
84
|
|
|
], |
85
|
|
|
'Total' => [ |
86
|
|
|
'title' => _t(__CLASS__ . '.Total', 'Total'), |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add elemental CSS and a unique class name to the GridField |
93
|
|
|
* |
94
|
|
|
* @return GridField |
95
|
|
|
*/ |
96
|
|
|
public function getReportField() |
97
|
|
|
{ |
98
|
|
|
Requirements::css('dnadesign/silverstripe-elemental:client/dist/styles/bundle.css'); |
99
|
|
|
|
100
|
|
|
/** @var GridField $field */ |
101
|
|
|
$field = parent::getReportField(); |
102
|
|
|
$field->addExtraClass('elemental-report__grid-field'); |
103
|
|
|
|
104
|
|
|
return $field; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|