Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / settings / editor / PhabricatorUserPreferencesEditor.php
bloba927cc747538d951e73bde2245ced7681f8608da
1 <?php
3 final class PhabricatorUserPreferencesEditor
4 extends PhabricatorApplicationTransactionEditor {
6 public function getEditorApplicationClass() {
7 return 'PhabricatorSettingsApplication';
10 public function getEditorObjectsDescription() {
11 return pht('Settings');
14 public function getTransactionTypes() {
15 $types = parent::getTransactionTypes();
17 $types[] = PhabricatorUserPreferencesTransaction::TYPE_SETTING;
19 return $types;
22 protected function expandTransaction(
23 PhabricatorLiskDAO $object,
24 PhabricatorApplicationTransaction $xaction) {
26 $setting_key = $xaction->getMetadataValue(
27 PhabricatorUserPreferencesTransaction::PROPERTY_SETTING);
29 $settings = $this->getSettings();
30 $setting = idx($settings, $setting_key);
31 if ($setting) {
32 return $setting->expandSettingTransaction($object, $xaction);
35 return parent::expandTransaction($object, $xaction);
39 protected function getCustomTransactionOldValue(
40 PhabricatorLiskDAO $object,
41 PhabricatorApplicationTransaction $xaction) {
43 $setting_key = $xaction->getMetadataValue(
44 PhabricatorUserPreferencesTransaction::PROPERTY_SETTING);
46 switch ($xaction->getTransactionType()) {
47 case PhabricatorUserPreferencesTransaction::TYPE_SETTING:
48 return $object->getPreference($setting_key);
51 return parent::getCustomTransactionOldValue($object, $xaction);
54 protected function getCustomTransactionNewValue(
55 PhabricatorLiskDAO $object,
56 PhabricatorApplicationTransaction $xaction) {
58 $actor = $this->getActor();
60 $setting_key = $xaction->getMetadataValue(
61 PhabricatorUserPreferencesTransaction::PROPERTY_SETTING);
63 $settings = PhabricatorSetting::getAllEnabledSettings($actor);
64 $setting = $settings[$setting_key];
66 switch ($xaction->getTransactionType()) {
67 case PhabricatorUserPreferencesTransaction::TYPE_SETTING:
68 $value = $xaction->getNewValue();
69 $value = $setting->getTransactionNewValue($value);
70 return $value;
73 return parent::getCustomTransactionNewValue($object, $xaction);
76 protected function applyCustomInternalTransaction(
77 PhabricatorLiskDAO $object,
78 PhabricatorApplicationTransaction $xaction) {
80 $setting_key = $xaction->getMetadataValue(
81 PhabricatorUserPreferencesTransaction::PROPERTY_SETTING);
83 switch ($xaction->getTransactionType()) {
84 case PhabricatorUserPreferencesTransaction::TYPE_SETTING:
85 $new_value = $xaction->getNewValue();
86 if ($new_value === null) {
87 $object->unsetPreference($setting_key);
88 } else {
89 $object->setPreference($setting_key, $new_value);
91 return;
94 return parent::applyCustomInternalTransaction($object, $xaction);
97 protected function applyCustomExternalTransaction(
98 PhabricatorLiskDAO $object,
99 PhabricatorApplicationTransaction $xaction) {
101 switch ($xaction->getTransactionType()) {
102 case PhabricatorUserPreferencesTransaction::TYPE_SETTING:
103 return;
106 return parent::applyCustomExternalTransaction($object, $xaction);
109 protected function validateTransaction(
110 PhabricatorLiskDAO $object,
111 $type,
112 array $xactions) {
114 $errors = parent::validateTransaction($object, $type, $xactions);
115 $settings = $this->getSettings();
117 switch ($type) {
118 case PhabricatorUserPreferencesTransaction::TYPE_SETTING:
119 foreach ($xactions as $xaction) {
120 $setting_key = $xaction->getMetadataValue(
121 PhabricatorUserPreferencesTransaction::PROPERTY_SETTING);
123 $setting = idx($settings, $setting_key);
124 if (!$setting) {
125 $errors[] = new PhabricatorApplicationTransactionValidationError(
126 $type,
127 pht('Invalid'),
128 pht(
129 'There is no known application setting with key "%s".',
130 $setting_key),
131 $xaction);
132 continue;
135 try {
136 $setting->validateTransactionValue($xaction->getNewValue());
137 } catch (Exception $ex) {
138 $errors[] = new PhabricatorApplicationTransactionValidationError(
139 $type,
140 pht('Invalid'),
141 $ex->getMessage(),
142 $xaction);
145 break;
148 return $errors;
151 protected function applyFinalEffects(
152 PhabricatorLiskDAO $object,
153 array $xactions) {
155 $user_phid = $object->getUserPHID();
156 if ($user_phid) {
157 PhabricatorUserCache::clearCache(
158 PhabricatorUserPreferencesCacheType::KEY_PREFERENCES,
159 $user_phid);
160 } else {
161 $cache = PhabricatorCaches::getMutableStructureCache();
162 $cache->deleteKey(PhabricatorUser::getGlobalSettingsCacheKey());
164 PhabricatorUserCache::clearCacheForAllUsers(
165 PhabricatorUserPreferencesCacheType::KEY_PREFERENCES);
168 return $xactions;
171 private function getSettings() {
172 $actor = $this->getActor();
173 $settings = PhabricatorSetting::getAllEnabledSettings($actor);
175 foreach ($settings as $key => $setting) {
176 $setting = clone $setting;
177 $setting->setViewer($actor);
178 $settings[$key] = $setting;
181 return $settings;