Path: blob/master/src/applications/nuance/controller/NuanceItemActionController.php
12256 views
<?php12final class NuanceItemActionController extends NuanceController {34public function handleRequest(AphrontRequest $request) {5$viewer = $this->getViewer();6$id = $request->getURIData('id');78if (!$request->validateCSRF()) {9return new Aphront400Response();10}1112// NOTE: This controller can be reached from an individual item (usually13// by a user) or while working through a queue (usually by staff). When14// a command originates from a queue, the URI will have a queue ID.1516$item = id(new NuanceItemQuery())17->setViewer($viewer)18->withIDs(array($id))19->executeOne();20if (!$item) {21return new Aphront404Response();22}2324$cancel_uri = $item->getURI();2526$queue_id = $request->getURIData('queueID');27$queue = null;28if ($queue_id) {29$queue = id(new NuanceQueueQuery())30->setViewer($viewer)31->withIDs(array($queue_id))32->executeOne();33if (!$queue) {34return new Aphront404Response();35}3637$item_queue = $item->getQueue();38if (!$item_queue || ($item_queue->getPHID() != $queue->getPHID())) {39return $this->newDialog()40->setTitle(pht('Wrong Queue'))41->appendParagraph(42pht(43'You are trying to act on this item from the wrong queue: it '.44'is currently in a different queue.'))45->addCancelButton($cancel_uri);46}47}4849$action = $request->getURIData('action');5051$impl = $item->getImplementation();52$impl->setViewer($viewer);53$impl->setController($this);5455$executors = NuanceCommandImplementation::getAllCommands();56$executor = idx($executors, $action);57if (!$executor) {58return new Aphront404Response();59}6061$executor = id(clone $executor)62->setActor($viewer);6364if (!$executor->canApplyToItem($item)) {65return $this->newDialog()66->setTitle(pht('Command Not Supported'))67->appendParagraph(68pht(69'This item does not support the specified command ("%s").',70$action))71->addCancelButton($cancel_uri);72}7374$command = NuanceItemCommand::initializeNewCommand()75->setItemPHID($item->getPHID())76->setAuthorPHID($viewer->getPHID())77->setCommand($action);7879if ($queue) {80$command->setQueuePHID($queue->getPHID());81}8283$command->save();8485// If this command can be applied immediately, try to apply it now.8687// In most cases, local commands (like closing an item) can be applied88// immediately.8990// Commands that require making a call to a remote system (for example,91// to reply to a tweet or close a remote object) are usually done in the92// background so the user doesn't have to wait for the operation to93// complete before they can continue work.9495$did_apply = false;96$immediate = $executor->canApplyImmediately($item, $command);97if ($immediate) {98// TODO: Move this stuff to a new Engine, and have the controller and99// worker both call into the Engine.100$worker = new NuanceItemUpdateWorker(array());101$did_apply = $worker->executeCommands($item, array($command));102}103104// If this can't be applied immediately or we were unable to get a lock105// fast enough, do the update in the background instead.106if (!$did_apply) {107$item->scheduleUpdate();108}109110if ($queue) {111$done_uri = $queue->getWorkURI();112} else {113$done_uri = $item->getURI();114}115116return id(new AphrontRedirectResponse())117->setURI($done_uri);118}119120}121122123