1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-19 12:00:55 +01:00

Simplify members() datasource function

Summary: Ref T4100. Implement members() in terms of the real projects datasource.

Test Plan: Used members() functions in Differential, browsed members() functions.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T4100

Differential Revision: https://secure.phabricator.com/D12459
This commit is contained in:
epriestley 2015-04-18 09:26:04 -07:00
parent c246aa2638
commit eb69b115a7
3 changed files with 33 additions and 69 deletions

View file

@ -5675,7 +5675,7 @@ phutil_register_library_map(array(
'PhabricatorProjectIcon' => 'Phobject', 'PhabricatorProjectIcon' => 'Phobject',
'PhabricatorProjectListController' => 'PhabricatorProjectController', 'PhabricatorProjectListController' => 'PhabricatorProjectController',
'PhabricatorProjectMemberOfProjectEdgeType' => 'PhabricatorEdgeType', 'PhabricatorProjectMemberOfProjectEdgeType' => 'PhabricatorEdgeType',
'PhabricatorProjectMembersDatasource' => 'PhabricatorTypeaheadDatasource', 'PhabricatorProjectMembersDatasource' => 'PhabricatorTypeaheadCompositeDatasource',
'PhabricatorProjectMembersEditController' => 'PhabricatorProjectController', 'PhabricatorProjectMembersEditController' => 'PhabricatorProjectController',
'PhabricatorProjectMembersRemoveController' => 'PhabricatorProjectController', 'PhabricatorProjectMembersRemoveController' => 'PhabricatorProjectController',
'PhabricatorProjectMoveController' => 'PhabricatorProjectController', 'PhabricatorProjectMoveController' => 'PhabricatorProjectController',

View file

@ -73,6 +73,7 @@ final class PhabricatorUserProjectsDatasource
->setValue(pht('Projects: Invalid User')); ->setValue(pht('Projects: Invalid User'));
} else { } else {
$token $token
->setIcon('fa-users')
->setTokenType(PhabricatorTypeaheadTokenView::TYPE_FUNCTION) ->setTokenType(PhabricatorTypeaheadTokenView::TYPE_FUNCTION)
->setKey('projects('.$token->getKey().')') ->setKey('projects('.$token->getKey().')')
->setValue(pht('Projects: %s', $token->getValue())); ->setValue(pht('Projects: %s', $token->getValue()));

View file

@ -1,7 +1,7 @@
<?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>)...');
@ -11,40 +11,33 @@ final class PhabricatorProjectMembersDatasource
return 'PhabricatorProjectApplication'; return 'PhabricatorProjectApplication';
} }
public function loadResults() { public function getComponentDatasources() {
$viewer = $this->getViewer(); return array(
$raw_query = $this->getRawQuery(); new PhabricatorProjectDatasource(),
);
}
$pattern = $raw_query; public function getDatasourceFunctions() {
if (self::isFunctionToken($raw_query)) { return array(
$function = $this->parseFunction($raw_query, $allow_partial = true); 'members' => array(
if ($function) { 'name' => pht('Find results for members of a project.'),
$pattern = head($function['argv']); ),
} );
} }
// Allow users to type "#qa" or "qa" to find "Quality Assurance". protected function didLoadResults(array $results) {
$pattern = ltrim($pattern, '#'); foreach ($results as $result) {
$tokens = self::tokenizeString($pattern); $result
->setTokenType(PhabricatorTypeaheadTokenView::TYPE_FUNCTION)
$query = $this->newQuery(); ->setIcon('fa-users')
if ($tokens) { ->setPHID('members('.$result->getPHID().')')
$query->withNameTokens($tokens); ->setDisplayName(pht('Members: %s', $result->getDisplayName()))
} ->setName($result->getName().' members');
$projects = $this->executeQuery($query);
$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) {
@ -73,51 +66,21 @@ final class PhabricatorProjectMembersDatasource
$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);
}
} }