From 4eea368c7d16771594437de18d2b7b5e3f544023 Mon Sep 17 00:00:00 2001 From: Johan Vervloet Date: Tue, 3 Mar 2015 21:31:23 +0100 Subject: [PATCH] Created a custom civicrm_api3_get_using_query_object. One to work around the issue. Still lots of TODOs. If you filter on a custom field using the api, the custom get_using_query_object is used. And it still ignores the custom fields :-) --- api/v3/Event.php | 8 +++++++ api/v3/utils.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/api/v3/Event.php b/api/v3/Event.php index b6c24ff5ee..c7928b2534 100644 --- a/api/v3/Event.php +++ b/api/v3/Event.php @@ -106,6 +106,14 @@ function _civicrm_api3_event_create_legacy_support_42(&$params) { * Array of all found event property values. */ function civicrm_api3_event_get($params) { + // If $params refers to a custom field, use a hack for + // CRM-16036 + foreach (array_keys($params) as $key) { + if (substr($key, 0, 7) === 'custom_') { + return _civicrm_api3_get_using_query_object_simple( + _civicrm_api3_get_BAO(__FUNCTION__), $params); + } + } //legacy support for $params['return.sort'] if (!empty($params['return.sort'])) { diff --git a/api/v3/utils.php b/api/v3/utils.php index e2a204c2c1..63f071e7a8 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -458,6 +458,59 @@ function _civicrm_api3_store_values(&$fields, &$params, &$values) { return $valueFound; } +/** + * Get function for query object api. + * + * This is a simple get function, but it should be usable for any kind of + * entity. I created it to work around CRM-16036. + * + * @param string $dao_name + * Name of DAO + * @param array $params + * As passed into api get function. + * @return array + */ +function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { + $dao = new $dao_name(); + $entity = _civicrm_api_get_entity_name_from_dao($dao); + $entity_fields = _civicrm_api3_build_fields_array($dao, TRUE); + $where_fields = array_intersect(array_keys($entity_fields), array_keys($params)); + + // TODO: find out which fields to select + $select_fields = "title,id"; + + $select = "SELECT $select_fields "; + $from = "FROM " . $dao->tableName() . " "; + $where = "WHERE 1=1 "; + + $query_params = array(); + + foreach($params as $key => $value) { + // TODO: values of the form array("op" => "value") + if (in_array($key, $where_fields)) { + $param_nr = count($query_params) + 1; + $query_params[$param_nr] = array($value, 'String'); + $where .= "AND $key = %$param_nr "; + } + }; + + $query = "$select $from $where"; + + // TODO: limit, sort,... + + $result_entities = array(); + + $result_dao = CRM_Core_DAO::executeQuery($query, $query_params); + while ($result_dao->fetch()) { + $result_entities[$result_dao->id] = array(); + foreach (explode(",", $select_fields) as $field) { + $result_entities[$result_dao->id][$field] = $result_dao->$field; + }; + } + + return civicrm_api3_create_success($result_entities, $params, $entity, 'get', $dao); +} + /** * Get function for query object api. * @@ -705,6 +758,7 @@ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE) { $dao->setApiFilter($params); } + /** * Apply filters (e.g. high, low) to DAO object (prior to find). * -- 2.25.1