1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Forms\Field\Props;
/**
* Trait for classes:
* - `\MvcCore\Ext\Forms\Fields\DataList`
* - `\MvcCore\Ext\Forms\Fields\Select`
* - `\MvcCore\Ext\Forms\Fields\CountrySelect`
* - `\MvcCore\Ext\Forms\FieldsGroup`
* - `\MvcCore\Ext\Forms\CheckboxGroup`
* - `\MvcCore\Ext\Forms\RadioGroup`
*/
trait Options {
/**
* Form group control options to render
* more sub-control attributes for specified
* submitted values (array keys).
* This property configuration is required.
*
* Example:
* ```
* // To configure for example radio buttons named: `gender` for `Female` and `Male`:
* // <label for="gender-f">Female:</label>
* // <input id="gender-f" type="radio" name="gender" value="f" />
* // <label for="gender-m">Male:</label>
* // <input id="gender-m" type="radio" name="gender" value="m" />
* // use this configuration:
* $field->name = 'gender';
* $field->options = array(
* 'f' => 'Female',
* 'm' => 'Male',
* );
*
* // Or you can use more advanced configuration with css class names
* // and html element attributes, let's consider html code like this:
* // <label for="gender-f" class="female">Female:</label>
* // <input id="gender-f" type="radio" name="gender" value="f" class="female" data-any="something-for-females" />
* // <label for="gender-m" class="male">Male:</label>
* // <input id="gender-m" type="radio" name="gender" value="m" class="male" data-any="something-for-males" />
* // For that use this configuration:
* $field->name = 'gender';
* $field->options = array(
* 'f' => array(
* 'text' => 'Female', // text key will be also automatically translated
* 'class' => 'female',
* 'attrs' => array('data-any' => 'something-for-females'),
* ),
* 'm' => array(
* 'text' => 'Male', // text key will be also automatically translated
* 'class' => 'male',
* 'attrs' => array('data-any' => 'something-for-males'),
* ),
* ));
* ```
* @requires
* @var array
*/
protected $options = [];
/**
* Temp flatten key/value array to cache flatten options for submit checking.
* @var array|NULL
*/
protected $flattenOptions = NULL;
/**
* Boolean about to translate options texts, default `TRUE` to translate.
* @var bool
*/
protected $translateOptions = TRUE;
/**
* Set form control or group control options to render
* more values for more specified submitted keys.
*
* Example:
* ```
* // To configure for example radio buttons named: `gender` for `Female` and `Male`:
* // <label for="gender-f">Female:</label>
* // <input id="gender-f" type="radio" name="gender" value="f" />
* // <label for="gender-m">Male:</label>
* // <input id="gender-m" type="radio" name="gender" value="m" />
* // use this configuration:
* $field->SetName('gender')->SetOptions(array(
* // field values will be automatically translated,
* // if form has configured translator `callable`
* 'f' => 'Female',
* 'm' => 'Male',
* ));
*
* // Or you can use more advanced configuration with css class names
* // and html element attributes, let's consider html code like this:
* // <label for="gender-f" class="female">Female:</label>
* // <input id="gender-f" type="radio" name="gender" value="f" class="female" data-any="something-for-females" />
* // <label for="gender-m" class="male">Male:</label>
* // <input id="gender-m" type="radio" name="gender" value="m" class="male" data-any="something-for-males" />
* // For that use this configuration:
* $field->SetName('gender')->SetOptions(array(
* 'f' => array(
* 'text' => 'Female', // text key will be also automatically translated
* 'class' => 'female',
* 'attrs' => array('data-any' => 'something-for-females'),
* ),
* 'm' => array(
* 'text' => 'Male', // text key will be also automatically translated
* 'class' => 'male',
* 'attrs' => array('data-any' => 'something-for-males'),
* ),
* ));
* ```
* @param array $options
* @return \MvcCore\Ext\Forms\Field
*/
public function SetOptions (array $options = []) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->options = $options;
return $this;
}
/**
* Add form control or group control options to render
* more values for more specified submitted keys.
* Previous options will be merged with given options.
* @param array $options
* @return \MvcCore\Ext\Forms\Field
*/
public function AddOptions (array $options = []) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->options = array_merge($this->options, $options);
return $this;
}
/**
* Return reference to configured options array.
* @return array
*/
public function & GetOptions () {
/** @var $this \MvcCore\Ext\Forms\Field */
return $this->options;
}
/**
* Set `FALSE` if you don't want to translate options texts, default `TRUE`.
* @param bool $translateOptions
* @return \MvcCore\Ext\Forms\Field
*/
public function SetTranslateOptions ($translateOptions = TRUE) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->translateOptions = $translateOptions;
return $this;
}
/**
* Return boolean if options are translated or not.
* @return bool
*/
public function GetTranslateOptions () {
/** @var $this \MvcCore\Ext\Forms\Field */
return $this->translateOptions;
}
/**
* Merge given field options with possible grouped options into single
* level flatten array for submit checking purposes.
* @param array|NULL $fieldOptions
* @return array
*/
public function & GetFlattenOptions (array $fieldOptions = NULL) {
if ($fieldOptions === NULL && $this->flattenOptions !== NULL)
return $this->flattenOptions;
$this->flattenOptions = [];
/** @var $this \MvcCore\Ext\Forms\Field */
$fieldOptions = $fieldOptions === NULL
? $this->options
: $fieldOptions;
foreach ($fieldOptions as $key1 => $value1) {
if (is_scalar($value1)) {
// most simple key/value array options configuration
$this->flattenOptions[$key1] = $value1;
} else if (is_array($value1)) {
if (array_key_exists('options', $value1) && is_array($value1['options'])) {
// `<optgroup>` options configuration
$subOptions = $value1['options'];
foreach ($subOptions as $key2 => $value2) {
if (is_scalar($value2)) {
// most simple key/value array options configuration
$this->flattenOptions[$key2] = $value2;
} else if (is_array($value2)) {
// advanced configuration with key, text, cs class,
// and any other attributes for single option tag
$value = array_key_exists('value', $value2)
? $value2['value']
: $key2;
$text = array_key_exists('text', $value2)
? $value2['text']
: $key2;
$this->flattenOptions[$value] = $text;
}
}
} else {
// advanced configuration with `value`, `text`, `attrs` or css class,
// and any other attributes for single option tag:
$value = array_key_exists('value', $value1)
? $value1['value']
: $key1;
$text = array_key_exists('text', $value1)
? $value1['text']
: $key1;
$this->flattenOptions[$value] = $text;
}
}
}
return $this->flattenOptions;
}
}