Changeset View
Changeset View
Standalone View
Standalone View
src/applications/tokens/editor/PhabricatorTokenGivenEditor.php
<?php | <?php | ||||
final class PhabricatorTokenGivenEditor | final class PhabricatorTokenGivenEditor | ||||
extends PhabricatorEditor { | extends PhabricatorEditor { | ||||
private $contentSource; | private $contentSource; | ||||
private $request; | |||||
private $cancelURI; | |||||
public function setContentSource(PhabricatorContentSource $content_source) { | public function setContentSource(PhabricatorContentSource $content_source) { | ||||
$this->contentSource = $content_source; | $this->contentSource = $content_source; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getContentSource() { | public function getContentSource() { | ||||
return $this->contentSource; | return $this->contentSource; | ||||
} | } | ||||
public function setRequest(AphrontRequest $request) { | |||||
$this->request = $request; | |||||
return $this; | |||||
} | |||||
public function getRequest() { | |||||
return $this->request; | |||||
} | |||||
public function setCancelURI($cancel_uri) { | |||||
$this->cancelURI = $cancel_uri; | |||||
return $this; | |||||
} | |||||
public function getCancelURI() { | |||||
return $this->cancelURI; | |||||
} | |||||
public function addToken($object_phid, $token_phid) { | public function addToken($object_phid, $token_phid) { | ||||
$token = $this->validateToken($token_phid); | $token = $this->validateToken($token_phid); | ||||
$object = $this->validateObject($object_phid); | $object = $this->validateObject($object_phid); | ||||
$current_token = $this->loadCurrentToken($object); | $current_token = $this->loadCurrentToken($object); | ||||
$actor = $this->requireActor(); | $actor = $this->requireActor(); | ||||
$token_given = id(new PhabricatorTokenGiven()) | $token_given = id(new PhabricatorTokenGiven()) | ||||
Show All 11 Lines | $token_given->openTransaction(); | ||||
queryfx( | queryfx( | ||||
$token_given->establishConnection('w'), | $token_given->establishConnection('w'), | ||||
'INSERT INTO %T (objectPHID, tokenCount) VALUES (%s, 1) | 'INSERT INTO %T (objectPHID, tokenCount) VALUES (%s, 1) | ||||
ON DUPLICATE KEY UPDATE tokenCount = tokenCount + 1', | ON DUPLICATE KEY UPDATE tokenCount = tokenCount + 1', | ||||
id(new PhabricatorTokenCount())->getTableName(), | id(new PhabricatorTokenCount())->getTableName(), | ||||
$object->getPHID()); | $object->getPHID()); | ||||
$token_given->saveTransaction(); | |||||
$current_token_phid = null; | $current_token_phid = null; | ||||
if ($current_token) { | if ($current_token) { | ||||
$current_token_phid = $current_token->getTokenPHID(); | $current_token_phid = $current_token->getTokenPHID(); | ||||
} | } | ||||
try { | |||||
$this->publishTransaction( | $this->publishTransaction( | ||||
$object, | $object, | ||||
$current_token_phid, | $current_token_phid, | ||||
$token->getPHID()); | $token->getPHID()); | ||||
} catch (Exception $ex) { | |||||
$token_given->killTransaction(); | |||||
throw $ex; | |||||
} | |||||
$token_given->saveTransaction(); | |||||
$subscribed_phids = $object->getUsersToNotifyOfTokenGiven(); | $subscribed_phids = $object->getUsersToNotifyOfTokenGiven(); | ||||
if ($subscribed_phids) { | if ($subscribed_phids) { | ||||
$related_phids = $subscribed_phids; | $related_phids = $subscribed_phids; | ||||
$related_phids[] = $actor->getPHID(); | $related_phids[] = $actor->getPHID(); | ||||
$story_type = 'PhabricatorTokenGivenFeedStory'; | $story_type = 'PhabricatorTokenGivenFeedStory'; | ||||
$story_data = array( | $story_data = array( | ||||
Show All 18 Lines | final class PhabricatorTokenGivenEditor | ||||
public function deleteToken($object_phid) { | public function deleteToken($object_phid) { | ||||
$object = $this->validateObject($object_phid); | $object = $this->validateObject($object_phid); | ||||
$token_given = $this->loadCurrentToken($object); | $token_given = $this->loadCurrentToken($object); | ||||
if (!$token_given) { | if (!$token_given) { | ||||
return; | return; | ||||
} | } | ||||
$token_given->openTransaction(); | |||||
$this->executeDeleteToken($object, $token_given); | $this->executeDeleteToken($object, $token_given); | ||||
try { | |||||
$this->publishTransaction( | $this->publishTransaction( | ||||
$object, | $object, | ||||
$token_given->getTokenPHID(), | $token_given->getTokenPHID(), | ||||
null); | null); | ||||
} catch (Exception $ex) { | |||||
$token_given->killTransaction(); | |||||
throw $ex; | |||||
} | |||||
$token_given->saveTransaction(); | |||||
} | } | ||||
private function executeDeleteToken( | private function executeDeleteToken( | ||||
PhabricatorTokenReceiverInterface $object, | PhabricatorTokenReceiverInterface $object, | ||||
PhabricatorTokenGiven $token_given) { | PhabricatorTokenGiven $token_given) { | ||||
$token_given->openTransaction(); | $token_given->openTransaction(); | ||||
$token_given->delete(); | $token_given->delete(); | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | $xactions[] = id($object->getApplicationTransactionTemplate()) | ||||
->setNewValue($new_token_phid); | ->setNewValue($new_token_phid); | ||||
$editor = $object->getApplicationTransactionEditor() | $editor = $object->getApplicationTransactionEditor() | ||||
->setActor($actor) | ->setActor($actor) | ||||
->setContentSource($this->getContentSource()) | ->setContentSource($this->getContentSource()) | ||||
->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
->setContinueOnMissingFields(true); | ->setContinueOnMissingFields(true); | ||||
$request = $this->getRequest(); | |||||
if ($request) { | |||||
$editor->setRequest($request); | |||||
} | |||||
$cancel_uri = $this->getCancelURI(); | |||||
if ($cancel_uri) { | |||||
$editor->setCancelURI($cancel_uri); | |||||
} | |||||
$editor->applyTransactions($object, $xactions); | $editor->applyTransactions($object, $xactions); | ||||
} | } | ||||
} | } |