You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NavigationController.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Controller;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\AppFramework\OCSController;
  26. use OCP\INavigationManager;
  27. use OCP\IRequest;
  28. use OCP\IURLGenerator;
  29. class NavigationController extends OCSController {
  30. /** @var INavigationManager */
  31. private $navigationManager;
  32. /** @var IURLGenerator */
  33. private $urlGenerator;
  34. public function __construct(string $appName, IRequest $request, INavigationManager $navigationManager, IURLGenerator $urlGenerator) {
  35. parent::__construct($appName, $request);
  36. $this->navigationManager = $navigationManager;
  37. $this->urlGenerator = $urlGenerator;
  38. }
  39. /**
  40. * @NoAdminRequired
  41. * @NoCSRFRequired
  42. *
  43. * @param bool $absolute
  44. * @return DataResponse
  45. */
  46. public function getAppsNavigation(bool $absolute = false): DataResponse {
  47. $navigation = $this->navigationManager->getAll();
  48. if ($absolute) {
  49. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  50. }
  51. return new DataResponse($navigation);
  52. }
  53. /**
  54. * @NoAdminRequired
  55. * @NoCSRFRequired
  56. *
  57. * @param bool $absolute
  58. * @return DataResponse
  59. */
  60. public function getSettingsNavigation(bool $absolute = false): DataResponse {
  61. $navigation = $this->navigationManager->getAll('settings');
  62. if ($absolute) {
  63. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  64. }
  65. return new DataResponse($navigation);
  66. }
  67. /**
  68. * Rewrite href attribute of navigation entries to an absolute URL
  69. *
  70. * @param array $navigation
  71. * @return array
  72. */
  73. private function rewriteToAbsoluteUrls(array $navigation): array {
  74. foreach ($navigation as &$entry) {
  75. if (0 !== strpos($entry['href'], $this->urlGenerator->getBaseUrl())) {
  76. $entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
  77. }
  78. if (0 !== strpos($entry['icon'], $this->urlGenerator->getBaseUrl())) {
  79. $entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
  80. }
  81. }
  82. return $navigation;
  83. }
  84. }