diff --git a/CRM/Member/BAO/Query.php b/CRM/Member/BAO/Query.php index e90dfc84b069..a8908e0553a7 100644 --- a/CRM/Member/BAO/Query.php +++ b/CRM/Member/BAO/Query.php @@ -153,6 +153,32 @@ public static function whereClauseSingle(&$values, &$query) { list($name, $op, $value, $grouping) = $values; $fields = self::getFields(); + $quoteValue = NULL; + + if (!empty($value) && !is_array($value)) { + $quoteValue = "\"$value\""; + } + + $fieldAliases = self::getLegacySupportedFields(); + + $fieldName = $name = self::getFieldName($values); + $qillName = $name; + if (in_array($name, $fieldAliases)) { + $qillName = array_search($name, $fieldAliases); + } + $pseudoExtraParam = []; + $fieldSpec = CRM_Utils_Array::value($fieldName, $fields, []); + $tableName = CRM_Utils_Array::value('table_name', $fieldSpec, 'civicrm_membership'); + $dataType = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $fieldSpec)); + if ($dataType === 'Timestamp' || $dataType === 'Date') { + $title = empty($fieldSpec['unique_title']) ? $fieldSpec['title'] : $fieldSpec['unique_title']; + $query->_tables['civicrm_membership'] = $query->_whereTables['civicrm_membership'] = 1; + $query->dateQueryBuilder($values, + $tableName, $fieldName, $fieldSpec['name'], $title + ); + return; + } + switch ($name) { case 'member_join_date_low': case 'member_join_date_high': @@ -167,7 +193,6 @@ public static function whereClauseSingle(&$values, &$query) { ); return; - case 'membership_start_date': case 'member_start_date_low': case 'member_start_date_high': Civi::log()->warning( @@ -181,7 +206,6 @@ public static function whereClauseSingle(&$values, &$query) { ); return; - case 'membership_end_date': case 'member_end_date_low': case 'member_end_date_high': Civi::log()->warning( diff --git a/tests/phpunit/CRM/Member/BAO/QueryTest.php b/tests/phpunit/CRM/Member/BAO/QueryTest.php index 0d1df9c75f48..98a563abf3a0 100644 --- a/tests/phpunit/CRM/Member/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Member/BAO/QueryTest.php @@ -80,4 +80,47 @@ public function testConvertEntityFieldMultipleValueEntityRefDoubleRun() { $this->assertEquals(['civicrm_membership.membership_type_id IN ("1", "2")'], $obj->_where[0]); } + /** + * Membership Date fields + * @return array + */ + public function membershipDateFields() { + $fields = []; + $fields[] = ['membership_join_date']; + $fields[] = ['membership_start_date']; + $fields[] = ['membership_end_date']; + return $fields; + } + + /** + * Test generating a correct where clause for date fields as generated by search builder + * @dataProvider membershipDateFields + */ + public function testMembershipDateWhereSearchBuilder($dateField) { + $dbDateField = str_replace('membership_', '', $dateField); + $formValues = [ + 'mapper' => [ + 1 => [['Membership', $dateField], [''], [''], [''], ['']], + 2 => [[''], [''], [''], [''], ['']], + 3 => [[''], [''], [''], [''], ['']], + ], + 'operator' => [ + 1 => ['<=', '', '', '', ''], + 2 => ['', '', '', '', ''], + 3 => ['', '', '', '', ''], + ], + 'value' => [ + 1 => ['20200201', '', '', '', ''], + 2 => ['', '', '', '', '', ''], + 3 => ['', '', '', '', '', ''], + ], + 'radio_ts' => '', + ]; + $searchBuilderForm = new CRM_Contact_Form_Search_Builder(); + $params = [[$dateField, "<=", "20200201", 1, 0]]; + $this->assertEquals($params, $searchBuilderForm->convertFormValues($formValues)); + $obj = new CRM_Contact_BAO_Query($params); + $this->assertEquals(['civicrm_membership.' . $dbDateField . ' <= \'20200201000000\''], $obj->_where[1]); + } + }