return $params;
}
+ self::filterCountryFromValuesIfStateExists($formValues);
+
foreach ($formValues as $id => $values) {
if (self::isAlreadyProcessedForQueryFormat($values)) {
return in_array($operator, CRM_Core_DAO::acceptedSQLOperators());
}
+ /**
+ * 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.
*
if (!is_array($defaults)) {
$defaults = array();
}
-
+ $this->loadDefaultCountryBasedOnState($defaults);
if ($this->_ssID && empty($_POST)) {
$defaults = array_merge($defaults, CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID));
}
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'))
+ );
+ }
+ }
+
}