123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- namespace OC;
- use OC\App\AppManager;
- use OC\Group\Manager;
- use OCP\App\IAppManager;
- use OCP\IConfig;
- use OCP\IGroupManager;
- use OCP\INavigationManager;
- use OCP\IURLGenerator;
- use OCP\IUserSession;
- use OCP\L10N\IFactory;
- class NavigationManager implements INavigationManager {
- protected $entries = [];
- protected $closureEntries = [];
- protected $activeEntry;
- protected $unreadCounters = [];
-
- protected $init = false;
-
- protected $appManager;
-
- private $urlGenerator;
-
- private $l10nFac;
-
- private $userSession;
-
- private $groupManager;
-
- private $config;
-
- private ?string $defaultApp;
-
- private array $customAppOrder;
- public function __construct(IAppManager $appManager,
- IURLGenerator $urlGenerator,
- IFactory $l10nFac,
- IUserSession $userSession,
- IGroupManager $groupManager,
- IConfig $config) {
- $this->appManager = $appManager;
- $this->urlGenerator = $urlGenerator;
- $this->l10nFac = $l10nFac;
- $this->userSession = $userSession;
- $this->groupManager = $groupManager;
- $this->config = $config;
- $this->defaultApp = null;
- }
-
- public function add($entry) {
- if ($entry instanceof \Closure) {
- $this->closureEntries[] = $entry;
- return;
- }
- $this->init();
- $id = $entry['id'];
- $entry['active'] = false;
- $entry['unread'] = $this->unreadCounters[$id] ?? 0;
- if (!isset($entry['icon'])) {
- $entry['icon'] = '';
- }
- if (!isset($entry['classes'])) {
- $entry['classes'] = '';
- }
- if (!isset($entry['type'])) {
- $entry['type'] = 'link';
- }
- if ($entry['type'] === 'link') {
-
- if (!isset($entry['app']) && $this->appManager->isEnabledForUser($id)) {
- $entry['app'] = $id;
- }
-
- $entry['default'] = ($entry['app'] ?? false) === $this->defaultApp;
-
- $entry['order'] = (int)($this->customAppOrder[$id]['order'] ?? $entry['order'] ?? 100);
- }
- $this->entries[$id] = $entry;
- }
-
- public function getAll(string $type = 'link'): array {
- $this->init();
- foreach ($this->closureEntries as $c) {
- $this->add($c());
- }
- $this->closureEntries = [];
- $result = $this->entries;
- if ($type !== 'all') {
- $result = array_filter($this->entries, function ($entry) use ($type) {
- return $entry['type'] === $type;
- });
- }
- return $this->proceedNavigation($result, $type);
- }
-
- private function proceedNavigation(array $list, string $type): array {
- uasort($list, function ($a, $b) {
- if (($a['default'] ?? false) xor ($b['default'] ?? false)) {
-
- return ($a['default'] ?? false) ? -1 : 1;
- } elseif (isset($a['order']) && isset($b['order'])) {
-
- return ($a['order'] < $b['order']) ? -1 : 1;
- } elseif (isset($a['order']) || isset($b['order'])) {
-
- return isset($a['order']) ? -1 : 1;
- } else {
-
- return ($a['name'] < $b['name']) ? -1 : 1;
- }
- });
- if ($type === 'all' || $type === 'link') {
-
-
- foreach ($list as $index => &$navEntry) {
- if ($navEntry['type'] === 'link') {
- $navEntry['default'] = true;
- break;
- }
- }
- unset($navEntry);
- }
- $activeApp = $this->getActiveEntry();
- if ($activeApp !== null) {
- foreach ($list as $index => &$navEntry) {
- if ($navEntry['id'] == $activeApp) {
- $navEntry['active'] = true;
- } else {
- $navEntry['active'] = false;
- }
- }
- unset($navEntry);
- }
- return $list;
- }
-
- public function clear($loadDefaultLinks = true) {
- $this->entries = [];
- $this->closureEntries = [];
- $this->init = !$loadDefaultLinks;
- }
-
- public function setActiveEntry($appId) {
- $this->activeEntry = $appId;
- }
-
- public function getActiveEntry() {
- return $this->activeEntry;
- }
- private function init() {
- if ($this->init) {
- return;
- }
- $this->init = true;
- $l = $this->l10nFac->get('lib');
- if ($this->config->getSystemValueBool('knowledgebaseenabled', true)) {
- $this->add([
- 'type' => 'settings',
- 'id' => 'help',
- 'order' => 99998,
- 'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
- 'name' => $l->t('Help & privacy'),
- 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
- ]);
- }
- $this->defaultApp = $this->appManager->getDefaultAppForUser($this->userSession->getUser(), false);
- if ($this->userSession->isLoggedIn()) {
-
- $this->add([
- 'type' => 'settings',
- 'id' => 'profile',
- 'order' => 1,
- 'href' => $this->urlGenerator->linkToRoute(
- 'core.ProfilePage.index',
- ['targetUserId' => $this->userSession->getUser()->getUID()],
- ),
- 'name' => $l->t('View profile'),
- ]);
-
- if ($this->appManager->isEnabledForUser('theming', $this->userSession->getUser())) {
- $this->add([
- 'type' => 'settings',
- 'id' => 'accessibility_settings',
- 'order' => 2,
- 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'theming']),
- 'name' => $l->t('Appearance and accessibility'),
- 'icon' => $this->urlGenerator->imagePath('theming', 'accessibility-dark.svg'),
- ]);
- }
- if ($this->isAdmin()) {
-
- $this->add([
- 'type' => 'settings',
- 'id' => 'core_apps',
- 'order' => 5,
- 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
- 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
- 'name' => $l->t('Apps'),
- ]);
-
- $this->add([
- 'type' => 'settings',
- 'id' => 'settings',
- 'order' => 3,
- 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
- 'name' => $l->t('Personal settings'),
- 'icon' => $this->urlGenerator->imagePath('settings', 'personal.svg'),
- ]);
-
- $this->add([
- 'type' => 'settings',
- 'id' => 'admin_settings',
- 'order' => 4,
- 'href' => $this->urlGenerator->linkToRoute('settings.AdminSettings.index', ['section' => 'overview']),
- 'name' => $l->t('Administration settings'),
- 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
- ]);
- } else {
-
- $this->add([
- 'type' => 'settings',
- 'id' => 'settings',
- 'order' => 3,
- 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
- 'name' => $l->t('Settings'),
- 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
- ]);
- }
- $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
- if ($logoutUrl !== '') {
-
- $this->add([
- 'type' => 'settings',
- 'id' => 'logout',
- 'order' => 99999,
- 'href' => $logoutUrl,
- 'name' => $l->t('Log out'),
- 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
- ]);
- }
- if ($this->isSubadmin()) {
-
- $this->add([
- 'type' => 'settings',
- 'id' => 'core_users',
- 'order' => 6,
- 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
- 'name' => $l->t('Accounts'),
- 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
- ]);
- }
- }
- if ($this->userSession->isLoggedIn()) {
- $user = $this->userSession->getUser();
- $apps = $this->appManager->getEnabledAppsForUser($user);
- $this->customAppOrder = json_decode($this->config->getUserValue($user->getUID(), 'core', 'apporder', '[]'), true, flags:JSON_THROW_ON_ERROR);
- } else {
- $apps = $this->appManager->getInstalledApps();
- $this->customAppOrder = [];
- }
- foreach ($apps as $app) {
- if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
- continue;
- }
-
- $info = $this->appManager->getAppInfo($app);
- if (!isset($info['navigations']['navigation'])) {
- continue;
- }
- foreach ($info['navigations']['navigation'] as $key => $nav) {
- $nav['type'] = $nav['type'] ?? 'link';
- if (!isset($nav['name'])) {
- continue;
- }
-
- if (!isset($nav['route']) && $nav['type'] !== 'settings') {
- continue;
- }
- $role = $nav['@attributes']['role'] ?? 'all';
- if ($role === 'admin' && !$this->isAdmin()) {
- continue;
- }
- $l = $this->l10nFac->get($app);
- $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
- $order = $nav['order'] ?? 100;
- $type = $nav['type'];
- $route = !empty($nav['route']) ? $this->urlGenerator->linkToRoute($nav['route']) : '';
- $icon = $nav['icon'] ?? null;
- if ($icon !== null) {
- try {
- $icon = $this->urlGenerator->imagePath($app, $icon);
- } catch (\RuntimeException $ex) {
-
- }
- }
- if ($icon === null) {
- $icon = $this->appManager->getAppIcon($app);
- }
- if ($icon === null) {
- $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
- }
- $this->add(array_merge([
-
- 'id' => $id,
-
- 'order' => $order,
-
- 'href' => $route,
-
- 'icon' => $icon,
-
- 'type' => $type,
-
- 'name' => $l->t($nav['name']),
- ], $type === 'link' ? [
-
- 'app' => $app,
- ] : []
- ));
- }
- }
- }
- private function isAdmin() {
- $user = $this->userSession->getUser();
- if ($user !== null) {
- return $this->groupManager->isAdmin($user->getUID());
- }
- return false;
- }
- private function isSubadmin() {
- $user = $this->userSession->getUser();
- if ($user !== null) {
- return $this->groupManager->getSubAdmin()->isSubAdmin($user);
- }
- return false;
- }
- public function setUnreadCounter(string $id, int $unreadCounter): void {
- $this->unreadCounters[$id] = $unreadCounter;
- }
- }
|