forked from silverstripe/silverstripe-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHierarchy.php
More file actions
586 lines (531 loc) · 20.3 KB
/
Hierarchy.php
File metadata and controls
586 lines (531 loc) · 20.3 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<?php
namespace SilverStripe\ORM\Hierarchy;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Core\ClassInfo;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use Exception;
use SilverStripe\Dev\Deprecation;
use SilverStripe\View\ViewableData;
/**
* DataObjects that use the Hierarchy extension can be be organised as a hierarchy, with children and parents. The most
* obvious example of this is SiteTree.
*
* @property int $ParentID
* @method DataObject Parent()
* @extends DataExtension<DataObject&static>
*/
class Hierarchy extends DataExtension
{
/**
* The lower bounds for the amount of nodes to mark. If set, the logic will expand nodes until it reaches at least
* this number, and then stops. Root nodes will always show regardless of this setting. Further nodes can be
* lazy-loaded via ajax. This isn't a hard limit. Example: On a value of 10, with 20 root nodes, each having 30
* children, the actual node count will be 50 (all root nodes plus first expanded child).
*
* @config
* @var int
*/
private static $node_threshold_total = 50;
/**
* Limit on the maximum children a specific node can display. Serves as a hard limit to avoid exceeding available
* server resources in generating the tree, and browser resources in rendering it. Nodes with children exceeding
* this value typically won't display any children, although this is configurable through the $nodeCountCallback
* parameter in {@link getChildrenAsUL()}. "Root" nodes will always show all children, regardless of this setting.
*
* @config
* @var int
*/
private static $node_threshold_leaf = 250;
/**
* A list of classnames to exclude from display in both the CMS and front end
* displays. ->Children() and ->AllChildren affected.
* Especially useful for big sets of pages like listings
* If you use this, and still need the classes to be editable
* then add a model admin for the class
* Note: Does not filter subclasses (non-inheriting)
*
* @var array
* @config
*/
private static $hide_from_hierarchy = [];
/**
* A list of classnames to exclude from display in the page tree views of the CMS,
* unlike $hide_from_hierarchy above which effects both CMS and front end.
* Especially useful for big sets of pages like listings
* If you use this, and still need the classes to be editable
* then add a model admin for the class
* Note: Does not filter subclasses (non-inheriting)
*
* @var array
* @config
*/
private static $hide_from_cms_tree = [];
/**
* Used to enable or disable the prepopulation of the numchildren cache.
* Defaults to true.
*
* @config
* @var boolean
*/
private static $prepopulate_numchildren_cache = true;
/**
* Prevent virtual page virtualising these fields
*
* @config
* @var array
*/
private static $non_virtual_fields = [
'_cache_children',
];
/**
* A cache used by numChildren().
* Clear through {@link flushCache()}.
* version (int)0 means not on this stage.
*
* @var array
*/
protected static $cache_numChildren = [];
public static function get_extra_config($class, $extension, $args)
{
return [
'has_one' => ['Parent' => $class]
];
}
/**
* Validate the owner object - check for existence of infinite loops.
*
* @param ValidationResult $validationResult
* @deprecated 5.4.0 Will be renamed to updateValidate()
*/
public function validate(ValidationResult $validationResult)
{
Deprecation::notice('5.4.0', 'Will be renamed to updateValidate()');
// The object is new, won't be looping.
$owner = $this->owner;
if (!$owner->ID) {
return;
}
// The object has no parent, won't be looping.
if (!$owner->ParentID) {
return;
}
// The parent has not changed, skip the check for performance reasons.
if (!$owner->isChanged('ParentID')) {
return;
}
// Walk the hierarchy upwards until we reach the top, or until we reach the originating node again.
$node = $owner;
while ($node && $node->ParentID) {
if ((int)$node->ParentID === (int)$owner->ID) {
// Hierarchy is looping.
$validationResult->addError(
_t(
__CLASS__ . '.InfiniteLoopNotAllowed',
'Infinite loop found within the "{type}" hierarchy. Please change the parent to resolve this',
'First argument is the class that makes up the hierarchy.',
['type' => get_class($owner)]
),
'bad',
'INFINITE_LOOP'
);
break;
}
$node = $node->Parent();
}
}
/**
* Get a list of this DataObject's and all it's descendants IDs.
*
* @return int[]
*/
public function getDescendantIDList()
{
$idList = [];
$this->loadDescendantIDListInto($idList);
return $idList;
}
/**
* Get a list of this DataObject's and all it's descendants ID, and put them in $idList.
*
* @param array $idList Array to put results in.
* @param DataObject|Hierarchy $node
*/
protected function loadDescendantIDListInto(&$idList, $node = null)
{
if (!$node) {
$node = $this->owner;
}
$children = $node->AllChildren();
foreach ($children as $child) {
if (!in_array($child->ID, $idList ?? [])) {
$idList[] = $child->ID;
$this->loadDescendantIDListInto($idList, $child);
}
}
}
/**
* Get the children for this DataObject filtered by canView()
*
* @return SS_List<DataObject&static>
*/
public function Children()
{
$children = $this->owner->_cache_children;
if ($children) {
return $children;
}
$children = $this
->owner
->stageChildren(false)
->filterByCallback(function (DataObject $record) {
return $record->canView();
});
$this->owner->_cache_children = $children;
return $children;
}
/**
* Return all children, including those 'not in menus'.
*
* @return DataList<DataObject&static>
*/
public function AllChildren()
{
return $this->owner->stageChildren(true);
}
/**
* Return all children, including those that have been deleted but are still in live.
* - Deleted children will be marked as "DeletedFromStage"
* - Added children will be marked as "AddedToStage"
* - Modified children will be marked as "ModifiedOnStage"
* - Everything else has "SameOnStage" set, as an indicator that this information has been looked up.
*
* @return ArrayList<DataObject&static>
*/
public function AllChildrenIncludingDeleted()
{
/** @var DataObject|Hierarchy|Versioned $owner */
$owner = $this->owner;
$stageChildren = $owner->stageChildren(true);
// Add live site content that doesn't exist on the stage site, if required.
if ($owner->hasExtension(Versioned::class) && $owner->hasStages()) {
// Next, go through the live children. Only some of these will be listed
$liveChildren = $owner->liveChildren(true, true);
if ($liveChildren) {
$merged = new ArrayList();
$merged->merge($stageChildren);
$merged->merge($liveChildren);
$stageChildren = $merged;
}
}
$owner->extend("augmentAllChildrenIncludingDeleted", $stageChildren);
return $stageChildren;
}
/**
* Return all the children that this page had, including pages that were deleted from both stage & live.
*
* @return DataList<DataObject&static>
* @throws Exception
*/
public function AllHistoricalChildren()
{
/** @var DataObject|Versioned|Hierarchy $owner */
$owner = $this->owner;
if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) {
throw new Exception(
'Hierarchy->AllHistoricalChildren() only works with Versioned extension applied with staging'
);
}
$baseTable = $owner->baseTable();
$parentIDColumn = $owner->getSchema()->sqlColumnForField($owner, 'ParentID');
return Versioned::get_including_deleted(
$owner->baseClass(),
[ $parentIDColumn => $owner->ID ],
"\"{$baseTable}\".\"ID\" ASC"
);
}
/**
* Return the number of children that this page ever had, including pages that were deleted.
*
* @return int
*/
public function numHistoricalChildren()
{
return $this->AllHistoricalChildren()->count();
}
/**
* Return the number of direct children. By default, values are cached after the first invocation. Can be
* augmented by {@link augmentNumChildrenCountQuery()}.
*
* @param bool $cache Whether to retrieve values from cache
* @return int
*/
public function numChildren($cache = true)
{
$baseClass = $this->owner->baseClass();
$cacheType = 'numChildren';
$id = $this->owner->ID;
// cached call
if ($cache) {
if (isset(Hierarchy::$cache_numChildren[$baseClass][$cacheType][$id])) {
return Hierarchy::$cache_numChildren[$baseClass][$cacheType][$id];
} elseif (isset(Hierarchy::$cache_numChildren[$baseClass][$cacheType]['_complete'])) {
// If the cache is complete and we didn't find our ID in the cache, it means this object is childless.
return 0;
}
}
// We call stageChildren(), because Children() has canView() filtering
$numChildren = (int)$this->owner->stageChildren(true)->Count();
// Save if caching
if ($cache) {
Hierarchy::$cache_numChildren[$baseClass][$cacheType][$id] = $numChildren;
}
return $numChildren;
}
/**
* Pre-populate any appropriate caches prior to rendering a tree.
* This is used to allow for the efficient rendering of tree views, notably in the CMS.
* In the case of Hierarchy, it caches numChildren values. Other extensions can provide an
* onPrepopulateTreeDataCache(DataList $recordList = null, array $options) methods to hook
* into this event as well.
*
* @param DataList|array $recordList The list of records to prepopulate caches for. Null for all records.
* @param array $options A map of hints about what should be cached. "numChildrenMethod" and
* "childrenMethod" are allowed keys.
*/
public function prepopulateTreeDataCache($recordList = null, array $options = [])
{
if (empty($options['numChildrenMethod']) || $options['numChildrenMethod'] === 'numChildren') {
$idList = is_array($recordList) ? $recordList :
($recordList instanceof DataList ? $recordList->column('ID') : null);
Hierarchy::prepopulate_numchildren_cache($this->getHierarchyBaseClass(), $idList);
}
$this->owner->extend('onPrepopulateTreeDataCache', $recordList, $options);
}
/**
* Pre-populate the cache for Versioned::get_versionnumber_by_stage() for
* a list of record IDs, for more efficient database querying. If $idList
* is null, then every record will be pre-cached.
*
* @param string $baseClass
* @param array $idList
*/
public static function prepopulate_numchildren_cache($baseClass, $idList = null)
{
if (!Config::inst()->get(static::class, 'prepopulate_numchildren_cache')) {
return;
}
/** @var DataObject&static $dummyObject */
$dummyObject = DataObject::singleton($baseClass);
$baseTable = $dummyObject->baseTable();
$idColumn = Convert::symbol2sql("{$baseTable}.ID");
// Get the stageChildren() result of a dummy object and break down into a generic query
$query = $dummyObject->stageChildren(true, true)->dataQuery()->query();
// optional ID-list filter
if ($idList) {
// Validate the ID list
foreach ($idList as $id) {
if (!is_numeric($id)) {
throw new \InvalidArgumentException(
"Bad ID passed to Versioned::prepopulate_numchildren_cache() in \$idList: " . $id
);
}
}
$query->addWhere(['"ParentID" IN (' . DB::placeholders($idList) . ')' => $idList]);
}
$query->setOrderBy(null);
$query->setSelect([
'"ParentID"',
"COUNT(DISTINCT $idColumn) AS \"NumChildren\"",
]);
$query->setGroupBy([Convert::symbol2sql("ParentID")]);
$numChildren = $query->execute()->map();
Hierarchy::$cache_numChildren[$baseClass]['numChildren'] = $numChildren;
if (!$idList) {
// If all objects are being cached, mark this cache as complete
// to avoid counting children of childless object.
Hierarchy::$cache_numChildren[$baseClass]['numChildren']['_complete'] = true;
}
}
/**
* Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree?
*
* @return bool
*/
public function showingCMSTree()
{
if (!Controller::has_curr() || !class_exists(LeftAndMain::class)) {
return false;
}
$controller = Controller::curr();
return $controller instanceof LeftAndMain
&& in_array($controller->getAction(), ["treeview", "listview", "getsubtree"]);
}
/**
* Find the first class in the inheritance chain that has Hierarchy extension applied
*
* @return string
*/
private function getHierarchyBaseClass(): string
{
$ancestry = ClassInfo::ancestry($this->owner);
$ancestorClass = array_shift($ancestry);
while ($ancestorClass && !ViewableData::has_extension($ancestorClass, Hierarchy::class)) {
$ancestorClass = array_shift($ancestry);
}
return $ancestorClass;
}
/**
* Return children in the stage site.
*
* @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when
* extension is applied to {@link SiteTree}.
* @param bool $skipParentIDFilter Set to true to suppress the ParentID and ID where statements.
* @return DataList<DataObject&static>
*/
public function stageChildren($showAll = false, $skipParentIDFilter = false)
{
$owner = $this->owner;
$hideFromHierarchy = $owner->config()->hide_from_hierarchy;
$hideFromCMSTree = $owner->config()->hide_from_cms_tree;
$class = $this->getHierarchyBaseClass();
$schema = DataObject::getSchema();
$tableForParentID = $schema->tableForField($class, 'ParentID');
$tableForID = $schema->tableForField($class, 'ID');
$staged = DataObject::get($class)->where(sprintf(
'%s.%s <> %s.%s',
Convert::symbol2sql($tableForParentID),
Convert::symbol2sql("ParentID"),
Convert::symbol2sql($tableForID),
Convert::symbol2sql("ID")
));
if (!$skipParentIDFilter) {
// There's no filtering by ID if we don't have an ID.
$staged = $staged->filter('ParentID', (int)$this->owner->ID);
}
if ($hideFromHierarchy) {
$staged = $staged->exclude('ClassName', $hideFromHierarchy);
}
if ($hideFromCMSTree && $this->showingCMSTree()) {
$staged = $staged->exclude('ClassName', $hideFromCMSTree);
}
if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) {
$staged = $staged->filter('ShowInMenus', 1);
}
$this->owner->extend("augmentStageChildren", $staged, $showAll);
return $staged;
}
/**
* Return children in the live site, if it exists.
*
* @param bool $showAll Include all of the elements, even those not shown in the menus. Only
* applicable when extension is applied to {@link SiteTree}.
* @param bool $onlyDeletedFromStage Only return items that have been deleted from stage
* @return DataList<DataObject&static>
* @throws Exception
*/
public function liveChildren($showAll = false, $onlyDeletedFromStage = false)
{
/** @var Versioned|DataObject|Hierarchy $owner */
$owner = $this->owner;
if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) {
throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied with staging');
}
$hideFromHierarchy = $owner->config()->hide_from_hierarchy;
$hideFromCMSTree = $owner->config()->hide_from_cms_tree;
$children = DataObject::get($this->getHierarchyBaseClass())
->filter('ParentID', (int)$owner->ID)
->exclude('ID', (int)$owner->ID)
->setDataQueryParam([
'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage',
'Versioned.stage' => 'Live'
]);
if ($hideFromHierarchy) {
$children = $children->exclude('ClassName', $hideFromHierarchy);
}
if ($hideFromCMSTree && $this->showingCMSTree()) {
$children = $children->exclude('ClassName', $hideFromCMSTree);
}
if (!$showAll && DataObject::getSchema()->fieldSpec($owner, 'ShowInMenus')) {
$children = $children->filter('ShowInMenus', 1);
}
return $children;
}
/**
* Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing
* is returned.
*
* @param string $filter
* @return DataObject&static
*/
public function getParent($filter = null)
{
$parentID = $this->owner->ParentID;
if (empty($parentID)) {
return null;
}
$baseClass = $this->owner->baseClass();
$idSQL = $this->owner->getSchema()->sqlColumnForField($baseClass, 'ID');
return DataObject::get_one($baseClass, [
[$idSQL => $parentID],
$filter
]);
}
/**
* Return all the parents of this class in a set ordered from the closest to furtherest parent.
*
* @param bool $includeSelf
* @return ArrayList<DataObject&static>
*/
public function getAncestors($includeSelf = false)
{
$ancestors = new ArrayList();
$object = $this->owner;
if ($includeSelf) {
$ancestors->push($object);
}
while ($object = $object->getParent()) {
$ancestors->push($object);
}
return $ancestors;
}
/**
* Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute.
*
* @param string $separator
* @return string
*/
public function getBreadcrumbs($separator = ' » ')
{
$crumbs = [];
$ancestors = array_reverse($this->owner->getAncestors()->toArray() ?? []);
/** @var DataObject $ancestor */
foreach ($ancestors as $ancestor) {
$crumbs[] = $ancestor->getTitle();
}
$crumbs[] = $this->owner->getTitle();
return implode($separator ?? '', $crumbs);
}
/**
* Flush all Hierarchy caches:
* - Children (instance)
* - NumChildren (instance)
*
* @deprecated 5.4.0 Will be renamed to onFlushCache()
*/
public function flushCache()
{
Deprecation::notice('5.4.0', 'Will be renamed to onFlushCache()');
$this->owner->_cache_children = null;
Hierarchy::$cache_numChildren = [];
}
}