CRM-13966 - Refactor autocomplete custom field
authorColeman Watts <coleman@civicrm.org>
Sun, 2 Mar 2014 02:37:27 +0000 (21:37 -0500)
committerColeman Watts <coleman@civicrm.org>
Sun, 2 Mar 2014 14:38:19 +0000 (09:38 -0500)
18 files changed:
CRM/Contact/Page/AJAX.php
CRM/Core/BAO/CustomField.php
CRM/Core/BAO/CustomGroup.php
CRM/Core/BAO/CustomQuery.php
CRM/Core/BAO/UFGroup.php
CRM/Core/xml/Menu/Contact.xml
templates/CRM/Campaign/Form/Petition/Block.tpl
templates/CRM/Campaign/Form/Task/Interview.tpl
templates/CRM/Contact/Form/Edit/Address/CustomField.tpl
templates/CRM/Contribute/Form/Contribution/OnBehalfOf.tpl
templates/CRM/Custom/Form/AutoComplete.tpl [deleted file]
templates/CRM/Custom/Form/CustomField.tpl
templates/CRM/Custom/Form/Preview.tpl
templates/CRM/Custom/Form/Search.tpl
templates/CRM/Profile/Form/Dynamic.tpl
templates/CRM/Profile/Form/Search.tpl
templates/CRM/UF/Form/Block.tpl
templates/CRM/UF/Form/Preview.tpl

index e4995f716323daccb3da87051d078cf9a1f83d93..6c71451721fadd7a27eeacd8f7831fd1196675a2 100644 (file)
@@ -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);
index 49334b14f247795e79a4aeb901d84035c22924a5..e5aa34e42620d19d018e6323e668318e50fba287 100644 (file)
@@ -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;
 
index 1c06d2d3fe2651d4ab7a21f0150700e38b1f66e7..1a399bbea1247305536ccf3be3fcd124581f395f 100644 (file)
@@ -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;
 
index 0f8a2ea74f03b0e2182b5731b44f91cce17ed8bb..36436d31e637e31763fa5dac5331af6047b06cea 100644 (file)
@@ -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');
                 }
index 4da2bb6c5616781b9e73cf84e66174251a804ad2..f61e682c29936e2da920e9e8f9356a2fdf3bbc05 100644 (file)
@@ -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;
 
index a79deb234410cb21acba44fc86c132e95b9be1e8..7be31f430437fe431017498f6be73f96199626d5 100644 (file)
      <page_callback>CRM_Contact_Page_AJAX::getContactPhone</page_callback>
      <access_callback>1</access_callback>
 </item>
-<item>
-     <path>civicrm/ajax/auto</path>
-     <page_callback>CRM_Contact_Page_AJAX::autocomplete</page_callback>
-     <access_callback>1</access_callback>
-</item>
 <item>
      <path>civicrm/ajax/subtype</path>
      <page_callback>CRM_Contact_Page_AJAX::buildSubTypes</page_callback>
index ccc528f8d077f12277d4ec777b380b9e1b8038e5..8bcf12bacb235f72baf139e1f230a6a217257e57 100644 (file)
              {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 *}
index 8a447df75169fea58d2cabebb010c7ecac848ade..f0b2cced26d13c3d0af28a242e5a62ecf1b2a84b 100644 (file)
                 {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}
               </td>
             {/foreach}
           {/if}
index 4ce81755cf6b02854a36a21949130c846b5ccba9..132027540963ba8c35a63e5de554f268969f2da3 100644 (file)
@@ -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}
             </td>
index c5c5506fe65ab42dc6dafef524b535470a8a1511..76427ec8084cf495d741c02259355a062eccff12 100644 (file)
             {if $fieldName eq 'organization_name'}
               <div id="id-onbehalf-orgname-help" class="description">{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}</div>
             {/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 (file)
index 6c91b86..0000000
+++ /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}
-<script type="text/javascript">
-cj( function( ) {
-    var url       = "{/literal}{$customUrls.$element_name}{literal}";
-    var custom    = "{/literal}#{$element_name|replace:']':''|replace:'[':'_'}{literal}";
-    var custom_id = "{/literal}input[name=\"{$element_name|cat:'_id'|regex_replace:'/\]_id$/':'_id]'}\"]{literal}";
-
-    var customObj   = cj(custom);
-    var customIdObj = cj(custom_id);
-
-    if ( !customObj.hasClass('ac_input') ) {
-        customObj.autocomplete( url,
-            { width : 250, selectFirst : false, matchContains: true, max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal}
-            }).result(
-                function(event, data ) {
-                    customIdObj.val( data[1] );
-                }
-        );
-        customObj.click( function( ) {
-            customIdObj.val('');
-      });
-     }
-});
-</script>
-{/literal}
index 9bb38a40924318881b0581cb2ecacf6a03222195..3c130ef78320fea3290cab73008c7fb676e4f368 100644 (file)
@@ -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}
             </td>
index 219d936e272bc38e72a14d16c6565302c54ffb28..1229d984b2e909db2251021c84a879e5b7ead82c 100644 (file)
@@ -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}
           </td>
index 7ba22e63fa5f9eb3fbbca8a9f2b59ce79cf317d9..63b7d0c06876a59f86d8efe5b5034c734b175f67 100644 (file)
@@ -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}
             </td>
index 75b97da33d4caac267a8875a9ed76cd4a497a058..7d28acde8aa7f9c4e4bce44fc2bf9248e5272092 100644 (file)
@@ -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}
index f9605b3430bb1c2154012f5f5ccb23a349a6889a..58120b4c29f8edd938f46349a7b64af660ed63d2 100644 (file)
               {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}
             </td>
index 3c93b82d9734fe5b479dfacfb7719a04a4c2c0a1..d7ecd9e97977da53d6deba9379af47a66b0723f7 100644 (file)
               {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}
           </div>
index 9d47362840a1f9eec8925d9432857223931116ff..980d84857bf4499d7e98aa187d1f66f9c4bf299b 100644 (file)
                   {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}