Core Setting
The Setting component provides a generic interface for handling configuration options. Configuration data can be stored in the database or the file system and can be manipulated through a GUI or the command line.
Integration
Initialization
The first step is to initialize a repository. A repository is a container of setting options of a specific component. The repository can either be stored in the database or in the file system using the appropriate storage engine. All options in a repository are organized in groups.
Note
If the repository does not yet exists, it'll be created on the fly on storage.
The syntax to initialize a repository is as follows:
Where the arguments have the following meaning:
Argument | Description | Optional |
---|---|---|
<component> | The component to load the options from. I.e: 'Shop' | No |
<group> | Load only the options of group <group> | Yes |
<engine> | Set engine to use | Yes |
Example
Initialize repository of component TodoManager with engine FileSystem:
This will store the setting data in the file config/TodoManager.yml.
Add/define configuration options
A setting repository can hold an unlimited number of options.
The syntax to add a new configuration option is as follows:
Where the arguments have the following meaning:
Argument | Description | Optional |
---|---|---|
<option-name> | The name of the configuration option to add | No |
<option-value> | The initial default value of the configuration option | No |
<order-id> | An integer used to put the option in order (in a GUI) in relation to the other defined configuration options. The higher the integer is, the later will the option be parsed (in relation to other options having a lower integer set). | Yes |
<option-type> | Any of the available option types. Defaults to 'text'. | Yes |
<option-data> | Additional option types specific data. I.e. for option type Radio the available radio options are defined through this argument. | Yes |
<group> | Assign the option to the specified group. If left empty, the option will be added to the last registered/initialized group. | No |
Example
Warning
The repository must have been initialized before a new option can be added.
Add a simple text option to group config:
Add settings for a component
The easiest way to add settings for a component is to specify them in Component.yml. The structure defined for this is as follows:
The placeholders have the exact same meaning as specified in the table above. All indexes except for
flags
are mandatory. The following flags are known:
user
: Creates a user-specific setting.preInit
: Set this if you need this setting to be available in preInit hooks.
The "section" is automatically set to the component's name.
By default the "Database" engine is used, unless one of the component's settings has the preInit
flag set in which case the "FileSystem" engine is used for all settings for this component.
Show configuration GUI
A simple configuration GUI can be generated from an initialized setting repository. If the setting repository contains more than one group, then only the options of a selected group can get parsed or the options of each group can be parsed into a separate tab.
The basic syntax is as follows:
Where the arguments have the following meaning:
Argument | Description | Optional |
---|---|---|
<template-object> | Instance of \Cx\Core\Html\Sigma into which the configuration GUI will be parsed into. The instance can either be empty (no template loaded) or have a custom template set to be used for parsing the configuration GUI. If the instance has no template set, then the default template for the configuration GUI will be used and parsed. After the GUI has been parsed it can be fetched from the template instance using the method \Cx\Core\Html\Sigma::get(). | No |
<url-of-current-section> | The URI of the current backend section. I.e.: /cadmin/Config | No |
<component> | The component to parse the options from. If not set, then options of the currently initialized component are parsed. | Yes |
<tab-name> | When set, it will generate a tab by the name <tab-name>. This is used if there are more than one group present in the setting repository. | Yes |
<text-variable-prefix> | For each configuration option, a label and tooltip can be assigned through a Text-Variable. The taken Text-Variable for the label of the option is based on the capitalized form of the option's name and the prefix <text-variable-prefix>. The <text-variable-prefix> defaults to TXT_. I.e. for option simpleTextOption the Text-Variable for the label has the form TXT_SIMPLETEXTOPTION. Note: According to the guidelines the prefix must have the following scheme: TXT_<COMPONENT>_. More information can be found here: Text-Variable. | Yes |
<readonly> | Whether the configuration GUI shall be in read-only mode which does not allow any modification of the parsed options. Defaults to false | Yes |
Example
The following generates the configuration GUI for the options of group general of component TodoManager:
Store changes from configuration GUI
Changes made to any options in the configuration GUI can be updated in the repository using the method \Cx\Core\Setting\Controller\Setting::storeFromPost().
Example
Automatically store all options from POST-data (from auto-generated view):
Fetch data of options
The data of the configuration options can be accessed using the method \Cx\Core\Setting\Controller\Setting::getValue().
Warning
Before any data can be accessed, the setting repository must be initialized first.
Example
Once a setting repository has been initialized, it can be accessed in the same cx-request without having the need to re-initialize it again. To ensure the option is fetched from the repository of choice, simple add the repository's component as second argument to \Cx\Core\Setting\Controller\Setting::getValue():
Global Configuration
The Global Configuration section (Administration > Global Configuration > System) does use this library to store and manage its configuration options. As a special extension to the Setting
library, the configuration options of the Global Configuration are shadowed into the file config/settings.php
. This is used to make the Global Configuration options available in a very early stage of the bootstrapping process.
Note
The Setting
library is not available until the postInit
-hook. To overcome this issue, the Global Configuration options are accessible through $this->cx->getConfig()->getBaseConfig($key)
in earlier stages.
Warning
However, from the postInit
-hook onwards you must always use the Setting
library to access the Global Configuration options. Accessing the Global Configuration options through $this->cx->getConfig()
after the postComponentLoad
-hook (which is the predecessor of postInit
) is regarded as deprecated code. More information can be found here: postInit
.
Engines
The interface provides the following storage engines that can be used as the target medium for storage.
Engine |
Description |
---|---|
Database (default) |
Stores the configuration options in the database (table contrexx_core_setting) |
FileSystem |
Stores the configuration options in the filesystem in plain YAML notation (in ''config/ .yml'') |
Yaml Deprecated |
Stores the configuration options as serialized objects in the filesystem in YAML notation (in ''config/ .yml'') |
Option Types
The library supports the following option types.
Option |
Type (PHP) |
Type (CLI) |
Description |
Additional specific data |
Examples |
---|---|---|---|---|---|
Dropdown |
TYPE_DROPDOWN |
dropdown |
A HTML-select dropdown. Options are defined in attribute values either as a static comma-separated list or dynamic by referencing a callback function. |
Separate options by comma: To use custom option keys, do use the scheme
Use the following scheme to specify a callback function to fetch the available options:
|
|
Multiselect |
TYPE_DROPDOWN_MULTISELECT |
dropdown_multiselect |
A HTML-select element with multiple selection. |
Options are defined analogous to type Dropdown |
|
User profile attribute dropdown |
TYPE_DROPDOWN_USER_CUSTOM_ATTRIBUTE |
dropdown_user_custom_attribute |
HTML-select dropdown of preconfigured profile attributes of users |
- |
|
User group dropdown |
TYPE_DROPDOWN_USERGROUP |
dropdown_usergroup |
HTML-select dropdown of existing user groups |
- |
|
WYSIWYG |
TYPE_WYSIWYG |
wysiwyg |
WYSIWYG-editor for HTML content |
- |
|
File upload |
TYPE_FILEUPLOAD |
fileupload |
Uploader-element |
- |
|
Text |
TYPE_TEXT |
text |
A regular HTML-input element |
Set special input parsing behavior through JSON-encoded option
|
|
Multiline text |
TYPE_TEXTAREA |
textarea |
HTML-textarea |
- |
|
TYPE_EMAIL |
HTML-input type for email |
- |
|
||
Checkbox |
TYPE_CHECKBOX |
checkbox |
HTML-input type checkbox |
- |
|
Radio |
TYPE_RADIO |
radio |
HTMl-input type radio |
Options are defined analogous to type Dropdown |
|
Date |
TYPE_DATE |
date |
Date selector |
- |
|
Datetime |
TYPE_DATETIME |
datetime |
Datetime selector |
- |
|
Image |
TYPE_IMAGE |
image |
An image selection option. Two modes are suppored: reference (default) and copy |
Mode reference is used to store the path to the selected image.
Mode copy is used to copy the selected image to the location set in attribute value. |
|
File content |
TYPE_FILECONTENT |
file |
A HTML-textarea with content of specified file. |
Specify path of file to edit in attribute values. Path to file must be relative to the installations document root. |
|
Button Experimental |
TYPE_BUTTON |
button |
|||
Checkbox group Experimental |
TYPE_CHECKBOXGROUP |
checkboxgroup |
Advanced usages
Conditional option adding
Add new configuration option only if it does not yet exist and throw exception in case the operation fails.
Array populating options
If section is already initialized, we can define behavior of populating this section with data. For that purpose we have 5th parameter. Assuming we have already initialized section Config\Yaml and have following data in it's array: array(1,2)
, and by calling init of this section we would retrieve array(2,3,4)
. Then:
NOT_PUPULATE
(default)
POPULATE
REPOPULATE
Switching section, engine, module
There are several ways to switch between section and engine once initialized.
By calling setEngineType
(Recommended)
By calling init()
Fetching data
Fetch all configuration options at once:
getSettings()
(Recommended)
Fetch all options from a repository:
Fetch options of a specific group of a repository:
getArray()
Fetch all options from a repository:
Fetch options of a specific group of a repository:
Display configuration options
Integrate external data into configuration GUI (like MailTemplate GUI)
Update configuration option
Update single configuration option
Update multiple configuration options
CLI
The Setting component is available through the CLI interface of Cloudrexx.
It provides the ability to list all options from a specific repository or to add/remove and get/set options in a repository.
Fetch options
Synopsis
Argument |
Description |
Optional |
---|---|---|
<component> |
The component to load the options from. I.e: 'Shop' |
No |
<group> |
Load only the options of group |
Yes |
<engine> |
Set engine to use |
Yes |
<repository> |
Relative path to the directory where the repository is located. I.e. if the repository of the component Shop is located at tmp/data/Shop.yml then you would set this argument to: tmp/data This option defaults to directory "config" of the Cloudrexx installation. |
Yes |
Example
List all options from Yaml repository of component Config:
Add option
Synopsis
Argument |
Description |
Optional |
---|---|---|
<component> |
The component to load the options from. I.e: 'Shop' |
No |
<group> |
Load only the options of group |
Yes |
<engine> |
Set engine to use |
Yes |
<repository> |
Relative path to the directory where the repository is located. I.e. if the repository of the component Shop is located at tmp/data/Shop.yml then you would set this argument to: tmp/data This option defaults to directory "config" of the Cloudrexx installation. |
Yes |
<name> |
The name of the configuration option to add |
No |
<value> |
The initial default value of the configuration option |
No |
<ord> |
An integer used to put the option in order (in a GUI) in relation to the other defined configuration options. The higher the integer is, the later will the option be parsed (in relation to other options having a lower integer set). |
No |
<type> |
Any of the available option types. Defaults to 'text'. |
No |
<values> |
Additional option types specific data. I.e. for option type Radio the available radio options are defined through this argument. Note: For option types without specific data do set to an empty quoted string |
No |
Examples
Alter option
Synopsis
Argument |
Description |
Optional |
---|---|---|
<component> |
The component to load the option from. I.e: 'Shop' |
No |
<group> |
The group to load the option from |
Yes |
<engine> |
Set engine to use |
Yes |
<repository> |
Relative path to the directory where the repository is located. I.e. if the repository of the component Shop is located at tmp/data/Shop.yml then you would set this argument to: tmp/data This option defaults to directory "config" of the Cloudrexx installation. |
Yes |
<name> |
The name of the configuration option to alter |
No |
<new_type> |
Any of the available option types. |
No |
<new_values> |
Additional option types specific data. I.e. for option type Radio the available radio options are defined through this argument. Note: For option types without specific data do set to an empty quoted string |
No |
<new_group> |
New group to assign the option to |
Yes |
<new_ord> |
An integer used to put the option in order (in a GUI) in relation to the other defined configuration options. The higher the integer is, the later will the option be parsed (in relation to other options having a lower integer set). |
Yes |
Examples
Delete option
Synopsis
Argument |
Description |
Optional |
---|---|---|
<component> |
The component to load the options from. I.e: 'Shop' |
No |
<group> |
Load only the options of group |
Yes |
<engine> |
Set engine to use |
Yes |
<repository> |
Relative path to the directory where the repository is located. I.e. if the repository of the component Shop is located at tmp/data/Shop.yml then you would set this argument to: tmp/data This option defaults to directory "config" of the Cloudrexx installation. |
Yes |
<name> |
The name of the configuration option to delete |
No |
Example
Delete option used to define if virtual language directories shall be used:
Update option
Synopsis
Argument |
Description |
Optional |
---|---|---|
<component> |
The component to load the options from. I.e: 'Shop' |
No |
<group> |
Load only the options of group |
Yes |
<engine> |
Set engine to use |
Yes |
<repository> |
Relative path to the directory where the repository is located. I.e. if the repository of the component Shop is located at tmp/data/Shop.yml then you would set this argument to: tmp/data This option defaults to directory "config" of the Cloudrexx installation. |
Yes |
<name> |
The name of the configuration option to update |
No |
<value> |
The new value of the configuration option to store |
No |
Example
Activate the use of virtual language directories:
Fetch option
Synopsis
Argument |
Description |
Optional |
---|---|---|
<component> |
The component to load the options from. I.e: 'Shop' |
No |
<group> |
Load only the options of group |
Yes |
<engine> |
Set engine to use |
Yes |
<repository> |
Relative path to the directory where the repository is located. I.e. if the repository of the component Shop is located at tmp/data/Shop.yml then you would set this argument to: tmp/data This option defaults to directory "config" of the Cloudrexx installation. |
Yes |
<name> |
The name of the configuration option to fetch |
No |
Example
Get if the usage of virtual language directories is activated:
1 |
|
API Documentation
TODO: This should be moved into the API documentation
-
static function changed()
- TRUE, if minimum one of the loaded setting has been changed (updateAll() will check these)
- \Cx\Core\Setting\Controller\Setting::changed()
-
static function tab_index($tab_index=null)
- Get or set the current tab
- \Cx\Core\Setting\Controller\Setting::tab_index(integer $tab_index)
-
static function init($section, $group = null, $engine = 'Database', $fileSystemConfigRepository = null, $populate = 0)
- Activate or load the setting for a specific module (as the case may be constricted on a given group)
- Section must be set up
- The Engine type Database or File system, Default to set Database, You may use engine type FileSystem
- Database engine: \Cx\Core\Setting\Controller\Setting::init($section, $group,$engine = 'Database')
- FileSystem engine: \Cx\Core\Setting\Controller\Setting::init($section, $group,$engine = 'FileSystem')
- Yaml engine: \Cx\Core\Setting\Controller\Setting::init($section, $group,$engine = 'Yaml')
- Once you initialize section, it is stored in global Setting array and you don't not need to reinitialize it, unless other specified.
- You may set behavior of what to do when section already exists with $populate:
- \Cx\Core\Setting\Controller\Setting::NOT_POPULATE - do nothing, return; default behavior
- \Cx\Core\Setting\Controller\Setting::POPULATE - add values to existing array
- \Cx\Core\Setting\Controller\Setting::REPOPULATE - reload section; default behavior in old logic
- Example: \Cx\Core\Setting\Controller\Setting::init($section, $group, $engine, null, \Cx\Core\Setting\Controller\Setting::POPULATE)
-
static function flush()
- Reset the content of the class (forces for example the re-initialization at the next getArray())
- \Cx\Core\Setting\Controller\Setting::flush()
-
static function getArray($section = null, $group=null)
- Returns array of current engine based on $section and $group.
- If no section is specified, currently set section is taken into consideration.
-
static function getValue($name, $section = null)
- Returns the value for the setting with the given name
- If $section is specified, loads value from $section, otherwise loads value from currently set section
- \Cx\Core\Setting\Controller\Setting::getValue($name, $section)
-
static function set($name, $value)
- Set the value of the setting with the given name
- \Cx\Core\Setting\Controller\Setting::set($name,$value)
-
static function updateAll()
- Stores the loaded settings into the database or FileSystem\Yaml (only if minimum one change have been done)
- \Cx\Core\Setting\Controller\Setting::updateAll()
-
static function update($name)
- Stores one setting (without check!)
- Updates the value for the given name in the settings,note that this method does not work for adding new settings.
- \Cx\Core\Setting\Controller\Setting::update($name)
-
'static function add($name, $value, $ord=false, $type='text', $values=, $group=null)'''
- Adds a new setting to the group
- This method is only used for an installation script for a new module or will be used in update.
- \Cx\Core\Setting\Controller\Setting::add( $name, $value, $ord, $type, $values,$group)
-
static function storeFromPost()
- Update and store all settings found in the post array
- \Cx\Core\Setting\Controller\Setting::storeFromPost()
-
static function delete($name=null, $group=null)
- Deletes the setting with the given name. Only for advanced users!
- Delete one or more records from the Database or FileSystem
- At least one of the parameter values must be non-empty.
- \Cx\Core\Setting\Controller\Setting::delete($name, $group)
-
static function show(&$objTemplateLocal, $uriBase, $section=, $tab_name=, $prefix='TXT_')
- Shows the settings in the given template object
- \Cx\Core\Setting\Controller\Setting::show($objTemplate, $uriBase, $section, $tab_name, $prefix)
-
static function show_section(&$objTemplateLocal, $section=, $prefix='TXT_')
- Shows the settings for the given group in the given template object
- \Cx\Core\Setting\Controller\Setting::show_section($objTemplate, $section, $prefix)
-
static function show_external(&$objTemplateLocal, $tab_name, $content)
- Includes an external site (e.g. MailTemplates) into the Setting view (as a new tab)
- Adds an external settings view to the current template
- \Cx\Core\Setting\Controller\Setting::show_external($objTemplate, $tab_name, $content)
-
static function storeFromPost()
- Stores POST data and saves it to Database\FileSystem\Yaml using set() and updateAll().
- Should always be used to store POST data.
- \Cx\Core\Setting\Controller\Setting::storeFromPost()
-
static function getSectionEngine($section = null, $engine = null)
- Returns engine of section.
- If no section specified takes currenty set section.
- If section don't exist throws exception.
- If $engine don't exist takes default engine of section.
- \Cx\Core\Setting\Controller\Setting::getSectionEngine($section, $engine)
-
static function getSectionEngine($section = null, $engine = null)
- Returns engine of section.
- If no section specified takes currenty set section.
- If section don't exist throws exception.
- If $engine don't exist takes default engine of section.
- \Cx\Core\Setting\Controller\Setting::getSectionEngine($section, $engine)
-
static function setSectionEngine($oSectionEngine, $populate)
- Sets engine for section. Uses by init() but can also be called separately;
- If engine of section already exists - acts depending of $populate behavior:
- \Cx\Core\Setting\Controller\Setting::NOT_POPULATE - do nothing, return; default behavior
- \Cx\Core\Setting\Controller\Setting::POPULATE - add values to existing array
- \Cx\Core\Setting\Controller\Setting::REPOPULATE - reload section; default behavior in old logic
- \Cx\Core\Setting\Controller\Setting::setSectionEngine($oSectionEngine, $populate)
-
static function setEngineType($section, $engine, $group = null)
- Sets current section, engine and group;
- Once all data is initialized, should be used instead of init(); in case you want to get specific section\engine\group (i.e. in tabs)!
- \Cx\Core\Setting\Controller\Setting::setEngineType($section, $engine, $group)
-
static function addToArray($name, $value)
- Adds an element to array.
- Current section and engine are taken into consideration.
- \Cx\Core\Setting\Controller\Setting::addToArray($name, $value)
-
static function getSettings($section = null, $engine = null, $group = null)
- Basic method to get data array easy based of section, engine and group. Recommended to use!
- If section is null returns whole multidimensional array of all engines and sections.
- If section is set returns multidimensional array of all engines of that section.
- If section and engine is set returns data array from that section and engine.
- If section, engine and group are specified returns elements of concrete group from section and engine array.
- ''\Cx\Core\Setting\Controller\Setting::getSettings($section = null, $engine = null, $group = null)
-
static function getCurrentSettings()
- Returns data array based on currently set section, engine and group.
- Current section, engine and group can be set up via \Cx\Core\Setting\Controller\Setting::setEngineType($section, $engine, $group)
- Recommended to use in most of cases (i.e. in storeFromPost or other functions that work with data arrays).
- \Cx\Core\Setting\Controller\Setting::getCurrentSettings()