Esempio n. 1
0
 /**
  * Add/edit/register contacts through profile.
  *
  * @param array $params
  *   Array of profile fields to be edited/added.
  * @param array $fields
  *   Array of fields from UFGroup.
  * @param int $contactID
  *   Id of the contact to be edited/added.
  * @param int $addToGroupID
  *   Specifies the default group to which contact is added.
  * @param int $ufGroupId
  *   Uf group id (profile id).
  * @param string $ctype
  * @param bool $visibility
  *   Basically lets us know where this request is coming from.
  *                                if via a profile from web, we restrict what groups are changed
  *
  * @return int
  *   contact id created/edited
  */
 public static function createProfileContact(&$params, &$fields, $contactID = NULL, $addToGroupID = NULL, $ufGroupId = NULL, $ctype = NULL, $visibility = FALSE)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     self::addBillingNameFieldsIfOtherwiseNotSet($params);
     // If a user has logged in, or accessed via a checksum
     // Then deliberately 'blanking' a value in the profile should remove it from their record
     $session = CRM_Core_Session::singleton();
     $params['updateBlankLocInfo'] = TRUE;
     if (($session->get('authSrc') & CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN) == 0) {
         $params['updateBlankLocInfo'] = FALSE;
     }
     if ($contactID) {
         $editHook = TRUE;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = FALSE;
         CRM_Utils_Hook::pre('create', 'Profile', NULL, $params);
     }
     list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype);
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && !empty($contactDetails['contact_id'])) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     $contact = self::create($data);
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (empty($contactID)) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (!empty($fields['group'])) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (!empty($fields['tag'])) {
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } elseif ($addToGroupID) {
         $contactIds = array($contactID);
         CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
     }
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Esempio n. 2
0
 /**
  * Add/edit/register contacts through profile.
  *
  * @param array $params
  *   Array of profile fields to be edited/added.
  * @param array $fields
  *   Array of fields from UFGroup.
  * @param int $contactID
  *   Id of the contact to be edited/added.
  * @param int $addToGroupID
  *   Specifies the default group to which contact is added.
  * @param int $ufGroupId
  *   Uf group id (profile id).
  * @param string $ctype
  * @param bool $visibility
  *   Basically lets us know where this request is coming from.
  *                                if via a profile from web, we restrict what groups are changed
  *
  * @return int
  *   contact id created/edited
  */
 public static function createProfileContact(&$params, &$fields, $contactID = NULL, $addToGroupID = NULL, $ufGroupId = NULL, $ctype = NULL, $visibility = FALSE)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     if ($contactID) {
         $editHook = TRUE;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = FALSE;
         CRM_Utils_Hook::pre('create', 'Profile', NULL, $params);
     }
     list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype);
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && !empty($contactDetails['contact_id'])) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     $contact = self::create($data);
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (empty($contactID)) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (!empty($fields['group'])) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (!empty($fields['tag'])) {
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } elseif ($addToGroupID) {
         $contactIds = array($contactID);
         CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
     }
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Esempio n. 3
0
/**
 * Submit a set of fields against a profile.
 * Note choice of submit versus create is discussed CRM-13234 & related to the fact
 * 'profile' is being treated as a data-entry entity
 *
 * @param array $params
 *
 * @throws API_Exception
 * @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', $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($profileID);
    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 = civicrm_api3('profile', 'getfields', array('action' => 'submit', 'profile_id' => $profileID));
    $profileFields = $profileFields['values'];
    if ($isContactActivityProfile) {
        civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
        $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], $params['contact_id'], $profileID);
        if (!empty($errors)) {
            throw new API_Exception(array_pop($errors));
        }
    }
    foreach ($profileFields as $fieldName => $field) {
        if (!isset($params[$fieldName])) {
            continue;
        }
        $value = $params[$fieldName];
        if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
            $value = $params[$fieldName . '_id'];
        }
        $contactEntities = array('contact', 'individual', 'organization', 'household');
        $locationEntities = array('email', 'address', 'phone', 'website', 'im');
        $entity = strtolower(CRM_Utils_Array::value('entity', $field));
        if ($entity && !in_array($entity, array_merge($contactEntities, $locationEntities))) {
            $contactParams['api.' . $entity . '.create'][$fieldName] = $value;
            //@todo we are not currently declaring this option
            if (isset($params['batch_id']) && strtolower($entity) == 'contribution') {
                $contactParams['api.' . $entity . '.create']['batch_id'] = $params['batch_id'];
            }
            if (isset($params[$entity . '_id'])) {
                //todo possibly declare $entity_id in getfields ?
                $contactParams['api.' . $entity . '.create']['id'] = $params[$entity . '_id'];
            }
        } else {
            $contactParams[_civicrm_api3_profile_translate_fieldnames_for_bao($fieldName)] = $value;
        }
    }
    if (isset($contactParams['api.contribution.create']) && isset($contactParams['api.membership.create'])) {
        $contactParams['api.membership_payment.create'] = array('contribution_id' => '$value.api.contribution.create.id', 'membership_id' => '$value.api.membership.create.id');
    }
    if (isset($contactParams['api.contribution.create']) && isset($contactParams['api.participant.create'])) {
        $contactParams['api.participant_payment.create'] = array('contribution_id' => '$value.api.contribution.create.id', 'participant_id' => '$value.api.participant.create.id');
    }
    $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params);
    $contactParams['profile_id'] = $profileID;
    $contactParams['skip_custom'] = 1;
    $contactProfileParams = civicrm_api3_profile_apply($contactParams);
    // Contact profile fields
    $profileParams = $contactProfileParams['values'];
    // If profile having activity fields
    if ($isContactActivityProfile && !empty($activityParams)) {
        $activityParams['id'] = $params['activity_id'];
        $profileParams['api.activity.create'] = $activityParams;
    }
    $groups = $tags = array();
    if (isset($profileParams['group'])) {
        $groups = $profileParams['group'];
        unset($profileParams['group']);
    }
    if (isset($profileParams['tag'])) {
        $tags = $profileParams['tag'];
        unset($profileParams['tag']);
    }
    return civicrm_api3('contact', 'create', $profileParams);
    $ufGroupDetails = array();
    $ufGroupParams = array('id' => $profileID);
    CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails);
    if (isset($profileFields['group'])) {
        CRM_Contact_BAO_GroupContact::create($groups, $params['contact_id'], FALSE, 'Admin');
    }
    if (isset($profileFields['tag'])) {
        CRM_Core_BAO_EntityTag::create($tags, 'civicrm_contact', $params['contact_id']);
    }
    if (!empty($ufGroupDetails['add_to_group_id'])) {
        $contactIds = array($params['contact_id']);
        CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $ufGroupDetails['add_to_group_id']);
    }
    return $result;
}
Esempio n. 4
0
 /**
  * function to add/edit/register contacts through profile.
  *
  * @params  array  $params        Array of profile fields to be edited/added.
  * @params  int    $contactID     contact_id of the contact to be edited/added.
  * @params  array  $fields        array of fields from UFGroup
  * @params  int    $addToGroupID  specifies the default group to which contact is added.
  * @params  int    $ufGroupId     uf group id (profile id)
  * @param   string $ctype         contact type
  *
  * @return  int                   contact id created/edited
  * @static
  * @access public
  */
 static function createProfileContact(&$params, &$fields, $contactID = null, $addToGroupID = null, $ufGroupId = null, $ctype = null, $visibility = false)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     require_once 'CRM/Utils/Hook.php';
     if ($contactID) {
         $editHook = true;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = false;
         CRM_Utils_Hook::pre('create', 'Profile', null, $params);
     }
     $data = $contactDetails = array();
     // get the contact details (hier)
     if ($contactID) {
         list($details, $options) = self::getHierContactDetails($contactID, $fields);
         $contactDetails = $details[$contactID];
         $data['contact_type'] = CRM_Utils_Array::value('contact_type', $contactDetails);
     } else {
         //we should get contact type only if contact
         if ($ufGroupId) {
             require_once "CRM/Core/BAO/UFField.php";
             $data['contact_type'] = CRM_Core_BAO_UFField::getProfileType($ufGroupId);
             //special case to handle profile with only contact fields
             if ($data['contact_type'] == 'Contact') {
                 $data['contact_type'] = 'Individual';
             } else {
                 if (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) {
                     $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']);
                 }
             }
         } else {
             if ($ctype) {
                 $data['contact_type'] = $ctype;
             } else {
                 $data['contact_type'] = 'Individual';
             }
         }
     }
     //fix contact sub type CRM-5125
     if ($subType = CRM_Utils_Array::value('contact_sub_type', $params)) {
         $data['contact_sub_type'] = $subType;
     } else {
         if ($ufGroupId) {
             $data['contact_sub_type'] = CRM_Core_BAO_UFField::getProfileSubType($ufGroupId, $data['contact_type']);
         }
     }
     if ($ctype == "Organization") {
         $data["organization_name"] = $contactDetails["organization_name"];
     } else {
         if ($ctype == "Household") {
             $data["household_name"] = $contactDetails["household_name"];
         }
     }
     $locationType = array();
     $count = 1;
     if ($contactID) {
         //add contact id
         $data['contact_id'] = $contactID;
         $primaryLocationType = self::getPrimaryLocationType($contactID);
     } else {
         require_once "CRM/Core/BAO/LocationType.php";
         $defaultLocation =& CRM_Core_BAO_LocationType::getDefault();
         $defaultLocationId = $defaultLocation->id;
     }
     // get the billing location type
     $locationTypes =& CRM_Core_PseudoConstant::locationType();
     $billingLocationTypeId = array_search('Billing', $locationTypes);
     $blocks = array('email', 'phone', 'im', 'openid');
     // prevent overwritten of formatted array, reset all block from
     // params if it is not in valid format (since import pass valid format)
     foreach ($blocks as $blk) {
         if (array_key_exists($blk, $params) && !is_array($params[$blk])) {
             unset($params[$blk]);
         }
     }
     $primaryPhoneLoc = null;
     foreach ($params as $key => $value) {
         $fieldName = $locTypeId = $typeId = null;
         list($fieldName, $locTypeId, $typeId) = CRM_Utils_System::explode('-', $key, 3);
         //store original location type id
         $actualLocTypeId = $locTypeId;
         if ($locTypeId == 'Primary') {
             if ($contactID) {
                 $locTypeId = $primaryLocationType;
             } else {
                 $locTypeId = $defaultLocationId;
             }
         }
         if (is_numeric($locTypeId)) {
             $index = $locTypeId;
             if (is_numeric($typeId)) {
                 $index .= '-' . $typeId;
             }
             if (!in_array($index, $locationType)) {
                 $locationType[$count] = $index;
                 $count++;
             }
             require_once 'CRM/Utils/Array.php';
             $loc = CRM_Utils_Array::key($index, $locationType);
             $blockName = 'address';
             if (in_array($fieldName, $blocks)) {
                 $blockName = $fieldName;
             }
             $data[$blockName][$loc]['location_type_id'] = $locTypeId;
             //set is_billing true, for location type "Billing"
             if ($locTypeId == $billingLocationTypeId) {
                 $data[$blockName][$loc]['is_billing'] = 1;
             }
             if ($contactID) {
                 //get the primary location type
                 if ($locTypeId == $primaryLocationType) {
                     $data[$blockName][$loc]['is_primary'] = 1;
                 }
             } else {
                 if (($locTypeId == $defaultLocationId || $locTypeId == $billingLocationTypeId) && ($loc == 1 || !CRM_Utils_Array::retrieveValueRecursive($data['location'][$loc - 1], 'is_primary'))) {
                     $data[$blockName][$loc]['is_primary'] = 1;
                 }
             }
             if ($fieldName == 'phone') {
                 if ($typeId) {
                     $data['phone'][$loc]['phone_type_id'] = $typeId;
                 } else {
                     $data['phone'][$loc]['phone_type_id'] = '';
                 }
                 $data['phone'][$loc]['phone'] = $value;
                 //special case to handle primary phone with different phone types
                 // in this case we make first phone type as primary
                 if (isset($data['phone'][$loc]['is_primary']) && !$primaryPhoneLoc) {
                     $primaryPhoneLoc = $loc;
                 }
                 if ($loc != $primaryPhoneLoc) {
                     unset($data['phone'][$loc]['is_primary']);
                 }
             } else {
                 if ($fieldName == 'email') {
                     $data['email'][$loc]['email'] = $value;
                 } else {
                     if ($fieldName == 'im') {
                         if (isset($params[$key . '-provider_id'])) {
                             $data['im'][$loc]['provider_id'] = $params[$key . '-provider_id'];
                         }
                         $data['im'][$loc]['name'] = $value;
                     } else {
                         if ($fieldName == 'openid') {
                             # $value should be a hash of the OpenID fields
                             foreach ($value as $key => $val) {
                                 $data['openid'][$loc][$key] = $val;
                             }
                         } else {
                             if ($fieldName === 'state_province') {
                                 // CRM-3393
                                 if (is_numeric($value) && (int) $value >= 1000) {
                                     $data['address'][$loc]['state_province_id'] = $value;
                                 } else {
                                     $data['address'][$loc]['state_province'] = $value;
                                 }
                             } else {
                                 if ($fieldName === 'country') {
                                     // CRM-3393
                                     if (is_numeric($value) && (int) $value >= 1000) {
                                         $data['address'][$loc]['country_id'] = $value;
                                     } else {
                                         $data['address'][$loc]['country'] = $value;
                                     }
                                 } else {
                                     if ($fieldName === 'county') {
                                         $data['address'][$loc]['address']['county_id'] = $value;
                                     } else {
                                         if ($fieldName == 'address_name') {
                                             $data['address'][$loc]['name'] = $value;
                                         } else {
                                             $data['address'][$loc][$fieldName] = $value;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if ($key === 'individual_suffix') {
                 $data['suffix_id'] = $value;
             } else {
                 if ($key === 'individual_prefix') {
                     $data['prefix_id'] = $value;
                 } else {
                     if ($key === 'gender') {
                         $data['gender_id'] = $value;
                     } else {
                         if ($key === 'email_greeting') {
                             //save email/postal greeting and addressee values if any, CRM-4575
                             $data['email_greeting_id'] = $value;
                         } else {
                             if ($key === 'postal_greeting') {
                                 $data['postal_greeting_id'] = $value;
                             } else {
                                 if ($key === 'addressee') {
                                     $data['addressee_id'] = $value;
                                 } else {
                                     if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) {
                                         // for autocomplete transfer hidden value instead of label
                                         if (isset($params[$key . '_id'])) {
                                             $value = $params[$key . '_id'];
                                         }
                                         $type = CRM_Utils_Array::value('contact_sub_type', $data) ? $data['contact_sub_type'] : $data['contact_type'];
                                         CRM_Core_BAO_CustomField::formatCustomField($customFieldId, $data['custom'], $value, $type, null, $contactID);
                                     } else {
                                         if ($key == 'edit') {
                                             continue;
                                         } else {
                                             if ($key == 'location') {
                                                 foreach ($value as $locationTypeId => $field) {
                                                     foreach ($field as $block => $val) {
                                                         if ($block == 'address' && array_key_exists('address_name', $val)) {
                                                             $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name'];
                                                         }
                                                     }
                                                 }
                                             }
                                             $data[$key] = $value;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // FIX ME: need to check if we need this code
     // //make sure primary location is at first position in location array
     // if ( isset( $data['location'] ) && count( $data['location'] ) > 1 ) {
     //     // if first location is primary skip manipulation
     //     if ( !isset($data['location'][1]['is_primary']) ) {
     //         //find the key for primary location
     //         foreach ( $data['location'] as $primaryLocationKey => $value ) {
     //             if ( isset( $value['is_primary'] ) ) {
     //                 break;
     //             }
     //         }
     //
     //         // swap first location with primary location
     //         $tempLocation        = $data['location'][1];
     //         $data['location'][1] = $data['location'][$primaryLocationKey];
     //         $data['location'][$primaryLocationKey] = $tempLocation;
     //     }
     // }
     if (!isset($data['contact_type'])) {
         $data['contact_type'] = 'Individual';
     }
     if (CRM_Core_Permission::access('Quest')) {
         $studentFieldPresent = 0;
         foreach ($fields as $name => $field) {
             // check if student fields present
             require_once 'CRM/Quest/BAO/Student.php';
             if (!$studentFieldPresent && array_key_exists($name, CRM_Quest_BAO_Student::exportableFields())) {
                 $studentFieldPresent = 1;
             }
         }
     }
     //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone)
     $privacy = CRM_Core_SelectValues::privacy();
     foreach ($privacy as $key => $value) {
         if (array_key_exists($key, $fields)) {
             if ($params[$key]) {
                 $data[$key] = $params[$key];
             } else {
                 $data[$key] = 0;
             }
         }
     }
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, false);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, false);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && CRM_Utils_Array::value('contact_id', $contactDetails)) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     if ($data['contact_type'] != 'Student') {
         $contact =& self::create($data);
     }
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (!$contactID) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (CRM_Utils_Array::value('group', $fields)) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (CRM_Utils_Array::value('tag', $fields)) {
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } else {
         if ($addToGroupID) {
             $contactIds = array($contactID);
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
         }
     }
     //to update student record
     if (CRM_Core_Permission::access('Quest') && $studentFieldPresent) {
         $ids = array();
         $dao =& new CRM_Quest_DAO_Student();
         $dao->contact_id = $contactID;
         if ($dao->find(true)) {
             $ids['id'] = $dao->id;
         }
         $ssids = array();
         $studentSummary =& new CRM_Quest_DAO_StudentSummary();
         $studentSummary->contact_id = $contactID;
         if ($studentSummary->find(true)) {
             $ssids['id'] = $studentSummary->id;
         }
         $params['contact_id'] = $contactID;
         //fixed for check boxes
         $specialFields = array('educational_interest', 'college_type', 'college_interest', 'test_tutoring');
         foreach ($specialFields as $field) {
             if ($params[$field]) {
                 $params[$field] = implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, array_keys($params[$field]));
             }
         }
         CRM_Quest_BAO_Student::create($params, $ids);
         CRM_Quest_BAO_Student::createStudentSummary($params, $ssids);
     }
     // reset the group contact cache for this group
     require_once 'CRM/Contact/BAO/GroupContactCache.php';
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Esempio n. 5
0
 /**
  * Form submission of new/edit contact is processed.
  *
  * @access public
  * @return None
  */
 function postProcess()
 {
     // check if dedupe button, if so return.
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->_dedupeButtonName) {
         return;
     }
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     // action is taken depending upon the mode
     $ids = array();
     if ($this->_action & CRM_CORE_ACTION_UPDATE) {
         // if update get all the valid database ids
         // from the session
         $ids = $this->get('ids');
     }
     $params['contact_type'] = $this->_contactType;
     $contact = CRM_Contact_BAO_Contact::create($params, $ids, CRM_CONTACT_FORM_EDIT_LOCATION_BLOCKS);
     //add contact to gruoup
     CRM_Contact_BAO_GroupContact::create($params['group'], $params['contact_id']);
     //add contact to tags
     CRM_Core_BAO_EntityTag::create($params['tag'], $params['contact_id']);
     // here we replace the user context with the url to view this contact
     $config =& CRM_Core_Config::singleton();
     $session =& CRM_Core_Session::singleton();
     CRM_Core_Session::setStatus(ts('Your %1 contact record has been saved.', array(1 => $contact->contact_type_display)));
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->getButtonName('next', 'new')) {
         // add the recently viewed contact
         list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($contact->id);
         CRM_Utils_Recent::add($displayName, CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id), $contactImage, $contact->id);
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/add' . $contact->contact_type[0], 'reset=1&c_type=' . $contact->contact_type));
     } else {
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id));
     }
     CRM_Core_BAO_CustomGroup::postProcess($this->_groupTree, $params);
     // do the updates/inserts
     CRM_Core_BAO_CustomGroup::updateCustomData($this->_groupTree, $this->_contactType, $contact->id);
 }
/**
 * Update Profile field values.
 *
 * @param array  $params       Associative array of property name/value
 *                             pairs to update profile field values
 *
 * @return Updated Contact/ Activity object|CRM_Error
 *
 * @todo add example
 * @todo add test cases
 *
 */
function civicrm_api3_profile_set($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');
    }
    $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']);
    if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
        return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
    }
    $contactParams = $activityParams = $missingParams = array();
    $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], FALSE, NULL, NULL, NULL, FALSE, NULL, TRUE, NULL, CRM_Core_Permission::EDIT);
    if ($isContactActivityProfile) {
        civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
        require_once 'CRM/Profile/Form.php';
        $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], $params['contact_id'], $params['profile_id']);
        if (!empty($errors)) {
            return civicrm_api3_create_error(array_pop($errors));
        }
    }
    foreach ($profileFields as $fieldName => $field) {
        if (CRM_Utils_Array::value('is_required', $field)) {
            if (!CRM_Utils_Array::value($fieldName, $params) || empty($params[$fieldName])) {
                $missingParams[] = $fieldName;
            }
        }
        if (!isset($params[$fieldName])) {
            continue;
        }
        $value = $params[$fieldName];
        if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
            $value = $params[$fieldName . '_id'];
        }
        if ($isContactActivityProfile && CRM_Utils_Array::value('field_type', $field) == 'Activity') {
            $activityParams[$fieldName] = $value;
        } else {
            $contactParams[$fieldName] = $value;
        }
    }
    if (!empty($missingParams)) {
        return civicrm_api3_create_error("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['skip_custom'] = 1;
    $contactProfileParams = civicrm_api3_profile_apply($contactParams);
    if (CRM_Utils_Array::value('is_error', $contactProfileParams)) {
        return $contactProfileParams;
    }
    // Contact profile fields
    $profileParams = $contactProfileParams['values'];
    // If profile having activity fields
    if ($isContactActivityProfile && !empty($activityParams)) {
        $activityParams['id'] = $params['activity_id'];
        $profileParams['api.activity.create'] = $activityParams;
    }
    $groups = $tags = array();
    if (isset($profileParams['group'])) {
        $groups = $profileParams['group'];
        unset($profileParams['group']);
    }
    if (isset($profileParams['tag'])) {
        $tags = $profileParams['tag'];
        unset($profileParams['tag']);
    }
    $result = civicrm_api('contact', 'create', $profileParams);
    if (CRM_Utils_Array::value('is_error', $result)) {
        return $result;
    }
    $ufGroupDetails = array();
    $ufGroupParams = array('id' => $params['profile_id']);
    CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails);
    if (isset($profileFields['group'])) {
        CRM_Contact_BAO_GroupContact::create($groups, $params['contact_id'], FALSE, 'Admin');
    }
    if (isset($profileFields['tag'])) {
        require_once 'CRM/Core/BAO/EntityTag.php';
        CRM_Core_BAO_EntityTag::create($tags, 'civicrm_contact', $params['contact_id']);
    }
    if (CRM_Utils_Array::value('add_to_group_id', $ufGroupDetails)) {
        $contactIds = array($params['contact_id']);
        CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $ufGroupDetails['add_to_group_id']);
    }
    return $result;
}
 /**
  * function to add/edit/register contacts through profile.
  *
  * @params  array  $params        Array of profile fields to be edited/added.
  * @params  int    $contactID     contact_id of the contact to be edited/added.
  * @params  array  $fields        array of fields from UFGroup
  * @params  int    $addToGroupID  specifies the default group to which contact is added.
  * @params  int    $ufGroupId     uf group id (profile id)
  * @param   string $ctype         contact type
  * @param   boolean $visibility   basically lets us know where this request is coming from
  *                                if via a profile from web, we restrict what groups are changed
  *
  * @return  int                   contact id created/edited
  * @static
  * @access public
  */
 static function createProfileContact(&$params, &$fields, $contactID = NULL, $addToGroupID = NULL, $ufGroupId = NULL, $ctype = NULL, $visibility = FALSE)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     if ($contactID) {
         $editHook = TRUE;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = FALSE;
         CRM_Utils_Hook::pre('create', 'Profile', NULL, $params);
     }
     list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype);
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && CRM_Utils_Array::value('contact_id', $contactDetails)) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     if ($data['contact_type'] != 'Student') {
         $contact = self::create($data);
     }
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (!$contactID) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (CRM_Utils_Array::value('group', $fields)) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (CRM_Utils_Array::value('tag', $fields)) {
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } elseif ($addToGroupID) {
         $contactIds = array($contactID);
         CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
     }
     //to update student record
     if (CRM_Core_Permission::access('Quest') && $studentFieldPresent) {
         $ids = array();
         $dao = new CRM_Quest_DAO_Student();
         $dao->contact_id = $contactID;
         if ($dao->find(TRUE)) {
             $ids['id'] = $dao->id;
         }
         $ssids = array();
         $studentSummary = new CRM_Quest_DAO_StudentSummary();
         $studentSummary->contact_id = $contactID;
         if ($studentSummary->find(TRUE)) {
             $ssids['id'] = $studentSummary->id;
         }
         $params['contact_id'] = $contactID;
         //fixed for check boxes
         $specialFields = array('educational_interest', 'college_type', 'college_interest', 'test_tutoring');
         foreach ($specialFields as $field) {
             if ($params[$field]) {
                 $params[$field] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params[$field]));
             }
         }
         CRM_Quest_BAO_Student::create($params, $ids);
         CRM_Quest_BAO_Student::createStudentSummary($params, $ssids);
     }
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Esempio n. 8
0
 /**
  * Process the user submitted custom data values.
  *
  * @access public
  * @return void
  */
 function postProcess()
 {
     $params = $this->controller->exportValues($this->_name);
     $data = array();
     $data['contact_type'] = 'Individual';
     //get the custom fields for the contact
     $customFields = CRM_Core_BAO_CustomField::getFields($data['contact_type']);
     $locationType = array();
     $count = 1;
     if ($this->_id) {
         $primaryLocationType = CRM_Contact_BAO_Contact::getPrimaryLocationType($this->_id);
     }
     $phoneLoc = 0;
     foreach ($params as $key => $value) {
         $keyValue = explode('-', $key);
         if (is_numeric($keyValue[1])) {
             if (!in_array($keyValue[1], $locationType)) {
                 $locationType[$count] = $keyValue[1];
                 $count++;
             }
             require_once 'CRM/Utils/Array.php';
             $loc = CRM_Utils_Array::key($keyValue[1], $locationType);
             $data['location'][$loc]['location_type_id'] = $keyValue[1];
             if ($this->_id) {
                 //get the primary location type
                 if ($keyValue[1] == $primaryLocationType) {
                     $data['location'][$loc]['is_primary'] = 1;
                 }
             } else {
                 if ($loc == 1) {
                     $data['location'][$loc]['is_primary'] = 1;
                 }
             }
             if ($keyValue[0] == 'name') {
                 $data['location'][$loc]['name'] = $value;
             } else {
                 if ($keyValue[0] == 'phone') {
                     $phoneLoc++;
                     if ($keyValue[2]) {
                         $data['location'][$loc]['phone'][$phoneLoc]['phone_type'] = $keyValue[2];
                     } else {
                         $data['location'][$loc]['phone'][$phoneLoc]['phone_type'] = '';
                         $data['location'][$loc]['phone'][$phoneLoc]['is_primary'] = 1;
                     }
                     $data['location'][$loc]['phone'][$phoneLoc]['phone'] = $value;
                 } else {
                     if ($keyValue[0] == 'email') {
                         $data['location'][$loc]['email'][1]['email'] = $value;
                         $data['location'][$loc]['email'][1]['is_primary'] = 1;
                     } else {
                         if ($keyValue[0] == 'im') {
                             $data['location'][$loc]['im'][1]['name'] = $value;
                             $data['location'][$loc]['im'][1]['is_primary'] = 1;
                         } else {
                             if ($keyValue[0] === 'state_province') {
                                 $data['location'][$loc]['address']['state_province_id'] = $value;
                             } else {
                                 if ($keyValue[0] === 'country') {
                                     $data['location'][$loc]['address']['country_id'] = $value;
                                 } else {
                                     $data['location'][$loc]['address'][$keyValue[0]] = $value;
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if ($key === 'individual_suffix') {
                 $data['suffix_id'] = $value;
             } else {
                 if ($key === 'individual_prefix') {
                     $data['prefix_id'] = $value;
                 } else {
                     if ($key === 'gender') {
                         $data['gender_id'] = $value;
                     } else {
                         if (substr($key, 0, 6) === 'custom') {
                             if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
                                 //fix checkbox
                                 if ($customFields[$customFieldID][3] == 'CheckBox') {
                                     $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 }
                                 if ($customFields[$customFieldID][3] == 'Multi-Select') {
                                     $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $value);
                                 }
                                 // fix the date field
                                 if ($customFields[$customFieldID][2] == 'Date') {
                                     $date = CRM_Utils_Date::format($value);
                                     if (!$date) {
                                         $date = '';
                                     }
                                     $value = $date;
                                 }
                                 //to add the id of custom value if exits
                                 //$this->_contact['custom_value_5_id'] = 123;
                                 $str = 'custom_value_' . $customFieldID . '_id';
                                 if ($this->_contact[$str]) {
                                     $id = $this->_contact[$str];
                                 }
                                 $data['custom'][$customFieldID] = array('id' => $id, 'value' => $value, 'extends' => $customFields[$customFieldID][3], 'type' => $customFields[$customFieldID][2], 'custom_field_id' => $customFieldID);
                             }
                         } else {
                             if ($key == 'edit') {
                                 continue;
                             } else {
                                 $data[$key] = $value;
                             }
                         }
                     }
                 }
             }
         }
     }
     // fix all the custom field checkboxes which are empty
     foreach ($this->_fields as $name => $field) {
         $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
         // if there is a custom field of type checkbox and it has not been set
         // then set it to null, thanx to html protocol
         if ($cfID && $customFields[$cfID][3] == 'CheckBox' && CRM_Utils_Array::value('custom', $data) && !CRM_Utils_Array::value($cfID, $data['custom'])) {
             $str = 'custom_value_' . $cfID . '_id';
             if ($this->_contact[$str]) {
                 $id = $this->_contact[$str];
             }
             $data['custom'][$cfID] = array('id' => $id, 'value' => '', 'extends' => $customFields[$cfID][3], 'type' => $customFields[$cfID][2], 'custom_field_id' => $cfID);
         }
     }
     if ($this->_id) {
         $objects = array('contact_id', 'individual_id', 'location_id', 'address_id');
         $ids = array();
         $phoneLoc = 0;
         foreach ($this->_fields as $name => $field) {
             $nameValue = explode('-', $name);
             foreach ($this->_contact as $key => $value) {
                 if (in_array($key, $objects)) {
                     $ids[substr($key, 0, strlen($key) - 3)] = $value;
                 } else {
                     if (is_array($value)) {
                         //fixed for CRM-665
                         if ($nameValue[1] == $value['location_type_id']) {
                             $locations[$value['location_type_id']] = 1;
                             $loc_no = count($locations);
                             if ($nameValue[0] == 'phone') {
                                 $phoneLoc++;
                                 if (isset($nameValue[2])) {
                                     $ids['location'][$loc_no]['phone'][$phoneLoc] = $value['phone'][$nameValue[2] . '_id'];
                                 } else {
                                     $ids['location'][$loc_no]['phone'][$phoneLoc] = $value['phone']['1_id'];
                                 }
                             } else {
                                 if ($nameValue[0] == 'im') {
                                     $ids['location'][$loc_no]['im'][1] = $value['im']['1_id'];
                                 } else {
                                     if ($nameValue[0] == 'email') {
                                         $ids['location'][$loc_no]['email'][1] = $value['email']['1_id'];
                                     } else {
                                         $ids['location'][$loc_no]['address'] = $value['address_id'];
                                     }
                                 }
                             }
                             $ids['location'][$loc_no]['id'] = $value['location_id'];
                         }
                     }
                 }
             }
         }
     }
     //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone)
     $privacy = CRM_Core_SelectValues::privacy();
     foreach ($privacy as $key => $value) {
         if (array_key_exists($key, $this->_fields)) {
             if ($params[$key]) {
                 $data[$key] = $params[$key];
             } else {
                 $data[$key] = 0;
             }
         }
     }
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $this->_fields)) {
         $wasOptOut = $this->_contact['is_opt_out'] ? true : false;
         $isOptOut = $params['is_opt_out'] ? true : false;
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut) {
             $shParams = array('contact_id' => $this->_contact['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     if ($this->_mode == CRM_PROFILE_FORM_MODE_REGISTER) {
         require_once 'CRM/Core/BAO/Address.php';
         CRM_Core_BAO_Address::setOverwrite(false);
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     $contact = CRM_Contact_BAO_Contact::create($data, $ids, count($data['location']));
     // Process group and tag
     if (CRM_Utils_Array::value('group', $this->_fields)) {
         CRM_Contact_BAO_GroupContact::create($params['group'], $contact->id);
     }
     if (CRM_Utils_Array::value('tag', $this->_fields)) {
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], $contact->id);
     }
 }
Esempio n. 9
0
 /** 
  * takes an associative array and creates a contact object and all the associated 
  * derived objects (i.e. individual, location, email, phone etc) 
  * 
  * This function is invoked from within the web form layer and also from the api layer
  * primarily from the profile / contribute forms where we dont have a nice hierarchy
  * and are too lazy to create one. This function should be obsoleted at some time
  * 
  * @param array $params (reference ) an assoc array of name/value pairs 
  * @param array $ids    the array that holds all the db ids 
  * 
  * @return object CRM_Contact_BAO_Contact object  
  * @access public 
  * @static 
  */
 function &createFlat(&$params, &$ids)
 {
     require_once 'CRM/Utils/Hook.php';
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::pre('edit', 'Individual', $ids['contact'], $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Individual', null, $params);
     }
     CRM_Core_DAO::transaction('BEGIN');
     $params['contact_type'] = 'Individual';
     $contact = CRM_Contact_BAO_Contact::add($params, $ids);
     $params['contact_id'] = $contact->id;
     require_once 'CRM/Contact/BAO/Individual.php';
     CRM_Contact_BAO_Individual::add($params, $ids);
     require_once 'CRM/Core/BAO/LocationType.php';
     $locationType =& CRM_Core_BAO_LocationType::getDefault();
     $locationTypeId = $locationType->id;
     $locationIds = CRM_Utils_Array::value('location', $ids);
     // extract the first location id
     if ($locationIds) {
         foreach ($locationIds as $dontCare => $locationId) {
             $locationIds = $locationId;
             break;
         }
     }
     $location =& new CRM_Core_DAO_Location();
     $location->location_type_id = $locationTypeId;
     $location->entity_table = 'civicrm_contact';
     $location->entity_id = $contact->id;
     $location->id = CRM_Utils_Array::value('id', $locationIds);
     if ($location->find(true)) {
         if (!$location->is_primary) {
             $location->is_primary = true;
         }
     } else {
         $location->is_primary = true;
     }
     $location->save();
     $address =& new CRM_Core_BAO_Address();
     CRM_Core_BAO_Address::fixAddress($params);
     if (!$address->copyValues($params)) {
         $address->id = CRM_Utils_Array::value('address', $locationIds);
         $address->location_id = $location->id;
         $address->save();
     }
     $phone =& new CRM_Core_BAO_Phone();
     if (!$phone->copyValues($params)) {
         $blockIds = CRM_Utils_Array::value('phone', $locationIds);
         $phone->id = CRM_Utils_Array::value(1, $blockIds);
         $phone->location_id = $location->id;
         $phone->is_primary = true;
         $phone->save();
     }
     $email =& new CRM_Core_BAO_Email();
     if (!$email->copyValues($params)) {
         $blockIds = CRM_Utils_Array::value('email', $locationIds);
         $email->id = CRM_Utils_Array::value(1, $blockIds);
         $email->location_id = $location->id;
         $email->is_primary = true;
         $email->save();
     }
     /* Process custom field values and other values */
     foreach ($params as $key => $value) {
         if ($key == 'group') {
             CRM_Contact_BAO_GroupContact::create($params['group'], $contact->id);
         } else {
             if ($key == 'tag') {
                 require_once 'CRM/Core/BAO/EntityTag.php';
                 CRM_Core_BAO_EntityTag::create($params['tag'], $contact->id);
             } else {
                 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($key)) {
                     $custom_field_id = $cfID;
                     $cf =& new CRM_Core_BAO_CustomField();
                     $cf->id = $custom_field_id;
                     if ($cf->find(true)) {
                         switch ($cf->html_type) {
                             case 'Select Date':
                                 $date = CRM_Utils_Date::format($value);
                                 if (!$date) {
                                     $date = '';
                                 }
                                 $customValue = $date;
                                 break;
                             case 'CheckBox':
                                 $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 break;
                                 //added a case for Multi-Select
                             //added a case for Multi-Select
                             case 'Multi-Select':
                                 $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 break;
                             default:
                                 $customValue = $value;
                         }
                     }
                     CRM_Core_BAO_CustomValue::updateValue($contact->id, $custom_field_id, $customValue);
                 }
             }
         }
     }
     CRM_Core_DAO::transaction('COMMIT');
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::post('edit', 'Individual', $contact->id, $contact);
     } else {
         CRM_Utils_Hook::post('create', 'Individual', $contact->id, $contact);
     }
     return $contact;
 }