Differential D18155 Diff 43701 src/applications/config/controller/PhabricatorConfigEditController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/config/controller/PhabricatorConfigEditController.php
Show First 20 Lines • Show All 268 Lines • ▼ Show 20 Lines | return $this->newPage() | ||||
->setCrumbs($crumbs) | ->setCrumbs($crumbs) | ||||
->appendChild($view); | ->appendChild($view); | ||||
} | } | ||||
private function readRequest( | private function readRequest( | ||||
PhabricatorConfigOption $option, | PhabricatorConfigOption $option, | ||||
AphrontRequest $request) { | AphrontRequest $request) { | ||||
$type = $option->newOptionType(); | |||||
if ($type) { | |||||
$is_set = $type->isValuePresentInRequest($option, $request); | |||||
if ($is_set) { | |||||
$value = $type->readValueFromRequest($option, $request); | |||||
$errors = array(); | |||||
try { | |||||
$canonical_value = $type->newValueFromRequestValue( | |||||
$option, | |||||
$value); | |||||
$type->validateStoredValue($canonical_value); | |||||
$xaction = $type->newTransaction($option, $canonical_value); | |||||
} catch (PhabricatorConfigValidationException $ex) { | |||||
$errors[] = $ex->getMessage(); | |||||
$xaction = null; | |||||
} | |||||
return array( | |||||
$errors ? pht('Invalid') : null, | |||||
$errors, | |||||
$value, | |||||
$xaction, | |||||
); | |||||
} else { | |||||
$delete_xaction = id(new PhabricatorConfigTransaction()) | |||||
->setTransactionType(PhabricatorConfigTransaction::TYPE_EDIT) | |||||
->setNewValue( | |||||
array( | |||||
'deleted' => true, | |||||
'value' => null, | |||||
)); | |||||
return array( | |||||
null, | |||||
array(), | |||||
null, | |||||
$delete_xaction, | |||||
); | |||||
} | |||||
} | |||||
// TODO: If we missed on the new `PhabricatorConfigType` map, fall back | |||||
// to the old semi-modular, semi-hacky way of doing things. | |||||
$xaction = new PhabricatorConfigTransaction(); | $xaction = new PhabricatorConfigTransaction(); | ||||
$xaction->setTransactionType(PhabricatorConfigTransaction::TYPE_EDIT); | $xaction->setTransactionType(PhabricatorConfigTransaction::TYPE_EDIT); | ||||
$e_value = null; | $e_value = null; | ||||
$errors = array(); | $errors = array(); | ||||
if ($option->isCustomType()) { | if ($option->isCustomType()) { | ||||
$info = $option->getCustomObject()->readRequest($option, $request); | $info = $option->getCustomObject()->readRequest($option, $request); | ||||
list($e_value, $errors, $set_value, $value) = $info; | list($e_value, $errors, $set_value, $value) = $info; | ||||
} else { | } else { | ||||
$value = $request->getStr('value'); | $value = $request->getStr('value'); | ||||
if (!strlen($value)) { | if (!strlen($value)) { | ||||
$value = null; | $value = null; | ||||
$xaction->setNewValue( | $xaction->setNewValue( | ||||
array( | array( | ||||
'deleted' => true, | 'deleted' => true, | ||||
'value' => null, | 'value' => null, | ||||
)); | )); | ||||
return array($e_value, $errors, $value, $xaction); | return array($e_value, $errors, $value, $xaction); | ||||
} | } | ||||
$type = $option->getType(); | $type = $option->getType(); | ||||
$set_value = null; | $set_value = null; | ||||
switch ($type) { | switch ($type) { | ||||
case 'int': | |||||
if (preg_match('/^-?[0-9]+$/', trim($value))) { | |||||
$set_value = (int)$value; | |||||
} else { | |||||
$e_value = pht('Invalid'); | |||||
$errors[] = pht('Value must be an integer.'); | |||||
} | |||||
break; | |||||
case 'string': | case 'string': | ||||
case 'enum': | case 'enum': | ||||
$set_value = (string)$value; | $set_value = (string)$value; | ||||
break; | break; | ||||
case 'list<string>': | case 'list<string>': | ||||
case 'list<regex>': | case 'list<regex>': | ||||
$set_value = phutil_split_lines( | $set_value = phutil_split_lines( | ||||
$request->getStr('value'), | $request->getStr('value'), | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | private function readRequest( | ||||
return array($e_value, $errors, $value, $xaction); | return array($e_value, $errors, $value, $xaction); | ||||
} | } | ||||
private function getDisplayValue( | private function getDisplayValue( | ||||
PhabricatorConfigOption $option, | PhabricatorConfigOption $option, | ||||
PhabricatorConfigEntry $entry, | PhabricatorConfigEntry $entry, | ||||
$value) { | $value) { | ||||
$type = $option->newOptionType(); | |||||
if ($type) { | |||||
return $type->newDisplayValue($option, $value); | |||||
} | |||||
if ($option->isCustomType()) { | if ($option->isCustomType()) { | ||||
return $option->getCustomObject()->getDisplayValue( | return $option->getCustomObject()->getDisplayValue( | ||||
$option, | $option, | ||||
$entry, | $entry, | ||||
$value); | $value); | ||||
} else { | } else { | ||||
$type = $option->getType(); | $type = $option->getType(); | ||||
switch ($type) { | switch ($type) { | ||||
case 'int': | |||||
case 'string': | case 'string': | ||||
case 'enum': | case 'enum': | ||||
case 'class': | case 'class': | ||||
return $value; | return $value; | ||||
case 'bool': | case 'bool': | ||||
return $value ? 'true' : 'false'; | return $value ? 'true' : 'false'; | ||||
case 'list<string>': | case 'list<string>': | ||||
case 'list<regex>': | case 'list<regex>': | ||||
return implode("\n", nonempty($value, array())); | return implode("\n", nonempty($value, array())); | ||||
case 'set': | case 'set': | ||||
return implode("\n", nonempty(array_keys($value), array())); | return implode("\n", nonempty(array_keys($value), array())); | ||||
default: | default: | ||||
return PhabricatorConfigJSON::prettyPrintJSON($value); | return PhabricatorConfigJSON::prettyPrintJSON($value); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private function renderControls( | private function renderControls( | ||||
PhabricatorConfigOption $option, | PhabricatorConfigOption $option, | ||||
$display_value, | $display_value, | ||||
$e_value) { | $e_value) { | ||||
$type = $option->newOptionType(); | |||||
if ($type) { | |||||
return $type->newControls( | |||||
$option, | |||||
$display_value, | |||||
$e_value); | |||||
} | |||||
if ($option->isCustomType()) { | if ($option->isCustomType()) { | ||||
$controls = $option->getCustomObject()->renderControls( | $controls = $option->getCustomObject()->renderControls( | ||||
$option, | $option, | ||||
$display_value, | $display_value, | ||||
$e_value); | $e_value); | ||||
} else { | } else { | ||||
$type = $option->getType(); | $type = $option->getType(); | ||||
switch ($type) { | switch ($type) { | ||||
case 'int': | |||||
case 'string': | case 'string': | ||||
$control = id(new AphrontFormTextControl()); | $control = id(new AphrontFormTextControl()); | ||||
break; | break; | ||||
case 'bool': | case 'bool': | ||||
$control = id(new AphrontFormSelectControl()) | $control = id(new AphrontFormSelectControl()) | ||||
->setOptions( | ->setOptions( | ||||
array( | array( | ||||
'' => pht('(Use Default)'), | '' => pht('(Use Default)'), | ||||
▲ Show 20 Lines • Show All 155 Lines • Show Last 20 Lines |