NavigationManager.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC;
  27. use OC\App\AppManager;
  28. use OCP\App\IAppManager;
  29. use OCP\IGroupManager;
  30. use OCP\INavigationManager;
  31. use OCP\IURLGenerator;
  32. use OCP\IUserSession;
  33. use OCP\L10N\IFactory;
  34. /**
  35. * Manages the ownCloud navigation
  36. */
  37. class NavigationManager implements INavigationManager {
  38. protected $entries = [];
  39. protected $closureEntries = [];
  40. protected $activeEntry;
  41. /** @var bool */
  42. protected $init = false;
  43. /** @var IAppManager|AppManager */
  44. protected $appManager;
  45. /** @var IURLGenerator */
  46. private $urlGenerator;
  47. /** @var IFactory */
  48. private $l10nFac;
  49. /** @var IUserSession */
  50. private $userSession;
  51. /** @var IGroupManager */
  52. private $groupManager;
  53. public function __construct(IAppManager $appManager = null,
  54. IURLGenerator $urlGenerator = null,
  55. IFactory $l10nFac = null,
  56. IUserSession $userSession = null,
  57. IGroupManager$groupManager = null) {
  58. $this->appManager = $appManager;
  59. $this->urlGenerator = $urlGenerator;
  60. $this->l10nFac = $l10nFac;
  61. $this->userSession = $userSession;
  62. $this->groupManager = $groupManager;
  63. }
  64. /**
  65. * Creates a new navigation entry
  66. *
  67. * @param array|\Closure $entry Array containing: id, name, order, icon and href key
  68. * The use of a closure is preferred, because it will avoid
  69. * loading the routing of your app, unless required.
  70. * @return void
  71. */
  72. public function add($entry) {
  73. if ($entry instanceof \Closure) {
  74. $this->closureEntries[] = $entry;
  75. return;
  76. }
  77. $entry['active'] = false;
  78. if(!isset($entry['icon'])) {
  79. $entry['icon'] = '';
  80. }
  81. $this->entries[] = $entry;
  82. }
  83. /**
  84. * returns all the added Menu entries
  85. * @return array an array of the added entries
  86. */
  87. public function getAll() {
  88. $this->init();
  89. foreach ($this->closureEntries as $c) {
  90. $this->add($c());
  91. }
  92. $this->closureEntries = array();
  93. return $this->entries;
  94. }
  95. /**
  96. * removes all the entries
  97. */
  98. public function clear() {
  99. $this->entries = [];
  100. $this->closureEntries = [];
  101. $this->init = false;
  102. }
  103. /**
  104. * Sets the current navigation entry of the currently running app
  105. * @param string $id of the app entry to activate (from added $entry)
  106. */
  107. public function setActiveEntry($id) {
  108. $this->activeEntry = $id;
  109. }
  110. /**
  111. * gets the active Menu entry
  112. * @return string id or empty string
  113. *
  114. * This function returns the id of the active navigation entry (set by
  115. * setActiveEntry
  116. */
  117. public function getActiveEntry() {
  118. return $this->activeEntry;
  119. }
  120. private function init() {
  121. if ($this->init) {
  122. return;
  123. }
  124. $this->init = true;
  125. if (is_null($this->appManager)) {
  126. return;
  127. }
  128. foreach ($this->appManager->getInstalledApps() as $app) {
  129. // load plugins and collections from info.xml
  130. $info = $this->appManager->getAppInfo($app);
  131. if (!isset($info['navigation'])) {
  132. continue;
  133. }
  134. $nav = $info['navigation'];
  135. if (!isset($nav['name'])) {
  136. continue;
  137. }
  138. if (!isset($nav['route'])) {
  139. continue;
  140. }
  141. $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
  142. if ($role === 'admin' && !$this->isAdmin()) {
  143. continue;
  144. }
  145. $l = $this->l10nFac->get($app);
  146. $order = isset($nav['order']) ? $nav['order'] : 100;
  147. $route = $this->urlGenerator->linkToRoute($nav['route']);
  148. $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
  149. foreach ([$icon, "$app.svg"] as $i) {
  150. try {
  151. $icon = $this->urlGenerator->imagePath($app, $i);
  152. break;
  153. } catch (\RuntimeException $ex) {
  154. // no icon? - ignore it then
  155. }
  156. }
  157. if (is_null($icon)) {
  158. $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
  159. }
  160. $this->add([
  161. 'id' => $app,
  162. 'order' => $order,
  163. 'href' => $route,
  164. 'icon' => $icon,
  165. 'name' => $l->t($nav['name']),
  166. ]);
  167. }
  168. }
  169. private function isAdmin() {
  170. $user = $this->userSession->getUser();
  171. if ($user !== null) {
  172. return $this->groupManager->isAdmin($user->getUID());
  173. }
  174. return false;
  175. }
  176. }