function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) { $entity = substr($dao->__table, 8); $allfields = _civicrm_api3_build_fields_array($dao, $unique); $fields = array_intersect(array_keys($allfields), array_keys($params)); if (isset($params[$entity . "_id"])) { //if entity_id is set then treat it as ID (will be overridden by id if set) $dao->id = $params[$entity . "_id"]; } //apply options like sort _civicrm_api3_apply_options_to_dao($params, $dao, $entity); //accept filters like filter.activity_date_time_high // std is now 'filters' => .. if (strstr(implode(',', array_keys($params)), 'filter')) { if (isset($params['filters']) && is_array($params['filters'])) { foreach ($params['filters'] as $paramkey => $paramvalue) { _civicrm_api3_apply_filters_to_dao($paramkey, $paramvalue, $dao); } } else { foreach ($params as $paramkey => $paramvalue) { if (strstr($paramkey, 'filter')) { _civicrm_api3_apply_filters_to_dao(substr($paramkey, 7), $paramvalue, $dao); } } } } // http://issues.civicrm.org/jira/browse/CRM-9150 - stick with 'simple' operators for now // support for other syntaxes is discussed in ticket but being put off for now $acceptedSQLOperators = array('=', '<=', '>=', '>', '<', 'LIKE', "<>", "!=", "NOT LIKE", 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'); if (!$fields) { return; } foreach ($fields as $field) { if (is_array($params[$field])) { //get the actual fieldname from db $fieldName = $allfields[$field]['name']; //array is the syntax for SQL clause foreach ($params[$field] as $operator => $criteria) { if (in_array($operator, $acceptedSQLOperators)) { switch ($operator) { // unary operators case 'IS NULL': case 'IS NOT NULL': $dao->whereAdd(sprintf('%s %s', $fieldName, $operator)); break; // ternary operators // ternary operators case 'BETWEEN': case 'NOT BETWEEN': if (empty($criteria[0]) || empty($criteria[1])) { throw new exception("invalid criteria for {$operator}"); } $dao->whereAdd(sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO::escapeString($criteria[0]), CRM_Core_DAO::escapeString($criteria[1]))); break; // n-ary operators // n-ary operators case 'IN': case 'NOT IN': if (empty($criteria)) { throw new exception("invalid criteria for {$operator}"); } $escapedCriteria = array_map(array('CRM_Core_DAO', 'escapeString'), $criteria); $dao->whereAdd(sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria))); break; // binary operators // binary operators default: $dao->whereAdd(sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO::escapeString($criteria))); } } } } else { if ($unique) { $dao->{$allfields}[$field]['name'] = $params[$field]; } else { $dao->{$field} = $params[$field]; } } } if (!empty($params['return']) && is_array($params['return'])) { $dao->selectAdd(); $allfields = _civicrm_api3_get_unique_name_array($dao); $returnMatched = array_intersect($params['return'], $allfields); $returnUniqueMatched = array_intersect(array_diff($params['return'], $returnMatched), array_flip($allfields)); foreach ($returnMatched as $returnValue) { $dao->selectAdd($returnValue); } foreach ($returnUniqueMatched as $uniqueVal) { $dao->selectAdd($allfields[$uniqueVal]); } $dao->selectAdd('id'); } }
/** * Function transfers the filters being passed into the DAO onto the params object. * * @param CRM_Core_DAO $dao * @param array $params * @param bool $unique * * @throws API_Exception * @throws Exception */ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE) { $entity = _civicrm_api_get_entity_name_from_dao($dao); $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity); if (!empty($params[$lowercase_entity . "_id"]) && empty($params['id'])) { //if entity_id is set then treat it as ID (will be overridden by id if set) $params['id'] = $params[$lowercase_entity . "_id"]; } $allfields = _civicrm_api3_build_fields_array($dao, $unique); $fields = array_intersect(array_keys($allfields), array_keys($params)); $options = _civicrm_api3_get_options_from_params($params); //apply options like sort _civicrm_api3_apply_options_to_dao($params, $dao, $entity); //accept filters like filter.activity_date_time_high // std is now 'filters' => .. if (strstr(implode(',', array_keys($params)), 'filter')) { if (isset($params['filters']) && is_array($params['filters'])) { foreach ($params['filters'] as $paramkey => $paramvalue) { _civicrm_api3_apply_filters_to_dao($paramkey, $paramvalue, $dao); } } else { foreach ($params as $paramkey => $paramvalue) { if (strstr($paramkey, 'filter')) { _civicrm_api3_apply_filters_to_dao(substr($paramkey, 7), $paramvalue, $dao); } } } } if (!$fields) { $fields = array(); } foreach ($fields as $field) { if (is_array($params[$field])) { //get the actual fieldname from db $fieldName = $allfields[$field]['name']; $where = CRM_Core_DAO::createSqlFilter($fieldName, $params[$field], 'String'); if (!empty($where)) { $dao->whereAdd($where); } } else { if ($unique) { $daoFieldName = $allfields[$field]['name']; if (empty($daoFieldName)) { throw new API_Exception("Failed to determine field name for \"{$field}\""); } $dao->{$daoFieldName} = $params[$field]; } else { $dao->{$field} = $params[$field]; } } } if (!empty($options['return']) && is_array($options['return']) && empty($options['is_count'])) { $dao->selectAdd(); // Ensure 'id' is included. $options['return']['id'] = TRUE; $allfields = _civicrm_api3_get_unique_name_array($dao); $returnMatched = array_intersect(array_keys($options['return']), $allfields); foreach ($returnMatched as $returnValue) { $dao->selectAdd($returnValue); } // Not already matched on the field names. $unmatchedFields = array_diff(array_keys($options['return']), $returnMatched); $returnUniqueMatched = array_intersect($unmatchedFields, array_flip($allfields)); foreach ($returnUniqueMatched as $uniqueVal) { $dao->selectAdd($allfields[$uniqueVal]); } } $dao->setApiFilter($params); }
/** * Function transfers the filters being passed into the DAO onto the params object */ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) { $entity = substr($dao->__table, 8); $allfields = _civicrm_api3_build_fields_array($dao, $unique); $fields = array_intersect(array_keys($allfields), array_keys($params)); if (isset($params[$entity . "_id"])) { //if entity_id is set then treat it as ID (will be overridden by id if set) $dao->id = $params[$entity . "_id"]; } $options = _civicrm_api3_get_options_from_params($params); //apply options like sort _civicrm_api3_apply_options_to_dao($params, $dao, $entity); //accept filters like filter.activity_date_time_high // std is now 'filters' => .. if (strstr(implode(',', array_keys($params)), 'filter')) { if (isset($params['filters']) && is_array($params['filters'])) { foreach ($params['filters'] as $paramkey => $paramvalue) { _civicrm_api3_apply_filters_to_dao($paramkey, $paramvalue, $dao); } } else { foreach ($params as $paramkey => $paramvalue) { if (strstr($paramkey, 'filter')) { _civicrm_api3_apply_filters_to_dao(substr($paramkey, 7), $paramvalue, $dao); } } } } // http://issues.civicrm.org/jira/browse/CRM-9150 - stick with 'simple' operators for now // support for other syntaxes is discussed in ticket but being put off for now $acceptedSQLOperators = array('=', '<=', '>=', '>', '<', 'LIKE', "<>", "!=", "NOT LIKE", 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'); if (!$fields) { $fields = array(); } foreach ($fields as $field) { if (is_array($params[$field])) { //get the actual fieldname from db $fieldName = $allfields[$field]['name']; $where = CRM_Core_DAO::createSqlFilter($fieldName, $params[$field], 'String'); if (!empty($where)) { $dao->whereAdd($where); } } else { if ($unique) { $daoFieldName = $allfields[$field]['name']; if (empty($daoFieldName)) { throw new API_Exception("Failed to determine field name for \"{$field}\""); } $dao->{$daoFieldName} = $params[$field]; } else { $dao->{$field} = $params[$field]; } } } if (!empty($options['return']) && is_array($options['return']) && empty($options['is_count'])) { $dao->selectAdd(); $options['return']['id'] = TRUE; // ensure 'id' is included $allfields = _civicrm_api3_get_unique_name_array($dao); $returnMatched = array_intersect(array_keys($options['return']), $allfields); foreach ($returnMatched as $returnValue) { $dao->selectAdd($returnValue); } $unmatchedFields = array_diff(array_keys($options['return']), $returnMatched); $returnUniqueMatched = array_intersect($unmatchedFields, array_flip($allfields)); foreach ($returnUniqueMatched as $uniqueVal) { $dao->selectAdd($allfields[$uniqueVal]); } } $dao->setApiFilter($params); }