<?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. Getfields information
- * is used for documentation, validation, default setting
+ * Get information about fields for a given api request.
+ *
+ * Getfields information is used for documentation, validation, default setting
* We first query the scheme using the $dao->fields function & then augment
* that information by calling the _spec functions that apply to the relevant function
* Note that we use 'unique' field names as described in the xml/schema files
* 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
- * - entity: string
- * - action: string
- * - version: string
- * - function: callback (mixed)
- * - params: array, varies
- * @return array API success object
+ * @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
*/
function civicrm_api3_generic_getfields($apiRequest) {
static $results = array();
$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));
}
}
case 'getfields':
$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':
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
'title' => 'Context',
),
);
- break;
+ break;
+
default:
// oddballs are on their own
$metadata = array();
list ($apiProvider, $hypApiRequest) = \Civi\Core\Container::singleton()->get('civi_api_kernel')->resolve($hypApiRequest);
if (isset($hypApiRequest['function'])) {
$helper = '_' . $hypApiRequest['function'] . '_spec';
- } else {
+ }
+ else {
// not implemented MagicFunctionProvider
$helper = NULL;
}
- } catch (\Civi\API\Exception\NotImplementedException $e) {
+ }
+ catch (\Civi\API\Exception\NotImplementedException $e) {
$helper = NULL;
}
if (function_exists($helper)) {
$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);
}
}
/**
- * 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
+ * @param array $apiRequest
+ * Api request as an array. Keys are.
*
* @throws API_Exception
- * @return integer count of results
+ * @return int
+ * 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'];
}
/**
- * 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
+ * @param array $apiRequest
+ * Api request as an array. Keys are.
*
- * @return integer count of results
+ * @return int
+ * 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) {
}
/**
- * 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
+ * @param array $apiRequest
+ * Api request as an array. Keys are.
*
- * @return integer count of results
+ * @return int
+ * count of results
*/
function civicrm_api3_generic_getvalue($apiRequest) {
$apiRequest['params']['sequential'] = 1;
}
/**
+ * Get count of contact references.
+ *
* @param array $params
*/
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
+ * @param array $apiRequest
+ * Api request as an array.
*
* @throws API_Exception
- * @return array API result (int 0 or 1)
+ * @return array
+ * API result (int 0 or 1)
*/
function civicrm_api3_generic_getrefcount($apiRequest) {
$entityToClassMap = CRM_Core_DAO_AllCoreTables::daoToClass();
}
/**
- * API wrapper for replace function
+ * 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
+ * @return int
+ * count of results
*/
function civicrm_api3_generic_replace($apiRequest) {
return _civicrm_api3_generic_replace($apiRequest['entity'], $apiRequest['params']);
}
/**
- * API wrapper for getoptions function
+ * 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
+ * @return array
+ * 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.");
}
/**
+ * 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)
*
* 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 $apiRequest
- * @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 array $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, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve){
+function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve) {
if (empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['option_group_id'])) {
return;
}