FieldConfig
2 years ago
Measurable
2 years ago
Plugin
2 years ago
Storage
2 years ago
FieldConfig.php
2 years ago
Setting.php
2 years ago
Settings.php
2 years ago
Settings.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\Settings; |
| 11 | |
| 12 | /** |
| 13 | * Base class of all settings providers. |
| 14 | * |
| 15 | * @api |
| 16 | */ |
| 17 | abstract class Settings |
| 18 | { |
| 19 | /** |
| 20 | * An array containing all available settings: Array ( [setting-name] => [setting] ) |
| 21 | * |
| 22 | * @var Setting[] |
| 23 | */ |
| 24 | private $settings = array(); |
| 25 | protected $pluginName; |
| 26 | /** |
| 27 | * By default the plugin name is shown in the UI when managing plugin settings. However, you can overwrite |
| 28 | * the displayed title by specifying a title. |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $title = ''; |
| 32 | public function __construct() |
| 33 | { |
| 34 | if (!isset($this->pluginName)) { |
| 35 | $classname = get_class($this); |
| 36 | $parts = explode('\\', $classname); |
| 37 | if (count($parts) >= 3) { |
| 38 | $this->pluginName = $parts[2]; |
| 39 | } else { |
| 40 | throw new \Exception(sprintf('Plugin Settings must have a plugin name specified in %s, could not detect plugin name', $classname)); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | public function getTitle() |
| 45 | { |
| 46 | if (!empty($this->title)) { |
| 47 | return $this->title; |
| 48 | } |
| 49 | return $this->pluginName; |
| 50 | } |
| 51 | /** |
| 52 | * @ignore |
| 53 | */ |
| 54 | public function getPluginName() |
| 55 | { |
| 56 | return $this->pluginName; |
| 57 | } |
| 58 | /** |
| 59 | * @ignore |
| 60 | * @return Setting |
| 61 | */ |
| 62 | public function getSetting($name) |
| 63 | { |
| 64 | if (array_key_exists($name, $this->settings)) { |
| 65 | return $this->settings[$name]; |
| 66 | } |
| 67 | } |
| 68 | /** |
| 69 | * Implemented by descendants. This method should define plugin settings (via the |
| 70 | * {@link addSetting()}) method and set the introduction text (via the |
| 71 | * {@link setIntroduction()}). |
| 72 | */ |
| 73 | protected abstract function init(); |
| 74 | /** |
| 75 | * Returns the settings that can be displayed for the current user. |
| 76 | * |
| 77 | * @return Setting[] |
| 78 | */ |
| 79 | public function getSettingsWritableByCurrentUser() |
| 80 | { |
| 81 | return array_filter($this->settings, function (\Piwik\Settings\Setting $setting) { |
| 82 | return $setting->isWritableByCurrentUser(); |
| 83 | }); |
| 84 | } |
| 85 | /** |
| 86 | * Adds a new setting to the settings container. |
| 87 | * |
| 88 | * @param Setting $setting |
| 89 | * @throws \Exception If there is a setting with the same name that already exists. |
| 90 | * If the name contains non-alphanumeric characters. |
| 91 | */ |
| 92 | public function addSetting(\Piwik\Settings\Setting $setting) |
| 93 | { |
| 94 | $name = $setting->getName(); |
| 95 | if (isset($this->settings[$name])) { |
| 96 | throw new \Exception(sprintf('A setting with name "%s" does already exist for plugin "%s"', $name, $this->pluginName)); |
| 97 | } |
| 98 | $this->settings[$name] = $setting; |
| 99 | } |
| 100 | /** |
| 101 | * Saves (persists) the current setting values in the database. |
| 102 | */ |
| 103 | public function save() |
| 104 | { |
| 105 | foreach ($this->settings as $setting) { |
| 106 | $setting->save(); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 |