Merge pull request #2354 from totten/4.4-dashboard-api
[civicrm-core.git] / CRM / Contact / Selector.php
index c649c3cf065ee058ff19e11e2759008632279b2d..0e13abdea1828cc98ef378312a4cd827373c0a53 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -506,7 +506,18 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
     // note the formvalues were given by CRM_Contact_Form_Search to us
     // and contain the search criteria (parameters)
     // note that the default action is basic
-    $result = $this->_query->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
+    if ($rowCount) {
+      $cacheKey = $this->buildPrevNextCache($sort);
+      $result = $this->_query->getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds);
+
+      // CRM-13996: result is empty when selector columns are sorted. hence we need to run the query again
+      if ( $result->N == 0) {
+        $result = $this->_query->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
+      }
+    }
+    else {
+      $result = $this->_query->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
+    }
 
     // process the result of the query
     $rows = array();
@@ -583,34 +594,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
     $groupID = CRM_Utils_Array::key('1', $this->_formValues['group']);
 
     $pseudoconstants = array();
-    if (!empty($this->_fields)) {
-      // get all the pseudoconstant values
-      foreach ($this->_fields as $name => $values) {
-        if (isset($this->_fields[$name]['pseudoconstant'])) {
-          $pseudoconstants[$name] =
-            array(
-              'dbName' => $this->_fields[$name]['name'],
-              'values' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', $name),
-            );
-        }
-      }
-    }
-
     // for CRM-3157 purposes
-    if (in_array('country', $names)) {
-      $pseudoconstants['country'] = array(
-        'dbName' => 'country_id',
-        'values' => CRM_Core_PseudoConstant::country()
-      );
-    }
-
-    if (in_array('state_province', $names)) {
-      $pseudoconstants['state_province'] = array(
-        'dbName' => 'state_province_id',
-        'values' => CRM_Core_PseudoConstant::stateProvince()
-      );
-    }
-
     if (in_array('world_region', $names)) {
       $pseudoconstants['world_region'] = array(
         'dbName' => 'world_region_id',
@@ -621,6 +605,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
     $seenIDs = array();
     while ($result->fetch()) {
       $row = array();
+      $this->_query->convertToPseudoNames($result);
 
       // the columns we are interested in
       foreach ($names as $property) {
@@ -660,6 +645,9 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
           $greeting = $property . '_display';
           $row[$property] = $result->$greeting;
         }
+        elseif ($property == 'state_province') {
+          $row[$property] = $result->state_province_name;
+        }
         elseif (isset($pseudoconstants[$property])) {
           $row[$property] = CRM_Utils_Array::value(
             $result->{$pseudoconstants[$property]['dbName']},
@@ -788,16 +776,20 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
       }
     }
 
-    $this->buildPrevNextCache($sort);
-
     return $rows;
   }
 
   function buildPrevNextCache($sort) {
-    $cacheKey = CRM_Utils_Array::value('qfKey', $this->_formValues);
+    $cacheKey = 'civicrm search ' . $this->_key;
+
+    // Get current page requested
+    $pageNum = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
+    // When starting from scratch, clear any old cache
+    if (!$pageNum) {
+      CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact');
+      $pageNum = 1;
+    }
 
-    //for prev/next pagination
-    $pageNum = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, 1);
     $pageSize = CRM_Utils_Request::retrieve('crmRowCount', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, 50);
     $firstRecord = ($pageNum - 1) * $pageSize;
 
@@ -805,21 +797,17 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
     $sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', CRM_Core_DAO::$_nullObject);
 
     //for text field pagination selection save
-    $countRow = CRM_Core_BAO_PrevNextCache::getCount("%civicrm search {$cacheKey}%", NULL, "entity_table = 'civicrm_contact'", "LIKE");
+    $countRow = CRM_Core_BAO_PrevNextCache::getCount($cacheKey, NULL, "entity_table = 'civicrm_contact'");
 
     // $sortByCharacter triggers a refresh in the prevNext cache
-    if ($sortByCharacter) {
-      $cacheKeyCharacter = "civicrm search {$cacheKey}_alphabet";
-      if ($sortByCharacter == 'all') {
-        //delete the alphabet key corresponding records in prevnext_cache
-        CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKeyCharacter, 'civicrm_contact');
-        $cacheKeyCharacter = NULL;
-      }
-      $this->fillupPrevNextCache($sort, $cacheKeyCharacter);
+    if ($sortByCharacter && $sortByCharacter != 'all') {
+      $cacheKey .= "_alphabet";
+      $this->fillupPrevNextCache($sort, $cacheKey);
     }
     elseif ($firstRecord >= $countRow) {
-      $this->fillupPrevNextCache($sort, NULL, $countRow, $firstRecord + 500);
+      $this->fillupPrevNextCache($sort, $cacheKey, $countRow, $firstRecord + 500);
     }
+    return $cacheKey;
   }
 
   function addActions(&$rows) {
@@ -902,15 +890,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
    * @param int $start
    * @param int $end
    */
-  function fillupPrevNextCache($sort, $cacheKey = NULL, $start = 0, $end = 500) {
-    if (!$cacheKey) {
-      $cacheKey = "civicrm search {$this->_key}";
-    }
-
-    // When starting from zero, clear any old cache
-    if ($start == 0) {
-      CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact');
-    }
+  function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = 500) {
 
     // For custom searches, use the contactIDs method
     if (is_a($this, 'CRM_Contact_Selector_Custom')) {
@@ -943,7 +923,6 @@ SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.dis
 
     $sql = str_replace($replaceSQL, $insertSQL, $sql);
 
-
     CRM_Core_Error::ignoreException();
     $result = CRM_Core_DAO::executeQuery($sql);
     CRM_Core_Error::setCallback();