1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-01 03:02:43 +01:00
phorge-phorge/src/applications/transactions/controller/PhabricatorEditEngineConfigurationDefaultsController.php
epriestley 53d5cd3950 Allow EditEngine forms to have defaults assigned
Summary: Ref T9132. Allow form configurations to include defaults (like default projects, spaces, policies, etc).

Test Plan:
Defaulted "Language" to "Rainbow", plus other adjustments:

{F975746}

{F975747}

{F975748}

{F975749}

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T9132

Differential Revision: https://secure.phabricator.com/D14508
2015-11-22 16:25:00 -08:00

111 lines
3 KiB
PHP

<?php
final class PhabricatorEditEngineConfigurationDefaultsController
extends PhabricatorEditEngineController {
public function handleRequest(AphrontRequest $request) {
$engine_key = $request->getURIData('engineKey');
$this->setEngineKey($engine_key);
$key = $request->getURIData('key');
$viewer = $this->getViewer();
$config = id(new PhabricatorEditEngineConfigurationQuery())
->setViewer($viewer)
->withEngineKeys(array($engine_key))
->withIdentifiers(array($key))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$config) {
return id(new Aphront404Response());
}
$cancel_uri = "/transactions/editengine/{$engine_key}/view/{$key}/";
$engine = $config->getEngine();
$fields = $engine->getFieldsForConfig($config);
foreach ($fields as $key => $field) {
if (!$field->getIsDefaultable()) {
unset($fields[$key]);
continue;
}
}
foreach ($fields as $field) {
$field->setIsEditDefaults(true);
}
if ($request->isFormPost()) {
$xactions = array();
foreach ($fields as $field) {
$field->readValueFromSubmit($request);
}
$type = PhabricatorEditEngineConfigurationTransaction::TYPE_DEFAULT;
$xactions = array();
foreach ($fields as $field) {
$new_value = $field->getValueForDefaults();
$xactions[] = id(new PhabricatorEditEngineConfigurationTransaction())
->setTransactionType($type)
->setMetadataValue('field.key', $field->getKey())
->setNewValue($new_value);
}
$editor = id(new PhabricatorEditEngineConfigurationEditor())
->setActor($viewer)
->setContentSourceFromRequest($request)
->setContinueOnMissingFields(true)
->setContinueOnNoEffect(true);
$editor->applyTransactions($config, $xactions);
return id(new AphrontRedirectResponse())
->setURI($cancel_uri);
}
$title = pht('Edit Form Defaults');
$form = id(new AphrontFormView())
->setUser($viewer);
foreach ($fields as $field) {
$field->appendToForm($form);
}
$form
->appendControl(
id(new AphrontFormSubmitControl())
->setValue(pht('Save Defaults'))
->addCancelButton($cancel_uri));
$info = id(new PHUIInfoView())
->setSeverity(PHUIInfoView::SEVERITY_NOTICE)
->setErrors(
array(
pht('You are editing the default values for this form.'),
));
$box = id(new PHUIObjectBoxView())
->setHeaderText($title)
->setInfoView($info)
->setForm($form);
$crumbs = $this->buildApplicationCrumbs();
$crumbs->addTextCrumb(pht('Form %d', $config->getID()), $cancel_uri);
$crumbs->addTextCrumb(pht('Edit Defaults'));
return $this->newPage()
->setTitle($title)
->setCrumbs($crumbs)
->appendChild($box);
}
}