From: Coleman Watts Date: Tue, 18 Dec 2018 18:27:43 +0000 (-0500) Subject: dev/core#337 - Fix search range for select/radio custom fields X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0afd6ed29d5224eaa65eff7ec384550c5b2b194a;p=civicrm-core.git dev/core#337 - Fix search range for select/radio custom fields --- diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 554eb94dc9..8397f847ab 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -879,6 +879,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { } } + $rangeDataTypes = ['Int', 'Float', 'Money']; + if (!isset($label)) { $label = $field->label; } @@ -888,7 +890,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { switch ($widget) { case 'Text': case 'Link': - if ($field->is_search_range && $search) { + if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) { $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $field->attributes); $qf->add('text', $elementName . '_to', ts('To'), $field->attributes); } @@ -954,43 +956,55 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { break; case 'Radio': - $choice = array(); - parse_str($field->attributes, $radioAttributes); - $radioAttributes = array_merge($radioAttributes, $customFieldAttributes); - - foreach ($options as $v => $l) { - $choice[] = $qf->createElement('radio', NULL, '', $l, (string) $v, $radioAttributes); - } - $element = $qf->addGroup($choice, $elementName, $label); - $optionEditKey = 'data-option-edit-path'; - if (isset($selectAttributes[$optionEditKey])) { - $element->setAttribute($optionEditKey, $selectAttributes[$optionEditKey]); - } - - if ($useRequired && !$search) { - $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); + if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) { + $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $field->attributes); + $qf->add('text', $elementName . '_to', ts('To'), $field->attributes); } else { - $element->setAttribute('allowClear', TRUE); + $choice = array(); + parse_str($field->attributes, $radioAttributes); + $radioAttributes = array_merge($radioAttributes, $customFieldAttributes); + + foreach ($options as $v => $l) { + $choice[] = $qf->createElement('radio', NULL, '', $l, (string) $v, $radioAttributes); + } + $element = $qf->addGroup($choice, $elementName, $label); + $optionEditKey = 'data-option-edit-path'; + if (isset($selectAttributes[$optionEditKey])) { + $element->setAttribute($optionEditKey, $selectAttributes[$optionEditKey]); + } + + if ($useRequired && !$search) { + $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); + } + else { + $element->setAttribute('allowClear', TRUE); + } } break; // For all select elements case 'Select': - if (empty($selectAttributes['multiple'])) { - $options = array('' => $placeholder) + $options; + if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) { + $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $field->attributes); + $qf->add('text', $elementName . '_to', ts('To'), $field->attributes); } - $element = $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $selectAttributes); + else { + if (empty($selectAttributes['multiple'])) { + $options = array('' => $placeholder) + $options; + } + $element = $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $selectAttributes); - // Add and/or option for fields that store multiple values - if ($search && self::isSerialized($field)) { + // Add and/or option for fields that store multiple values + if ($search && self::isSerialized($field)) { - $operators = array( - $qf->createElement('radio', NULL, '', ts('Any'), 'or', array('title' => ts('Results may contain any of the selected options'))), - $qf->createElement('radio', NULL, '', ts('All'), 'and', array('title' => ts('Results must have all of the selected options'))), - ); - $qf->addGroup($operators, $elementName . '_operator'); - $qf->setDefaults(array($elementName . '_operator' => 'or')); + $operators = array( + $qf->createElement('radio', NULL, '', ts('Any'), 'or', array('title' => ts('Results may contain any of the selected options'))), + $qf->createElement('radio', NULL, '', ts('All'), 'and', array('title' => ts('Results must have all of the selected options'))), + ); + $qf->addGroup($operators, $elementName . '_operator'); + $qf->setDefaults(array($elementName . '_operator' => 'or')); + } } break; diff --git a/templates/CRM/Custom/Form/Field.tpl b/templates/CRM/Custom/Form/Field.tpl index 32723fd6b3..a587e26f90 100644 --- a/templates/CRM/Custom/Form/Field.tpl +++ b/templates/CRM/Custom/Form/Field.tpl @@ -190,7 +190,7 @@ var htmlType = $("[name='data_type[1]']", $form).val(), dataType = dataTypes[$("[name='data_type[0]']", $form).val()]; - if (((dataType === 'Int' || dataType === 'Float' || dataType === 'Money') && (htmlType === "Text")) || dataType === 'Date') { + if (dataType === 'Int' || dataType === 'Float' || dataType === 'Money' || dataType === 'Date') { if ($('#is_searchable', $form).is(':checked')) { $("#searchByRange", $form).show(); } else { @@ -200,8 +200,6 @@ $("#searchByRange", $form).hide(); } } - $('#is_searchable, [name^="data_type"]', $form).change(showSearchRange); - showSearchRange(); function toggleContactRefFilter(e) { var setSelected = $(this).attr('href'); @@ -284,6 +282,8 @@ $('[name^="data_type"]', $form).change(customOptionHtmlType); customOptionHtmlType(); + $('#is_searchable, [name^="data_type"]', $form).change(showSearchRange); + showSearchRange(); }); {/literal}