11<?php
22/**
3- * @copyright Copyright (c) 2016, ownCloud, Inc.
3+ * @copyright Copyright (c) 2016, ownCloud GmbH
44 *
55 * @author Bart Visscher <bartv@thisnet.nl>
66 * @author Joas Schilling <coding@schilljs.com>
2626
2727namespace OC ;
2828
29+ use OC \App \AppManager ;
30+ use OCP \App \IAppManager ;
31+ use OCP \IGroupManager ;
32+ use OCP \INavigationManager ;
33+ use OCP \IURLGenerator ;
34+ use OCP \IUserSession ;
35+ use OCP \L10N \IFactory ;
36+
2937/**
3038 * Manages the ownCloud navigation
3139 */
32- class NavigationManager implements \OCP \INavigationManager {
33- protected $ entries = array ();
34- protected $ closureEntries = array ();
40+
41+ class NavigationManager implements INavigationManager {
42+ protected $ entries = [];
43+ protected $ closureEntries = [];
3544 protected $ activeEntry ;
45+ /** @var bool */
46+ protected $ init = false ;
47+ /** @var IAppManager|AppManager */
48+ protected $ appManager ;
49+ /** @var IURLGenerator */
50+ private $ urlGenerator ;
51+ /** @var IFactory */
52+ private $ l10nFac ;
53+ /** @var IUserSession */
54+ private $ userSession ;
55+ /** @var IGroupManager */
56+ private $ groupManager ;
57+
58+ public function __construct (IAppManager $ appManager = null ,
59+ IURLGenerator $ urlGenerator = null ,
60+ IFactory $ l10nFac = null ,
61+ IUserSession $ userSession = null ,
62+ IGroupManager $ groupManager = null ) {
63+ $ this ->appManager = $ appManager ;
64+ $ this ->urlGenerator = $ urlGenerator ;
65+ $ this ->l10nFac = $ l10nFac ;
66+ $ this ->userSession = $ userSession ;
67+ $ this ->groupManager = $ groupManager ;
68+ }
3669
3770 /**
3871 * Creates a new navigation entry
@@ -60,6 +93,7 @@ public function add($entry) {
6093 * @return array an array of the added entries
6194 */
6295 public function getAll () {
96+ $ this ->init ();
6397 foreach ($ this ->closureEntries as $ c ) {
6498 $ this ->add ($ c ());
6599 }
@@ -71,8 +105,9 @@ public function getAll() {
71105 * removes all the entries
72106 */
73107 public function clear () {
74- $ this ->entries = array ();
75- $ this ->closureEntries = array ();
108+ $ this ->entries = [];
109+ $ this ->closureEntries = [];
110+ $ this ->init = false ;
76111 }
77112
78113 /**
@@ -93,4 +128,64 @@ public function setActiveEntry($id) {
93128 public function getActiveEntry () {
94129 return $ this ->activeEntry ;
95130 }
131+
132+ private function init () {
133+ if ($ this ->init ) {
134+ return ;
135+ }
136+ $ this ->init = true ;
137+ if (is_null ($ this ->appManager )) {
138+ return ;
139+ }
140+ foreach ($ this ->appManager ->getInstalledApps () as $ app ) {
141+ // load plugins and collections from info.xml
142+ $ info = $ this ->appManager ->getAppInfo ($ app );
143+ if (!isset ($ info ['navigation ' ])) {
144+ continue ;
145+ }
146+ $ nav = $ info ['navigation ' ];
147+ if (!isset ($ nav ['name ' ])) {
148+ continue ;
149+ }
150+ if (!isset ($ nav ['route ' ])) {
151+ continue ;
152+ }
153+ $ role = isset ($ nav ['@attributes ' ]['role ' ]) ? $ nav ['@attributes ' ]['role ' ] : 'all ' ;
154+ if ($ role === 'admin ' && !$ this ->isAdmin ()) {
155+ continue ;
156+ }
157+ $ l = $ this ->l10nFac ->get ($ app );
158+ $ order = isset ($ nav ['order ' ]) ? $ nav ['order ' ] : 100 ;
159+ $ route = $ this ->urlGenerator ->linkToRoute ($ nav ['route ' ]);
160+ $ icon = isset ($ nav ['icon ' ]) ? $ nav ['icon ' ] : 'app.svg ' ;
161+ foreach ([$ icon , "$ app.svg " ] as $ i ) {
162+ try {
163+ $ icon = $ this ->urlGenerator ->imagePath ($ app , $ i );
164+ break ;
165+ } catch (\RuntimeException $ ex ) {
166+ // no icon? - ignore it then
167+ }
168+ }
169+ if (is_null ($ icon )) {
170+ $ icon = $ this ->urlGenerator ->imagePath ('core ' , 'default-app-icon ' );
171+ }
172+
173+ $ this ->add ([
174+ 'id ' => $ app ,
175+ 'order ' => $ order ,
176+ 'href ' => $ route ,
177+ 'icon ' => $ icon ,
178+ 'name ' => $ l ->t ($ nav ['name ' ]),
179+ ]);
180+ }
181+ }
182+
183+ private function isAdmin () {
184+ $ user = $ this ->userSession ->getUser ();
185+ if ($ user !== null ) {
186+ return $ this ->groupManager ->isAdmin ($ user ->getUID ());
187+ }
188+ return false ;
189+ }
190+
96191}
0 commit comments