forked from silverstripe/silverstripe-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistryPage.php
146 lines (124 loc) · 4.24 KB
/
RegistryPage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace SilverStripe\Registry;
use Page;
use SilverStripe\Control\Controller;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\View\SSViewer;
use SilverStripe\View\ArrayData;
class RegistryPage extends Page
{
/**
* @deprecated 5.4.0 use class_description instead.
*/
private static $description = 'Shows large series of data in a filterable, searchable, and paginated list';
private static $class_description = 'Shows large series of data in a filterable, searchable, and paginated list';
private static $table_name = 'RegistryPage';
private static $icon_class = 'font-icon-p-data';
private static $db = [
'DataClass' => 'Varchar(100)',
'PageLength' => 'Int',
];
/**
* The default length of a page of registry entries
*
* @config
* @var integer
*/
private static $page_length_default = 10;
public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);
$labels['DataClass'] = _t(__CLASS__ . '.DataClassFieldLabel', "Data Class");
$labels['PageLength'] = _t(__CLASS__ . '.PageLengthFieldLabel', "Results page length");
return $labels;
}
public function getDataClasses()
{
$map = array();
foreach (ClassInfo::implementorsOf(RegistryDataInterface::class) as $class) {
$map[$class] = singleton($class)->singular_name();
}
return $map;
}
public function getDataClass()
{
return $this->getField('DataClass');
}
public function getDataSingleton()
{
$class = $this->getDataClass();
if (!$class) {
return null;
}
return singleton($this->getDataClass());
}
public function getPageLength()
{
$length = $this->getField('PageLength');
return $length ?: $this->config()->get('page_length_default');
}
public function getCMSFields()
{
$fields = parent::getCMSFields();
$classDropdown = DropdownField::create('DataClass', $this->fieldLabel('DataClass'), $this->getDataClasses());
$classDropdown->setEmptyString(_t(__CLASS__ . '.SelectDropdownDefault', 'Select one'));
$fields->addFieldToTab('Root.Main', $classDropdown, 'Content');
$fields->addFieldToTab(
'Root.Main',
NumericField::create('PageLength', $this->fieldLabel('PageLength')),
'Content'
);
return $fields;
}
public function LastUpdated()
{
$elements = DataList::create($this->dataClass);
$lastUpdated = DBDatetime::create('LastUpdated');
$lastUpdated->setValue($elements->max('LastEdited'));
return $lastUpdated;
}
/**
* Modified version of Breadcrumbs, to cater for viewing items.
*/
public function Breadcrumbs(
$maxDepth = 20,
$unlinked = false,
$stopAtPageType = false,
$showHidden = false,
$delimiter = '»'
) {
$page = $this;
$pages = [];
while ($page
&& (!$maxDepth || count($pages ?? []) < $maxDepth)
&& (!$stopAtPageType || $page->ClassName != $stopAtPageType)
) {
if ($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) {
$pages[] = $page;
}
$page = $page->Parent;
}
// Add on the item we're currently showing.
$controller = Controller::curr();
if ($controller) {
$request = $controller->getRequest();
if ($request->param('Action') === 'show') {
$id = $request->param('ID');
if ($id) {
$object = DataObject::get_by_id($this->getDataClass(), $id);
array_unshift($pages, $object);
}
}
}
$template = SSViewer::create('BreadcrumbsTemplate');
return $template->process($this->customise(ArrayData::create([
'Pages' => ArrayList::create(array_reverse($pages ?? []))
])));
}
}