Changeset View
Changeset View
Standalone View
Standalone View
src/applications/herald/query/HeraldRuleSearchEngine.php
<?php | <?php | ||||
final class HeraldRuleSearchEngine extends PhabricatorApplicationSearchEngine { | final class HeraldRuleSearchEngine extends PhabricatorApplicationSearchEngine { | ||||
public function getResultTypeDescription() { | public function getResultTypeDescription() { | ||||
return pht('Herald Rules'); | return pht('Herald Rules'); | ||||
} | } | ||||
public function getApplicationClassName() { | public function getApplicationClassName() { | ||||
return 'PhabricatorHeraldApplication'; | return 'PhabricatorHeraldApplication'; | ||||
} | } | ||||
public function buildSavedQueryFromRequest(AphrontRequest $request) { | public function newQuery() { | ||||
$saved = new PhabricatorSavedQuery(); | return new HeraldRuleQuery(); | ||||
} | |||||
$saved->setParameter( | protected function buildCustomSearchFields() { | ||||
'authorPHIDs', | $viewer = $this->requireViewer(); | ||||
$this->readUsersFromRequest($request, 'authors')); | |||||
$saved->setParameter('contentType', $request->getStr('contentType')); | $rule_types = HeraldRuleTypeConfig::getRuleTypeMap(); | ||||
$saved->setParameter('ruleType', $request->getStr('ruleType')); | $content_types = HeraldAdapter::getEnabledAdapterMap($viewer); | ||||
$saved->setParameter( | |||||
'disabled', | |||||
$this->readBoolFromRequest($request, 'disabled')); | |||||
return $saved; | return array( | ||||
id(new PhabricatorUsersSearchField()) | |||||
->setLabel(pht('Authors')) | |||||
->setKey('authorPHIDs') | |||||
->setAliases(array('author', 'authors', 'authorPHID')) | |||||
->setDescription( | |||||
pht('Search for rules with given authors.')), | |||||
id(new PhabricatorSearchCheckboxesField()) | |||||
->setKey('ruleTypes') | |||||
->setAliases(array('ruleType')) | |||||
->setLabel(pht('Rule Type')) | |||||
->setDescription( | |||||
pht('Search for rules of given types.')) | |||||
->setOptions($rule_types), | |||||
id(new PhabricatorSearchCheckboxesField()) | |||||
->setKey('contentTypes') | |||||
->setLabel(pht('Content Type')) | |||||
->setDescription( | |||||
pht('Search for rules affecting given types of content.')) | |||||
->setOptions($content_types), | |||||
id(new PhabricatorSearchThreeStateField()) | |||||
->setLabel(pht('Rule Status')) | |||||
->setKey('disabled') | |||||
->setOptions( | |||||
pht('(Show All)'), | |||||
pht('Show Only Disabled Rules'), | |||||
pht('Show Only Enabled Rules')), | |||||
); | |||||
} | } | ||||
public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { | protected function buildQueryFromParameters(array $map) { | ||||
$query = id(new HeraldRuleQuery()); | $query = $this->newQuery(); | ||||
$author_phids = $saved->getParameter('authorPHIDs'); | if ($map['authorPHIDs']) { | ||||
if ($author_phids) { | $query->withAuthorPHIDs($map['authorPHIDs']); | ||||
$query->withAuthorPHIDs($author_phids); | |||||
} | } | ||||
$content_type = $saved->getParameter('contentType'); | if ($map['contentTypes']) { | ||||
$content_type = idx($this->getContentTypeValues(), $content_type); | $query->withContentTypes($map['contentTypes']); | ||||
if ($content_type) { | |||||
$query->withContentTypes(array($content_type)); | |||||
} | } | ||||
$rule_type = $saved->getParameter('ruleType'); | if ($map['ruleTypes']) { | ||||
$rule_type = idx($this->getRuleTypeValues(), $rule_type); | $query->withRuleTypes($map['ruleTypes']); | ||||
if ($rule_type) { | |||||
$query->withRuleTypes(array($rule_type)); | |||||
} | } | ||||
$disabled = $saved->getParameter('disabled'); | if ($map['disabled'] !== null) { | ||||
if ($disabled !== null) { | $query->withDisabled($map['disabled']); | ||||
$query->withDisabled($disabled); | |||||
} | } | ||||
return $query; | return $query; | ||||
} | } | ||||
amckinley: Wow, I didn't realize how much nicer the new-style search stuff looks! | |||||
public function buildSearchForm( | |||||
AphrontFormView $form, | |||||
PhabricatorSavedQuery $saved_query) { | |||||
$author_phids = $saved_query->getParameter('authorPHIDs', array()); | |||||
$content_type = $saved_query->getParameter('contentType'); | |||||
$rule_type = $saved_query->getParameter('ruleType'); | |||||
$form | |||||
->appendControl( | |||||
id(new AphrontFormTokenizerControl()) | |||||
->setDatasource(new PhabricatorPeopleDatasource()) | |||||
->setName('authors') | |||||
->setLabel(pht('Authors')) | |||||
->setValue($author_phids)) | |||||
->appendChild( | |||||
id(new AphrontFormSelectControl()) | |||||
->setName('contentType') | |||||
->setLabel(pht('Content Type')) | |||||
->setValue($content_type) | |||||
->setOptions($this->getContentTypeOptions())) | |||||
->appendChild( | |||||
id(new AphrontFormSelectControl()) | |||||
->setName('ruleType') | |||||
->setLabel(pht('Rule Type')) | |||||
->setValue($rule_type) | |||||
->setOptions($this->getRuleTypeOptions())) | |||||
->appendChild( | |||||
id(new AphrontFormSelectControl()) | |||||
->setName('disabled') | |||||
->setLabel(pht('Rule Status')) | |||||
->setValue($this->getBoolFromQuery($saved_query, 'disabled')) | |||||
->setOptions( | |||||
array( | |||||
'' => pht('Show Enabled and Disabled Rules'), | |||||
'false' => pht('Show Only Enabled Rules'), | |||||
'true' => pht('Show Only Disabled Rules'), | |||||
))); | |||||
} | |||||
protected function getURI($path) { | protected function getURI($path) { | ||||
return '/herald/'.$path; | return '/herald/'.$path; | ||||
} | } | ||||
protected function getBuiltinQueryNames() { | protected function getBuiltinQueryNames() { | ||||
$names = array(); | $names = array(); | ||||
if ($this->requireViewer()->isLoggedIn()) { | if ($this->requireViewer()->isLoggedIn()) { | ||||
Show All 21 Lines | switch ($query_key) { | ||||
return $query | return $query | ||||
->setParameter('authorPHIDs', array($viewer_phid)) | ->setParameter('authorPHIDs', array($viewer_phid)) | ||||
->setParameter('disabled', false); | ->setParameter('disabled', false); | ||||
} | } | ||||
return parent::buildSavedQueryFromBuiltin($query_key); | return parent::buildSavedQueryFromBuiltin($query_key); | ||||
} | } | ||||
private function getContentTypeOptions() { | |||||
return array( | |||||
'' => pht('(All Content Types)'), | |||||
) + HeraldAdapter::getEnabledAdapterMap($this->requireViewer()); | |||||
} | |||||
private function getContentTypeValues() { | |||||
return array_fuse( | |||||
array_keys( | |||||
HeraldAdapter::getEnabledAdapterMap($this->requireViewer()))); | |||||
} | |||||
private function getRuleTypeOptions() { | |||||
return array( | |||||
'' => pht('(All Rule Types)'), | |||||
) + HeraldRuleTypeConfig::getRuleTypeMap(); | |||||
} | |||||
private function getRuleTypeValues() { | |||||
return array_fuse(array_keys(HeraldRuleTypeConfig::getRuleTypeMap())); | |||||
} | |||||
protected function getRequiredHandlePHIDsForResultList( | |||||
array $rules, | |||||
PhabricatorSavedQuery $query) { | |||||
return mpull($rules, 'getAuthorPHID'); | |||||
} | |||||
protected function renderResultList( | protected function renderResultList( | ||||
array $rules, | array $rules, | ||||
PhabricatorSavedQuery $query, | PhabricatorSavedQuery $query, | ||||
array $handles) { | array $handles) { | ||||
assert_instances_of($rules, 'HeraldRule'); | assert_instances_of($rules, 'HeraldRule'); | ||||
$viewer = $this->requireViewer(); | $viewer = $this->requireViewer(); | ||||
$handles = $viewer->loadHandles(mpull($rules, 'getAuthorPHID')); | |||||
$content_type_map = HeraldAdapter::getEnabledAdapterMap($viewer); | $content_type_map = HeraldAdapter::getEnabledAdapterMap($viewer); | ||||
$list = id(new PHUIObjectItemListView()) | $list = id(new PHUIObjectItemListView()) | ||||
->setUser($viewer); | ->setUser($viewer); | ||||
foreach ($rules as $rule) { | foreach ($rules as $rule) { | ||||
$monogram = $rule->getMonogram(); | $monogram = $rule->getMonogram(); | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |
Wow, I didn't realize how much nicer the new-style search stuff looks!