mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-10 00:42:41 +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:
parent
6b40cfaa60
commit
85d2fda082
3 changed files with 97 additions and 34 deletions
|
@ -1880,6 +1880,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorBadgesController' => 'applications/badges/controller/PhabricatorBadgesController.php',
|
||||
'PhabricatorBadgesCreateCapability' => 'applications/badges/capability/PhabricatorBadgesCreateCapability.php',
|
||||
'PhabricatorBadgesDAO' => 'applications/badges/storage/PhabricatorBadgesDAO.php',
|
||||
'PhabricatorBadgesDatasource' => 'applications/badges/typeahead/PhabricatorBadgesDatasource.php',
|
||||
'PhabricatorBadgesDefaultEditCapability' => 'applications/badges/capability/PhabricatorBadgesDefaultEditCapability.php',
|
||||
'PhabricatorBadgesEditConduitAPIMethod' => 'applications/badges/conduit/PhabricatorBadgesEditConduitAPIMethod.php',
|
||||
'PhabricatorBadgesEditController' => 'applications/badges/controller/PhabricatorBadgesEditController.php',
|
||||
|
@ -6274,6 +6275,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorBadgesController' => 'PhabricatorController',
|
||||
'PhabricatorBadgesCreateCapability' => 'PhabricatorPolicyCapability',
|
||||
'PhabricatorBadgesDAO' => 'PhabricatorLiskDAO',
|
||||
'PhabricatorBadgesDatasource' => 'PhabricatorTypeaheadDatasource',
|
||||
'PhabricatorBadgesDefaultEditCapability' => 'PhabricatorPolicyCapability',
|
||||
'PhabricatorBadgesEditConduitAPIMethod' => 'PhabricatorEditEngineAPIMethod',
|
||||
'PhabricatorBadgesEditController' => 'PhabricatorBadgesController',
|
||||
|
|
|
@ -18,60 +18,52 @@ final class PhabricatorBadgesAwardController
|
|||
$view_uri = '/p/'.$user->getUsername();
|
||||
|
||||
if ($request->isFormPost()) {
|
||||
$xactions = array();
|
||||
$badge_phid = $request->getStr('badgePHID');
|
||||
$badge = id(new PhabricatorBadgesQuery())
|
||||
$badge_phids = $request->getArr('badgePHIDs');
|
||||
$badges = id(new PhabricatorBadgesQuery())
|
||||
->setViewer($viewer)
|
||||
->withPHIDs(array($badge_phid))
|
||||
->withPHIDs($badge_phids)
|
||||
->needRecipients(true)
|
||||
->requireCapabilities(
|
||||
array(
|
||||
PhabricatorPolicyCapability::CAN_EDIT,
|
||||
PhabricatorPolicyCapability::CAN_VIEW,
|
||||
))
|
||||
->executeOne();
|
||||
if (!$badge) {
|
||||
->execute();
|
||||
if (!$badges) {
|
||||
return new Aphront404Response();
|
||||
}
|
||||
$award_phids = array($user->getPHID());
|
||||
|
||||
$xactions[] = id(new PhabricatorBadgesTransaction())
|
||||
->setTransactionType(PhabricatorBadgesTransaction::TYPE_AWARD)
|
||||
->setNewValue($award_phids);
|
||||
foreach ($badges as $badge) {
|
||||
$xactions = array();
|
||||
$xactions[] = id(new PhabricatorBadgesTransaction())
|
||||
->setTransactionType(PhabricatorBadgesTransaction::TYPE_AWARD)
|
||||
->setNewValue($award_phids);
|
||||
|
||||
$editor = id(new PhabricatorBadgesEditor($badge))
|
||||
->setActor($viewer)
|
||||
->setContentSourceFromRequest($request)
|
||||
->setContinueOnNoEffect(true)
|
||||
->setContinueOnMissingFields(true)
|
||||
->applyTransactions($badge, $xactions);
|
||||
$editor = id(new PhabricatorBadgesEditor($badge))
|
||||
->setActor($viewer)
|
||||
->setContentSourceFromRequest($request)
|
||||
->setContinueOnNoEffect(true)
|
||||
->setContinueOnMissingFields(true)
|
||||
->applyTransactions($badge, $xactions);
|
||||
}
|
||||
|
||||
return id(new AphrontRedirectResponse())
|
||||
->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())
|
||||
->setUser($viewer)
|
||||
->appendChild(
|
||||
id(new AphrontFormSelectControl())
|
||||
->appendControl(
|
||||
id(new AphrontFormTokenizerControl())
|
||||
->setLabel(pht('Badge'))
|
||||
->setName('badgePHID')
|
||||
->setOptions($options));
|
||||
->setName('badgePHIDs')
|
||||
->setDatasource(
|
||||
id(new PhabricatorBadgesDatasource())
|
||||
->setParameters(
|
||||
array(
|
||||
'recipientPHID' => $user->getPHID(),
|
||||
))));
|
||||
|
||||
$dialog = $this->newDialog()
|
||||
->setTitle(pht('Grant Badge'))
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue