/** * Ensure that the return values comply with the whitelist's * "fields" policy. * * Most API's follow a convention where the result includes * a 'values' array (which in turn is a list of records). Unfortunately, * some don't. If the API result doesn't meet our expectation, * then we probably don't know what's going on, so we abort the * request. * * This will probably break some of the layered-sugar APIs (like * getsingle, getvalue). Just use the meat-and-potatoes API instead. * Or craft a suitably targeted patch. * * @param array $apiRequest * API request. * @param array $apiResult * API result. * @return array * Modified API result. * @throws \API_Exception */ public function filter($apiRequest, $apiResult) { if ($this->fields === '*') { return $apiResult; } if (isset($apiResult['values']) && empty($apiResult['values'])) { // No data; filtering doesn't matter. return $apiResult; } if (is_array($apiResult['values'])) { $firstRow = \CRM_Utils_Array::first($apiResult['values']); if (is_array($firstRow)) { $fields = $this->filterFields(array_keys($firstRow)); $apiResult['values'] = \CRM_Utils_Array::filterColumns($apiResult['values'], $fields); return $apiResult; } } throw new \API_Exception(sprintf('Filtering failed for %s.%s. Unrecognized result format.', $apiRequest['entity'], $apiRequest['action'])); }
/** * A generic "get" API based on simple array data. This is comparable to * _civicrm_api3_basic_get but does not use DAO/BAO. This is useful for * small/mid-size data loaded from external JSON or XML documents. * * @param array $params * API parameters. * @param array $records * List of all records. * @param string $idCol * The property which defines the ID of a record * @param array $fields * List of filterable fields. * @return array */ function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $fields) { $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get'); // TODO // $sort = CRM_Utils_Array::value('sort', $options, NULL); $offset = CRM_Utils_Array::value('offset', $options); $limit = CRM_Utils_Array::value('limit', $options); $matches = array(); $currentOffset = 0; foreach ($records as $record) { if ($idCol != 'id') { $record['id'] = $record[$idCol]; } $match = TRUE; foreach ($params as $k => $v) { if ($k == 'id') { $k = $idCol; } if (in_array($k, $fields) && $record[$k] != $v) { $match = FALSE; break; } } if ($match) { if ($currentOffset >= $offset) { $matches[$record[$idCol]] = $record; } if ($limit && count($matches) >= $limit) { break; } $currentOffset++; } } $return = CRM_Utils_Array::value('return', $options, array()); if (!empty($return)) { $return['id'] = 1; $matches = CRM_Utils_Array::filterColumns($matches, array_keys($return)); } return civicrm_api3_create_success($matches, $params); }