CRM-13254 (Closes) make entity_tag.get api use basic create rather than ...
$params['current_employer'] = array(
'title' => 'Current Employer',
'description' => 'Name of Current Employer',
+ 'type' => CRM_Utils_Type::T_STRING,
);
$params['dupe_check'] = array(
'title' => 'Check for Duplicates',
return civicrm_api3_create_success($values, $params);
}
else {
- return civicrm_api3_create_error($result['error_message']);
+ throw new API_Exception($result['error_message']);
}
}
else {
* @return array API result array
*/
function civicrm_api3_profile_submit($params) {
+ $profileID = _civicrm_api3_profile_getProfileID($params['profile_id']);
- if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
+ if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_active')) {
+ //@todo declare pseudoconstant & let api do this
throw new API_Exception('Invalid value for profile_id');
}
- $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']);
+ $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($profileID);
- if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
- throw new API_Exception('Can not retrieve values for profiles include fields for more than one record type.');
+ if (!empty($params['id']) && CRM_Core_BAO_UFField::checkProfileType($profileID) && !$isContactActivityProfile) {
+ throw new API_Exception('Update profiles including more than one entity not currently supported');
}
$contactParams = $activityParams = $missingParams = array();
- $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'],
+ $profileFields = CRM_Core_BAO_UFGroup::getFields($profileID,
FALSE,
NULL,
NULL,
$errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'],
$params['contact_id'],
- $params['profile_id']
+ $profileID
);
if (!empty($errors)) {
throw new API_Exception(array_pop($errors));
throw new API_Exception("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams));
}
- $contactParams['version'] = 3;
$contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params);
- $contactParams['profile_id'] = $params['profile_id'];
+ $contactParams['profile_id'] = $profileID;
$contactParams['skip_custom'] = 1;
$contactProfileParams = civicrm_api3_profile_apply($contactParams);
return civicrm_api3('contact', 'create', $profileParams);
$ufGroupDetails = array();
- $ufGroupParams = array('id' => $params['profile_id']);
+ $ufGroupParams = array('id' => $profileID);
CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails);
if (isset($profileFields['group'])) {
*/
function civicrm_api3_profile_apply($params) {
- civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
-
- if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
- return civicrm_api3_create_error('Invalid value for profile_id');
- }
-
$profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'],
FALSE,
NULL,
$realFieldName = $field['field_name'];
//see function notes
+ // as we build up a list of these we should be able to determine a generic approach
+ //
$hardCodedEntityFields = array(
'state_province' => 'state_province_id',
'country' => 'country_id',
'participant_status' => 'status_id',
'gender' => 'gender_id',
+ 'financial_type' => 'financial_type_id',
+ 'soft_credit' => 'soft_credit_to',
+ 'group' => 'group_id',
+ 'tag' => 'tag_id',
);
+
if(array_key_exists($realFieldName, $hardCodedEntityFields)) {
$realFieldName = $hardCodedEntityFields[$realFieldName];
}
+
$entities[$entity][$fieldName] = $realFieldName;
}
foreach ($entities as $entity => $entityFields) {
$result = civicrm_api3($entity, 'getfields', array('action' => 'create'));
+ $entityGetFieldsResult = _civicrm_api3_profile_appendaliases($result['values'], $entity);
foreach ($entityFields as $entityfield => $realName) {
- $profileFields[$profileID][$entityfield] = $result['values'][$realName];
- if($optionsBehaviour && !empty($result['values'][$realName]['pseudoconstant'])) {
+ $profileFields[$profileID][$entityfield] = $entityGetFieldsResult[$realName];
+ if($optionsBehaviour && !empty($entityGetFieldsResult[$realName]['pseudoconstant'])) {
if($optionsBehaviour > 1 || !in_array($realName, array('state_province_id', 'county_id', 'country_id'))) {
$options = civicrm_api3($entity, 'getoptions', array('field' => $realName));
$profileFields[$profileID][$entityfield]['options'] = $options['values'];
$fieldName .= '-' . $field['location_type_id'];
$entity = 'Phone';
}
+ // @todo - sort this out!
//here we do a hard-code list of known fields that don't map to where they are mapped to
- // not a great solution but probably if we looked in the BAO we'd find a scary switch
+ // not a great solution but probably if we looked in the BAO we'd find a scary switch statement
+ // in a perfect world the uf_field table would hold the correct entity for each item
+ // & only the relationships between entities would need to be coded
$hardCodedEntityMappings = array(
'street_address' => 'Address',
'street_number' => 'Address',
'state_province' => 'Address',
'country' => 'Address',
'county' => 'Address',
+ //note that in discussions about how to restructure the api we discussed making these membership
+ // fields into 'membership_payment' fields - which would entail declaring them in getfields
+ // & renaming them in existing profiles
+ 'financial_type' => 'Contribution',
+ 'total_amount' => 'Contribution',
+ 'receive_date' => 'Contribution',
+ 'payment_instrument' => 'Contribution',
+ 'check_number' => 'Contribution',
+ 'contribution_status_id' => 'Contribution',
+ 'soft_credit' => 'Contribution',
+ 'group' => 'GroupContact',
+ 'tag' => 'EntityTag',
);
if(array_key_exists($fieldName, $hardCodedEntityMappings)) {
$entity = $hardCodedEntityMappings[$fieldName];
* how we add a a pseudoconstant to this pseudoapi to make that work
*/
function _civicrm_api3_profile_getProfileID($profileID) {
- if(!empty($profileID) && !strtolower($profileID) == 'billing' && !is_numeric($profileID)) {
+ if(!empty($profileID) && strtolower($profileID) != 'billing' && !is_numeric($profileID)) {
$profileID = civicrm_api3('uf_group', 'getvalue', array('return' => 'id', 'name' => $profileID));
}
return $profileID;
+}
+
+/**
+ * helper function to add all aliases as keys to getfields response so we can look for keys within it
+ * since the relationship between profile fields & api / metadata based fields is a bit inconsistent
+ * @param array $values
+ *
+ * e.g getfields response incl 'membership_type_id' - with api.aliases = 'membership_type'
+ * returned array will include both as keys (with the same values)
+ */
+function _civicrm_api3_profile_appendaliases($values, $entity) {
+ foreach ($values as $field => $spec) {
+ if(!empty($spec['api.aliases'])) {
+ foreach ($spec['api.aliases'] as $alias) {
+ $values[$alias] = $spec;
+ }
+ }
+ if(!empty($spec['uniqueName'])) {
+ $values[$spec['uniqueName']] = $spec;
+ }
+ }
+ //special case on membership & contribution - can't see how to handle in a generic way
+ if(in_array($entity, array('Membership', 'Contribution'))) {
+ $values['send_receipt'] = array('title' => 'Send Receipt', 'type' => (int) 16);
+ }
+ return $values;
}
\ No newline at end of file
),
'3' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '3',
'1' => 'warm beer',
- '2' => '',
+ '2' => 'defaultValue',
),
'4' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '4',
- '1' => '',
- '2' => '',
+ '1' => 'defaultValue',
+ '2' => 'defaultValue',
),
'5' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '5',
- '1' => '',
+ '1' => 'defaultValue',
),
'6' => array(
'entity_id' => '1',
),
'7' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '7',
- '1' => '',
+ '1' => 'defaultValue',
),
),
),
'pseudoconstant' => array(
'optionGroupName' => 'individual_prefix',
),
+ 'api.aliases' => array(
+ '0' => 'individual_prefix',
+ '1' => 'individual_prefix_id',
+ ),
),
'suffix_id' => array(
'name' => 'suffix_id',
'pseudoconstant' => array(
'optionGroupName' => 'individual_suffix',
),
+ 'api.aliases' => array(
+ '0' => 'individual_suffix',
+ '1' => 'individual_suffix_id',
+ ),
),
'email_greeting_id' => array(
'name' => 'email_greeting_id',
'groupTitle' => 'select_test_g',
'data_type' => 'String',
'html_type' => 'Select',
+ 'default_value' => '',
'text_length' => '',
'options_per_line' => '',
'custom_group_id' => '1',
'1' => array(
'id' => '1',
'custom_group_id' => '1',
- 'name' => 'Name1',
+ 'name' => 'test_textfield2',
'label' => 'Name1',
'data_type' => 'String',
'html_type' => 'Text',
$expectedResult = array(
'is_error' => 0,
'version' => 3,
- 'count' => 4,
+ 'count' => 7,
'values' => array(
'mySingleField' => array(
'entity_id' => '2',
'latest' => 'value 1',
'id' => 'mySingleField',
),
- 'Cust_Field' => array(
+ 'field_12' => array(
+ 'entity_id' => '2',
+ 'latest' => 'value 3',
+ 'id' => 'field_12',
+ '1' => 'value 2',
+ '2' => 'value 3',
+ ),
+ 'field_22' => array(
+ 'entity_id' => '2',
+ 'latest' => '',
+ 'id' => 'field_22',
+ '1' => 'warm beer',
+ '2' => '',
+ ),
+ 'field_32' => array(
+ 'entity_id' => '2',
+ 'latest' => '',
+ 'id' => 'field_32',
+ '1' => 'fl* w*',
+ '2' => '',
+ ),
+ 'field_13' => array(
'entity_id' => '2',
'latest' => 'coffee',
- 'id' => 'Cust_Field',
- '1' => '',
+ 'id' => 'field_13',
+ '1' => 'defaultValue',
'2' => 'coffee',
),
- 'field_2' => array(
+ 'field_23' => array(
'entity_id' => '2',
'latest' => 'value 4',
- 'id' => 'field_2',
+ 'id' => 'field_23',
'1' => '',
'2' => 'value 4',
),
- 'field_3' => array(
+ 'field_33' => array(
'entity_id' => '2',
'latest' => '',
- 'id' => 'field_3',
+ 'id' => 'field_33',
'1' => 'vegemite',
'2' => '',
),
'entity_id' => '2',
'latest' => 'coffee',
'id' => '5',
- '1' => '',
+ '1' => 'defaultValue',
'2' => 'coffee',
),
'6' => array(
'date_format' => '',
'time_format' => '',
'name' => 'custom_1',
+ 'type' => 2,
),
'profile_id' => array(
'api.required' => true,
foreach ($customfields as $key => $value) {
// Regular fields have a 'name' property
$value['name'] = 'custom_' . $key;
+ $value['type'] = _getStandardTypeFromCustomDataType($value['data_type']);
$customfields['custom_' . $key] = $value;
if (in_array('custom_' . $key, $getoptions)) {
$customfields['custom_' . $key]['options'] = CRM_Core_BAO_CustomOption::valuesByID($key);
}
return $customfields;
}
-
+/**
+ * Translate the custom field data_type attribute into a std 'type'
+ */
+function _getStandardTypeFromCustomDataType($dataType) {
+ $mapping = array(
+ 'String' => CRM_Utils_Type::T_STRING,
+ 'Int' => CRM_Utils_Type::T_INT,
+ 'Money' => CRM_Utils_Type::T_MONEY,
+ 'Memo' => CRM_Utils_Type::T_LONGTEXT,
+ 'Float' => CRM_Utils_Type::T_FLOAT,
+ 'Date' => CRM_Utils_Type::T_DATE,
+ 'Boolean' => CRM_Utils_Type::T_BOOLEAN,
+ 'StateProvince' => CRM_Utils_Type::T_INT,
+ 'File' => CRM_Utils_Type::T_STRING,
+ 'Link' => CRM_Utils_Type::T_STRING,
+ 'ContactReference' => CRM_Utils_Type::T_INT,
+ );
+ return $mapping[$dataType];
+}
/**
* Return array of defaults for the given API (function is a wrapper on getfields)
*/
$customGroup = $this->CustomGroupMultipleCreateByParams($params);
$ids['custom_group_id'] = $customGroup['id'];
- $customField = $this->customFieldCreate($ids['custom_group_id']);
+ $customField = $this->customFieldCreate(array('custom_group_id' => $ids['custom_group_id'], 'label' => 'field_1' . $ids['custom_group_id']));
$ids['custom_field_id'][] = $customField['id'];
- $customField = $this->customFieldCreate($ids['custom_group_id'], 'field_2');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $ids['custom_group_id'], 'default_value' => '', 'label' => 'field_2' . $ids['custom_group_id']));
$ids['custom_field_id'][] = $customField['id'];
- $customField = $this->customFieldCreate($ids['custom_group_id'], 'field_3');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $ids['custom_group_id'], 'default_value' => '', 'label' => 'field_3' . $ids['custom_group_id']));
$ids['custom_field_id'][] = $customField['id'];
return $ids;
$entity = substr(basename($filename), 0, strlen(basename($filename)) - 8);
$params['extends'] = $entity ? $entity : 'Contact';
$customGroup = $this->CustomGroupCreate($params);
- $customField = $this->customFieldCreate($customGroup['id'], $function);
+ $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']);
* @param int $customGroupID
*/
function customGroupDelete($customGroupID) {
-
$params['id'] = $customGroupID;
return $this->callAPISuccess('custom_group', 'delete', $params);
}
/**
* Function to create custom field
*
- * @param int $customGroupID
+ * @param array $params (custom_group_id) is required
* @param string $name name of custom field
* @param int $apiversion API version to use
*/
- function customFieldCreate($customGroupID, $name = "Cust Field") {
-
- $params = array(
- 'label' => $name,
- 'name' => $name,
- 'custom_group_id' => $customGroupID,
+ function customFieldCreate($params) {
+ $params = array_merge(array(
+ 'label' => 'Custom Field',
'data_type' => 'String',
'html_type' => 'Text',
'is_searchable' => 1,
'is_active' => 1,
'default_value' => 'defaultValue',
- );
+ ), $params);
$result = $this->callAPISuccess('custom_field', 'create', $params);
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
$params['custom_' . $ids['custom_field_id']] = "custom string";
$moreids = $this->CustomGroupMultipleCreateWithFields();
- $andmoreids = $this->CustomGroupMultipleCreateWithFields(array('title' => "another group"));
+ $andmoreids = $this->CustomGroupMultipleCreateWithFields(array('title' => "another group", 'name' => 'another name'));
$description = "/*this demonstrates the usage of chained api functions. A variety of techniques are used";
$subfile = "APIChainedArrayMultipleCustom";
$params = array(
*/
function testCustomFieldGetReturnOptions(){
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$result = $this->callAPISuccess('custom_field', 'getsingle', array(
'id' => $customField['id'],
*/
function testCustomFieldGetReturnArray(){
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$result = $this->callAPISuccess('custom_field', 'getsingle', array(
'id' => $customField['id'],
*/
function testCustomFieldGetReturnTwoOptions(){
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$result = $this->callAPISuccess('custom_field', 'getsingle', array(
'id' => $customField['id'],
*/
function testCustomFieldDelete() {
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$this->assertNotNull($customField['id'], 'in line ' . __LINE__);
$params = array(
$this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
- $this->IndividualField = $this->customFieldCreate($this->CustomGroupIndividual['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
+ $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id']));
// Create Group For Individual-Student Contact Sub Type
$groupIndiStudent = array(
$this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
- $this->IndiStudentField = $this->customFieldCreate($this->CustomGroupIndiStudent['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
+ $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id']));
$params = array(
'first_name' => 'Mathev',
$result = $this->callAPISuccess('Contact', 'create', $params);
$contact_id = $result['id'];
- $result = $this->callAPISuccess('Contact', 'create',
- array(
- 'contact_type' => 'Individual',
- 'id' => $contact_id,
- 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 3",
- 'custom_' . $this->ids['multi2']['custom_field_id'][0] => "coffee",
- 'custom_' . $this->ids['multi2']['custom_field_id'][1] => "value 4",
- )
+ $firstCustomField = $this->ids['multi']['custom_field_id'][0];
+ $secondCustomField = $this->ids['multi2']['custom_field_id'][0];
+ $thirdCustomField = $this->ids['multi2']['custom_field_id'][1];
+ $createParams = array(
+ 'contact_type' => 'Individual',
+ 'id' => $contact_id,
+ 'custom_' . $firstCustomField => "value 3",
+ 'custom_' . $secondCustomField => "coffee",
+ 'custom_' . $thirdCustomField => "value 4",
);
+ $result = $this->callAPISuccess('Contact', 'create', $createParams);
$params = array(
'id' => $result['id'],
$resultformatted = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, "utilises field names", 'formatFieldName');
// delete the contact
$this->callAPISuccess('contact', 'delete', array('id' => $contact_id));
-
- $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['2'], "In line " . __LINE__);
- $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['latest'], "In line " . __LINE__);
- $this->assertEquals($this->ids['multi2']['custom_field_id'][0], $result['values'][$this->ids['multi2']['custom_field_id'][0]]['id'], "In line " . __LINE__);
- $this->assertEquals('', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['1'], "In line " . __LINE__);
- $this->assertEquals($contact_id, $result['values'][$this->ids['multi2']['custom_field_id'][0]]['entity_id'], "In line " . __LINE__);
- $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0'], "In line " . __LINE__);
- $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest'], "In line " . __LINE__);
- $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest'], "In line " . __LINE__);
+ $this->assertEquals('coffee', $result['values'][$secondCustomField]['2']);
+ $this->assertEquals('coffee', $result['values'][$secondCustomField]['latest']);
+ $this->assertEquals($secondCustomField, $result['values'][$secondCustomField]['id']);
+ $this->assertEquals('defaultValue', $result['values'][$secondCustomField]['1']);
+ $this->assertEquals($contact_id, $result['values'][$secondCustomField]['entity_id']);
+ $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0']);
+ $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest']);
+ $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest']);
+ $this->assertEquals('', $result['values'][$thirdCustomField]['1']);
+ $this->assertEquals('value 4', $result['values'][$thirdCustomField]['2']);
}
}
$this->assertFalse(array_key_exists('participant_status', $result['values']));
}
/**
- * Check getfields works & gives us our fields
+ * Check getfields works & gives us our fields - partipant profile
*/
function testGetFieldsParticipantProfile() {
$result = $this->callAPISuccess('profile', 'getfields', array(
$this->assertTrue(array_key_exists('participant_status', $result['values']));
$this->assertEquals('Attended', $result['values']['participant_status']['options'][2]);
}
+
+ /**
+ * Check getfields works & gives us our fields - membership_batch_entry
+ * (getting to the end with no e-notices is pretty good evidence it's working)
+ */
+ function testGetFieldsMembershipBatchProfile() {
+ $result = $this->callAPISuccess('profile', 'getfields', array(
+ 'action' => 'submit',
+ 'profile_id' => 'membership_batch_entry',
+ 'get_options' => 'all')
+ );
+ $this->assertTrue(array_key_exists('total_amount', $result['values']));
+ $this->assertEquals(12, $result['values']['receive_date']['type']);
+ }
+
+ /**
+ * Check getfields works & gives us our fields - do them all
+ * (getting to the end with no e-notices is pretty good evidence it's working)
+ */
+ function testGetFieldsAllProfiles() {
+ $result = $this->callAPISuccess('uf_group', 'get', array('return' => 'id'));
+ $profileIDs = array_keys($result['values']);
+ foreach ($profileIDs as $profileID) {
+ $result = $this->callAPISuccess('profile', 'getfields', array(
+ 'action' => 'submit',
+ 'profile_id' => $profileID,
+ 'get_options' => 'all')
+ );
+ }
+ }
/////////////// test $this->callAPISuccess3_profile_set //////////////////
/**