function setUp()
 {
     parent::setUp();
     //  Create Group For Individual  Contact Type
     $groupIndividual = array('title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5), 'extends' => array('Individual'), 'style' => 'Inline', 'is_active' => 1);
     $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
     $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id']));
     //  Create Group For Individual-Student  Contact Sub  Type
     $groupIndiStudent = array('title' => 'Student Test' . substr(sha1(rand()), 0, 5), 'extends' => array('Individual', array('Student')), 'style' => 'Inline', 'is_active' => 1);
     $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
     $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id']));
     $params = array('first_name' => 'Mathev', 'last_name' => 'Adison', 'contact_type' => 'Individual');
     $this->individual = $this->individualCreate($params);
     $params = array('first_name' => 'Steve', 'last_name' => 'Tosun', 'contact_type' => 'Individual', 'contact_sub_type' => 'Student');
     $this->individualStudent = $this->individualCreate($params);
     $params = array('first_name' => 'Mark', 'last_name' => 'Dawson', 'contact_type' => 'Individual', 'contact_sub_type' => 'Parent');
     $this->individualParent = $this->individualCreate($params);
     $params = array('organization_name' => 'Wellspring', 'contact_type' => 'Organization');
     $this->organization = $this->organizationCreate($params);
     $params = array('organization_name' => 'SubUrban', 'contact_type' => 'Organization', 'contact_sub_type' => 'Sponsor');
     $this->organizationSponsor = $this->organizationCreate($params);
     //refresh php cached variables
     CRM_Core_PseudoConstant::flush();
     CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndividualField['id'], True);
     CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndiStudentField['id'], True);
 }
 public function testCaseType()
 {
     CRM_Core_PseudoConstant::flush();
     $caseTypes = CRM_Case_PseudoConstant::caseType();
     $expectedTypes = array(1 => 'Housing Support', 2 => 'Adult Day Care Referral');
     $this->assertEquals($expectedTypes, $caseTypes);
 }
Esempio n. 3
0
 public function setUp()
 {
     $this->_entity = 'Address';
     parent::setUp();
     $this->_contactID = $this->organizationCreate();
     $this->_locationType = $this->locationTypeCreate();
     CRM_Core_PseudoConstant::flush();
     $this->_params = array('contact_id' => $this->_contactID, 'location_type_id' => $this->_locationType->id, 'street_name' => 'Ambachtstraat', 'street_number' => '23', 'street_address' => 'Ambachtstraat 23', 'postal_code' => '6971 BN', 'country_id' => '1152', 'city' => 'Brummen', 'is_primary' => 1);
 }
Esempio n. 4
0
 public function setUp()
 {
     $this->_apiversion = 3;
     parent::setUp();
     $this->useTransaction();
     $this->_contactID = $this->organizationCreate();
     $loc = $this->locationTypeCreate();
     $this->_locationType = $loc->id;
     CRM_Core_PseudoConstant::flush();
     $this->_params = array('contact_id' => $this->_contactID, 'location_type_id' => $this->_locationType, 'phone' => '(123) 456-7890', 'is_primary' => 1, 'phone_type_id' => 1);
 }
 public function setUp()
 {
     $this->_apiversion = 3;
     parent::setUp();
     $this->useTransaction(TRUE);
     $this->_contactID = $this->organizationCreate();
     $activity = $this->activityCreate();
     $this->_activityID = $activity['id'];
     CRM_Core_PseudoConstant::flush();
     $this->_params = array('contact_id' => $this->_contactID, 'activity_id' => $this->_activityID, 'record_type_id' => 2);
 }
 function setUp()
 {
     $this->_apiversion = 3;
     parent::setUp();
     $this->_contactID = $this->organizationCreate();
     $activity = $this->activityCreate();
     $this->_activityID = $activity['id'];
     CRM_Core_PseudoConstant::flush();
     $this->quickCleanup(array('civicrm_activity_contact'));
     $this->_params = array('contact_id' => $this->_contactID, 'activity_id' => $this->_activityID, 'record_type_id' => 2);
 }
Esempio n. 7
0
 /**
  * Test civicrm_address_getoptions( 'location_type_id' )
  */
 public function testLocationTypeGet()
 {
     // needed to get rid of cached values from previous tests
     CRM_Core_PseudoConstant::flush();
     $params = array('field' => 'location_type_id');
     $result = $this->callAPIAndDocument('address', 'getoptions', $params, __FUNCTION__, __FILE__);
     $this->assertTrue($result['count'] > 3, "In line " . __LINE__);
     $this->assertContains('Home', $result['values'], "In line " . __LINE__);
     $this->assertContains('Work', $result['values'], "In line " . __LINE__);
     $this->assertContains('Main', $result['values'], "In line " . __LINE__);
     $this->assertContains('Billing', $result['values'], "In line " . __LINE__);
 }
