Eileen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/274048

Change subject: CRM-18125 remove country_id from filter where state_province_id 
is present
......................................................................

CRM-18125 remove country_id from filter where state_province_id is present

The country_id is implcit in the state_province_id, but in the absence of a 
combined filter including it hurts performance

Note this greatly improves the initial search query but the export is still 
slow, however, I expect the field choice will impact there

Change-Id: I968dae83238af67fca20fea48f71531a714b4b97
---
M CRM/Contact/BAO/Query.php
M CRM/Contact/Form/Search/Advanced.php
2 files changed, 47 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm 
refs/changes/48/274048/1

diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php
index c9d4de7..a88b32c 100644
--- a/CRM/Contact/BAO/Query.php
+++ b/CRM/Contact/BAO/Query.php
@@ -1505,6 +1505,8 @@
       return $params;
     }
 
+    self::filterCountryFromValuesIfStateExists($formValues);
+
     foreach ($formValues as $id => $values) {
       if (self::isAlreadyProcessedForQueryFormat($values)) {
         $params[] = $values;
@@ -4568,6 +4570,32 @@
   }
 
   /**
+   * If the state and country are passed remove state.
+   *
+   * Country is implicit from the state, but including both results in
+   * a poor query as there is no combined index on state AND country.
+   *
+   * CRM-18125
+   *
+   * @param array $formValues
+   */
+  public static function filterCountryFromValuesIfStateExists(&$formValues)
+  {
+    if (!empty($formValues['country'])) {
+      if (isset($formValues['state_province'])) {
+        // The use of array map sanitises the data by ensuring we are dealing 
with integers.
+        $states = implode(', ', array_map('intval', 
$formValues['state_province']));
+        $countryList = CRM_Core_DAO::singleValueQuery(
+          "SELECT GROUP_CONCAT(country_id) FROM civicrm_state_province WHERE 
id IN ($states)"
+        );
+        if ($countryList == $formValues['country']) {
+          unset($formValues['country']);
+        }
+      }
+    }
+  }
+
+  /**
    * Create and query the db for an contact search.
    *
    * @param int $offset
diff --git a/CRM/Contact/Form/Search/Advanced.php 
b/CRM/Contact/Form/Search/Advanced.php
index ce207e2..5e4591f 100644
--- a/CRM/Contact/Form/Search/Advanced.php
+++ b/CRM/Contact/Form/Search/Advanced.php
@@ -421,7 +421,7 @@
     if (!is_array($defaults)) {
       $defaults = array();
     }
-
+    $this->loadDefaultCountryBasedOnState($defaults);
     if ($this->_ssID && empty($_POST)) {
       $specialFields = array('contact_type', 'group', 'contact_tags', 
'member_membership_type_id', 'member_status_id');
 
@@ -456,4 +456,22 @@
     return $defaults;
   }
 
+  /**
+   * Set the default country for the form.
+   *
+   * For performance reasons country might be removed from the form CRM-18125
+   * but we need to include it in our defaults or the state will not be 
visible.
+   *
+   * @param array $defaults
+   */
+  public function loadDefaultCountryBasedOnState(&$defaults) {
+    if (!empty($defaults['state_province'])) {
+      $defaults['country'] = CRM_Core_DAO::singleValueQuery(
+        "SELECT country_id FROM civicrm_state_province
+         WHERE id = %1",
+        array(1 => array($defaults['state_province'][0], 'Integer'))
+      );
+    }
+  }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/274048
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I968dae83238af67fca20fea48f71531a714b4b97
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to