Class luya\admin\components\AdminMenuBuilder
Inheritance | luya\admin\components\AdminMenuBuilder » yii\base\BaseObject |
---|---|
Implements | luya\admin\components\AdminMenuBuilderInterface, yii\base\Configurable |
Available since version | 1.0.0 |
Source Code | https://github.com/luyadev/luya-module-admin/blob/master/src/components/AdminMenuBuilder.php |
Builder class for the Administration Menu/Navigation.
This class is used to build the admin menu/navigation for all admin modules and is called in the getMenu()
method of each
admin module class.
Example of how to use the AdminMenuBuilder class inside the getMenu()
method of an Admin Module:
public function getMenu()
{
return (new AdminMenuBuilder($this))
->nodeRoute('menu_node_filemanager', 'folder_open', 'admin/storage/index')
->node('menu_node_system', 'layers')
->group('menu_group_access')
->itemApi('menu_access_item_user', 'admin/user/index', 'person', 'api-admin-user')
->itemApi('menu_access_item_group', 'admin/group/index', 'group', 'api-admin-group')
->group('menu_group_system')
->itemApi('menu_system_item_language', 'admin/lang/index', 'language', 'api-admin-lang')
->itemApi('menu_system_item_tags', 'admin/tag/index', 'label', 'api-admin-tag')
->itemApi('menu_system_logger', 'admin/logger/index', 'label', 'api-admin-logger')
->group('menu_group_images')
->itemApi('menu_images_item_effects', 'admin/effect/index', 'blur_circular', 'api-admin-effect')
->itemApi('menu_images_item_filters', 'admin/filter/index', 'adjust', 'api-admin-filter');
}
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$permissionApis | luya\admin\components\AdminMenuBuilder | ||
$permissionRoutes | luya\admin\components\AdminMenuBuilder |
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$moduleContext | luya\base\AdminModuleInterface | The context on what the menu is running. | luya\admin\components\AdminMenuBuilder |
$options | array | The available options for itemApi and itemRoute. | luya\admin\components\AdminMenuBuilder |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | luya\admin\components\AdminMenuBuilder | |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
getOptionValue() | Helper method to get then value of an options inside an item. | luya\admin\components\AdminMenuBuilder |
getPermissionApis() | luya\admin\components\AdminMenuBuilder | |
getPermissionRoutes() | luya\admin\components\AdminMenuBuilder | |
group() | Add a group, all items (api or route) must be child items of a group. The group is the title in the left menu of the admin interface. | luya\admin\components\AdminMenuBuilder |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the object. | yii\base\BaseObject |
itemApi() | Add an item to a group. API items are based on the ngrest crud concept. | luya\admin\components\AdminMenuBuilder |
itemPoolApi() | Generate a permission for an API with a Pool | luya\admin\components\AdminMenuBuilder |
itemRoute() | Add an item to a group. Route items opens a angular view. | luya\admin\components\AdminMenuBuilder |
menu() | Get the Menu Array. | luya\admin\components\AdminMenuBuilder |
node() | The node is the menu entry in the TOP navigation of the luya administration interface. | luya\admin\components\AdminMenuBuilder |
nodeRoute() | A node which is a custom route to open, nodes are the top menu of the luya administration interfaces. | luya\admin\components\AdminMenuBuilder |
Protected Methods
Method | Description | Defined By |
---|---|---|
verifyOptions() | Verify the additional options of an itemRoute or itemApi item. | luya\admin\components\AdminMenuBuilder |
Property Details
The context on what the menu is running.
The available options for itemApi and itemRoute.
Method Details
Defined in: yii\base\BaseObject::__call()
Calls the named method which is not a class method.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
public mixed __call ( $name, $params ) | ||
$name | string |
The method name |
$params | array |
Method parameters |
return | mixed |
The method return value |
---|---|---|
throws | yii\base\UnknownMethodException |
when calling unknown method |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
public void __construct ( luya\base\AdminModuleInterface $module, array $config = [] ) | ||
$module | luya\base\AdminModuleInterface | |
$config | array |
public function __construct(AdminModuleInterface $module, array $config = [])
{
$this->moduleContext = $module;
parent::__construct($config);
}
Defined in: yii\base\BaseObject::__get()
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
See also __set().
public mixed __get ( $name ) | ||
$name | string |
The property name |
return | mixed |
The property value |
---|---|---|
throws | yii\base\UnknownPropertyException |
if the property is not defined |
throws | yii\base\InvalidCallException |
if the property is write-only |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\BaseObject::__isset()
Checks if a property is set, i.e. defined and not null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property)
.
Note that if the property is not defined, false will be returned.
public boolean __isset ( $name ) | ||
$name | string |
The property name or the event name |
return | boolean |
Whether the named property is set (not null). |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in: yii\base\BaseObject::__set()
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
See also __get().
public void __set ( $name, $value ) | ||
$name | string |
The property name or the event name |
$value | mixed |
The property value |
throws | yii\base\UnknownPropertyException |
if the property is not defined |
---|---|---|
throws | yii\base\InvalidCallException |
if the property is read-only |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::__unset()
Sets an object property to null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property)
.
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
public void __unset ( $name ) | ||
$name | string |
The property name |
throws | yii\base\InvalidCallException |
if the property is read only. |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::canGetProperty()
Returns a value indicating whether a property can be read.
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be read |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in: yii\base\BaseObject::canSetProperty()
Returns a value indicating whether a property can be set.
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be written |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
instead.
Defined in: yii\base\BaseObject::className()
Returns the fully qualified name of this class.
public static string className ( ) | ||
return | string |
The fully qualified name of this class. |
---|
public static function className()
{
return get_called_class();
}
Helper method to get then value of an options inside an item.
public static mixed getOptionValue ( array $item, $optionName, $defaultValue = false ) | ||
$item | array |
The item where the option key persists. |
$optionName | string |
The name of the option to get. |
$defaultValue | mixed |
The default value if the option is not available for this item. |
public static function getOptionValue(array $item, $optionName, $defaultValue = false)
{
if (!isset($item['options'])) {
return $defaultValue;
}
return $item['options'][$optionName] ?? $defaultValue;
}
public void getPermissionApis ( ) |
public function getPermissionApis()
{
return $this->_permissionApis;
}
public void getPermissionRoutes ( ) |
public function getPermissionRoutes()
{
return $this->_permissionRoutes;
}
Add a group, all items (api or route) must be child items of a group. The group is the title in the left menu of the admin interface.
public luya\admin\components\AdminMenuBuilder group ( $name ) | ||
$name | string |
The name of the group. |
public function group($name)
{
$this->_pointers['group'] = $name;
$this->_menu[$this->_pointers['node']]['groups'][$name] = ['name' => $name, 'items' => []];
return $this;
}
Defined in: yii\base\BaseObject::hasMethod()
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
public boolean hasMethod ( $name ) | ||
$name | string |
The method name |
return | boolean |
Whether the method is defined |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
Defined in: yii\base\BaseObject::hasProperty()
Returns a value indicating whether a property is defined.
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also:
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property is defined |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
Defined in: yii\base\BaseObject::init()
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) |
public function init()
{
}
Add an item to a group. API items are based on the ngrest crud concept.
public luya\admin\components\AdminMenuBuilder itemApi ( $name, $route, $icon, $apiEndpoint, array $options = [] ) | ||
$name | string |
The name of the Api (displayed as menu point in the left navigation), all names run through the |
$route | string |
The api route to the ngrest controller |
$icon | string |
The icon name based on the google icons font see https://design.google.com/icons/. |
$apiEndpoint | string |
The api endpoint defined in the NgRestModel::ngRestApiEndpoint |
$options | array |
An array with options you can provided and read inside the admin menu component. See {{\luya\admin\components\AdminMenuBuilder::verifyOptions}} for detail list and informations. |
public function itemApi($name, $route, $icon, $apiEndpoint, array $options = [])
{
$item = [
'alias' => $name,
'route' => $route,
'icon' => $icon,
'permissionApiEndpoint' => $apiEndpoint,
'permissionIsRoute' => false,
'permissionIsApi' => true,
'searchModelClass' => false,
'options' => $this->verifyOptions($options),
];
$this->_menu[$this->_pointers['node']]['groups'][$this->_pointers['group']]['items'][] = $item;
$this->_permissionApis[] = ['api' => $apiEndpoint, 'alias' => $name, 'pool' => static::getOptionValue($item, 'pool', null)];
return $this;
}
Generate a permission for an API with a Pool
public luya\admin\components\AdminMenuBuilder itemPoolApi ( $name, $route, $icon, $apiEndpoint, $pool, array $options = [] ) | ||
$name | string | |
$route | string | |
$icon | string | |
$apiEndpoint | string | |
$pool | string | |
$options | array |
public function itemPoolApi($name, $route, $icon, $apiEndpoint, $pool, array $options = [])
{
return $this->itemApi($name, $route, $icon, $apiEndpoint, array_merge($options, [
'pool' => $pool,
]));
}
Add an item to a group. Route items opens a angular view.
public luya\admin\components\AdminMenuBuilder itemRoute ( $name, $route, $icon, $searchModelClass = null, array $options = [] ) | ||
$name | string |
The name of the Api (displayed as menu point in the left navigation), all names run through the |
$route | string |
The route to the template |
$icon | string |
The icon name based on the google icons font see https://design.google.com/icons/. |
$searchModelClass | string |
The search model must implement the {{luya\admin\base\GenericSearchInterface}}. |
$options | array |
An array with options you can provided and read inside the admin menu component. See {{\luya\admin\components\AdminMenuBuilder::verifyOptions}} for detail list and informations. |
public function itemRoute($name, $route, $icon, $searchModelClass = null, array $options = [])
{
$this->_menu[$this->_pointers['node']]['groups'][$this->_pointers['group']]['items'][] = [
'alias' => $name,
'route' => $route,
'icon' => $icon,
'permissionApiEndpoint' => null,
'permissionIsRoute' => true,
'permissionIsApi' => false,
'searchModelClass' => $searchModelClass,
'options' => $this->verifyOptions($options),
];
$this->_permissionRoutes[] = ['route' => $route, 'alias' => $name];
return $this;
}
The node is the menu entry in the TOP navigation of the luya administration interface.
public luya\admin\components\AdminMenuBuilder node ( $name, $icon, $template = false ) | ||
$name | string |
The name of the node, all names will process trough the |
$icon | string |
The icon name based on the google icons font see https://design.google.com/icons/. |
$template | boolean |
Whether to use a custom template or not. |
public function node($name, $icon, $template = false)
{
$this->_pointers['node'] = self::$index;
$this->_menu[self::$index] = [
'id' => self::$index,
'moduleId' => $this->moduleContext->id,
'template' => $template,
'routing' => $template ? 'custom' : 'default',
'alias' => $name,
'icon' => $icon,
'permissionRoute' => false,
'permissionIsRoute' => false,
'searchModelClass' => false,
];
self::$index++;
return $this;
}
A node which is a custom route to open, nodes are the top menu of the luya administration interfaces.
public luya\admin\components\AdminMenuBuilder nodeRoute ( $name, $icon, $route, $searchModelClass = null ) | ||
$name | string |
The name of the node, all names will process trough the |
$icon | string |
The icon name based on the google icons font see https://design.google.com/icons/. |
$route | string |
The route to the template which is going to be render by angular, example |
$searchModelClass | string |
The path to the model to search inside the admin global search, must implement the {{luya\admin\base\GenericSearchInterface}}. |
public function nodeRoute($name, $icon, $route, $searchModelClass = null)
{
$this->_pointers['node'] = self::$index;
$this->_menu[self::$index] = [
'id' => self::$index,
'moduleId' => $this->moduleContext->id,
'template' => $route, // as the template is equal to the route of the node which is loaded
'routing' => 'custom',
'alias' => $name,
'icon' => $icon,
'permissionRoute' => $route,
'permissionIsRoute' => true,
'searchModelClass' => $searchModelClass,
];
$this->_permissionRoutes[] = ['route' => $route, 'alias' => $name];
self::$index++;
return $this;
}
Verify the additional options of an itemRoute or itemApi item.
The following options are currently supported
- hiddenInMenu: If set to true the item will be hidden in the left menu, this is usefull when creating ngrest crud's for crud-realtion views.
protected array verifyOptions ( array $options = [] ) | ||
$options | array |
The options to verify |
return | array |
The verified allowed options. |
---|
protected function verifyOptions(array $options = [])
{
foreach ($options as $key => $value) {
if (!in_array($key, static::$options)) {
unset($options[$key]);
}
}
return $options;
}