From dbc6f6d6ef466bec4c2935cd9e2c5dbcf56a5dfb Mon Sep 17 00:00:00 2001 From: atif-shaikh Date: Mon, 21 Jul 2014 18:10:10 +0530 Subject: [PATCH] CRM-14983 - Sunning Contribution via Search Builder Fails Putting Contacts in Smart Group https://issues.civicrm.org/jira/browse/CRM-14983 --- CRM/Contact/BAO/Query.php | 15 +++++++------- CRM/Contact/Form/Search/Builder.php | 24 ++++++++++++---------- CRM/Contribute/BAO/Query.php | 31 ++++++++++++++++++++++------- CRM/Core/BAO/Mapping.php | 8 ++++++++ CRM/Event/BAO/Query.php | 10 +++++++--- CRM/Member/BAO/Query.php | 4 ++-- CRM/Pledge/BAO/Query.php | 12 ++++++----- 7 files changed, 68 insertions(+), 36 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 9dc6990f5e..efb22085f5 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1819,7 +1819,7 @@ class CRM_Contact_BAO_Query { $this->_where[0][] = "contact_a.id IN ({$idList})"; } else { - $this->_where[0][] = "contact_a.id {$this->_params[$id][1]} {$this->_params[$id][2]}"; + $this->_where[0][] = self::buildClause("contact_a.id", "{$this->_params[$id][1]}", "{$this->_params[$id][2]}"); } } else { @@ -2659,7 +2659,7 @@ class CRM_Contact_BAO_Query { } else { $type = array_pop($clause); - $this->_where[$grouping][] = "contact_a.contact_type $op $type"; + $this->_where[$grouping][] = self::buildClause("contact_a.contact_type", $op, $contactType); } $this->_qill[$grouping][] = ts('Contact Type') . ' - ' . implode(' ' . ts('or') . ' ', $quill); @@ -5376,7 +5376,7 @@ AND displayRelType.is_active = 1 $useIDsOnly = FALSE ) { - if (!empty($selectValues) && !empty($selectValues[$value])) { + if (!empty($selectValues) && !is_array($value) && !empty($selectValues[$value])) { $qill = $selectValues[$value]; } else { @@ -5389,13 +5389,12 @@ AND displayRelType.is_active = 1 $qill = $selectValues[(int ) $value]; } elseif ($op == 'IN' || $op == 'NOT IN') { - $values = self::parseSearchBuilderString($value); - if (is_array($values)) { + if (is_array($value)) { $intVals = array(); $newValues = array(); - foreach ($values as $v) { - $intVals[] = (int) $v; - $newValues[] = $selectValues[(int ) $v]; + foreach ($value as $k => $v) { + $intVals[$k] = (int) $k; + $newValues[] = $selectValues[(int) $k]; } $value = (in_array($name, $pseudoFields)) ? $intVals : $newValues; diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 3af5a4bb67..e2a88eaded 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -236,7 +236,7 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { // 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], array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) { - if ((($type == 'Int' || $type == 'Boolean') && !trim($v[2])) && $v[2] != '0') { + if ((($type == 'Int' || $type == 'Boolean') && !is_array($v[2]) && !trim($v[2])) && $v[2] != '0') { $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); } elseif ($type == 'Date' && !trim($v[2])) { @@ -248,23 +248,25 @@ 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') { - $inVal = trim($v[2]); - //checking for format to avoid db errors - if ($type == 'Int') { - if (!preg_match('/^[(]([A-Za-z0-9\,]+)[)]$/', $inVal)) { - $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); + if (!is_array($v[2])) { + $inVal = trim($v[2]); + //checking for format to avoid db errors + if ($type == 'Int') { + if (!preg_match('/^[(]([A-Za-z0-9\,]+)[)]$/', $inVal)) { + $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); + } } - } - else { - if (!(substr($inVal, 0, 1) == '(' && substr($inVal, -1, 1) == ')') && !preg_match('/^[(]([A-Za-z0-9åäöÅÄÖüÜœŒæÆøØ\,\s]+)[)]$/', $inVal)) { - $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); + else { + if (!(substr($inVal, 0, 1) == '(' && substr($inVal, -1, 1) == ')') && !preg_match('/^[(]([A-Za-z0-9åäöÅÄÖüÜœŒæÆøØ\,\s]+)[)]$/', $inVal)) { + $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); + } } } // Validate each value in parenthesis to avoid db errors if (empty($errorMsg)) { $parenValues = array(); - $parenValues = explode(',', trim($inVal, "(..)")); + $parenValues = is_array($v[2]) ? $v[2] : explode(',', trim($inVal, "(..)")); foreach ($parenValues as $val) { $val = trim($val); if (!$val && $val != '0') { diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index 30d92de62f..047c1fd152 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -245,6 +245,7 @@ class CRM_Contribute_BAO_Query { static function whereClauseSingle(&$values, &$query) { list($name, $op, $value, $grouping, $wildcard) = $values; + $quoteValue = NULL; $fields = self::getFields(); if (!empty($value) && !is_array($value)) { @@ -348,7 +349,9 @@ class CRM_Contribute_BAO_Query { } } else { - $names[] = $types[$value]; + if (!empty($value)) { + $names[] = $types[$value]; + } } $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.financial_type_id", @@ -411,7 +414,6 @@ class CRM_Contribute_BAO_Query { } } else { - $op = '='; $scTypes = $value; $names[] = $softCreditTypes[$value]; } @@ -428,13 +430,27 @@ class CRM_Contribute_BAO_Query { case 'contribution_payment_instrument_id': case 'contribution_payment_instrument': - $pi = $value; + $pi = array(); $pis = CRM_Contribute_PseudoConstant::paymentInstrument(); + if (is_array($value)) { + foreach ($value as $k => $v) { + if ($v) { + $op = 'IN'; + $pi[] = $pis[$v]; + } + } + } + else { + if (!empty($value)) { + $pi[] = $pis[$value]; + } + } + $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.payment_instrument_id", $op, $value, "Integer" ); - $query->_qill[$grouping][] = ts('Paid By - %1', array(1 => $pis[$pi])); + $query->_qill[$grouping][] = ts('Paid By - %1', array(1 => $op)) . " '" . implode("' " . ts('or') . " '", $pi) . "'"; $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; return; @@ -449,13 +465,12 @@ class CRM_Contribute_BAO_Query { $status = implode(',', $val); - if (count($val) > 1) { + if (count($val) > 0) { $op = 'IN'; $status = "({$status})"; } } else { - $op = '='; $status = $value; } @@ -470,7 +485,9 @@ class CRM_Contribute_BAO_Query { } } else { - $names[] = $statusValues[$value]; + if (!empty($value)) { + $names[] = $statusValues[$value]; + } } $query->_qill[$grouping][] = ts('Contribution Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names); diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 32ac611cc3..7ec58dbafe 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -1042,6 +1042,7 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { // CRM-14563: we store checkbox, multi-select and adv-multi select custom field using separator, hence it // needs special handling. if ($cfID = CRM_Core_BAO_CustomField::getKeyID($v[1])) { + $isCustomField = TRUE; $customFieldType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $cfID, 'html_type'); $specialHTMLType = array( 'CheckBox', @@ -1062,6 +1063,13 @@ 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 ($row) { $fields[] = array( $fldName, diff --git a/CRM/Event/BAO/Query.php b/CRM/Event/BAO/Query.php index 26b36a36bc..3e4873a602 100644 --- a/CRM/Event/BAO/Query.php +++ b/CRM/Event/BAO/Query.php @@ -333,7 +333,7 @@ class CRM_Event_BAO_Query { $status = $value; } - if (count($val) > 1) { + if (count($val) > 0) { $op = 'IN'; $status = "({$status})"; } @@ -347,7 +347,9 @@ class CRM_Event_BAO_Query { } } else { - $names[] = $statusTypes[$value]; + if (!empty($value)) { + $names[] = $statusTypes[$value]; + } } $query->_qill[$grouping][] = ts('Participant Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names); @@ -378,7 +380,9 @@ class CRM_Event_BAO_Query { $names = array(); foreach ($val as $id => $dontCare) { - $names[] = $roleTypes[$id]; + if (!empty($roleTypes[$id]) ) { + $names[] = $roleTypes[$id]; + } } if (!empty($names)) { diff --git a/CRM/Member/BAO/Query.php b/CRM/Member/BAO/Query.php index 8d7a22605a..6bcb800832 100644 --- a/CRM/Member/BAO/Query.php +++ b/CRM/Member/BAO/Query.php @@ -215,7 +215,7 @@ class CRM_Member_BAO_Query { } else { $status = implode(',', array_keys($value)); - if (count($value) > 1) { + if (count($value) > 0) { $op = 'IN'; $status = "({$status})"; } @@ -298,7 +298,7 @@ class CRM_Member_BAO_Query { } else { $mType = implode(',', array_keys($value)); - if (count($value) > 1) { + if (count($value) > 0) { $op = 'IN'; $mType = "({$mType})"; } diff --git a/CRM/Pledge/BAO/Query.php b/CRM/Pledge/BAO/Query.php index d6375ecf22..d1c0464c0a 100644 --- a/CRM/Pledge/BAO/Query.php +++ b/CRM/Pledge/BAO/Query.php @@ -277,13 +277,12 @@ class CRM_Pledge_BAO_Query { $status = implode(',', $val); - if (count($val) > 1) { + if (count($val) > 0) { $op = 'IN'; $status = "({$status})"; } } else { - $op = '='; $status = $value; } @@ -296,7 +295,9 @@ class CRM_Pledge_BAO_Query { } } else { - $names[] = $statusValues[$value]; + if (!empty($value) ) { + $names[] = $statusValues[$value]; + } } $query->_qill[$grouping][] = ts('Pledge Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names); @@ -324,7 +325,6 @@ class CRM_Pledge_BAO_Query { } } else { - $op = '='; $status = $value; } @@ -337,7 +337,9 @@ class CRM_Pledge_BAO_Query { } } else { - $names[] = $statusValues[$value]; + if (!empty($value) ) { + $names[] = $statusValues[$value]; + } } $query->_qill[$grouping][] = ts('Pledge Payment Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names); -- 2.25.1