diff --git a/.travis.yml b/.travis.yml index 6e8bed24f425ad6b4c059c37437d7861a1d6e319..63273458541695c9f95cc750a1ede64cce3692ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,8 @@ env: - TEST_SUITE=IntegrationTests - TEST_SUITE=PluginTests - TEST_SUITE=CoreTests + - MYSQL_ADAPTER=PDO_MYSQL + - MYSQL_ADAPTER=MYSQLI global: - secure: "AMhZmPZx4SUcuZRBzGHlQPxzM4D8FvFB3UThDa52gbi9KIBrwcumzV2VGi6B\n5fgjwtB4XTE1In7qhY2HMikPWBmWYYOQ5QcMPJsqqHt4iMmahx8WKzne6NOk\nNpqAuje/fulNGeP2LJZi0nrub3Fh4VwXaOvpNloKNQN/2JuqPtM=" - secure: "DySde80fX3dw0osGY5s5lGiGLjMleUkkONsuRDC4xjT1hAQP94FGHpPXVZV8\nAQY0s8MQmcK2eRLOQrmE9Bh1XXbGqh40s66XhSWq0hulFHxQH5OLaZXccFeu\n5WxGkHXnNDIc+CVhXW/IIbHZPO1K6hffS8fuvP1o6mYgxwFL6Xw=" diff --git a/core/Period.php b/core/Period.php index a0b28ecc6fb8dac5a6e48a82ba2041272cac3982..749d88c1142c4b3ca10d49e4a8c37ac16b254e00 100644 --- a/core/Period.php +++ b/core/Period.php @@ -78,7 +78,7 @@ abstract class Period * * Note: This method cannot create Range periods. * - * @param string $strPeriod `"day"`, `"week"`, `"month"`, `"year"`, `"range"` + * @param string $strPeriod `"day"`, `"week"`, `"month"`, `"year"`, `"range"`. * @param Date|string $date A date within the period or the range of dates. * @throws Exception If `$strPeriod` is invalid. * @return \Piwik\Period @@ -334,4 +334,4 @@ abstract class Period { return $this->getDateStart()->toString("Y-m-d") . "," . $this->getDateEnd()->toString("Y-m-d"); } -} \ No newline at end of file +} diff --git a/core/Plugin/Settings.php b/core/Plugin/Settings.php index 8b1ad09365a0a5619948e8932df3c1b873d11175..625512f9eb93c7ae5d59ab5e79ab9832fb55b648 100644 --- a/core/Plugin/Settings.php +++ b/core/Plugin/Settings.php @@ -16,8 +16,14 @@ use Piwik\Settings\Setting; use Piwik\Settings\StorageInterface; /** - * Settings class that plugins can extend in order to create settings for their plugins. - * + * Base class of all Settings providers. Plugins that define their own settings can extend + * this class to easily make their settings available to Piwik users. + * + * Descendants of this class should implement the [init](#init) method and call the + * [addSetting](#addSetting) method for each of the plugin's settings. + * + * For an example, see the [ExampleSettingsPlugin](#) plugin. + * * @package Piwik\Plugin * @api */ @@ -29,13 +35,13 @@ abstract class Settings implements StorageInterface const TYPE_BOOL = 'boolean'; const TYPE_ARRAY = 'array'; - const FIELD_RADIO = 'radio'; - const FIELD_TEXT = 'text'; - const FIELD_TEXTAREA = 'textarea'; - const FIELD_CHECKBOX = 'checkbox'; - const FIELD_PASSWORD = 'password'; - const FIELD_MULTI_SELECT = 'multiselect'; - const FIELD_SINGLE_SELECT = 'select'; + const CONTROL_RADIO = 'radio'; + const CONTROL_TEXT = 'text'; + const CONTROL_TEXTAREA = 'textarea'; + const CONTROL_CHECKBOX = 'checkbox'; + const CONTROL_PASSWORD = 'password'; + const CONTROL_MULTI_SELECT = 'multiselect'; + const CONTROL_SINGLE_SELECT = 'select'; /** * An array containing all available settings: Array ( [setting-name] => [setting] ) @@ -54,6 +60,11 @@ abstract class Settings implements StorageInterface private $introduction; private $pluginName; + /** + * Constructor. + * + * @param string $pluginName The name of the plugin these settings are for. + */ public function __construct($pluginName) { $this->pluginName = $pluginName; @@ -63,12 +74,14 @@ abstract class Settings implements StorageInterface } /** - * Define your settings and introduction here. + * Implemented by descendants. This method should define plugin settings (via the + * [addSetting](#addSetting)) method and set the introduction text (via the + * [setIntroduction](#setIntroduction)). */ abstract protected function init(); /** - * Sets (overwrites) the plugin settings introduction. + * Sets the text used to introduce this plugin's settings in the _Plugin Settings_ page. * * @param string $introduction */ @@ -77,14 +90,18 @@ abstract class Settings implements StorageInterface $this->introduction = $introduction; } + /** + * Returns the introduction text for this plugin's settings. + * + * @return string + */ public function getIntroduction() { return $this->introduction; } /** - * Returns only settings that can be displayed for current user. For instance a regular user won't see get - * any settings that require super user permissions. + * Returns the settings that can be displayed for the current user. * * @return Setting[] */ @@ -117,7 +134,8 @@ abstract class Settings implements StorageInterface } /** - * Get all available settings without checking any permissions. + * Returns all available settings. This will include settings that are not available + * to the current user (such as settings available only to the Super User). * * @return Setting[] */ @@ -135,7 +153,8 @@ abstract class Settings implements StorageInterface } /** - * Removes all settings for this plugin. Useful for instance while uninstalling the plugin. + * Removes all settings for this plugin from the database. Useful when uninstalling + * a plugin. */ public function removeAllPluginSettings() { @@ -146,13 +165,12 @@ abstract class Settings implements StorageInterface } /** - * Gets the current value for this setting. If no value is specified, the default value will be returned. + * Returns the current value for a setting. If no value is stored, the default value + * is be returned. * * @param Setting $setting - * * @return mixed - * - * @throws \Exception In case the setting does not exist or if the current user is not allowed to change the value + * @throws \Exception If the setting does not exist or if the current user is not allowed to change the value * of this setting. */ public function getSettingValue(Setting $setting) @@ -168,14 +186,16 @@ abstract class Settings implements StorageInterface } /** - * Sets (overwrites) the value for the given setting. Make sure to call `save()` afterwards, otherwise the change - * has no effect. Before the value is saved a possibly define `validate` closure and `filter` closure will be - * called. Alternatively the value will be casted to the specfied setting type. + * Sets (overwrites) the value of a setting in memory. To persist the change, [save](#save) must be + * called afterwards, otherwise the change has no effect. + * + * Before the setting is changed, the [Setting::validate](#) and [Setting::transform](#) closures + * will be invoked (if defined). If there is no validation filter, the setting value will be casted + * to the appropriate data type. * * @param Setting $setting * @param string $value - * - * @throws \Exception In case the setting does not exist or if the current user is not allowed to change the value + * @throws \Exception If the setting does not exist or if the current user is not allowed to change the value * of this setting. */ public function setSettingValue(Setting $setting, $value) @@ -186,8 +206,8 @@ abstract class Settings implements StorageInterface call_user_func($setting->validate, $value, $setting); } - if ($setting->filter && $setting->filter instanceof \Closure) { - $value = call_user_func($setting->filter, $value, $setting); + if ($setting->transform && $setting->transform instanceof \Closure) { + $value = call_user_func($setting->transform, $value, $setting); } elseif (isset($setting->type)) { settype($value, $setting->type); } @@ -196,8 +216,8 @@ abstract class Settings implements StorageInterface } /** - * Removes the value for the given setting. Make sure to call `save()` afterwards, otherwise the removal has no - * effect. + * Unsets a setting value in memory. To persist the change, [save](#save) must be + * called afterwards, otherwise the change has no effect. * * @param Setting $setting */ @@ -213,11 +233,11 @@ abstract class Settings implements StorageInterface } /** - * Adds a new setting. + * Makes a new plugin setting available. * * @param Setting $setting - * @throws \Exception In case a setting having the same name already exists. - * In case the name contains non-alnum characters. + * @throws \Exception If there is a setting with the same name that already exists. + * If the name contains non-alphanumeric characters. */ protected function addSetting(Setting $setting) { @@ -274,32 +294,32 @@ abstract class Settings implements StorageInterface } } - private function getDefaultType($field) + private function getDefaultType($controlType) { $defaultTypes = array( - static::FIELD_TEXT => static::TYPE_STRING, - static::FIELD_TEXTAREA => static::TYPE_STRING, - static::FIELD_PASSWORD => static::TYPE_STRING, - static::FIELD_CHECKBOX => static::TYPE_BOOL, - static::FIELD_MULTI_SELECT => static::TYPE_ARRAY, - static::FIELD_RADIO => static::TYPE_STRING, - static::FIELD_SINGLE_SELECT => static::TYPE_STRING, + static::CONTROL_TEXT => static::TYPE_STRING, + static::CONTROL_TEXTAREA => static::TYPE_STRING, + static::CONTROL_PASSWORD => static::TYPE_STRING, + static::CONTROL_CHECKBOX => static::TYPE_BOOL, + static::CONTROL_MULTI_SELECT => static::TYPE_ARRAY, + static::CONTROL_RADIO => static::TYPE_STRING, + static::CONTROL_SINGLE_SELECT => static::TYPE_STRING, ); - return $defaultTypes[$field]; + return $defaultTypes[$controlType]; } - private function getDefaultField($type) + private function getDefaultCONTROL($type) { - $defaultFields = array( - static::TYPE_INT => static::FIELD_TEXT, - static::TYPE_FLOAT => static::FIELD_TEXT, - static::TYPE_STRING => static::FIELD_TEXT, - static::TYPE_BOOL => static::FIELD_CHECKBOX, - static::TYPE_ARRAY => static::FIELD_MULTI_SELECT, + $defaultControlTypes = array( + static::TYPE_INT => static::CONTROL_TEXT, + static::TYPE_FLOAT => static::CONTROL_TEXT, + static::TYPE_STRING => static::CONTROL_TEXT, + static::TYPE_BOOL => static::CONTROL_CHECKBOX, + static::TYPE_ARRAY => static::CONTROL_MULTI_SELECT, ); - return $defaultFields[$type]; + return $defaultControlTypes[$type]; } /** @@ -316,19 +336,19 @@ abstract class Settings implements StorageInterface private function setDefaultTypeAndFieldIfNeeded(Setting $setting) { - if (!is_null($setting->field) && is_null($setting->type)) { - $setting->type = $this->getDefaultType($setting->field); - } elseif (!is_null($setting->type) && is_null($setting->field)) { - $setting->field = $this->getDefaultField($setting->type); - } elseif (is_null($setting->field) && is_null($setting->type)) { + if (!is_null($setting->uiControlType) && is_null($setting->type)) { + $setting->type = $this->getDefaultType($setting->uiControlType); + } elseif (!is_null($setting->type) && is_null($setting->uiControlType)) { + $setting->uiControlType = $this->getDefaultCONTROL($setting->type); + } elseif (is_null($setting->uiControlType) && is_null($setting->type)) { $setting->type = static::TYPE_STRING; - $setting->field = static::FIELD_TEXT; + $setting->uiControlType = static::CONTROL_TEXT; } } private function addValidatorIfNeeded(Setting $setting) { - if (!is_null($setting->validate) || is_null($setting->fieldOptions)) { + if (!is_null($setting->validate) || is_null($setting->availableValues)) { return; } @@ -341,15 +361,15 @@ abstract class Settings implements StorageInterface if (is_array($value) && $setting->type == Settings::TYPE_ARRAY) { foreach ($value as $val) { - if (!array_key_exists($val, $setting->fieldOptions)) { + if (!array_key_exists($val, $setting->availableValues)) { throw new \Exception($errorMsg); } } } else { - if (!array_key_exists($value, $setting->fieldOptions)) { + if (!array_key_exists($value, $setting->availableValues)) { throw new \Exception($errorMsg); } } }; } -} +} \ No newline at end of file diff --git a/core/Settings/Setting.php b/core/Settings/Setting.php index bb06efd9ab2252089622427fd26c35b142d9aeee..8a72f393712f592e8d9405c7bb50db0d3bba696f 100644 --- a/core/Settings/Setting.php +++ b/core/Settings/Setting.php @@ -12,73 +12,96 @@ namespace Piwik\Settings; /** - * Base setting class. Extend this class to define your own type of setting. + * Base setting type class. * * @package Piwik * @subpackage Settings + * @api */ abstract class Setting { /** - * Defines the PHP type of the setting. Before the value is saved it will be cased depending on this setting. + * Describes the setting's PHP data type. When saved, setting values will always be casted to this + * type. + * + * See [Settings](#) for a list of supported data types. * * @var string */ public $type = null; /** - * Defines which field type should be displayed on the setting page. + * Describes how the setting should be manipulated through Piwik's UI. * + * See [Settings](#) for a list of supportted control types. + * * @var string */ - public $field = null; + public $uiControlType = null; /** - * An array of field attributes that will be added as HTML attributes to the HTML form field. - * Example: `array('size' => 3)`. Please note the attributes will be escaped for security. + * Name-value mapping of HTML attributes that will be added HTML form control, eg, + * `array('size' => 3)`. Attributes will be escaped before outputting. + * * @var array */ - public $fieldAttributes = array(); - - /** - * Defines available options in case you want to give the user the possibility to select a value (in a select - * field). For instance `array('nb_visits' => 'Visits', 'nb_actions' => 'Actions')`. - * In case field options are set and you do not specify a validator, a validator will be automatically added - * to check the set value is one of the defined array keys. An error will be triggered in case a user tries to - * save a value that is not allowed. + public $uiControlAttributes = array(); + + /** + * The list of all available values for this setting. If null, the setting can have any value. + * + * If supplied, this field should be an array mapping available values with their prettified + * display value. Eg, if set to `array('nb_visits' => 'Visits', 'nb_actions' => 'Actions')`, + * the UI will display **Visits** and **Actions**, and when the user selects one, Piwik will + * set the setting to **nb_visits** or **nb_actions** respectively. + * + * The setting value will be validated if this field is set. If the value is not one of the + * available values, an error will be triggered. + * + * _Note: If a custom validator is supplied (see [validate](#validate)), the setting value will + * not be validated._ * * @var null|array */ - public $fieldOptions = null; + public $availableValues = null; /** - * Defines an introduction that will be displayed as a text block above the setting. + * Text that will appear above this setting's section in the _Plugin Settings_ admin page. + * * @var null|string */ public $introduction = null; /** - * Defines a description that will be displayed underneath the setting title. It should be just a short description - * what the setting is about. + * Text that will be appear directly underneath the setting title in the _Plugin Settings_ admin + * page. If set, should be a short description of the setting. + * * @var null|string */ public $description = null; /** - * The inline help will be displayed in a separate help box next to the setting and can contain some further - * explanations about the setting. For instance if only some specific characters are allowed to can explain them - * here. + * Text that will appear next to the setting's section in the _Plugin Settings_ admin page. If set, + * it should contain information about the setting that is more specific than a general description, + * such as the format of the setting value if it has a special format. + * * @var null|string */ public $inlineHelp = null; /** - * If a closure is set, the closure will be executed before a new value is saved. In case a user tries to save an - * invalid value just throw an exception containing a useful message. Example: + * A closure that does some custom validation on the setting before the setting is persisted. + * + * The closure should take two arguments: the setting value and the [Setting](#) instance being + * validated. If the value is found to be invalid, the closure should throw an exception with + * a message that describes the error. + * + * **Example** + * * ``` - * function ($value, Setting $setting) { + * $setting->validate = function ($value, Setting $setting) { * if ($value > 60) { - * throw new \Exception('Value has to be at <= 60 as an hour has only 60 minutes'); + * throw new \Exception('The time limit is not allowed to be greater than 60 minutes.'); * } * } * ``` @@ -88,12 +111,17 @@ abstract class Setting public $validate = null; /** - * You can define a filter closure that is executed after a value is validated. In case you define a value, the - * property `$type` has no effect. That means the value won't be casted to the specified type. It is on your own to - * cast or format the value on your needs. Make sure to return a value at the end of the function as this value - * will be saved. Example: + * A closure that transforms the setting value. If supplied, this closure will be executed after + * the setting has been validated. + * + * _Note: If a transform is supplied, the setting's [type](#type) has no effect. This means the + * transformation function will be responsible for casting the setting value to the appropriate + * data type._ + * + * **Example** + * * ``` - * function ($value, Setting $setting) { + * $setting->transform = function ($value, Setting $setting) { * if ($value > 30) { * $value = 30; * } @@ -104,18 +132,20 @@ abstract class Setting * * @var null|\Closure */ - public $filter = null; + public $transform = null; /** - * Defines the default value for this setting that will be used in case the user has not specified a value so far. - * The default value won't be casted so make sure to define an appropriate value. + * Default value of this setting. + * + * The default value is not casted to the appropriate data type. This means _**you**_ have to make + * sure the value is of the correct type. * * @var mixed */ public $defaultValue = null; /** - * Defines the title of the setting which will be visible to the user. For instance `Refresh Interval` + * This setting's display name, for example, `'Refresh Interval'`. * * @var string */ @@ -131,10 +161,11 @@ abstract class Setting private $storage; /** - * Creates a new setting. + * Constructor. * - * @param string $name The name of the setting, only alnum characters are allowed. For instance `refreshInterval` - * @param string $title The title of the setting which will be visible to the user. For instance `Refresh Interval` + * @param string $name The setting's persisted name. Only alphanumeric characters are allowed, eg, + * `'refreshInterval'`. + * @param string $title The setting's display name, eg, `'Refresh Interval'`. */ public function __construct($name, $title) { @@ -143,23 +174,42 @@ abstract class Setting $this->title = $title; } + /** + * Returns the setting's persisted name, eg, `'refreshInterval'`. + * + * @return string + */ public function getName() { return $this->name; } + /** + * Returns true if this setting can be displayed for the current user, false if otherwise. + * + * @return bool + */ public function canBeDisplayedForCurrentUser() { return $this->displayedForCurrentUser; } + /** + * Sets the object used to persist settings. + * + * @return StorageInterface + */ public function setStorage(StorageInterface $storage) { $this->storage = $storage; } /** - * @see StorageInterface::getSettingValue + * Returns the previously persisted setting value. If no value was set, the default value + * is returned. + * + * @return mixed + * @throws \Exception If the current user is not allowed to change the value of this setting. */ public function getValue() { @@ -167,7 +217,10 @@ abstract class Setting } /** - * @see StorageInterface::setSettingValue + * Sets and persists this setting's value overwriting any existing value. + * + * @param mixed $value + * @throws \Exception If the current user is not allowed to change the value of this setting. */ public function setValue($value) { @@ -175,7 +228,7 @@ abstract class Setting } /** - * Returns the key under which property name the setting will be stored. + * Returns the unique string key used to store this setting. * * @return string */ @@ -185,11 +238,12 @@ abstract class Setting } /** - * Determine the order for displaying. The lower the order, the earlier the setting will be displayed. + * Returns the display order. The lower the return value, the earlier the setting will be displayed. + * * @return int */ public function getOrder() { return 100; } -} +} \ No newline at end of file diff --git a/core/Settings/StorageInterface.php b/core/Settings/StorageInterface.php index c80a3757288df6f5e97ca05413e2df89bf947afb..86048f17875b1c49b40a1ae3557730594f50678c 100644 --- a/core/Settings/StorageInterface.php +++ b/core/Settings/StorageInterface.php @@ -7,6 +7,9 @@ */ namespace Piwik\Settings; +/** + * Base type of all Setting storage implementations. + */ interface StorageInterface { /** diff --git a/core/Settings/SystemSetting.php b/core/Settings/SystemSetting.php index 1c77f1caaa0a7bce1a38b9313cf5d9cb403d2c68..e96f1a1fb0cf2ae5d61936f90b2c79ffa9251f62 100644 --- a/core/Settings/SystemSetting.php +++ b/core/Settings/SystemSetting.php @@ -14,8 +14,8 @@ namespace Piwik\Settings; use Piwik\Piwik; /** - * System wide setting. Only the super user can change this kind of settings and the value of the setting effects all - * users. + * Describes a system wide setting. Only the super user can change this type of setting and + * the value of this setting will affect all users. * * @package Piwik * @subpackage Settings @@ -24,6 +24,12 @@ use Piwik\Piwik; */ class SystemSetting extends Setting { + /** + * Constructor. + * + * @param string $name The persisted name of the setting. + * @param string $title The display name of the setting. + */ public function __construct($name, $title) { parent::__construct($name, $title); @@ -31,8 +37,13 @@ class SystemSetting extends Setting $this->displayedForCurrentUser = Piwik::isUserIsSuperUser(); } + /** + * Returns the display order. User settings are displayed after system settings. + * + * @return int + */ public function getOrder() { return 30; } -} +} \ No newline at end of file diff --git a/core/Settings/UserSetting.php b/core/Settings/UserSetting.php index b83ed16afb1057f0f0ad0dd65dcb3dd029884af2..7512f693c4231302cf8c830e94d89ea9b0a1aee5 100644 --- a/core/Settings/UserSetting.php +++ b/core/Settings/UserSetting.php @@ -14,8 +14,8 @@ use Piwik\Common; use Piwik\Piwik; /** - * Per user setting. Each user will be able to change this setting but each user can set a different value. That means - * a changed value does not effect any other users. + * Describes a per user setting. Each user will be able to change this setting but each user + * can set a different value. Changes from one user will not affect other users. * * @package Piwik * @subpackage Settings @@ -27,9 +27,11 @@ class UserSetting extends Setting private $userLogin = null; /** - * @param string $name - * @param string $title - * @param null|string $userLogin Defaults to the current user login. + * Constructor. + * + * @param string $name The setting's persisted name. + * @param string $title The setting's display name. + * @param null|string $userLogin The user this setting applies to. Will default to the current user login. */ public function __construct($name, $title, $userLogin = null) { @@ -40,6 +42,11 @@ class UserSetting extends Setting $this->displayedForCurrentUser = !Piwik::isUserIsAnonymous() && Piwik::isUserHasSomeViewAccess(); } + /** + * Returns the display order. User settings are displayed after system settings. + * + * @return int + */ public function getOrder() { return 60; @@ -65,11 +72,11 @@ class UserSetting extends Setting } /** - * Sets (overwrites) the userLogin. + * Sets the name of the user this setting will be set for. * * @param $userLogin - * - * @throws \Exception In case you set a userLogin that is not your userLogin and you are not the superUser. + * @throws \Exception If the current user does not have permission to set the setting value + * of `$userLogin`. */ public function setUserLogin($userLogin) { @@ -82,12 +89,11 @@ class UserSetting extends Setting } /** - * Remove all stored settings of the given userLogin. This is important to cleanup all settings for a user once he - * is deleted. Otherwise a user could register with the same name afterwards and see the previous user's settings. + * Unsets all settings for a user. The settings will be removed from the database. Used when + * a user is deleted. * * @param string $userLogin - * - * @throws \Exception In case the userLogin is empty. + * @throws \Exception If the `$userLogin` is empty. */ public static function removeAllUserSettingsForUser($userLogin) { @@ -113,5 +119,4 @@ class UserSetting extends Setting $pluginSettings->save(); } } - -} +} \ No newline at end of file diff --git a/plugins/CoreAdminHome/templates/pluginSettings.twig b/plugins/CoreAdminHome/templates/pluginSettings.twig index c42676c35c9e1fbdd916c210d95d95dac84b5497..8d47453bdec2ca5bd5bb7ceb8262b8d88eabca2d 100644 --- a/plugins/CoreAdminHome/templates/pluginSettings.twig +++ b/plugins/CoreAdminHome/templates/pluginSettings.twig @@ -60,15 +60,15 @@ <td class="columnField"> <fieldset> <label> - {% if setting.field == 'select' or setting.field == 'multiselect' %} + {% if setting.uiControlType == 'select' or setting.uiControlType == 'multiselect' %} <select - {% for attr, val in setting.fieldAttributes %} + {% for attr, val in setting.uiControlAttributes %} {{ attr|e('html_attr') }}="{{ val|e('html_attr') }}" {% endfor %} name="{{ setting.getKey|e('html_attr') }}" - {% if setting.field == 'multiselect' %}multiple{% endif %}> + {% if setting.uiControlType == 'multiselect' %}multiple{% endif %}> - {% for key, value in setting.fieldOptions %} + {% for key, value in setting.availableValues %} <option value='{{ key }}' {% if settingValue is iterable and key in settingValue %} selected='selected' @@ -80,21 +80,21 @@ {% endfor %} </select> - {% elseif setting.field == 'textarea' %} + {% elseif setting.uiControlType == 'textarea' %} <textarea style="width: 176px;" - {% for attr, val in setting.fieldAttributes %} + {% for attr, val in setting.uiControlAttributes %} {{ attr|e('html_attr') }}="{{ val|e('html_attr') }}" {% endfor %} name="{{ setting.getKey|e('html_attr') }}" > {{- settingValue -}} </textarea> - {% elseif setting.field == 'radio' %} + {% elseif setting.uiControlType == 'radio' %} - {% for key, value in setting.fieldOptions %} + {% for key, value in setting.availableValues %} <input - {% for attr, val in setting.fieldAttributes %} + {% for attr, val in setting.uiControlAttributes %} {{ attr|e('html_attr') }}="{{ val|e('html_attr') }}" {% endfor %} {% if settingValue==key %} @@ -112,23 +112,23 @@ {% else %} <input - {% for attr, val in setting.fieldAttributes %} + {% for attr, val in setting.uiControlAttributes %} {{ attr|e('html_attr') }}="{{ val|e('html_attr') }}" {% endfor %} - {% if setting.field == 'checkbox' %} + {% if setting.uiControlType == 'checkbox' %} value="1" {% endif %} - {% if setting.field == 'checkbox' and settingValue %} + {% if setting.uiControlType == 'checkbox' and settingValue %} checked="checked" {% endif %} - type="{{ setting.field|e('html_attr') }}" + type="{{ setting.uiControlType|e('html_attr') }}" name="{{ setting.getKey|e('html_attr') }}" value="{{ settingValue|e('html_attr') }}" > {% endif %} - {% if setting.defaultValue and setting.field != 'checkbox' %} + {% if setting.defaultValue and setting.uiControlType != 'checkbox' %} <br/> <span class='form-description'> {{ 'General_Default'|translate }} diff --git a/plugins/ExampleSettingsPlugin/Settings.php b/plugins/ExampleSettingsPlugin/Settings.php index 140dc477cd296955f31a756fc392123212ed650f..a25c61351890d43c4feb13065e8ec6fb4e3120d6 100644 --- a/plugins/ExampleSettingsPlugin/Settings.php +++ b/plugins/ExampleSettingsPlugin/Settings.php @@ -76,9 +76,9 @@ class Settings extends \Piwik\Plugin\Settings { $this->autoRefresh = new UserSetting('autoRefresh', 'Auto refresh'); $this->autoRefresh->type = static::TYPE_BOOL; - $this->autoRefresh->field = static::FIELD_CHECKBOX; - $this->autoRefresh->description = 'If enabled, the value will be automatically refreshed depending on the specified interval'; - $this->autoRefresh->defaultValue = false; + $this->autoRefresh->uiControlType = static::CONTROL_CHECKBOX; + $this->autoRefresh->description = 'If enabled, the value will be automatically refreshed depending on the specified interval'; + $this->autoRefresh->defaultValue = false; $this->addSetting($this->autoRefresh); } @@ -87,8 +87,8 @@ class Settings extends \Piwik\Plugin\Settings { $this->refreshInterval = new UserSetting('refreshInterval', 'Refresh Interval'); $this->refreshInterval->type = static::TYPE_INT; - $this->refreshInterval->field = static::FIELD_TEXT; - $this->refreshInterval->fieldAttributes = array('size' => 3); + $this->refreshInterval->uiControlType = static::CONTROL_TEXT; + $this->refreshInterval->uiControlAttributes = array('size' => 3); $this->refreshInterval->description = 'Defines how often the value should be updated'; $this->refreshInterval->inlineHelp = 'Enter a number which is >= 15'; $this->refreshInterval->defaultValue = '30'; @@ -104,9 +104,9 @@ class Settings extends \Piwik\Plugin\Settings private function createColorSetting() { $this->color = new UserSetting('color', 'Color'); - $this->color->field = static::FIELD_RADIO; - $this->color->description = 'Pick your favourite color'; - $this->color->fieldOptions = array('red' => 'Red', 'blue' => 'Blue', 'green' => 'Green'); + $this->color->uiControlType = static::CONTROL_RADIO; + $this->color->description = 'Pick your favourite color'; + $this->color->availableValues = array('red' => 'Red', 'blue' => 'Blue', 'green' => 'Green'); $this->addSetting($this->color); } @@ -115,11 +115,11 @@ class Settings extends \Piwik\Plugin\Settings { $this->metric = new SystemSetting('metric', 'Metric to display'); $this->metric->type = static::TYPE_STRING; - $this->metric->field = static::FIELD_SINGLE_SELECT; - $this->metric->fieldOptions = array('nb_visits' => 'Visits', 'nb_actions' => 'Actions', 'visitors' => 'Visitors'); - $this->metric->introduction = 'Only super users can change the following settings:'; - $this->metric->description = 'Choose the metric that should be displayed in the browser tab'; - $this->metric->defaultValue = 'nb_visits'; + $this->metric->uiControlType = static::CONTROL_SINGLE_SELECT; + $this->metric->availableValues = array('nb_visits' => 'Visits', 'nb_actions' => 'Actions', 'visitors' => 'Visitors'); + $this->metric->introduction = 'Only super users can change the following settings:'; + $this->metric->description = 'Choose the metric that should be displayed in the browser tab'; + $this->metric->defaultValue = 'nb_visits'; $this->addSetting($this->metric); } @@ -128,33 +128,33 @@ class Settings extends \Piwik\Plugin\Settings { $this->browsers = new SystemSetting('browsers', 'Supported Browsers'); $this->browsers->type = static::TYPE_ARRAY; - $this->browsers->field = static::FIELD_MULTI_SELECT; - $this->browsers->fieldOptions = array('firefox' => 'Firefox', 'chromium' => 'Chromium', 'safari' => 'safari'); - $this->browsers->description = 'The value will be only displayed in the following browsers'; - $this->browsers->defaultValue = array('firefox', 'chromium', 'safari'); + $this->browsers->uiControlType = static::CONTROL_MULTI_SELECT; + $this->browsers->availableValues = array('firefox' => 'Firefox', 'chromium' => 'Chromium', 'safari' => 'safari'); + $this->browsers->description = 'The value will be only displayed in the following browsers'; + $this->browsers->defaultValue = array('firefox', 'chromium', 'safari'); $this->addSetting($this->browsers); } private function createDescriptionSetting() { - $this->description = new SystemSetting('description', 'Description for value'); - $this->description->field = static::FIELD_TEXTAREA; - $this->description->description = 'This description will be displayed next to the value'; - $this->description->defaultValue = "This is the value: \nAnother line"; + $this->description = new SystemSetting('description', 'Description for value'); + $this->description->uiControlType = static::CONTROL_TEXTAREA; + $this->description->description = 'This description will be displayed next to the value'; + $this->description->defaultValue = "This is the value: \nAnother line"; $this->addSetting($this->description); } private function createPasswordSetting() { - $this->password = new SystemSetting('password', 'API password'); - $this->password->field = static::FIELD_PASSWORD; - $this->password->description = 'Password for the 3rd API where we fetch the value'; - $this->password->filter = function ($value) { + $this->password = new SystemSetting('password', 'API password'); + $this->password->uiControlType = static::CONTROL_PASSWORD; + $this->password->description = 'Password for the 3rd API where we fetch the value'; + $this->password->transform = function ($value) { return sha1($value . 'salt'); }; $this->addSetting($this->password); } -} +} \ No newline at end of file diff --git a/tests/PHPUnit/Core/Plugin/SettingsTest.php b/tests/PHPUnit/Core/Plugin/SettingsTest.php index 9756e5d9f13c35c657f1c6f2e4de8058bdaa2974..e56801ef2792858019edac8d23a7555d5f07b845 100644 --- a/tests/PHPUnit/Core/Plugin/SettingsTest.php +++ b/tests/PHPUnit/Core/Plugin/SettingsTest.php @@ -76,7 +76,7 @@ class SettingsTest extends DatabaseTestCase public function test_addSetting_shouldAssignDefaultType_IfFieldIsGivenButNoType() { $setting = $this->buildUserSetting('myname', 'mytitle'); - $setting->field = TestablePluginSettings::FIELD_MULTI_SELECT; + $setting->uiControlType = TestablePluginSettings::CONTROL_MULTI_SELECT; $this->settings->addSetting($setting); @@ -90,13 +90,13 @@ class SettingsTest extends DatabaseTestCase $this->settings->addSetting($setting); - $this->assertEquals(TestablePluginSettings::FIELD_MULTI_SELECT, $setting->field); + $this->assertEquals(TestablePluginSettings::CONTROL_MULTI_SELECT, $setting->uiControlType); } public function test_addSetting_shouldAddAValidator_IfFieldOptionsAreGiven() { $setting = $this->buildUserSetting('myname', 'mytitle'); - $setting->fieldOptions = array('allowedval' => 'DisplayName', 'allowedval2' => 'Name 2'); + $setting->availableValues = array('allowedval' => 'DisplayName', 'allowedval2' => 'Name 2'); $this->settings->addSetting($setting); @@ -203,7 +203,7 @@ class SettingsTest extends DatabaseTestCase { $this->setUser(); $setting = $this->buildUserSetting('mysystem', 'mytitle'); - $setting->fieldOptions = array('allowed' => 'text', 'allowed2' => 'text2'); + $setting->availableValues = array('allowed' => 'text', 'allowed2' => 'text2'); $this->settings->addSetting($setting); @@ -218,8 +218,8 @@ class SettingsTest extends DatabaseTestCase { $this->setUser(); $setting = $this->buildUserSetting('mysystem', 'mytitle'); - $setting->fieldOptions = array('allowed' => 'text', 'allowed2' => 'text2'); - $setting->field = PluginSettings::FIELD_MULTI_SELECT; + $setting->availableValues = array('allowed' => 'text', 'allowed2' => 'text2'); + $setting->uiControlType = PluginSettings::CONTROL_MULTI_SELECT; $this->settings->addSetting($setting); @@ -230,8 +230,8 @@ class SettingsTest extends DatabaseTestCase { $this->setUser(); $setting = $this->buildUserSetting('mysystem', 'mytitle'); - $setting->fieldOptions = array('allowed' => 'text', 'allowed2' => 'text2'); - $setting->field = PluginSettings::FIELD_MULTI_SELECT; + $setting->availableValues = array('allowed' => 'text', 'allowed2' => 'text2'); + $setting->uiControlType = PluginSettings::CONTROL_MULTI_SELECT; $this->settings->addSetting($setting); @@ -282,7 +282,7 @@ class SettingsTest extends DatabaseTestCase $setting->type = PluginSettings::TYPE_INT; $self = $this; - $setting->filter = function ($value, $userSetting) use ($self, $setting) { + $setting->transform = function ($value, $userSetting) use ($self, $setting) { $self->assertEquals('31xm42', $value); $self->assertEquals($setting, $userSetting); diff --git a/tests/travis/prepare.sh b/tests/travis/prepare.sh index 93bed0daf7514e312e6e52ce21b190ec0a687868..edac96a92edc41d3d8b7faae2fa8d6805a29220b 100755 --- a/tests/travis/prepare.sh +++ b/tests/travis/prepare.sh @@ -13,7 +13,7 @@ fi # Copy Piwik configuration echo "Install config.ini.php" -cp ./tests/PHPUnit/config.ini.travis.php ./config/config.ini.php +sed "s/PDO_MYSQL/${MYSQL_ADAPTER}/g" ./tests/PHPUnit/config.ini.travis.php > ./config/config.ini.php # Prepare phpunit.xml echo "Adjusting phpunit.xml"