/** * 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 ($subType = CRM_Utils_Array::value('contact_sub_type_hidden', $params)) { // if profile was used, and had any subtype, we obtain it from there $data['contact_sub_type'] = $subType; } } if ($ctype == 'Organization') { $data['organization_name'] = CRM_Utils_Array::value('organization_name', $contactDetails); } else { if ($ctype == 'Household') { $data['household_name'] = CRM_Utils_Array::value('household_name', $contactDetails); } } $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'); $multiplFields = array('url'); // 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) && !in_array($fieldName, $multiplFields)) { $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') { $data['openid'][$loc]['openid'] = $value; } 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 { if (substr($fieldName, 0, 14) === 'address_custom') { $data['address'][$loc][substr($fieldName, 8)] = $value; } else { $data['address'][$loc][$fieldName] = $value; } } } } } } } } } } else { if (substr($key, 0, 4) === 'url-') { list($url, $cnt, $websiteTypeId) = explode('-', $key); if ($websiteTypeId) { $data['website'][$cnt]['website_type_id'] = $value; } else { $data['website'][$cnt]['url'] = $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 ($params[$key] && isset($params[$key . '_id'])) { $value = $params[$key . '_id']; } // we need to append time with date if ($params[$key] && isset($params[$key . '_time'])) { $value .= ' ' . $params[$key . '_time']; } $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; } } } } } } } } } } } 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 (CRM_Utils_Array::value($key, $params)) { $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'], '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); } } 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; }
/** * 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; }