Changeset View
Changeset View
Standalone View
Standalone View
src/applications/settings/storage/PhabricatorUserPreferences.php
| <?php | <?php | ||||
| final class PhabricatorUserPreferences | final class PhabricatorUserPreferences | ||||
| extends PhabricatorUserDAO | extends PhabricatorUserDAO | ||||
| implements | implements | ||||
| PhabricatorPolicyInterface, | PhabricatorPolicyInterface, | ||||
| PhabricatorDestructibleInterface, | PhabricatorDestructibleInterface, | ||||
| PhabricatorApplicationTransactionInterface { | PhabricatorApplicationTransactionInterface { | ||||
| const BUILTIN_GLOBAL_DEFAULT = 'global'; | |||||
| protected $userPHID; | protected $userPHID; | ||||
| protected $preferences = array(); | protected $preferences = array(); | ||||
| protected $builtinKey; | |||||
| private $user = self::ATTACHABLE; | private $user = self::ATTACHABLE; | ||||
| private $defaultSettings; | |||||
| protected function getConfiguration() { | protected function getConfiguration() { | ||||
| return array( | return array( | ||||
| self::CONFIG_AUX_PHID => true, | self::CONFIG_AUX_PHID => true, | ||||
| self::CONFIG_SERIALIZATION => array( | self::CONFIG_SERIALIZATION => array( | ||||
| 'preferences' => self::SERIALIZATION_JSON, | 'preferences' => self::SERIALIZATION_JSON, | ||||
| ), | ), | ||||
| self::CONFIG_COLUMN_SCHEMA => array( | |||||
| 'userPHID' => 'phid?', | |||||
| 'builtinKey' => 'text32?', | |||||
| ), | |||||
| self::CONFIG_KEY_SCHEMA => array( | self::CONFIG_KEY_SCHEMA => array( | ||||
| 'userPHID' => array( | 'key_user' => array( | ||||
| 'columns' => array('userPHID'), | 'columns' => array('userPHID'), | ||||
| 'unique' => true, | 'unique' => true, | ||||
| ), | ), | ||||
| 'key_builtin' => array( | |||||
| 'columns' => array('builtinKey'), | |||||
| 'unique' => true, | |||||
| ), | |||||
| ), | ), | ||||
| ) + parent::getConfiguration(); | ) + parent::getConfiguration(); | ||||
| } | } | ||||
| public function generatePHID() { | public function generatePHID() { | ||||
| return PhabricatorPHID::generateNewPHID( | return PhabricatorPHID::generateNewPHID( | ||||
| PhabricatorUserPreferencesPHIDType::TYPECONST); | PhabricatorUserPreferencesPHIDType::TYPECONST); | ||||
| } | } | ||||
| public function getPreference($key, $default = null) { | public function getPreference($key, $default = null) { | ||||
| return idx($this->preferences, $key, $default); | return idx($this->preferences, $key, $default); | ||||
| } | } | ||||
| public function setPreference($key, $value) { | public function setPreference($key, $value) { | ||||
| $this->preferences[$key] = $value; | $this->preferences[$key] = $value; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function unsetPreference($key) { | public function unsetPreference($key) { | ||||
| unset($this->preferences[$key]); | unset($this->preferences[$key]); | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getDefaultValue($key) { | public function getDefaultValue($key) { | ||||
| if ($this->defaultSettings) { | |||||
| return $this->defaultSettings->getSettingValue($key); | |||||
| } | |||||
| $setting = self::getSettingObject($key); | $setting = self::getSettingObject($key); | ||||
| if (!$setting) { | if (!$setting) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| $setting = id(clone $setting) | $setting = id(clone $setting) | ||||
| ->setViewer($this->getUser()); | ->setViewer($this->getUser()); | ||||
| return $setting->getSettingDefaultValue(); | return $setting->getSettingDefaultValue(); | ||||
| } | } | ||||
| public function getSettingValue($key) { | public function getSettingValue($key) { | ||||
| if (array_key_exists($key, $this->preferences)) { | if (array_key_exists($key, $this->preferences)) { | ||||
| return $this->preferences[$key]; | return $this->preferences[$key]; | ||||
| } | } | ||||
| // TODO: If this setting set inherits from another preference set, | |||||
| // we would look it up here. | |||||
| return $this->getDefaultValue($key); | return $this->getDefaultValue($key); | ||||
| } | } | ||||
| private static function getSettingObject($key) { | private static function getSettingObject($key) { | ||||
| $settings = PhabricatorSetting::getAllSettings(); | $settings = PhabricatorSetting::getAllSettings(); | ||||
| return idx($settings, $key); | return idx($settings, $key); | ||||
| } | } | ||||
| public function attachDefaultSettings(PhabricatorUserPreferences $settings) { | |||||
| $this->defaultSettings = $settings; | |||||
| return $this; | |||||
| } | |||||
| public function attachUser(PhabricatorUser $user = null) { | public function attachUser(PhabricatorUser $user = null) { | ||||
| $this->user = $user; | $this->user = $user; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getUser() { | public function getUser() { | ||||
| return $this->assertAttached($this->user); | return $this->assertAttached($this->user); | ||||
| } | } | ||||
| Show All 36 Lines | public function newTransaction($key, $value) { | ||||
| $xaction_type = PhabricatorUserPreferencesTransaction::TYPE_SETTING; | $xaction_type = PhabricatorUserPreferencesTransaction::TYPE_SETTING; | ||||
| return id(clone $this->getApplicationTransactionTemplate()) | return id(clone $this->getApplicationTransactionTemplate()) | ||||
| ->setTransactionType($xaction_type) | ->setTransactionType($xaction_type) | ||||
| ->setMetadataValue($setting_property, $key) | ->setMetadataValue($setting_property, $key) | ||||
| ->setNewValue($value); | ->setNewValue($value); | ||||
| } | } | ||||
| public function getEditURI() { | |||||
| if ($this->getUser()) { | |||||
| return '/settings/user/'.$this->getUser()->getUsername().'/'; | |||||
| } else { | |||||
| return '/settings/builtin/'.$this->getBuiltinKey().'/'; | |||||
| } | |||||
| } | |||||
| public function getDisplayName() { | |||||
| if ($this->getBuiltinKey()) { | |||||
| return pht('Global Default Settings'); | |||||
| } | |||||
| return pht('Personal Settings'); | |||||
| } | |||||
| /* -( PhabricatorPolicyInterface )----------------------------------------- */ | /* -( PhabricatorPolicyInterface )----------------------------------------- */ | ||||
| public function getCapabilities() { | public function getCapabilities() { | ||||
| return array( | return array( | ||||
| PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
| PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
| ▲ Show 20 Lines • Show All 72 Lines • Show Last 20 Lines | |||||