2017 © Pedro Peláez
 

yii2-extension yii2-options

This extension provide application configuration support stored in database.

image

futuretek/yii2-options

This extension provide application configuration support stored in database.

  • Sunday, July 22, 2018
  • by futuretek
  • Repository
  • 3 Watchers
  • 1 Stars
  • 202 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 21 Versions
  • 66 % Grown

The README.md

Yii2 Options

This extension provide application configuration support stored in database., (*1)

WARNING: Version 2.x introduces breaking changes. Upgrading from version 1.x is possible but requires additional actions., (*2)

Steps to migrate to version 2.x: * Create options config for every used option item * Include component in Yii2 config * Rewrite commands to new syntax (or use compat class futuretek\options\compat\Option), (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

php composer.phar require --prefer-dist futuretek/yii2-options "*"

or add, (*6)

"futuretek/yii2-options": "*"

to the require section of your composer.json file., (*7)

Configuration

To start using this extension you have to load it in Yii config:, (*8)

return [
    'components' => [
        'options' => [
            'class' => 'futuretek\options\Option',
            'configFile' => __DIR__ . '/options.php',
            'enableCaching' => true,
        ],     
    ],
];

The component itself can be configured with the following properties:, (*9)

Property Default value Description
db db The DB connection object or the application component ID of the DB connection.
cache cache The cache object or the application component ID of the cache object. The option data will be cached using this cache object.
optionTable {{%option}} The name of the option table.
cachingDuration 0 The time in seconds that the options can remain valid in cache. Use 0 to indicate that the cached data will never expire.
enableCaching false Whether to enable options caching
configFile Options config file. This file will be required. File must return config array.
config Variable into which the config from configFile will be loaded.

Notice: Option config is lazy-loaded on init (as opposite of including it in the application config). This is because the translation engine is not available at that time., (*10)

Option config/definition array contains list of the option groups., (*11)

Option group

Every option group consists of these attributes. Attributes marked with (*) are required., (*12)

  • title (*) - Group title
  • items (*) - Contains array of option items (see below)
  • visible - If the group is visible

Option items

Option group attribute items is array containing option items., (*13)

Option item consists of these attributes. Attributes marked with (*) are required., (*14)

  • name (*) - Option name (unique)
  • type (*) - Option type. See Option::TYPE_* constants.
  • title (*) - Option title
  • hint - Option description
  • visible - If the option is visible
  • default - Specify option default value. Getting defined but unset option yields this value. When not set, null will be returned.
  • context - If the option is context option. If this is true, option will not be rendered using provided actions.
  • data - For option type=TYPE_OPTION this attribute contains data in form of:
    1. array of arrays containing keys id and name (see example below) or
    2. anonymous function returning the same array

ADVICE: If you need to create divider between options in the same option group, simply put string as one of the items array item., (*15)

Config example, (*16)

return [
    [
        'title' => Yii::t('app', 'Group one'),
        'visible' => true,
        'items' => [
            ['name' => 'PHONE', 'type' => Option::TYPE_PHONE, 'title' => Yii::t('app', 'Phone number')],
            ['name' => 'EMAIL', 'type' => Option::TYPE_EMAIL, 'title' => Yii::t('app', 'Email address')],
            Yii::t('app', 'Apperance'), //Divider
            [
                'name' => 'COLOR',
                'type' => Option::TYPE_OPTION,
                'title' => Yii::t('app', 'Color scheme'),
                'data' => [
                    ['id' => 'ff0000', 'name' => 'Red'],
                    ['id' => '00ff00', 'name' => 'Green'],
                    ['id' => '0000ff', 'name' => 'Blue'],
                ],
            ],
        ],
    ],
    [
        'title' => Yii::t('app', 'Another group'),
        'visible' => YII_ENV_DEV, //Visible only in dev env
        'items' => [
            [
                'name' => 'LOGIN_TYPE',
                'type' => Option::TYPE_OPTION,
                'title' => Yii::t('app', 'Login Type'),
                'hint' => Yii::t('app', 'Please select desired login type'),
                'data' => function () {
                    return \app\classes\Tools::findAvailableLoginProviders();
                },
            ],
        ],
    ],
];

Usage

This extension creates options component for storing configuration., (*17)

Getting options, (*18)

$value = \Yii::$app->options->get('OPTION_NAME');

Setting options, (*19)

\Yii::$app->options->set('OPTION_NAME', $value);

Context options, (*20)

Context comes handy in case you need to deal with storing different configuration for multiple entities (for example user configuration)., (*21)

In this use case context can be uses. Simply specify entity identificator as context and entity record id as context_id and you are done., (*22)

Getting and setting options is done via another arguments of methods get() and set()., (*23)

$userValue = \Yii::$app->options->get('OPTION_NAME', self::class, $this->getPrimaryKey());
\Yii::$app->options->set('OPTION_NAME', $userValue, self::class, $this->getPrimaryKey());

Rendering settings page

If you want to render settings page, there is futuretek\options\IndesAction provided. You can simply add it to the controller actions() method., (*24)

There is also futuretek\options\OptionsHelper that provides method for rendering option edit field based on option type., (*25)

See mentioned files for more details., (*26)

Development

Translations

Translations are managed trough standard Yii2 translations. To automatically register extension translations we recommend to use our another extension futuretek/yii2-composer., (*27)

To generate messages for translating run the following command in extension root directory., (*28)

yii message/extract messages/config.php

Assets

Assets are managed by Compass, (*29)

  • While developing run compass watch in extension root directory
  • To compile assets for final distribution run compass compile -e production --force in extension root directory.

The Versions

22/07 2018

dev-master

9999999-dev http://www.futuretek.cz/

This extension provide application configuration support stored in database.

  Sources   Download

Apache-2.0

The Requires

 

component extension model yii options

22/07 2018

2.0.4

2.0.4.0 http://www.futuretek.cz/

This extension provide application configuration support stored in database.

  Sources   Download

Apache-2.0

The Requires

 

component extension yii options

22/07 2018

2.0.3

2.0.3.0 http://www.futuretek.cz/

This extension provide application configuration support stored in database.

  Sources   Download

Apache-2.0

The Requires

 

component extension yii options

21/07 2018

2.0.2

2.0.2.0 http://www.futuretek.cz/

This extension provide application configuration support stored in database.

  Sources   Download

Apache-2.0

The Requires

 

component extension yii options

12/06 2018

2.0.1

2.0.1.0 http://www.futuretek.cz/

This extension provide application configuration support stored in database.

  Sources   Download

Apache-2.0

The Requires

 

component extension yii options

29/04 2018

2.0.0

2.0.0.0 http://www.futuretek.cz/

This extension provide application configuration support stored in database.

  Sources   Download

Apache-2.0

The Requires

 

component extension yii options

09/04 2018

1.2.5

1.2.5.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

09/04 2018

1.2.4

1.2.4.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

14/03 2018

1.2.3

1.2.3.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

18/10 2017

1.2.2

1.2.2.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

17/10 2017

1.2.1

1.2.1.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

13/10 2017

1.2.0

1.2.0.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

24/09 2017

1.1.3

1.1.3.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

19/09 2017

1.1.2

1.1.2.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

19/09 2017

1.1.1

1.1.1.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

18/09 2017

1.1.0

1.1.0.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

18/09 2017

1.0.4

1.0.4.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

18/09 2017

1.0.3

1.0.3.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

15/09 2017

1.0.2

1.0.2.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

15/09 2017

1.0.1

1.0.1.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options

14/09 2017

1.0.0

1.0.0.0 http://www.futuretek.cz/

Database configuration storage support

  Sources   Download

Apache-2.0

The Requires

 

extension model yii options