* Array of permissions to check for this entity-action combo
*/
function _civicrm_api3_permissions($entity, $action, &$params) {
+ // FIXME: Lowercase entity_names are nonstandard but difficult to fix here
+ // because this function invokes hook_civicrm_alterAPIPermissions
$entity = _civicrm_api_get_entity_name_from_camel($entity);
- $action = strtolower($action);
/**
* @var array of permissions
// First try the obvious replacements
$daoName = str_replace(array('_BAO_', '_Form_', '_Page_'), '_DAO_', $className);
- $shortName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
+ $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
// If that didn't work, try a different pattern
- if (!$shortName) {
+ if (!$entityName) {
list(, $parent, , $child) = explode('_', $className);
$daoName = "CRM_{$parent}_DAO_$child";
- $shortName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
+ $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
}
// If that didn't work, try a different pattern
- if (!$shortName) {
+ if (!$entityName) {
$daoName = "CRM_{$parent}_DAO_$parent";
- $shortName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
+ $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
}
// If that didn't work, try a different pattern
- if (!$shortName) {
+ if (!$entityName) {
$daoName = "CRM_Core_DAO_$child";
- $shortName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
+ $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
}
// If that didn't work, try using just the trailing name
- if (!$shortName) {
- $shortName = CRM_Core_DAO_AllCoreTables::getFullName($child) ? $child : NULL;
+ if (!$entityName) {
+ $entityName = CRM_Core_DAO_AllCoreTables::getFullName($child) ? $child : NULL;
}
// If that didn't work, try using just the leading name
- if (!$shortName) {
- $shortName = CRM_Core_DAO_AllCoreTables::getFullName($parent) ? $parent : NULL;
+ if (!$entityName) {
+ $entityName = CRM_Core_DAO_AllCoreTables::getFullName($parent) ? $parent : NULL;
}
- if (!$shortName) {
+ if (!$entityName) {
throw new CRM_Core_Exception('Could not find api name for supplied class');
}
- return _civicrm_api_get_entity_name_from_camel($shortName);
+ return $entityName;
}
}
public function __construct($apiKernel) {
$this->apiKernel = $apiKernel;
$this->actions = array(
- // FIXME: We really need to deal with the api's lack of uniformity wrt
- // case (Entity or entity).
'Entity' => array('get', 'getactions'),
- 'entity' => array('get', 'getactions'),
'*' => array('getactions'), // 'getfields'
);
}
* @throws \Exception
*/
protected function callNestedApi(&$params, &$result, $action, $entity, $version) {
- $entity = _civicrm_api_get_entity_name_from_camel($entity);
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
// We don't need to worry about nested api in the getfields/getoptions
// actions, so just return immediately.
- if (in_array(strtolower($action), array('getfields', 'getoptions'))) {
+ if (in_array($action, array('getfields', 'getoptions'))) {
return;
}
- if (strtolower($action) == 'getsingle') {
+ if ($action == 'getsingle') {
// I don't understand the protocol here, but we don't want
// $result to be a recursive array
// $result['values'][0] = $result;
$subParams = array(
'debug' => \CRM_Utils_Array::value('debug', $params),
);
- $subEntity = $subAPI[1];
+ $subEntity = _civicrm_api_get_entity_name_from_camel($subAPI[1]);
foreach ($result['values'] as $idIndex => $parentAPIValues) {
- if (strtolower($subEntity) != 'contact') {
+ if ($subEntity != 'contact') {
//contact spits the dummy at activity_id so what else won't it like?
//set entity_id & entity table based on the parent's id & entity.
//e.g for something like note if the parent call is contact
//from the parent call. in this case 'contact_id' will also be
//set to the parent's id
$subParams["entity_id"] = $parentAPIValues['id'];
- $subParams['entity_table'] = 'civicrm_' . _civicrm_api_get_entity_name_from_camel($entity);
- $subParams[strtolower($entity) . "_id"] = $parentAPIValues['id'];
+ $subParams['entity_table'] = 'civicrm_' . $lowercase_entity;
+ $subParams[$lowercase_entity . "_id"] = $parentAPIValues['id'];
}
- if (strtolower($entity) != 'contact' && \CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) {
+ if ($entity != 'Contact' && \CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) {
//e.g. if event_id is in the values returned & subentity is event
//then pass in event_id as 'id' don't do this for contact as it
//does some wierd things like returning primary email &
}
// if we are dealing with the same entity pass 'id' through
// (useful for get + delete for example)
- if (strtolower($entity) == strtolower($subEntity)) {
+ if ($lowercase_entity == $subEntity) {
$subParams['id'] = $result['values'][$idIndex]['id'];
}
}
}
}
- if (strtolower($action) == 'getsingle') {
+ if ($action == 'getsingle') {
$result = $result['values'][0];
}
}
/**
* Get camel case version of entity or action name.
*
- * @param $entity
+ * @param string|null $entity
*
- * @return string
+ * @return string|null
*/
function _civicrm_api_get_camel_name($entity) {
- return CRM_Utils_String::convertStringToCamel($entity);
+ return is_string($entity) ? CRM_Utils_String::convertStringToCamel($entity) : NULL;
}
/**
*/
function _civicrm_api_get_entity_name_from_dao($bao) {
$daoName = str_replace("BAO", "DAO", get_class($bao));
- return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName));
+ return CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
}
*/
function civicrm_api3_custom_field_setvalue($params) {
require_once 'api/v3/Generic/Setvalue.php';
- $result = civicrm_api3_generic_setValue(array("entity" => 'custom_field', 'params' => $params));
+ $result = civicrm_api3_generic_setValue(array("entity" => 'CustomField', 'params' => $params));
if (empty($result['is_error'])) {
CRM_Utils_System::flushCache();
}
*/
function civicrm_api3_custom_group_setvalue($params) {
require_once 'api/v3/Generic/Setvalue.php';
- $result = civicrm_api3_generic_setValue(array("entity" => 'custom_group', 'params' => $params));
+ $result = civicrm_api3_generic_setValue(array("entity" => 'CustomGroup', 'params' => $params));
if (empty($result['is_error'])) {
CRM_Utils_System::flushCache();
}
}
}
}
- $entity = _civicrm_api_get_camel_name($apiRequest['entity']);
- $lcase_entity = _civicrm_api_get_entity_name_from_camel($entity);
+ $entity = $apiRequest['entity'];
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
$subentity = CRM_Utils_Array::value('contact_type', $apiRequest['params']);
- $action = strtolower(CRM_Utils_Array::value('action', $apiRequest['params']));
+ $action = CRM_Utils_Array::value('action', $apiRequest['params']);
$sequential = empty($apiRequest['params']) ? 0 : 1;
$apiOptions = CRM_Utils_Array::value('options', $apiRequest['params'], array());
if (!$action || $action == 'getvalue' || $action == 'getcount') {
if (empty($metadata['id'])) {
// if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias
if (!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) {
- $metadata['id'] = $metadata[$lcase_entity . '_id'];
- unset($metadata[$lcase_entity . '_id']);
- $metadata['id']['api.aliases'] = array($lcase_entity . '_id');
+ $metadata['id'] = $metadata[$lowercase_entity . '_id'];
+ unset($metadata[$lowercase_entity . '_id']);
+ $metadata['id']['api.aliases'] = array($lowercase_entity . '_id');
}
}
else {
// (note) or setting for all api where fields is returning 'id' & we want to accept 'note_id' @ the api layer
// nb we don't officially accept note_id anyway - rationale here is more about centralising a now-tested
// inconsistency
- $metadata['id']['api.aliases'] = array($lcase_entity . '_id');
+ $metadata['id']['api.aliases'] = array($lowercase_entity . '_id');
}
break;
'title' => $entity . ' ID',
'name' => 'id',
'api.required' => 1,
- 'api.aliases' => array($lcase_entity . '_id'),
+ 'api.aliases' => array($lowercase_entity . '_id'),
'type' => CRM_Utils_Type::T_INT,
));
break;
*/
function civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL, &$dao = NULL, $extraReturnValues = array()) {
$result = array();
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
+ // TODO: This shouldn't be necessary but this fn sometimes gets called with lowercase entity
+ $entity = _civicrm_api_get_camel_name($entity);
$result['is_error'] = 0;
//lets set the ['id'] field if it's not set & we know what the entity is
- if (is_array($values) && !empty($entity) && $action != 'getfields') {
+ if (is_array($values) && $entity && $action != 'getfields') {
foreach ($values as $key => $item) {
- if (empty($item['id']) && !empty($item[$entity . "_id"])) {
- $values[$key]['id'] = $item[$entity . "_id"];
+ if (empty($item['id']) && !empty($item[$lowercase_entity . "_id"])) {
+ $values[$key]['id'] = $item[$lowercase_entity . "_id"];
}
if (!empty($item['financial_type_id'])) {
//4.3 legacy handling
* options extracted from params
*/
function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $entity = '', $action = '') {
- // Entity should be l-case so it can be concatenated into field names
- $entity = _civicrm_api_get_entity_name_from_camel($entity);
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
$is_count = FALSE;
$sort = CRM_Utils_Array::value('sort', $params, 0);
$sort = CRM_Utils_Array::value('option.sort', $params, $sort);
}
if ($entity && $action == 'get') {
if (!empty($returnProperties['id'])) {
- $returnProperties[$entity . '_id'] = 1;
+ $returnProperties[$lowercase_entity . '_id'] = 1;
unset($returnProperties['id']);
}
switch (trim(strtolower($sort))) {
case 'id':
case 'id desc':
case 'id asc':
- $sort = str_replace('id', $entity . '_id', $sort);
+ $sort = str_replace('id', $lowercase_entity . '_id', $sort);
}
}
$legacyreturnProperties[substr($n, 7)] = $v;
}
elseif ($n == 'id') {
- $inputParams[$entity . '_id'] = $v;
+ $inputParams[$lowercase_entity . '_id'] = $v;
}
elseif (in_array($n, $otherVars)) {
}
$fields = $bao->fields();
if ($unique) {
if (empty($fields['id'])) {
- $entity = _civicrm_api_get_entity_name_from_dao($bao);
- $fields['id'] = $fields[$entity . '_id'];
- unset($fields[$entity . '_id']);
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel(_civicrm_api_get_entity_name_from_dao($bao));
+ $fields['id'] = $fields[$lowercase_entity . '_id'];
+ unset($fields[$lowercase_entity . '_id']);
}
return $fields;
}
*/
function _civicrm_api3_deprecation_check($entity, $result = array()) {
if ($entity) {
- $apiFile = 'api/v3/' . _civicrm_api_get_camel_name($entity) . '.php';
+ $apiFile = "api/v3/$entity.php";
if (CRM_Utils_File::isIncludable($apiFile)) {
require_once $apiFile;
}
- $entity = _civicrm_api_get_entity_name_from_camel($entity);
- $fnName = "_civicrm_api3_{$entity}_deprecation";
+ $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
+ $fnName = "_civicrm_api3_{$lowercase_entity}_deprecation";
if (function_exists($fnName)) {
return $fnName($result);
}
* @inheritDoc
*/
public function fromApiInput($apiRequest) {
- if ($apiRequest['entity'] == 'contact' && $apiRequest['action'] == 'create') {
+ if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') {
if ('Invalid' == CRM_Utils_Array::value('contact_type', $apiRequest['params'])) {
$apiRequest['params']['contact_type'] = 'Individual';
}
* @inheritDoc
*/
public function toApiOutput($apiRequest, $result) {
- if ($apiRequest['entity'] == 'contact' && $apiRequest['action'] == 'create') {
+ if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'create') {
if (isset($result['id'], $result['values'][$result['id']]['display_name'])) {
$result['values'][$result['id']]['display_name_munged'] = 'MUNGE! ' . $result['values'][$result['id']]['display_name'];
unset($result['values'][$result['id']]['display_name']);