1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-27 09:12:41 +01:00

Move People to SearchFields

Summary:
Ref T8441. Ref T7715. This is the second of three ApplicationSearch + CustomField use cases (Maniphest is the third).

Also add a way to set a default ordering for the fields.

Test Plan:
  - Performed searches with each field.
  - Added a custom field and searched for it.
  - Observed desired ordering.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T7715, T8441

Differential Revision: https://secure.phabricator.com/D13190
This commit is contained in:
epriestley 2015-06-08 12:20:35 -07:00
parent c8b866a956
commit 3e2b0c35f9
2 changed files with 149 additions and 159 deletions

View file

@ -11,45 +11,72 @@ final class PhabricatorPeopleSearchEngine
return 'PhabricatorPeopleApplication'; return 'PhabricatorPeopleApplication';
} }
public function getCustomFieldObject() { public function newResultObject() {
return new PhabricatorUser(); return new PhabricatorUser();
} }
public function buildSavedQueryFromRequest(AphrontRequest $request) { protected function buildCustomSearchFields() {
$saved = new PhabricatorSavedQuery(); return array(
id(new PhabricatorSearchStringListField())
$saved->setParameter('usernames', $request->getStrList('usernames')); ->setLabel(pht('Usernames'))
$saved->setParameter('nameLike', $request->getStr('nameLike')); ->setKey('usernames')
->setAliases(array('username')),
$saved->setParameter( id(new PhabricatorSearchTextField())
'isAdmin', ->setLabel(pht('Name Contains'))
$this->readBoolFromRequest($request, 'isAdmin')); ->setKey('nameLike'),
id(new PhabricatorSearchThreeStateField())
$saved->setParameter( ->setLabel(pht('Administrators'))
'isDisabled', ->setKey('isAdmin')
$this->readBoolFromRequest($request, 'isDisabled')); ->setOptions(
pht('(Show All)'),
$saved->setParameter( pht('Show Only Administrators'),
'isSystemAgent', pht('Hide Administrators')),
$this->readBoolFromRequest($request, 'isSystemAgent')); id(new PhabricatorSearchThreeStateField())
->setLabel(pht('Disabled'))
$saved->setParameter( ->setKey('isDisabled')
'isMailingList', ->setOptions(
$this->readBoolFromRequest($request, 'isMailingList')); pht('(Show All)'),
pht('Show Only Disabled Users'),
$saved->setParameter( pht('Hide Disabled Users')),
'needsApproval', id(new PhabricatorSearchThreeStateField())
$this->readBoolFromRequest($request, 'needsApproval')); ->setLabel(pht('Bots'))
->setKey('isSystemAgent')
$saved->setParameter('createdStart', $request->getStr('createdStart')); ->setOptions(
$saved->setParameter('createdEnd', $request->getStr('createdEnd')); pht('(Show All)'),
pht('Show Only Bots'),
$this->readCustomFieldsFromRequest($request, $saved); pht('Hide Bots')),
id(new PhabricatorSearchThreeStateField())
return $saved; ->setLabel(pht('Mailing Lists'))
->setKey('isMailingList')
->setOptions(
pht('(Show All)'),
pht('Show Only Mailing Lists'),
pht('Hide Mailing Lists')),
id(new PhabricatorSearchThreeStateField())
->setLabel(pht('Needs Approval'))
->setKey('needsApproval')
->setOptions(
pht('(Show All)'),
pht('Show Only Unapproved Users'),
pht('Hide Unappproved Users')),
id(new PhabricatorSearchDateField())
->setKey('createdStart')
->setLabel(pht('Joined After')),
id(new PhabricatorSearchDateField())
->setKey('createdEnd')
->setLabel(pht('Joined Before')),
);
} }
public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { protected function getDefaultFieldOrder() {
return array(
'...',
'createdStart',
'createdEnd',
);
}
public function buildQueryFromParameters(array $map) {
$query = id(new PhabricatorPeopleQuery()) $query = id(new PhabricatorPeopleQuery())
->needPrimaryEmail(true) ->needPrimaryEmail(true)
->needProfileImage(true); ->needProfileImage(true);
@ -68,149 +95,45 @@ final class PhabricatorPeopleSearchEngine
$query->withPHIDs(array($viewer->getPHID())); $query->withPHIDs(array($viewer->getPHID()));
} }
$usernames = $saved->getParameter('usernames', array()); if ($map['usernames']) {
if ($usernames) { $query->withUsernames($map['usernames']);
$query->withUsernames($usernames);
} }
$like = $saved->getParameter('nameLike'); if ($map['nameLike']) {
if ($like) { $query->withNameLike($map['nameLike']);
$query->withNameLike($like);
} }
$is_admin = $saved->getParameter('isAdmin'); if ($map['isAdmin'] !== null) {
$is_disabled = $saved->getParameter('isDisabled'); $query->withIsAdmin($map['isAdmin']);
$is_system_agent = $saved->getParameter('isSystemAgent');
$is_mailing_list = $saved->getParameter('isMailingList');
$needs_approval = $saved->getParameter('needsApproval');
if ($is_admin !== null) {
$query->withIsAdmin($is_admin);
} }
if ($is_disabled !== null) { if ($map['isDisabled'] !== null) {
$query->withIsDisabled($is_disabled); $query->withIsDisabled($map['isDisabled']);
} }
if ($is_system_agent !== null) { if ($map['isMailingList'] !== null) {
$query->withIsSystemAgent($is_system_agent); $query->withIsMailingList($map['isMailingList']);
} }
if ($is_mailing_list !== null) { if ($map['isSystemAgent'] !== null) {
$query->withIsMailingList($is_mailing_list); $query->withIsSystemAgent($map['isSystemAgent']);
} }
if ($needs_approval !== null) { if ($map['needsApproval'] !== null) {
$query->withIsApproved(!$needs_approval); $query->withIsApproved(!$map['needsApproval']);
} }
$start = $this->parseDateTime($saved->getParameter('createdStart')); if ($map['createdStart']) {
$end = $this->parseDateTime($saved->getParameter('createdEnd')); $query->withDateCreatedAfter($map['createdStart']);
if ($start) {
$query->withDateCreatedAfter($start);
} }
if ($end) { if ($map['createdEnd']) {
$query->withDateCreatedBefore($end); $query->withDateCreatedBefore($map['createdEnd']);
} }
$this->applyCustomFieldsToQuery($query, $saved);
return $query; return $query;
} }
public function buildSearchForm(
AphrontFormView $form,
PhabricatorSavedQuery $saved) {
$usernames = $saved->getParameter('usernames', array());
$like = $saved->getParameter('nameLike');
$is_admin = $this->getBoolFromQuery($saved, 'isAdmin');
$is_disabled = $this->getBoolFromQuery($saved, 'isDisabled');
$is_system_agent = $this->getBoolFromQuery($saved, 'isSystemAgent');
$is_mailing_list = $this->getBoolFromQuery($saved, 'isMailingList');
$needs_approval = $this->getBoolFromQuery($saved, 'needsApproval');
$form
->appendChild(
id(new AphrontFormTextControl())
->setName('usernames')
->setLabel(pht('Usernames'))
->setValue(implode(', ', $usernames)))
->appendChild(
id(new AphrontFormTextControl())
->setName('nameLike')
->setLabel(pht('Name Contains'))
->setValue($like))
->appendChild(
id(new AphrontFormSelectControl())
->setName('isAdmin')
->setLabel(pht('Administrators'))
->setValue($is_admin)
->setOptions(
array(
'' => pht('(Show All)'),
'true' => pht('Show Only Administrators'),
'false' => pht('Hide Administrators'),
)))
->appendChild(
id(new AphrontFormSelectControl())
->setName('isDisabled')
->setLabel(pht('Disabled'))
->setValue($is_disabled)
->setOptions(
array(
'' => pht('(Show All)'),
'true' => pht('Show Only Disabled Users'),
'false' => pht('Hide Disabled Users'),
)))
->appendChild(
id(new AphrontFormSelectControl())
->setName('isSystemAgent')
->setLabel(pht('Bots'))
->setValue($is_system_agent)
->setOptions(
array(
'' => pht('(Show All)'),
'true' => pht('Show Only Bots'),
'false' => pht('Hide Bots'),
)))
->appendChild(
id(new AphrontFormSelectControl())
->setName('isMailingList')
->setLabel(pht('Mailing Lists'))
->setValue($is_mailing_list)
->setOptions(
array(
'' => pht('(Show All)'),
'true' => pht('Show Only Mailing Lists'),
'false' => pht('Hide Mailing Lists'),
)))
->appendChild(
id(new AphrontFormSelectControl())
->setName('needsApproval')
->setLabel(pht('Needs Approval'))
->setValue($needs_approval)
->setOptions(
array(
'' => pht('(Show All)'),
'true' => pht('Show Only Unapproved Users'),
'false' => pht('Hide Unapproved Users'),
)));
$this->appendCustomFieldsToForm($form, $saved);
$this->buildDateRange(
$form,
$saved,
'createdStart',
pht('Joined After'),
'createdEnd',
pht('Joined Before'));
}
protected function getURI($path) { protected function getURI($path) {
return '/people/'.$path; return '/people/'.$path;
} }

