INFRA-132 - Fix misc oddball syntax
[civicrm-core.git] / api / v3 / Generic.php
index 7327d90750f3fd718f2b79e326088e3576ba08cd..ee41baa8a80ccab305cf2cb86beb98fa14f4afc0 100644 (file)
  * access multiple objects e.g. contact api accesses is_deleted from the activity
  * table & from the contact table
  *
- * @param array $apiRequest api request as an array. Keys are
+ * @param array $apiRequest
+ *   Api request as an array. Keys are.
  *  - entity: string
  *  - action: string
  *  - version: string
  *  - function: callback (mixed)
  *  - params: array, varies
- *  @return array API success object
+ * @return array API success object
  */
 function civicrm_api3_generic_getfields($apiRequest) {
   static $results = array();
@@ -24,12 +25,12 @@ function civicrm_api3_generic_getfields($apiRequest) {
     $results = array();
     // we will also clear pseudoconstants here - should potentially be moved to relevant BAO classes
     CRM_Core_PseudoConstant::flush();
-    if(!empty($apiRequest['params']['fieldname'])){
+    if (!empty($apiRequest['params']['fieldname'])) {
       CRM_Utils_PseudoConstant::flushConstant($apiRequest['params']['fieldname']);
     }
-    if(!empty($apiRequest['params']['option_group_id'])){
-      $optionGroupName = civicrm_api('option_group', 'getvalue', array('version' => 3, 'id' => $apiRequest['params']['option_group_id'], 'return' => 'name') );
-      if(is_string($optionGroupName)){
+    if (!empty($apiRequest['params']['option_group_id'])) {
+      $optionGroupName = civicrm_api('option_group', 'getvalue', array('version' => 3, 'id' => $apiRequest['params']['option_group_id'], 'return' => 'name'));
+      if (is_string($optionGroupName)) {
         CRM_Utils_PseudoConstant::flushConstant(_civicrm_api_get_camel_name($optionGroupName));
       }
     }
@@ -52,23 +53,25 @@ function civicrm_api3_generic_getfields($apiRequest) {
   // defaults based on data model and API policy
   switch ($action) {
     case 'getfields':
-      $values = _civicrm_api_get_fields($entity, false, $apiRequest['params']);
+      $values = _civicrm_api_get_fields($entity, FALSE, $apiRequest['params']);
       return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields');
     case 'create':
     case 'update':
     case 'replace':
       $unique = FALSE;
     case 'get':
+    case 'getsingle':
+    case 'getcount':
       $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']);
-      if (empty($metadata['id'])){
+      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'])) {
+        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');
         }
       }
-      else{
+      else {
         // really the preference would be to set the unique name in the xml
         // question is which is a less risky fix this close to a release - setting in xml for the known failure
         // (note) or setting for all api where fields is returning 'id' & we want to accept 'note_id' @ the api layer
@@ -80,7 +83,9 @@ function civicrm_api3_generic_getfields($apiRequest) {
 
     case 'delete':
       $metadata = array(
-        'id' => array('title' => 'Unique Identifier',
+        'id' => array(
+          'title' => $entity . ' ID',
+          'name' => 'id',
           'api.required' => 1,
           'api.aliases' => array($lcase_entity . '_id'),
           'type' => CRM_Utils_Type::T_INT,
@@ -90,14 +95,17 @@ function civicrm_api3_generic_getfields($apiRequest) {
     case 'getoptions':
       $metadata = array(
         'field' => array(
-          'title' => 'Field to retrieve options for',
+          'name' => 'field',
+          'title' => 'Field name',
           'api.required' => 1,
         ),
         'context' => array(
-          'title' => 'Context string',
+          'name' => 'context',
+          'title' => 'Context',
         ),
       );
-        break;
+      break;
+
     default:
       // oddballs are on their own
       $metadata = array();
@@ -105,8 +113,19 @@ function civicrm_api3_generic_getfields($apiRequest) {
 
   // find any supplemental information
   $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']);
-  $hypApiRequest += _civicrm_api_resolve($hypApiRequest);
-  $helper = '_' . $hypApiRequest['function'] . '_spec';
+  try {
+    list ($apiProvider, $hypApiRequest) = \Civi\Core\Container::singleton()->get('civi_api_kernel')->resolve($hypApiRequest);
+    if (isset($hypApiRequest['function'])) {
+      $helper = '_' . $hypApiRequest['function'] . '_spec';
+    }
+    else {
+      // not implemented MagicFunctionProvider
+      $helper = NULL;
+    }
+  }
+  catch (\Civi\API\Exception\NotImplementedException $e) {
+    $helper = NULL;
+  }
   if (function_exists($helper)) {
     // alter
     $helper($metadata, $apiRequest);
@@ -115,27 +134,29 @@ function civicrm_api3_generic_getfields($apiRequest) {
   $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array());
 
   foreach ($metadata as $fieldname => $fieldSpec) {
-    _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest['entity'], $fieldname, $fieldSpec, $fieldsToResolve);
+    _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve);
   }
 
-  $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields');
+  $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], $entity, 'getfields');
   return $results[$entity][$action][$sequential];
 }
 
 /**
  * API return function to reformat results as count
  *
- * @param array $apiRequest api request as an array. Keys are
+ * @param array $apiRequest
+ *   Api request as an array. Keys are.
  *
+ * @throws API_Exception
  * @return integer count of results
  */
 function civicrm_api3_generic_getcount($apiRequest) {
   $apiRequest['params']['options']['is_count'] = TRUE;
   $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']);
-  if(is_numeric (CRM_Utils_Array::value('values', $result))) {
+  if (is_numeric(CRM_Utils_Array::value('values', $result))) {
     return (int) $result['values'];
   }
-  if(!isset($result['count'])) {
+  if (!isset($result['count'])) {
     throw new API_Exception(ts('Unexpected result from getcount') . print_r($result, TRUE));
   }
   return $result['count'];
@@ -144,7 +165,8 @@ function civicrm_api3_generic_getcount($apiRequest) {
 /**
  * API return function to reformat results as single result
  *
- * @param array $apiRequest api request as an array. Keys are
+ * @param array $apiRequest
+ *   Api request as an array. Keys are.
  *
  * @return integer count of results
  */
@@ -167,7 +189,8 @@ function civicrm_api3_generic_getsingle($apiRequest) {
 /**
  * API return function to reformat results as single value
  *
- * @param array $apiRequest api request as an array. Keys are
+ * @param array $apiRequest
+ *   Api request as an array. Keys are.
  *
  * @return integer count of results
  */
@@ -194,10 +217,46 @@ function civicrm_api3_generic_getvalue($apiRequest) {
   return civicrm_api3_create_error("missing param return=field you want to read the value of", array('error_type' => 'mandatory_missing', 'missing_param' => 'return'));
 }
 
+/**
+ * @param array $params
+ */
+function _civicrm_api3_generic_getrefcount_spec(&$params) {
+  $params['id']['api.required'] = 1;
+  $params['id']['title'] = 'Entity ID';
+}
+
+/**
+ * API to determine if a record is in-use
+ *
+ * @param array $apiRequest
+ *   Api request as an array.
+ *
+ * @throws API_Exception
+ * @return array API result (int 0 or 1)
+ */
+function civicrm_api3_generic_getrefcount($apiRequest) {
+  $entityToClassMap = CRM_Core_DAO_AllCoreTables::daoToClass();
+  if (!isset($entityToClassMap[$apiRequest['entity']])) {
+    throw new API_Exception("The entity '{$apiRequest['entity']}' is unknown or unsupported by 'getrefcount'. Consider implementing this API.", 'getrefcount_unsupported');
+  }
+  $daoClass = $entityToClassMap[$apiRequest['entity']];
+
+  /* @var $dao CRM_Core_DAO */
+  $dao = new $daoClass();
+  $dao->id = $apiRequest['params']['id'];
+  if ($dao->find(TRUE)) {
+    return civicrm_api3_create_success($dao->getReferenceCounts());
+  }
+  else {
+    return civicrm_api3_create_success(array());
+  }
+}
+
 /**
  * API wrapper for replace function
  *
- * @param array $apiRequest api request as an array. Keys are
+ * @param array $apiRequest
+ *   Api request as an array. Keys are.
  *
  * @return integer count of results
  */
@@ -208,7 +267,8 @@ function civicrm_api3_generic_replace($apiRequest) {
 /**
  * API wrapper for getoptions function
  *
- * @param array $apiRequest api request as an array.
+ * @param array $apiRequest
+ *   Api request as an array.
  *
  * @return array of results
  */
@@ -224,18 +284,15 @@ function civicrm_api3_generic_getoptions($apiRequest) {
   unset($apiRequest['params']['context'], $apiRequest['params']['field']);
 
   $baoName = _civicrm_api3_get_BAO($apiRequest['entity']);
-  $options = $output = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
+  $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
   if ($options === FALSE) {
     return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list.");
   }
   // Support 'sequential' output as a non-associative array
   if (!empty($apiRequest['params']['sequential'])) {
-    $output = array();
-    foreach ($options as $key => $val) {
-      $output[] = array('key' => $key, 'value' => $val);
-    }
+    $options = CRM_Utils_Array::makeNonAssociative($options);
   }
-  return civicrm_api3_create_success($output, $apiRequest['params'], $apiRequest['entity'], 'getoptions');
+  return civicrm_api3_create_success($options, $apiRequest['params'], $apiRequest['entity'], 'getoptions');
 }
 
 /**
@@ -245,16 +302,22 @@ function civicrm_api3_generic_getoptions($apiRequest) {
  * 2) the field is a pseudoconstant and is NOT an FK
  * - the reason for this is that checking / transformation is done on pseudoconstants but
  * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure)
- * @todo - if may be we should define a 'resolve' key on the psuedoconstant for when these rules are not fine enough
+ * @todo - if may be we should define a 'resolve' key on the pseudoconstant for when these rules are not fine enough
  *
  * This function is only split out for the purpose of code clarity / comment block documentation
- * @param array $metadata the array of metadata that will form the result of the getfields function
- * @param string $fieldname field currently being processed
- * @param array $fieldSpec metadata for that field
- * @param array $fieldsToResolve anny field resolutions specifically requested
+ *
+ * @param array $metadata
+ *   The array of metadata that will form the result of the getfields function.
+ * @param $apiRequest
+ * @param string $fieldname
+ *   Field currently being processed.
+ * @param array $fieldSpec
+ *   Metadata for that field.
+ * @param array $fieldsToResolve
+ *   Anny field resolutions specifically requested.
  */
-function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldname, $fieldSpec, $fieldsToResolve){
-  if (empty($fieldSpec['pseudoconstant'])) {
+function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve) {
+  if (empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['option_group_id'])) {
     return;
   }
 
@@ -262,7 +325,7 @@ function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldn
     return;
   }
 
-  $options = civicrm_api($entity, 'getoptions', array('version' => 3, 'field' => $fieldname));
+  $options = civicrm_api($apiRequest['entity'], 'getoptions', array('version' => 3, 'field' => $fieldname, 'sequential' => !empty($apiRequest['params']['sequential'])));
   if (is_array(CRM_Utils_Array::value('values', $options))) {
     $metadata[$fieldname]['options'] = $options['values'];
   }