3 final class NuanceItemActionController
extends NuanceController
{
5 public function handleRequest(AphrontRequest
$request) {
6 $viewer = $this->getViewer();
7 $id = $request->getURIData('id');
9 if (!$request->validateCSRF()) {
10 return new Aphront400Response();
13 // NOTE: This controller can be reached from an individual item (usually
14 // by a user) or while working through a queue (usually by staff). When
15 // a command originates from a queue, the URI will have a queue ID.
17 $item = id(new NuanceItemQuery())
22 return new Aphront404Response();
25 $cancel_uri = $item->getURI();
27 $queue_id = $request->getURIData('queueID');
30 $queue = id(new NuanceQueueQuery())
32 ->withIDs(array($queue_id))
35 return new Aphront404Response();
38 $item_queue = $item->getQueue();
39 if (!$item_queue ||
($item_queue->getPHID() != $queue->getPHID())) {
40 return $this->newDialog()
41 ->setTitle(pht('Wrong Queue'))
44 'You are trying to act on this item from the wrong queue: it '.
45 'is currently in a different queue.'))
46 ->addCancelButton($cancel_uri);
50 $action = $request->getURIData('action');
52 $impl = $item->getImplementation();
53 $impl->setViewer($viewer);
54 $impl->setController($this);
56 $executors = NuanceCommandImplementation
::getAllCommands();
57 $executor = idx($executors, $action);
59 return new Aphront404Response();
62 $executor = id(clone $executor)
65 if (!$executor->canApplyToItem($item)) {
66 return $this->newDialog()
67 ->setTitle(pht('Command Not Supported'))
70 'This item does not support the specified command ("%s").',
72 ->addCancelButton($cancel_uri);
75 $command = NuanceItemCommand
::initializeNewCommand()
76 ->setItemPHID($item->getPHID())
77 ->setAuthorPHID($viewer->getPHID())
78 ->setCommand($action);
81 $command->setQueuePHID($queue->getPHID());
86 // If this command can be applied immediately, try to apply it now.
88 // In most cases, local commands (like closing an item) can be applied
91 // Commands that require making a call to a remote system (for example,
92 // to reply to a tweet or close a remote object) are usually done in the
93 // background so the user doesn't have to wait for the operation to
94 // complete before they can continue work.
97 $immediate = $executor->canApplyImmediately($item, $command);
99 // TODO: Move this stuff to a new Engine, and have the controller and
100 // worker both call into the Engine.
101 $worker = new NuanceItemUpdateWorker(array());
102 $did_apply = $worker->executeCommands($item, array($command));
105 // If this can't be applied immediately or we were unable to get a lock
106 // fast enough, do the update in the background instead.
108 $item->scheduleUpdate();
112 $done_uri = $queue->getWorkURI();
114 $done_uri = $item->getURI();
117 return id(new AphrontRedirectResponse())