FieldConfig
2 years ago
Interfaces
4 months ago
Measurable
1 month ago
Plugin
1 month ago
Storage
1 month ago
FieldConfig.php
1 year ago
Setting.php
1 month ago
Settings.php
3 months ago
FieldConfig.php
255 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Settings; |
| 10 | |
| 11 | use Piwik\Validators\BaseValidator; |
| 12 | /** |
| 13 | * Lets you configure a form field. |
| 14 | * |
| 15 | * @api |
| 16 | */ |
| 17 | class FieldConfig |
| 18 | { |
| 19 | /** |
| 20 | * Shows a radio field. To use this field assign it to the `$uiControl` property. |
| 21 | */ |
| 22 | public const UI_CONTROL_RADIO = 'radio'; |
| 23 | /** |
| 24 | * Shows a text field. To use this field assign it to the `$uiControl` property. |
| 25 | */ |
| 26 | public const UI_CONTROL_TEXT = 'text'; |
| 27 | /** |
| 28 | * Shows an email text field. To use this field assign it to the `$uiControl` property. |
| 29 | */ |
| 30 | public const UI_CONTROL_EMAIL = 'email'; |
| 31 | /** |
| 32 | * Shows a URL text field. To use this field assign it to the `$uiControl` property. |
| 33 | */ |
| 34 | public const UI_CONTROL_URL = 'url'; |
| 35 | /** |
| 36 | * Shows a text area. To use this field assign it to the `$uiControl` property. |
| 37 | */ |
| 38 | public const UI_CONTROL_TEXTAREA = 'textarea'; |
| 39 | /** |
| 40 | * Shows a checkbox. To use this field assign it to the `$uiControl` property. |
| 41 | */ |
| 42 | public const UI_CONTROL_CHECKBOX = 'checkbox'; |
| 43 | /** |
| 44 | * Shows a password field. To use this field assign it to the `$uiControl` property. |
| 45 | */ |
| 46 | public const UI_CONTROL_PASSWORD = 'password'; |
| 47 | /** |
| 48 | * Shows a select field where a user can select multiple values. |
| 49 | * The type "Array" is required for this ui control. To use this field assign it to the `$uiControl` property. |
| 50 | */ |
| 51 | public const UI_CONTROL_MULTI_SELECT = 'multiselect'; |
| 52 | /** |
| 53 | * Shows a select field. To use this field assign it to the `$uiControl` property. |
| 54 | */ |
| 55 | public const UI_CONTROL_SINGLE_SELECT = 'select'; |
| 56 | /** |
| 57 | * Shows an expandable select field which is useful when each selectable value belongs to a group. |
| 58 | * To use this field assign it to the `$uiControl` property. |
| 59 | */ |
| 60 | public const UI_CONTROL_SINGLE_EXPANDABLE_SELECT = 'expandable-select'; |
| 61 | /** |
| 62 | * Lets a user configure an array of form fields. |
| 63 | */ |
| 64 | public const UI_CONTROL_FIELD_ARRAY = 'field-array'; |
| 65 | /** |
| 66 | * Lets a user configure two form fields next to each other, and add multiple entries of those two pairs. |
| 67 | */ |
| 68 | public const UI_CONTROL_MULTI_TUPLE = 'multituple'; |
| 69 | /** |
| 70 | * Generates a hidden form field. To use this field assign it to the `$uiControl` property. |
| 71 | */ |
| 72 | public const UI_CONTROL_HIDDEN = 'hidden'; |
| 73 | /** |
| 74 | * Expects an integer value. Is usually used when creating a setting. |
| 75 | */ |
| 76 | public const TYPE_INT = 'integer'; |
| 77 | /** |
| 78 | * Expects a float value. Is usually used when creating a setting. |
| 79 | */ |
| 80 | public const TYPE_FLOAT = 'float'; |
| 81 | /** |
| 82 | * Expects a string. Is usually used when creating a setting. |
| 83 | */ |
| 84 | public const TYPE_STRING = 'string'; |
| 85 | /** |
| 86 | * Expects a boolean. Is usually used when creating a setting. |
| 87 | */ |
| 88 | public const TYPE_BOOL = 'boolean'; |
| 89 | /** |
| 90 | * Expects an array containing multiple values. |
| 91 | */ |
| 92 | public const TYPE_ARRAY = 'array'; |
| 93 | /** |
| 94 | * Describes what HTML element should be used to manipulate the setting through Piwik's UI. |
| 95 | * |
| 96 | * See {@link Piwik\Plugin\Settings} for a list of supported control types. |
| 97 | * |
| 98 | * @var string |
| 99 | */ |
| 100 | public $uiControl = null; |
| 101 | /** |
| 102 | * Defines a custom Vue component to use for the internal field UI control. This should be an array with two |
| 103 | * keys: |
| 104 | * |
| 105 | * - plugin: the name of the plugin that the UI control exists in. |
| 106 | * - name: the name of the export for the component in the plugin's Vue UMD module. |
| 107 | * |
| 108 | * @var string[] |
| 109 | */ |
| 110 | public $customFieldComponent; |
| 111 | /** |
| 112 | * Name-value mapping of HTML attributes that will be added HTML form control, eg, |
| 113 | * `array('size' => 3)`. Attributes will be escaped before outputting. |
| 114 | * |
| 115 | * @var array |
| 116 | */ |
| 117 | public $uiControlAttributes = array(); |
| 118 | /** |
| 119 | * Makes field full width. |
| 120 | * Useful for `$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;` |
| 121 | * |
| 122 | * @var bool |
| 123 | */ |
| 124 | public $fullWidth = \false; |
| 125 | /** |
| 126 | * The list of all available values for this setting. If null, the setting can have any value. |
| 127 | * |
| 128 | * If supplied, this field should be an array mapping available values with their prettified |
| 129 | * display value. Eg, if set to `array('nb_visits' => 'Visits', 'nb_actions' => 'Actions')`, |
| 130 | * the UI will display **Visits** and **Actions**, and when the user selects one, Piwik will |
| 131 | * set the setting to **nb_visits** or **nb_actions** respectively. |
| 132 | * |
| 133 | * The setting value will be validated if this field is set. If the value is not one of the |
| 134 | * available values, an error will be triggered. |
| 135 | * |
| 136 | * _Note: If a custom validator is supplied (see {@link $validate}), the setting value will |
| 137 | * not be validated._ |
| 138 | * |
| 139 | * @var null|array |
| 140 | */ |
| 141 | public $availableValues = null; |
| 142 | /** |
| 143 | * Text that will appear above this setting's section in the _Plugin Settings_ admin page. |
| 144 | * |
| 145 | * @var null|string |
| 146 | */ |
| 147 | public $introduction = null; |
| 148 | /** |
| 149 | * Text that will appear directly underneath the setting title in the _Plugin Settings_ admin |
| 150 | * page. If set, should be a short description of the setting. |
| 151 | * |
| 152 | * @var null|string |
| 153 | */ |
| 154 | public $description = null; |
| 155 | /** |
| 156 | * Text that will appear next to the setting's section in the _Plugin Settings_ admin page. If set, |
| 157 | * it should contain information about the setting that is more specific than a general description, |
| 158 | * such as the format of the setting value if it has a special format. |
| 159 | * |
| 160 | * Be sure to escape any user input as HTML can be used here. |
| 161 | * |
| 162 | * @var null|string |
| 163 | */ |
| 164 | public $inlineHelp = null; |
| 165 | /** |
| 166 | * A closure that prepares the setting value. If supplied, this closure will be executed before |
| 167 | * the setting has been validated. |
| 168 | * |
| 169 | * **Example** |
| 170 | * |
| 171 | * $setting->prepare = function ($value, Setting $setting) { |
| 172 | * return mb_strtolower($value); |
| 173 | * } |
| 174 | * |
| 175 | * @var null|\Closure |
| 176 | */ |
| 177 | public $prepare = null; |
| 178 | /** |
| 179 | * A closure that does some custom validation on the setting before the setting is persisted. |
| 180 | * |
| 181 | * The closure should take two arguments: the setting value and the {@link Setting} instance being |
| 182 | * validated. If the value is found to be invalid, the closure should throw an exception with |
| 183 | * a message that describes the error. |
| 184 | * |
| 185 | * **Example** |
| 186 | * |
| 187 | * $setting->validate = function ($value, Setting $setting) { |
| 188 | * if ($value > 60) { |
| 189 | * throw new \Exception('The time limit is not allowed to be greater than 60 minutes.'); |
| 190 | * } |
| 191 | * } |
| 192 | * |
| 193 | * @var null|\Closure |
| 194 | */ |
| 195 | public $validate = null; |
| 196 | /** |
| 197 | * A closure that transforms the setting value. If supplied, this closure will be executed after |
| 198 | * the setting has been validated. |
| 199 | * |
| 200 | * _Note: If a transform is supplied, the setting's {@link $type} has no effect. This means the |
| 201 | * transformation function will be responsible for casting the setting value to the appropriate |
| 202 | * data type._ |
| 203 | * |
| 204 | * **Example** |
| 205 | * |
| 206 | * $setting->transform = function ($value, Setting $setting) { |
| 207 | * if ($value > 30) { |
| 208 | * $value = 30; |
| 209 | * } |
| 210 | * |
| 211 | * return (int) $value; |
| 212 | * } |
| 213 | * |
| 214 | * @var null|\Closure |
| 215 | */ |
| 216 | public $transform = null; |
| 217 | /** |
| 218 | * This setting's display name, for example, `'Refresh Interval'`. |
| 219 | * |
| 220 | * Be sure to escape any user input as HTML can be used here. |
| 221 | * |
| 222 | * @var string |
| 223 | */ |
| 224 | public $title = ''; |
| 225 | /** |
| 226 | * Here you can define conditions so that certain form fields will be only shown when a certain condition |
| 227 | * is true. This condition is supposed to be evaluated on the client side dynamically. This way you can hide |
| 228 | * for example some fields depending on another field. For example if SiteSearch is disabled, fields to enter |
| 229 | * site search keywords is not needed anymore and can be disabled. |
| 230 | * |
| 231 | * For example 'sitesearch', or 'sitesearch && !use_sitesearch_default' where 'sitesearch' and 'use_sitesearch_default' |
| 232 | * are both values of fields. |
| 233 | * |
| 234 | * @var string |
| 235 | */ |
| 236 | public $condition; |
| 237 | /** |
| 238 | * Here you can add one or multiple instances of `Piwik\Validators\BaseValidator` to avoid having to |
| 239 | * write the same validators over and over again in {@link $validate}. |
| 240 | * |
| 241 | * Examples |
| 242 | * Want to require a value to be set? |
| 243 | * $fieldConfig->validators[] = new Piwik\Validators\NotEmpty(); |
| 244 | * |
| 245 | * Want to require an email? |
| 246 | * $fieldConfig->validators[] = new Piwik\Validators\NotEmpty(); |
| 247 | * $fieldConfig->validators[] = new Piwik\Validators\Email(); |
| 248 | * |
| 249 | * The core comes with a set of various validators that can be used. |
| 250 | * |
| 251 | * @var BaseValidator[] |
| 252 | */ |
| 253 | public $validators = []; |
| 254 | } |
| 255 |