CRM-10760 - Autocomplte - Create utility to abstract rendering of autocomplete output
authorColeman Watts <coleman@civicrm.org>
Tue, 28 Jan 2014 23:01:55 +0000 (15:01 -0800)
committerColeman Watts <coleman@civicrm.org>
Wed, 29 Jan 2014 05:38:47 +0000 (21:38 -0800)
CRM/Case/Page/AJAX.php
CRM/Contact/Page/AJAX.php
CRM/Core/Page/AJAX.php
CRM/Event/Page/AJAX.php
CRM/Group/Form/Edit.php
templates/CRM/Group/Form/Edit.tpl

index 3bfb16fdabcc5f8586b7866728405a309faae4e3..30ae8a77a5ee252676baea37e2c407029748b14a 100644 (file)
@@ -60,12 +60,11 @@ class CRM_Case_Page_AJAX {
       $excludeCaseIds = explode(',', $excludeIdStr);
     }
     $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases($params, $excludeCaseIds);
-
+    $results = array();
     foreach ($unclosedCases as $caseId => $details) {
-      echo $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ') ' . "|$caseId|" . $details['contact_id'] . '|' . $details['case_type'] . '|' . $details['sort_name'] . "\n";
+      $results["$caseId|" . $details['contact_id'] . '|' . $details['case_type'] . '|' . $details['sort_name']] = $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ')';
     }
-
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($results);
   }
 
   function processCaseTags() {
index aa0f0c492fff6ff0197029c8704d534ef510a52d..04730ae2ba48517d03a6714c1eb5269ba8d756b7 100644 (file)
@@ -86,12 +86,7 @@ class CRM_Contact_Page_AJAX {
     }
 
     $result = civicrm_api('Contact', 'getquick', $params);
-    if (empty($result['is_error']) && !empty($result['values'])) {
-      foreach ($result['values'] as $key => $val) {
-        echo "{$val['data']}|{$val['id']}\n";
-      }
-    }
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults(CRM_Utils_Array::value('values', $result), 'data');
   }
 
   static function contactReference() {
@@ -105,8 +100,7 @@ class CRM_Contact_Page_AJAX {
     $fldValues        = array();
     CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties);
     if (!$cf['id'] || !$cf['is_active'] || $cf['data_type'] != 'ContactReference') {
-      echo "$name|error\n";
-      CRM_Utils_System::civiExit();
+      CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name));
     }
 
     if ($cf['filter']) {
@@ -118,8 +112,7 @@ class CRM_Contact_Page_AJAX {
       if (!empty($action) &&
         !in_array($action, array('get', 'lookup'))
       ) {
-        echo "$name|error\n";
-        CRM_Utils_System::civiExit();
+        CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name));
       }
     }
 
@@ -172,11 +165,10 @@ class CRM_Contact_Page_AJAX {
     $contact = civicrm_api('Contact', 'Get', $params);
 
     if (!empty($contact['is_error'])) {
-      echo "$name|error\n";
-      CRM_Utils_System::civiExit();
+      CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name));
     }
 
-    $contactList = '';
+    $contactList = array();
     foreach ($contact['values'] as $value) {
       $view = array();
       foreach ($return as $fld) {
@@ -184,14 +176,14 @@ class CRM_Contact_Page_AJAX {
           $view[] = $value[$fld];
         }
       }
-      echo $contactList = implode(' :: ', $view) . "|" . $value['id'] . "\n";
+      $contactList[$value['id']] = implode(' :: ', $view);
     }
 
     if (!$contactList) {
-      echo "$name|$name\n";
+      $contactList = array($name => $name);
     }
 
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($contactList);
   }
 
   /**
@@ -241,12 +233,11 @@ class CRM_Contact_Page_AJAX {
         ";
 
     $dao = CRM_Core_DAO::executeQuery($query);
-
+    $results = array();
     while ($dao->fetch()) {
-      echo $pcpList = "$dao->data|$dao->id\n";
+      $results[$dao->id] = $dao->data;
     }
-
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($results);
   }
 
   /**
@@ -258,14 +249,13 @@ class CRM_Contact_Page_AJAX {
     $label         = CRM_Utils_Type::escape($_GET['s'], 'String');
 
     $selectOption = CRM_Core_BAO_CustomOption::valuesByID($fieldID, $optionGroupID);
-
-    $completeList = NULL;
+    $results = array();
     foreach ($selectOption as $id => $value) {
       if (strtolower($label) == strtolower(substr($value, 0, strlen($label)))) {
-        echo $completeList = "$value|$id\n";
+        $results[$id] = $value;
       }
     }
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($results);
   }
 
   static function relationship() {
@@ -349,13 +339,13 @@ class CRM_Contact_Page_AJAX {
     $name = str_replace('*', '%', $name);
 
     $elements = CRM_Contact_BAO_Relationship::getPermissionedEmployer($cid, $name);
-
+    $results = array();
     if (!empty($elements)) {
       foreach ($elements as $cid => $name) {
-        echo $element = $name['name'] . "|$cid\n";
+        $results[$cid] = $name['name'];
       }
     }
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($results);
   }
 
 
@@ -366,7 +356,9 @@ class CRM_Contact_Page_AJAX {
   }
 
   /**
-   * Function for building contact combo box
+   * @deprecated
+   * Old quicksearch function. No longer used in core.
+   * @todo: Remove this function and associated menu entry in CiviCRM 5
    */
   static function search() {
     $json = TRUE;
index 8acb653ab87c120e9682d21e2bba4462c7568502..f9e1093e50d03efa9677b09ba1c03bdda74d907f 100644 (file)
@@ -196,5 +196,26 @@ class CRM_Core_Page_AJAX {
     }
     CRM_Utils_System::civiExit();
   }
