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