コード例 #1
0
ファイル: Generic.php プロジェクト: nielosz/civicrm-core
/**
 * 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
 * for get requests & just field name for create. This is because some get functions
 * 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
 *
 * @param bool $unique
 *   Determines whether to key by unique field names (only affects get-type) actions
 *
 * @return array
 *   API success object
 */
function civicrm_api3_generic_getfields($apiRequest, $unique = TRUE)
{
    static $results = array();
    if (CRM_Utils_Array::value('cache_clear', $apiRequest['params'])) {
        $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'])) {
            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)) {
                CRM_Utils_PseudoConstant::flushConstant(_civicrm_api_get_camel_name($optionGroupName));
            }
        }
    }
    $entity = $apiRequest['entity'];
    $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
    $subentity = CRM_Utils_Array::value('contact_type', $apiRequest['params']);
    $action = CRM_Utils_Array::value('action', $apiRequest['params']);
    $sequential = empty($apiRequest['params']['sequential']) ? 0 : 1;
    $apiRequest['params']['options'] = CRM_Utils_Array::value('options', $apiRequest['params'], array());
    $optionsToResolve = (array) CRM_Utils_Array::value('get_options', $apiRequest['params']['options'], array());
    if (!$action || $action == 'getvalue' || $action == 'getcount') {
        $action = 'get';
    }
    // If no options, return results from cache
    if (!$apiRequest['params']['options'] && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity]) && isset($action, $results[$entity . $subentity][$sequential])) {
        return $results[$entity . $subentity][$action][$sequential];
    }
    // defaults based on data model and API policy
    switch ($action) {
        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':
            $unique = FALSE;
        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[$lowercase_entity . '_id'];
                    unset($metadata[$lowercase_entity . '_id']);
                    $metadata['id']['api.aliases'] = array($lowercase_entity . '_id');
                }
            } 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
                // nb we don't officially accept note_id anyway - rationale here is more about centralising a now-tested
                // inconsistency
                $metadata['id']['api.aliases'] = array($lowercase_entity . '_id');
            }
            break;
        case 'delete':
            $metadata = array('id' => array('title' => $entity . ' ID', 'api.required' => 1, 'api.aliases' => array($lowercase_entity . '_id'), 'type' => CRM_Utils_Type::T_INT));
            break;
            // Note: adding setvalue case here instead of in a generic spec function because
            // some APIs override the generic setvalue fn which causes the generic spec to be overlooked.
        // Note: adding setvalue case here instead of in a generic spec function because
        // some APIs override the generic setvalue fn which causes the generic spec to be overlooked.
        case 'setvalue':
            $metadata = array('field' => array('title' => 'Field name', 'api.required' => 1, 'type' => CRM_Utils_Type::T_STRING), 'id' => array('title' => $entity . ' ID', 'api.required' => 1, 'type' => CRM_Utils_Type::T_INT), 'value' => array('title' => 'Value', 'description' => "Field value to set", 'api.required' => 1));
            if (array_intersect(array('all', 'field'), $optionsToResolve)) {
                $options = civicrm_api3_generic_getfields(array('entity' => $entity, array('params' => array('action' => 'create'))));
                $metadata['field']['options'] = CRM_Utils_Array::collect('title', $options['values']);
            }
            break;
        default:
            // oddballs are on their own
            $metadata = array();
    }
    // Normalize this for the sake of spec funcions
    $apiRequest['params']['options']['get_options'] = $optionsToResolve;
    // find any supplemental information
    $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']);
    try {
        list($apiProvider, $hypApiRequest) = \Civi::service('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);
    }
    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);
        // Convert options to "sequential" format
        if ($sequential && !empty($metadata[$fieldname]['options'])) {
            $metadata[$fieldname]['options'] = CRM_Utils_Array::makeNonAssociative($metadata[$fieldname]['options']);
        }
    }
    $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], $entity, 'getfields');
    return $results[$entity][$action][$sequential];
}
コード例 #2
0
 /**
  * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
  * any static properties which have corresponding static methods.
  *
  * This may be inefficient and should generally be avoided.
  *
  * @return array
  *   Array of string, constant names
  */
 public static function findConstants()
 {
     if (self::$constants === NULL) {
         self::$constants = array();
         foreach (self::$constantClasses as $class) {
             foreach (self::findConstantsByClass($class) as $constant) {
                 self::$constants[$constant] = $class;
             }
         }
     }
     return array_keys(self::$constants);
 }
