Class luya\admin\ngrest\Config
Inheritance | luya\admin\ngrest\Config » yii\base\BaseObject |
---|---|
Implements | luya\admin\ngrest\ConfigInterface, yii\base\Configurable |
Available since version | 1.0.0 |
Source Code | https://github.com/luyadev/luya-module-admin/blob/master/src/ngrest/Config.php |
Defines and holds an NgRest Config.
Example config array to set via setConfig()
.
$array = [
'list' => [
'firstname' => [
'name' => 'firstname',
'alias' => 'Vorname',
'i18n' => false,
'extraField' => false,
'type' => [
'class' => '\\admin\\ngrest\\plugins\\Text',
'args' => ['arg1' => 'arg1_value', 'arg2' => 'arg2_value']
]
]
],
'create' => [
//...
]
];
Public Properties
Public Methods
Property Details
An array with hash, label and icon key.
public void setActiveSelections ( array $buttons )
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()");
}
Defined in: yii\base\BaseObject::__construct()
Constructor.
The default implementation does two things:
- Initializes the object with the given configuration
$config
. - Call init().
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$config
here. - call the parent implementation at the end of the constructor.
public void __construct ( $config = [] ) | ||
$config | array |
Name-value pairs that will be used to initialize the object properties |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
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);
}
}
public boolean addField ( $pointer, $field, array $options = [] ) | ||
$pointer | string | |
$field | string | |
$options | array |
public function addField($pointer, $field, array $options = [])
{
if ($this->hasField($pointer, $field)) {
return false;
}
$options = ArrayHelper::merge([
'name' => null,
'i18n' => false,
'alias' => null,
'type' => null,
'extraField' => false,
], $options);
// can not unshift non array value, create array for this pointer.
if (empty($this->_config[$pointer])) {
$this->_config[$pointer] = [];
}
$this->_config[$pointer] = ArrayHelper::arrayUnshiftAssoc($this->_config[$pointer], $field, $options);
return true;
}
public void appendFieldOption ( $fieldName, $optionKey, $optionValue ) | ||
$fieldName | string | |
$optionKey | string | |
$optionValue | string |
public function appendFieldOption($fieldName, $optionKey, $optionValue)
{
foreach ($this->getConfig() as $pointer => $fields) {
if (is_array($fields)) {
foreach ($fields as $field) {
if (isset($field['name'])) {
if ($field['name'] == $fieldName) {
$this->_config[$pointer][$field['name']][$optionKey] = $optionValue;
}
}
}
}
}
}
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();
}
Create a plugin object cased on a field array config.
public static luya\admin\ngrest\base\Plugin createField ( array $plugin ) | ||
$plugin | array |
public static function createField(array $plugin)
{
return NgRest::createPluginObject($plugin['type']['class'], $plugin['name'], $plugin['alias'], $plugin['i18n'], $plugin['type']['args']);
}
Get an array with the button configuration like hash, label and icon.
public array getActiveButtons ( ) | ||
return | array |
An array with hash, label and icon key. |
---|
public function getActiveButtons()
{
if ($this->_activeButtons === null) {
$buttons = $this->model->ngRestActiveButtons();
$btns = [];
foreach ($buttons as $button) {
$hash = sha1($button['class']);
$object = Yii::createObject($button);
$btns[] = [
'hash' => $hash,
'label' => $object->getLabel(),
'icon' => $object->getIcon(),
'condition' => $object->getCondition(),
'permissionLevel' => $object->getPermissionLevel(),
];
}
$this->_activeButtons = $btns;
}
return $this->_activeButtons;
}
Returns an array with object instance of {{luya\admin\ngrest\base\ActiveSelection}}
public luya\admin\ngrest\base\ActiveSelection[] getActiveSelections ( ) |
public function getActiveSelections()
{
return $this->_activeSelections;
}
public void getAttributeGroups ( ) |
public function getAttributeGroups()
{
return $this->_attributeGroups;
}
Returns an array with default order options.
public array getDefaultOrder ( ) |
public function getDefaultOrder()
{
return $this->_defaultOrder;
}
public void getDefaultOrderDirection ( ) |
public function getDefaultOrderDirection()
{
if (!$this->getDefaultOrder()) {
return false;
}
$direction = is_array($this->getDefaultOrder()) ? current($this->getDefaultOrder()) : null; // us preg split to find in string?
if ($direction == SORT_ASC || strtolower($direction) == 'asc') {
return '+';
}
if ($direction == SORT_DESC || strtolower($direction) == 'desc') {
return '-';
}
return '+';
}
public void getDefaultOrderField ( ) |
public function getDefaultOrderField()
{
if (!$this->getDefaultOrder()) {
return false;
}
return key($this->getDefaultOrder());
}
Get all extra fields.
public array getExtraFields ( ) |
public function getExtraFields()
{
if ($this->_extraFields === null) {
$extraFields = [];
foreach ($this->getConfig() as $pointer => $fields) {
if (is_array($fields)) {
foreach ($fields as $field) {
if (isset($field['extraField']) && $field['extraField']) {
if (!array_key_exists($field['name'], $extraFields)) {
$extraFields[] = $field['name'];
}
}
}
}
}
$this->_extraFields = $extraFields;
}
return $this->_extraFields;
}
public boolean getField ( $pointer, $field ) | ||
$pointer | string | |
$field | string |
public function getField($pointer, $field)
{
return $this->hasField($pointer, $field) ? $this->_config[$pointer][$field] : false;
}
public boolean[] getFields ( $pointer, array $fields ) | ||
$pointer | string | |
$fields | array |
public function getFields($pointer, array $fields)
{
$data = [];
foreach ($fields as $fieldName) {
if ($this->getField($pointer, $fieldName)) {
$data[$fieldName] = $this->getField($pointer, $fieldName);
}
}
return $data;
}
public void getFilters ( ) |
public function getFilters()
{
if ($this->_filters === null) {
$this->_filters = $this->model->ngRestFilters();
}
return $this->_filters;
}
Determine whether the groups are expanded by default or not.
If enabled, the groups are expanded otherwise they are collapsed.
public boolean getGroupByExpanded ( ) |
public function getGroupByExpanded()
{
return $this->_groupByExpanded;
}
public void getHash ( ) |
public function getHash()
{
if ($this->_hash === null) {
$this->_hash = md5((string) $this->getApiEndpoint());
}
return $this->_hash;
}
Getter method for the model
public luya\admin\ngrest\base\NgRestModel getModel ( ) |
public function getModel()
{
return $this->_model;
}
Get an option by its key from the options pointer. Define options like
$configBuilder->options = ['saveCallback' => 'console.log(this)'];
Get the option parameter
$config->getOption('saveCallback');
public boolean getOption ( $key ) | ||
$key | string |
public function getOption($key)
{
return ($this->hasPointer('options') && array_key_exists($key, $this->_config['options'])) ? $this->_config['options'][$key] : false;
}
Get all plugins.
public array getPlugins ( ) |
public function getPlugins()
{
if ($this->_plugins === null) {
$plugins = [];
foreach ($this->getConfig() as $pointer => $fields) {
if (is_array($fields)) {
foreach ($fields as $field) {
if (isset($field['type'])) {
$fieldName = $field['name'];
if (!array_key_exists($fieldName, $plugins)) {
$plugins[$fieldName] = $field;
}
}
}
}
}
$this->_plugins = $plugins;
}
return $this->_plugins;
}
public string|array getPointer ( $pointer, $defaultValue = false ) | ||
$pointer | string | |
$defaultValue | boolean | |
return | string|array |
If default value is an array, an array is returned as default value |
---|
public function getPointer($pointer, $defaultValue = false)
{
return $this->hasPointer($pointer) ? $this->_config[$pointer] : $defaultValue;
}
public void getPointerExtraFields ( $pointer ) | ||
$pointer |
public function getPointerExtraFields($pointer)
{
$extraFields = [];
foreach ($this->getPointer($pointer, []) as $field) {
if (isset($field['extraField']) && $field['extraField']) {
$extraFields[] = $field['name'];
}
}
return $extraFields;
}
Get all plugin objects for a given pointer.
public luya\admin\ngrest\base\Plugin getPointerPlugins ( $pointer ) | ||
$pointer | string |
The name of the pointer (list, create, update). |
return | luya\admin\ngrest\base\Plugin |
An array with plugin objects |
---|
public function getPointerPlugins($pointer)
{
$plugins = [];
foreach ($this->getPointer($pointer, []) as $field) {
$plugins[] = self::createField($field);
}
return $plugins;
}
public void getPrimaryKey ( ) |
public function getPrimaryKey()
{
if ($this->_primaryKey === null) {
$this->_primaryKey = $this->model->getNgRestPrimaryKey();
}
return $this->_primaryKey;
}
public void getRelations ( ) |
public function getRelations()
{
if ($this->_relations === null) {
// ensure relations are made not on composite table.
if ($this->model->ngRestRelations() && (is_countable($this->getPrimaryKey()) ? count($this->getPrimaryKey()) : 0) > 1) {
throw new InvalidConfigException("Its not allowed to have ngRestRealtions() on models with composite primary keys.");
}
// generate relations
$relations = [];
foreach ($this->model->generateNgRestRelations() as $relation) {
/** @var $relation \luya\admin\ngrest\base\NgRestRelationInterface */
$relations[] = [
'label' => $relation->getLabel(),
'apiEndpoint' => $relation->getApiEndpoint(),
'arrayIndex' => $relation->getArrayIndex(),
'modelClass' => $relation->getModelClass(),
'tabLabelAttribute' => $relation->getTabLabelAttribute(),
'relationLink' => $relation->getRelationLink(),
];
}
$this->_relations = $relations;
}
return $this->_relations;
}
public boolean hasField ( $pointer, $field ) | ||
$pointer | string | |
$field | string |
public function hasField($pointer, $field)
{
return $this->getPointer($pointer) ? array_key_exists($field, $this->_config[$pointer]) : false;
}
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);
}
public boolean hasPointer ( $pointer ) | ||
$pointer | string |
public function hasPointer($pointer)
{
return array_key_exists($pointer, $this->_config);
}
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()
{
}
Whether delete is enabled or not.
public boolean isDeletable ( ) |
public function isDeletable()
{
return $this->getPointer('delete') === true ? true : false;
}
public void onFinish ( ) |
public function onFinish()
{
foreach ($this->getPrimaryKey() as $pk) {
if (!$this->hasField('list', $pk)) {
$alias = $pk;
if (array_key_exists($alias, $this->_attributeLabels)) {
$alias = $this->_attributeLabels[$alias];
} elseif (strtolower($alias) == 'id') {
$alias = Module::t('model_pk_id'); // use default translation for IDs if not label is given
}
$this->addField('list', $pk, [
'name' => $pk,
'alias' => $alias,
'type' => [
'class' => 'luya\admin\ngrest\plugins\Number',
'args' => [],
],
]);
}
}
}
Setter method for the active button array from the model
public void setActiveButtons ( array $buttons ) | ||
$buttons |
public function setActiveButtons(array $buttons)
{
$this->_activeButtons = $buttons;
}
Set all active selection definitions
public void setActiveSelections ( array $buttons ) | ||
$buttons | array |
public function setActiveSelections(array $buttons)
{
$objects = [];
foreach ($buttons as $button) {
if (!array_key_exists('class', $button)) {
$button['class'] = ActiveSelection::class;
}
$objects[] = Yii::createObject($button);
}
$this->_activeSelections = $objects;
}
public void setApiEndpoint ( $apiEndpoint ) | ||
$apiEndpoint | string |
public function setApiEndpoint($apiEndpoint)
{
$this->_apiEndpoint = $apiEndpoint;
}
public void setAttributeGroups ( array $groups ) | ||
$groups | array |
public function setAttributeGroups(array $groups)
{
$this->_attributeGroups = $groups;
}
public void setAttributeLabels ( array $labels ) | ||
$labels |
public function setAttributeLabels(array $labels)
{
$this->_attributeLabels = $labels;
}
public void setConfig ( array $config ) | ||
$config |
public function setConfig(array $config)
{
if (!empty($this->_config)) {
throw new InvalidConfigException("Unable to override an already provided Config.");
}
$this->_config = $config;
}
public void setDefaultOrder ( $defaultOrder ) | ||
$defaultOrder |
public function setDefaultOrder($defaultOrder)
{
$this->_defaultOrder = $defaultOrder;
}
Setter method for filters.
public void setFilters ( array $filters ) | ||
$filters | array |
public function setFilters(array $filters)
{
$this->_filters = $filters;
}
public void setGroupByExpanded ( $groupByExpanded ) | ||
$groupByExpanded | boolean |
public function setGroupByExpanded($groupByExpanded)
{
$this->_groupByExpanded = $groupByExpanded;
}
public void setGroupByField ( $groupByField ) | ||
$groupByField | string |
public function setGroupByField($groupByField)
{
$this->_groupByField = $groupByField;
}
Setter methdo for ngrest model context.
The model that can be lazy loaded on request instead of preloading from model.
public void setModel ( luya\admin\ngrest\base\NgRestModel $model ) | ||
$model | luya\admin\ngrest\base\NgRestModel |
public function setModel(NgRestModel $model)
{
$this->_model = $model;
}
public void setPrimaryKey ( array $key ) | ||
$key | string |
public function setPrimaryKey(array $key)
{
$this->_primaryKey = $key;
}
public void setRelation ( luya\admin\ngrest\base\NgRestRelation $relation ) | ||
$relation |
public function setRelation(NgRestRelation $relation)
{
$this->_relations[] = $relation;
}
public void setTableName ( $tableName ) | ||
$tableName |
public function setTableName($tableName)
{
$this->_tableName = $tableName;
}