From 9bee5ea2cc81d8db6542ed4b8aeb54a9f72c1e6e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 24 May 2014 21:12:31 +1200 Subject: [PATCH] CRM-14743 add ability to api-range-query modified date --- CRM/Contact/BAO/Query.php | 4 ++- api/v3/utils.php | 43 ++++++++++++++++++++-------- tests/phpunit/api/v3/ContactTest.php | 21 ++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 58985301b8..c643bcb6ab 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -2116,7 +2116,9 @@ class CRM_Contact_BAO_Query { CRM_Core_Error::fatal(); } } - $value = CRM_Core_DAO::createSQLFilter($name, $value, NULL); + $this->_where[$grouping][] = CRM_Core_DAO::createSQLFilter($name, $value, NULL); + //since this is not currently being called by the form layer we can skip worrying about the 'qill' for now + return; } if (!empty($field['where'])) { diff --git a/api/v3/utils.php b/api/v3/utils.php index 15ee63310b..f040ced75d 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1229,6 +1229,7 @@ function _civicrm_api3_validate_fields($entity, $action, &$params, $fields, $err 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; @@ -1277,27 +1278,45 @@ function _civicrm_api3_validate_fields($entity, $action, &$params, $fields, $err * * @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); } /** diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index cff73f0aa1..673d831a7f 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -1800,4 +1800,25 @@ class api_v3_ContactTest extends CiviUnitTestCase { $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); + } } -- 2.25.1