Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant custom field types #18378

Merged
merged 1 commit into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ public static function buildOptions($fieldName, $context = NULL, $props = []) {
// This provides legacy support for APIv3, allowing no-longer-existent html types
if ($fieldName == 'html_type' && isset($props['version']) && $props['version'] == 3) {
$options['Multi-Select'] = 'Multi-Select';
$options['Select Country'] = 'Select Country';
$options['Multi-Select Country'] = 'Multi-Select Country';
$options['Select State/Province'] = 'Select State/Province';
$options['Multi-Select State/Province'] = 'Multi-Select State/Province';
}
return $options;
Expand Down Expand Up @@ -678,11 +680,8 @@ public static function addQuickFormElement(

$placeholder = $search ? ts('- any -') : ($useRequired ? ts('- select -') : ts('- none -'));

// FIXME: Why are select state/country separate widget types?
$isSelect = (in_array($widget, [
'Select',
'Select State/Province',
'Select Country',
'CheckBox',
'Radio',
]));
Expand Down Expand Up @@ -1057,8 +1056,6 @@ private static function formatDisplayValue($value, $field, $entityId = NULL) {
case 'Select':
case 'Autocomplete-Select':
case 'Radio':
case 'Select Country':
case 'Select State/Province':
case 'CheckBox':
if ($field['data_type'] == 'ContactReference' && $value) {
if (is_numeric($value)) {
Expand Down
2 changes: 0 additions & 2 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ public static function customHtmlType() {
'CheckBox' => ts('Checkbox(es)'),
'Select Date' => ts('Select Date'),
'File' => ts('File'),
'Select State/Province' => ts('Select State/Province'),
'Select Country' => ts('Select Country'),
'RichTextEditor' => ts('Rich Text Editor'),
'Autocomplete-Select' => ts('Autocomplete-Select'),
'Link' => ts('Link'),
Expand Down
8 changes: 4 additions & 4 deletions CRM/Custom/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class CRM_Custom_Form_Field extends CRM_Core_Form {
['TextArea' => 'TextArea', 'RichTextEditor' => 'RichTextEditor'],
['Date' => 'Select Date'],
['Radio' => 'Radio'],
['StateProvince' => 'Select State/Province'],
['Country' => 'Select Country'],
['StateProvince' => 'Select'],
['Country' => 'Select'],
['File' => 'File'],
['Link' => 'Link'],
['ContactReference' => 'Autocomplete-Select'],
Expand Down Expand Up @@ -158,8 +158,8 @@ public function preProcess() {
['TextArea' => ts('TextArea'), 'RichTextEditor' => ts('Rich Text Editor')],
['Date' => ts('Select Date')],
['Radio' => ts('Radio')],
['StateProvince' => ts('Select State/Province')],
['Country' => ts('Select Country')],
['StateProvince' => ts('Select')],
['Country' => ts('Select')],
['File' => ts('Select File')],
['Link' => ts('Link')],
['ContactReference' => ts('Autocomplete-Select')],
Expand Down
4 changes: 1 addition & 3 deletions CRM/Profile/Page/MultipleRecordFieldsListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,6 @@ public function browse() {

case 'Radio':
case 'Select':
case 'Select Country':
case 'Select State/Province':
$editable = TRUE;
$fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
if (!$spec['is_required']) {
Expand Down Expand Up @@ -408,7 +406,7 @@ public function browse() {

$headers = [];
if (!empty($fieldIDs)) {
$fields = ['Radio', 'Select', 'Select Country', 'Select State/Province'];
$fields = ['Radio', 'Select'];
foreach ($fieldIDs as $fieldID) {
if ($this->_pageViewType == 'profileDataView') {
$headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel'];
Expand Down
4 changes: 4 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.31.alpha1.mysql.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{* file to handle db changes in 5.31.alpha1 during upgrade *}

{* Remove Country & State special select fields *}
UPDATE civicrm_custom_field SET html_type = 'Select'
WHERE html_type IN ('Select Country', 'Select State/Province');
2 changes: 0 additions & 2 deletions Civi/Api4/Service/Spec/SpecFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ public static function setInputTypeAndAttrs(FieldSpec &$fieldSpec, $data, $dataT
unset($inputAttrs['type']);

$map = [
'Select State/Province' => 'Select',
'Select Country' => 'Select',
'Select Date' => 'Date',
'Link' => 'Url',
];
Expand Down
34 changes: 26 additions & 8 deletions api/v3/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
function civicrm_api3_custom_field_create($params) {

// Legacy handling for old way of naming serialized fields
if (!empty($params['html_type']) && ($params['html_type'] == 'CheckBox' || strpos($params['html_type'], 'Multi-') === 0)) {
$params['serialize'] = 1;
$params['html_type'] = str_replace('Multi-', '', $params['html_type']);
if (!empty($params['html_type'])) {
if ($params['html_type'] == 'CheckBox' || strpos($params['html_type'], 'Multi-') === 0) {
$params['serialize'] = 1;
}
$params['html_type'] = str_replace(['Multi-Select', 'Select Country', 'Select State/Province'], 'Select', $params['html_type']);
}

// Array created for passing options in params.
Expand Down Expand Up @@ -131,7 +133,14 @@ function civicrm_api3_custom_field_get($params) {
if (!in_array('serialize', $params['return'])) {
$params['return'][] = 'serialize';
}
if (!in_array('data_type', $params['return'])) {
$params['return'][] = 'data_type';
}
}
$legacyDataTypes = [
'Select State/Province' => 'StateProvince',
'Select Country' => 'Country',
];
if ($handleLegacy && !empty($params['html_type'])) {
$serializedTypes = ['CheckBox', 'Multi-Select', 'Multi-Select Country', 'Multi-Select State/Province'];
if (is_string($params['html_type'])) {
Expand All @@ -142,12 +151,16 @@ function civicrm_api3_custom_field_get($params) {
elseif (!in_array($params['html_type'], $serializedTypes)) {
$params['serialize'] = 0;
}
if (isset($legacyDataTypes[$params['html_type']])) {
$params['data_type'] = $legacyDataTypes[$params['html_type']];
unset($params['html_type']);
}
}
elseif (is_array($params['html_type']) && !empty($params['html_type']['IN'])) {
$excludeNonSerialized = !array_diff($params['html_type']['IN'], $serializedTypes);
$onlyNonSerialized = !array_intersect($params['html_type']['IN'], $serializedTypes);
$params['html_type']['IN'] = array_map(function($val) {
return str_replace('Multi-Select', 'Select', $val);
return str_replace(['Multi-Select', 'Select Country', 'Select State/Province'], 'Select', $val);
}, $params['html_type']['IN']);
if ($excludeNonSerialized) {
$params['serialize'] = 1;
Expand All @@ -160,10 +173,15 @@ function civicrm_api3_custom_field_get($params) {

$results = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);

if ($handleLegacy && !empty($results['values']) && is_array($results['values']) && !isset($params['serialize'])) {
foreach ($results['values'] as $id => $result) {
if (!empty($result['serialize']) && !empty($result['html_type'])) {
$results['values'][$id]['html_type'] = str_replace('Select', 'Multi-Select', $result['html_type']);
if ($handleLegacy && !empty($results['values']) && is_array($results['values'])) {
foreach ($results['values'] as $id => &$result) {
if (!empty($result['html_type'])) {
if (in_array($result['data_type'], $legacyDataTypes)) {
$result['html_type'] = array_search($result['data_type'], $legacyDataTypes);
}
if (!empty($result['serialize'])) {
$result['html_type'] = str_replace('Select', 'Multi-Select', $result['html_type']);
}
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,16 @@ public function testGetDisplayedValues() {
$fieldsToCreate = [
[
'data_type' => 'Country',
'html_type' => 'Select Country',
'html_type' => 'Select',
'tests' => [
'United States' => 1228,
'' => NULL,
],
],
[
'data_type' => 'StateProvince',
'html_type' => 'Multi-Select State/Province',
'html_type' => 'Select',
'serialize' => 1,
'tests' => [
'' => 0,
'Alabama' => 1000,
Expand Down Expand Up @@ -296,21 +297,21 @@ public function testMoveField() {
'custom_group_id' => $groups['A']['id'],
'label' => 'Country A',
'dataType' => 'Country',
'htmlType' => 'Select Country',
'htmlType' => 'Select',
'default_value' => NULL,
]);
$countryB = $this->customFieldCreate([
'custom_group_id' => $groups['A']['id'],
'label' => 'Country B',
'dataType' => 'Country',
'htmlType' => 'Select Country',
'htmlType' => 'Select',
'default_value' => NULL,
]);
$countryC = $this->customFieldCreate([
'custom_group_id' => $groups['B']['id'],
'label' => 'Country C',
'dataType' => 'Country',
'htmlType' => 'Select Country',
'htmlType' => 'Select',
'default_value' => NULL,
]);

Expand Down Expand Up @@ -460,7 +461,7 @@ public function testGetFieldsForImport() {
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'Country',
'html_type' => 'Select Country',
'html_type' => 'Select',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('country'),
'label' => 'Country',
Expand Down Expand Up @@ -499,7 +500,7 @@ public function testGetFieldsForImport() {
'options_per_line' => NULL,
'text_length' => NULL,
'data_type' => 'Country',
'html_type' => 'Select Country',
'html_type' => 'Select',
'is_search_range' => '0',
'id' => $this->getCustomFieldID('multi_country'),
'label' => 'Country-multi',
Expand Down Expand Up @@ -797,7 +798,7 @@ public function testGetFieldsForImport() {
'import' => 1,
'data_type' => 'StateProvince',
'type' => 1,
'html_type' => 'Select State/Province',
'html_type' => 'Select',
'text_length' => NULL,
'options_per_line' => NULL,
'is_search_range' => '0',
Expand Down Expand Up @@ -835,7 +836,7 @@ public function testGetFieldsForImport() {
'data_type' => 'StateProvince',
'name' => $this->getCustomFieldName('multi_state'),
'type' => 1,
'html_type' => 'Select State/Province',
'html_type' => 'Select',
'text_length' => NULL,
'options_per_line' => NULL,
'is_search_range' => '0',
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Core/BAO/CustomValueTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testStoreStateProvince() {
$fields = [
'custom_group_id' => $customGroup['id'],
'data_type' => 'StateProvince',
'html_type' => 'Select State/Province',
'html_type' => 'Select',
'default_value' => '',
];

Expand Down
83 changes: 83 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public function testCustomFieldCreateAllAvailableFormInputs() {
$dtype = CRM_Core_BAO_CustomField::dataType();
$htype = CRM_Custom_Form_Field::$_dataToHTML;

// Legacy html types returned by v3
foreach ($htype as &$item) {
if (isset($item['StateProvince'])) {
$item['StateProvince'] = 'Select State/Province';
}
if (isset($item['Country'])) {
$item['Country'] = 'Select Country';
}
}

$n = 0;
foreach ($dtype as $dkey => $dvalue) {
foreach ($htype[$n] as $hkey => $hvalue) {
Expand Down Expand Up @@ -678,4 +688,77 @@ public function testLegacyHtmlType() {
$this->assertEquals('SingleSelect', $result['values'][0]['label']);
}

public function testLegacyStateCountryTypes() {
$customGroup = $this->customGroupCreate([
'name' => 'testCustomGroup',
'title' => 'testCustomGroup',
'extends' => 'Individual',
]);
$f1 = $this->callAPISuccess('CustomField', 'create', [
'label' => 'CountrySelect',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'Country',
'html_type' => 'Select Country',
]);
$f2 = $this->callAPISuccess('CustomField', 'create', [
'label' => 'StateSelect',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'StateProvince',
'html_type' => 'Select State/Province',
]);
$f3 = $this->callAPISuccess('CustomField', 'create', [
'label' => 'MultiSelectSP',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'StateProvince',
'html_type' => 'Multi-Select State/Province',
]);
$f4 = $this->callAPISuccess('CustomField', 'create', [
'label' => 'MultiSelectCountry',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'Country',
'html_type' => 'Select Country',
'serialize' => 1,
]);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => 'Multi-Select State/Province',
'sequential' => 1,
]);
$this->assertCount(1, $result['values']);
$this->assertEquals('MultiSelectSP', $result['values'][0]['label']);
$this->assertEquals('Multi-Select State/Province', $result['values'][0]['html_type']);
$this->assertEquals('1', $result['values'][0]['serialize']);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => 'Multi-Select Country',
'sequential' => 1,
]);
$this->assertCount(1, $result['values']);
$this->assertEquals('MultiSelectCountry', $result['values'][0]['label']);
$this->assertEquals('Multi-Select Country', $result['values'][0]['html_type']);
$this->assertEquals('1', $result['values'][0]['serialize']);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => 'Select Country',
'sequential' => 1,
]);
$this->assertCount(1, $result['values']);
$this->assertEquals('CountrySelect', $result['values'][0]['label']);
$this->assertEquals('Select Country', $result['values'][0]['html_type']);
$this->assertEquals('0', $result['values'][0]['serialize']);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => 'Select State/Province',
'sequential' => 1,
]);
$this->assertCount(1, $result['values']);
$this->assertEquals('StateSelect', $result['values'][0]['label']);
$this->assertEquals('Select State/Province', $result['values'][0]['html_type']);
$this->assertEquals('0', $result['values'][0]['serialize']);
}

}