+
+  /**
+   * Send autocomplete results to the client. Input can be a simple or nested array.
+   * @param array $results - If nested array, also provide:
+   * @param string $val - array key to use as the value
+   * @param string $key - array key to use as the key
+   */
+  static function autocompleteResults($results, $val='label', $key='id') {
+    $output = array();
+    if (is_array($results)) {
+      foreach ($results as $k => $v) {
+        if (is_array($v)) {
+          echo $v[$val] . '|' . $v[$key] . "\n";
+        }
+        else {
+          echo "$v|$k\n";
+        }
+      }
+    }
+    CRM_Utils_System::civiExit();
+  }
 }
 
index 6e9e1fee803daf25bc097b10db65d4a7e64cc55a..44addaac0d3566dac6479c9afb614828a88a7ddc 100644 (file)
@@ -67,6 +67,7 @@ class CRM_Event_Page_AJAX {
         civicrm_event.title
 ";
     $dao = CRM_Core_DAO::executeQuery($query);
+    $results = array();
     while ($dao->fetch()) {
       $fields = array();
       foreach (array('title', 'city') as $field) {
@@ -77,10 +78,9 @@ class CRM_Event_Page_AJAX {
       if (isset($dao->start_date)) {
         array_push($fields, CRM_Utils_Date::customFormat($dao->start_date));
       }
-      $eventinfo = implode(' - ', $fields);
-      echo $elements = "$eventinfo|$dao->id\n";
+      $results[$dao->id] = implode(' - ', $fields);
     }
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($results);
   }
 
   /**
@@ -104,10 +104,11 @@ AND {$whereClause}
 ORDER by v.weight";
 
     $dao = CRM_Core_DAO::executeQuery($query);
+    $results = array();
     while ($dao->fetch()) {
-      echo $elements = "$dao->label|$dao->value\n";
+      $results[$dao->value] = $dao->label;
     }
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($results);
   }
 
   /**
@@ -131,10 +132,11 @@ LEFT JOIN civicrm_price_set_entity ce ON ce.price_set_id = cf.price_set_id
 WHERE ce.entity_table = 'civicrm_event' AND {$whereClause}
 GROUP BY cv.label";
     $dao = CRM_Core_DAO::executeQuery($query);
+    $results = array();
     while ($dao->fetch()) {
-      echo $elements = "$dao->label|$dao->id\n";
+      $results[$dao->id] = $dao->label;
     }
-    CRM_Utils_System::civiExit();
+    CRM_Core_Page_AJAX::autocompleteResults($results);
   }
 
   function eventList() {
index 5085648c9830f0493a06416d14ab6578651a6acf..17d97c96c43a036f452a37759e613c4f32f7c9b1 100644 (file)
@@ -183,8 +183,16 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
           //used in edit mode
           $this->_groupOrganizationID = $defaults['group_organization'];
         }
-
-        $this->assign('organizationID', CRM_Utils_Array::value('organization_id',$defaults));
+        if (!empty($defaults['organization_id'])) {
+          $result = civicrm_api3('contact', 'getquick', array(
+            'org' => 1,
+            'id' => $defaults['organization_id']
+          ));
+          $this->assign('organizationName', $result['values'][0]['data']);
+        }
+        else {
+          $this->assign('organizationName', '');
+        }
       }
     }
 
index 9183f6256e11df0d2e135deac8fda9f52645bb7d..12bf2e17a49eff96f14568ae63664820e3d7a055 100644 (file)
         <tr class="crm-group-form-block-organization">
             <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$form.organization.label}</td>
       <td>{$form.organization.html|crmAddClass:huge}
-          <div id="organization_address" style="font-size:10px"></div>
+          <div id="organization_address" style="font-size:10px">{$organizationName}</div>
       </td>
         </tr>
     </table>
@@ -151,26 +151,9 @@ cj('input[type=checkbox][name="group_type[{/literal}{$freezeMailignList}{literal
 cj('input[type=checkbox][name="group_type[{/literal}{$hideMailignList}{literal}]"]').hide();
 cj('label[for="group_type[{/literal}{$hideMailignList}{literal}]"]').hide();
 {/literal}{/if}{literal}
-{/literal}{if $organizationID}{literal}
-    cj(document).ready( function() {
-  //group organzation default setting
-  var dataUrl = "{/literal}{crmURL p='civicrm/ajax/search' h=0 q="org=1&id=$organizationID"}{literal}";
-  cj.ajax({
-          url     : dataUrl,
-          async   : false,
-          success : function(html){
-                      //fixme for showing address in div
-                      htmlText = html.split( '|' , 2);
-                      htmlDiv = htmlText[0].replace( /::/gi, ' ');
-          cj('#organization').val(htmlText[0]);
-                      cj('div#organization_address').html(htmlDiv);
-                    }
-  });
-    });
-{/literal}{/if}{literal}
 
-var dataUrl = "{/literal}{$groupOrgDataURL}{literal}";
-cj('#organization').autocomplete( dataUrl, {
+var dataUrl = "{/literal}{crmURL p='civicrm/ajax/rest' q='className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&org=1&context=groupcontact' h=0 }{literal}";
+cj('#organization').val(cj('#organization_address').text()).autocomplete( dataUrl, {
               width : 250, selectFirst : false, matchContains: true
               }).result( function(event, data, formatted) {
                                                        cj( "#organization_id" ).val( data[1] );