1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-20 13:52:40 +01:00

Add PhabricatorSlowvoteQuery

Summary: Ref T603. Ref T2625. Start pushing Slowvote (the greatest app of all time) into the modern era.

Test Plan: Looked at vote detail. Used `V1` and `{V1}` embeds.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T603, T2625

Differential Revision: https://secure.phabricator.com/D6444
This commit is contained in:
epriestley 2013-07-13 10:41:30 -07:00
parent 0fe18f5460
commit 9be755ab12
6 changed files with 116 additions and 8 deletions

View file

@ -1532,6 +1532,7 @@ phutil_register_library_map(array(
'PhabricatorSlowvoteOption' => 'applications/slowvote/storage/PhabricatorSlowvoteOption.php',
'PhabricatorSlowvotePoll' => 'applications/slowvote/storage/PhabricatorSlowvotePoll.php',
'PhabricatorSlowvotePollController' => 'applications/slowvote/controller/PhabricatorSlowvotePollController.php',
'PhabricatorSlowvoteQuery' => 'applications/slowvote/query/PhabricatorSlowvoteQuery.php',
'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php',
'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php',
'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php',
@ -3483,8 +3484,13 @@ phutil_register_library_map(array(
'PhabricatorSlowvoteDAO' => 'PhabricatorLiskDAO',
'PhabricatorSlowvoteListController' => 'PhabricatorSlowvoteController',
'PhabricatorSlowvoteOption' => 'PhabricatorSlowvoteDAO',
'PhabricatorSlowvotePoll' => 'PhabricatorSlowvoteDAO',
'PhabricatorSlowvotePoll' =>
array(
0 => 'PhabricatorSlowvoteDAO',
1 => 'PhabricatorPolicyInterface',
),
'PhabricatorSlowvotePollController' => 'PhabricatorSlowvoteController',
'PhabricatorSlowvoteQuery' => 'PhabricatorPolicyAwareCursorPagedQuery',
'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController',
'PhabricatorSlugTestCase' => 'PhabricatorTestCase',
'PhabricatorSortTableExample' => 'PhabricatorUIExample',

View file

@ -18,7 +18,10 @@ final class PhabricatorSlowvotePollController
$user = $request->getUser();
$viewer_phid = $user->getPHID();
$poll = id(new PhabricatorSlowvotePoll())->load($this->id);
$poll = id(new PhabricatorSlowvoteQuery())
->setViewer($user)
->withIDs(array($this->id))
->executeOne();
if (!$poll) {
return new Aphront404Response();
}

View file

@ -16,7 +16,14 @@ final class PhabricatorSlowvoteVoteController
$request = $this->getRequest();
$user = $request->getUser();
$poll = id(new PhabricatorSlowvotePoll())->load($this->id);
$poll = id(new PhabricatorSlowvoteQuery())
->setViewer($user)
->withIDs(array($this->id))
->executeOne();
if (!$poll) {
return new Aphront404Response();
}
$options = id(new PhabricatorSlowvoteOption())->loadAllWhere(
'pollID = %d',
$poll->getID());

View file

@ -0,0 +1,71 @@
<?php
/**
* @group slowvote
*/
final class PhabricatorSlowvoteQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $ids;
private $phids;
private $authorPHIDs;
public function withIDs($ids) {
$this->ids = $ids;
return $this;
}
public function withPHIDs($phids) {
$this->phids = $phids;
return $this;
}
public function withAuthorPHIDs($author_phids) {
$this->authorPHIDs = $author_phids;
return $this;
}
public function loadPage() {
$table = new PhabricatorSlowvotePoll();
$conn_r = $table->establishConnection('r');
$data = queryfx_all(
$conn_r,
'SELECT * FROM %T %Q %Q %Q',
$table->getTableName(),
$this->buildWhereClause($conn_r),
$this->buildOrderClause($conn_r),
$this->buildLimitClause($conn_r));
return $table->loadAllFromArray($data);
}
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
$where = array();
if ($this->ids) {
$where[] = qsprintf(
$conn_r,
'id IN (%Ld)',
$this->ids);
}
if ($this->phids) {
$where[] = qsprintf(
$conn_r,
'phid IN (%Ls)',
$this->phids);
}
if ($this->authorPHIDs) {
$where[] = qsprintf(
$conn_r,
'authorPHID IN (%Ls)',
$this->authorPHIDs);
}
$where[] = $this->buildPagingClause($conn_r);
return $this->formatWhereClause($where);
}
}

View file

@ -11,10 +11,12 @@ final class SlowvoteRemarkupRule
}
protected function loadObjects(array $ids) {
$polls = array(id(new PhabricatorSlowvotePoll())->load(head($ids)));
$viewer = $this->getEngine()->getConfig('viewer');
return id(new PhabricatorSlowvotePoll())
->loadAllWhere('id IN (%Ld)', $ids);
return id(new PhabricatorSlowvoteQuery())
->setViewer($viewer)
->withIDs($ids)
->execute();
}
protected function renderObjectEmbed($object, $handle, $options) {

View file

@ -3,7 +3,8 @@
/**
* @group slowvote
*/
final class PhabricatorSlowvotePoll extends PhabricatorSlowvoteDAO {
final class PhabricatorSlowvotePoll extends PhabricatorSlowvoteDAO
implements PhabricatorPolicyInterface {
const RESPONSES_VISIBLE = 0;
const RESPONSES_VOTERS = 1;
@ -13,7 +14,6 @@ final class PhabricatorSlowvotePoll extends PhabricatorSlowvoteDAO {
const METHOD_APPROVAL = 1;
protected $question;
protected $phid;
protected $authorPHID;
protected $responseVisibility;
protected $shuffle;
@ -30,4 +30,23 @@ final class PhabricatorSlowvotePoll extends PhabricatorSlowvoteDAO {
PhabricatorPHIDConstants::PHID_TYPE_POLL);
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
);
}
public function getPolicy($capability) {
return PhabricatorPolicies::POLICY_USER;
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return ($viewer->getPHID() == $this->getAuthorPHID());
}
}