From 52e8a0d868069c090997aaa134ecc51ef275b4c2 Mon Sep 17 00:00:00 2001
From: Thomas Steur
Date: Thu, 24 Oct 2013 01:43:51 +0000
Subject: refs #4126 added more example, handle type array and field multi
select correct, code cleanup
---
core/Plugin/Settings.php | 39 ++++++++++++++---
core/Settings/Setting.php | 17 +++-----
core/Settings/UserSetting.php | 4 +-
plugins/CoreAdminHome/Controller.php | 7 ++--
.../CoreAdminHome/javascripts/pluginSettings.js | 9 +++-
.../CoreAdminHome/templates/pluginSettings.twig | 43 +++++++++++--------
plugins/ExampleSettingsPlugin/Settings.php | 49 ++++++++++++++++++++--
7 files changed, 124 insertions(+), 44 deletions(-)
diff --git a/core/Plugin/Settings.php b/core/Plugin/Settings.php
index 50f82fc8f7..56ab232b49 100644
--- a/core/Plugin/Settings.php
+++ b/core/Plugin/Settings.php
@@ -33,9 +33,17 @@ class Settings
const FIELD_SINGLE_SELECT = 'select';
/**
+ * An array containing all available settings: Array ( [setting-name] => [setting] )
+ *
* @var Settings[]
*/
private $settings = array();
+
+ /**
+ * Array containing all plugin settings values: Array( [setting-key] => [setting-value] ).
+ *
+ * @var array
+ */
private $settingsValues = array();
private $introduction;
@@ -51,7 +59,7 @@ class Settings
protected function init()
{
- // define your settings here
+ // Define your settings and introduction here.
}
/**
@@ -164,7 +172,7 @@ class Settings
*
* @param Setting $setting
*/
- public function removeValue(Setting $setting)
+ public function removeSettingValue(Setting $setting)
{
$key = $setting->getKey();
@@ -173,10 +181,22 @@ class Settings
}
}
+ /**
+ * Adds a new setting.
+ *
+ * @param Setting $setting
+ * @throws \Exception In case a setting having the same name already exists.
+ * In case the name contains non-alnum characters.
+ */
protected function addSetting(Setting $setting)
{
+ if (!ctype_alnum($setting->getName())) {
+ $msg = sprintf('The setting name "%s" in plugin "%s" is not valid. Only alpha and numerical characters are allowed', $setting->getName(), $this->pluginName);
+ throw new \Exception($msg);
+ }
+
if (array_key_exists($setting->getName(), $this->settings)) {
- throw new \Exception(sprintf('A setting with name %s does already exist', $setting->getName()));
+ throw new \Exception(sprintf('A setting with name "%s" does already exist for plugin "%s"', $setting->getName(), $this->pluginName));
}
if (!is_null($setting->field) && is_null($setting->type)) {
@@ -188,8 +208,16 @@ class Settings
if (is_null($setting->validate) && !is_null($setting->fieldOptions)) {
$pluginName = $this->pluginName;
$setting->validate = function ($value) use ($setting, $pluginName) {
- if (!array_key_exists($value, $setting->fieldOptions)) {
- throw new \Exception(sprintf('The selected value for field "%s" and plugin "%s" is not allowed.', $setting->getTitle(), $pluginName));
+ if (is_array($value) && $setting->type == Settings::TYPE_ARRAY) {
+ foreach ($value as $val) {
+ if (!array_key_exists($val, $setting->fieldOptions)) {
+ throw new \Exception(sprintf('The selected value for field "%s" in plugin "%s" is not allowed.', $setting->title, $pluginName));
+ }
+ }
+ } else {
+ if (!array_key_exists($value, $setting->fieldOptions)) {
+ throw new \Exception(sprintf('The selected value for field "%s" in plugin "%s" is not allowed.', $setting->title, $pluginName));
+ }
}
};
}
@@ -216,7 +244,6 @@ class Settings
$setting = $this->getSetting($name);
if (empty($setting)) {
- // TODO escape $name? or is it automatically escaped?
throw new \Exception(sprintf('The setting %s does not exist', $name));
}
diff --git a/core/Settings/Setting.php b/core/Settings/Setting.php
index 43bbd49149..00efc9d876 100644
--- a/core/Settings/Setting.php
+++ b/core/Settings/Setting.php
@@ -128,26 +128,14 @@ abstract class Setting
protected $name;
protected $displayedForCurrentUser = false;
- public function canBeDisplayedForCurrentUser()
- {
- return $this->displayedForCurrentUser;
- }
-
/**
* Creates a new setting.
*
* @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`
- *
- * @throws \Exception In case the name contains invalid characters.
*/
public function __construct($name, $title)
{
- if (!ctype_alnum($name)) {
- $msg = sprintf('The setting name %s is not valid. Only alpha and numerical characters are allowed', $name);
- throw new \Exception($msg);
- }
-
$this->key = $name;
$this->name = $name;
$this->title = $title;
@@ -158,6 +146,11 @@ abstract class Setting
return $this->name;
}
+ public function canBeDisplayedForCurrentUser()
+ {
+ return $this->displayedForCurrentUser;
+ }
+
/**
* Returns the key under which property name the setting will be stored.
*
diff --git a/core/Settings/UserSetting.php b/core/Settings/UserSetting.php
index c207e2e805..c7eb7eeb26 100644
--- a/core/Settings/UserSetting.php
+++ b/core/Settings/UserSetting.php
@@ -37,7 +37,7 @@ class UserSetting extends Setting
$this->setUserLogin($userLogin);
- $this->displayedForCurrentUser = !Piwik::isUserIsAnonymous();
+ $this->displayedForCurrentUser = !Piwik::isUserIsAnonymous() && Piwik::isUserHasSomeViewAccess();
}
private function buildUserSettingName($name, $userLogin = null)
@@ -94,7 +94,7 @@ class UserSetting extends Setting
if ($setting instanceof UserSetting) {
$setting->setUserLogin($userLogin);
- $pluginSettings->removeValue($setting);
+ $pluginSettings->removeSettingValue($setting);
}
}
diff --git a/plugins/CoreAdminHome/Controller.php b/plugins/CoreAdminHome/Controller.php
index 21aeaa4cc8..2e5cf517b5 100644
--- a/plugins/CoreAdminHome/Controller.php
+++ b/plugins/CoreAdminHome/Controller.php
@@ -99,12 +99,12 @@ class Controller extends \Piwik\Plugin\ControllerAdmin
{
Piwik::checkUserIsNotAnonymous();
- $view = new View('@CoreAdminHome/pluginSettings');
-
$settings = SettingsManager::getPluginSettingsForCurrentUser();
ksort($settings);
+
+ $view = new View('@CoreAdminHome/pluginSettings');
$view->pluginSettings = $settings;
- $view->nonce = Nonce::getNonce(static::SET_PLUGIN_SETTINGS_NONCE);
+ $view->nonce = Nonce::getNonce(static::SET_PLUGIN_SETTINGS_NONCE);
$this->setBasicVariablesView($view);
@@ -152,6 +152,7 @@ class Controller extends \Piwik\Plugin\ControllerAdmin
}
} catch (Exception $e) {
+ // TODO escape message
echo json_encode(array('result' => 'error', 'message' => $e->getMessage()));
return;
}
diff --git a/plugins/CoreAdminHome/javascripts/pluginSettings.js b/plugins/CoreAdminHome/javascripts/pluginSettings.js
index 1146fb918f..084ef0cd38 100644
--- a/plugins/CoreAdminHome/javascripts/pluginSettings.js
+++ b/plugins/CoreAdminHome/javascripts/pluginSettings.js
@@ -47,7 +47,14 @@ $(document).ready(function () {
$pluginSection = $(pluginSection);
var pluginName = $pluginSection.attr('data-pluginname');
- var serialized = $('input, textarea, select', $pluginSection).serializeArray();
+ var serialized = $('input, textarea, select:not([multiple])', $pluginSection).serializeArray();
+
+ // by default, it does not generate an array
+ var $multiSelects = $('select[multiple]', $pluginSection);
+ $multiSelects.each(function (index, multiSelect) {
+ var name = $(multiSelect).attr('name');
+ serialized.push({name: name, value: $(multiSelect).val()});
+ });
// by default, values of unchecked checkboxes are not send
var $uncheckedNodes = $('input[type=checkbox]:not(:checked)', $pluginSection);
diff --git a/plugins/CoreAdminHome/templates/pluginSettings.twig b/plugins/CoreAdminHome/templates/pluginSettings.twig
index 4737acfb5b..f6d724b418 100644
--- a/plugins/CoreAdminHome/templates/pluginSettings.twig
+++ b/plugins/CoreAdminHome/templates/pluginSettings.twig
@@ -24,7 +24,7 @@
{% endif %}
-
+
{% for setting in settings.getSettingsForCurrentUser %}
{% set settingValue = settings.getSettingValue(setting) %}
@@ -61,36 +61,40 @@
{% for key, value in setting.fieldOptions %}
{% endfor %}
{% elseif setting.field == 'textarea' %}
-
{% else %}
{% endif %}
@@ -98,7 +102,12 @@
{% if setting.defaultValue and setting.field != 'checkbox' %}
- {{ 'General_Default'|translate }} {{ setting.defaultValue }}
+ {{ 'General_Default'|translate }}
+ {% if setting.defaultValue is iterable %}
+ {{ setting.defaultValue|join(', ')|truncate(50) }}
+ {% else %}
+ {{ setting.defaultValue|truncate(50) }}
+ {% endif %}
{% endif %}
diff --git a/plugins/ExampleSettingsPlugin/Settings.php b/plugins/ExampleSettingsPlugin/Settings.php
index 400b74184d..89afa86b37 100644
--- a/plugins/ExampleSettingsPlugin/Settings.php
+++ b/plugins/ExampleSettingsPlugin/Settings.php
@@ -32,8 +32,17 @@ class Settings extends PluginSettings
// User setting --> textbox converted to int defining a validator and filter
$this->addSetting($this->getRefreshIntervalSetting());
- // System setting --> allows selection of a value
+ // System setting --> allows selection of a single value
$this->addSetting($this->getMetricSetting());
+
+ // System setting --> allows selection of multiple values
+ $this->addSetting($this->getBrowsersSetting());
+
+ // System setting --> textarea
+ $this->addSetting($this->getDescriptionSetting());
+
+ // System setting --> textarea
+ $this->addSetting($this->getPasswordSetting());
}
public function isAutoRefreshEnabled()
@@ -93,10 +102,44 @@ class Settings extends PluginSettings
$metric->type = static::TYPE_STRING;
$metric->field = static::FIELD_SINGLE_SELECT;
$metric->fieldOptions = array('nb_visits' => 'Visits', 'nb_actions' => 'Actions', 'visitors' => 'Visitors');
- $metric->introduction = 'Only super users can change this setting.';
- $metric->description = Piwik::translate('LiveTab_MetricDescription');
+ $metric->introduction = 'Only super users can change the following settings:';
+ $metric->description = 'Choose the metric that should be displayed in the browser tab';
$metric->defaultValue = 'nb_visits';
return $metric;
}
+
+ private function getBrowsersSetting()
+ {
+ $browsers = new SystemSetting('browsers', 'Supported Browsers');
+ $browsers->type = static::TYPE_ARRAY;
+ $browsers->field = static::FIELD_MULTI_SELECT;
+ $browsers->fieldOptions = array('firefox' => 'Firefox', 'chromium' => 'Chromium', 'safari' => 'safari');
+ $browsers->description = 'The value will be only displayed in the following browsers';
+ $browsers->defaultValue = array('firefox', 'chromium', 'safari');
+
+ return $browsers;
+ }
+
+ private function getDescriptionSetting()
+ {
+ $description = new SystemSetting('description', 'Description for value');
+ $description->field = static::FIELD_TEXTAREA;
+ $description->description = 'This description will be displayed next to the value';
+ $description->defaultValue = "This is the value: \nAnother line";
+
+ return $description;
+ }
+
+ private function getPasswordSetting()
+ {
+ $description = new SystemSetting('password', 'API password');
+ $description->field = static::FIELD_PASSWORD;
+ $description->description = 'Password for the 3rd API where we fetch the value';
+ $description->filter = function ($value) {
+ return sha1($value . 'salt');
+ };
+
+ return $description;
+ }
}
--
cgit v1.2.3