Skip to content

Commit

Permalink
Standardise custom fields metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 1, 2019
1 parent 18a4320 commit 13258de
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 23 deletions.
34 changes: 12 additions & 22 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,18 @@ public static function &getFields(

$fields = array();
while (($dao->fetch()) != NULL) {
$regexp = preg_replace('/[.,;:!?]/', '', NULL);
$fields[$dao->id]['id'] = $dao->id;
$fields[$dao->id]['label'] = $dao->label;
// This seems broken, but not in a new way.
$fields[$dao->id]['headerPattern'] = '/' . preg_quote($regexp, '/') . '/';
// To support the consolidation of various functions & their expectations.
$fields[$dao->id]['title'] = $dao->label;
$fields[$dao->id]['custom_field_id'] = $dao->id;
$fields[$dao->id]['groupTitle'] = $dao->title;
$fields[$dao->id]['data_type'] = $dao->data_type;
$fields[$dao->id]['name'] = 'custom_' . $dao->id;
$fields[$dao->id]['type'] = CRM_Utils_Array::value($dao->data_type, self::dataToType());
$fields[$dao->id]['html_type'] = $dao->html_type;
$fields[$dao->id]['default_value'] = $dao->default_value;
$fields[$dao->id]['text_length'] = $dao->text_length;
Expand All @@ -656,6 +664,7 @@ public static function &getFields(
$fields[$dao->id]['is_required'] = $dao->is_required;
$fields[$dao->id]['table_name'] = $dao->table_name;
$fields[$dao->id]['column_name'] = $dao->column_name;
$fields[$dao->id]['where'] = $dao->table_name . '.' . $dao->column_name;
// Probably we should use a different fn to get the extends tables but this is a refactor so not changing that now.
$fields[$dao->id]['extends_table'] = array_key_exists($dao->extends, CRM_Core_BAO_CustomQuery::$extendsMap) ? CRM_Core_BAO_CustomQuery::$extendsMap[$dao->extends] : '';
if (in_array($dao->extends, CRM_Contact_BAO_ContactType::subTypes())) {
Expand Down Expand Up @@ -716,7 +725,7 @@ public static function getFieldsForImport(
$checkPermission
);

$importableFields = array();
$importableFields = [];
foreach ($fields as $id => $values) {
// for now we should not allow multiple fields in profile / export etc, hence unsetting
if (!$search &&
Expand All @@ -728,27 +737,8 @@ public static function getFieldsForImport(
/* generate the key for the fields array */

$key = "custom_$id";

$regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values));
$importableFields[$key] = array(
'name' => $key,
'type' => CRM_Utils_Array::value(CRM_Utils_Array::value('data_type', $values), self::dataToType()),
'title' => CRM_Utils_Array::value('label', $values),
'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
'import' => 1,
'custom_field_id' => $id,
'options_per_line' => CRM_Utils_Array::value('options_per_line', $values),
'text_length' => CRM_Utils_Array::value('text_length', $values, 255),
'data_type' => CRM_Utils_Array::value('data_type', $values),
'html_type' => CRM_Utils_Array::value('html_type', $values),
'is_search_range' => CRM_Utils_Array::value('is_search_range', $values),
);

// CRM-6681, pass date and time format when html_type = Select Date
if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') {
$importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values);
$importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values);
}
$importableFields[$key] = $values;
$importableFields[$key]['import'] = 1;
}

return $importableFields;
Expand Down
121 changes: 120 additions & 1 deletion tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,14 @@ protected function createCustomField($groupTitle = 'new custom group') {
}

/**
* Tet the getFieldsForImport function.
* Test the getFieldsForImport function.
*
* @throws \Exception
*/
public function testGetFieldsForImport() {
$this->entity = 'Contact';
$this->createCustomGroupWithFieldsOfAllTypes();
$customGroupID = $this->ids['CustomGroup']['Custom Group'];
$expected = [
$this->getCustomFieldName('country') => [
'name' => $this->getCustomFieldName('country') ,
Expand All @@ -417,6 +420,25 @@ public function testGetFieldsForImport() {
'data_type' => 'Int',
'html_type' => 'Select Country',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('country'),
'label' => 'Country',
'groupTitle' => 'Custom Group',
'default_value' => null,
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => null,
'extends_entity_column_id' => null,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => null,
'date_format' => null,
'time_format' => null,
'is_required' => '0',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'country_' . $this->getCustomFieldID('country'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('file') => [
'name' => $this->getCustomFieldName('file'),
Expand All @@ -430,6 +452,25 @@ public function testGetFieldsForImport() {
'data_type' => 'File',
'html_type' => 'File',
'is_search_range' => '0',
'id' => '5',
'label' => 'Custom Field',
'groupTitle' => 'Custom Group',
'default_value' => null,
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => null,
'extends_entity_column_id' => null,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => null,
'date_format' => null,
'time_format' => null,
'is_required' => '0',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'custom_field_5',
'where' => 'civicrm_value_custom_group_1.custom_field_5',
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('text') => [
'name' => $this->getCustomFieldName('text'),
Expand All @@ -443,6 +484,25 @@ public function testGetFieldsForImport() {
'data_type' => 'String',
'html_type' => 'Text',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('text'),
'label' => 'Enter text here',
'groupTitle' => 'Custom Group',
'default_value' => 'xyz',
'custom_group_id' => '1',
'extends' => 'Contact',
'extends_entity_column_value' => null,
'extends_entity_column_id' => null,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => null,
'date_format' => null,
'time_format' => null,
'is_required' => '1',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('select_string') => [
'name' => $this->getCustomFieldName('select_string'),
Expand All @@ -456,6 +516,29 @@ public function testGetFieldsForImport() {
'data_type' => 'String',
'html_type' => 'Select',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('select_string'),
'label' => 'Pick Color',
'groupTitle' => 'Custom Group',
'default_value' => NULL,
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => null,
'extends_entity_column_id' => null,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']),
'date_format' => null,
'time_format' => null,
'is_required' => '1',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'pseudoconstant' => [
'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
],
],
$this->getCustomFieldName('select_date') => [
'name' => $this->getCustomFieldName('select_date'),
Expand All @@ -471,6 +554,23 @@ public function testGetFieldsForImport() {
'is_search_range' => '0',
'date_format' => 'mm/dd/yy',
'time_format' => '1',
'id' => $this->getCustomFieldID('select_date'),
'label' => 'test_date',
'groupTitle' => 'Custom Group',
'default_value' => '20090711',
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => null,
'extends_entity_column_id' => null,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => null,
'is_required' => '0',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'test_date_3',
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
$this->getCustomFieldName('link') => [
'name' => $this->getCustomFieldName('link'),
Expand All @@ -484,6 +584,25 @@ public function testGetFieldsForImport() {
'data_type' => 'Link',
'html_type' => 'Link',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('link'),
'label' => 'test_link',
'groupTitle' => 'Custom Group',
'default_value' => 'http://civicrm.org',
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => null,
'extends_entity_column_id' => null,
'is_view' => '0',
'is_multiple' => '0',
'option_group_id' => null,
'date_format' => null,
'time_format' => null,
'is_required' => '1',
'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
'column_name' => 'test_link_' . $this->getCustomFieldID('link'),
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
];
$this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport());
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/CRM/Core/BAO/CustomQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public function testSearchCustomDataDateRelative() {
'is_required' => '0',
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'headerPattern' => '//',
'title' => 'date field',
'custom_field_id' => $dateCustomField['id'],
'name' => 'custom_' . $dateCustomField['id'],
'type' => 4,
'where' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'] . '.date_field_' . $dateCustomField['id'],
], $queryObj->getFields()[$dateCustomField['id']]);

}
Expand Down

0 comments on commit 13258de

Please sign in to comment.