2012-12-28 00:20:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorConfigEditController
|
|
|
|
extends PhabricatorConfigController {
|
|
|
|
|
|
|
|
private $key;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
2012-12-31 00:36:06 +01:00
|
|
|
$this->key = $data['key'];
|
2012-12-28 00:20:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2012-12-31 00:36:06 +01:00
|
|
|
|
|
|
|
$options = PhabricatorApplicationConfigOptions::loadAllOptions();
|
|
|
|
if (empty($options[$this->key])) {
|
2012-12-28 00:20:09 +01:00
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2012-12-31 00:36:06 +01:00
|
|
|
$option = $options[$this->key];
|
|
|
|
$group = $option->getGroup();
|
|
|
|
$group_uri = $this->getApplicationURI('group/'.$group->getKey().'/');
|
|
|
|
|
2012-12-31 02:04:38 +01:00
|
|
|
$issue = $request->getStr('issue');
|
|
|
|
if ($issue) {
|
|
|
|
// If the user came here from an open setup issue, send them back.
|
|
|
|
$done_uri = $this->getApplicationURI('issue/'.$issue.'/');
|
|
|
|
} else {
|
|
|
|
$done_uri = $group_uri;
|
|
|
|
}
|
|
|
|
|
2012-12-31 00:36:06 +01:00
|
|
|
// TODO: This isn't quite correct -- we should read from the entire
|
|
|
|
// configuration stack, ignoring database configuration. For now, though,
|
|
|
|
// it's a reasonable approximation.
|
|
|
|
$default_value = $option->getDefault();
|
2012-12-28 00:20:09 +01:00
|
|
|
|
2012-12-31 00:36:06 +01:00
|
|
|
$default = $this->prettyPrintJSON($default_value);
|
2012-12-28 20:37:55 +01:00
|
|
|
|
2012-12-28 00:20:09 +01:00
|
|
|
// Check if the config key is already stored in the database.
|
|
|
|
// Grab the value if it is.
|
|
|
|
$value = null;
|
|
|
|
$config_entry = id(new PhabricatorConfigEntry())
|
|
|
|
->loadOneWhere(
|
2012-12-31 00:36:06 +01:00
|
|
|
'configKey = %s AND namespace = %s',
|
2012-12-28 00:20:09 +01:00
|
|
|
$this->key,
|
|
|
|
'default');
|
|
|
|
if ($config_entry) {
|
|
|
|
$value = $config_entry->getValue();
|
|
|
|
} else {
|
|
|
|
$config_entry = id(new PhabricatorConfigEntry())
|
|
|
|
->setConfigKey($this->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
$e_value = null;
|
|
|
|
$errors = array();
|
|
|
|
if ($request->isFormPost()) {
|
2012-12-31 00:36:06 +01:00
|
|
|
|
2012-12-28 00:20:09 +01:00
|
|
|
$new_value = $request->getStr('value');
|
|
|
|
if (strlen($new_value)) {
|
|
|
|
$json = json_decode($new_value, true);
|
2012-12-30 15:15:41 +01:00
|
|
|
if ($json === null && strtolower($new_value) != 'null') {
|
2012-12-28 00:20:09 +01:00
|
|
|
$e_value = 'Invalid';
|
|
|
|
$errors[] = 'The given value must be valid JSON. This means, among '.
|
|
|
|
'other things, that you must wrap strings in double-quotes.';
|
|
|
|
$value = $new_value;
|
|
|
|
} else {
|
|
|
|
$value = $json;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: When we do Transactions, make this just set isDeleted = 1
|
|
|
|
$config_entry->delete();
|
2012-12-31 02:04:38 +01:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($done_uri);
|
2012-12-28 00:20:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$config_entry->setValue($value);
|
|
|
|
$config_entry->setNamespace('default');
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
$config_entry->save();
|
2012-12-31 02:04:38 +01:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($done_uri);
|
2012-12-28 00:20:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new AphrontFormView();
|
|
|
|
$form->setFlexible(true);
|
|
|
|
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
|
|
|
->setTitle('You broke everything!')
|
|
|
|
->setErrors($errors);
|
|
|
|
} else {
|
2012-12-28 20:37:55 +01:00
|
|
|
$value = $this->prettyPrintJSON($value);
|
2012-12-28 00:20:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$form
|
|
|
|
->setUser($user)
|
2012-12-31 02:04:38 +01:00
|
|
|
->addHiddenInput('issue', $request->getStr('issue'))
|
2012-12-28 00:20:09 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextAreaControl())
|
|
|
|
->setLabel('JSON Value')
|
|
|
|
->setError($e_value)
|
|
|
|
->setValue($value)
|
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
|
|
|
->setCustomClass('PhabricatorMonospaced')
|
2012-12-28 20:37:55 +01:00
|
|
|
->setName('value'))
|
2012-12-28 00:20:09 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2012-12-31 02:04:38 +01:00
|
|
|
->addCancelButton($done_uri)
|
2012-12-28 20:37:55 +01:00
|
|
|
->setValue(pht('Save Config Entry')))
|
|
|
|
->appendChild(
|
|
|
|
phutil_render_tag(
|
|
|
|
'p',
|
|
|
|
array(
|
|
|
|
'class' => 'aphront-form-input',
|
|
|
|
),
|
|
|
|
'If left blank, the setting will return to its default value. '.
|
|
|
|
'Its default value is:'))
|
|
|
|
->appendChild(
|
|
|
|
phutil_render_tag(
|
|
|
|
'pre',
|
|
|
|
array(
|
|
|
|
'class' => 'aphront-form-input',
|
|
|
|
),
|
|
|
|
phutil_escape_html($default)));
|
2012-12-28 00:20:09 +01:00
|
|
|
|
|
|
|
$title = pht('Edit %s', $this->key);
|
|
|
|
$short = pht('Edit');
|
|
|
|
|
2012-12-31 00:36:06 +01:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
$crumbs
|
|
|
|
->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName(pht('Config'))
|
|
|
|
->setHref($this->getApplicationURI()))
|
|
|
|
->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName($group->getName())
|
|
|
|
->setHref($group_uri))
|
|
|
|
->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName($this->key)
|
|
|
|
->setHref('/config/edit/'.$this->key));
|
2012-12-28 00:20:09 +01:00
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
array(
|
|
|
|
$crumbs,
|
|
|
|
id(new PhabricatorHeaderView())->setHeader($title),
|
|
|
|
$error_view,
|
|
|
|
$form,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
'device' => true,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|