diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2013-10-24 04:31:25 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2013-10-24 04:31:25 +0400 |
commit | 0785060693c83e5e78a9a532e53de8fe5ed22fc0 (patch) | |
tree | c6bd6d6dcc859ecdcb2f15d5903f3d88b972464a /plugins/ExampleSettingsPlugin | |
parent | 48e9a1b3f0c0f8bd825deb4ce8da87a5d7897025 (diff) |
refs #4126 added example plugin for settings
Diffstat (limited to 'plugins/ExampleSettingsPlugin')
-rw-r--r-- | plugins/ExampleSettingsPlugin/Settings.php | 102 | ||||
-rw-r--r-- | plugins/ExampleSettingsPlugin/plugin.json | 6 |
2 files changed, 108 insertions, 0 deletions
diff --git a/plugins/ExampleSettingsPlugin/Settings.php b/plugins/ExampleSettingsPlugin/Settings.php new file mode 100644 index 0000000000..400b74184d --- /dev/null +++ b/plugins/ExampleSettingsPlugin/Settings.php @@ -0,0 +1,102 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik_Plugins + * @package ExampleSettingsPlugin + */ +namespace Piwik\Plugins\ExampleSettingsPlugin; + +use Piwik\Piwik; +use Piwik\Plugin\Settings as PluginSettings; +use Piwik\Settings\SystemSetting; +use Piwik\Settings\UserSetting; + +/** + * Settings + * + * @package ExampleSettingsPlugin + */ +class Settings extends PluginSettings +{ + protected function init() + { + $this->setIntroduction('Here you can specify the settings for this plugin.'); + + // User setting --> checkbox converted to bool + $this->addSetting($this->getAutoRefreshSetting()); + + // User setting --> textbox converted to int defining a validator and filter + $this->addSetting($this->getRefreshIntervalSetting()); + + // System setting --> allows selection of a value + $this->addSetting($this->getMetricSetting()); + } + + public function isAutoRefreshEnabled() + { + return $this->getSettingValue($this->getAutoRefreshSetting()); + } + + public function getRefreshInterval() + { + return $this->getSettingValue($this->getRefreshIntervalSetting()); + } + + public function getMetric() + { + return $this->getSettingValue($this->getMetricSetting()); + } + + private function getAutoRefreshSetting() + { + $autoRefresh = new UserSetting('autoRefresh', 'Auto refresh'); + $autoRefresh->type = static::TYPE_BOOL; + $autoRefresh->field = static::FIELD_CHECKBOX; + $autoRefresh->description = 'If enabled, the value will be automatically refreshed depending on the specified interval'; + $autoRefresh->defaultValue = false; + + return $autoRefresh; + } + + private function getRefreshIntervalSetting() + { + $refreshInterval = new UserSetting('refreshInterval', 'Refresh Interval'); + $refreshInterval->type = static::TYPE_INT; + $refreshInterval->field = static::FIELD_TEXT; + $refreshInterval->fieldAttributes = array('size' => 3); + $refreshInterval->description = 'Defines how often the value should be updated'; + $refreshInterval->inlineHelp = 'Enter a number which is >= 15'; + $refreshInterval->defaultValue = '30'; + $refreshInterval->validate = function ($value, $setting) { + if ($value < 15) { + throw new \Exception('Value is invalid'); + } + }; + $refreshInterval->filter = function ($value, $setting) { + if ($value > 30) { + $value = 30; + } + + return $value; + }; + + return $refreshInterval; + } + + private function getMetricSetting() + { + $metric = new SystemSetting('metric', 'Metric to display'); + $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->defaultValue = 'nb_visits'; + + return $metric; + } +} diff --git a/plugins/ExampleSettingsPlugin/plugin.json b/plugins/ExampleSettingsPlugin/plugin.json new file mode 100644 index 0000000000..ca450fd2f4 --- /dev/null +++ b/plugins/ExampleSettingsPlugin/plugin.json @@ -0,0 +1,6 @@ +{ + "name": "ExampleSettingsPlugin", + "version": "0.1.0", + "description": "Plugin to demonstrate how to define and how to access plugin settings", + "theme": false +}
\ No newline at end of file |