From: Coleman Watts Date: Sun, 2 Mar 2014 02:37:27 +0000 (-0500) Subject: CRM-13966 - Refactor autocomplete custom field X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1b4d9e39858a5b3c468ae8ee96430c2f800f37cb;p=civicrm-core.git CRM-13966 - Refactor autocomplete custom field --- diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index e4995f7163..6c71451721 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -241,24 +241,6 @@ class CRM_Contact_Page_AJAX { CRM_Core_Page_AJAX::autocompleteResults($results); } - /** - * Function to fetch the values - */ - static function autocomplete() { - $fieldID = CRM_Utils_Type::escape($_GET['cfid'], 'Integer'); - $optionGroupID = CRM_Utils_Type::escape($_GET['ogid'], 'Integer'); - $label = CRM_Utils_Type::escape($_GET['s'], 'String'); - - $selectOption = CRM_Core_BAO_CustomOption::valuesByID($fieldID, $optionGroupID); - $results = array(); - foreach ($selectOption as $id => $value) { - if (strtolower($label) == strtolower(substr($value, 0, strlen($label)))) { - $results[$id] = $value; - } - } - CRM_Core_Page_AJAX::autocompleteResults($results); - } - static function relationship() { $relType = CRM_Utils_Array::value('rel_type', $_REQUEST); $relContactID = CRM_Utils_Array::value('rel_contact', $_REQUEST); diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 49334b14f2..e5aa34e426 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -682,7 +682,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { * * @param int $fieldID the custom field ID * - * @return object $field the field object + * @return CRM_Core_DAO_CustomField $field the field object * @static * public */ @@ -714,7 +714,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { /** * This function for building custom fields * - * @param object $qf form object (reference) + * @param CRM_Core_Form $qf form object (reference) * @param string $elementName name of the custom field * @param boolean $inactiveNeeded * @param boolean $userRequired true if required else false @@ -753,6 +753,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { if ($field->html_type == 'TextArea' && $search) { $field->html_type = 'Text'; } + + $placeholder = $search ? ts('- any -') : ($useRequired ? ts('- select -') : ts('- none -')); // FIXME: Why are select state/country separate widget types? if (in_array($field->html_type, array('Select', 'Multi-Select', 'Select State/Province', 'Multi-Select State/Province', 'Select Country', 'Multi-Select Country'))) { @@ -884,8 +886,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $field->option_group_id ); $qf->add('select', $elementName, $label, - array( - '' => ts('- select -')) + $selectOption, + array('' => $placeholder) + $selectOption, $useRequired && !$search, $selectAttributes ); @@ -975,7 +976,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { case 'Select State/Province': //Add State - $stateOption = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvince(); + $stateOption = array('' => $placeholder) + CRM_Core_PseudoConstant::stateProvince(); $qf->add('select', $elementName, $label, $stateOption, $useRequired && !$search, $selectAttributes @@ -995,7 +996,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { case 'Select Country': //Add Country - $countryOption = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country(); + $countryOption = array('' => $placeholder) + CRM_Core_PseudoConstant::country(); $qf->add('select', $elementName, $label, $countryOption, $useRequired && !$search, $selectAttributes @@ -1021,18 +1022,18 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { break; case 'Autocomplete-Select': - $qf->add('text', $elementName, $label, $field->attributes, - $useRequired && !$search - ); - - $hiddenEleName = $elementName . '_id'; - if (substr($elementName, -1) == ']') { - $hiddenEleName = substr($elementName, 0, -1) . '_id]'; - } - $qf->addElement('hidden', $hiddenEleName, '', array('id' => str_replace(array(']', '['), array('', '_'), $hiddenEleName))); - static $customUrls = array(); if ($field->data_type == 'ContactReference') { + $qf->add('text', $elementName, $label, $field->attributes, + $useRequired && !$search + ); + + $hiddenEleName = $elementName . '_id'; + if (substr($elementName, -1) == ']') { + $hiddenEleName = substr($elementName, 0, -1) . '_id]'; + } + $qf->addElement('hidden', $hiddenEleName, '', array('id' => str_replace(array(']', '['), array('', '_'), $hiddenEleName))); + //$urlParams = "className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&reset=1&context=customfield&id={$field->id}"; $urlParams = "context=customfield&id={$field->id}"; @@ -1045,16 +1046,24 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $qf->addRule($elementName, ts('Select a valid contact for %1.', array(1 => $label)), 'validContact', $actualElementValue); } else { - $customUrls[$elementName] = CRM_Utils_System::url('civicrm/ajax/auto', - "reset=1&ogid={$field->option_group_id}&cfid={$field->id}", - FALSE, NULL, FALSE - ); - $qf->addRule($elementName, ts('Select a valid value for %1.', array(1 => $label)), - 'autocomplete', array( - 'fieldID' => $field->id, - 'optionGroupID' => $field->option_group_id, - ) + $attributes = array(); + // Fixme: why is this a string in the first place??? + if ($field->attributes) { + foreach(explode(' ', $field->attributes) as $at) { + if (strpos($at, '=')) { + list($k, $v) = explode('=', $at); + $attributes[$k] = trim($v, ' "'); + } + } + } + $attributes += array( + 'entity' => 'option_value', + 'placeholder' => $placeholder, + 'api' => array( + 'params' => array('option_group_id' => $field->option_group_id), + ), ); + $qf->addEntityRef($elementName, $label, $attributes, $useRequired && !$search); } $qf->assign('customUrls', $customUrls); @@ -1475,9 +1484,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { } } else { - $label = CRM_Core_BAO_CustomOption::getOptionLabel($customField->id, $value); - $defaults[$elementName . '_id'] = $value; - $defaults[$elementName] = $label; + $defaults[$elementName] = $value; } break; diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 1c06d2d3fe..1a399bbea1 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -1328,9 +1328,7 @@ ORDER BY civicrm_custom_group.weight, } } else { - $label = CRM_Core_BAO_CustomOption::getOptionLabel($field['id'], $value); - $defaults[$hiddenEleName] = $value; - $defaults[$elementName] = $label; + $defaults[$elementName] = $value; } break; diff --git a/CRM/Core/BAO/CustomQuery.php b/CRM/Core/BAO/CustomQuery.php index 0f8a2ea74f..36436d31e6 100644 --- a/CRM/Core/BAO/CustomQuery.php +++ b/CRM/Core/BAO/CustomQuery.php @@ -411,12 +411,7 @@ SELECT label, value ); } else { - if ($field['html_type'] == 'Autocomplete-Select') { - $wildcard = FALSE; - $val = array_search($value, $this->_options[$field['id']]); - } - elseif (in_array($field['html_type'], array( - 'Select', 'Radio'))) { + if (in_array($field['html_type'], array('Select', 'Radio', 'Autocomplete-Select'))) { $wildcard = FALSE; $val = CRM_Utils_Type::escape($value, 'String'); } diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 4da2bb6c56..f61e682c29 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -1098,7 +1098,7 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { $cfID, $options ); - if ($htmlType == 'Autocomplete-Select') { + if ($field['data_type'] == 'ContactReference') { $params[$index] = $values[$index]; } if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', @@ -2320,9 +2320,7 @@ AND ( entity_id IS NULL OR entity_id <= 0 ) } } else { - $label = CRM_Core_BAO_CustomOption::getOptionLabel($customFieldId, $details[$name]); - $defaults[$fldName . '_id'] = $details[$name]; - $defaults[$fldName] = $label; + $defaults[$fldName] = $details[$name]; } break; diff --git a/CRM/Core/xml/Menu/Contact.xml b/CRM/Core/xml/Menu/Contact.xml index a79deb2344..7be31f4304 100644 --- a/CRM/Core/xml/Menu/Contact.xml +++ b/CRM/Core/xml/Menu/Contact.xml @@ -302,11 +302,6 @@ CRM_Contact_Page_AJAX::getContactPhone 1 - - civicrm/ajax/auto - CRM_Contact_Page_AJAX::autocomplete - 1 - civicrm/ajax/subtype CRM_Contact_Page_AJAX::buildSubTypes diff --git a/templates/CRM/Campaign/Form/Petition/Block.tpl b/templates/CRM/Campaign/Form/Petition/Block.tpl index ccc528f8d0..8bcf12bacb 100644 --- a/templates/CRM/Campaign/Form/Petition/Block.tpl +++ b/templates/CRM/Campaign/Form/Petition/Block.tpl @@ -116,8 +116,6 @@ {if $field.html_type eq 'Autocomplete-Select'} {if $field.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl" element_name = $n} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl" element_name = $n} {/if} {/if} {* Show explanatory text for field if not in 'view' or 'preview' modes *} diff --git a/templates/CRM/Campaign/Form/Task/Interview.tpl b/templates/CRM/Campaign/Form/Task/Interview.tpl index 8a447df751..f0b2cced26 100644 --- a/templates/CRM/Campaign/Form/Task/Interview.tpl +++ b/templates/CRM/Campaign/Form/Task/Interview.tpl @@ -167,9 +167,6 @@ {else} {$form.field.$voterId.$fieldName.html} {/if} - {if $field.html_type eq 'Autocomplete-Select'} - {include file="CRM/Custom/Form/AutoComplete.tpl" element_name = field[`$voterId`][`$fieldName`]} - {/if} {/foreach} {/if} diff --git a/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl b/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl index 4ce81755cf..1320275409 100644 --- a/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl +++ b/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl @@ -96,8 +96,6 @@ {assign var="element_name" value="address[$blockId][$element_name]" } {if $element.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl"} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl"} {/if} {/if} diff --git a/templates/CRM/Contribute/Form/Contribution/OnBehalfOf.tpl b/templates/CRM/Contribute/Form/Contribution/OnBehalfOf.tpl index c5c5506fe6..76427ec808 100644 --- a/templates/CRM/Contribute/Form/Contribution/OnBehalfOf.tpl +++ b/templates/CRM/Contribute/Form/Contribution/OnBehalfOf.tpl @@ -108,10 +108,6 @@ {if $fieldName eq 'organization_name'}
{ts}Start typing the name of an organization that you have saved previously to use it again. Otherwise click "Enter a new organization" above.{/ts}
{/if} - {if !empty($onBehalfOfFields.$fieldName.html_type) && $onBehalfOfFields.$fieldName.html_type eq 'Autocomplete-Select'} - {assign var=elementName value=onbehalf[$fieldName]} - {include file="CRM/Custom/Form/AutoComplete.tpl" element_name=$elementName} - {/if} {if $onBehalfOfFields.$fieldName.name|substr:0:5 eq 'phone'} {assign var="phone_ext_field" value=$onBehalfOfFields.$fieldName.name|replace:'phone':'phone_ext'} {if $form.onbehalf.$phone_ext_field.html} diff --git a/templates/CRM/Custom/Form/AutoComplete.tpl b/templates/CRM/Custom/Form/AutoComplete.tpl deleted file mode 100644 index 6c91b862bb..0000000000 --- a/templates/CRM/Custom/Form/AutoComplete.tpl +++ /dev/null @@ -1,50 +0,0 @@ -{* - +--------------------------------------------------------------------+ - | CiviCRM version 4.4 | - +--------------------------------------------------------------------+ - | Copyright CiviCRM LLC (c) 2004-2013 | - +--------------------------------------------------------------------+ - | This file is a part of CiviCRM. | - | | - | CiviCRM is free software; you can copy, modify, and distribute it | - | under the terms of the GNU Affero General Public License | - | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | - | | - | CiviCRM is distributed in the hope that it will be useful, but | - | WITHOUT ANY WARRANTY; without even the implied warranty of | - | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | - | See the GNU Affero General Public License for more details. | - | | - | You should have received a copy of the GNU Affero General Public | - | License and the CiviCRM Licensing Exception along | - | with this program; if not, contact CiviCRM LLC | - | at info[AT]civicrm[DOT]org. If you have questions about the | - | GNU Affero General Public License or the licensing of CiviCRM, | - | see the CiviCRM license FAQ at http://civicrm.org/licensing | - +--------------------------------------------------------------------+ -*} -{literal} - -{/literal} diff --git a/templates/CRM/Custom/Form/CustomField.tpl b/templates/CRM/Custom/Form/CustomField.tpl index 9bb38a4092..3c130ef783 100644 --- a/templates/CRM/Custom/Form/CustomField.tpl +++ b/templates/CRM/Custom/Form/CustomField.tpl @@ -93,8 +93,6 @@ {elseif $element.html_type eq 'Autocomplete-Select'} {if $element.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl"} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl"} {/if} {/if} diff --git a/templates/CRM/Custom/Form/Preview.tpl b/templates/CRM/Custom/Form/Preview.tpl index 219d936e27..1229d984b2 100644 --- a/templates/CRM/Custom/Form/Preview.tpl +++ b/templates/CRM/Custom/Form/Preview.tpl @@ -92,8 +92,6 @@ {if $element.html_type eq 'Autocomplete-Select'} {if $element.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl"} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl"} {/if} {/if} diff --git a/templates/CRM/Custom/Form/Search.tpl b/templates/CRM/Custom/Form/Search.tpl index 7ba22e63fa..63b7d0c068 100644 --- a/templates/CRM/Custom/Form/Search.tpl +++ b/templates/CRM/Custom/Form/Search.tpl @@ -97,8 +97,6 @@ {if $element.html_type eq 'Autocomplete-Select'} {if $element.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl"} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl"} {/if} {/if} diff --git a/templates/CRM/Profile/Form/Dynamic.tpl b/templates/CRM/Profile/Form/Dynamic.tpl index 75b97da33d..7d28acde8a 100644 --- a/templates/CRM/Profile/Form/Dynamic.tpl +++ b/templates/CRM/Profile/Form/Dynamic.tpl @@ -205,8 +205,6 @@ function checkResponse(responseText, statusText, xhr, $form) { {if $field.html_type eq 'Autocomplete-Select'} {if $field.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl" element_name = $n} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl" element_name = $n} {/if} {/if} {/if} diff --git a/templates/CRM/Profile/Form/Search.tpl b/templates/CRM/Profile/Form/Search.tpl index f9605b3430..58120b4c29 100644 --- a/templates/CRM/Profile/Form/Search.tpl +++ b/templates/CRM/Profile/Form/Search.tpl @@ -122,8 +122,6 @@ {if $field.html_type eq 'Autocomplete-Select'} {if $field.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl" element_name = $n} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl" element_name = $n} {/if} {/if} diff --git a/templates/CRM/UF/Form/Block.tpl b/templates/CRM/UF/Form/Block.tpl index 3c93b82d97..d7ecd9e979 100644 --- a/templates/CRM/UF/Form/Block.tpl +++ b/templates/CRM/UF/Form/Block.tpl @@ -137,8 +137,6 @@ {if $field.html_type eq 'Autocomplete-Select'} {if $field.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl" element_name = $n} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl" element_name = $n} {/if} {/if} diff --git a/templates/CRM/UF/Form/Preview.tpl b/templates/CRM/UF/Form/Preview.tpl index 9d47362840..980d84857b 100644 --- a/templates/CRM/UF/Form/Preview.tpl +++ b/templates/CRM/UF/Form/Preview.tpl @@ -132,8 +132,6 @@ {if $field.html_type eq 'Autocomplete-Select'} {if $field.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl" element_name = $n} - {else} - {include file="CRM/Custom/Form/AutoComplete.tpl" element_name = $n} {/if} {/if} {/if}