case 4:
case 12:
+ case CRM_Utils_Type::T_TIMESTAMP:
//field is of type date or datetime
_civicrm_api3_validate_date($params, $fieldName, $fieldInfo);
break;
*
* @param array $params params from civicrm_api
* @param string $fieldName uniquename of field being checked
- * @param $fieldInfo
+ * @param array $fieldInfo
* @throws Exception
- * @internal param array $fieldinfo array of fields from getfields function
*/
function _civicrm_api3_validate_date(&$params, &$fieldName, &$fieldInfo) {
//should we check first to prevent it from being copied if they have passed in sql friendly format?
if (!empty($params[$fieldInfo['name']])) {
- //accept 'whatever strtotime accepts
- if (strtotime($params[$fieldInfo['name']]) === FALSE) {
- throw new Exception($fieldInfo['name'] . " is not a valid date: " . $params[$fieldInfo['name']]);
- }
- $format = ($fieldInfo['type'] == CRM_Utils_Type::T_DATE) ? 'Ymd000000' : 'YmdHis';
- $params[$fieldInfo['name']] = CRM_Utils_Date::processDate($params[$fieldInfo['name']], NULL, FALSE, $format);
+ $params[$fieldInfo['name']] = _civicrm_api3_getValidDate($params[$fieldInfo['name']], $fieldInfo['name'], $fieldInfo['type']);
}
if ((CRM_Utils_Array::value('name', $fieldInfo) != $fieldName) && !empty($params[$fieldName])) {
- //If the unique field name differs from the db name & is set handle it here
- if (strtotime($params[$fieldName]) === FALSE) {
- throw new Exception($fieldName . " is not a valid date: " . $params[$fieldName]);
+ $params[$fieldName] = _civicrm_api3_getValidDate($params[$fieldName], $fieldName, $fieldInfo['type']);
+ }
+}
+
+/**
+ * convert date into BAO friendly date
+ * we accept 'whatever strtotime accepts'
+ *
+ * @param string $dateValue
+ * @param $fieldName
+ * @param $fieldType
+ *
+ * @throws Exception
+ * @internal param $fieldInfo
+ *
+ * @internal param $params
+ * @return mixed
+ */
+function _civicrm_api3_getValidDate($dateValue, $fieldName, $fieldType) {
+ if (is_array($dateValue)) {
+ foreach ($dateValue as $key => $value) {
+ $dateValue[$key] = _civicrm_api3_getValidDate($value, $fieldName, $fieldType);
}
- $params[$fieldName] = CRM_Utils_Date::processDate($params[$fieldName]);
+ return $dateValue;
+ }
+ if (strtotime($dateValue) === FALSE) {
+ throw new Exception($fieldName . " is not a valid date: " . $dateValue);
}
+ $format = ($fieldType == CRM_Utils_Type::T_DATE) ? 'Ymd000000' : 'YmdHis';
+ return CRM_Utils_Date::processDate($dateValue, NULL, FALSE, $format);
}
/**
$contacts = $this->callAPISuccess('contact', 'get', array('legal_name' => array('IS NULL' => TRUE)));
$this->assertEquals($contacts['count'], CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact WHERE legal_name IS NULL'));
}
+
+ /**
+ /**
+ * CRM-14743 - test api respects search operators
+ */
+ function testGetModifiedDateByOperators() {
+ $preExistingContactCount = CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact');
+ $contact1 = $this->individualCreate();
+ $sql = "UPDATE civicrm_contact SET created_date = '2012-01-01', modified_date = '2013-01-01' WHERE id = " . $contact1;
+ CRM_Core_DAO::executeQuery($sql);
+ $contact2 = $this->individualCreate();
+ $sql = "UPDATE civicrm_contact SET created_date = '2012-02-01', modified_date = '2013-02-01' WHERE id = " . $contact2;
+ CRM_Core_DAO::executeQuery($sql);
+ $contact3 = $this->householdCreate();
+ $sql = "UPDATE civicrm_contact SET created_date = '2012-03-01', modified_date = '2013-03-01' WHERE id = " . $contact3;
+ CRM_Core_DAO::executeQuery($sql);
+ $contacts = $this->callAPISuccess('contact', 'get', array('modified_date' => array('<' => '2014-01-01')));
+ $this->assertEquals($contacts['count'], 3);
+ $contacts = $this->callAPISuccess('contact', 'get', array('modified_date' => array('>' => '2014-01-01')));
+ $this->assertEquals($contacts['count'], $preExistingContactCount);
+ }
}