From fbc6a4d46ce57075415867bc0cd643814672c9fe Mon Sep 17 00:00:00 2001 From: monishdeb Date: Thu, 28 May 2015 14:23:37 +0530 Subject: [PATCH] Fix for IN and NOT IN operators on Search Builder validation --- CRM/Contact/BAO/Query.php | 8 +++++--- CRM/Contact/Form/Search/Builder.php | 20 +++++++++++++++++--- CRM/Core/BAO/Mapping.php | 6 ++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index b049465f00..1ed08a1ae3 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -5047,16 +5047,18 @@ SELECT COUNT( conts.total_amount ) as cancel_count, $op = key($value); $value = $value[$op]; } - print_r(strpos($op, 'IN') ); - if ($op == 'IN' || $op == 'NOT IN') { + if (strstr($op, 'IN')) { + $format = array(); foreach ($value as &$date) { $date = CRM_Utils_Date::processDate($date); if (!$appendTimeStamp) { $date = substr($date, 0, 8); } + $format[] = CRM_Utils_Date::customFormat($date); } $date = "('" . implode("','", $value) . "')"; + $format = implode(', ', $format); } else { $date = CRM_Utils_Date::processDate($value); @@ -5064,6 +5066,7 @@ SELECT COUNT( conts.total_amount ) as cancel_count, $date = substr($date, 0, 8); } $date = "'$date'"; + $format = CRM_Utils_Date::customFormat($date); } if ($date) { @@ -5075,7 +5078,6 @@ SELECT COUNT( conts.total_amount ) as cancel_count, $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1; $op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op); - $format = CRM_Utils_Date::customFormat($date); $this->_qill[$grouping][] = "$fieldTitle $op \"$format\""; } } diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 142281ac0d..7fab36c8c3 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -229,8 +229,14 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { $fldValue = CRM_Utils_Array::value($fldName, $fields); $fldType = CRM_Utils_Array::value('type', $fldValue); $type = CRM_Utils_Type::typeToString($fldType); + + if (strstr($v[1], 'IN')) { + if (empty($v[2])) { + $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); + } + } // Check Empty values for Integer Or Boolean Or Date type For operators other than IS NULL and IS NOT NULL. - if (!in_array($v[1], + elseif (!in_array($v[1], array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) ) { if ((($type == 'Int' || $type == 'Boolean') && !is_array($v[2]) && !trim($v[2])) && $v[2] != '0') { @@ -244,7 +250,7 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { if ($type && empty($errorMsg)) { // check for valid format while using IN Operator - if ($v[1] == 'IN') { + if (strstr($v[1], 'IN')) { if (!is_array($v[2])) { $inVal = trim($v[2]); //checking for format to avoid db errors @@ -265,7 +271,15 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { $parenValues = array(); $parenValues = is_array($v[2]) ? $v[2] : explode(',', trim($inVal, "(..)")); foreach ($parenValues as $val) { - $val = trim($val); + if ($type == 'Date' || $type == 'Timestamp') { + $val = CRM_Utils_Date::processDate($val); + if ($type == 'Date') { + $val = substr($val, 0, 8); + } + } + else { + $val = trim($val); + } if (!$val && $val != '0') { $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter the values correctly."); } diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 2ea67c4bec..52d5581114 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -1061,10 +1061,8 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { } // CRM-14983: verify if values are comma separated convert to array - if (!is_array($value) && (strpos($value, ',') !== FALSE || strstr($value, '(')) && empty($isCustomField) && $params['operator'][$key][$k] == 'IN') { - preg_match('#\((.*?)\)#', $value, $match); - $tmpArray = explode(',', $match[1]); - $value = array_combine(array_values($tmpArray), array_values($tmpArray)); + if (strstr($params['operator'][$key][$k], 'IN') && !is_array($value) && (strpos($value, ',') !== FALSE || strstr($value, '(') || !empty($value)) && empty($isCustomField)) { + $value = explode(',', str_replace(array('(', ')'), '', $value)); } if ($row) { -- 2.25.1