コード例 #3
0
ファイル: System.php プロジェクト: rollox/civicrm-core
 /**
  * Reset the various system caches and some important static variables.
  */
 public static function flushCache()
 {
     // flush out all cache entries so we can reload new data
     // a bit aggressive, but livable for now
     $cache = CRM_Utils_Cache::singleton();
     $cache->flush();
     // also reset the various static memory caches
     // reset the memory or array cache
     CRM_Core_BAO_Cache::deleteGroup('contact fields', NULL, FALSE);
     // reset ACL cache
     CRM_ACL_BAO_Cache::resetCache();
     // reset various static arrays used here
     CRM_Contact_BAO_Contact::$_importableFields = CRM_Contact_BAO_Contact::$_exportableFields = CRM_Contribute_BAO_Contribution::$_importableFields = CRM_Contribute_BAO_Contribution::$_exportableFields = CRM_Pledge_BAO_Pledge::$_exportableFields = CRM_Contribute_BAO_Query::$_contributionFields = CRM_Core_BAO_CustomField::$_importFields = CRM_Core_BAO_Cache::$_cache = CRM_Core_DAO::$_dbColumnValueCache = NULL;
     CRM_Core_OptionGroup::flushAll();
     CRM_Utils_PseudoConstant::flushAll();
 }
コード例 #4
0
ファイル: Generic.php プロジェクト: hguru/224Civi
/**
 * 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
 * for get requests & just field name for create. This is because some get functions
 * 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
 */
function civicrm_api3_generic_getfields($apiRequest)
{
    static $results = array();
    if (CRM_Utils_Array::value('cache_clear', $apiRequest['params'])) {
        $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'])) {
            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)) {
                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);
    $subentity = CRM_Utils_Array::value('contact_type', $apiRequest['params']);
    $action = strtolower(CRM_Utils_Array::value('action', $apiRequest['params']));
    $sequential = empty($apiRequest['params']) ? 0 : 1;
    $apiOptions = CRM_Utils_Array::value('options', $apiRequest['params'], array());
    if ($action == 'getvalue' || $action == 'getvalue' || $action == 'getcount') {
        $action = 'get';
    }
    if (empty($action)) {
        $action = 'get';
    }
    // determines whether to use unique field names - seem comment block above
    $unique = TRUE;
    if (empty($apiOptions) && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity]) && isset($action, $results[$entity . $subentity][$sequential])) {
        return $results[$entity . $subentity][$action][$sequential];
    }
    // defaults based on data model and API policy
    switch ($action) {
        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':
            $unique = FALSE;
        case 'get':
            $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');
                }
            } 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
                // 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');
            }
            break;
        case 'delete':
            $metadata = array('id' => array('title' => 'Unique Identifier', 'api.required' => 1, 'api.aliases' => array($lcase_entity . '_id'), 'type' => CRM_Utils_Type::T_INT));
            break;
        case 'getoptions':
            $metadata = array('field' => array('title' => 'Field to retrieve options for', 'api.required' => 1), 'context' => array('title' => 'Context string'));
            break;
        default:
            // oddballs are on their own
            $metadata = array();
    }
    // find any supplemental information
    $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']);
    $hypApiRequest += _civicrm_api_resolve($hypApiRequest);
    $helper = '_' . $hypApiRequest['function'] . '_spec';
    if (function_exists($helper)) {
        // alter
        $helper($metadata, $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);
    }
    $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields');
    return $results[$entity][$action][$sequential];
}