From e523585811c82ba21892467ed543e4cdcbea36f0 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 21 Mar 2016 17:38:22 -0700 Subject: [PATCH] Allow Nuance item types to provide actions for items Summary: Ref T10537. This allows item types to expose item actions. Eventually these actions might be things like "promote to task", "tweet reply", "ban user forever", etc. For now, provide a simple action which shows a raw item in a dialog. Test Plan: {F1185573} Reviewers: chad Reviewed By: chad Subscribers: Luke081515.2 Maniphest Tasks: T10537 Differential Revision: https://secure.phabricator.com/D15504 --- src/__phutil_library_map__.php | 2 + .../PhabricatorNuanceApplication.php | 2 + .../controller/NuanceItemActionController.php | 26 ++++++++++++ .../controller/NuanceItemViewController.php | 7 ++++ .../nuance/item/NuanceGitHubEventItemType.php | 42 +++++++++++++++++++ .../nuance/item/NuanceItemType.php | 36 ++++++++++++++++ 6 files changed, 115 insertions(+) create mode 100644 src/applications/nuance/controller/NuanceItemActionController.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 74dd5958eb..92290ecd77 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1439,6 +1439,7 @@ phutil_register_library_map(array( 'NuanceImportCursorDataQuery' => 'applications/nuance/query/NuanceImportCursorDataQuery.php', 'NuanceImportCursorPHIDType' => 'applications/nuance/phid/NuanceImportCursorPHIDType.php', 'NuanceItem' => 'applications/nuance/storage/NuanceItem.php', + 'NuanceItemActionController' => 'applications/nuance/controller/NuanceItemActionController.php', 'NuanceItemController' => 'applications/nuance/controller/NuanceItemController.php', 'NuanceItemEditor' => 'applications/nuance/editor/NuanceItemEditor.php', 'NuanceItemListController' => 'applications/nuance/controller/NuanceItemListController.php', @@ -5726,6 +5727,7 @@ phutil_register_library_map(array( 'PhabricatorPolicyInterface', 'PhabricatorApplicationTransactionInterface', ), + 'NuanceItemActionController' => 'NuanceController', 'NuanceItemController' => 'NuanceController', 'NuanceItemEditor' => 'PhabricatorApplicationTransactionEditor', 'NuanceItemListController' => 'NuanceItemController', diff --git a/src/applications/nuance/application/PhabricatorNuanceApplication.php b/src/applications/nuance/application/PhabricatorNuanceApplication.php index 92e89f8255..27f683380f 100644 --- a/src/applications/nuance/application/PhabricatorNuanceApplication.php +++ b/src/applications/nuance/application/PhabricatorNuanceApplication.php @@ -43,6 +43,8 @@ final class PhabricatorNuanceApplication extends PhabricatorApplication { $this->getQueryRoutePattern() => 'NuanceItemListController', 'view/(?P[1-9]\d*)/' => 'NuanceItemViewController', 'manage/(?P[1-9]\d*)/' => 'NuanceItemManageController', + 'action/(?P[1-9]\d*)/(?P[^/]+)/' + => 'NuanceItemActionController', ), 'source/' => array( $this->getQueryRoutePattern() => 'NuanceSourceListController', diff --git a/src/applications/nuance/controller/NuanceItemActionController.php b/src/applications/nuance/controller/NuanceItemActionController.php new file mode 100644 index 0000000000..c64ac5f6ac --- /dev/null +++ b/src/applications/nuance/controller/NuanceItemActionController.php @@ -0,0 +1,26 @@ +getViewer(); + $id = $request->getURIData('id'); + + $item = id(new NuanceItemQuery()) + ->setViewer($viewer) + ->withIDs(array($id)) + ->executeOne(); + if (!$item) { + return new Aphront404Response(); + } + + $action = $request->getURIData('action'); + + $impl = $item->getImplementation(); + $impl->setViewer($viewer); + $impl->setController($this); + + return $impl->buildActionResponse($item, $action); + } + +} diff --git a/src/applications/nuance/controller/NuanceItemViewController.php b/src/applications/nuance/controller/NuanceItemViewController.php index fd8fbc4563..e8e3967545 100644 --- a/src/applications/nuance/controller/NuanceItemViewController.php +++ b/src/applications/nuance/controller/NuanceItemViewController.php @@ -58,6 +58,13 @@ final class NuanceItemViewController extends NuanceController { ->setIcon('fa-cogs') ->setHref($this->getApplicationURI("item/manage/{$id}/"))); + $impl = $item->getImplementation(); + $impl->setViewer($viewer); + + foreach ($impl->getItemActions($item) as $action) { + $curtain->addAction($action); + } + return $curtain; } diff --git a/src/applications/nuance/item/NuanceGitHubEventItemType.php b/src/applications/nuance/item/NuanceGitHubEventItemType.php index 1e6a72b3b3..cec3d366ec 100644 --- a/src/applications/nuance/item/NuanceGitHubEventItemType.php +++ b/src/applications/nuance/item/NuanceGitHubEventItemType.php @@ -144,4 +144,46 @@ final class NuanceGitHubEventItemType return NuanceGitHubRawEvent::newEvent($type, $raw); } + public function getItemActions(NuanceItem $item) { + $actions = array(); + + $actions[] = $this->newItemAction($item, 'raw') + ->setName(pht('View Raw Event')) + ->setWorkflow(true) + ->setIcon('fa-code'); + + return $actions; + } + + protected function handleAction(NuanceItem $item, $action) { + $controller = $this->getController(); + + switch ($action) { + case 'raw': + $raw = array( + 'api.type' => $item->getItemProperty('api.type'), + 'api.raw' => $item->getItemProperty('api.raw'), + ); + + $raw_output = id(new PhutilJSON())->encodeFormatted($raw); + + $raw_box = id(new AphrontFormTextAreaControl()) + ->setCustomClass('PhabricatorMonospaced') + ->setLabel(pht('Raw Event')) + ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL) + ->setValue($raw_output); + + $form = id(new AphrontFormView()) + ->appendChild($raw_box); + + return $controller->newDialog() + ->setWidth(AphrontDialogView::WIDTH_FULL) + ->setTitle(pht('GitHub Raw Event')) + ->appendForm($form) + ->addCancelButton($item->getURI(), pht('Done')); + } + + return null; + } + } diff --git a/src/applications/nuance/item/NuanceItemType.php b/src/applications/nuance/item/NuanceItemType.php index 1e0cc7a95c..2f348dee1f 100644 --- a/src/applications/nuance/item/NuanceItemType.php +++ b/src/applications/nuance/item/NuanceItemType.php @@ -4,6 +4,7 @@ abstract class NuanceItemType extends Phobject { private $viewer; + private $controller; public function setViewer(PhabricatorUser $viewer) { $this->viewer = $viewer; @@ -14,6 +15,15 @@ abstract class NuanceItemType return $this->viewer; } + public function setController(PhabricatorController $controller) { + $this->controller = $controller; + return $this; + } + + public function getController() { + return $this->controller; + } + public function canUpdateItems() { return false; } @@ -30,6 +40,10 @@ abstract class NuanceItemType return null; } + public function getItemActions(NuanceItem $item) { + return array(); + } + abstract public function getItemTypeDisplayName(); abstract public function getItemDisplayName(NuanceItem $item); @@ -60,4 +74,26 @@ abstract class NuanceItemType ->execute(); } + final protected function newItemAction(NuanceItem $item, $key) { + $id = $item->getID(); + $action_uri = "/nuance/item/action/{$id}/{$key}/"; + + return id(new PhabricatorActionView()) + ->setHref($action_uri); + } + + final public function buildActionResponse(NuanceItem $item, $action) { + $response = $this->handleAction($item, $action); + + if ($response === null) { + return new Aphront404Response(); + } + + return $response; + } + + protected function handleAction(NuanceItem $item, $action) { + return null; + } + }