diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2016-03-10 00:55:45 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2016-04-11 05:11:33 +0300 |
commit | b52ae4e7e488e0474d67c54578e1d6c1aa066bff (patch) | |
tree | f94b02f774cbc24faaa18f29ee1e19fef8b338af /plugins/ExampleSettingsPlugin | |
parent | 6ba622a68a26792af8cc22131f488f7ff5189d2c (diff) |
refs #7983 let plugins add or remove fields to websites and better settings api
Diffstat (limited to 'plugins/ExampleSettingsPlugin')
-rw-r--r-- | plugins/ExampleSettingsPlugin/MeasurableSettings.php | 65 | ||||
-rw-r--r-- | plugins/ExampleSettingsPlugin/Settings.php | 160 | ||||
-rw-r--r-- | plugins/ExampleSettingsPlugin/SystemSettings.php | 96 | ||||
-rw-r--r-- | plugins/ExampleSettingsPlugin/UserSettings.php | 80 |
4 files changed, 241 insertions, 160 deletions
diff --git a/plugins/ExampleSettingsPlugin/MeasurableSettings.php b/plugins/ExampleSettingsPlugin/MeasurableSettings.php new file mode 100644 index 0000000000..be1879750b --- /dev/null +++ b/plugins/ExampleSettingsPlugin/MeasurableSettings.php @@ -0,0 +1,65 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\ExampleSettingsPlugin; + +use Piwik\Plugins\MobileAppMeasurable\Type as MobileAppType; +use Piwik\Settings\Setting; +use Piwik\Settings\FieldConfig; + +/** + * Defines Settings for ExampleSettingsPlugin. + * + * Usage like this: + * // require Piwik\Plugin\SettingsProvider via Dependency Injection eg in constructor of your class + * $settings = $settingsProvider->getMeasurableSettings('ExampleSettingsPlugin', $idSite); + * $settings->appId->getValue(); + * $settings->contactEmails->getValue(); + */ +class MeasurableSettings extends \Piwik\Settings\Measurable\MeasurableSettings +{ + /** @var Setting|null */ + public $appId; + + /** @var Setting */ + public $contactEmails; + + protected function init() + { + if ($this->hasMeasurableType(MobileAppType::ID)) { + // this setting will be only shown for mobile apps + $this->appId = $this->makeAppIdSetting(); + } + + $this->contactEmails = $this->makeContactEmailsSetting(); + } + + private function makeAppIdSetting() + { + $defaultValue = ''; + $type = FieldConfig::TYPE_STRING; + + return $this->makeSetting('mobile_app_id', $defaultValue, $type, function (FieldConfig $field) { + $field->title = 'App ID'; + $field->inlineHelp = 'Enter the id of the mobile app eg "org.domain.example"'; + $field->uiControl = FieldConfig::UI_CONTROL_TEXT; + }); + } + + private function makeContactEmailsSetting() + { + $defaultValue = array(); + $type = FieldConfig::TYPE_ARRAY; + + return $this->makeSetting('contact_email', $defaultValue, $type, function (FieldConfig $field) { + $field->title = 'Contact email addresses'; + $field->uiControl = FieldConfig::UI_CONTROL_TEXTAREA; + }); + } + +} diff --git a/plugins/ExampleSettingsPlugin/Settings.php b/plugins/ExampleSettingsPlugin/Settings.php deleted file mode 100644 index 1b110d2871..0000000000 --- a/plugins/ExampleSettingsPlugin/Settings.php +++ /dev/null @@ -1,160 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ - -namespace Piwik\Plugins\ExampleSettingsPlugin; - -use Piwik\Settings\SystemSetting; -use Piwik\Settings\UserSetting; - -/** - * Defines Settings for ExampleSettingsPlugin. - * - * Usage like this: - * $settings = new Settings('ExampleSettingsPlugin'); - * $settings->autoRefresh->getValue(); - * $settings->metric->getValue(); - */ -class Settings extends \Piwik\Plugin\Settings -{ - /** @var UserSetting */ - public $autoRefresh; - - /** @var UserSetting */ - public $refreshInterval; - - /** @var UserSetting */ - public $color; - - /** @var SystemSetting */ - public $metric; - - /** @var SystemSetting */ - public $browsers; - - /** @var SystemSetting */ - public $description; - - /** @var SystemSetting */ - public $password; - - protected function init() - { - $this->setIntroduction('Here you can specify the settings for this plugin.'); - - // User setting --> checkbox converted to bool - $this->createAutoRefreshSetting(); - - // User setting --> textbox converted to int defining a validator and filter - $this->createRefreshIntervalSetting(); - - // User setting --> radio - $this->createColorSetting(); - - // System setting --> allows selection of a single value - $this->createMetricSetting(); - - // System setting --> allows selection of multiple values - $this->createBrowsersSetting(); - - // System setting --> textarea - $this->createDescriptionSetting(); - - // System setting --> textarea - $this->createPasswordSetting(); - } - - private function createAutoRefreshSetting() - { - $this->autoRefresh = new UserSetting('autoRefresh', 'Auto refresh'); - $this->autoRefresh->type = static::TYPE_BOOL; - $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); - } - - private function createRefreshIntervalSetting() - { - $this->refreshInterval = new UserSetting('refreshInterval', 'Refresh Interval'); - $this->refreshInterval->type = static::TYPE_INT; - $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'; - $this->refreshInterval->validate = function ($value, $setting) { - if ($value < 15) { - throw new \Exception('Value is invalid'); - } - }; - - $this->addSetting($this->refreshInterval); - } - - private function createColorSetting() - { - $this->color = new UserSetting('color', 'Color'); - $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); - } - - private function createMetricSetting() - { - $this->metric = new SystemSetting('metric', 'Metric to display'); - $this->metric->type = static::TYPE_STRING; - $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->metric->readableByCurrentUser = true; - - $this->addSetting($this->metric); - } - - private function createBrowsersSetting() - { - $this->browsers = new SystemSetting('browsers', 'Supported Browsers'); - $this->browsers->type = static::TYPE_ARRAY; - $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->browsers->readableByCurrentUser = true; - - $this->addSetting($this->browsers); - } - - private function createDescriptionSetting() - { - $this->description = new SystemSetting('description', 'Description for value'); - $this->description->readableByCurrentUser = true; - $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->readableByCurrentUser = true; - $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); - } -} diff --git a/plugins/ExampleSettingsPlugin/SystemSettings.php b/plugins/ExampleSettingsPlugin/SystemSettings.php new file mode 100644 index 0000000000..1d2082e949 --- /dev/null +++ b/plugins/ExampleSettingsPlugin/SystemSettings.php @@ -0,0 +1,96 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\ExampleSettingsPlugin; + +use Piwik\Settings\Setting; +use Piwik\Settings\FieldConfig; + +/** + * Defines Settings for ExampleSettingsPlugin. + * + * Usage like this: + * $settings = new SystemSettings(); + * $settings->metric->getValue(); + * $settings->description->getValue(); + */ +class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings +{ + /** @var Setting */ + public $metric; + + /** @var Setting */ + public $browsers; + + /** @var Setting */ + public $description; + + /** @var Setting */ + public $password; + + protected function init() + { + // System setting --> allows selection of a single value + $this->metric = $this->createMetricSetting(); + + // System setting --> allows selection of multiple values + $this->browsers = $this->createBrowsersSetting(); + + // System setting --> textarea + $this->description = $this->createDescriptionSetting(); + + // System setting --> textarea + $this->password = $this->createPasswordSetting(); + } + + private function createMetricSetting() + { + return $this->makeSetting('metric', $default = 'nb_visits', FieldConfig::TYPE_STRING, function (FieldConfig $field) { + $field->title = 'Metric to display'; + $field->uiControl = FieldConfig::UI_CONTROL_SINGLE_SELECT; + $field->availableValues = array('nb_visits' => 'Visits', 'nb_actions' => 'Actions', 'visitors' => 'Visitors'); + $field->introduction = 'Only Super Users can change the following settings:'; + $field->description = 'Choose the metric that should be displayed in the browser tab'; + }); + } + + private function createBrowsersSetting() + { + $default = array('firefox', 'chromium', 'safari'); + + return $this->makeSetting('browsers', $default, FieldConfig::TYPE_ARRAY, function (FieldConfig $field) { + $field->title = 'Supported Browsers'; + $field->uiControl = FieldConfig::UI_CONTROL_MULTI_SELECT; + $field->availableValues = array('firefox' => 'Firefox', 'chromium' => 'Chromium', 'safari' => 'safari'); + $field->description = 'The value will be only displayed in the following browsers'; + }); + } + + private function createDescriptionSetting() + { + $default = "This is the value: \nAnother line"; + + return $this->makeSetting('description', $default, FieldConfig::TYPE_STRING, function (FieldConfig $field) { + $field->title = 'Description for value'; + $field->uiControl = FieldConfig::UI_CONTROL_TEXTAREA; + $field->description = 'This description will be displayed next to the value'; + }); + } + + private function createPasswordSetting() + { + return $this->makeSetting('password', $default = null, FieldConfig::TYPE_STRING, function (FieldConfig $field) { + $field->title = 'API password'; + $field->uiControl = FieldConfig::UI_CONTROL_PASSWORD; + $field->description = 'Password for the 3rd API where we fetch the value'; + $field->transform = function ($value) { + return sha1($value . 'salt'); + }; + }); + } +} diff --git a/plugins/ExampleSettingsPlugin/UserSettings.php b/plugins/ExampleSettingsPlugin/UserSettings.php new file mode 100644 index 0000000000..cffa88d16d --- /dev/null +++ b/plugins/ExampleSettingsPlugin/UserSettings.php @@ -0,0 +1,80 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\ExampleSettingsPlugin; + +use Piwik\Settings\Setting; +use Piwik\Settings\FieldConfig; + +/** + * Defines Settings for ExampleSettingsPlugin. + * + * Usage like this: + * $settings = new UserSettings(); + * $settings->autoRefresh->getValue(); + * $settings->color->getValue(); + */ +class UserSettings extends \Piwik\Settings\Plugin\UserSettings +{ + /** @var Setting */ + public $autoRefresh; + + /** @var Setting */ + public $refreshInterval; + + /** @var Setting */ + public $color; + + protected function init() + { + // User setting --> checkbox converted to bool + $this->autoRefresh = $this->createAutoRefreshSetting(); + + // User setting --> textbox converted to int defining a validator and filter + $this->refreshInterval = $this->createRefreshIntervalSetting(); + + // User setting --> radio + $this->color = $this->createColorSetting(); + } + + private function createAutoRefreshSetting() + { + return $this->makeSetting('autoRefresh', $default = false, FieldConfig::TYPE_BOOL, function (FieldConfig $field) { + $field->title = 'Auto refresh'; + $field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX; + $field->description = 'If enabled, the value will be automatically refreshed depending on the specified interval'; + }); + } + + private function createRefreshIntervalSetting() + { + return $this->makeSetting('refreshInterval', $default = '30', FieldConfig::TYPE_INT, function (FieldConfig $field) { + $field->title = 'Refresh Interval'; + $field->uiControl = FieldConfig::UI_CONTROL_TEXT; + $field->uiControlAttributes = array('size' => 3); + $field->description = 'Defines how often the value should be updated'; + $field->inlineHelp = 'Enter a number which is >= 15'; + $field->validate = function ($value, $setting) { + if ($value < 15) { + throw new \Exception('Value is invalid'); + } + }; + }); + } + + private function createColorSetting() + { + return $this->makeSetting('color', $default = 'red', FieldConfig::TYPE_STRING, function (FieldConfig $field) { + $field->title = 'Color'; + $field->uiControl = FieldConfig::UI_CONTROL_RADIO; + $field->description = 'Pick your favourite color'; + $field->availableValues = array('red' => 'Red', 'blue' => 'Blue', 'green' => 'Green'); + }); + } + +} |