public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $options = PhabricatorApplicationConfigOptions::loadAllOptions();
     if (empty($options[$this->key])) {
         $ancient = PhabricatorExtraConfigSetupCheck::getAncientConfig();
         if (isset($ancient[$this->key])) {
             $desc = pht("This configuration has been removed. You can safely delete " . "it.\n\n%s", $ancient[$this->key]);
         } else {
             $desc = pht('This configuration option is unknown. It may be misspelled, ' . 'or have existed in a previous version of Phabricator.');
         }
         // This may be a dead config entry, which existed in the past but no
         // longer exists. Allow it to be edited so it can be reviewed and
         // deleted.
         $option = id(new PhabricatorConfigOption())->setKey($this->key)->setType('wild')->setDefault(null)->setDescription($desc);
         $group = null;
         $group_uri = $this->getApplicationURI();
     } else {
         $option = $options[$this->key];
         $group = $option->getGroup();
         $group_uri = $this->getApplicationURI('group/' . $group->getKey() . '/');
     }
     $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;
     }
     // Check if the config key is already stored in the database.
     // Grab the value if it is.
     $config_entry = id(new PhabricatorConfigEntry())->loadOneWhere('configKey = %s AND namespace = %s', $this->key, 'default');
     if (!$config_entry) {
         $config_entry = id(new PhabricatorConfigEntry())->setConfigKey($this->key)->setNamespace('default')->setIsDeleted(true);
         $config_entry->setPHID($config_entry->generatePHID());
     }
     $e_value = null;
     $errors = array();
     if ($request->isFormPost() && !$option->getLocked()) {
         $result = $this->readRequest($option, $request);
         list($e_value, $value_errors, $display_value, $xaction) = $result;
         $errors = array_merge($errors, $value_errors);
         if (!$errors) {
             $editor = id(new PhabricatorConfigEditor())->setActor($user)->setContinueOnNoEffect(true)->setContentSourceFromRequest($request);
             try {
                 $editor->applyTransactions($config_entry, array($xaction));
                 return id(new AphrontRedirectResponse())->setURI($done_uri);
             } catch (PhabricatorConfigValidationException $ex) {
                 $e_value = pht('Invalid');
                 $errors[] = $ex->getMessage();
             }
         }
     } else {
         if ($config_entry->getIsDeleted()) {
             $display_value = null;
         } else {
             $display_value = $this->getDisplayValue($option, $config_entry, $config_entry->getValue());
         }
     }
     $form = new AphrontFormView();
     $error_view = null;
     if ($errors) {
         $error_view = id(new PHUIInfoView())->setErrors($errors);
     } else {
         if ($option->getHidden()) {
             $msg = pht('This configuration is hidden and can not be edited or viewed from ' . 'the web interface.');
             $error_view = id(new PHUIInfoView())->setTitle(pht('Configuration Hidden'))->setSeverity(PHUIInfoView::SEVERITY_WARNING)->appendChild(phutil_tag('p', array(), $msg));
         } else {
             if ($option->getLocked()) {
                 $msg = $option->getLockedMessage();
                 $error_view = id(new PHUIInfoView())->setTitle(pht('Configuration Locked'))->setSeverity(PHUIInfoView::SEVERITY_NOTICE)->appendChild(phutil_tag('p', array(), $msg));
             }
         }
     }
     if ($option->getHidden()) {
         $control = null;
     } else {
         $control = $this->renderControl($option, $display_value, $e_value);
     }
     $engine = new PhabricatorMarkupEngine();
     $engine->setViewer($user);
     $engine->addObject($option, 'description');
     $engine->process();
     $description = phutil_tag('div', array('class' => 'phabricator-remarkup'), $engine->getOutput($option, 'description'));
     $form->setUser($user)->addHiddenInput('issue', $request->getStr('issue'))->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Description'))->setValue($description));
     if ($group) {
         $extra = $group->renderContextualDescription($option, $request);
         if ($extra !== null) {
             $form->appendChild(id(new AphrontFormMarkupControl())->setValue($extra));
         }
     }
     $form->appendChild($control);
     $submit_control = id(new AphrontFormSubmitControl())->addCancelButton($done_uri);
     if (!$option->getLocked()) {
         $submit_control->setValue(pht('Save Config Entry'));
     }
     $form->appendChild($submit_control);
     $examples = $this->renderExamples($option);
     if ($examples) {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Examples'))->setValue($examples));
     }
     if (!$option->getHidden()) {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Default'))->setValue($this->renderDefaults($option, $config_entry)));
     }
     $title = pht('Edit %s', $this->key);
     $short = pht('Edit');
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setForm($form);
     if ($error_view) {
         $form_box->setInfoView($error_view);
     }
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb(pht('Config'), $this->getApplicationURI());
     if ($group) {
         $crumbs->addTextCrumb($group->getName(), $group_uri);
     }
     $crumbs->addTextCrumb($this->key, '/config/edit/' . $this->key);
     $timeline = $this->buildTransactionTimeline($config_entry, new PhabricatorConfigTransactionQuery());
     $timeline->setShouldTerminate(true);
     return $this->buildApplicationPage(array($crumbs, $form_box, $timeline), array('title' => $title));
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $key = $request->getURIData('key');
     $options = PhabricatorApplicationConfigOptions::loadAllOptions();
     if (empty($options[$key])) {
         $ancient = PhabricatorExtraConfigSetupCheck::getAncientConfig();
         if (isset($ancient[$key])) {
             $desc = pht("This configuration has been removed. You can safely delete " . "it.\n\n%s", $ancient[$key]);
         } else {
             $desc = pht('This configuration option is unknown. It may be misspelled, ' . 'or have existed in a previous version of Phabricator.');
         }
         // This may be a dead config entry, which existed in the past but no
         // longer exists. Allow it to be edited so it can be reviewed and
         // deleted.
         $option = id(new PhabricatorConfigOption())->setKey($key)->setType('wild')->setDefault(null)->setDescription($desc);
         $group = null;
         $group_uri = $this->getApplicationURI();
     } else {
         $option = $options[$key];
         $group = $option->getGroup();
         $group_uri = $this->getApplicationURI('group/' . $group->getKey() . '/');
     }
     $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;
     }
     // Check if the config key is already stored in the database.
     // Grab the value if it is.
     $config_entry = id(new PhabricatorConfigEntry())->loadOneWhere('configKey = %s AND namespace = %s', $key, 'default');
     if (!$config_entry) {
         $config_entry = id(new PhabricatorConfigEntry())->setConfigKey($key)->setNamespace('default')->setIsDeleted(true);
         $config_entry->setPHID($config_entry->generatePHID());
     }
     $e_value = null;
     $errors = array();
     if ($request->isFormPost() && !$option->getLocked()) {
         $result = $this->readRequest($option, $request);
         list($e_value, $value_errors, $display_value, $xaction) = $result;
         $errors = array_merge($errors, $value_errors);
         if (!$errors) {
             $editor = id(new PhabricatorConfigEditor())->setActor($viewer)->setContinueOnNoEffect(true)->setContentSourceFromRequest($request);
             try {
                 $editor->applyTransactions($config_entry, array($xaction));
                 return id(new AphrontRedirectResponse())->setURI($done_uri);
             } catch (PhabricatorConfigValidationException $ex) {
                 $e_value = pht('Invalid');
                 $errors[] = $ex->getMessage();
             }
         }
     } else {
         if ($config_entry->getIsDeleted()) {
             $display_value = null;
         } else {
             $display_value = $this->getDisplayValue($option, $config_entry, $config_entry->getValue());
         }
     }
     $form = id(new AphrontFormView())->setEncType('multipart/form-data');
     $error_view = null;
     if ($errors) {
         $error_view = id(new PHUIInfoView())->setErrors($errors);
     }
     $status_items = array();
     if ($option->getHidden()) {
         $message = pht('This configuration is hidden and can not be edited or viewed from ' . 'the web interface.');
         $status_items[] = id(new PHUIStatusItemView())->setIcon('fa-eye-slash red')->setTarget(phutil_tag('strong', array(), pht('Configuration Hidden')))->setNote($message);
     } else {
         if ($option->getLocked()) {
             $message = $option->getLockedMessage();
             $status_items[] = id(new PHUIStatusItemView())->setIcon('fa-lock red')->setTarget(phutil_tag('strong', array(), pht('Configuration Locked')))->setNote($message);
         }
     }
     if ($status_items) {
         $doc_href = PhabricatorEnv::getDoclink('Configuration Guide: Locked and Hidden Configuration');
         $doc_link = phutil_tag('a', array('href' => $doc_href, 'target' => '_blank'), pht('Configuration Guide: Locked and Hidden Configuration'));
         $status_items[] = id(new PHUIStatusItemView())->setIcon('fa-book')->setTarget(phutil_tag('strong', array(), pht('Learn More')))->setNote($doc_link);
     }
     if ($option->getHidden() || $option->getLocked()) {
         $controls = array();
     } else {
         $controls = $this->renderControls($option, $display_value, $e_value);
     }
     $engine = new PhabricatorMarkupEngine();
     $engine->setViewer($viewer);
     $engine->addObject($option, 'description');
     $engine->process();
     $description = phutil_tag('div', array('class' => 'phabricator-remarkup'), $engine->getOutput($option, 'description'));
     $form->setUser($viewer)->addHiddenInput('issue', $request->getStr('issue'));
     if ($status_items) {
         $status_view = id(new PHUIStatusListView());
         foreach ($status_items as $status_item) {
             $status_view->addItem($status_item);
         }
         $form->appendControl(id(new AphrontFormMarkupControl())->setValue($status_view));
     }
     $description = $option->getDescription();
     if (strlen($description)) {
         $description_view = new PHUIRemarkupView($viewer, $description);
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Description'))->setValue($description_view));
     }
     if ($group) {
         $extra = $group->renderContextualDescription($option, $request);
         if ($extra !== null) {
             $form->appendChild(id(new AphrontFormMarkupControl())->setValue($extra));
         }
     }
     foreach ($controls as $control) {
         $form->appendControl($control);
     }
     if (!$option->getLocked()) {
         $form->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($done_uri)->setValue(pht('Save Config Entry')));
     }
     if (!$option->getHidden()) {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Current Configuration'))->setValue($this->renderDefaults($option, $config_entry)));
     }
     $examples = $this->renderExamples($option);
     if ($examples) {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Examples'))->setValue($examples));
     }
     $title = pht('Edit Option: %s', $key);
     $header_icon = 'fa-pencil';
     $short = pht('Edit');
     $form_box = id(new PHUIObjectBoxView())->setHeaderText(pht('Config Option'))->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setForm($form);
     if ($error_view) {
         $form_box->setInfoView($error_view);
     }
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb(pht('Config'), $this->getApplicationURI());
     if ($group) {
         $crumbs->addTextCrumb($group->getName(), $group_uri);
     }
     $crumbs->addTextCrumb($key, '/config/edit/' . $key);
     $crumbs->setBorder(true);
     $timeline = $this->buildTransactionTimeline($config_entry, new PhabricatorConfigTransactionQuery());
     $timeline->setShouldTerminate(true);
     $header = id(new PHUIHeaderView())->setHeader($title)->setHeaderIcon($header_icon);
     $view = id(new PHUITwoColumnView())->setHeader($header)->setFooter($form_box);
     return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild($view);
 }