2014-05-14 13:24:20 +00:00
< ? php namespace Backend\Classes ;
2014-07-12 05:06:03 +00:00
use Event ;
2014-05-14 13:24:20 +00:00
use BackendAuth ;
use System\Classes\PluginManager ;
2019-08-15 03:47:13 +00:00
use Validator ;
use SystemException ;
use Log ;
use Config ;
2014-05-14 13:24:20 +00:00
/**
* Manages the backend navigation .
*
* @ package october\backend
* @ author Alexey Bobkov , Samuel Georges
*/
class NavigationManager
{
use \October\Rain\Support\Traits\Singleton ;
/**
* @ var array Cache of registration callbacks .
*/
2014-08-01 08:20:55 +00:00
protected $callbacks = [];
2014-05-14 13:24:20 +00:00
/**
2020-03-01 07:10:37 +00:00
* @ var MainMenuItem [] List of registered items .
2014-05-14 13:24:20 +00:00
*/
2014-08-01 08:20:55 +00:00
protected $items ;
2014-05-14 13:24:20 +00:00
2014-08-01 08:20:55 +00:00
protected $contextSidenavPartials = [];
2014-07-24 04:19:00 +00:00
2014-08-01 08:20:55 +00:00
protected $contextOwner ;
protected $contextMainMenuItemCode ;
protected $contextSideMenuItemCode ;
2014-05-14 13:24:20 +00:00
/**
2020-03-01 07:10:37 +00:00
* @ var PluginManager
2014-05-14 13:24:20 +00:00
*/
protected $pluginManager ;
/**
* Initialize this singleton .
*/
protected function init ()
{
$this -> pluginManager = PluginManager :: instance ();
}
2014-06-06 11:38:34 +00:00
/**
* Loads the menu items from modules and plugins
* @ return void
2020-03-01 07:10:37 +00:00
* @ throws SystemException
2014-06-06 11:38:34 +00:00
*/
2014-05-14 13:24:20 +00:00
protected function loadItems ()
{
/*
* Load module items
*/
foreach ( $this -> callbacks as $callback ) {
$callback ( $this );
}
/*
* Load plugin items
*/
$plugins = $this -> pluginManager -> getPlugins ();
foreach ( $plugins as $id => $plugin ) {
$items = $plugin -> registerNavigation ();
2014-10-10 21:12:50 +00:00
if ( ! is_array ( $items )) {
2014-05-14 13:24:20 +00:00
continue ;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
$this -> registerMenuItems ( $id , $items );
}
2019-11-25 05:59:00 +00:00
/**
* @ event backend . menu . extendItems
* Provides an opportunity to manipulate the backend navigation
*
* Example usage :
*
* Event :: listen ( 'backend.menu.extendItems' , function (( \Backend\Classes\NavigationManager ) $navigationManager ) {
* $navigationManager -> addMainMenuItems ( ... )
* $navigationManager -> addSideMenuItems ( ... )
* $navigationManager -> removeMainMenuItem ( ... )
* });
*
2014-07-12 05:06:03 +00:00
*/
Event :: fire ( 'backend.menu.extendItems' , [ $this ]);
2014-05-14 13:24:20 +00:00
/*
* Sort menu items
*/
2020-03-01 07:10:37 +00:00
uasort ( $this -> items , static function ( $a , $b ) {
2015-05-02 00:47:06 +00:00
return $a -> order - $b -> order ;
});
2014-05-14 13:24:20 +00:00
/*
* Filter items user lacks permission for
*/
$user = BackendAuth :: getUser ();
$this -> items = $this -> filterItemPermissions ( $user , $this -> items );
foreach ( $this -> items as $item ) {
2014-10-10 21:12:50 +00:00
if ( ! $item -> sideMenu || ! count ( $item -> sideMenu )) {
2014-05-14 13:24:20 +00:00
continue ;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
2015-05-02 00:47:06 +00:00
/*
* Apply incremental default orders
*/
$orderCount = 0 ;
foreach ( $item -> sideMenu as $sideMenuItem ) {
2019-07-18 14:50:37 +00:00
if ( $sideMenuItem -> order !== - 1 ) {
continue ;
}
2015-05-02 00:47:06 +00:00
$sideMenuItem -> order = ( $orderCount += 100 );
}
2015-04-22 21:16:06 +00:00
2015-05-02 00:47:06 +00:00
/*
* Sort side menu items
*/
2020-03-01 07:10:37 +00:00
uasort ( $item -> sideMenu , static function ( $a , $b ) {
2015-05-02 00:47:06 +00:00
return $a -> order - $b -> order ;
});
2014-05-14 13:24:20 +00:00
2015-05-02 00:47:06 +00:00
/*
* Filter items user lacks permission for
*/
$item -> sideMenu = $this -> filterItemPermissions ( $user , $item -> sideMenu );
2015-04-22 21:16:06 +00:00
}
}
2014-05-14 13:24:20 +00:00
/**
* Registers a callback function that defines menu items .
* The callback function should register menu items by calling the manager ' s
2017-03-16 06:08:20 +00:00
* `registerMenuItems` method . The manager instance is passed to the callback
* function as an argument . Usage :
*
2019-08-15 15:14:54 +00:00
* BackendMenu :: registerCallback ( function ( $manager ) {
2017-03-16 06:08:20 +00:00
* $manager -> registerMenuItems ([ ... ]);
* });
*
2014-05-14 13:24:20 +00:00
* @ param callable $callback A callable function .
*/
public function registerCallback ( callable $callback )
{
$this -> callbacks [] = $callback ;
}
/**
* Registers the back - end menu items .
2015-04-22 21:16:06 +00:00
* The argument is an array of the main menu items . The array keys represent the
* menu item codes , specific for the plugin / module . Each element in the
2014-05-14 13:24:20 +00:00
* array should be an associative array with the following keys :
* - label - specifies the menu label localization string key , required .
* - icon - an icon name from the Font Awesome icon collection , required .
* - url - the back - end relative URL the menu item should point to , required .
* - permissions - an array of permissions the back - end user should have , optional .
* The item will be displayed if the user has any of the specified permissions .
* - order - a position of the item in the menu , optional .
* - counter - an optional numeric value to output near the menu icon . The value should be
* a number or a callable returning a number .
2014-11-10 09:33:43 +00:00
* - counterLabel - an optional string value to describe the numeric reference in counter .
2019-02-13 20:54:13 +00:00
* - sideMenu - an array of side menu items , optional . If provided , the array items
* should represent the side menu item code , and each value should be an associative
* array with the following keys :
* - label - specifies the menu label localization string key , required .
* - icon - an icon name from the Font Awesome icon collection , required .
* - url - the back - end relative URL the menu item should point to , required .
* - attributes - an array of attributes and values to apply to the menu item , optional .
* - permissions - an array of permissions the back - end user should have , optional .
* - counter - an optional numeric value to output near the menu icon . The value should be
* a number or a callable returning a number .
* - counterLabel - an optional string value to describe the numeric reference in counter .
2020-03-30 17:49:03 +00:00
* - badge - an optional string value to output near the menu icon . The value should be
* a string . This value will override the counter if set .
2014-06-15 05:49:06 +00:00
* @ param string $owner Specifies the menu items owner plugin or module in the format Author . Plugin .
2014-05-14 13:24:20 +00:00
* @ param array $definitions An array of the menu item definitions .
2020-03-01 07:10:37 +00:00
* @ throws SystemException
2014-05-14 13:24:20 +00:00
*/
public function registerMenuItems ( $owner , array $definitions )
{
2014-10-10 21:12:50 +00:00
if ( ! $this -> items ) {
2014-05-14 13:24:20 +00:00
$this -> items = [];
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
2019-08-15 03:47:13 +00:00
$validator = Validator :: make ( $definitions , [
'*.label' => 'required' ,
'*.icon' => 'required_without:*.iconSvg' ,
'*.url' => 'required' ,
'*.sideMenu.*.label' => 'nullable|required' ,
'*.sideMenu.*.icon' => 'nullable|required_without:*.sideMenu.*.iconSvg' ,
'*.sideMenu.*.url' => 'nullable|required' ,
]);
if ( $validator -> fails ()) {
$errorMessage = 'Invalid menu item detected in ' . $owner . '. Contact the plugin author to fix (' . $validator -> errors () -> first () . ')' ;
if ( Config :: get ( 'app.debug' , false )) {
throw new SystemException ( $errorMessage );
}
2020-03-01 07:10:37 +00:00
Log :: error ( $errorMessage );
2019-08-15 03:47:13 +00:00
}
2017-02-07 18:43:40 +00:00
$this -> addMainMenuItems ( $owner , $definitions );
2014-05-14 13:24:20 +00:00
}
2014-07-12 05:06:03 +00:00
/**
* Dynamically add an array of main menu items
* @ param string $owner
* @ param array $definitions
*/
public function addMainMenuItems ( $owner , array $definitions )
{
2014-10-10 21:12:50 +00:00
foreach ( $definitions as $code => $definition ) {
2014-07-12 05:06:03 +00:00
$this -> addMainMenuItem ( $owner , $code , $definition );
2014-10-10 21:12:50 +00:00
}
2014-07-12 05:06:03 +00:00
}
/**
* Dynamically add a single main menu item
* @ param string $owner
* @ param string $code
2020-03-01 07:10:37 +00:00
* @ param array $definition
2014-07-12 05:06:03 +00:00
*/
public function addMainMenuItem ( $owner , $code , array $definition )
{
$itemKey = $this -> makeItemKey ( $owner , $code );
2017-02-07 18:43:40 +00:00
2014-10-10 21:12:50 +00:00
if ( isset ( $this -> items [ $itemKey ])) {
2014-07-12 05:06:03 +00:00
$definition = array_merge (( array ) $this -> items [ $itemKey ], $definition );
2014-10-10 21:12:50 +00:00
}
2014-07-12 05:06:03 +00:00
2020-03-01 07:10:37 +00:00
$item = array_merge ( $definition , [
2014-07-12 05:06:03 +00:00
'code' => $code ,
'owner' => $owner
2020-03-01 07:10:37 +00:00
]);
$this -> items [ $itemKey ] = MainMenuItem :: createFromArray ( $item );
if ( array_key_exists ( 'sideMenu' , $item )) {
$this -> addSideMenuItems ( $owner , $code , $item [ 'sideMenu' ]);
}
}
2014-07-12 05:06:03 +00:00
2020-03-01 07:10:37 +00:00
/**
* @ param string $owner
* @ param string $code
* @ return MainMenuItem
* @ throws SystemException
*/
2020-03-01 07:15:44 +00:00
public function getMainMenuItem ( string $owner , string $code )
2020-03-01 07:10:37 +00:00
{
$itemKey = $this -> makeItemKey ( $owner , $code );
2014-07-12 05:06:03 +00:00
2020-03-01 07:10:37 +00:00
if ( ! array_key_exists ( $itemKey , $this -> items )) {
throw new SystemException ( 'No main menu item found with key ' . $itemKey );
2014-10-10 21:12:50 +00:00
}
2020-03-01 07:10:37 +00:00
return $this -> items [ $itemKey ];
2014-07-12 05:06:03 +00:00
}
2015-09-11 22:45:58 +00:00
/**
* Removes a single main menu item
2020-03-01 07:10:37 +00:00
* @ param $owner
* @ param $code
2015-09-11 22:45:58 +00:00
*/
public function removeMainMenuItem ( $owner , $code )
{
$itemKey = $this -> makeItemKey ( $owner , $code );
unset ( $this -> items [ $itemKey ]);
}
2014-07-12 05:06:03 +00:00
/**
* Dynamically add an array of side menu items
* @ param string $owner
* @ param string $code
* @ param array $definitions
*/
public function addSideMenuItems ( $owner , $code , array $definitions )
{
2014-10-10 21:12:50 +00:00
foreach ( $definitions as $sideCode => $definition ) {
2017-03-24 15:05:35 +00:00
$this -> addSideMenuItem ( $owner , $code , $sideCode , ( array ) $definition );
2014-10-10 21:12:50 +00:00
}
2014-07-12 05:06:03 +00:00
}
/**
* Dynamically add a single side menu item
* @ param string $owner
* @ param string $code
* @ param string $sideCode
2020-03-01 07:10:37 +00:00
* @ param array $definition
* @ return bool
2014-07-12 05:06:03 +00:00
*/
2020-03-01 07:15:44 +00:00
public function addSideMenuItem ( $owner , $code , $sideCode , array $definition )
2014-07-12 05:06:03 +00:00
{
$itemKey = $this -> makeItemKey ( $owner , $code );
2017-02-07 18:43:40 +00:00
2014-10-10 21:12:50 +00:00
if ( ! isset ( $this -> items [ $itemKey ])) {
2014-07-12 05:06:03 +00:00
return false ;
2014-10-10 21:12:50 +00:00
}
2014-07-12 05:06:03 +00:00
2017-02-07 18:43:40 +00:00
$mainItem = $this -> items [ $itemKey ];
2015-08-03 16:07:56 +00:00
$definition = array_merge ( $definition , [
'code' => $sideCode ,
'owner' => $owner
]);
2014-10-10 21:12:50 +00:00
if ( isset ( $mainItem -> sideMenu [ $sideCode ])) {
2014-07-12 05:06:03 +00:00
$definition = array_merge (( array ) $mainItem -> sideMenu [ $sideCode ], $definition );
2014-10-10 21:12:50 +00:00
}
2014-07-12 05:06:03 +00:00
2020-03-01 07:10:37 +00:00
$item = SideMenuItem :: createFromArray ( $definition );
2017-02-07 18:43:40 +00:00
2020-03-01 07:10:37 +00:00
$this -> items [ $itemKey ] -> addSideMenuItem ( $item );
return true ;
2014-07-12 05:06:03 +00:00
}
2020-10-13 07:51:59 +00:00
/**
* Remove multiple side menu items
*
* @ param string $owner
* @ param string $code
* @ param array $sideCodes
* @ return void
*/
public function removeSideMenuItems ( $owner , $code , $sideCodes )
{
foreach ( $sideCodes as $sideCode ) {
$this -> removeSideMenuItem ( $owner , $code , $sideCode );
}
}
2014-07-12 05:06:03 +00:00
2015-09-11 22:45:58 +00:00
/**
* Removes a single main menu item
2020-03-01 07:10:37 +00:00
* @ param string $owner
* @ param string $code
* @ param string $sideCode
* @ return bool
2015-09-11 22:45:58 +00:00
*/
2020-03-01 07:15:44 +00:00
public function removeSideMenuItem ( $owner , $code , $sideCode )
2015-09-11 22:45:58 +00:00
{
$itemKey = $this -> makeItemKey ( $owner , $code );
if ( ! isset ( $this -> items [ $itemKey ])) {
return false ;
}
$mainItem = $this -> items [ $itemKey ];
2020-03-01 07:10:37 +00:00
$mainItem -> removeSideMenuItem ( $sideCode );
return true ;
2015-09-11 22:45:58 +00:00
}
2014-05-14 13:24:20 +00:00
/**
* Returns a list of the main menu items .
* @ return array
2020-03-01 07:10:37 +00:00
* @ throws SystemException
2014-05-14 13:24:20 +00:00
*/
public function listMainMenuItems ()
{
2014-10-10 21:12:50 +00:00
if ( $this -> items === null ) {
2014-05-14 13:24:20 +00:00
$this -> loadItems ();
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
2019-02-13 20:54:13 +00:00
foreach ( $this -> items as $item ) {
2020-03-30 17:49:03 +00:00
if ( $item -> badge ) {
$item -> counter = ( string ) $item -> badge ;
continue ;
}
2019-02-21 17:55:43 +00:00
if ( $item -> counter === false ) {
continue ;
}
2019-02-13 20:54:13 +00:00
if ( $item -> counter !== null && is_callable ( $item -> counter )) {
$item -> counter = call_user_func ( $item -> counter , $item );
2019-03-16 19:53:25 +00:00
} elseif ( ! empty (( int ) $item -> counter )) {
$item -> counter = ( int ) $item -> counter ;
2019-02-21 17:55:43 +00:00
} elseif ( ! empty ( $sideItems = $this -> listSideMenuItems ( $item -> owner , $item -> code ))) {
$item -> counter = 0 ;
foreach ( $sideItems as $sideItem ) {
2020-03-30 17:49:03 +00:00
if ( $sideItem -> badge ) {
continue ;
}
2019-02-21 17:55:43 +00:00
$item -> counter += $sideItem -> counter ;
2019-02-13 20:54:13 +00:00
}
}
2019-02-21 17:55:43 +00:00
2020-06-20 05:54:44 +00:00
if ( empty ( $item -> counter ) || ! is_numeric ( $item -> counter )) {
2019-02-21 17:55:43 +00:00
$item -> counter = null ;
}
2019-02-13 20:54:13 +00:00
}
2014-05-14 13:24:20 +00:00
return $this -> items ;
}
/**
* Returns a list of side menu items for the currently active main menu item .
* The currently active main menu item is set with the setContext methods .
2020-03-01 07:10:37 +00:00
* @ param null $owner
* @ param null $code
* @ return SideMenuItem []
* @ throws SystemException
2014-05-14 13:24:20 +00:00
*/
2020-03-01 07:15:44 +00:00
public function listSideMenuItems ( $owner = null , $code = null )
2014-05-14 13:24:20 +00:00
{
$activeItem = null ;
2018-08-15 16:54:46 +00:00
if ( $owner !== null && $code !== null ) {
2017-10-18 17:22:33 +00:00
$activeItem = @ $this -> items [ $this -> makeItemKey ( $owner , $code )];
} else {
foreach ( $this -> listMainMenuItems () as $item ) {
if ( $this -> isMainMenuItemActive ( $item )) {
$activeItem = $item ;
break ;
}
2014-05-14 13:24:20 +00:00
}
}
2014-10-10 21:12:50 +00:00
if ( ! $activeItem ) {
2014-05-14 13:24:20 +00:00
return [];
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
$items = $activeItem -> sideMenu ;
foreach ( $items as $item ) {
2020-03-30 17:49:03 +00:00
if ( $item -> badge ) {
$item -> counter = ( string ) $item -> badge ;
continue ;
}
2014-10-10 21:12:50 +00:00
if ( $item -> counter !== null && is_callable ( $item -> counter )) {
2014-05-14 13:24:20 +00:00
$item -> counter = call_user_func ( $item -> counter , $item );
2019-02-13 20:54:13 +00:00
if ( empty ( $item -> counter )) {
$item -> counter = null ;
}
2014-10-10 21:12:50 +00:00
}
2020-06-20 05:54:44 +00:00
if ( ! is_null ( $item -> counter ) && ! is_numeric ( $item -> counter )) {
throw new SystemException ( " The menu item { $activeItem -> code } . { $item -> code } 's counter property is invalid. Check to make sure it's numeric or callable. Value: " . var_export ( $item -> counter , true ));
}
2014-05-14 13:24:20 +00:00
}
return $items ;
}
/**
* Sets the navigation context .
* The function sets the navigation owner , main menu item code and the side menu item code .
2014-07-24 04:19:00 +00:00
* @ param string $owner Specifies the navigation owner in the format Vendor / Module
* @ param string $mainMenuItemCode Specifies the main menu item code
* @ param string $sideMenuItemCode Specifies the side menu item code
2014-05-14 13:24:20 +00:00
*/
public function setContext ( $owner , $mainMenuItemCode , $sideMenuItemCode = null )
{
$this -> setContextOwner ( $owner );
$this -> setContextMainMenu ( $mainMenuItemCode );
$this -> setContextSideMenu ( $sideMenuItemCode );
}
/**
* Sets the navigation context .
* The function sets the navigation owner .
2014-07-24 04:19:00 +00:00
* @ param string $owner Specifies the navigation owner in the format Vendor / Module
2014-05-14 13:24:20 +00:00
*/
public function setContextOwner ( $owner )
{
$this -> contextOwner = $owner ;
}
/**
* Specifies a code of the main menu item in the current navigation context .
2014-07-24 04:19:00 +00:00
* @ param string $mainMenuItemCode Specifies the main menu item code
2014-05-14 13:24:20 +00:00
*/
public function setContextMainMenu ( $mainMenuItemCode )
{
$this -> contextMainMenuItemCode = $mainMenuItemCode ;
}
/**
* Returns information about the current navigation context .
* @ return mixed Returns an object with the following fields :
* - mainMenuCode
* - sideMenuCode
2014-07-24 04:19:00 +00:00
* - owner
2014-05-14 13:24:20 +00:00
*/
public function getContext ()
{
return ( object )[
'mainMenuCode' => $this -> contextMainMenuItemCode ,
2014-07-24 04:19:00 +00:00
'sideMenuCode' => $this -> contextSideMenuItemCode ,
2014-10-10 21:12:50 +00:00
'owner' => $this -> contextOwner
2014-05-14 13:24:20 +00:00
];
}
/**
* Specifies a code of the side menu item in the current navigation context .
2014-11-09 01:35:10 +00:00
* If the code is set to TRUE , the first item will be flagged as active .
2014-07-24 04:19:00 +00:00
* @ param string $sideMenuItemCode Specifies the side menu item code
2014-05-14 13:24:20 +00:00
*/
2014-07-12 05:06:03 +00:00
public function setContextSideMenu ( $sideMenuItemCode )
2014-05-14 13:24:20 +00:00
{
$this -> contextSideMenuItemCode = $sideMenuItemCode ;
}
/**
* Determines if a main menu item is active .
2020-03-01 07:10:37 +00:00
* @ param MainMenuItem $item Specifies the item object .
2014-05-14 13:24:20 +00:00
* @ return boolean Returns true if the menu item is active .
*/
2020-03-01 07:15:44 +00:00
public function isMainMenuItemActive ( $item )
2014-05-14 13:24:20 +00:00
{
2020-03-01 07:10:37 +00:00
return $this -> contextOwner === $item -> owner && $this -> contextMainMenuItemCode === $item -> code ;
2014-05-14 13:24:20 +00:00
}
2014-07-28 06:40:18 +00:00
/**
* Returns the currently active main menu item
2020-03-01 07:10:37 +00:00
* @ return null | MainMenuItem $item Returns the item object or null .
* @ throws SystemException
2014-07-28 06:40:18 +00:00
*/
public function getActiveMainMenuItem ()
{
foreach ( $this -> listMainMenuItems () as $item ) {
2014-10-10 21:12:50 +00:00
if ( $this -> isMainMenuItemActive ( $item )) {
2014-07-28 06:40:18 +00:00
return $item ;
2014-10-10 21:12:50 +00:00
}
2014-07-28 06:40:18 +00:00
}
return null ;
}
2014-05-14 13:24:20 +00:00
/**
* Determines if a side menu item is active .
2020-03-01 07:10:37 +00:00
* @ param SideMenuItem $item Specifies the item object .
2014-05-14 13:24:20 +00:00
* @ return boolean Returns true if the side item is active .
*/
2020-03-01 07:15:44 +00:00
public function isSideMenuItemActive ( $item )
2014-05-14 13:24:20 +00:00
{
2014-11-09 01:35:10 +00:00
if ( $this -> contextSideMenuItemCode === true ) {
$this -> contextSideMenuItemCode = null ;
return true ;
}
2020-03-01 07:10:37 +00:00
return $this -> contextOwner === $item -> owner && $this -> contextSideMenuItemCode === $item -> code ;
2014-05-14 13:24:20 +00:00
}
2014-07-24 04:19:00 +00:00
/**
* Registers a special side navigation partial for a specific main menu .
* The sidenav partial replaces the standard side navigation .
* @ param string $owner Specifies the navigation owner in the format Vendor / Module .
* @ param string $mainMenuItemCode Specifies the main menu item code .
* @ param string $partial Specifies the partial name .
*/
public function registerContextSidenavPartial ( $owner , $mainMenuItemCode , $partial )
{
$this -> contextSidenavPartials [ $owner . $mainMenuItemCode ] = $partial ;
}
/**
2014-10-10 21:12:50 +00:00
* Returns the side navigation partial for a specific main menu previously registered
* with the registerContextSidenavPartial () method .
*
2014-07-24 04:19:00 +00:00
* @ param string $owner Specifies the navigation owner in the format Vendor / Module .
* @ param string $mainMenuItemCode Specifies the main menu item code .
* @ return mixed Returns the partial name or null .
*/
public function getContextSidenavPartial ( $owner , $mainMenuItemCode )
{
$key = $owner . $mainMenuItemCode ;
2018-08-15 17:15:13 +00:00
return $this -> contextSidenavPartials [ $key ] ? ? null ;
2014-07-24 04:19:00 +00:00
}
2014-05-14 13:24:20 +00:00
/**
* Removes menu items from an array if the supplied user lacks permission .
2020-03-01 07:10:37 +00:00
* @ param \Backend\Models\User $user A user object
* @ param MainMenuItem [] | SideMenuItem [] $items A collection of menu items
2014-05-14 13:24:20 +00:00
* @ return array The filtered menu items
*/
2020-03-01 07:15:44 +00:00
protected function filterItemPermissions ( $user , array $items )
2014-05-14 13:24:20 +00:00
{
2015-07-28 08:52:12 +00:00
if ( ! $user ) {
return $items ;
}
2020-03-01 07:10:37 +00:00
$items = array_filter ( $items , static function ( $item ) use ( $user ) {
2014-10-10 21:12:50 +00:00
if ( ! $item -> permissions || ! count ( $item -> permissions )) {
2014-05-14 13:24:20 +00:00
return true ;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
return $user -> hasAnyAccess ( $item -> permissions );
});
return $items ;
}
2014-07-12 05:06:03 +00:00
/**
* Internal method to make a unique key for an item .
2020-03-01 07:10:37 +00:00
* @ param string $owner
* @ param string $code
2014-07-12 05:06:03 +00:00
* @ return string
*/
2020-03-01 07:15:44 +00:00
protected function makeItemKey ( $owner , $code )
2014-07-12 05:06:03 +00:00
{
return strtoupper ( $owner ) . '.' . strtoupper ( $code );
}
2019-07-18 14:50:37 +00:00
}