Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / settings / setting / PhabricatorSetting.php
blob5a25df5fac0c19d222be3ad536773a8047213746
1 <?php
3 abstract class PhabricatorSetting extends Phobject {
5 private $viewer = false;
7 public function setViewer(PhabricatorUser $viewer = null) {
8 $this->viewer = $viewer;
9 return $this;
12 public function getViewer() {
13 if ($this->viewer === false) {
14 throw new PhutilInvalidStateException('setViewer');
16 return $this->viewer;
19 abstract public function getSettingName();
21 public function getSettingPanelKey() {
22 return null;
25 protected function getSettingOrder() {
26 return 1000;
29 public function getSettingOrderVector() {
30 return id(new PhutilSortVector())
31 ->addInt($this->getSettingOrder())
32 ->addString($this->getSettingName());
35 protected function getControlInstructions() {
36 return null;
39 protected function isEnabledForViewer(PhabricatorUser $viewer) {
40 return true;
43 public function getSettingDefaultValue() {
44 return null;
47 final public function getSettingKey() {
48 return $this->getPhobjectClassConstant('SETTINGKEY');
51 public static function getAllSettings() {
52 return id(new PhutilClassMapQuery())
53 ->setAncestorClass(__CLASS__)
54 ->setUniqueMethod('getSettingKey')
55 ->execute();
58 public static function getAllEnabledSettings(PhabricatorUser $viewer) {
59 $settings = self::getAllSettings();
60 foreach ($settings as $key => $setting) {
61 if (!$setting->isEnabledForViewer($viewer)) {
62 unset($settings[$key]);
65 return $settings;
68 final public function newCustomEditFields($object) {
69 $fields = array();
71 $field = $this->newCustomEditField($object);
72 if ($field) {
73 $fields[] = $field;
76 return $fields;
79 protected function newCustomEditField($object) {
80 return null;
83 protected function newEditField($object, PhabricatorEditField $template) {
84 $setting_property = PhabricatorUserPreferencesTransaction::PROPERTY_SETTING;
85 $setting_key = $this->getSettingKey();
86 $value = $object->getPreference($setting_key);
87 $xaction_type = PhabricatorUserPreferencesTransaction::TYPE_SETTING;
88 $label = $this->getSettingName();
90 $template
91 ->setKey($setting_key)
92 ->setLabel($label)
93 ->setValue($value)
94 ->setTransactionType($xaction_type)
95 ->setMetadataValue($setting_property, $setting_key);
97 $instructions = $this->getControlInstructions();
98 if (strlen($instructions)) {
99 $template->setControlInstructions($instructions);
102 return $template;
105 public function validateTransactionValue($value) {
106 return;
109 public function assertValidValue($value) {
110 $this->validateTransactionValue($value);
113 public function getTransactionNewValue($value) {
114 return $value;
117 public function expandSettingTransaction($object, $xaction) {
118 return array($xaction);
121 protected function newSettingTransaction($object, $key, $value) {
122 $setting_property = PhabricatorUserPreferencesTransaction::PROPERTY_SETTING;
123 $xaction_type = PhabricatorUserPreferencesTransaction::TYPE_SETTING;
125 return id(clone $object->getApplicationTransactionTemplate())
126 ->setTransactionType($xaction_type)
127 ->setMetadataValue($setting_property, $key)
128 ->setNewValue($value);