From 06d67d5302926a71ee3eacb134e986892199ab86 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Tue, 13 Oct 2015 18:41:12 +0530 Subject: [PATCH] CRM-17383 fix - Smart group form values broken in 4.7 https://issues.civicrm.org/jira/browse/CRM-17383 --- CRM/Contact/BAO/Query.php | 24 +++++-- CRM/Contact/BAO/SavedSearch.php | 69 +++++++++++++++---- CRM/Contact/Form/Search/Advanced.php | 30 +------- CRM/Contact/Form/Search/Custom/Group.php | 20 ------ .../Form/Search/Custom/MultipleValues.php | 7 -- CRM/Contact/Form/Search/Custom/PriceSet.php | 7 -- CRM/Contact/Form/Search/Custom/Proximity.php | 3 + CRM/Contact/Form/Search/Custom/Sample.php | 4 +- .../Form/Search/Custom/ZipCodeRange.php | 7 -- CRM/Contact/Selector.php | 2 +- 10 files changed, 79 insertions(+), 94 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index baaf0b1479..f024f09b1f 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1507,12 +1507,13 @@ class CRM_Contact_BAO_Query { foreach ($formValues as $id => $values) { - self::legacyConvertFormValues($id, $values); - if (self::isAlreadyProcessedForQueryFormat($values)) { $params[] = $values; continue; } + + self::legacyConvertFormValues($id, $values); + // The form uses 1 field to represent two db fields if ($id == 'contact_type' && $values && (!is_array($values) || !array_intersect(array_keys($values), CRM_Core_DAO::acceptedSQLOperators()))) { $contactType = array(); @@ -1611,12 +1612,25 @@ class CRM_Contact_BAO_Query { * */ public static function legacyConvertFormValues($id, &$values) { - if (in_array($id, array('group', 'tag')) && is_array($values)) { + $legacyElements = array( + 'group', + 'tag', + 'contact_tags', + 'contact_type', + 'membership_type_id', + 'membership_status_id', + ); + if (in_array($id, $legacyElements) && is_array($values)) { // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1), // as per the recent Search fixes $values need to be in standard array(id1, id2) format $ids = array_keys($values, 1); if (count($ids) > 1 || - (count($ids) == 1 && (key($values) > 1 || (key($values) == 1 && $values[1] == 1))) // handle (0 => 4), (1 => 1) + (count($ids) == 1 && + (key($values) > 1 || + is_string(key($values)) || + (key($values) == 1 && $values[1] == 1) // handle (0 => 4), (1 => 1) + ) + ) ) { $values = $ids; } @@ -4492,7 +4506,7 @@ civicrm_relationship.is_permission_a_b = 0 * * @return bool; */ - protected static function isAlreadyProcessedForQueryFormat($values) { + public static function isAlreadyProcessedForQueryFormat($values) { if (!is_array($values)) { return FALSE; } diff --git a/CRM/Contact/BAO/SavedSearch.php b/CRM/Contact/BAO/SavedSearch.php index d3b0307a35..ccfe4f917b 100644 --- a/CRM/Contact/BAO/SavedSearch.php +++ b/CRM/Contact/BAO/SavedSearch.php @@ -99,27 +99,66 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch { $result = unserialize($fv); } - // check to see if we need to convert the old privacy array - // CRM-9180 - if (isset($result['privacy'])) { - if (is_array($result['privacy'])) { - $result['privacy_operator'] = 'AND'; - $result['privacy_toggle'] = 1; - if (isset($result['privacy']['do_not_toggle'])) { - if ($result['privacy']['do_not_toggle']) { - $result['privacy_toggle'] = 2; + $specialFields = array('contact_type', 'group', 'contact_tags', 'member_membership_type_id', 'member_status_id'); + foreach ($result as $element => $value) { + if (CRM_Contact_BAO_Query::isAlreadyProcessedForQueryFormat($value)) { + $id = CRM_Utils_Array::value(0, $value); + $value = CRM_Utils_Array::value(2, $value); + if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { + $value = CRM_Utils_Array::value(key($value), $value); + } + $result[$id] = $value; + unset($result[$element]); + continue; + } + if (!empty($value) && is_array($value)) { + if (in_array($element, $specialFields)) { + $element = str_replace('member_membership_type_id', 'membership_type_id', $element); + $element = str_replace('member_status_id', 'membership_status_id', $element); + CRM_Contact_BAO_Query::legacyConvertFormValues($element, $value); + $result[$element] = $value; + } + // As per the OK (Operator as Key) value format, value array may contain key + // as an operator so to ensure the default is always set actual value + elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { + $result[$element] = CRM_Utils_Array::value(key($value), $value); + if (is_string($result[$element])) { + $result[$element] = str_replace("%", '', $result[$element]); } - unset($result['privacy']['do_not_toggle']); } + } + if (substr($element, 0, 7) == 'custom_' && + (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to') + ) { + // Ensure the _relative field is set if from or to are set to ensure custom date + // fields with 'from' or 'to' values are displayed when the are set in the smart group + // being loaded. (CRM-17116) + if (!isset($result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) { + $result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0; + } + } + // check to see if we need to convert the old privacy array + // CRM-9180 + if (!empty($result['privacy'])) { + if (is_array($result['privacy'])) { + $result['privacy_operator'] = 'AND'; + $result['privacy_toggle'] = 1; + if (isset($result['privacy']['do_not_toggle'])) { + if ($result['privacy']['do_not_toggle']) { + $result['privacy_toggle'] = 2; + } + unset($result['privacy']['do_not_toggle']); + } - $result['privacy_options'] = array(); - foreach ($result['privacy'] as $name => $value) { - if ($value) { - $result['privacy_options'][] = $name; + $result['privacy_options'] = array(); + foreach ($result['privacy'] as $name => $value) { + if ($value) { + $result['privacy_options'][] = $name; + } } } + unset($result['privacy']); } - unset($result['privacy']); } return $result; diff --git a/CRM/Contact/Form/Search/Advanced.php b/CRM/Contact/Form/Search/Advanced.php index 9f6b2e294c..0dd5e95ae8 100644 --- a/CRM/Contact/Form/Search/Advanced.php +++ b/CRM/Contact/Form/Search/Advanced.php @@ -392,35 +392,7 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { } if ($this->_ssID && empty($_POST)) { - $specialFields = array('contact_type', 'group', 'contact_tags', 'member_membership_type_id', 'member_status_id'); - - foreach ($defaults as $element => $value) { - if (!empty($value) && is_array($value)) { - if (in_array($element, $specialFields)) { - $element = str_replace('member_membership_type_id', 'membership_type_id', $element); - $element = str_replace('member_status_id', 'membership_status_id', $element); - $defaults[$element] = array_keys($value); - } - // As per the OK (Operator as Key) value format, value array may contain key - // as an operator so to ensure the default is always set actual value - elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { - $defaults[$element] = CRM_Utils_Array::value(key($value), $value); - if (is_string($defaults[$element])) { - $defaults[$element] = str_replace("%", '', $defaults[$element]); - } - } - } - if (substr($element, 0, 7) == 'custom_' && - (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to') - ) { - // Ensure the _relative field is set if from or to are set to ensure custom date - // fields with 'from' or 'to' values are displayed when the are set in the smart group - // being loaded. (CRM-17116) - if (!isset($defaults[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) { - $defaults[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0; - } - } - } + $defaults = array_merge($defaults, CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID)); } return $defaults; } diff --git a/CRM/Contact/Form/Search/Custom/Group.php b/CRM/Contact/Form/Search/Custom/Group.php index 2900468923..dc8968f0a3 100644 --- a/CRM/Contact/Form/Search/Custom/Group.php +++ b/CRM/Contact/Form/Search/Custom/Group.php @@ -150,26 +150,6 @@ class CRM_Contact_Form_Search_Custom_Group extends CRM_Contact_Form_Search_Custo $form->assign('elements', array('includeGroups', 'excludeGroups', 'andOr', 'includeTags', 'excludeTags')); } - /** - * Set search form field defaults here. - * @return array - */ - public function setDefaultValues() { - $defaults = array('andOr' => '1'); - - if (!empty($this->_formValues)) { - $defaults['andOr'] = CRM_Utils_Array::value('andOr', $this->_formValues, '1'); - - $defaults['includeGroups'] = CRM_Utils_Array::value('includeGroups', $this->_formValues); - $defaults['excludeGroups'] = CRM_Utils_Array::value('excludeGroups', $this->_formValues); - - $defaults['includeTags'] = CRM_Utils_Array::value('includeTags', $this->_formValues); - $defaults['excludeTags'] = CRM_Utils_Array::value('excludeTags', $this->_formValues); - } - - return $defaults; - } - /** * @param int $offset * @param int $rowcount diff --git a/CRM/Contact/Form/Search/Custom/MultipleValues.php b/CRM/Contact/Form/Search/Custom/MultipleValues.php index 829dd5f476..0fdd01a5aa 100644 --- a/CRM/Contact/Form/Search/Custom/MultipleValues.php +++ b/CRM/Contact/Form/Search/Custom/MultipleValues.php @@ -288,13 +288,6 @@ contact_a.sort_name as sort_name, return 'CRM/Contact/Form/Search/Custom/MultipleValues.tpl'; } - /** - * @return array - */ - public function setDefaultValues() { - return array(); - } - /** * @param $row */ diff --git a/CRM/Contact/Form/Search/Custom/PriceSet.php b/CRM/Contact/Form/Search/Custom/PriceSet.php index bde740e950..eca744c22a 100644 --- a/CRM/Contact/Form/Search/Custom/PriceSet.php +++ b/CRM/Contact/Form/Search/Custom/PriceSet.php @@ -338,13 +338,6 @@ INNER JOIN {$this->_tableName} tempTable ON ( tempTable.contact_id = contact_a.i return 'CRM/Contact/Form/Search/Custom.tpl'; } - /** - * @return array - */ - public function setDefaultValues() { - return array(); - } - /** * @param $row */ diff --git a/CRM/Contact/Form/Search/Custom/Proximity.php b/CRM/Contact/Form/Search/Custom/Proximity.php index d854c078e6..bf53ab8c2a 100644 --- a/CRM/Contact/Form/Search/Custom/Proximity.php +++ b/CRM/Contact/Form/Search/Custom/Proximity.php @@ -271,6 +271,9 @@ AND cgc.group_id = {$this->_group} * @return array|null */ public function setDefaultValues() { + if (!empty($this->_formValues)) { + return $this->_formValues; + } $config = CRM_Core_Config::singleton(); $countryDefault = $config->defaultContactCountry; $stateprovinceDefault = $config->defaultContactStateProvince; diff --git a/CRM/Contact/Form/Search/Custom/Sample.php b/CRM/Contact/Form/Search/Custom/Sample.php index 32b9cdc1bb..b79bfce55b 100644 --- a/CRM/Contact/Form/Search/Custom/Sample.php +++ b/CRM/Contact/Form/Search/Custom/Sample.php @@ -213,9 +213,7 @@ LEFT JOIN civicrm_state_province state_province ON state_province.id = address.s * @return array */ public function setDefaultValues() { - return array( - 'household_name' => '', - ); + return array_merge(array('household_name' => ''), $this->_formValues); } /** diff --git a/CRM/Contact/Form/Search/Custom/ZipCodeRange.php b/CRM/Contact/Form/Search/Custom/ZipCodeRange.php index b7331a6664..12f07d9482 100644 --- a/CRM/Contact/Form/Search/Custom/ZipCodeRange.php +++ b/CRM/Contact/Form/Search/Custom/ZipCodeRange.php @@ -178,13 +178,6 @@ LEFT JOIN civicrm_email email ON ( email.contact_id = contact_a.id AND return $this->whereClause($where, $params); } - /** - * @return array - */ - public function setDefaultValues() { - return array(); - } - /** * @return string */ diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index 45354aa4a2..629de94560 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -627,7 +627,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se $links = self::links($this->_context, $this->_contextMenu, $this->_key); //check explicitly added contact to a Smart Group. - $groupID = CRM_Utils_Array::key('1', $this->_formValues['group']); + $groupID = CRM_Utils_Array::value('group', $this->_formValues); $pseudoconstants = array(); // for CRM-3157 purposes -- 2.25.1