1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-20 17:28:51 +02:00

Use ApplicationSearch in Slowvote

Summary: Ref T2625. Ref T603. Make the vote list policy-aware, mobile-friendly, and use ApplicationSearch.

Test Plan: {F50022}

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T603, T2625

Differential Revision: https://secure.phabricator.com/D6445
This commit is contained in:
epriestley 2013-07-13 10:41:49 -07:00
parent 9be755ab12
commit 0630ffffaa
6 changed files with 183 additions and 178 deletions

View file

@ -1533,6 +1533,7 @@ phutil_register_library_map(array(
'PhabricatorSlowvotePoll' => 'applications/slowvote/storage/PhabricatorSlowvotePoll.php', 'PhabricatorSlowvotePoll' => 'applications/slowvote/storage/PhabricatorSlowvotePoll.php',
'PhabricatorSlowvotePollController' => 'applications/slowvote/controller/PhabricatorSlowvotePollController.php', 'PhabricatorSlowvotePollController' => 'applications/slowvote/controller/PhabricatorSlowvotePollController.php',
'PhabricatorSlowvoteQuery' => 'applications/slowvote/query/PhabricatorSlowvoteQuery.php', 'PhabricatorSlowvoteQuery' => 'applications/slowvote/query/PhabricatorSlowvoteQuery.php',
'PhabricatorSlowvoteSearchEngine' => 'applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php',
'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php', 'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php',
'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php', 'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php',
'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php', 'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php',
@ -3482,7 +3483,11 @@ phutil_register_library_map(array(
'PhabricatorSlowvoteController' => 'PhabricatorController', 'PhabricatorSlowvoteController' => 'PhabricatorController',
'PhabricatorSlowvoteCreateController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteCreateController' => 'PhabricatorSlowvoteController',
'PhabricatorSlowvoteDAO' => 'PhabricatorLiskDAO', 'PhabricatorSlowvoteDAO' => 'PhabricatorLiskDAO',
'PhabricatorSlowvoteListController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteListController' =>
array(
0 => 'PhabricatorSlowvoteController',
1 => 'PhabricatorApplicationSearchResultsControllerInterface',
),
'PhabricatorSlowvoteOption' => 'PhabricatorSlowvoteDAO', 'PhabricatorSlowvoteOption' => 'PhabricatorSlowvoteDAO',
'PhabricatorSlowvotePoll' => 'PhabricatorSlowvotePoll' =>
array( array(
@ -3490,7 +3495,8 @@ phutil_register_library_map(array(
1 => 'PhabricatorPolicyInterface', 1 => 'PhabricatorPolicyInterface',
), ),
'PhabricatorSlowvotePollController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvotePollController' => 'PhabricatorSlowvoteController',
'PhabricatorSlowvoteQuery' => 'PhabricatorPolicyAwareCursorPagedQuery', 'PhabricatorSlowvoteQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorSlowvoteSearchEngine' => 'PhabricatorApplicationSearchEngine',
'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController',
'PhabricatorSlugTestCase' => 'PhabricatorTestCase', 'PhabricatorSlugTestCase' => 'PhabricatorTestCase',
'PhabricatorSortTableExample' => 'PhabricatorUIExample', 'PhabricatorSortTableExample' => 'PhabricatorUIExample',

View file

@ -40,7 +40,8 @@ final class PhabricatorApplicationSlowvote extends PhabricatorApplication {
return array( return array(
'/V(?P<id>[1-9]\d*)' => 'PhabricatorSlowvotePollController', '/V(?P<id>[1-9]\d*)' => 'PhabricatorSlowvotePollController',
'/vote/' => array( '/vote/' => array(
'(?:view/(?P<view>\w+)/)?' => 'PhabricatorSlowvoteListController', '(?:query/(?P<queryKey>[^/]+)/)?'
=> 'PhabricatorSlowvoteListController',
'create/' => 'PhabricatorSlowvoteCreateController', 'create/' => 'PhabricatorSlowvoteCreateController',
'(?P<id>[1-9]\d*)/' => 'PhabricatorSlowvoteVoteController', '(?P<id>[1-9]\d*)/' => 'PhabricatorSlowvoteVoteController',
), ),

View file

@ -5,46 +5,28 @@
*/ */
abstract class PhabricatorSlowvoteController extends PhabricatorController { abstract class PhabricatorSlowvoteController extends PhabricatorController {
const VIEW_ALL = 'all'; public function buildSideNavView($for_app = false) {
const VIEW_CREATED = 'created'; $user = $this->getRequest()->getUser();
const VIEW_VOTED = 'voted';
public function buildStandardPageResponse($view, array $data) { $nav = new AphrontSideNavFilterView();
$page = $this->buildStandardPageView(); $nav->setBaseURI(new PhutilURI($this->getApplicationURI()));
$page->setApplicationName(pht('Slowvote'));
$page->setBaseURI('/vote/');
$page->setTitle(idx($data, 'title'));
$page->setGlyph("\xE2\x9C\x94");
$page->appendChild($view);
$response = new AphrontWebpageResponse();
return $response->setContent($page->render());
}
public function buildSideNavView($filter = null, $for_app = false) {
$views = $this->getViews();
$side_nav = new AphrontSideNavFilterView();
$side_nav->setBaseURI(new PhutilURI('/vote/view/'));
foreach ($views as $key => $name) {
$side_nav->addFilter($key, $name);
}
if ($filter) {
$side_nav->selectFilter($filter, null);
}
if ($for_app) { if ($for_app) {
$side_nav->addFilter('', pht('Create Question'), $nav->addFilter('', pht('Create Poll'),
$this->getApplicationURI('create/')); $this->getApplicationURI('create/'));
} }
return $side_nav; id(new PhabricatorSlowvoteSearchEngine())
->setViewer($user)
->addNavigationItems($nav->getMenu());
$nav->selectFilter(null);
return $nav;
} }
public function buildApplicationMenu() { public function buildApplicationMenu() {
return $this->buildSideNavView(null, true)->getMenu(); return $this->buildSideNavView(true)->getMenu();
} }
public function buildApplicationCrumbs() { public function buildApplicationCrumbs() {
@ -52,19 +34,11 @@ abstract class PhabricatorSlowvoteController extends PhabricatorController {
$crumbs->addAction( $crumbs->addAction(
id(new PHUIListItemView()) id(new PHUIListItemView())
->setName(pht('Create Question')) ->setName(pht('Create Poll'))
->setHref($this->getApplicationURI('create/')) ->setHref($this->getApplicationURI('create/'))
->setIcon('create')); ->setIcon('create'));
return $crumbs; return $crumbs;
} }
public function getViews() {
return array(
self::VIEW_ALL => pht('All Slowvotes'),
self::VIEW_CREATED => pht('Created'),
self::VIEW_VOTED => pht('Voted In'),
);
}
} }

View file

@ -4,154 +4,63 @@
* @group slowvote * @group slowvote
*/ */
final class PhabricatorSlowvoteListController final class PhabricatorSlowvoteListController
extends PhabricatorSlowvoteController { extends PhabricatorSlowvoteController
implements PhabricatorApplicationSearchResultsControllerInterface {
private $view; private $queryKey;
public function shouldAllowPublic() {
return true;
}
public function willProcessRequest(array $data) { public function willProcessRequest(array $data) {
$this->view = idx($data, 'view', parent::VIEW_ALL); $this->queryKey = idx($data, 'queryKey');
} }
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $controller = id(new PhabricatorApplicationSearchController($request))
->setQueryKey($this->queryKey)
->setSearchEngine(new PhabricatorSlowvoteSearchEngine())
->setNavigation($this->buildSideNavView());
$view = $this->view; return $this->delegateToController($controller);
$views = $this->getViews(); }
$side_nav = $this->buildSideNavView($view); public function renderResultsList(
array $polls,
PhabricatorSavedQuery $query) {
assert_instances_of($polls, 'PhabricatorSlowvotePoll');
$viewer = $this->getRequest()->getUser();
$pager = new AphrontPagerView(); $list = id(new PhabricatorObjectItemListView())
$pager->setOffset($request->getInt('page')); ->setUser($viewer);
$pager->setURI($request->getRequestURI(), 'page');
$polls = $this->loadPolls($pager, $view);
$phids = mpull($polls, 'getAuthorPHID'); $phids = mpull($polls, 'getAuthorPHID');
$handles = $this->loadViewerHandles($phids); $handles = $this->loadViewerHandles($phids);
$rows = array();
foreach ($polls as $poll) { foreach ($polls as $poll) {
$rows[] = array( $date_created = phabricator_datetime($poll->getDateCreated(), $viewer);
'V'.$poll->getID(), if ($poll->getAuthorPHID()) {
phutil_tag( $author = $handles[$poll->getAuthorPHID()]->renderLink();
'a', } else {
array( $author = null;
'href' => '/V'.$poll->getID(),
),
$poll->getQuestion()),
$handles[$poll->getAuthorPHID()]->renderLink(),
phabricator_date($poll->getDateCreated(), $user),
phabricator_time($poll->getDateCreated(), $user),
);
} }
$table = new AphrontTableView($rows); $item = id(new PhabricatorObjectItemView())
$table->setColumnClasses( ->setObjectName('V'.$poll->getID())
array( ->setHeader($poll->getQuestion())
'', ->setHref('/V'.$poll->getID())
'pri wide', ->addIcon('none', $date_created);
'',
'',
'right',
));
$table->setHeaders(
array(
pht('ID'),
pht('Poll'),
pht('Author'),
pht('Date'),
pht('Time'),
));
switch ($view) { if ($author) {
case self::VIEW_ALL: $item->addByline(pht('Author: %s', $author));
$table_header =
pht('Slowvotes Not Yet Consumed by the Ravages of Time');
break;
case self::VIEW_CREATED:
$table_header =
pht('Slowvotes Birthed from Your Noblest of Great Minds');
break;
case self::VIEW_VOTED:
$table_header =
pht('Slowvotes Within Which You Express Your Mighty Opinion');
break;
} }
$panel = new AphrontPanelView(); $list->addItem($item);
$panel->setHeader($table_header);
$panel->setNoBackground();
$panel->appendChild($table);
$panel->appendChild($pager);
$side_nav->appendChild($panel);
$crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
$crumbs->addCrumb(
id(new PhabricatorCrumbView())
->setName($views[$view])
->setHref($this->getApplicationURI()));
$side_nav->setCrumbs($crumbs);
return $this->buildApplicationPage(
$side_nav,
array(
'title' => pht('Slowvotes'),
'device' => true,
));
} }
private function loadPolls(AphrontPagerView $pager, $view) { return $list;
$request = $this->getRequest();
$user = $request->getUser();
$poll = new PhabricatorSlowvotePoll();
$conn = $poll->establishConnection('r');
$offset = $pager->getOffset();
$limit = $pager->getPageSize() + 1;
switch ($view) {
case self::VIEW_ALL:
$data = queryfx_all(
$conn,
'SELECT * FROM %T ORDER BY id DESC LIMIT %d, %d',
$poll->getTableName(),
$offset,
$limit);
break;
case self::VIEW_CREATED:
$data = queryfx_all(
$conn,
'SELECT * FROM %T WHERE authorPHID = %s ORDER BY id DESC
LIMIT %d, %d',
$poll->getTableName(),
$user->getPHID(),
$offset,
$limit);
break;
case self::VIEW_VOTED:
$choice = new PhabricatorSlowvoteChoice();
$data = queryfx_all(
$conn,
'SELECT p.* FROM %T p JOIN %T o
ON o.pollID = p.id
WHERE o.authorPHID = %s
GROUP BY p.id
ORDER BY p.id DESC
LIMIT %d, %d',
$poll->getTableName(),
$choice->getTableName(),
$user->getPHID(),
$offset,
$limit);
break;
}
$data = $pager->sliceResults($data);
return $poll->loadAllFromArray($data);
} }
} }

View file

@ -9,6 +9,7 @@ final class PhabricatorSlowvoteQuery
private $ids; private $ids;
private $phids; private $phids;
private $authorPHIDs; private $authorPHIDs;
private $withVotesByViewer;
public function withIDs($ids) { public function withIDs($ids) {
$this->ids = $ids; $this->ids = $ids;
@ -25,14 +26,20 @@ final class PhabricatorSlowvoteQuery
return $this; return $this;
} }
public function withVotesByViewer($with_vote) {
$this->withVotesByViewer = $with_vote;
return $this;
}
public function loadPage() { public function loadPage() {
$table = new PhabricatorSlowvotePoll(); $table = new PhabricatorSlowvotePoll();
$conn_r = $table->establishConnection('r'); $conn_r = $table->establishConnection('r');
$data = queryfx_all( $data = queryfx_all(
$conn_r, $conn_r,
'SELECT * FROM %T %Q %Q %Q', 'SELECT p.* FROM %T p %Q %Q %Q %Q',
$table->getTableName(), $table->getTableName(),
$this->buildJoinsClause($conn_r),
$this->buildWhereClause($conn_r), $this->buildWhereClause($conn_r),
$this->buildOrderClause($conn_r), $this->buildOrderClause($conn_r),
$this->buildLimitClause($conn_r)); $this->buildLimitClause($conn_r));
@ -46,21 +53,21 @@ final class PhabricatorSlowvoteQuery
if ($this->ids) { if ($this->ids) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn_r,
'id IN (%Ld)', 'p.id IN (%Ld)',
$this->ids); $this->ids);
} }
if ($this->phids) { if ($this->phids) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn_r,
'phid IN (%Ls)', 'p.phid IN (%Ls)',
$this->phids); $this->phids);
} }
if ($this->authorPHIDs) { if ($this->authorPHIDs) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn_r,
'authorPHID IN (%Ls)', 'p.authorPHID IN (%Ls)',
$this->authorPHIDs); $this->authorPHIDs);
} }
@ -68,4 +75,22 @@ final class PhabricatorSlowvoteQuery
return $this->formatWhereClause($where); return $this->formatWhereClause($where);
} }
private function buildJoinsClause(AphrontDatabaseConnection $conn_r) {
$joins = array();
if ($this->withVotesByViewer) {
$joins[] = qsprintf(
$conn_r,
'JOIN %T vv ON vv.pollID = p.id AND vv.authorPHID = %s',
id(new PhabricatorSlowvoteChoice())->getTableName(),
$this->getViewer()->getPHID());
}
return implode(' ', $joins);
}
protected function getPagingColumn() {
return 'p.id';
}
} }

View file

@ -0,0 +1,90 @@
<?php
final class PhabricatorSlowvoteSearchEngine
extends PhabricatorApplicationSearchEngine {
public function buildSavedQueryFromRequest(AphrontRequest $request) {
$saved = new PhabricatorSavedQuery();
$saved->setParameter(
'authorPHIDs',
array_values($request->getArr('authors')));
$saved->setParameter('voted', $request->getBool('voted'));
return $saved;
}
public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) {
$query = id(new PhabricatorSlowvoteQuery())
->withAuthorPHIDs($saved->getParameter('authorPHIDs', array()));
if ($saved->getParameter('voted')) {
$query->withVotesByViewer(true);
}
return $query;
}
public function buildSearchForm(
AphrontFormView $form,
PhabricatorSavedQuery $saved_query) {
$phids = $saved_query->getParameter('authorPHIDs', array());
$handles = id(new PhabricatorObjectHandleData($phids))
->setViewer($this->requireViewer())
->loadHandles();
$author_tokens = mpull($handles, 'getFullName', 'getPHID');
$voted = $saved_query->getParameter('voted', false);
$form
->appendChild(
id(new AphrontFormTokenizerControl())
->setDatasource('/typeahead/common/users/')
->setName('authors')
->setLabel(pht('Authors'))
->setValue($author_tokens))
->appendChild(
id(new AphrontFormCheckboxControl())
->addCheckbox(
'voted',
1,
pht("Show only polls I've voted in."),
$voted));
}
protected function getURI($path) {
return '/vote/'.$path;
}
public function getBuiltinQueryNames() {
$names = array(
'all' => pht('All Polls'),
);
if ($this->requireViewer()->isLoggedIn()) {
$names['authored'] = pht('Authored');
$names['voted'] = pht('Voted In');
}
return $names;
}
public function buildSavedQueryFromBuiltin($query_key) {
$query = $this->newSavedQuery();
$query->setQueryKey($query_key);
switch ($query_key) {
case 'all':
return $query;
case 'authored':
return $query->setParameter(
'authorPHIDs',
array($this->requireViewer()->getPHID()));
case 'voted':
return $query->setParameter('voted', true);
}
return parent::buildSavedQueryFromBuiltin($query_key);
}
}