1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-04 20:52:43 +01:00
phorge-phorge/src/applications/settings/setting/PhabricatorSetting.php

132 lines
3.2 KiB
PHP
Raw Normal View History

<?php
abstract class PhabricatorSetting extends Phobject {
private $viewer = false;
public function setViewer(PhabricatorUser $viewer = null) {
$this->viewer = $viewer;
return $this;
}
public function getViewer() {
if ($this->viewer === false) {
throw new PhutilInvalidStateException('setViewer');
}
return $this->viewer;
}
abstract public function getSettingName();
public function getSettingPanelKey() {
return null;
}
protected function getSettingOrder() {
return 1000;
}
public function getSettingOrderVector() {
return id(new PhutilSortVector())
->addInt($this->getSettingOrder())
->addString($this->getSettingName());
}
protected function getControlInstructions() {
return null;
}
protected function isEnabledForViewer(PhabricatorUser $viewer) {
return true;
}
public function getSettingDefaultValue() {
return null;
}
final public function getSettingKey() {
return $this->getPhobjectClassConstant('SETTINGKEY');
}
public static function getAllSettings() {
return id(new PhutilClassMapQuery())
->setAncestorClass(__CLASS__)
->setUniqueMethod('getSettingKey')
->execute();
}
public static function getAllEnabledSettings(PhabricatorUser $viewer) {
$settings = self::getAllSettings();
foreach ($settings as $key => $setting) {
if (!$setting->isEnabledForViewer($viewer)) {
unset($settings[$key]);
}
}
return $settings;
}
final public function newCustomEditFields($object) {
$fields = array();
$field = $this->newCustomEditField($object);
if ($field) {
$fields[] = $field;
}
return $fields;
}
protected function newCustomEditField($object) {
return null;
}
protected function newEditField($object, PhabricatorEditField $template) {
$setting_property = PhabricatorUserPreferencesTransaction::PROPERTY_SETTING;
$setting_key = $this->getSettingKey();
$value = $object->getPreference($setting_key);
$xaction_type = PhabricatorUserPreferencesTransaction::TYPE_SETTING;
$label = $this->getSettingName();
$template
->setKey($setting_key)
->setLabel($label)
->setValue($value)
->setTransactionType($xaction_type)
->setMetadataValue($setting_property, $setting_key);
$instructions = $this->getControlInstructions();
if (strlen($instructions)) {
$template->setControlInstructions($instructions);
}
return $template;
}
public function validateTransactionValue($value) {
return;
}
Continue modernizing application access to user preferences Summary: Ref T4103. This is just incremental cleanup: - Add "internal" settings, which aren't editable via the UI. They can still do validation and run through the normal pathway. Move a couple settings to use this. - Remove `getPreference()` on `PhabricatorUser`, which was a sort of prototype version of `getUserSetting()`. - Make `getUserSetting()` validate setting values before returning them, to improve robustness if we change allowable values later. - Add a user setting cache, since reading user settings was getting fairly expensive on Calendar. - Improve performance of setting validation for timezone setting (don't require building/computing all timezone offsets). - Since we have the cache anyway, make the timezone override a little more general in its approach. - Move editor stuff to use `getUserSetting()`. Test Plan: - Changed search scopes. - Reconciled local and server timezone settings by ignoring and changing timezones. - Changed date/time settings, browsed Calendar, queried date ranges. - Verified editor links generate properly in Diffusion. - Browsed around with time/date settings looking at timestamps. - Grepped for `getPreference()`, nuked all the ones coming off `$user` or `$viewer` that I could find. - Changed accessiblity to high-contrast colors. - Ran all unit tests. - Grepped for removed constants. Reviewers: chad Reviewed By: chad Maniphest Tasks: T4103 Differential Revision: https://secure.phabricator.com/D16015
2016-06-03 02:12:15 +02:00
public function assertValidValue($value) {
$this->validateTransactionValue($value);
}
public function getTransactionNewValue($value) {
return $value;
}
public function expandSettingTransaction($object, $xaction) {
return array($xaction);
}
protected function newSettingTransaction($object, $key, $value) {
$setting_property = PhabricatorUserPreferencesTransaction::PROPERTY_SETTING;
$xaction_type = PhabricatorUserPreferencesTransaction::TYPE_SETTING;
return id(clone $object->getApplicationTransactionTemplate())
->setTransactionType($xaction_type)
->setMetadataValue($setting_property, $key)
->setNewValue($value);
}
}