| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Settings; |
|
|
| use Piwik\Piwik; |
| use Piwik\Settings\Storage\Storage; |
| use Exception; |
| use Piwik\Validators\BaseValidator; |
|
|
| |
| |
| |
| |
| |
| class Setting |
| { |
| |
| |
| |
| |
| protected $name; |
|
|
| |
| |
| |
| |
| protected $hasWritePermission = null; |
|
|
| |
| |
| |
| protected $storage; |
|
|
| |
| |
| |
| protected $pluginName; |
|
|
| |
| |
| |
| protected $config; |
|
|
| |
| |
| |
| protected $configureCallback; |
|
|
| |
| |
| |
| protected $defaultValue; |
|
|
| |
| |
| |
| protected $type; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function __construct($name, $defaultValue, $type, $pluginName) |
| { |
| if (!ctype_alnum(str_replace('_', '', $name))) { |
| $msg = sprintf('The setting name "%s" in plugin "%s" is invalid. Only underscores, alpha and numerical characters are allowed', $name, $pluginName); |
| throw new Exception($msg); |
| } |
|
|
| $this->name = $name; |
| $this->type = $type; |
| $this->pluginName = $pluginName; |
| $this->setDefaultValue($defaultValue); |
| } |
|
|
| |
| |
| |
| |
| public function getName() |
| { |
| return $this->name; |
| } |
|
|
| |
| |
| |
| |
| public function getType() |
| { |
| return $this->type; |
| } |
|
|
| |
| |
| |
| |
| |
| public function setConfigureCallback($callback) |
| { |
| $this->configureCallback = $callback; |
| $this->config = null; |
| } |
|
|
| |
| |
| |
| public function getDefaultValue() |
| { |
| return $this->defaultValue; |
| } |
|
|
| |
| |
| |
| |
| public function setDefaultValue($defaultValue) |
| { |
| $this->defaultValue = $defaultValue; |
| } |
|
|
| |
| |
| |
| public function setStorage(Storage $storage) |
| { |
| $this->storage = $storage; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function configureField() |
| { |
| if (!$this->config) { |
| $this->config = new FieldConfig(); |
|
|
| if ($this->configureCallback) { |
| call_user_func($this->configureCallback, $this->config); |
| } |
|
|
| $this->setUiControlIfNeeded($this->config); |
| $this->checkType($this->config); |
| } |
|
|
| return $this->config; |
| } |
|
|
| |
| |
| |
| |
| |
| public function setIsWritableByCurrentUser($isWritable) |
| { |
| $this->hasWritePermission = (bool) $isWritable; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function isWritableByCurrentUser() |
| { |
| return (bool) $this->hasWritePermission; |
| } |
|
|
| |
| |
| |
| public function save() |
| { |
| $this->storage->save(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getValue() |
| { |
| return $this->storage->getValue($this->name, $this->defaultValue, $this->type); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setValue($value) |
| { |
| $this->checkHasEnoughWritePermission(); |
|
|
| $config = $this->configureField(); |
|
|
| if ($config->prepare && $config->prepare instanceof \Closure) { |
| $value = call_user_func($config->prepare, $value, $this); |
| } |
|
|
| $this->validateValue($value); |
|
|
| if ($config->transform && $config->transform instanceof \Closure) { |
| $value = call_user_func($config->transform, $value, $this); |
| } |
|
|
| if (isset($this->type) && !is_null($value)) { |
| settype($value, $this->type); |
| } |
|
|
| $this->storage->setValue($this->name, $value); |
| } |
|
|
| private function validateValue($value) |
| { |
| $config = $this->configureField(); |
|
|
| if (!empty($config->validators)) { |
| BaseValidator::check($config->title, $value, $config->validators); |
| } |
|
|
| if ($config->validate && $config->validate instanceof \Closure) { |
| call_user_func($config->validate, $value, $this); |
| } elseif (is_array($config->availableValues)) { |
| if (is_bool($value) && $value) { |
| $value = '1'; |
| } elseif (is_bool($value)) { |
| $value = '0'; |
| } |
|
|
| |
| $errorMsg = Piwik::translate( |
| 'CoreAdminHome_PluginSettingsValueNotAllowed', |
| array(strip_tags($config->title), $this->pluginName) |
| ); |
|
|
| if (is_array($value) && $this->type === FieldConfig::TYPE_ARRAY) { |
| foreach ($value as $val) { |
| if (!array_key_exists($val, $config->availableValues)) { |
| throw new \Exception($errorMsg); |
| } |
| } |
| } else { |
| if (!array_key_exists($value, $config->availableValues)) { |
| throw new \Exception($errorMsg); |
| } |
| } |
| } elseif ($this->type === FieldConfig::TYPE_INT || $this->type === FieldConfig::TYPE_FLOAT) { |
| if (!is_numeric($value)) { |
| $errorMsg = Piwik::translate( |
| 'CoreAdminHome_PluginSettingsValueNotAllowed', |
| array(strip_tags($config->title), $this->pluginName) |
| ); |
| throw new \Exception($errorMsg); |
| } |
| } elseif ($this->type === FieldConfig::TYPE_BOOL) { |
| if (!in_array($value, array(true, false, '0', '1', 0, 1), true)) { |
| $errorMsg = Piwik::translate( |
| 'CoreAdminHome_PluginSettingsValueNotAllowed', |
| array(strip_tags($config->title), $this->pluginName) |
| ); |
| throw new \Exception($errorMsg); |
| } |
| } |
| } |
|
|
| |
| |
| |
| private function checkHasEnoughWritePermission() |
| { |
| if (!$this->isWritableByCurrentUser()) { |
| $errorMsg = Piwik::translate('CoreAdminHome_PluginSettingChangeNotAllowed', array($this->name, $this->pluginName)); |
| throw new \Exception($errorMsg); |
| } |
| } |
|
|
| private function setUiControlIfNeeded(FieldConfig $field) |
| { |
| if (!isset($field->uiControl)) { |
| $defaultControlTypes = array( |
| FieldConfig::TYPE_INT => FieldConfig::UI_CONTROL_TEXT, |
| FieldConfig::TYPE_FLOAT => FieldConfig::UI_CONTROL_TEXT, |
| FieldConfig::TYPE_STRING => FieldConfig::UI_CONTROL_TEXT, |
| FieldConfig::TYPE_BOOL => FieldConfig::UI_CONTROL_CHECKBOX, |
| FieldConfig::TYPE_ARRAY => FieldConfig::UI_CONTROL_MULTI_SELECT, |
| ); |
|
|
| if (isset($defaultControlTypes[$this->type])) { |
| $field->uiControl = $defaultControlTypes[$this->type]; |
| } else { |
| $field->uiControl = FieldConfig::UI_CONTROL_TEXT; |
| } |
| } |
| } |
|
|
| private function checkType(FieldConfig $field) |
| { |
| if ( |
| $field->uiControl === FieldConfig::UI_CONTROL_MULTI_SELECT && |
| $this->type !== FieldConfig::TYPE_ARRAY |
| ) { |
| throw new Exception('Type must be an array when using a multi select'); |
| } |
|
|
| if ( |
| $field->uiControl === FieldConfig::UI_CONTROL_MULTI_TUPLE && |
| $this->type !== FieldConfig::TYPE_ARRAY |
| ) { |
| throw new Exception('Type must be an array when using a multi pair'); |
| } |
|
|
| $types = array( |
| FieldConfig::TYPE_INT, |
| FieldConfig::TYPE_FLOAT, |
| FieldConfig::TYPE_STRING, |
| FieldConfig::TYPE_BOOL, |
| FieldConfig::TYPE_ARRAY, |
| ); |
|
|
| if (!in_array($this->type, $types)) { |
| throw new Exception('Type does not exist'); |
| } |
| } |
| } |
|
|