) {
// @todo - remove dateFormat - pretty sure it's never passed in...
list($name, $op, $value, $grouping, $wildcard) = $values;
-
+ if ($name !== $fieldName && $name !== "{$fieldName}_low" && $name !== "{$fieldName}_high") {
+ CRM_Core_Error::deprecatedFunctionWarning('Date query builder called unexpectedly');
+ return;
+ }
if ($tableName === 'civicrm_contact') {
// Special handling for contact table as it has a known alias in advanced search.
$tableName = 'contact_a';
$secondDateFormat = CRM_Utils_Date::customFormat($secondDate);
}
- $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
if ($secondDate) {
$highDBFieldName = $highDBFieldName ?? $dbFieldName;
$this->_where[$grouping][] = "
$this->_where[$grouping][] = self::buildClause("{$tableName}.{$dbFieldName}", $op);
}
- $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
-
$op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
$this->_qill[$grouping][] = "$fieldTitle $op $format";
}
+
+ // Ensure the tables are set, but don't whomp anything.
+ $this->_tables[$tableName] = $this->_tables[$tableName] ?? 1;
+ $this->_whereTables[$tableName] = $this->_whereTables[$tableName] ?? 1;
}
/**
$this->getCustomFieldName('select_date') => [
'name' => $this->getCustomFieldName('select_date'),
'type' => 4,
- 'title' => 'test_date',
+ 'title' => 'Test Date',
'headerPattern' => '//',
'import' => 1,
'custom_field_id' => $this->getCustomFieldID('select_date'),
'date_format' => 'mm/dd/yy',
'time_format' => '1',
'id' => $this->getCustomFieldID('select_date'),
- 'label' => 'test_date',
+ 'label' => 'Test Date',
'groupTitle' => 'Custom Group',
'default_value' => '20090711',
'custom_group_id' => $customGroupID,
$this->assertEquals('LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id', trim($queryObject->_whereTables[$this->getCustomGroupTable()]));
}
+ /**
+ * Test filtering by the renamed custom date fields.
+ *
+ * The conversion to date picker will result int these fields
+ * being renamed _high & _low and needing to return correctly.
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testSearchCustomDataDateLowWithPermsInPlay() {
+ $this->createLoggedInUser();
+ CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access all custom data'];
+ $this->createCustomGroupWithFieldOfType([], 'date');
+ $dateCustomFieldName = $this->getCustomFieldName('date');
+ // Assigning the relevant form value to be within a custom key is normally done in
+ // build field params. It would be better if it were all done in convertFormValues
+ // but for now we just imitate it.
+ $formValues = [
+ $dateCustomFieldName . '_low' => '2014-06-06',
+ ];
+
+ $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+ $queryObject = new CRM_Contact_BAO_Query($params);
+ $queryObject->query();
+ $this->assertEquals(
+ 'civicrm_value_group_with_fi_1.' . $this->getCustomFieldColumnName('date') . ' >= \'20140606000000\'',
+ trim($queryObject->_where[0][0])
+ );
+ $this->assertEquals(
+ 'FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id ) LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1) LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id
+LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id',
+ trim($queryObject->_fromClause)
+ );
+ $this->assertEquals('Test Date - greater than or equal to "June 6th, 2014 12:00 AM"', $queryObject->_qill[0][0]);
+ $this->assertEquals(1, $queryObject->_whereTables['civicrm_contact']);
+ $this->assertEquals('LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id', trim($queryObject->_whereTables[$this->getCustomGroupTable()]));
+ }
+
/**
* Test filtering by relative custom data dates.
*