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