-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathButtonRegistry.php
More file actions
104 lines (88 loc) · 3.3 KB
/
Copy pathButtonRegistry.php
File metadata and controls
104 lines (88 loc) · 3.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
<?php
namespace FriendsOfRedaxo\QuickNavigation\Button;
use rex;
use rex_addon;
class ButtonRegistry
{
/**
* @var array<array{id: string, label: string, instance: mixed, priority: int}> an array that contains button instances and their priorities
*/
protected static array $buttons = [];
/**
* Registers a button with an optional priority.
* Lower priority values cause the button to appear earlier in the list.
*
* @param string $id Unique identifier for the button (e.g. 'article_navigation', 'watson')
* @param string $label Human readable label for config (e.g. 'Artikelnavigation', 'Watson')
*/
public static function registerButton(ButtonInterface $buttonInstance, int $priority = 10, string $id = '', string $label = ''): void
{
// Generate ID from class name if not provided
if ($id === '') {
$className = get_class($buttonInstance);
$id = strtolower(str_replace('Button', '', substr($className, strrpos($className, '\\') + 1)));
}
// Use ID as label if not provided
if ($label === '') {
$label = ucfirst(str_replace('_', ' ', $id));
}
self::$buttons[] = [
'id' => $id,
'label' => $label,
'instance' => $buttonInstance,
'priority' => $priority
];
}
/**
* Returns the buttons sorted by their priority, filtered by user preferences.
*/
public static function getButtonsOutput(): string
{
$user = rex::getUser();
if (!$user) {
return '';
}
$userId = $user->getId();
$addon = rex_addon::get('quick_navigation');
// Get disabled buttons for this user (Opt-Out)
$disabledButtons = $addon->getConfig('quick_navigation_disabled_buttons' . $userId, []);
if (!is_array($disabledButtons)) {
$disabledButtons = [];
}
// Sorts the buttons based on their priority
usort(self::$buttons, static function (array $a, array $b): int {
return $a['priority'] <=> $b['priority'];
});
$resultString = '';
foreach (self::$buttons as $button) {
// Skip if button is disabled for this user
if (in_array($button['id'], $disabledButtons, true)) {
continue;
}
// Since all instances implement ButtonInterface, it's guaranteed that get() exists.
$resultString .= $button['instance']->get();
}
return $resultString;
}
/**
* Returns all available buttons with their metadata for configuration.
* @return array<array{id: string, label: string, priority: int}>
*/
public static function getAvailableButtons(): array
{
// Sort by priority
$sortedButtons = self::$buttons;
usort($sortedButtons, static function (array $a, array $b): int {
return $a['priority'] <=> $b['priority'];
});
$result = [];
foreach ($sortedButtons as $button) {
$result[] = [
'id' => $button['id'],
'label' => $button['label'],
'priority' => $button['priority']
];
}
return $result;
}
}