MeasurableProperty.php
6 years ago
MeasurableSetting.php
6 years ago
MeasurableSettings.php
6 years ago
MeasurableSettings.php
142 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Settings\Measurable; |
| 10 | |
| 11 | use Piwik\Db; |
| 12 | use Piwik\Piwik; |
| 13 | use Piwik\Settings\Settings; |
| 14 | use Piwik\Settings\Storage; |
| 15 | use Piwik\Site; |
| 16 | use Exception; |
| 17 | |
| 18 | /** |
| 19 | * Base class of all measurable settings providers. Plugins that define their own configuration settings |
| 20 | * can extend this class to easily make their measurable settings available to Piwik users. |
| 21 | * |
| 22 | * Descendants of this class should implement the {@link init()} method and call the |
| 23 | * {@link makeSetting()} method for each of the measurable's settings. |
| 24 | * |
| 25 | * For an example, see the {@link Piwik\Plugins\ExampleSettingsPlugin\MeasurableSettings} plugin. |
| 26 | * |
| 27 | * $settingsProvider = new Piwik\Plugin\SettingsProvider(); // get this instance via dependency injection |
| 28 | * $measurableSettings = $settingProvider->getMeasurableSettings($yourPluginName, $idsite, $idType = null); |
| 29 | * $measurableSettings->yourSetting->getValue(); |
| 30 | * |
| 31 | * @api |
| 32 | */ |
| 33 | abstract class MeasurableSettings extends Settings |
| 34 | { |
| 35 | /** |
| 36 | * @var int |
| 37 | */ |
| 38 | protected $idSite; |
| 39 | |
| 40 | /** |
| 41 | * @var string |
| 42 | */ |
| 43 | protected $idMeasurableType; |
| 44 | |
| 45 | /** |
| 46 | * Constructor. |
| 47 | * @param int $idSite If creating settings for a new site that is not created yet, use idSite = 0 |
| 48 | * @param string|null $idMeasurableType If null, idType will be detected from idSite |
| 49 | * @throws Exception |
| 50 | */ |
| 51 | public function __construct($idSite, $idMeasurableType = null) |
| 52 | { |
| 53 | parent::__construct(); |
| 54 | |
| 55 | $this->idSite = (int) $idSite; |
| 56 | |
| 57 | if (!empty($idMeasurableType)) { |
| 58 | $this->idMeasurableType = $idMeasurableType; |
| 59 | } elseif (!empty($idSite)) { |
| 60 | $this->idMeasurableType = Site::getTypeFor($idSite); |
| 61 | } else { |
| 62 | throw new Exception('No idType specified for ' . get_class($this)); |
| 63 | } |
| 64 | |
| 65 | $this->init(); |
| 66 | } |
| 67 | |
| 68 | protected function hasMeasurableType($typeId) |
| 69 | { |
| 70 | return $typeId === $this->idMeasurableType; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Creates a new measurable setting. |
| 75 | * |
| 76 | * Settings will be displayed in the UI depending on the order of `makeSetting` calls. This means you can define |
| 77 | * the order of the displayed settings by calling makeSetting first for more important settings. |
| 78 | * |
| 79 | * @param string $name The name of the setting that shall be created |
| 80 | * @param mixed $defaultValue The default value for this setting. Note the value will not be converted to the |
| 81 | * specified type. |
| 82 | * @param string $type The PHP internal type the value of this setting should have. |
| 83 | * Use one of FieldConfig::TYPE_* constancts |
| 84 | * @param \Closure $fieldConfigCallback A callback method to configure the field that shall be displayed in the |
| 85 | * UI to define the value for this setting |
| 86 | * @return MeasurableSetting Returns an instance of the created measurable setting. |
| 87 | * @throws Exception |
| 88 | */ |
| 89 | protected function makeSetting($name, $defaultValue, $type, $fieldConfigCallback) |
| 90 | { |
| 91 | $setting = new MeasurableSetting($name, $defaultValue, $type, $this->pluginName, $this->idSite); |
| 92 | $setting->setConfigureCallback($fieldConfigCallback); |
| 93 | |
| 94 | $this->addSetting($setting); |
| 95 | |
| 96 | return $setting; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @internal |
| 101 | * @param $name |
| 102 | * @param $defaultValue |
| 103 | * @param $type |
| 104 | * @param $configureCallback |
| 105 | * @return MeasurableProperty |
| 106 | * @throws Exception |
| 107 | */ |
| 108 | protected function makeProperty($name, $defaultValue, $type, $configureCallback) |
| 109 | { |
| 110 | $setting = new MeasurableProperty($name, $defaultValue, $type, $this->pluginName, $this->idSite); |
| 111 | $setting->setConfigureCallback($configureCallback); |
| 112 | |
| 113 | $this->addSetting($setting); |
| 114 | |
| 115 | return $setting; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Saves (persists) the current measurable setting values in the database. |
| 120 | * |
| 121 | * Will trigger an event to notify plugins that a value has been changed. |
| 122 | */ |
| 123 | public function save() |
| 124 | { |
| 125 | parent::save(); |
| 126 | |
| 127 | /** |
| 128 | * Triggered after a plugin settings have been updated. |
| 129 | * |
| 130 | * **Example** |
| 131 | * |
| 132 | * Piwik::addAction('MeasurableSettings.updated', function (MeasurableSettings $settings) { |
| 133 | * $value = $settings->someSetting->getValue(); |
| 134 | * // Do something with the new setting value |
| 135 | * }); |
| 136 | * |
| 137 | * @param Settings $settings The plugin settings object. |
| 138 | */ |
| 139 | Piwik::postEvent('MeasurableSettings.updated', array($this, $this->idSite)); |
| 140 | } |
| 141 | } |
| 142 |