return [
'String' => CRM_Utils_Type::T_STRING,
'Int' => CRM_Utils_Type::T_INT,
+ 'Money' => CRM_Utils_Type::T_MONEY,
+ 'Memo' => CRM_Utils_Type::T_LONGTEXT,
'Float' => CRM_Utils_Type::T_FLOAT,
- 'Money' => CRM_Utils_Type::T_FLOAT,
- 'Memo' => CRM_Utils_Type::T_TEXT,
'Date' => CRM_Utils_Type::T_DATE,
+ 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
'Boolean' => CRM_Utils_Type::T_BOOLEAN,
'StateProvince' => CRM_Utils_Type::T_INT,
- 'Country' => CRM_Utils_Type::T_INT,
+ 'File' => CRM_Utils_Type::T_STRING,
'Link' => CRM_Utils_Type::T_STRING,
'ContactReference' => CRM_Utils_Type::T_INT,
+ 'Country' => CRM_Utils_Type::T_INT,
];
}
// Regular fields have a 'name' property
$value['name'] = 'custom_' . $key;
$value['title'] = $value['label'];
- $value['type'] = _getStandardTypeFromCustomDataType($value);
+ if ($value['data_type'] == 'Date' && CRM_Utils_Array::value('time_format', $value, 0) > 0) {
+ $value['data_type'] = 'DateTime';
+ }
+ $value['type'] = CRM_Utils_Array::value($value['data_type'], CRM_Core_BAO_CustomField::dataToType());
$ret['custom_' . $key] = $value;
}
return $ret;
}
-/**
- * Translate the custom field data_type attribute into a std 'type'.
- *
- * @param array $value
- *
- * @return int
- */
-function _getStandardTypeFromCustomDataType($value) {
- $dataType = $value['data_type'];
- //CRM-15792 - If date custom field contains timeformat change type to DateTime
- if ($value['data_type'] == 'Date' && isset($value['time_format']) && $value['time_format'] > 0) {
- $dataType = 'DateTime';
- }
- $mapping = array(
- 'String' => CRM_Utils_Type::T_STRING,
- 'Int' => CRM_Utils_Type::T_INT,
- 'Money' => CRM_Utils_Type::T_MONEY,
- 'Memo' => CRM_Utils_Type::T_LONGTEXT,
- 'Float' => CRM_Utils_Type::T_FLOAT,
- 'Date' => CRM_Utils_Type::T_DATE,
- 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
- 'Boolean' => CRM_Utils_Type::T_BOOLEAN,
- 'StateProvince' => CRM_Utils_Type::T_INT,
- 'File' => CRM_Utils_Type::T_STRING,
- 'Link' => CRM_Utils_Type::T_STRING,
- 'ContactReference' => CRM_Utils_Type::T_INT,
- 'Country' => CRM_Utils_Type::T_INT,
- );
- return $mapping[$dataType];
-}
-
/**
* Fill params array with alternate (alias) values where a field has an alias and that is filled & the main field isn't.
$this->assertTrue(strpos($query->_fromClause, $cgTableName) !== FALSE);
}
+ /**
+ * Check where clause of a date custom field when 'IS NOT EMPTY' operator is used
+ */
+ public function testCustomDateField() {
+ $contactID = $this->individualCreate();
+ //Create a test custom group and field.
+ $customGroup = $this->callAPISuccess('CustomGroup', 'create', array(
+ 'title' => "test custom group",
+ 'extends' => "Individual",
+ ));
+ $customTableName = $this->callAPISuccess('CustomGroup', 'getValue', ['id' => $customGroup, 'return' => 'table_name']);
+ $customGroupTableName = $customGroup['values'][$customGroup['id']]['table_name'];
+
+ $createdField = $this->callAPISuccess('customField', 'create', [
+ 'data_type' => 'Date',
+ 'html_type' => 'Select Date',
+ 'date_format' => 'd M yy',
+ 'time_format' => 1,
+ 'label' => 'test field',
+ 'custom_group_id' => $customGroup['id'],
+ ]);
+ $customFieldColumnName = $createdField['values'][$createdField['id']]['column_name'];
+
+ $this->callAPISuccess('Contact', 'create', [
+ 'id' => $contactID,
+ 'custom_' . $createdField['id'] => date('YmdHis'),
+ ]);
+
+ $selector = new CRM_Contact_Selector(
+ 'CRM_Contact_Selector',
+ ['custom_' . $createdField['id'] => ['IS NOT EMPTY' => 1]],
+ [[
+ 0 => 'custom_' . $createdField['id'],
+ 1 => 'IS NOT NULL',
+ 2 => 1,
+ 3 => 1,
+ 4 => 0,
+ ]],
+ [],
+ CRM_Core_Action::NONE,
+ NULL,
+ FALSE,
+ 'builder'
+ );
+
+ $whereClause = $selector->getQueryObject()->query()[2];
+ $expectedClause = sprintf("( %s.%s IS NOT NULL )", $customGroupTableName, $customFieldColumnName);
+ // test the presence of expected date clause
+ $this->assertEquals(TRUE, strpos($whereClause, $expectedClause));
+
+ $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL);
+ $this->assertEquals(1, count($rows));
+ }
+
/**
* Get the default select string since this is generally consistent.
*/