View file

@ -200,13 +200,81 @@ abstract class PhabricatorApplicationSearchEngine extends Phobject {
$fields[] = $custom_field; $fields[] = $custom_field;
} }
return $fields; $field_map = array();
foreach ($fields as $field) {
$key = $field->getKey();
if (isset($field_map[$key])) {
throw new Exception(
pht(
'Two fields in this SearchEngine use the same key ("%s"), but '.
'each field must use a unique key.',
$key));
}
$field_map[$key] = $field;
}
return $this->adjustFieldsForDisplay($field_map);
}
private function adjustFieldsForDisplay(array $field_map) {
$order = $this->getDefaultFieldOrder();
$head_keys = array();
$tail_keys = array();
$seen_tail = false;
foreach ($order as $order_key) {
if ($order_key === '...') {
$seen_tail = true;
continue;
}
if (!$seen_tail) {
$head_keys[] = $order_key;
} else {
$tail_keys[] = $order_key;
}
}
$head = array_select_keys($field_map, $head_keys);
$body = array_diff_key($field_map, array_fuse($tail_keys));
$tail = array_select_keys($field_map, $tail_keys);
return $head + $body + $tail;
} }
protected function buildCustomSearchFields() { protected function buildCustomSearchFields() {
throw new PhutilMethodNotImplementedException(); throw new PhutilMethodNotImplementedException();
} }
/**
* Define the default display order for fields by returning a list of
* field keys.
*
* You can use the special key `...` to mean "all unspecified fields go
* here". This lets you easily put important fields at the top of the form,
* standard fields in the middle of the form, and less important fields at
* the bottom.
*
* For example, you might return a list like this:
*
* return array(
* 'authorPHIDs',
* 'reviewerPHIDs',
* '...',
* 'createdAfter',
* 'createdBefore',
* );
*
* Any unspecified fields (including custom fields and fields added
* automatically by infrastruture) will be put in the middle.
*
* @return list<string> Default ordering for field keys.
*/
protected function getDefaultFieldOrder() {
return array();
}
public function getErrors() { public function getErrors() {
return $this->errors; return $this->errors;
} }
@ -1101,8 +1169,7 @@ abstract class PhabricatorApplicationSearchEngine extends Phobject {
return 'custom:'.$field->getFieldIndex(); return 'custom:'.$field->getFieldIndex();
} }
private function buildCustomFieldSearchFields() {
protected function buildCustomFieldSearchFields() {
$list = $this->getCustomFieldList(); $list = $this->getCustomFieldList();
if (!$list) { if (!$list) {
return array(); return array();