1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-01 03:02:43 +01:00
phorge-phorge/src/infrastructure/customfield/field/PhabricatorStandardCustomField.php
epriestley c8574cf6fd Integrate ApplicationSearch with CustomField
Summary:
Ref T2625. Ref T3794. Ref T418. Ref T1703.

This is a more general version of D5278. It expands CustomField support to include real integration with ApplicationSearch.

Broadly, custom fields may elect to:

  - build indicies when objects are updated;
  - populate ApplicationSearch forms with new controls;
  - read inputs entered into those controls out of the request; and
  - apply constraints to search queries.

Some utility/helper stuff is provided to make this easier. This part could be cleaner, but seems reasonable for a first cut. In particular, the Query and SearchEngine must manually call all the hooks right now instead of everything happening magically. I think that's fine for the moment; they're pretty easy to get right.

Test Plan:
I added a new searchable "Company" field to People:

{F58229}

This also cleaned up the disable/reorder view a little bit:

{F58230}

As it did before, this field appears on the edit screen:

{F58231}

However, because it has `search`, it also appears on the search screen:

{F58232}

When queried, it returns the expected results:

{F58233}

And the actually good bit of all this is that the query can take advantage of indexes:

  mysql> explain SELECT * FROM `user` user JOIN `user_customfieldstringindex` `appsearch_0` ON `appsearch_0`.objectPHID = user.phid AND `appsearch_0`.indexKey = 'mk3Ndy476ge6' AND `appsearch_0`.indexValue IN ('phacility') ORDER BY user.id DESC LIMIT 101;
  +----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
  | id | select_type | table       | type   | possible_keys     | key      | key_len | ref                                      | rows | Extra                                        |
  +----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
  |  1 | SIMPLE      | appsearch_0 | ref    | key_join,key_find | key_find | 232     | const,const                              |    1 | Using where; Using temporary; Using filesort |
  |  1 | SIMPLE      | user        | eq_ref | phid              | phid     | 194     | phabricator2_user.appsearch_0.objectPHID |    1 |                                              |
  +----+-------------+-------------+--------+-------------------+----------+---------+------------------------------------------+------+----------------------------------------------+
  2 rows in set (0.00 sec)

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T418, T1703, T2625, T3794

Differential Revision: https://secure.phabricator.com/D6992
2013-09-16 13:44:34 -07:00

237 lines
5.5 KiB
PHP

<?php
final class PhabricatorStandardCustomField
extends PhabricatorCustomField {
private $fieldKey;
private $fieldName;
private $fieldType;
private $fieldValue;
private $fieldDescription;
private $fieldConfig;
private $applicationField;
public static function buildStandardFields(
PhabricatorCustomField $template,
array $config) {
$fields = array();
foreach ($config as $key => $value) {
$namespace = $template->getStandardCustomFieldNamespace();
$full_key = "std:{$namespace}:{$key}";
$template = clone $template;
$standard = id(new PhabricatorStandardCustomField($full_key))
->setFieldConfig($value)
->setApplicationField($template);
$field = $template->setProxy($standard);
$fields[] = $field;
}
return $fields;
}
public function __construct($key) {
$this->fieldKey = $key;
}
public function setApplicationField(
PhabricatorStandardCustomFieldInterface $application_field) {
$this->applicationField = $application_field;
return $this;
}
public function getApplicationField() {
return $this->applicationField;
}
public function setFieldName($name) {
$this->fieldName = $name;
return $this;
}
public function setFieldType($type) {
$this->fieldType = $type;
return $this;
}
public function getFieldType() {
return $this->fieldType;
}
public function getFieldValue() {
return $this->fieldValue;
}
public function setFieldValue($value) {
$this->fieldValue = $value;
return $this;
}
public function setFieldDescription($description) {
$this->fieldDescription = $description;
return $this;
}
public function setFieldConfig(array $config) {
$this->setFieldType('text');
foreach ($config as $key => $value) {
switch ($key) {
case 'name':
$this->setFieldName($value);
break;
case 'type':
$this->setFieldType($value);
break;
case 'description':
$this->setFieldDescription($value);
break;
}
}
$this->fieldConfig = $config;
return $this;
}
public function getFieldConfigValue($key, $default = null) {
return idx($this->fieldConfig, $key, $default);
}
/* -( PhabricatorCustomField )--------------------------------------------- */
public function getFieldKey() {
return $this->fieldKey;
}
public function getFieldName() {
return coalesce($this->fieldName, parent::getFieldName());
}
public function getFieldDescription() {
return coalesce($this->fieldDescription, parent::getFieldDescription());
}
public function shouldUseStorage() {
return true;
}
public function getValueForStorage() {
return $this->getFieldValue();
}
public function setValueFromStorage($value) {
return $this->setFieldValue($value);
}
public function shouldAppearInApplicationTransactions() {
return true;
}
public function shouldAppearInEditView() {
return $this->getFieldConfigValue('edit', true);
}
public function readValueFromRequest(AphrontRequest $request) {
$this->setFieldValue($request->getStr($this->getFieldKey()));
}
public function renderEditControl() {
$type = $this->getFieldType();
switch ($type) {
case 'text':
default:
return id(new AphrontFormTextControl())
->setName($this->getFieldKey())
->setValue($this->getFieldValue())
->setLabel($this->getFieldName());
}
}
public function newStorageObject() {
return $this->getApplicationField()->newStorageObject();
}
public function shouldAppearInPropertyView() {
return $this->getFieldConfigValue('view', true);
}
public function renderPropertyViewValue() {
return $this->getFieldValue();
}
public function shouldAppearInApplicationSearch() {
return $this->getFieldConfigValue('search', false);
}
protected function newStringIndexStorage() {
return $this->getApplicationField()->newStringIndexStorage();
}
protected function newNumericIndexStorage() {
return $this->getApplicationField()->newNumericIndexStorage();
}
public function buildFieldIndexes() {
$type = $this->getFieldType();
switch ($type) {
case 'text':
default:
return array(
$this->newStringIndex($this->getFieldValue()),
);
}
}
public function readApplicationSearchValueFromRequest(
PhabricatorApplicationSearchEngine $engine,
AphrontRequest $request) {
$type = $this->getFieldType();
switch ($type) {
case 'text':
default:
return $request->getStr('std:'.$this->getFieldIndex());
}
}
public function applyApplicationSearchConstraintToQuery(
PhabricatorApplicationSearchEngine $engine,
PhabricatorCursorPagedPolicyAwareQuery $query,
$value) {
$type = $this->getFieldType();
switch ($type) {
case 'text':
default:
if (strlen($value)) {
$query->withApplicationSearchContainsConstraint(
$this->newStringIndex(null),
$value);
}
break;
}
}
public function appendToApplicationSearchForm(
PhabricatorApplicationSearchEngine $engine,
AphrontFormView $form,
$value,
array $handles) {
$type = $this->getFieldType();
switch ($type) {
case 'text':
default:
$form->appendChild(
id(new AphrontFormTextControl())
->setLabel($this->getFieldName())
->setName('std:'.$this->getFieldIndex())
->setValue($value));
break;
}
}
}