From 24104be67d1ad804ccc80b7ffc3a5ddba4ba7a3a Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 22 Feb 2016 05:11:02 -0800 Subject: [PATCH] Use EditEngine for Almanac Services, Devices, and Networks Summary: Ref T10411. This cleans up / modernizes things and lets me get an `almanac.network.edit` API in the future. This is mostly straightforward, except that Services have an extra "choose type" screen in front of them. Test Plan: - Created and edited Almanac networks, services, and devices. Reviewers: chad Reviewed By: chad Maniphest Tasks: T10411 Differential Revision: https://secure.phabricator.com/D15326 --- src/__phutil_library_map__.php | 4 +- .../AlmanacNamespaceEditController.php | 2 +- .../AlmanacNetworkEditController.php | 139 +----------------- .../editor/AlmanacNetworkEditEngine.php | 86 +++++++++++ .../storage/AlmanacNetworkTransaction.php | 21 ++- 5 files changed, 103 insertions(+), 149 deletions(-) create mode 100644 src/applications/almanac/editor/AlmanacNetworkEditEngine.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index e4196425e1..29c34e45cc 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -79,6 +79,7 @@ phutil_register_library_map(array( 'AlmanacNetwork' => 'applications/almanac/storage/AlmanacNetwork.php', 'AlmanacNetworkController' => 'applications/almanac/controller/AlmanacNetworkController.php', 'AlmanacNetworkEditController' => 'applications/almanac/controller/AlmanacNetworkEditController.php', + 'AlmanacNetworkEditEngine' => 'applications/almanac/editor/AlmanacNetworkEditEngine.php', 'AlmanacNetworkEditor' => 'applications/almanac/editor/AlmanacNetworkEditor.php', 'AlmanacNetworkListController' => 'applications/almanac/controller/AlmanacNetworkListController.php', 'AlmanacNetworkNameNgrams' => 'applications/almanac/storage/AlmanacNetworkNameNgrams.php', @@ -4072,7 +4073,7 @@ phutil_register_library_map(array( 'PhabricatorNgramsInterface', ), 'AlmanacNamespaceController' => 'AlmanacController', - 'AlmanacNamespaceEditController' => 'AlmanacController', + 'AlmanacNamespaceEditController' => 'AlmanacNamespaceController', 'AlmanacNamespaceEditEngine' => 'PhabricatorEditEngine', 'AlmanacNamespaceEditor' => 'PhabricatorApplicationTransactionEditor', 'AlmanacNamespaceListController' => 'AlmanacNamespaceController', @@ -4092,6 +4093,7 @@ phutil_register_library_map(array( ), 'AlmanacNetworkController' => 'AlmanacController', 'AlmanacNetworkEditController' => 'AlmanacNetworkController', + 'AlmanacNetworkEditEngine' => 'PhabricatorEditEngine', 'AlmanacNetworkEditor' => 'PhabricatorApplicationTransactionEditor', 'AlmanacNetworkListController' => 'AlmanacNetworkController', 'AlmanacNetworkNameNgrams' => 'PhabricatorSearchNgrams', diff --git a/src/applications/almanac/controller/AlmanacNamespaceEditController.php b/src/applications/almanac/controller/AlmanacNamespaceEditController.php index 1f3cf84e52..0bb6ec2620 100644 --- a/src/applications/almanac/controller/AlmanacNamespaceEditController.php +++ b/src/applications/almanac/controller/AlmanacNamespaceEditController.php @@ -1,6 +1,6 @@ getViewer(); - - $list_uri = $this->getApplicationURI('network/'); - - $id = $request->getURIData('id'); - if ($id) { - $network = id(new AlmanacNetworkQuery()) - ->setViewer($viewer) - ->withIDs(array($id)) - ->requireCapabilities( - array( - PhabricatorPolicyCapability::CAN_VIEW, - PhabricatorPolicyCapability::CAN_EDIT, - )) - ->executeOne(); - if (!$network) { - return new Aphront404Response(); - } - - $is_new = false; - $network_uri = $this->getApplicationURI('network/'.$network->getID().'/'); - $cancel_uri = $network_uri; - $title = pht('Edit Network'); - $save_button = pht('Save Changes'); - } else { - $this->requireApplicationCapability( - AlmanacCreateNetworksCapability::CAPABILITY); - - $network = AlmanacNetwork::initializeNewNetwork(); - $is_new = true; - - $cancel_uri = $list_uri; - $title = pht('Create Network'); - $save_button = pht('Create Network'); - } - - $v_name = $network->getName(); - $e_name = true; - $validation_exception = null; - - if ($request->isFormPost()) { - $v_name = $request->getStr('name'); - $v_view = $request->getStr('viewPolicy'); - $v_edit = $request->getStr('editPolicy'); - - $type_name = AlmanacNetworkTransaction::TYPE_NAME; - $type_view = PhabricatorTransactions::TYPE_VIEW_POLICY; - $type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY; - - $xactions = array(); - - $xactions[] = id(new AlmanacNetworkTransaction()) - ->setTransactionType($type_name) - ->setNewValue($v_name); - - $xactions[] = id(new AlmanacNetworkTransaction()) - ->setTransactionType($type_view) - ->setNewValue($v_view); - - $xactions[] = id(new AlmanacNetworkTransaction()) - ->setTransactionType($type_edit) - ->setNewValue($v_edit); - - $editor = id(new AlmanacNetworkEditor()) - ->setActor($viewer) - ->setContentSourceFromRequest($request) - ->setContinueOnNoEffect(true); - - try { - $editor->applyTransactions($network, $xactions); - - $id = $network->getID(); - $network_uri = $this->getApplicationURI("network/{$id}/"); - return id(new AphrontRedirectResponse())->setURI($network_uri); - } catch (PhabricatorApplicationTransactionValidationException $ex) { - $validation_exception = $ex; - $e_name = $ex->getShortMessage($type_name); - - $network->setViewPolicy($v_view); - $network->setEditPolicy($v_edit); - } - } - - $policies = id(new PhabricatorPolicyQuery()) - ->setViewer($viewer) - ->setObject($network) - ->execute(); - - $form = id(new AphrontFormView()) - ->setUser($viewer) - ->appendChild( - id(new AphrontFormTextControl()) - ->setLabel(pht('Name')) - ->setName('name') - ->setValue($v_name) - ->setError($e_name)) - ->appendChild( - id(new AphrontFormPolicyControl()) - ->setName('viewPolicy') - ->setPolicyObject($network) - ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) - ->setPolicies($policies)) - ->appendChild( - id(new AphrontFormPolicyControl()) - ->setName('editPolicy') - ->setPolicyObject($network) - ->setCapability(PhabricatorPolicyCapability::CAN_EDIT) - ->setPolicies($policies)) - ->appendChild( - id(new AphrontFormSubmitControl()) - ->addCancelButton($cancel_uri) - ->setValue($save_button)); - - $box = id(new PHUIObjectBoxView()) - ->setValidationException($validation_exception) - ->setHeaderText($title) - ->setForm($form); - - $crumbs = $this->buildApplicationCrumbs(); - if ($is_new) { - $crumbs->addTextCrumb(pht('Create Network')); - } else { - $crumbs->addTextCrumb($network->getName(), $network_uri); - $crumbs->addTextCrumb(pht('Edit')); - } - - return $this->newPage() - ->setTitle($title) - ->setCrumbs($crumbs) - ->appendChild( - array( - $box, - )); + return id(new AlmanacNetworkEditEngine()) + ->setController($this) + ->buildResponse(); } } diff --git a/src/applications/almanac/editor/AlmanacNetworkEditEngine.php b/src/applications/almanac/editor/AlmanacNetworkEditEngine.php new file mode 100644 index 0000000000..a99079ff11 --- /dev/null +++ b/src/applications/almanac/editor/AlmanacNetworkEditEngine.php @@ -0,0 +1,86 @@ +getName()); + } + + protected function getObjectEditShortText($object) { + return pht('Edit Network'); + } + + protected function getObjectCreateShortText() { + return pht('Create Network'); + } + + protected function getEditorURI() { + return '/almanac/network/edit/'; + } + + protected function getObjectCreateCancelURI($object) { + return '/almanac/network/'; + } + + protected function getObjectViewURI($object) { + $id = $object->getID(); + return "/almanac/network/{$id}/"; + } + + protected function getCreateNewObjectPolicy() { + return $this->getApplication()->getPolicy( + AlmanacCreateNetworksCapability::CAPABILITY); + } + + protected function buildCustomEditFields($object) { + return array( + id(new PhabricatorTextEditField()) + ->setKey('name') + ->setLabel(pht('Name')) + ->setDescription(pht('Name of the network.')) + ->setTransactionType(AlmanacNetworkTransaction::TYPE_NAME) + ->setIsRequired(true) + ->setValue($object->getName()), + ); + } + +} diff --git a/src/applications/almanac/storage/AlmanacNetworkTransaction.php b/src/applications/almanac/storage/AlmanacNetworkTransaction.php index 7d2f81c984..f8c2b43b0b 100644 --- a/src/applications/almanac/storage/AlmanacNetworkTransaction.php +++ b/src/applications/almanac/storage/AlmanacNetworkTransaction.php @@ -24,19 +24,16 @@ final class AlmanacNetworkTransaction $new = $this->getNewValue(); switch ($this->getTransactionType()) { + case PhabricatorTransactions::TYPE_CREATE: + return pht( + '%s created this network.', + $this->renderHandleLink($author_phid)); case self::TYPE_NAME: - if ($old === null) { - return pht( - '%s created this network.', - $this->renderHandleLink($author_phid)); - } else { - return pht( - '%s renamed this network from "%s" to "%s".', - $this->renderHandleLink($author_phid), - $old, - $new); - } - break; + return pht( + '%s renamed this network from "%s" to "%s".', + $this->renderHandleLink($author_phid), + $old, + $new); } return parent::getTitle();