diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index fb1abc26f4..3825279d98 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/applications/badges/controller/PhabricatorBadgesAwardController.php b/src/applications/badges/controller/PhabricatorBadgesAwardController.php index 0475dd6277..0e5679caa4 100644 --- a/src/applications/badges/controller/PhabricatorBadgesAwardController.php +++ b/src/applications/badges/controller/PhabricatorBadgesAwardController.php @@ -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')) diff --git a/src/applications/badges/typeahead/PhabricatorBadgesDatasource.php b/src/applications/badges/typeahead/PhabricatorBadgesDatasource.php new file mode 100644 index 0000000000..458c9230d5 --- /dev/null +++ b/src/applications/badges/typeahead/PhabricatorBadgesDatasource.php @@ -0,0 +1,69 @@ +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; + } + +}