CRM-16019 - Expose date/time format preferences to clientside
[civicrm-core.git] / api / v3 / Generic.php
index 19863eabefec24063ca1dd1e6fb74423193bed85..cef725e0850aea36088db56d168fa94c52dc2348 100644 (file)
@@ -1,4 +1,33 @@
 <?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.6                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * @package CiviCRM_APIv3
+ */
 
 /**
  * Get information about fields for a given api request.
@@ -18,6 +47,7 @@
  *   - version: string
  *   - function: callback (mixed)
  *   - params: array, varies
+ *
  * @return array
  *   API success object
  */
@@ -31,16 +61,20 @@ function civicrm_api3_generic_getfields($apiRequest) {
       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'));
+      $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));
       }
     }
   }
-  $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') {
@@ -65,13 +99,14 @@ function civicrm_api3_generic_getfields($apiRequest) {
     case 'get':
     case 'getsingle':
     case 'getcount':
+    case 'getstat':
       $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']);
       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 {
@@ -80,7 +115,7 @@ function civicrm_api3_generic_getfields($apiRequest) {
         // (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;
 
@@ -90,7 +125,7 @@ function civicrm_api3_generic_getfields($apiRequest) {
           '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;
@@ -137,6 +172,10 @@ function civicrm_api3_generic_getfields($apiRequest) {
   $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array());
 
   foreach ($metadata as $fieldname => $fieldSpec) {
+    // Ensure 'name' is set
+    if (!isset($fieldSpec['name'])) {
+      $metadata[$fieldname]['name'] = $fieldname;
+    }
     _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve);
   }
 
@@ -145,7 +184,7 @@ function civicrm_api3_generic_getfields($apiRequest) {
 }
 
 /**
- * API return function to reformat results as count
+ * API return function to reformat results as count.
  *
  * @param array $apiRequest
  *   Api request as an array. Keys are.
@@ -167,7 +206,7 @@ function civicrm_api3_generic_getcount($apiRequest) {
 }
 
 /**
- * API return function to reformat results as single result
+ * API return function to reformat results as single result.
  *
  * @param array $apiRequest
  *   Api request as an array. Keys are.
@@ -176,7 +215,7 @@ function civicrm_api3_generic_getcount($apiRequest) {
  *   count of results
  */
 function civicrm_api3_generic_getsingle($apiRequest) {
-  // so the first entity is always result['values'][0]
+  // So the first entity is always result['values'][0].
   $apiRequest['params']['sequential'] = 1;
   $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']);
   if ($result['is_error'] !== 0) {
@@ -192,7 +231,7 @@ function civicrm_api3_generic_getsingle($apiRequest) {
 }
 
 /**
- * API return function to reformat results as single value
+ * API return function to reformat results as single value.
  *
  * @param array $apiRequest
  *   Api request as an array. Keys are.
@@ -224,6 +263,8 @@ function civicrm_api3_generic_getvalue($apiRequest) {
 }
 
 /**
+ * Get count of contact references.
+ *
  * @param array $params
  */
 function _civicrm_api3_generic_getrefcount_spec(&$params) {
@@ -232,7 +273,7 @@ function _civicrm_api3_generic_getrefcount_spec(&$params) {
 }
 
 /**
- * API to determine if a record is in-use
+ * API to determine if a record is in-use.
  *
  * @param array $apiRequest
  *   Api request as an array.
@@ -260,7 +301,7 @@ function civicrm_api3_generic_getrefcount($apiRequest) {
 }
 
 /**
- * API wrapper for replace function
+ * API wrapper for replace function.
  *
  * @param array $apiRequest
  *   Api request as an array. Keys are.
@@ -273,7 +314,7 @@ function civicrm_api3_generic_replace($apiRequest) {
 }
 
 /**
- * API wrapper for getoptions function
+ * API wrapper for getoptions function.
  *
  * @param array $apiRequest
  *   Api request as an array.
@@ -282,7 +323,7 @@ function civicrm_api3_generic_replace($apiRequest) {
  *   Array of results
  */
 function civicrm_api3_generic_getoptions($apiRequest) {
-  // Resolve aliases
+  // Resolve aliases.
   $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $apiRequest['params']['field']);
   if (!$fieldName) {
     return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' doesn't exist.");
@@ -305,6 +346,8 @@ function civicrm_api3_generic_getoptions($apiRequest) {
 }
 
 /**
+ * Get metadata.
+ *
  * Function fills the 'options' array on the metadata returned by getfields if
  * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1)
  * (this is passed in as the $fieldsToResolve array)