@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

+97 -34
+2
src/__phutil_library_map__.php
··· 1880 1880 'PhabricatorBadgesController' => 'applications/badges/controller/PhabricatorBadgesController.php', 1881 1881 'PhabricatorBadgesCreateCapability' => 'applications/badges/capability/PhabricatorBadgesCreateCapability.php', 1882 1882 'PhabricatorBadgesDAO' => 'applications/badges/storage/PhabricatorBadgesDAO.php', 1883 + 'PhabricatorBadgesDatasource' => 'applications/badges/typeahead/PhabricatorBadgesDatasource.php', 1883 1884 'PhabricatorBadgesDefaultEditCapability' => 'applications/badges/capability/PhabricatorBadgesDefaultEditCapability.php', 1884 1885 'PhabricatorBadgesEditConduitAPIMethod' => 'applications/badges/conduit/PhabricatorBadgesEditConduitAPIMethod.php', 1885 1886 'PhabricatorBadgesEditController' => 'applications/badges/controller/PhabricatorBadgesEditController.php', ··· 6274 6275 'PhabricatorBadgesController' => 'PhabricatorController', 6275 6276 'PhabricatorBadgesCreateCapability' => 'PhabricatorPolicyCapability', 6276 6277 'PhabricatorBadgesDAO' => 'PhabricatorLiskDAO', 6278 + 'PhabricatorBadgesDatasource' => 'PhabricatorTypeaheadDatasource', 6277 6279 'PhabricatorBadgesDefaultEditCapability' => 'PhabricatorPolicyCapability', 6278 6280 'PhabricatorBadgesEditConduitAPIMethod' => 'PhabricatorEditEngineAPIMethod', 6279 6281 'PhabricatorBadgesEditController' => 'PhabricatorBadgesController',
+26 -34
src/applications/badges/controller/PhabricatorBadgesAwardController.php
··· 18 18 $view_uri = '/p/'.$user->getUsername(); 19 19 20 20 if ($request->isFormPost()) { 21 - $xactions = array(); 22 - $badge_phid = $request->getStr('badgePHID'); 23 - $badge = id(new PhabricatorBadgesQuery()) 21 + $badge_phids = $request->getArr('badgePHIDs'); 22 + $badges = id(new PhabricatorBadgesQuery()) 24 23 ->setViewer($viewer) 25 - ->withPHIDs(array($badge_phid)) 24 + ->withPHIDs($badge_phids) 26 25 ->needRecipients(true) 27 26 ->requireCapabilities( 28 27 array( 29 28 PhabricatorPolicyCapability::CAN_EDIT, 30 29 PhabricatorPolicyCapability::CAN_VIEW, 31 30 )) 32 - ->executeOne(); 33 - if (!$badge) { 31 + ->execute(); 32 + if (!$badges) { 34 33 return new Aphront404Response(); 35 34 } 36 35 $award_phids = array($user->getPHID()); 37 36 38 - $xactions[] = id(new PhabricatorBadgesTransaction()) 39 - ->setTransactionType(PhabricatorBadgesTransaction::TYPE_AWARD) 40 - ->setNewValue($award_phids); 37 + foreach ($badges as $badge) { 38 + $xactions = array(); 39 + $xactions[] = id(new PhabricatorBadgesTransaction()) 40 + ->setTransactionType(PhabricatorBadgesTransaction::TYPE_AWARD) 41 + ->setNewValue($award_phids); 41 42 42 - $editor = id(new PhabricatorBadgesEditor($badge)) 43 - ->setActor($viewer) 44 - ->setContentSourceFromRequest($request) 45 - ->setContinueOnNoEffect(true) 46 - ->setContinueOnMissingFields(true) 47 - ->applyTransactions($badge, $xactions); 43 + $editor = id(new PhabricatorBadgesEditor($badge)) 44 + ->setActor($viewer) 45 + ->setContentSourceFromRequest($request) 46 + ->setContinueOnNoEffect(true) 47 + ->setContinueOnMissingFields(true) 48 + ->applyTransactions($badge, $xactions); 49 + } 48 50 49 51 return id(new AphrontRedirectResponse()) 50 52 ->setURI($view_uri); 51 53 } 52 54 53 - $badges = id(new PhabricatorBadgesQuery()) 54 - ->setViewer($viewer) 55 - ->withStatuses(array( 56 - PhabricatorBadgesBadge::STATUS_ACTIVE, 57 - )) 58 - ->requireCapabilities( 59 - array( 60 - PhabricatorPolicyCapability::CAN_VIEW, 61 - PhabricatorPolicyCapability::CAN_EDIT, 62 - )) 63 - ->execute(); 64 - 65 - $options = mpull($badges, 'getName', 'getPHID'); 66 - asort($options); 67 - 68 55 $form = id(new AphrontFormView()) 69 56 ->setUser($viewer) 70 - ->appendChild( 71 - id(new AphrontFormSelectControl()) 57 + ->appendControl( 58 + id(new AphrontFormTokenizerControl()) 72 59 ->setLabel(pht('Badge')) 73 - ->setName('badgePHID') 74 - ->setOptions($options)); 60 + ->setName('badgePHIDs') 61 + ->setDatasource( 62 + id(new PhabricatorBadgesDatasource()) 63 + ->setParameters( 64 + array( 65 + 'recipientPHID' => $user->getPHID(), 66 + )))); 75 67 76 68 $dialog = $this->newDialog() 77 69 ->setTitle(pht('Grant Badge'))
+69
src/applications/badges/typeahead/PhabricatorBadgesDatasource.php
··· 1 + <?php 2 + 3 + final class PhabricatorBadgesDatasource 4 + extends PhabricatorTypeaheadDatasource { 5 + 6 + public function getBrowseTitle() { 7 + return pht('Browse Badges'); 8 + } 9 + 10 + public function getPlaceholderText() { 11 + return pht('Type a badge name...'); 12 + } 13 + 14 + public function getDatasourceApplicationClass() { 15 + return 'PhabricatorBadgesApplication'; 16 + } 17 + 18 + public function loadResults() { 19 + $viewer = $this->getViewer(); 20 + $raw_query = $this->getRawQuery(); 21 + 22 + $params = $this->getParameters(); 23 + $recipient_phid = $params['recipientPHID']; 24 + 25 + $badges = id(new PhabricatorBadgesQuery()) 26 + ->setViewer($viewer) 27 + ->requireCapabilities( 28 + array( 29 + PhabricatorPolicyCapability::CAN_VIEW, 30 + PhabricatorPolicyCapability::CAN_EDIT, 31 + )) 32 + ->execute(); 33 + 34 + $awards = id(new PhabricatorBadgesAwardQuery()) 35 + ->setViewer($viewer) 36 + ->withAwarderPHIDs(array($viewer->getPHID())) 37 + ->withRecipientPHIDs(array($recipient_phid)) 38 + ->execute(); 39 + $awards = mpull($awards, null, 'getBadgePHID'); 40 + 41 + $results = array(); 42 + foreach ($badges as $badge) { 43 + $closed = null; 44 + 45 + $badge_awards = idx($awards, $badge->getPHID(), null); 46 + if ($badge_awards) { 47 + $closed = pht('Already awarded'); 48 + } 49 + 50 + $status = $badge->getStatus(); 51 + if ($status === PhabricatorBadgesBadge::STATUS_ARCHIVED) { 52 + $closed = pht('Archived'); 53 + } 54 + 55 + $results[] = id(new PhabricatorTypeaheadResult()) 56 + ->setName($badge->getName()) 57 + ->setIcon($badge->getIcon()) 58 + ->setColor( 59 + PhabricatorBadgesQuality::getQualityColor($badge->getQuality())) 60 + ->setClosed($closed) 61 + ->setPHID($badge->getPHID()); 62 + } 63 + 64 + $results = $this->filterResultsAgainstTokens($results); 65 + 66 + return $results; 67 + } 68 + 69 + }