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

Move Projects to SearchFields

Summary:
Ref T8441. Ref T7715. This is mostly about getting SearchFields + CustomFields working.

(This includes a couple of SearchFields which aren't used quite yet.)

Test Plan:
  - Used all search controls.
  - Defined custom fields and searched for them.
  - Created an old saved search which searches on custom fields on master, switched to this patch, search worked exaclty as written.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T7715, T8441

Differential Revision: https://secure.phabricator.com/D13189
This commit is contained in:
epriestley 2015-06-08 12:20:16 -07:00
parent ace7505f2e
commit c8b866a956
11 changed files with 292 additions and 162 deletions

View file

@ -2498,8 +2498,10 @@ phutil_register_library_map(array(
'PhabricatorSearchCheckboxesField' => 'applications/search/field/PhabricatorSearchCheckboxesField.php', 'PhabricatorSearchCheckboxesField' => 'applications/search/field/PhabricatorSearchCheckboxesField.php',
'PhabricatorSearchConfigOptions' => 'applications/search/config/PhabricatorSearchConfigOptions.php', 'PhabricatorSearchConfigOptions' => 'applications/search/config/PhabricatorSearchConfigOptions.php',
'PhabricatorSearchController' => 'applications/search/controller/PhabricatorSearchController.php', 'PhabricatorSearchController' => 'applications/search/controller/PhabricatorSearchController.php',
'PhabricatorSearchCustomFieldProxyField' => 'applications/search/field/PhabricatorSearchCustomFieldProxyField.php',
'PhabricatorSearchDAO' => 'applications/search/storage/PhabricatorSearchDAO.php', 'PhabricatorSearchDAO' => 'applications/search/storage/PhabricatorSearchDAO.php',
'PhabricatorSearchDatasource' => 'applications/search/typeahead/PhabricatorSearchDatasource.php', 'PhabricatorSearchDatasource' => 'applications/search/typeahead/PhabricatorSearchDatasource.php',
'PhabricatorSearchDatasourceField' => 'applications/search/field/PhabricatorSearchDatasourceField.php',
'PhabricatorSearchDateField' => 'applications/search/field/PhabricatorSearchDateField.php', 'PhabricatorSearchDateField' => 'applications/search/field/PhabricatorSearchDateField.php',
'PhabricatorSearchDeleteController' => 'applications/search/controller/PhabricatorSearchDeleteController.php', 'PhabricatorSearchDeleteController' => 'applications/search/controller/PhabricatorSearchDeleteController.php',
'PhabricatorSearchDocument' => 'applications/search/storage/document/PhabricatorSearchDocument.php', 'PhabricatorSearchDocument' => 'applications/search/storage/document/PhabricatorSearchDocument.php',
@ -2518,13 +2520,16 @@ phutil_register_library_map(array(
'PhabricatorSearchManagementInitWorkflow' => 'applications/search/management/PhabricatorSearchManagementInitWorkflow.php', 'PhabricatorSearchManagementInitWorkflow' => 'applications/search/management/PhabricatorSearchManagementInitWorkflow.php',
'PhabricatorSearchManagementWorkflow' => 'applications/search/management/PhabricatorSearchManagementWorkflow.php', 'PhabricatorSearchManagementWorkflow' => 'applications/search/management/PhabricatorSearchManagementWorkflow.php',
'PhabricatorSearchOrderController' => 'applications/search/controller/PhabricatorSearchOrderController.php', 'PhabricatorSearchOrderController' => 'applications/search/controller/PhabricatorSearchOrderController.php',
'PhabricatorSearchOwnersField' => 'applications/search/field/PhabricatorSearchOwnersField.php',
'PhabricatorSearchPreferencesSettingsPanel' => 'applications/settings/panel/PhabricatorSearchPreferencesSettingsPanel.php', 'PhabricatorSearchPreferencesSettingsPanel' => 'applications/settings/panel/PhabricatorSearchPreferencesSettingsPanel.php',
'PhabricatorSearchProjectsField' => 'applications/search/field/PhabricatorSearchProjectsField.php', 'PhabricatorSearchProjectsField' => 'applications/search/field/PhabricatorSearchProjectsField.php',
'PhabricatorSearchRelationship' => 'applications/search/constants/PhabricatorSearchRelationship.php', 'PhabricatorSearchRelationship' => 'applications/search/constants/PhabricatorSearchRelationship.php',
'PhabricatorSearchResultView' => 'applications/search/view/PhabricatorSearchResultView.php', 'PhabricatorSearchResultView' => 'applications/search/view/PhabricatorSearchResultView.php',
'PhabricatorSearchSelectController' => 'applications/search/controller/PhabricatorSearchSelectController.php', 'PhabricatorSearchSelectController' => 'applications/search/controller/PhabricatorSearchSelectController.php',
'PhabricatorSearchSelectField' => 'applications/search/field/PhabricatorSearchSelectField.php',
'PhabricatorSearchSpacesField' => 'applications/search/field/PhabricatorSearchSpacesField.php', 'PhabricatorSearchSpacesField' => 'applications/search/field/PhabricatorSearchSpacesField.php',
'PhabricatorSearchStringListField' => 'applications/search/field/PhabricatorSearchStringListField.php', 'PhabricatorSearchStringListField' => 'applications/search/field/PhabricatorSearchStringListField.php',
'PhabricatorSearchTextField' => 'applications/search/field/PhabricatorSearchTextField.php',
'PhabricatorSearchThreeStateField' => 'applications/search/field/PhabricatorSearchThreeStateField.php', 'PhabricatorSearchThreeStateField' => 'applications/search/field/PhabricatorSearchThreeStateField.php',
'PhabricatorSearchTokenizerField' => 'applications/search/field/PhabricatorSearchTokenizerField.php', 'PhabricatorSearchTokenizerField' => 'applications/search/field/PhabricatorSearchTokenizerField.php',
'PhabricatorSearchUsersField' => 'applications/search/field/PhabricatorSearchUsersField.php', 'PhabricatorSearchUsersField' => 'applications/search/field/PhabricatorSearchUsersField.php',
@ -5998,8 +6003,10 @@ phutil_register_library_map(array(
'PhabricatorSearchCheckboxesField' => 'PhabricatorSearchField', 'PhabricatorSearchCheckboxesField' => 'PhabricatorSearchField',
'PhabricatorSearchConfigOptions' => 'PhabricatorApplicationConfigOptions', 'PhabricatorSearchConfigOptions' => 'PhabricatorApplicationConfigOptions',
'PhabricatorSearchController' => 'PhabricatorSearchBaseController', 'PhabricatorSearchController' => 'PhabricatorSearchBaseController',
'PhabricatorSearchCustomFieldProxyField' => 'PhabricatorSearchField',
'PhabricatorSearchDAO' => 'PhabricatorLiskDAO', 'PhabricatorSearchDAO' => 'PhabricatorLiskDAO',
'PhabricatorSearchDatasource' => 'PhabricatorTypeaheadCompositeDatasource', 'PhabricatorSearchDatasource' => 'PhabricatorTypeaheadCompositeDatasource',
'PhabricatorSearchDatasourceField' => 'PhabricatorSearchTokenizerField',
'PhabricatorSearchDateField' => 'PhabricatorSearchField', 'PhabricatorSearchDateField' => 'PhabricatorSearchField',
'PhabricatorSearchDeleteController' => 'PhabricatorSearchBaseController', 'PhabricatorSearchDeleteController' => 'PhabricatorSearchBaseController',
'PhabricatorSearchDocument' => 'PhabricatorSearchDAO', 'PhabricatorSearchDocument' => 'PhabricatorSearchDAO',
@ -6016,12 +6023,15 @@ phutil_register_library_map(array(
'PhabricatorSearchManagementInitWorkflow' => 'PhabricatorSearchManagementWorkflow', 'PhabricatorSearchManagementInitWorkflow' => 'PhabricatorSearchManagementWorkflow',
'PhabricatorSearchManagementWorkflow' => 'PhabricatorManagementWorkflow', 'PhabricatorSearchManagementWorkflow' => 'PhabricatorManagementWorkflow',
'PhabricatorSearchOrderController' => 'PhabricatorSearchBaseController', 'PhabricatorSearchOrderController' => 'PhabricatorSearchBaseController',
'PhabricatorSearchOwnersField' => 'PhabricatorSearchTokenizerField',
'PhabricatorSearchPreferencesSettingsPanel' => 'PhabricatorSettingsPanel', 'PhabricatorSearchPreferencesSettingsPanel' => 'PhabricatorSettingsPanel',
'PhabricatorSearchProjectsField' => 'PhabricatorSearchTokenizerField', 'PhabricatorSearchProjectsField' => 'PhabricatorSearchTokenizerField',
'PhabricatorSearchResultView' => 'AphrontView', 'PhabricatorSearchResultView' => 'AphrontView',
'PhabricatorSearchSelectController' => 'PhabricatorSearchBaseController', 'PhabricatorSearchSelectController' => 'PhabricatorSearchBaseController',
'PhabricatorSearchSelectField' => 'PhabricatorSearchField',
'PhabricatorSearchSpacesField' => 'PhabricatorSearchTokenizerField', 'PhabricatorSearchSpacesField' => 'PhabricatorSearchTokenizerField',
'PhabricatorSearchStringListField' => 'PhabricatorSearchField', 'PhabricatorSearchStringListField' => 'PhabricatorSearchField',
'PhabricatorSearchTextField' => 'PhabricatorSearchField',
'PhabricatorSearchThreeStateField' => 'PhabricatorSearchField', 'PhabricatorSearchThreeStateField' => 'PhabricatorSearchField',
'PhabricatorSearchTokenizerField' => 'PhabricatorSearchField', 'PhabricatorSearchTokenizerField' => 'PhabricatorSearchField',
'PhabricatorSearchUsersField' => 'PhabricatorSearchTokenizerField', 'PhabricatorSearchUsersField' => 'PhabricatorSearchTokenizerField',

View file

@ -11,133 +11,66 @@ final class PhabricatorProjectSearchEngine
return 'PhabricatorProjectApplication'; return 'PhabricatorProjectApplication';
} }
public function getCustomFieldObject() { public function newResultObject() {
return new PhabricatorProject(); return new PhabricatorProject();
} }
public function buildSavedQueryFromRequest(AphrontRequest $request) { protected function buildCustomSearchFields() {
$saved = new PhabricatorSavedQuery(); return array(
id(new PhabricatorSearchTextField())
$saved->setParameter( ->setLabel(pht('Name'))
'memberPHIDs', ->setKey('name'),
$this->readUsersFromRequest($request, 'members')); id(new PhabricatorSearchUsersField())
->setLabel(pht('Members'))
$saved->setParameter('status', $request->getStr('status')); ->setKey('memberPHIDs')
$saved->setParameter('name', $request->getStr('name')); ->setAliases(array('member', 'members')),
id(new PhabricatorSearchSelectField())
$saved->setParameter( ->setLabel(pht('Status'))
'icons', ->setKey('status')
$this->readListFromRequest($request, 'icons')); ->setOptions($this->getStatusOptions()),
id(new PhabricatorSearchCheckboxesField())
$saved->setParameter( ->setLabel(pht('Icons'))
'colors', ->setKey('icons')
$this->readListFromRequest($request, 'colors')); ->setOptions($this->getIconOptions()),
id(new PhabricatorSearchCheckboxesField())
$this->readCustomFieldsFromRequest($request, $saved); ->setLabel(pht('Colors'))
->setKey('colors')
return $saved; ->setOptions($this->getColorOptions()),
);
} }
public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) {
public function buildQueryFromParameters(array $map) {
$query = id(new PhabricatorProjectQuery()) $query = id(new PhabricatorProjectQuery())
->needImages(true); ->needImages(true);
$member_phids = $saved->getParameter('memberPHIDs', array()); if (strlen($map['name'])) {
if ($member_phids && is_array($member_phids)) { $tokens = PhabricatorTypeaheadDatasource::tokenizeString($map['name']);
$query->withMemberPHIDs($member_phids);
}
$status = $saved->getParameter('status');
$status = idx($this->getStatusValues(), $status);
if ($status) {
$query->withStatus($status);
}
$name = $saved->getParameter('name');
if (strlen($name)) {
$tokens = PhabricatorTypeaheadDatasource::tokenizeString($name);
$query->withNameTokens($tokens); $query->withNameTokens($tokens);
} }
$icons = $saved->getParameter('icons'); if ($map['memberPHIDs']) {
if ($icons) { $query->withMemberPHIDs($map['memberPHIDs']);
$query->withIcons($icons);
} }
$colors = $saved->getParameter('colors'); if ($map['status']) {
if ($colors) { $status = idx($this->getStatusValues(), $map['status']);
$query->withColors($colors); if ($status) {
$query->withStatus($status);
}
} }
$this->applyCustomFieldsToQuery($query, $saved); if ($map['icons']) {
$query->withIcons($map['icons']);
}
if ($map['colors']) {
$query->withColors($map['colors']);
}
return $query; return $query;
} }
public function buildSearchForm(
AphrontFormView $form,
PhabricatorSavedQuery $saved) {
$member_phids = $saved->getParameter('memberPHIDs', array());
$status = $saved->getParameter('status');
$name_match = $saved->getParameter('name');
$icons = array_fuse($saved->getParameter('icons', array()));
$colors = array_fuse($saved->getParameter('colors', array()));
$icon_control = id(new AphrontFormCheckboxControl())
->setLabel(pht('Icons'));
foreach (PhabricatorProjectIcon::getIconMap() as $icon => $name) {
$image = id(new PHUIIconView())
->setIconFont($icon);
$icon_control->addCheckbox(
'icons[]',
$icon,
array($image, ' ', $name),
isset($icons[$icon]));
}
$color_control = id(new AphrontFormCheckboxControl())
->setLabel(pht('Colors'));
foreach (PhabricatorProjectIcon::getColorMap() as $color => $name) {
$tag = id(new PHUITagView())
->setType(PHUITagView::TYPE_SHADE)
->setShade($color)
->setName($name);
$color_control->addCheckbox(
'colors[]',
$color,
$tag,
isset($colors[$color]));
}
$form
->appendChild(
id(new AphrontFormTextControl())
->setName('name')
->setLabel(pht('Name'))
->setValue($name_match))
->appendControl(
id(new AphrontFormTokenizerControl())
->setDatasource(new PhabricatorPeopleDatasource())
->setName('members')
->setLabel(pht('Members'))
->setValue($member_phids))
->appendChild(
id(new AphrontFormSelectControl())
->setLabel(pht('Status'))
->setName('status')
->setOptions($this->getStatusOptions())
->setValue($status))
->appendChild($icon_control)
->appendChild($color_control);
$this->appendCustomFieldsToForm($form, $saved);
}
protected function getURI($path) { protected function getURI($path) {
return '/project/'.$path; return '/project/'.$path;
} }
@ -192,14 +125,36 @@ final class PhabricatorProjectSearchEngine
); );
} }
private function getColorValues() {} private function getIconOptions() {
$options = array();
private function getIconValues() {} foreach (PhabricatorProjectIcon::getIconMap() as $icon => $name) {
$options[$icon] = array(
id(new PHUIIconView())
->setIconFont($icon),
' ',
$name,
);
}
protected function getRequiredHandlePHIDsForResultList( return $options;
array $projects, }
PhabricatorSavedQuery $query) {
return mpull($projects, 'getPHID'); private function getColorOptions() {
$options = array();
foreach (PhabricatorProjectIcon::getColorMap() as $color => $name) {
$options[$color] = array(
id(new PHUITagView())
->setType(PHUITagView::TYPE_SHADE)
->setShade($color)
->setName($name),
' ',
$name,
);
}
return $options;
} }
protected function renderResultList( protected function renderResultList(
@ -208,6 +163,7 @@ final class PhabricatorProjectSearchEngine
array $handles) { array $handles) {
assert_instances_of($projects, 'PhabricatorProject'); assert_instances_of($projects, 'PhabricatorProject');
$viewer = $this->requireViewer(); $viewer = $this->requireViewer();
$handles = $viewer->loadHandles(mpull($projects, 'getPHID'));
$list = new PHUIObjectItemListView(); $list = new PHUIObjectItemListView();
$list->setUser($viewer); $list->setUser($viewer);

View file

@ -127,6 +127,10 @@ abstract class PhabricatorApplicationSearchEngine extends Phobject {
} }
} }
if ($object instanceof PhabricatorCustomFieldInterface) {
$this->applyCustomFieldsToQuery($query, $saved);
}
return $query; return $query;
} }
@ -192,6 +196,10 @@ abstract class PhabricatorApplicationSearchEngine extends Phobject {
} }
} }
foreach ($this->buildCustomFieldSearchFields() as $custom_field) {
$fields[] = $custom_field;
}
return $fields; return $fields;
} }
@ -1094,14 +1102,22 @@ abstract class PhabricatorApplicationSearchEngine extends Phobject {
} }
/** protected function buildCustomFieldSearchFields() {
* Add inputs to an application search form so the user can query on custom $list = $this->getCustomFieldList();
* fields. if (!$list) {
* return array();
* @param AphrontFormView Form to update. }
* @param PhabricatorSavedQuery Values to prefill.
* @return void $fields = array();
*/ foreach ($list->getFields() as $field) {
$fields[] = id(new PhabricatorSearchCustomFieldProxyField())
->setSearchEngine($this)
->setCustomField($field);
}
return $fields;
}
// TODO: Remove.
protected function appendCustomFieldsToForm( protected function appendCustomFieldsToForm(
AphrontFormView $form, AphrontFormView $form,
PhabricatorSavedQuery $saved) { PhabricatorSavedQuery $saved) {

View file

@ -0,0 +1,58 @@
<?php
final class PhabricatorSearchCustomFieldProxyField
extends PhabricatorSearchField {
private $searchEngine;
private $customField;
public function setSearchEngine(PhabricatorApplicationSearchEngine $engine) {
$this->searchEngine = $engine;
return $this;
}
public function getSearchEngine() {
return $this->searchEngine;
}
public function setCustomField(PhabricatorCustomField $field) {
$this->customField = $field;
$this->setKey('custom:'.$field->getFieldIndex());
$aliases = array();
$aliases[] = $field->getFieldKey();
$this->setAliases($aliases);
return $this;
}
public function getCustomField() {
return $this->customField;
}
protected function getDefaultValue() {
return null;
}
protected function getValueExistsInRequest(AphrontRequest $request, $key) {
// TODO: For historical reasons, the keys we look for don't line up with
// the keys that CustomFields use. Just skip the check for existence and
// always read the value. It would be vaguely nice to make rendering more
// consistent instead.
return true;
}
protected function getValueFromRequest(AphrontRequest $request, $key) {
return $this->getCustomField()->readApplicationSearchValueFromRequest(
$this->getSearchEngine(),
$request);
}
public function appendToForm(AphrontFormView $form) {
return $this->getCustomField()->appendToApplicationSearchForm(
$this->getSearchEngine(),
$form,
$this->getValue());
}
}

View file

@ -0,0 +1,17 @@
<?php
final class PhabricatorSearchDatasourceField
extends PhabricatorSearchTokenizerField {
private $datasource;
protected function newDatasource() {
return id(clone $this->datasource);
}
public function setDatasource(PhabricatorTypeaheadDatasource $datasource) {
$this->datasource = $datasource;
return $this;
}
}

View file

@ -209,7 +209,9 @@ abstract class PhabricatorSearchField extends Phobject {
/* -( Rendering Controls )------------------------------------------------- */ /* -( Rendering Controls )------------------------------------------------- */
abstract protected function newControl(); protected function newControl() {
throw new PhutilMethodNotImplementedException();
}
protected function renderControl() { protected function renderControl() {

View file

@ -0,0 +1,18 @@
<?php
final class PhabricatorSearchOwnersField
extends PhabricatorSearchTokenizerField {
protected function getDefaultValue() {
return array();
}
protected function getValueFromRequest(AphrontRequest $request, $key) {
return $this->getUsersFromRequest($request, $key);
}
protected function newDatasource() {
return new PhabricatorPeopleOwnerDatasource();
}
}

View file

@ -0,0 +1,30 @@
<?php
final class PhabricatorSearchSelectField
extends PhabricatorSearchField {
private $options;
public function setOptions(array $options) {
$this->options = $options;
return $this;
}
public function getOptions() {
return $this->options;
}
protected function getDefaultValue() {
return null;
}
protected function getValueFromRequest(AphrontRequest $request, $key) {
return $request->getStr($key);
}
protected function newControl() {
return id(new AphrontFormSelectControl())
->setOptions($this->getOptions());
}
}

View file

@ -0,0 +1,18 @@
<?php
final class PhabricatorSearchTextField
extends PhabricatorSearchField {
protected function getDefaultValue() {
return '';
}
protected function getValueFromRequest(AphrontRequest $request, $key) {
return $request->getStr($key);
}
protected function newControl() {
return new AphrontFormTextControl();
}
}

View file

@ -25,4 +25,44 @@ abstract class PhabricatorSearchTokenizerField
abstract protected function newDatasource(); abstract protected function newDatasource();
protected function getUsersFromRequest(AphrontRequest $request, $key) {
$list = $this->getListFromRequest($request, $key);
$allow_types = array();
$phids = array();
$names = array();
$allow_types = array_fuse($allow_types);
$user_type = PhabricatorPeopleUserPHIDType::TYPECONST;
foreach ($list as $item) {
$type = phid_get_type($item);
if ($type == $user_type) {
$phids[] = $item;
} else if (isset($allow_types[$type])) {
$phids[] = $item;
} else {
if (PhabricatorTypeaheadDatasource::isFunctionToken($item)) {
// If this is a function, pass it through unchanged; we'll evaluate
// it later.
$phids[] = $item;
} else {
$names[] = $item;
}
}
}
if ($names) {
$users = id(new PhabricatorPeopleQuery())
->setViewer($this->getViewer())
->withUsernames($names)
->execute();
foreach ($users as $user) {
$phids[] = $user->getPHID();
}
$phids = array_unique($phids);
}
return $phids;
}
} }

View file

@ -7,47 +7,12 @@ final class PhabricatorSearchUsersField
return array(); return array();
} }
protected function getValueFromRequest(AphrontRequest $request, $key) {
return $this->getUsersFromRequest($request, $key);
}
protected function newDatasource() { protected function newDatasource() {
return new PhabricatorPeopleUserFunctionDatasource(); return new PhabricatorPeopleUserFunctionDatasource();
} }
protected function getValueFromRequest(AphrontRequest $request, $key) {
$list = $this->getListFromRequest($request, $key);
$allow_types = array();
$phids = array();
$names = array();
$allow_types = array_fuse($allow_types);
$user_type = PhabricatorPeopleUserPHIDType::TYPECONST;
foreach ($list as $item) {
$type = phid_get_type($item);
if ($type == $user_type) {
$phids[] = $item;
} else if (isset($allow_types[$type])) {
$phids[] = $item;
} else {
if (PhabricatorTypeaheadDatasource::isFunctionToken($item)) {
// If this is a function, pass it through unchanged; we'll evaluate
// it later.
$phids[] = $item;
} else {
$names[] = $item;
}
}
}
if ($names) {
$users = id(new PhabricatorPeopleQuery())
->setViewer($this->getViewer())
->withUsernames($names)
->execute();
foreach ($users as $user) {
$phids[] = $user->getPHID();
}
$phids = array_unique($phids);
}
return $phids;
}
} }