@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use EditEngine for AlmanacDevice

Summary: Ref T10449. Modernize the AlmanacDevice code a bit.

Test Plan:
- Created a device.
- Edited a device.
- Listed devices.
- Viewed a device.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T10449

Differential Revision: https://secure.phabricator.com/D15399

+102 -187
+2
src/__phutil_library_map__.php
··· 36 36 'AlmanacDevice' => 'applications/almanac/storage/AlmanacDevice.php', 37 37 'AlmanacDeviceController' => 'applications/almanac/controller/AlmanacDeviceController.php', 38 38 'AlmanacDeviceEditController' => 'applications/almanac/controller/AlmanacDeviceEditController.php', 39 + 'AlmanacDeviceEditEngine' => 'applications/almanac/editor/AlmanacDeviceEditEngine.php', 39 40 'AlmanacDeviceEditor' => 'applications/almanac/editor/AlmanacDeviceEditor.php', 40 41 'AlmanacDeviceListController' => 'applications/almanac/controller/AlmanacDeviceListController.php', 41 42 'AlmanacDeviceNameNgrams' => 'applications/almanac/storage/AlmanacDeviceNameNgrams.php', ··· 4045 4046 ), 4046 4047 'AlmanacDeviceController' => 'AlmanacController', 4047 4048 'AlmanacDeviceEditController' => 'AlmanacDeviceController', 4049 + 'AlmanacDeviceEditEngine' => 'PhabricatorEditEngine', 4048 4050 'AlmanacDeviceEditor' => 'AlmanacEditor', 4049 4051 'AlmanacDeviceListController' => 'AlmanacDeviceController', 4050 4052 'AlmanacDeviceNameNgrams' => 'PhabricatorSearchNgrams',
+1 -1
src/applications/almanac/application/PhabricatorAlmanacApplication.php
··· 50 50 ), 51 51 '(?P<objectType>device)/' => array( 52 52 $this->getQueryRoutePattern() => 'AlmanacDeviceListController', 53 - 'edit/(?:(?P<id>\d+)/)?' => 'AlmanacDeviceEditController', 53 + $this->getEditRoutePattern('edit/') => 'AlmanacDeviceEditController', 54 54 'view/(?P<name>[^/]+)/' => 'AlmanacDeviceViewController', 55 55 ), 56 56 'interface/' => array(
+5
src/applications/almanac/controller/AlmanacDeviceController.php
··· 2 2 3 3 abstract class AlmanacDeviceController extends AlmanacController { 4 4 5 + public function buildApplicationMenu() { 6 + return $this->newApplicationMenu() 7 + ->setSearchEngine(new AlmanacDeviceSearchEngine()); 8 + } 9 + 5 10 protected function buildApplicationCrumbs() { 6 11 $crumbs = parent::buildApplicationCrumbs(); 7 12
+3 -154
src/applications/almanac/controller/AlmanacDeviceEditController.php
··· 4 4 extends AlmanacDeviceController { 5 5 6 6 public function handleRequest(AphrontRequest $request) { 7 - $viewer = $request->getViewer(); 8 - 9 - $list_uri = $this->getApplicationURI('device/'); 10 - 11 - $id = $request->getURIData('id'); 12 - if ($id) { 13 - $device = id(new AlmanacDeviceQuery()) 14 - ->setViewer($viewer) 15 - ->withIDs(array($id)) 16 - ->requireCapabilities( 17 - array( 18 - PhabricatorPolicyCapability::CAN_VIEW, 19 - PhabricatorPolicyCapability::CAN_EDIT, 20 - )) 21 - ->executeOne(); 22 - if (!$device) { 23 - return new Aphront404Response(); 24 - } 25 - 26 - $is_new = false; 27 - $device_uri = $device->getURI(); 28 - $cancel_uri = $device_uri; 29 - $title = pht('Edit Device'); 30 - $save_button = pht('Save Changes'); 31 - } else { 32 - $this->requireApplicationCapability( 33 - AlmanacCreateDevicesCapability::CAPABILITY); 34 - 35 - $device = AlmanacDevice::initializeNewDevice(); 36 - $is_new = true; 37 - 38 - $cancel_uri = $list_uri; 39 - $title = pht('Create Device'); 40 - $save_button = pht('Create Device'); 41 - } 42 - 43 - $v_name = $device->getName(); 44 - $e_name = true; 45 - $validation_exception = null; 46 - 47 - if ($is_new) { 48 - $v_projects = array(); 49 - } else { 50 - $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs( 51 - $device->getPHID(), 52 - PhabricatorProjectObjectHasProjectEdgeType::EDGECONST); 53 - $v_projects = array_reverse($v_projects); 54 - } 55 - 56 - if ($request->isFormPost()) { 57 - $v_name = $request->getStr('name'); 58 - $v_view = $request->getStr('viewPolicy'); 59 - $v_edit = $request->getStr('editPolicy'); 60 - $v_projects = $request->getArr('projects'); 61 - 62 - $type_name = AlmanacDeviceTransaction::TYPE_NAME; 63 - $type_view = PhabricatorTransactions::TYPE_VIEW_POLICY; 64 - $type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY; 65 - 66 - $xactions = array(); 67 - 68 - $xactions[] = id(new AlmanacDeviceTransaction()) 69 - ->setTransactionType($type_name) 70 - ->setNewValue($v_name); 71 - 72 - $xactions[] = id(new AlmanacDeviceTransaction()) 73 - ->setTransactionType($type_view) 74 - ->setNewValue($v_view); 75 - 76 - $xactions[] = id(new AlmanacDeviceTransaction()) 77 - ->setTransactionType($type_edit) 78 - ->setNewValue($v_edit); 79 - 80 - $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST; 81 - $xactions[] = id(new AlmanacDeviceTransaction()) 82 - ->setTransactionType(PhabricatorTransactions::TYPE_EDGE) 83 - ->setMetadataValue('edge:type', $proj_edge_type) 84 - ->setNewValue(array('=' => array_fuse($v_projects))); 85 - 86 - $editor = id(new AlmanacDeviceEditor()) 87 - ->setActor($viewer) 88 - ->setContentSourceFromRequest($request) 89 - ->setContinueOnNoEffect(true); 90 - 91 - try { 92 - $editor->applyTransactions($device, $xactions); 93 - 94 - $device_uri = $device->getURI(); 95 - return id(new AphrontRedirectResponse())->setURI($device_uri); 96 - } catch (PhabricatorApplicationTransactionValidationException $ex) { 97 - $validation_exception = $ex; 98 - $e_name = $ex->getShortMessage($type_name); 99 - 100 - $device->setViewPolicy($v_view); 101 - $device->setEditPolicy($v_edit); 102 - } 103 - } 104 - 105 - $policies = id(new PhabricatorPolicyQuery()) 106 - ->setViewer($viewer) 107 - ->setObject($device) 108 - ->execute(); 109 - 110 - $form = id(new AphrontFormView()) 111 - ->setUser($viewer) 112 - ->appendChild( 113 - id(new AphrontFormTextControl()) 114 - ->setLabel(pht('Name')) 115 - ->setName('name') 116 - ->setValue($v_name) 117 - ->setError($e_name)) 118 - ->appendChild( 119 - id(new AphrontFormPolicyControl()) 120 - ->setName('viewPolicy') 121 - ->setPolicyObject($device) 122 - ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) 123 - ->setPolicies($policies)) 124 - ->appendChild( 125 - id(new AphrontFormPolicyControl()) 126 - ->setName('editPolicy') 127 - ->setPolicyObject($device) 128 - ->setCapability(PhabricatorPolicyCapability::CAN_EDIT) 129 - ->setPolicies($policies)) 130 - ->appendControl( 131 - id(new AphrontFormTokenizerControl()) 132 - ->setLabel(pht('Projects')) 133 - ->setName('projects') 134 - ->setValue($v_projects) 135 - ->setDatasource(new PhabricatorProjectDatasource())) 136 - ->appendChild( 137 - id(new AphrontFormSubmitControl()) 138 - ->addCancelButton($cancel_uri) 139 - ->setValue($save_button)); 140 - 141 - $box = id(new PHUIObjectBoxView()) 142 - ->setValidationException($validation_exception) 143 - ->setHeaderText($title) 144 - ->setForm($form); 145 - 146 - $crumbs = $this->buildApplicationCrumbs(); 147 - if ($is_new) { 148 - $crumbs->addTextCrumb(pht('Create Device')); 149 - } else { 150 - $crumbs->addTextCrumb($device->getName(), $device_uri); 151 - $crumbs->addTextCrumb(pht('Edit')); 152 - } 153 - 154 - return $this->newPage() 155 - ->setTitle($title) 156 - ->setCrumbs($crumbs) 157 - ->appendChild( 158 - array( 159 - $box, 160 - )); 7 + return id(new AlmanacDeviceEditEngine()) 8 + ->setController($this) 9 + ->buildResponse(); 161 10 } 162 11 163 12 }
+6 -32
src/applications/almanac/controller/AlmanacDeviceListController.php
··· 8 8 } 9 9 10 10 public function handleRequest(AphrontRequest $request) { 11 - $controller = id(new PhabricatorApplicationSearchController()) 12 - ->setQueryKey($request->getURIData('queryKey')) 13 - ->setSearchEngine(new AlmanacDeviceSearchEngine()) 14 - ->setNavigation($this->buildSideNavView()); 15 - 16 - return $this->delegateToController($controller); 11 + return id(new AlmanacDeviceSearchEngine()) 12 + ->setController($this) 13 + ->buildResponse(); 17 14 } 18 15 19 16 protected function buildApplicationCrumbs() { 20 17 $crumbs = parent::buildApplicationCrumbs(); 21 18 22 - $can_create = $this->hasApplicationCapability( 23 - AlmanacCreateDevicesCapability::CAPABILITY); 24 - 25 - $crumbs->addAction( 26 - id(new PHUIListItemView()) 27 - ->setName(pht('Create Device')) 28 - ->setHref($this->getApplicationURI('device/edit/')) 29 - ->setIcon('fa-plus-square') 30 - ->setDisabled(!$can_create) 31 - ->setWorkflow(!$can_create)); 19 + id(new AlmanacDeviceEditEngine()) 20 + ->setViewer($this->getViewer()) 21 + ->addActionToCrumbs($crumbs); 32 22 33 23 return $crumbs; 34 24 } 35 - 36 - public function buildSideNavView() { 37 - $viewer = $this->getViewer(); 38 - 39 - $nav = new AphrontSideNavFilterView(); 40 - $nav->setBaseURI(new PhutilURI($this->getApplicationURI())); 41 - 42 - id(new AlmanacDeviceSearchEngine()) 43 - ->setViewer($viewer) 44 - ->addNavigationItems($nav->getMenu()); 45 - 46 - $nav->selectFilter(null); 47 - 48 - return $nav; 49 - } 50 - 51 25 52 26 }
+85
src/applications/almanac/editor/AlmanacDeviceEditEngine.php
··· 1 + <?php 2 + 3 + final class AlmanacDeviceEditEngine 4 + extends PhabricatorEditEngine { 5 + 6 + const ENGINECONST = 'almanac.device'; 7 + 8 + public function isEngineConfigurable() { 9 + return false; 10 + } 11 + 12 + public function getEngineName() { 13 + return pht('Almanac Devices'); 14 + } 15 + 16 + public function getSummaryHeader() { 17 + return pht('Edit Almanac Device Configurations'); 18 + } 19 + 20 + public function getSummaryText() { 21 + return pht('This engine is used to edit Almanac devices.'); 22 + } 23 + 24 + public function getEngineApplicationClass() { 25 + return 'PhabricatorAlmanacApplication'; 26 + } 27 + 28 + protected function newEditableObject() { 29 + return AlmanacDevice::initializeNewDevice(); 30 + } 31 + 32 + protected function newObjectQuery() { 33 + return new AlmanacDeviceQuery(); 34 + } 35 + 36 + protected function getObjectCreateTitleText($object) { 37 + return pht('Create Device'); 38 + } 39 + 40 + protected function getObjectCreateButtonText($object) { 41 + return pht('Create Device'); 42 + } 43 + 44 + protected function getObjectEditTitleText($object) { 45 + return pht('Edit Device: %s', $object->getName()); 46 + } 47 + 48 + protected function getObjectEditShortText($object) { 49 + return pht('Edit Device'); 50 + } 51 + 52 + protected function getObjectCreateShortText() { 53 + return pht('Create Device'); 54 + } 55 + 56 + protected function getEditorURI() { 57 + return '/almanac/device/edit/'; 58 + } 59 + 60 + protected function getObjectCreateCancelURI($object) { 61 + return '/almanac/device/'; 62 + } 63 + 64 + protected function getObjectViewURI($object) { 65 + return $object->getURI(); 66 + } 67 + 68 + protected function getCreateNewObjectPolicy() { 69 + return $this->getApplication()->getPolicy( 70 + AlmanacCreateDevicesCapability::CAPABILITY); 71 + } 72 + 73 + protected function buildCustomEditFields($object) { 74 + return array( 75 + id(new PhabricatorTextEditField()) 76 + ->setKey('name') 77 + ->setLabel(pht('Name')) 78 + ->setDescription(pht('Name of the device.')) 79 + ->setTransactionType(AlmanacDeviceTransaction::TYPE_NAME) 80 + ->setIsRequired(true) 81 + ->setValue($object->getName()), 82 + ); 83 + } 84 + 85 + }