diff options
author | provokateurin <kate@provokateurin.de> | 2024-08-27 12:13:04 +0200 |
---|---|---|
committer | provokateurin <kate@provokateurin.de> | 2024-09-09 11:04:36 +0200 |
commit | b0baaaed9dfdce8c2630255d0d1921679579761d (patch) | |
tree | 552abcca392fd0e8b0241ff853c636f12fa6bf1c /lib/private/NavigationManager.php | |
parent | 0f732199d22cf85d991f48389c33fb6e76b7eefc (diff) | |
download | nextcloud-server-b0baaaed9dfdce8c2630255d0d1921679579761d.tar.gz nextcloud-server-b0baaaed9dfdce8c2630255d0d1921679579761d.zip |
feat(NavigationManager): Add default entries handling
Signed-off-by: provokateurin <kate@provokateurin.de>
Diffstat (limited to 'lib/private/NavigationManager.php')
-rw-r--r-- | lib/private/NavigationManager.php | 97 |
1 files changed, 87 insertions, 10 deletions
diff --git a/lib/private/NavigationManager.php b/lib/private/NavigationManager.php index 5d71c83e77a..e1fbb02fda4 100644 --- a/lib/private/NavigationManager.php +++ b/lib/private/NavigationManager.php @@ -7,6 +7,7 @@ */ namespace OC; +use InvalidArgumentException; use OC\App\AppManager; use OC\Group\Manager; use OCP\App\IAppManager; @@ -14,8 +15,10 @@ use OCP\IConfig; use OCP\IGroupManager; use OCP\INavigationManager; use OCP\IURLGenerator; +use OCP\IUser; use OCP\IUserSession; use OCP\L10N\IFactory; +use Psr\Log\LoggerInterface; /** * Manages the ownCloud navigation @@ -41,25 +44,30 @@ class NavigationManager implements INavigationManager { private $groupManager; /** @var IConfig */ private $config; - /** The default app for the current user (cached for the `add` function) */ - private ?string $defaultApp; + /** The default entry for the current user (cached for the `add` function) */ + private ?string $defaultEntry; /** User defined app order (cached for the `add` function) */ private array $customAppOrder; + private LoggerInterface $logger; - public function __construct(IAppManager $appManager, + public function __construct( + IAppManager $appManager, IURLGenerator $urlGenerator, IFactory $l10nFac, IUserSession $userSession, IGroupManager $groupManager, - IConfig $config) { + IConfig $config, + LoggerInterface $logger, + ) { $this->appManager = $appManager; $this->urlGenerator = $urlGenerator; $this->l10nFac = $l10nFac; $this->userSession = $userSession; $this->groupManager = $groupManager; $this->config = $config; + $this->logger = $logger; - $this->defaultApp = null; + $this->defaultEntry = null; } /** @@ -93,7 +101,7 @@ class NavigationManager implements INavigationManager { } // This is the default app that will always be shown first - $entry['default'] = ($entry['app'] ?? false) === $this->defaultApp; + $entry['default'] = ($entry['id'] ?? false) === $this->defaultEntry; // Set order from user defined app order $entry['order'] = (int)($this->customAppOrder[$id]['order'] ?? $entry['order'] ?? 100); } @@ -156,10 +164,10 @@ class NavigationManager implements INavigationManager { unset($navEntry); } - $activeApp = $this->getActiveEntry(); - if ($activeApp !== null) { + $activeEntry = $this->getActiveEntry(); + if ($activeEntry !== null) { foreach ($list as $index => &$navEntry) { - if ($navEntry['id'] == $activeApp) { + if ($navEntry['id'] == $activeEntry) { $navEntry['active'] = true; } else { $navEntry['active'] = false; @@ -213,7 +221,7 @@ class NavigationManager implements INavigationManager { ]); } - $this->defaultApp = $this->appManager->getDefaultAppForUser($this->userSession->getUser(), false); + $this->defaultEntry = $this->getDefaultEntryIdForUser($this->userSession->getUser(), false); if ($this->userSession->isLoggedIn()) { // Profile @@ -401,4 +409,73 @@ class NavigationManager implements INavigationManager { public function setUnreadCounter(string $id, int $unreadCounter): void { $this->unreadCounters[$id] = $unreadCounter; } + + public function get(string $id): array|null { + $this->init(); + foreach ($this->closureEntries as $c) { + $this->add($c()); + } + $this->closureEntries = []; + + return $this->entries[$id]; + } + + public function getDefaultEntryIdForUser(?IUser $user = null, bool $withFallbacks = true): string { + $this->init(); + $defaultEntryIds = explode(',', $this->config->getSystemValueString('defaultapp', '')); + $defaultEntryIds = array_filter($defaultEntryIds); + + $user ??= $this->userSession->getUser(); + + if ($user !== null) { + $userDefaultEntryIds = explode(',', $this->config->getUserValue($user->getUID(), 'core', 'defaultapp')); + $defaultEntryIds = array_filter(array_merge($userDefaultEntryIds, $defaultEntryIds)); + if (empty($defaultEntryIds) && $withFallbacks) { + /* Fallback on user defined apporder */ + $customOrders = json_decode($this->config->getUserValue($user->getUID(), 'core', 'apporder', '[]'), true, flags: JSON_THROW_ON_ERROR); + if (!empty($customOrders)) { + // filter only entries with app key (when added using closures or NavigationManager::add the app is not guaranteed to be set) + $customOrders = array_filter($customOrders, static fn ($entry) => isset($entry['app'])); + // sort apps by order + usort($customOrders, static fn ($a, $b) => $a['order'] - $b['order']); + // set default apps to sorted apps + $defaultEntryIds = array_map(static fn ($entry) => $entry['app'], $customOrders); + } + } + } + + if (empty($defaultEntryIds) && $withFallbacks) { + $defaultEntryIds = ['dashboard','files']; + } + + $entryIds = array_keys($this->entries); + + // Find the first app that is enabled for the current user + foreach ($defaultEntryIds as $defaultEntryId) { + if (in_array($defaultEntryId, $entryIds, true)) { + return $defaultEntryId; + } + } + + // Set fallback to always-enabled files app + return $withFallbacks ? 'files' : ''; + } + + public function getDefaultEntryIds(): array { + return explode(',', $this->config->getSystemValueString('defaultapp', 'dashboard,files')); + } + + public function setDefaultEntryIds(array $ids): void { + $this->init(); + $entryIds = array_keys($this->entries); + + foreach ($ids as $id) { + if (!in_array($id, $entryIds, true)) { + $this->logger->debug('Cannot set unavailable entry as default entry', ['missing_entry' => $id]); + throw new InvalidArgumentException('Entry not available'); + } + } + + $this->config->setSystemValue('defaultapp', join(',', $ids)); + } } |