Exemplo n.º 1
0
/**
 * Create a Event
 *  
 * This API is used for creating a Event
 *
 * @param  array   $params           (reference ) input parameters
 * Allowed @params array keys are:
 * {@schema Event/Event.xml} 
 * 
 * @return array of newly created event property values.
 * @access public
 */
function civicrm_event_create(&$params)
{
    _civicrm_initialize(true);
    try {
        civicrm_api_check_permission(__FUNCTION__, $params, true);
        civicrm_verify_mandatory($params, 'CRM_Event_DAO_Event', array('start_date', 'event_type_id', 'title'));
        // Do we really want $params[id], even if we have
        // $params[event_id]? if yes then please uncomment the below line
        //$ids['event'      ] = $params['id'];
        $ids['eventTypeId'] = (int) $params['event_type_id'];
        $ids['startDate'] = $params['start_date'];
        $ids['event_id'] = CRM_Utils_Array::value('event_id', $params);
        require_once 'CRM/Event/BAO/Event.php';
        $eventBAO = CRM_Event_BAO_Event::create($params, $ids);
        if (is_a($eventBAO, 'CRM_Core_Error')) {
            return civicrm_create_error("Event is not created");
        } else {
            $event = array();
            _civicrm_object_to_array($eventBAO, $event);
            $values = array();
            $values['event_id'] = $event['id'];
            $values['is_error'] = 0;
        }
        return $values;
    } catch (Exception $e) {
        return civicrm_create_error($e->getMessage());
    }
}
/**
 * @todo Write sth
 * @todo Serious FIXMES in the code! File issues.
 */
function civicrm_contact_update(&$params, $create_new = FALSE)
{
    _civicrm_initialize();
    try {
        civicrm_api_check_permission(__FUNCTION__, $params, TRUE);
    } catch (Exception $e) {
        return civicrm_create_error($e->getMessage());
    }
    require_once 'CRM/Utils/Array.php';
    $entityId = CRM_Utils_Array::value('contact_id', $params, NULL);
    if (!CRM_Utils_Array::value('contact_type', $params) && $entityId) {
        $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($entityId);
    }
    $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE);
    $values = civicrm_contact_check_params($params, $dupeCheck);
    if ($values) {
        return $values;
    }
    if ($create_new) {
        // Make sure nothing is screwed up before we create a new contact
        if (!empty($entityId)) {
            return civicrm_create_error('Cannot create new contact when contact_id is present');
        }
        if (empty($params['contact_type'])) {
            return civicrm_create_error('Contact Type not specified');
        }
        // If we get here, we're ready to create a new contact
        if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) {
            require_once 'CRM/Core/BAO/LocationType.php';
            $defLocType = CRM_Core_BAO_LocationType::getDefault();
            $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1));
        }
    }
    if ($homeUrl = CRM_Utils_Array::value('home_url', $params)) {
        require_once 'CRM/Core/PseudoConstant.php';
        $websiteTypes = CRM_Core_PseudoConstant::websiteType();
        $params['website'] = array(1 => array('website_type_id' => key($websiteTypes), 'url' => $homeUrl));
    }
    // FIXME: Some legacy support cruft, should get rid of this in 3.1
    $change = array('individual_prefix' => 'prefix', 'prefix' => 'prefix_id', 'individual_suffix' => 'suffix', 'suffix' => 'suffix_id', 'gender' => 'gender_id');
    foreach ($change as $field => $changeAs) {
        if (array_key_exists($field, $params)) {
            $params[$changeAs] = $params[$field];
            unset($params[$field]);
        }
    }
    // End legacy support cruft
    if (isset($params['suffix_id']) && !is_numeric($params['suffix_id'])) {
        $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix());
    }
    if (isset($params['prefix_id']) && !is_numeric($params['prefix_id'])) {
        $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix());
    }
    if (isset($params['gender_id']) && !is_numeric($params['gender_id'])) {
        $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender());
    }
    $error = _civicrm_greeting_format_params($params);
    if (civicrm_error($error)) {
        return $error;
    }
    $values = array();
    if (!($csType = CRM_Utils_Array::value('contact_sub_type', $params)) && $entityId) {
        require_once 'CRM/Contact/BAO/Contact.php';
        $csType = CRM_Contact_BAO_Contact::getContactSubType($entityId);
    }
    $customValue = civicrm_contact_check_custom_params($params, $csType);
    if ($customValue) {
        return $customValue;
    }
    _civicrm_custom_format_params($params, $values, $params['contact_type'], $entityId);
    $params = array_merge($params, $values);
    $contact =& _civicrm_contact_update($params, $entityId);
    if (is_a($contact, 'CRM_Core_Error')) {
        return civicrm_create_error($contact->_errors[0]['message']);
    } else {
        $values = array();
        $values['contact_id'] = $contact->id;
        $values['is_error'] = 0;
    }
    return $values;
}