1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-18 19:40:55 +01:00

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
This commit is contained in:
epriestley 2016-02-22 05:11:02 -08:00
parent ab86523ac4
commit 24104be67d
5 changed files with 103 additions and 149 deletions

View file

@ -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',

View file

@ -1,6 +1,6 @@
<?php
final class AlmanacNamespaceEditController extends AlmanacController {
final class AlmanacNamespaceEditController extends AlmanacNamespaceController {
public function handleRequest(AphrontRequest $request) {
return id(new AlmanacNamespaceEditEngine())

View file

@ -1,142 +1,11 @@
<?php
final class AlmanacNetworkEditController
extends AlmanacNetworkController {
final class AlmanacNetworkEditController extends AlmanacNetworkController {
public function handleRequest(AphrontRequest $request) {
$viewer = $request->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();
}
}

View file

@ -0,0 +1,86 @@
<?php
final class AlmanacNetworkEditEngine
extends PhabricatorEditEngine {
const ENGINECONST = 'almanac.network';
public function isEngineConfigurable() {
return false;
}
public function getEngineName() {
return pht('Almanac Networks');
}
public function getSummaryHeader() {
return pht('Edit Almanac Network Configurations');
}
public function getSummaryText() {
return pht('This engine is used to edit Almanac networks.');
}
public function getEngineApplicationClass() {
return 'PhabricatorAlmanacApplication';
}
protected function newEditableObject() {
return AlmanacNetwork::initializeNewNetwork();
}
protected function newObjectQuery() {
return new AlmanacNetworkQuery();
}
protected function getObjectCreateTitleText($object) {
return pht('Create Network');
}
protected function getObjectCreateButtonText($object) {
return pht('Create Network');
}
protected function getObjectEditTitleText($object) {
return pht('Edit Network: %s', $object->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()),
);
}
}

View file

@ -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();