Esempio n. 8
0
/**
 * Create an additional location for an existing contact
 *
 * @param array $params  input properties
 *  
 * @return array  the created location's params
 *
 * @access public
 */
function civicrm_location_add(&$params)
{
    _civicrm_initialize();
    $error = _civicrm_location_check_params($params);
    if (civicrm_error($error)) {
        return $error;
    }
    $locationTypeId = CRM_Utils_Array::value('location_type_id', $params);
    if (!$locationTypeId && '3.0' != CRM_Utils_Array::value('version', $params)) {
        require_once 'CRM/Core/DAO/LocationType.php';
        $locationTypeDAO = new CRM_Core_DAO_LocationType();
        $locationTypeDAO->name = $params['location_type'];
        $locationTypeDAO->find(true);
        $locationTypeId = $locationTypeDAO->id;
        CRM_Core_PseudoConstant::flush('locationType');
        if (!isset($locationTypeId)) {
            return civicrm_create_error(ts('$location_type is not valid one'));
        }
    }
    $location =& _civicrm_location_add($params, $locationTypeId);
    return $location;
}
Esempio n. 9
0
 /**
  * Delete Option Value.
  *
  * @param int $optionValueId
  *
  * @return bool
  *
  */
 public static function del($optionValueId)
 {
     $optionValue = new CRM_Core_DAO_OptionValue();
     $optionValue->id = $optionValueId;
     if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) {
         CRM_Core_PseudoConstant::flush();
         return $optionValue->delete();
     }
     return FALSE;
 }
Esempio n. 10
0
/**
 * 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];
}
Esempio n. 11
0
 /**
  * Ensure hook_civicrm_fieldOptions is working
  */
 public function testHookFieldOptions()
 {
     CRM_Core_PseudoConstant::flush();
     // Test replacing all options with a hook
     $this->targetField = 'case_type_id';
     $this->replaceOptions = array('foo' => 'Foo', 'bar' => 'Bar');
     $result = $this->callAPISuccess('case', 'getoptions', array('field' => 'case_type_id'));
     $this->assertEquals($result['values'], $this->replaceOptions);
     // TargetField doesn't match - should get unmodified option list
     $originalGender = CRM_Contact_BAO_Contact::buildOptions('gender_id');
     $this->assertNotEquals($originalGender, $this->replaceOptions);
     // This time we should get foo bar appended to the list
     $this->targetField = 'gender_id';
     $this->appendOptions = array('foo' => 'Foo', 'bar' => 'Bar');
     $this->replaceOptions = NULL;
     CRM_Core_PseudoConstant::flush();
     $result = CRM_Contact_BAO_Contact::buildOptions('gender_id');
     $this->assertEquals($result, $originalGender + $this->appendOptions);
 }
Esempio n. 12
0
 /**
  * Flush all caches related to pseudo-constants.
  *
  * This may be inefficient and should generally be avoided.
  */
 public static function flushAll()
 {
     foreach (self::findConstants() as $constant) {
         self::flushConstant($constant);
     }
     CRM_Core_PseudoConstant::flush();
 }
 public function tearDown()
 {
     $this->quickCleanup(array('civicrm_contribution', 'civicrm_contribution_soft', 'civicrm_event', 'civicrm_contribution_page', 'civicrm_participant', 'civicrm_participant_payment', 'civicrm_line_item', 'civicrm_financial_trxn', 'civicrm_financial_item', 'civicrm_entity_financial_trxn', 'civicrm_contact', 'civicrm_membership', 'civicrm_membership_payment', 'civicrm_payment_processor'));
     CRM_Core_PseudoConstant::flush('taxRates');
 }
Esempio n. 14
0
 /**
  * Create a custom group with a single text custom field.  See
  * participant:testCreateWithCustom for how to use this
  *
  * @param string $function
  *   __FUNCTION__.
  * @param string $filename
  *   $file __FILE__.
  *
  * @return array
  *   ids of created objects
  */
 public function entityCustomGroupWithSingleFieldCreate($function, $filename)
 {
     $params = array('title' => $function);
     $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8);
     $params['extends'] = $entity ? $entity : 'Contact';
     $customGroup = $this->CustomGroupCreate($params);
     $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'], 'label' => $function));
     CRM_Core_PseudoConstant::flush();
     return array('custom_group_id' => $customGroup['id'], 'custom_field_id' => $customField['id']);
 }
Esempio n. 15
0
/**
 * 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];
}