From 0a74debbc6ccd6cbbb1304de8e9bfcebcc3f9c61 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Wed, 1 Jul 2015 20:02:14 +0530 Subject: [PATCH] CRM-16782 fix - Fatal error on using IN/NOT IN operator on contribution and membership fields https://issues.civicrm.org/jira/browse/CRM-16782 --- CRM/Contact/BAO/Query.php | 24 +----- CRM/Member/BAO/Query.php | 90 +++++---------------- CRM/Member/Form/Search.php | 26 ++++-- templates/CRM/Member/Form/Search/Common.tpl | 20 +---- 4 files changed, 46 insertions(+), 114 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 476a3aeac2..8cf4854b9b 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -2217,7 +2217,7 @@ class CRM_Contact_BAO_Query { if (!in_array($operator, CRM_Core_DAO::acceptedSQLOperators())) { //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators //so this condition will satisfy the search for now - if (strstr('IN', $op)) { + if (strpos($op, 'IN') !== FALSE) { $value = array($op => $value); } // we don't know when this might happen @@ -5064,7 +5064,7 @@ SELECT COUNT( conts.total_amount ) as cancel_count, $date = "('" . implode("','", $value) . "')"; $format = implode(', ', $format); } - elseif ($value) { + elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) { $date = CRM_Utils_Date::processDate($value); if (!$appendTimeStamp) { $date = substr($date, 0, 8); @@ -5203,26 +5203,6 @@ SELECT COUNT( conts.total_amount ) as cancel_count, $clause = " (NULLIF($field, '') IS NOT NULL) "; return $clause; - case 'IN': - case 'NOT IN': - if (isset($dataType)) { - if (is_array($value)) { - $values = $value; - } - else { - $value = CRM_Utils_Type::escape($value, "String"); - $values = explode(',', CRM_Utils_Array::value(0, explode(')', CRM_Utils_Array::value(1, explode('(', $value))))); - } - // supporting multiple values in IN clause - $val = array(); - foreach ($values as $v) { - $v = trim($v); - $val[] = "'" . CRM_Utils_Type::escape($v, $dataType) . "'"; - } - $value = "(" . implode($val, ",") . ")"; - } - return "$clause $value"; - default: if (empty($dataType)) { $dataType = 'String'; diff --git a/CRM/Member/BAO/Query.php b/CRM/Member/BAO/Query.php index a4b8798492..8c35a1fd6c 100644 --- a/CRM/Member/BAO/Query.php +++ b/CRM/Member/BAO/Query.php @@ -203,34 +203,28 @@ class CRM_Member_BAO_Query { case 'membership_status': case 'member_status_id': - if (!is_array($value)) { - $status = $value; - if (!empty($value)) { - $value = array_flip(explode(",", str_replace(array('(', ')'), '', $value))); - } - else { - $value = array(); - } + case 'membership_type': + case 'membership_type_id': + case 'member_id': + if (strpos($name, 'status') !== FALSE) { + $name = 'status_id'; + $qillName = 'Membership Status(s)'; } - else { - $status = implode(',', array_keys($value)); - if (count($value) > 0) { - $op = 'IN'; - $status = "({$status})"; - } + elseif ($name == 'member_id') { + $name = 'id'; + $qillName = 'Membership ID'; } - - $names = array(); - $statusTypes = CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'); - foreach ($value as $id => $dontCare) { - $names[] = $statusTypes[$id]; + else { + $name = 'membership_type_id'; + $qillName = 'Memebership Type(s)'; } - $query->_qill[$grouping][] = ts('Membership Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names); - $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_membership.status_id", + $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_membership.$name", $op, - $status, + $value, "Integer" ); + list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Member_DAO_Membership', $name, $value, $op); + $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $qillName, 2 => $op, 3 => $value)); $query->_tables['civicrm_membership'] = $query->_whereTables['civicrm_membership'] = 1; return; @@ -282,46 +276,6 @@ class CRM_Member_BAO_Query { $query->_tables['civicrm_membership'] = $query->_whereTables['civicrm_membership'] = 1; return; - case 'membership_type': - case 'member_membership_type_id': - if (!is_array($value)) { - $mType = $value; - if (!empty($value)) { - $value = array_flip(explode(",", str_replace(array('(', ')'), '', $value))); - } - else { - $value = array(); - } - } - else { - $mType = implode(',', array_keys($value)); - if (count($value) > 0) { - $op = 'IN'; - $mType = "({$mType})"; - } - } - - $names = array(); - $membershipTypes = CRM_Member_PseudoConstant::membershipType(); - foreach ($value as $id => $dontCare) { - if (!empty($membershipTypes[$id])) { - $names[] = $membershipTypes[$id]; - } - } - $query->_qill[$grouping][] = ts('Membership Type %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names); - $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_membership.membership_type_id", - $op, - $mType, - "Integer" - ); - $query->_tables['civicrm_membership'] = $query->_whereTables['civicrm_membership'] = 1; - return; - - case 'member_id': - $query->_where[$grouping][] = " civicrm_membership.id $op $value"; - $query->_tables['civicrm_membership'] = $query->_whereTables['civicrm_membership'] = 1; - return; - case 'member_is_primary': if ($value) { $query->_where[$grouping][] = " civicrm_membership.owner_membership_id IS NULL"; @@ -436,13 +390,13 @@ class CRM_Member_BAO_Query { * @param CRM_Core_Form $form */ public static function buildSearchForm(&$form) { - foreach (CRM_Member_PseudoConstant::membershipType() as $id => $Name) { - $form->_membershipType = &$form->addElement('checkbox', "member_membership_type_id[$id]", NULL, $Name); - } + $form->addSelect('status_id', + array('entity' => 'membership', 'multiple' => 'multiple', 'label' => ts('Memebership Status(s)'), 'option_url' => NULL, 'placeholder' => ts('- any -')) + ); - foreach (CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label') as $sId => $sName) { - $form->_membershipStatus = &$form->addElement('checkbox', "member_status_id[$sId]", NULL, $sName); - } + $form->addSelect('membership_type_id', + array('entity' => 'membership', 'multiple' => 'multiple', 'label' => ts('Memebership Type(s)'), 'option_url' => NULL, 'placeholder' => ts('- any -')) + ); $form->addElement('text', 'member_source', ts('Source')); diff --git a/CRM/Member/Form/Search.php b/CRM/Member/Form/Search.php index ec4ae0718a..6d1bfa9b8d 100644 --- a/CRM/Member/Form/Search.php +++ b/CRM/Member/Form/Search.php @@ -209,6 +209,21 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { $this->_formValues["member_test"] = 0; } + $specialParams = array( + 'status_id', + 'membership_type_id', + ); + foreach ($specialParams as $element) { + $value = CRM_Utils_Array::value($element, $this->_formValues); + if (!empty($value) && is_array($value)) { + if ($element == 'status_id') { + unset($this->_formValues[$element]); + $element = 'member_' . $element; + } + $this->_formValues[$element] = array('IN' => $value); + } + } + CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); @@ -282,12 +297,7 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { ); if ($status) { $status = explode(',', $status); - $tempStatus = array(); - foreach ($status as $value) { - $tempStatus[$value] = 1; - } - $this->_formValues['member_status_id'] = $tempStatus; - $this->_defaults['member_status_id'] = $tempStatus; + $this->_formValues['status_id'] = $this->_defaults['status_id'] = (array) $status; } $membershipType = CRM_Utils_Request::retrieve('type', 'String', @@ -295,8 +305,8 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { ); if ($membershipType) { - $this->_formValues['member_membership_type_id'] = array($membershipType => 1); - $this->_defaults['member_membership_type_id'] = array($membershipType => 1); + $this->_formValues['membership_type_id'] = array($membershipType); + $this->_defaults['membership_type_id'] = array($membershipType); } $cid = CRM_Utils_Request::retrieve('cid', 'Positive', diff --git a/templates/CRM/Member/Form/Search/Common.tpl b/templates/CRM/Member/Form/Search/Common.tpl index 8ecc6652ac..638472ec39 100644 --- a/templates/CRM/Member/Form/Search/Common.tpl +++ b/templates/CRM/Member/Form/Search/Common.tpl @@ -24,23 +24,11 @@ +--------------------------------------------------------------------+ *} -
-
- {foreach from=$form.member_membership_type_id item="membership_type_val"} -
- {$membership_type_val.html} -
- {/foreach} -
+
+ {$form.membership_type_id.html|crmAddClass:twenty} -
-
- {foreach from=$form.member_status_id item="membership_status_val"} -
- {$membership_status_val.html} -
- {/foreach} -
+
+ {$form.status_id.html|crmAddClass:twenty} -- 2.25.1