예제 #1
0
/**
 * Use this API to get existing custom values for an entity.
 *
 * @param array $params
 *   Array specifying the entity_id.
 *   Optionally include entity_type param, i.e. 'entity_type' => 'Activity'
 *   If no entity_type is supplied, it will be determined based on the fields you request.
 *   If no entity_type is supplied and no fields are specified, 'Contact' will be assumed.
 *   Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned)
 *   Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1
 *   If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1
 *
 * @throws API_Exception
 * @return array
 */
function civicrm_api3_custom_value_get($params)
{
    $getParams = array('entityID' => $params['entity_id'], 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''));
    if (strstr($getParams['entityType'], 'civicrm_')) {
        $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
    }
    unset($params['entity_id'], $params['entity_table']);
    foreach ($params as $id => $param) {
        if ($param && substr($id, 0, 6) == 'return') {
            $id = substr($id, 7);
            list($c, $i) = CRM_Utils_System::explode('_', $id, 2);
            if ($c == 'custom' && is_numeric($i)) {
                $names['custom_' . $i] = 'custom_' . $i;
                $id = $i;
            } else {
                // Lookup names if ID was not supplied
                list($group, $field) = CRM_Utils_System::explode(':', $id, 2);
                $id = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
                if (!$id) {
                    continue;
                }
                $names['custom_' . $id] = 'custom_' . $i;
            }
            $getParams['custom_' . $id] = 1;
        }
    }
    $result = CRM_Core_BAO_CustomValueTable::getValues($getParams);
    if ($result['is_error']) {
        if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
            $values = array();
            return civicrm_api3_create_success($values, $params, 'CustomValue');
        } else {
            throw new API_Exception($result['error_message']);
        }
    } else {
        $entity_id = $result['entityID'];
        unset($result['is_error'], $result['entityID']);
        // Convert multi-value strings to arrays
        $sp = CRM_Core_DAO::VALUE_SEPARATOR;
        foreach ($result as $id => $value) {
            if (strpos($value, $sp) !== FALSE) {
                $value = explode($sp, trim($value, $sp));
            }
            $idArray = explode('_', $id);
            if ($idArray[0] != 'custom') {
                continue;
            }
            $fieldNumber = $idArray[1];
            $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber);
            $info = array_pop($customFieldInfo);
            // id is the index for returned results
            if (empty($idArray[2])) {
                $n = 0;
                $id = $fieldNumber;
            } else {
                $n = $idArray[2];
                $id = $fieldNumber . "." . $idArray[2];
            }
            if (!empty($params['format.field_names'])) {
                $id = $info['field_name'];
            } else {
                $id = $fieldNumber;
            }
            $values[$id]['entity_id'] = $getParams['entityID'];
            if (!empty($getParams['entityType'])) {
                $values[$id]['entity_table'] = $getParams['entityType'];
            }
            //set 'latest' -useful for multi fields but set for single for consistency
            $values[$id]['latest'] = $value;
            $values[$id]['id'] = $id;
            $values[$id][$n] = $value;
        }
        return civicrm_api3_create_success($values, $params, 'CustomValue');
    }
}
 static function createDefaultPcp($pcpContactId, $componentPageId, $component = 'event')
 {
     if (empty($pcpContactId) || empty($componentPageId)) {
         return FALSE;
     }
     $eventDetails = CRM_Pcpteams_Utils::getEventDetailsbyEventId($componentPageId);
     $contactDisplayName = CRM_Contact_BAO_Contact::displayName($pcpContactId);
     $pcpResult = civicrm_api('Pcpteams', 'create', array('version' => 3, 'title' => $contactDisplayName . ' : ' . $eventDetails['title'], 'intro_text' => "Welcome to " . $contactDisplayName . '\'s PCP', 'contact_id' => $pcpContactId, 'page_id' => $componentPageId, 'page_type' => $component, 'goal_amount' => '0.00'));
     if (!civicrm_error($pcpResult) && $pcpResult['id']) {
         //create activity for pcp created.
         $ids = array('target_contact_id' => $pcpContactId);
         $userId = self::getloggedInUserId();
         $customDigFund = CRM_Core_BAO_CustomField::getCustomFieldID(CRM_Pcpteams_Constant::C_CF_DIGITAL_FUNDRAISING_PCP_ID, CRM_Pcpteams_Constant::C_CG_DIGITAL_FUNDRAISING);
         if ($customDigFund) {
             $ids["custom_{$customDigFund}"] = $pcpResult['id'];
         }
         if ($userId != $pcpContactId) {
             $ids['source_contact_id'] = $userId;
         }
         $activityName = $subject = CRM_Pcpteams_Constant::C_AT_PCP_CREATED;
         $desc = 'New PCP has created';
         self::createPcpActivity($ids, $activityName);
         return $pcpResult['id'];
     }
     return NULL;
 }