1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Reports\ElementUseReport; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Core\Convert; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\ORM\ArrayList; |
10
|
|
|
use SilverStripe\Reports\Report; |
11
|
|
|
use SilverStripe\View\ArrayData; |
12
|
|
|
|
13
|
|
|
class ElementUseReport extends Report |
14
|
|
|
{ |
15
|
|
|
protected $title = 'Content Blocks in Use'; |
16
|
|
|
|
17
|
|
|
protected $description = 'Show what Content Blocks are being used.'; |
18
|
|
|
|
19
|
|
|
public function getCount($params = array()) |
20
|
|
|
{ |
21
|
|
|
return BaseElement::get()->count(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function sourceRecords($params = []) |
25
|
|
|
{ |
26
|
|
|
if (empty($params)) { |
27
|
|
|
$classes = ClassInfo::subclassesFor(BaseElement::class); |
28
|
|
|
$output = new ArrayList(); |
29
|
|
|
|
30
|
|
|
foreach ($classes as $class) { |
31
|
|
|
if ($class === BaseElement::class) { |
32
|
|
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$inst = Injector::inst()->create($class); |
36
|
|
|
|
37
|
|
|
$output->push(new ArrayData([ |
38
|
|
|
'Icon' => $inst->getIcon(), |
39
|
|
|
'Title' => $inst->getTypeNice(), |
40
|
|
|
'ClassName' => $inst->sanitiseClassName($class), |
41
|
|
|
'Total' => $class::get()->count() |
42
|
|
|
])); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $output; |
46
|
|
|
} elseif (isset($params['ClassName'])) { |
47
|
|
|
$convertClass = Injector::inst()->create(BaseElement::class)->unsanitiseClassName($params['ClassName']); |
48
|
|
|
|
49
|
|
|
if (ClassInfo::exists($convertClass)) { |
50
|
|
|
return $convertClass::get()->sort('Title', 'ASC'); |
51
|
|
|
} else { |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function columns() |
57
|
|
|
{ |
58
|
|
|
$params = $this->params; |
|
|
|
|
59
|
|
|
|
60
|
|
|
if (empty($params)) { |
61
|
|
|
return [ |
62
|
|
|
'Icon' => [ |
63
|
|
|
'title' => '' |
64
|
|
|
], |
65
|
|
|
'Title' => [ |
66
|
|
|
'title' => _t(__CLASS__.'.Title', 'Content Type') |
67
|
|
|
], |
68
|
|
|
'Total' => [ |
69
|
|
|
'title' => _t(__CLASS__.'.Total', 'Total'), |
70
|
|
|
'link' => function ($value, $item) { |
71
|
|
|
return sprintf( |
72
|
|
|
'<a class="grid-field__link" href="%s" title="%s">%s</a>', |
73
|
|
|
Convert::raw2att($this->getLink('?filters[ClassName]='. $item->ClassName)), |
74
|
|
|
Convert::raw2att($value), |
|
|
|
|
75
|
|
|
Convert::raw2xml($value) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
] |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return [ |
83
|
|
|
'EditorPreview' => [ |
84
|
|
|
'title' => '' |
85
|
|
|
], |
86
|
|
|
'PageTitle' => [ |
87
|
|
|
'title' => _t(__CLASS__.'.Page', 'Page'), |
88
|
|
|
'link' => true |
89
|
|
|
] |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.