From 63a64df656d79406af58490ba4b92b2f4386da94 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 22 Oct 2024 15:16:13 +1300 Subject: [PATCH] API Move logic from silverstripe/cms into central place --- src/ORM/Hierarchy/Hierarchy.php | 35 ++++++++++++++++++++++++++++ src/Security/Group.php | 10 -------- src/Security/PermissionCheckable.php | 14 +++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 src/Security/PermissionCheckable.php diff --git a/src/ORM/Hierarchy/Hierarchy.php b/src/ORM/Hierarchy/Hierarchy.php index 4ae4014668d..c9eff0c8183 100644 --- a/src/ORM/Hierarchy/Hierarchy.php +++ b/src/ORM/Hierarchy/Hierarchy.php @@ -617,6 +617,30 @@ public function showingCMSTree() && in_array($controller->getAction(), ["treeview", "listview", "getsubtree"]); } + /** + * Return the CSS classes to apply to this node in the CMS tree. + */ + public function CMSTreeClasses(): string + { + $owner = $this->getOwner(); + $classes = sprintf('class-%s', Convert::raw2htmlid(get_class($owner))); + + if (!$owner->canAddChildren()) { + $classes .= " nochildren"; + } + + if (!$owner->canEdit() && !$owner->canAddChildren()) { + if (!$owner->canView()) { + $classes .= " disabled"; + } else { + $classes .= " edit-disabled"; + } + } + + $owner->invokeWithExtensions('updateCMSTreeClasses', $classes); + return $classes; + } + /** * Find the first class in the inheritance chain that has Hierarchy extension applied * @@ -777,6 +801,17 @@ public function getBreadcrumbs($separator = ' » ') return implode($separator ?? '', $crumbs); } + /** + * Get the title that will be used in TreeDropdownField and other tree structures. + */ + public function getTreeTitle(): string + { + $owner = $this->getOwner(); + $title = $owner->MenuTitle ?: $owner->Title; + $owner->extend('updateTreeTitle', $title); + return Convert::raw2xml($title ?? ''); + } + /** * Get the name of the dedicated sort field, if there is one. */ diff --git a/src/Security/Group.php b/src/Security/Group.php index a1816bbc682..4298c11dd89 100755 --- a/src/Security/Group.php +++ b/src/Security/Group.php @@ -494,16 +494,6 @@ public function stageChildren() ->sort('"Sort"'); } - /** - * @return string - */ - public function getTreeTitle() - { - $title = htmlspecialchars($this->Title ?? '', ENT_QUOTES); - $this->extend('updateTreeTitle', $title); - return $title; - } - /** * Overloaded to ensure the code is always descent. * diff --git a/src/Security/PermissionCheckable.php b/src/Security/PermissionCheckable.php new file mode 100644 index 00000000000..1e5348f53a2 --- /dev/null +++ b/src/Security/PermissionCheckable.php @@ -0,0 +1,14 @@ +