Differential D15327 Diff 36968 src/applications/almanac/controller/AlmanacPropertyEditController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/almanac/controller/AlmanacPropertyEditController.php
Show All 18 Lines | if (!$object) { | ||||
return new Aphront404Response(); | return new Aphront404Response(); | ||||
} | } | ||||
if (!($object instanceof AlmanacPropertyInterface)) { | if (!($object instanceof AlmanacPropertyInterface)) { | ||||
return new Aphront404Response(); | return new Aphront404Response(); | ||||
} | } | ||||
$cancel_uri = $object->getURI(); | $cancel_uri = $object->getURI(); | ||||
$property_key = $request->getStr('key'); | |||||
$key = $request->getStr('key'); | if (!strlen($property_key)) { | ||||
if ($key) { | return $this->buildPropertyKeyResponse($cancel_uri, null); | ||||
$property_key = $key; | |||||
$is_new = false; | |||||
$title = pht('Edit Property'); | |||||
$save_button = pht('Save Changes'); | |||||
} else { | } else { | ||||
$property_key = null; | $error = null; | ||||
try { | |||||
$is_new = true; | AlmanacNames::validateName($property_key); | ||||
$title = pht('Add Property'); | } catch (Exception $ex) { | ||||
$save_button = pht('Add Property'); | $error = $ex->getMessage(); | ||||
} | } | ||||
if ($is_new) { | // NOTE: If you enter an existing name, we're just treating it as an | ||||
$errors = array(); | // edit operation. This might be a little confusing. | ||||
$property = null; | |||||
$v_name = null; | |||||
$e_name = true; | |||||
if ($error !== null) { | |||||
if ($request->isFormPost()) { | if ($request->isFormPost()) { | ||||
$name = $request->getStr('name'); | // The user is creating a new property and picked a bad name. Give | ||||
if (!strlen($name)) { | // them an opportunity to fix it. | ||||
$e_name = pht('Required'); | return $this->buildPropertyKeyResponse($cancel_uri, $error); | ||||
$errors[] = pht('You must provide a property name.'); | |||||
} else { | } else { | ||||
$caught = null; | // The user is editing an invalid property. | ||||
try { | return $this->newDialog() | ||||
AlmanacNames::validateName($name); | ->setTitle(pht('Invalid Property')) | ||||
} catch (Exception $ex) { | ->appendParagraph( | ||||
$caught = $ex; | pht( | ||||
'The property name "%s" is invalid. This property can not '. | |||||
'be edited.', | |||||
$property_key)) | |||||
->appendParagraph($error) | |||||
->addCancelButton($cancel_uri); | |||||
} | } | ||||
if ($caught) { | |||||
$e_name = pht('Invalid'); | |||||
$errors[] = $caught->getMessage(); | |||||
} | } | ||||
} | } | ||||
if (!$errors) { | return $object->newAlmanacPropertyEditEngine() | ||||
$property_key = $name; | ->addContextParameter('objectPHID') | ||||
} | ->addContextParameter('key') | ||||
->setTargetObject($object) | |||||
->setPropertyKey($property_key) | |||||
->setController($this) | |||||
->buildResponse(); | |||||
} | |||||
private function buildPropertyKeyResponse($cancel_uri, $error) { | |||||
$viewer = $this->getViewer(); | |||||
$request = $this->getRequest(); | |||||
$v_key = $request->getStr('key'); | |||||
if ($error !== null) { | |||||
$e_key = pht('Invalid'); | |||||
} else { | |||||
$e_key = true; | |||||
} | } | ||||
if ($property_key === null) { | |||||
$form = id(new AphrontFormView()) | $form = id(new AphrontFormView()) | ||||
->setUser($viewer) | ->setUser($viewer) | ||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
->setName('name') | ->setName('key') | ||||
->setLabel(pht('Name')) | ->setLabel(pht('Name')) | ||||
->setValue($v_name) | ->setValue($v_key) | ||||
->setError($e_name)); | ->setError($e_key)); | ||||
return $this->newDialog() | $errors = array(); | ||||
->setTitle($title) | if ($error !== null) { | ||||
->setErrors($errors) | $errors[] = $error; | ||||
->addHiddenInput('objectPHID', $request->getStr('objectPHID')) | |||||
->appendForm($form) | |||||
->addSubmitButton(pht('Continue')) | |||||
->addCancelButton($cancel_uri); | |||||
} | |||||
} | |||||
// Make sure property key is appropriate. | |||||
// TODO: It would be cleaner to put this safety check in the Editor. | |||||
AlmanacNames::validateName($property_key); | |||||
// If we're adding a new property, put a placeholder on the object so | |||||
// that we can build a CustomField for it. | |||||
if (!$object->hasAlmanacProperty($property_key)) { | |||||
$temporary_property = id(new AlmanacProperty()) | |||||
->setObjectPHID($object->getPHID()) | |||||
->setFieldName($property_key); | |||||
$object->attachAlmanacProperties(array($temporary_property)); | |||||
} | |||||
$field_list = PhabricatorCustomField::getObjectFields( | |||||
$object, | |||||
PhabricatorCustomField::ROLE_DEFAULT); | |||||
// Select only the field being edited. | |||||
$fields = $field_list->getFields(); | |||||
$fields = array_select_keys($fields, array($property_key)); | |||||
$field_list = new PhabricatorCustomFieldList($fields); | |||||
$field_list | |||||
->setViewer($viewer) | |||||
->readFieldsFromStorage($object); | |||||
$validation_exception = null; | |||||
if ($request->isFormPost() && $request->getStr('isValueEdit')) { | |||||
$xactions = $field_list->buildFieldTransactionsFromRequest( | |||||
$object->getApplicationTransactionTemplate(), | |||||
$request); | |||||
$editor = $object->getApplicationTransactionEditor() | |||||
->setActor($viewer) | |||||
->setContentSourceFromRequest($request) | |||||
->setContinueOnNoEffect(true) | |||||
->setContinueOnMissingFields(true); | |||||
try { | |||||
$editor->applyTransactions($object, $xactions); | |||||
return id(new AphrontRedirectResponse())->setURI($cancel_uri); | |||||
} catch (PhabricatorApplicationTransactionValidationException $ex) { | |||||
$validation_exception = $ex; | |||||
} | |||||
} | } | ||||
$form = id(new AphrontFormView()) | |||||
->setUser($viewer) | |||||
->addHiddenInput('objectPHID', $request->getStr('objectPHID')) | |||||
->addHiddenInput('key', $request->getStr('key')) | |||||
->addHiddenInput('name', $property_key) | |||||
->addHiddenInput('isValueEdit', true); | |||||
$field_list->appendFieldsToForm($form); | |||||
return $this->newDialog() | return $this->newDialog() | ||||
->setTitle($title) | ->setTitle(pht('Add Property')) | ||||
->setValidationException($validation_exception) | ->addHiddenInput('objectPHID', $request->getStr('objectPHID')) | ||||
->setErrors($errors) | |||||
->appendForm($form) | ->appendForm($form) | ||||
->addSubmitButton($save_button) | ->addSubmitButton(pht('Continue')) | ||||
->addCancelButton($cancel_uri); | ->addCancelButton($cancel_uri); | ||||
} | } | ||||
} | } |