Merge in 5.47
[civicrm-core.git] / api / api.php
index 8208af08db30d36f42b8a9e653f983f18bd69e62..2a857a1db1b8598b568cc1234ad15a044db6d2a9 100644 (file)
@@ -16,7 +16,7 @@
  * @param array $params
  *   array to be passed to function
  *
- * @return array|int
+ * @return array|int|Civi\Api4\Generic\Result
  */
 function civicrm_api(string $entity, string $action, array $params) {
   return \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params);
@@ -63,8 +63,8 @@ function civicrm_api4(string $entity, string $action, array $params = [], $index
   $indexField = $index && is_string($index) && !CRM_Utils_Rule::integer($index) ? $index : NULL;
   $removeIndexField = FALSE;
 
-  // If index field is not part of the select query, we add it here and remove it below
-  if ($indexField && !empty($params['select']) && is_array($params['select']) && !\Civi\Api4\Utils\SelectUtil::isFieldSelected($indexField, $params['select'])) {
+  // If index field is not part of the select query, we add it here and remove it below (except for oddball "Setting" api)
+  if ($indexField && !empty($params['select']) && is_array($params['select']) && !($entity === 'Setting' && $action === 'get') && !\Civi\Api4\Utils\SelectUtil::isFieldSelected($indexField, $params['select'])) {
     $params['select'][] = $indexField;
     $removeIndexField = TRUE;
   }
@@ -73,7 +73,8 @@ function civicrm_api4(string $entity, string $action, array $params = [], $index
   if ($index && is_array($index)) {
     $indexCol = reset($index);
     $indexField = key($index);
-    if (property_exists($apiCall, 'select')) {
+    // Index array indicates only 1 or 2 fields need to be selected (except for oddball "Setting" api)
+    if ($entity !== 'Setting' && property_exists($apiCall, 'select')) {
       $apiCall->setSelect([$indexCol]);
       if ($indexField && $indexField != $indexCol) {
         $apiCall->addSelect($indexField);
@@ -124,7 +125,8 @@ function civicrm_api4(string $entity, string $action, array $params = [], $index
  *
  * @throws CiviCRM_API3_Exception
  *
- * @return array
+ * @return array|int
+ *   Dependant on the $action
  */
 function civicrm_api3(string $entity, string $action, array $params = []) {
   $params['version'] = 3;
@@ -178,7 +180,7 @@ function _civicrm_api3_api_getfields(&$apiRequest) {
  * 'format.is_success' => 1
  * will result in a boolean success /fail being returned if that is what you need.
  *
- * @param $result
+ * @param mixed $result
  *
  * @return bool
  *   true if error, false otherwise
@@ -281,33 +283,25 @@ function _civicrm_api_replace_variable($value, $parentResult, $separator) {
  *
  * @return string
  *   Entity name in underscore separated format.
+ *
+ * @deprecated
  */
 function _civicrm_api_get_entity_name_from_camel($entity) {
-  if (!$entity || $entity === strtolower($entity)) {
-    return $entity;
-  }
-  elseif ($entity == 'PCP') {
-    return 'pcp';
-  }
-  else {
-    $entity = ltrim(strtolower(str_replace('U_F',
-          'uf',
-          // That's CamelCase, beside an odd UFCamel that is expected as uf_camel
-          preg_replace('/(?=[A-Z])/', '_$0', $entity)
-        )), '_');
+  if (!$entity) {
+    // @todo - this should not be called when empty.
+    return '';
   }
-  return $entity;
+  return CRM_Core_DAO_AllCoreTables::convertEntityNameToLower($entity);
 }
 
 /**
  * Having a DAO object find the entity name.
  *
- * @param object $bao
+ * @param CRM_Core_DAO $bao
  *   DAO being passed in.
  *
  * @return string
  */
 function _civicrm_api_get_entity_name_from_dao($bao) {
-  $daoName = str_replace("BAO", "DAO", get_class($bao));
-  return CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
+  return CRM_Core_DAO_AllCoreTables::getBriefName(get_class($bao));
 }