[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorSubscriptionsEditController 4 extends PhabricatorController { 5 6 private $phid; 7 private $action; 8 9 public function willProcessRequest(array $data) { 10 $this->phid = idx($data, 'phid'); 11 $this->action = idx($data, 'action'); 12 } 13 14 public function processRequest() { 15 $request = $this->getRequest(); 16 17 if (!$request->isFormPost()) { 18 return new Aphront400Response(); 19 } 20 21 switch ($this->action) { 22 case 'add': 23 $is_add = true; 24 break; 25 case 'delete': 26 $is_add = false; 27 break; 28 default: 29 return new Aphront400Response(); 30 } 31 32 $user = $request->getUser(); 33 $phid = $this->phid; 34 35 $handle = id(new PhabricatorHandleQuery()) 36 ->setViewer($user) 37 ->withPHIDs(array($phid)) 38 ->executeOne(); 39 40 if (phid_get_type($phid) == PhabricatorProjectProjectPHIDType::TYPECONST) { 41 // TODO: This is a big hack, but a weak argument for adding some kind 42 // of "load for role" feature to ObjectQuery, and also not a really great 43 // argument for adding some kind of "load extra stuff" feature to 44 // SubscriberInterface. Do this for now and wait for the best way forward 45 // to become more clear? 46 47 $object = id(new PhabricatorProjectQuery()) 48 ->setViewer($user) 49 ->withPHIDs(array($phid)) 50 ->needWatchers(true) 51 ->executeOne(); 52 } else { 53 $object = id(new PhabricatorObjectQuery()) 54 ->setViewer($user) 55 ->withPHIDs(array($phid)) 56 ->executeOne(); 57 } 58 59 if (!($object instanceof PhabricatorSubscribableInterface)) { 60 return $this->buildErrorResponse( 61 pht('Bad Object'), 62 pht('This object is not subscribable.'), 63 $handle->getURI()); 64 } 65 66 if ($object->isAutomaticallySubscribed($user->getPHID())) { 67 return $this->buildErrorResponse( 68 pht('Automatically Subscribed'), 69 pht('You are automatically subscribed to this object.'), 70 $handle->getURI()); 71 } 72 73 if (!$object->shouldAllowSubscription($user->getPHID())) { 74 return $this->buildErrorResponse( 75 pht('You Can Not Subscribe'), 76 pht('You can not subscribe to this object.'), 77 $handle->getURI()); 78 } 79 80 if ($object instanceof PhabricatorApplicationTransactionInterface) { 81 if ($is_add) { 82 $xaction_value = array( 83 '+' => array($user->getPHID()), 84 ); 85 } else { 86 $xaction_value = array( 87 '-' => array($user->getPHID()), 88 ); 89 } 90 91 $xaction = id($object->getApplicationTransactionTemplate()) 92 ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS) 93 ->setNewValue($xaction_value); 94 95 $editor = id($object->getApplicationTransactionEditor()) 96 ->setActor($user) 97 ->setContinueOnNoEffect(true) 98 ->setContentSourceFromRequest($request); 99 100 $editor->applyTransactions( 101 $object->getApplicationTransactionObject(), 102 array($xaction)); 103 } else { 104 105 // TODO: Eventually, get rid of this once everything implements 106 // PhabriatorApplicationTransactionInterface. 107 108 $editor = id(new PhabricatorSubscriptionsEditor()) 109 ->setActor($user) 110 ->setObject($object); 111 112 if ($is_add) { 113 $editor->subscribeExplicit(array($user->getPHID()), $explicit = true); 114 } else { 115 $editor->unsubscribe(array($user->getPHID())); 116 } 117 118 $editor->save(); 119 } 120 121 // TODO: We should just render the "Unsubscribe" action and swap it out 122 // in the document for Ajax requests. 123 return id(new AphrontReloadResponse())->setURI($handle->getURI()); 124 } 125 126 private function buildErrorResponse($title, $message, $uri) { 127 $request = $this->getRequest(); 128 $user = $request->getUser(); 129 130 $dialog = id(new AphrontDialogView()) 131 ->setUser($user) 132 ->setTitle($title) 133 ->appendChild($message) 134 ->addCancelButton($uri); 135 136 return id(new AphrontDialogResponse())->setDialog($dialog); 137 } 138 139 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |