1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-14 02:42:40 +01:00

First stab at a badges typeahead

Summary: Ref T10702

Test Plan: Open a user profile, attempt to award an archived or previously awarded badge, badges dialog should provide a typeahead, and the suggestions should offer details about whether a badge is archived or already awarded.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Maniphest Tasks: T10702

Differential Revision: https://secure.phabricator.com/D15665
This commit is contained in:
lkassianik 2016-04-09 11:40:02 -07:00
parent 6b40cfaa60
commit 85d2fda082
3 changed files with 97 additions and 34 deletions

View file

@ -1880,6 +1880,7 @@ phutil_register_library_map(array(
'PhabricatorBadgesController' => 'applications/badges/controller/PhabricatorBadgesController.php', 'PhabricatorBadgesController' => 'applications/badges/controller/PhabricatorBadgesController.php',
'PhabricatorBadgesCreateCapability' => 'applications/badges/capability/PhabricatorBadgesCreateCapability.php', 'PhabricatorBadgesCreateCapability' => 'applications/badges/capability/PhabricatorBadgesCreateCapability.php',
'PhabricatorBadgesDAO' => 'applications/badges/storage/PhabricatorBadgesDAO.php', 'PhabricatorBadgesDAO' => 'applications/badges/storage/PhabricatorBadgesDAO.php',
'PhabricatorBadgesDatasource' => 'applications/badges/typeahead/PhabricatorBadgesDatasource.php',
'PhabricatorBadgesDefaultEditCapability' => 'applications/badges/capability/PhabricatorBadgesDefaultEditCapability.php', 'PhabricatorBadgesDefaultEditCapability' => 'applications/badges/capability/PhabricatorBadgesDefaultEditCapability.php',
'PhabricatorBadgesEditConduitAPIMethod' => 'applications/badges/conduit/PhabricatorBadgesEditConduitAPIMethod.php', 'PhabricatorBadgesEditConduitAPIMethod' => 'applications/badges/conduit/PhabricatorBadgesEditConduitAPIMethod.php',
'PhabricatorBadgesEditController' => 'applications/badges/controller/PhabricatorBadgesEditController.php', 'PhabricatorBadgesEditController' => 'applications/badges/controller/PhabricatorBadgesEditController.php',
@ -6274,6 +6275,7 @@ phutil_register_library_map(array(
'PhabricatorBadgesController' => 'PhabricatorController', 'PhabricatorBadgesController' => 'PhabricatorController',
'PhabricatorBadgesCreateCapability' => 'PhabricatorPolicyCapability', 'PhabricatorBadgesCreateCapability' => 'PhabricatorPolicyCapability',
'PhabricatorBadgesDAO' => 'PhabricatorLiskDAO', 'PhabricatorBadgesDAO' => 'PhabricatorLiskDAO',
'PhabricatorBadgesDatasource' => 'PhabricatorTypeaheadDatasource',
'PhabricatorBadgesDefaultEditCapability' => 'PhabricatorPolicyCapability', 'PhabricatorBadgesDefaultEditCapability' => 'PhabricatorPolicyCapability',
'PhabricatorBadgesEditConduitAPIMethod' => 'PhabricatorEditEngineAPIMethod', 'PhabricatorBadgesEditConduitAPIMethod' => 'PhabricatorEditEngineAPIMethod',
'PhabricatorBadgesEditController' => 'PhabricatorBadgesController', 'PhabricatorBadgesEditController' => 'PhabricatorBadgesController',

View file

@ -18,23 +18,24 @@ final class PhabricatorBadgesAwardController
$view_uri = '/p/'.$user->getUsername(); $view_uri = '/p/'.$user->getUsername();
if ($request->isFormPost()) { if ($request->isFormPost()) {
$xactions = array(); $badge_phids = $request->getArr('badgePHIDs');
$badge_phid = $request->getStr('badgePHID'); $badges = id(new PhabricatorBadgesQuery())
$badge = id(new PhabricatorBadgesQuery())
->setViewer($viewer) ->setViewer($viewer)
->withPHIDs(array($badge_phid)) ->withPHIDs($badge_phids)
->needRecipients(true) ->needRecipients(true)
->requireCapabilities( ->requireCapabilities(
array( array(
PhabricatorPolicyCapability::CAN_EDIT, PhabricatorPolicyCapability::CAN_EDIT,
PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_VIEW,
)) ))
->executeOne(); ->execute();
if (!$badge) { if (!$badges) {
return new Aphront404Response(); return new Aphront404Response();
} }
$award_phids = array($user->getPHID()); $award_phids = array($user->getPHID());
foreach ($badges as $badge) {
$xactions = array();
$xactions[] = id(new PhabricatorBadgesTransaction()) $xactions[] = id(new PhabricatorBadgesTransaction())
->setTransactionType(PhabricatorBadgesTransaction::TYPE_AWARD) ->setTransactionType(PhabricatorBadgesTransaction::TYPE_AWARD)
->setNewValue($award_phids); ->setNewValue($award_phids);
@ -45,33 +46,24 @@ final class PhabricatorBadgesAwardController
->setContinueOnNoEffect(true) ->setContinueOnNoEffect(true)
->setContinueOnMissingFields(true) ->setContinueOnMissingFields(true)
->applyTransactions($badge, $xactions); ->applyTransactions($badge, $xactions);
}
return id(new AphrontRedirectResponse()) return id(new AphrontRedirectResponse())
->setURI($view_uri); ->setURI($view_uri);
} }
$badges = id(new PhabricatorBadgesQuery())
->setViewer($viewer)
->withStatuses(array(
PhabricatorBadgesBadge::STATUS_ACTIVE,
))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->execute();
$options = mpull($badges, 'getName', 'getPHID');
asort($options);
$form = id(new AphrontFormView()) $form = id(new AphrontFormView())
->setUser($viewer) ->setUser($viewer)
->appendChild( ->appendControl(
id(new AphrontFormSelectControl()) id(new AphrontFormTokenizerControl())
->setLabel(pht('Badge')) ->setLabel(pht('Badge'))
->setName('badgePHID') ->setName('badgePHIDs')
->setOptions($options)); ->setDatasource(
id(new PhabricatorBadgesDatasource())
->setParameters(
array(
'recipientPHID' => $user->getPHID(),
))));
$dialog = $this->newDialog() $dialog = $this->newDialog()
->setTitle(pht('Grant Badge')) ->setTitle(pht('Grant Badge'))

View file

@ -0,0 +1,69 @@
<?php
final class PhabricatorBadgesDatasource
extends PhabricatorTypeaheadDatasource {
public function getBrowseTitle() {
return pht('Browse Badges');
}
public function getPlaceholderText() {
return pht('Type a badge name...');
}
public function getDatasourceApplicationClass() {
return 'PhabricatorBadgesApplication';
}
public function loadResults() {
$viewer = $this->getViewer();
$raw_query = $this->getRawQuery();
$params = $this->getParameters();
$recipient_phid = $params['recipientPHID'];
$badges = id(new PhabricatorBadgesQuery())
->setViewer($viewer)
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->execute();
$awards = id(new PhabricatorBadgesAwardQuery())
->setViewer($viewer)
->withAwarderPHIDs(array($viewer->getPHID()))
->withRecipientPHIDs(array($recipient_phid))
->execute();
$awards = mpull($awards, null, 'getBadgePHID');
$results = array();
foreach ($badges as $badge) {
$closed = null;
$badge_awards = idx($awards, $badge->getPHID(), null);
if ($badge_awards) {
$closed = pht('Already awarded');
}
$status = $badge->getStatus();
if ($status === PhabricatorBadgesBadge::STATUS_ARCHIVED) {
$closed = pht('Archived');
}
$results[] = id(new PhabricatorTypeaheadResult())
->setName($badge->getName())
->setIcon($badge->getIcon())
->setColor(
PhabricatorBadgesQuality::getQualityColor($badge->getQuality()))
->setClosed($closed)
->setPHID($badge->getPHID());
}
$results = $this->filterResultsAgainstTokens($results);
return $results;
}
}