Differential D12459 Diff 29955 src/applications/project/typeahead/PhabricatorProjectMembersDatasource.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/project/typeahead/PhabricatorProjectMembersDatasource.php
<?php | <?php | ||||
final class PhabricatorProjectMembersDatasource | final class PhabricatorProjectMembersDatasource | ||||
extends PhabricatorTypeaheadDatasource { | extends PhabricatorTypeaheadCompositeDatasource { | ||||
public function getPlaceholderText() { | public function getPlaceholderText() { | ||||
return pht('Type members(<project>)...'); | return pht('Type members(<project>)...'); | ||||
} | } | ||||
public function getDatasourceApplicationClass() { | public function getDatasourceApplicationClass() { | ||||
return 'PhabricatorProjectApplication'; | return 'PhabricatorProjectApplication'; | ||||
} | } | ||||
public function loadResults() { | public function getComponentDatasources() { | ||||
$viewer = $this->getViewer(); | return array( | ||||
$raw_query = $this->getRawQuery(); | new PhabricatorProjectDatasource(), | ||||
); | |||||
$pattern = $raw_query; | } | ||||
if (self::isFunctionToken($raw_query)) { | |||||
$function = $this->parseFunction($raw_query, $allow_partial = true); | public function getDatasourceFunctions() { | ||||
if ($function) { | return array( | ||||
$pattern = head($function['argv']); | 'members' => array( | ||||
} | 'name' => pht('Find results for members of a project.'), | ||||
} | ), | ||||
); | |||||
// Allow users to type "#qa" or "qa" to find "Quality Assurance". | } | ||||
$pattern = ltrim($pattern, '#'); | |||||
$tokens = self::tokenizeString($pattern); | protected function didLoadResults(array $results) { | ||||
foreach ($results as $result) { | |||||
$query = $this->newQuery(); | $result | ||||
if ($tokens) { | ->setTokenType(PhabricatorTypeaheadTokenView::TYPE_FUNCTION) | ||||
$query->withNameTokens($tokens); | ->setIcon('fa-users') | ||||
} | ->setPHID('members('.$result->getPHID().')') | ||||
$projects = $this->executeQuery($query); | ->setDisplayName(pht('Members: %s', $result->getDisplayName())) | ||||
->setName($result->getName().' members'); | |||||
$results = array(); | |||||
foreach ($projects as $project) { | |||||
$results[] = $this->buildProjectResult($project); | |||||
} | } | ||||
return $results; | return $results; | ||||
} | } | ||||
protected function canEvaluateFunction($function) { | |||||
return ($function == 'members'); | |||||
} | |||||
protected function evaluateFunction($function, array $argv_list) { | protected function evaluateFunction($function, array $argv_list) { | ||||
$phids = array(); | $phids = array(); | ||||
foreach ($argv_list as $argv) { | foreach ($argv_list as $argv) { | ||||
$phids[] = head($argv); | $phids[] = head($argv); | ||||
} | } | ||||
$projects = id(new PhabricatorProjectQuery()) | $projects = id(new PhabricatorProjectQuery()) | ||||
->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
Show All 12 Lines | final class PhabricatorProjectMembersDatasource | ||||
} | } | ||||
public function renderFunctionTokens($function, array $argv_list) { | public function renderFunctionTokens($function, array $argv_list) { | ||||
$phids = array(); | $phids = array(); | ||||
foreach ($argv_list as $argv) { | foreach ($argv_list as $argv) { | ||||
$phids[] = head($argv); | $phids[] = head($argv); | ||||
} | } | ||||
$projects = $this->newQuery() | $tokens = $this->renderTokens($phids); | ||||
->withPHIDs($phids) | foreach ($tokens as $token) { | ||||
->execute(); | if ($token->isInvalid()) { | ||||
$projects = mpull($projects, null, 'getPHID'); | $token | ||||
->setValue(pht('Members: Invalid Project')); | |||||
$tokens = array(); | |||||
foreach ($phids as $phid) { | |||||
$project = idx($projects, $phid); | |||||
if ($project) { | |||||
$result = $this->buildProjectResult($project); | |||||
$tokens[] = PhabricatorTypeaheadTokenView::newFromTypeaheadResult( | |||||
$result); | |||||
} else { | } else { | ||||
$tokens[] = $this->newInvalidToken(pht('Members: Invalid Project')); | $token | ||||
->setIcon('fa-users') | |||||
->setTokenType(PhabricatorTypeaheadTokenView::TYPE_FUNCTION) | |||||
->setKey('members('.$token->getKey().')') | |||||
->setValue(pht('Members: %s', $token->getValue())); | |||||
} | } | ||||
} | } | ||||
return $tokens; | return $tokens; | ||||
} | } | ||||
private function newQuery() { | |||||
return id(new PhabricatorProjectQuery()) | |||||
->setViewer($this->getViewer()) | |||||
->needImages(true) | |||||
->needSlugs(true); | |||||
} | |||||
private function buildProjectResult(PhabricatorProject $project) { | |||||
$closed = null; | |||||
if ($project->isArchived()) { | |||||
$closed = pht('Archived'); | |||||
} | |||||
$all_strings = mpull($project->getSlugs(), 'getSlug'); | |||||
$all_strings[] = 'members'; | |||||
$all_strings[] = $project->getName(); | |||||
$all_strings = implode(' ', $all_strings); | |||||
return $this->newFunctionResult() | |||||
->setName($all_strings) | |||||
->setDisplayName(pht('Members: %s', $project->getName())) | |||||
->setURI('/tag/'.$project->getPrimarySlug().'/') | |||||
->setPHID('members('.$project->getPHID().')') | |||||
->setIcon('fa-users') | |||||
->setClosed($closed); | |||||
} | |||||
} | } |