Add Navigation class to server container

This commit is contained in:
Bart Visscher 2013-09-20 17:34:33 +02:00
parent aa8a85f77d
commit e3013c5801
5 changed files with 117 additions and 19 deletions

View File

@ -27,8 +27,6 @@
* upgrading and removing apps. * upgrading and removing apps.
*/ */
class OC_App{ class OC_App{
static private $activeapp = '';
static private $navigation = array();
static private $settingsForms = array(); static private $settingsForms = array();
static private $adminForms = array(); static private $adminForms = array();
static private $personalForms = array(); static private $personalForms = array();
@ -271,7 +269,7 @@ class OC_App{
/** /**
* @brief adds an entry to the navigation * @brief adds an entry to the navigation
* @param string $data array containing the data * @param array $data array containing the data
* @return bool * @return bool
* *
* This function adds a new entry to the navigation visible to users. $data * This function adds a new entry to the navigation visible to users. $data
@ -287,11 +285,7 @@ class OC_App{
* the navigation. Lower values come first. * the navigation. Lower values come first.
*/ */
public static function addNavigationEntry( $data ) { public static function addNavigationEntry( $data ) {
$data['active']=false; OC::$server->getNavigationManager()->add($data);
if(!isset($data['icon'])) {
$data['icon']='';
}
OC_App::$navigation[] = $data;
return true; return true;
} }
@ -305,9 +299,7 @@ class OC_App{
* highlighting the current position of the user. * highlighting the current position of the user.
*/ */
public static function setActiveNavigationEntry( $id ) { public static function setActiveNavigationEntry( $id ) {
// load all the apps, to make sure we have all the navigation entries OC::$server->getNavigationManager()->setActiveEntry($id);
self::loadApps();
self::$activeapp = $id;
return true; return true;
} }
@ -315,15 +307,14 @@ class OC_App{
* @brief Get the navigation entries for the $app * @brief Get the navigation entries for the $app
* @param string $app app * @param string $app app
* @return array of the $data added with addNavigationEntry * @return array of the $data added with addNavigationEntry
*
* Warning: destroys the existing entries
*/ */
public static function getAppNavigationEntries($app) { public static function getAppNavigationEntries($app) {
if(is_file(self::getAppPath($app).'/appinfo/app.php')) { if(is_file(self::getAppPath($app).'/appinfo/app.php')) {
$save = self::$navigation; OC::$server->getNavigationManager()->clear();
self::$navigation = array();
require $app.'/appinfo/app.php'; require $app.'/appinfo/app.php';
$app_entries = self::$navigation; return OC::$server->getNavigationManager()->getAll();
self::$navigation = $save;
return $app_entries;
} }
return array(); return array();
} }
@ -336,7 +327,7 @@ class OC_App{
* setActiveNavigationEntry * setActiveNavigationEntry
*/ */
public static function getActiveNavigationEntry() { public static function getActiveNavigationEntry() {
return self::$activeapp; return OC::$server->getNavigationManager()->getActiveEntry();
} }
/** /**
@ -419,8 +410,9 @@ class OC_App{
// This is private as well. It simply works, so don't ask for more details // This is private as well. It simply works, so don't ask for more details
private static function proceedNavigation( $list ) { private static function proceedNavigation( $list ) {
$activeapp = OC::$server->getNavigationManager()->getActiveEntry();
foreach( $list as &$naventry ) { foreach( $list as &$naventry ) {
if( $naventry['id'] == self::$activeapp ) { if( $naventry['id'] == $activeapp ) {
$naventry['active'] = true; $naventry['active'] = true;
} }
else{ else{
@ -572,7 +564,8 @@ class OC_App{
* - active: boolean, signals if the user is on this navigation entry * - active: boolean, signals if the user is on this navigation entry
*/ */
public static function getNavigation() { public static function getNavigation() {
$navigation = self::proceedNavigation( self::$navigation ); $entries = OC::$server->getNavigationManager()->getAll();
$navigation = self::proceedNavigation( $entries );
return $navigation; return $navigation;
} }

63
lib/navigationmanager.php Normal file
View File

@ -0,0 +1,63 @@
<?php
/**
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*
*/
namespace OC;
/**
* Manages the owncloud navigation
*/
class NavigationManager {
protected $entries = array();
/**
* Creates a new navigation entry
* @param array $entry containing: id, name, order, icon and href key
*/
public function add(array $entry) {
$entry['active'] = false;
if(!isset($entry['icon'])) {
$entry['icon'] = '';
}
$this->entries[] = $entry;
}
/**
* @brief returns all the added Menu entries
* @return array of the added entries
*/
public function getAll() {
return $this->entries;
}
/**
* @brief removes all the entries
*/
public function clear() {
$this->entries = array();
}
/**
* Sets the current navigation entry of the currently running app
* @param string $id of the app entry to activate (from added $entry)
*/
public function setActiveEntry($id) {
$this->activeEntry = $id;
}
/**
* @brief gets the active Menu entry
* @return string id or empty string
*
* This function returns the id of the active navigation entry (set by
* setActiveEntry
*/
public function getActiveEntry() {
return $this->activeEntry;
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*
*/
namespace OCP;
/**
* Manages the owncloud navigation
*/
interface INavigationManager {
/**
* Creates a new navigation entry
* @param array $entry containing: id, name, order, icon and href key
*/
public function add(array $entry);
/**
* Sets the current navigation entry of the currently running app
* @param string $appId id of the app entry to activate (from added $entry)
*/
public function setActiveEntry($appId);
}

View File

@ -69,6 +69,11 @@ interface IServerContainer {
*/ */
function getUserSession(); function getUserSession();
/**
* @return \OCP\INavigationManager
*/
function getNavigationManager();
/** /**
* Returns an ICache instance * Returns an ICache instance
* *

View File

@ -97,6 +97,9 @@ class Server extends SimpleContainer implements IServerContainer {
}); });
return $userSession; return $userSession;
}); });
$this->registerService('NavigationManager', function($c) {
return new \OC\NavigationManager();
});
$this->registerService('UserCache', function($c) { $this->registerService('UserCache', function($c) {
return new UserCache(); return new UserCache();
}); });
@ -152,6 +155,13 @@ class Server extends SimpleContainer implements IServerContainer {
return $this->query('UserSession'); return $this->query('UserSession');
} }
/**
* @return \OC\NavigationManager
*/
function getNavigationManager() {
return $this->query('NavigationManager');
}
/** /**
* Returns an ICache instance * Returns an ICache instance
* *