From c94d39fdda3e97d02ddcd20af61f1b81d805279d Mon Sep 17 00:00:00 2001 From: monishdeb Date: Wed, 29 Jul 2015 22:51:28 +0530 Subject: [PATCH] CRM-16927 fix - Notice Search membership with custom fields https://issues.civicrm.org/jira/browse/CRM-16927 --- CRM/Activity/Form/Search.php | 2 +- CRM/Case/Form/Search.php | 2 +- CRM/Contact/Form/Search/Advanced.php | 24 ++++++++++++++++-------- CRM/Contribute/Form/Search.php | 4 ++-- CRM/Core/BAO/CustomField.php | 12 +----------- CRM/Core/BAO/CustomQuery.php | 27 ++++++++++++++++----------- CRM/Core/BAO/CustomValue.php | 18 ++++++++++++++---- CRM/Event/Form/Search.php | 2 +- CRM/Member/Form/Search.php | 2 +- CRM/Pledge/Form/Search.php | 2 +- 10 files changed, 54 insertions(+), 41 deletions(-) diff --git a/CRM/Activity/Form/Search.php b/CRM/Activity/Form/Search.php index 8e2a4ba5d9..c58751dfa4 100644 --- a/CRM/Activity/Form/Search.php +++ b/CRM/Activity/Form/Search.php @@ -234,7 +234,7 @@ class CRM_Activity_Form_Search extends CRM_Core_Form_Search { $this->_formValues["activity_test"] = 0; } - CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); diff --git a/CRM/Case/Form/Search.php b/CRM/Case/Form/Search.php index 4c33c675de..de684a1f5e 100644 --- a/CRM/Case/Form/Search.php +++ b/CRM/Case/Form/Search.php @@ -248,7 +248,7 @@ class CRM_Case_Form_Search extends CRM_Core_Form_Search { if (empty($this->_formValues['case_deleted'])) { $this->_formValues['case_deleted'] = 0; } - CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); diff --git a/CRM/Contact/Form/Search/Advanced.php b/CRM/Contact/Form/Search/Advanced.php index 6572be6c58..86b127dc02 100644 --- a/CRM/Contact/Form/Search/Advanced.php +++ b/CRM/Contact/Form/Search/Advanced.php @@ -8,7 +8,7 @@ | This file is a part of CiviCRM. | | | | CiviCRM is free software; you can copy, modify, and distribute it | -s | under the terms of the GNU Affero General Public License | + | under the terms of the GNU Affero General Public License | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | | | CiviCRM is distributed in the hope that it will be useful, but | @@ -323,7 +323,7 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { $this->_sortByCharacter = NULL; } - CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties = &$this->returnProperties(); @@ -378,7 +378,6 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { 'status_id', 'activity_subject', ); - $specialParams += preg_grep('/^custom_/', array_keys($this->_formValues)); foreach ($specialParams as $element) { $value = CRM_Utils_Array::value($element, $this->_formValues); if ($value) { @@ -424,12 +423,21 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { } if ($this->_ssID && empty($_POST)) { - $fields = array('contact_type', 'group', 'contact_tags'); + $specialFields = array('contact_type', 'group', 'contact_tags'); - foreach ($fields as $field) { - $fieldValues = CRM_Utils_Array::value($field, $defaults); - if ($fieldValues && is_array($fieldValues)) { - $defaults[$field] = array_keys($fieldValues); + foreach ($defaults as $element => $value) { + if (!empty($value) && is_array($value)) { + if (in_array($element, $specialFields)) { + $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]); + } + } } } } diff --git a/CRM/Contribute/Form/Search.php b/CRM/Contribute/Form/Search.php index 9e224d1e59..e6635b93ba 100644 --- a/CRM/Contribute/Form/Search.php +++ b/CRM/Contribute/Form/Search.php @@ -120,7 +120,7 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { ); } - $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 1); + $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $selector = new CRM_Contribute_Selector_Search($this->_queryParams, $this->_action, NULL, @@ -306,7 +306,7 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { } } - CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index e76a8a2dd5..dd5529d470 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1240,18 +1240,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { } $v = array(); - $p = array(); foreach ($checkedData as $key => $val) { - if ($html_type == 'CheckBox') { - if ($val) { - $p[] = $key; - $v[] = CRM_Utils_Array::value($key, $option); - } - } - else { - $p[] = $val; - $v[] = CRM_Utils_Array::value($val, $option); - } + $v[] = CRM_Utils_Array::value($val, $option); } if (!empty($v)) { $display = implode(', ', $v); diff --git a/CRM/Core/BAO/CustomQuery.php b/CRM/Core/BAO/CustomQuery.php index 0914247326..37b00ab57c 100644 --- a/CRM/Core/BAO/CustomQuery.php +++ b/CRM/Core/BAO/CustomQuery.php @@ -388,6 +388,10 @@ SELECT label, value $op = key($value); $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value[$op], $id, $this->_options); } + else { + $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value, $id, $this->_options); + $value = array('IN' => $value); + } $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op); @@ -409,22 +413,23 @@ SELECT label, value if (!is_array($value)) { $value = CRM_Utils_Type::escape($strtolower($value), 'String'); } - elseif ($isSerialized && strstr($op, 'IN')) { + elseif ($isSerialized) { + if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { + $op = key($value); + $value = $value[$op]; + } $value = implode(',', $value); } // CRM-14563,CRM-16575 : Special handling of multi-select custom fields - if (!empty($value)) { - if ($isSerialized) { - if (strstr($op, 'IN')) { - $value = str_replace(array('(', ')'), '', str_replace(",", "[[:cntrl:]]|[[:cntrl:]]", $value)); - } - $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE'; - $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]"; + if ($isSerialized && !empty($value)) { + if (strstr($op, 'IN')) { + $value = str_replace(array('(', ')'), '', str_replace(",", "[[:cntrl:]]|[[:cntrl:]]", $value)); } - elseif ($wildcard) { - $value = "[[:cntrl:]]%$value%[[:cntrl:]]"; - $op = 'LIKE'; + $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE'; + $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]"; + if (!$wildcard) { + $value = str_replace("[[:cntrl:]]|", '', $value); } } diff --git a/CRM/Core/BAO/CustomValue.php b/CRM/Core/BAO/CustomValue.php index ab5e404a00..1f29fabea2 100644 --- a/CRM/Core/BAO/CustomValue.php +++ b/CRM/Core/BAO/CustomValue.php @@ -165,7 +165,7 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO { * @param array $formValues * @return null */ - public static function fixFieldValueOfTypeMemo(&$formValues) { + public static function fixCustomFieldValue(&$formValues) { if (empty($formValues)) { return NULL; } @@ -180,14 +180,24 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO { $htmlType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField', substr($key, 7), 'html_type' ); - if (($htmlType == 'TextArea') && + $dataType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField', + substr($key, 7), 'data_type' + ); + + if (is_array($formValues[$key])) { + if (!in_array(key($formValues[$key]), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { + $formValues[$key] = array('IN' => $formValues[$key]); + } + } + elseif (($htmlType == 'TextArea' || + ($htmlType == 'Text' && $dataType == 'String') + ) && !((substr($formValues[$key], 0, 1) == '%') || (substr($formValues[$key], -1, 1) == '%') ) ) { - $formValues[$key] = '%' . $formValues[$key] . '%'; + $formValues[$key] = array('LIKE' => '%' . $formValues[$key] . '%'); } - } } diff --git a/CRM/Event/Form/Search.php b/CRM/Event/Form/Search.php index 0834673f1b..5d77cbfd57 100644 --- a/CRM/Event/Form/Search.php +++ b/CRM/Event/Form/Search.php @@ -284,7 +284,7 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search { $this->_formValues["participant_test"] = 0; } - CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); diff --git a/CRM/Member/Form/Search.php b/CRM/Member/Form/Search.php index 0e69f987cd..ff21324f5f 100644 --- a/CRM/Member/Form/Search.php +++ b/CRM/Member/Form/Search.php @@ -220,7 +220,7 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { } } - CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); diff --git a/CRM/Pledge/Form/Search.php b/CRM/Pledge/Form/Search.php index 513344cdb8..74914ced03 100644 --- a/CRM/Pledge/Form/Search.php +++ b/CRM/Pledge/Form/Search.php @@ -218,7 +218,7 @@ class CRM_Pledge_Form_Search extends CRM_Core_Form_Search { $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } - CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); + CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); -- 2.25.1