/** * Membership processing section. * * This is in a separate function as part of a move towards refactoring. * * @param int $contactID * @param array $membershipParams * @param array $premiumParams * @param bool $isPayLater */ protected function doMembershipProcessing($contactID, $membershipParams, $premiumParams, $isPayLater) { // This could be set by a hook. if (!empty($this->_params['installments'])) { $membershipParams['installments'] = $this->_params['installments']; } // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups if (isset($this->_params['related_contact'])) { $membershipParams['cms_contactID'] = $this->_params['related_contact']; } else { $membershipParams['cms_contactID'] = $contactID; } if (!empty($membershipParams['onbehalf']) && is_array($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) { $this->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; } $customFieldsFormatted = $fieldTypes = array(); if (!empty($membershipParams['onbehalf']) && is_array($membershipParams['onbehalf'])) { foreach ($membershipParams['onbehalf'] as $key => $value) { if (strstr($key, 'custom_')) { $customFieldId = explode('_', $key); CRM_Core_BAO_CustomField::formatCustomField($customFieldId[1], $customFieldsFormatted, $value, 'Membership', NULL, $contactID); } } $fieldTypes = array('Contact', 'Organization', 'Membership'); } $priceFieldIds = $this->get('memberPriceFieldIDS'); if (!empty($priceFieldIds)) { $contributionTypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceFieldIds['id'], 'financial_type_id'); unset($priceFieldIds['id']); $membershipTypeIds = array(); $membershipTypeTerms = array(); foreach ($priceFieldIds as $priceFieldId) { if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) { $membershipTypeIds[] = $id; //@todo the value for $term is immediately overwritten. It is unclear from the code whether it was intentional to // do this or a double = was intended (this ambiguity is the reason many IDEs complain about 'assignment in condition' $term = 1; if ($term = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_num_terms')) { $membershipTypeTerms[$id] = $term > 1 ? $term : 1; } else { $membershipTypeTerms[$id] = 1; } } } $membershipParams['selectMembership'] = $membershipTypeIds; $membershipParams['financial_type_id'] = $contributionTypeID; $membershipParams['types_terms'] = $membershipTypeTerms; } if (!empty($membershipParams['selectMembership'])) { // CRM-12233 $membershipLineItems = array(); if ($this->_separateMembershipPayment && $this->_values['amount_block_is_active']) { foreach ($this->_values['fee'] as $key => $feeValues) { if ($feeValues['name'] == 'membership_amount') { $fieldId = $this->_params['price_' . $key]; $membershipLineItems[$this->_priceSetId][$fieldId] = $this->_lineItem[$this->_priceSetId][$fieldId]; unset($this->_lineItem[$this->_priceSetId][$fieldId]); break; } } } try { $this->processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, $membershipLineItems, $isPayLater); } catch (CRM_Core_Exception $e) { CRM_Core_Session::singleton()->setStatus($e->getMessage()); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=true&qfKey={$this->_params['qfKey']}")); } if (!$this->_amount > 0.0 || !$membershipParams['amount']) { // we need to explicitly create a CMS user in case of free memberships // since it is done under processConfirm for paid memberships CRM_Contribute_BAO_Contribution_Utils::createCMSUser($membershipParams, $membershipParams['cms_contactID'], 'email-' . $this->_bltID); } } }
function postProcess(&$params, &$customFields, $entityID, $customFieldExtends, $inline = false) { $customData = array(); foreach ($params as $key => $value) { if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, true)) { // 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']; } CRM_Core_BAO_CustomField::formatCustomField($customFieldInfo[0], $customData, $value, $customFieldExtends, $customFieldInfo[1], $entityID, $inline); } } return $customData; }
/** * Process the form * * @return void * @access public */ public function postProcess() { $config = CRM_Core_Config::singleton(); $contactID = $this->getContactID(); // add a description field at the very beginning $this->_params['description'] = ts('Online Contribution') . ': ' . ($this->_pcpInfo['title'] ? $this->_pcpInfo['title'] : $this->_values['title']); // also add accounting code $this->_params['accountingCode'] = CRM_Utils_Array::value('accountingCode', $this->_values); // fix currency ID $this->_params['currencyID'] = $config->defaultCurrency; $premiumParams = $membershipParams = $tempParams = $params = $this->_params; //carry payment processor id. if ($paymentProcessorId = CRM_Utils_Array::value('id', $this->_paymentProcessor)) { $this->_params['payment_processor_id'] = $paymentProcessorId; foreach (array('premiumParams', 'membershipParams', 'tempParams', 'params') as $p) { ${$p}['payment_processor_id'] = $paymentProcessorId; } } $fields = array(); if (!empty($params['image_URL'])) { CRM_Contact_BAO_Contact::processImageParams($params); } // set email for primary location. $fields['email-Primary'] = 1; // get the add to groups $addToGroups = array(); // now set the values for the billing location. foreach ($this->_fields as $name => $value) { $fields[$name] = 1; // get the add to groups for uf fields if (!empty($value['add_to_group_id'])) { $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id']; } } if (!array_key_exists('first_name', $fields)) { $nameFields = array('first_name', 'middle_name', 'last_name'); foreach ($nameFields as $name) { $fields[$name] = 1; if (array_key_exists("billing_{$name}", $params)) { $params[$name] = $params["billing_{$name}"]; $params['preserveDBName'] = TRUE; } } } // billing email address $fields["email-{$this->_bltID}"] = 1; //unset the billing parameters if it is pay later mode //to avoid creation of billing location if ($params['is_pay_later']) { $billingFields = array('billing_first_name', 'billing_middle_name', 'billing_last_name', "billing_street_address-{$this->_bltID}", "billing_city-{$this->_bltID}", "billing_state_province-{$this->_bltID}", "billing_state_province_id-{$this->_bltID}", "billing_postal_code-{$this->_bltID}", "billing_country-{$this->_bltID}", "billing_country_id-{$this->_bltID}"); foreach ($billingFields as $value) { unset($params[$value]); unset($fields[$value]); } } // if onbehalf-of-organization contribution, take out // organization params in a separate variable, to make sure // normal behavior is continued. And use that variable to // process on-behalf-of functionality. if (!empty($this->_params['hidden_onbehalf_profile'])) { $behalfOrganization = array(); $orgFields = array('organization_name', 'organization_id', 'org_option'); foreach ($orgFields as $fld) { if (array_key_exists($fld, $params)) { $behalfOrganization[$fld] = $params[$fld]; unset($params[$fld]); } } if (is_array($params['onbehalf']) && !empty($params['onbehalf'])) { foreach ($params['onbehalf'] as $fld => $values) { if (strstr($fld, 'custom_')) { $behalfOrganization[$fld] = $values; } elseif (!strstr($fld, '-')) { if (in_array($fld, array('contribution_campaign_id', 'member_campaign_id'))) { $fld = 'campaign_id'; } else { $behalfOrganization[$fld] = $values; } $this->_params[$fld] = $values; } } } if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) { foreach ($params['onbehalf_location'] as $block => $vals) { //fix for custom data (of type checkbox, multi-select) if (substr($block, 0, 7) == 'custom_') { continue; } // fix the index of block elements if (is_array($vals)) { foreach ($vals as $key => $val) { //dont adjust the index of address block as //it's index is WRT to location type $newKey = $block == 'address' ? $key : ++$key; $behalfOrganization[$block][$newKey] = $val; } } } unset($params['onbehalf_location']); } if (!empty($params['onbehalf[image_URL]'])) { $behalfOrganization['image_URL'] = $params['onbehalf[image_URL]']; } } // check for profile double opt-in and get groups to be subscribed $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID); // since we are directly adding contact to group lets unset it from mailing if (!empty($addToGroups)) { foreach ($addToGroups as $groupId) { if (isset($subscribeGroupIds[$groupId])) { unset($subscribeGroupIds[$groupId]); } } } foreach ($addToGroups as $k) { if (array_key_exists($k, $subscribeGroupIds)) { unset($addToGroups[$k]); } } if (empty($contactID)) { $dupeParams = $params; if (!empty($dupeParams['onbehalf'])) { unset($dupeParams['onbehalf']); } $dedupeParams = CRM_Dedupe_Finder::formatParams($dupeParams, 'Individual'); $dedupeParams['check_permission'] = FALSE; $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); // if we find more than one contact, use the first one $contact_id = CRM_Utils_Array::value(0, $ids); // Fetch default greeting id's if creating a contact if (!$contact_id) { foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { if (!isset($params[$greeting])) { $params[$greeting] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting); } } } $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contact_id, $addToGroups, NULL, NULL, TRUE); } else { $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type'); $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contactID, $addToGroups, NULL, $ctype, TRUE); } // Make the contact ID associated with the contribution available at the Class level. // Also make available to the session. //@todo consider handling this in $this->getContactID(); $this->set('contactID', $contactID); $this->_contactID = $contactID; //get email primary first if exist $subscribtionEmail = array('email' => CRM_Utils_Array::value('email-Primary', $params)); if (!$subscribtionEmail['email']) { $subscribtionEmail['email'] = CRM_Utils_Array::value("email-{$this->_bltID}", $params); } // subscribing contact to groups if (!empty($subscribeGroupIds) && $subscribtionEmail['email']) { CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscribtionEmail, $contactID); } // If onbehalf-of-organization contribution / signup, add organization // and it's location. if (isset($params['hidden_onbehalf_profile']) && isset($behalfOrganization['organization_name'])) { $ufFields = array(); foreach ($this->_fields['onbehalf'] as $name => $value) { $ufFields[$name] = 1; } self::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values, $this->_params, $ufFields); } else { if (!empty($this->_membershipContactID) && $contactID != $this->_membershipContactID) { // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household, // store current user id as related contact for later use for mailing / activity.. $this->_values['related_contact'] = $contactID; $this->_params['related_contact'] = $contactID; // swap contact like we do for on-behalf-org case, so parent/primary membership is affected $contactID = $this->_membershipContactID; } } // lets store the contactID in the session // for things like tell a friend $session = CRM_Core_Session::singleton(); if (!$session->get('userID')) { $session->set('transaction.userID', $contactID); } else { $session->set('transaction.userID', NULL); } $this->_useForMember = $this->get('useForMember'); // store the fact that this is a membership and membership type is selected $processMembership = FALSE; if (!empty($membershipParams['selectMembership']) && $membershipParams['selectMembership'] != 'no_thanks' || $this->_useForMember) { $processMembership = TRUE; if (!$this->_useForMember) { $this->assign('membership_assign', TRUE); $this->set('membershipTypeID', $this->_params['selectMembership']); } if ($this->_action & CRM_Core_Action::PREVIEW) { $membershipParams['is_test'] = 1; } if ($this->_params['is_pay_later']) { $membershipParams['is_pay_later'] = 1; } } if ($processMembership) { CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $membershipParams, TRUE); // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups if (isset($this->_params['related_contact'])) { $membershipParams['cms_contactID'] = $this->_params['related_contact']; } else { $membershipParams['cms_contactID'] = $contactID; } //inherit campaign from contirb page. if (!array_key_exists('campaign_id', $membershipParams)) { $membershipParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values); } if (!empty($membershipParams['onbehalf']) && is_array($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) { $this->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; } $customFieldsFormatted = $fieldTypes = array(); if (!empty($membershipParams['onbehalf']) && is_array($membershipParams['onbehalf'])) { foreach ($membershipParams['onbehalf'] as $key => $value) { if (strstr($key, 'custom_')) { $customFieldId = explode('_', $key); CRM_Core_BAO_CustomField::formatCustomField($customFieldId[1], $customFieldsFormatted, $value, 'Membership', NULL, $contactID); } } $fieldTypes = array('Contact', 'Organization', 'Membership'); } $priceFieldIds = $this->get('memberPriceFieldIDS'); if (!empty($priceFieldIds)) { $contributionTypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceFieldIds['id'], 'financial_type_id'); unset($priceFieldIds['id']); $membershipTypeIds = array(); $membershipTypeTerms = array(); foreach ($priceFieldIds as $priceFieldId) { if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) { $membershipTypeIds[] = $id; $term = 1; if ($term = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_num_terms')) { $membershipTypeTerms[$id] = $term > 1 ? $term : 1; } else { $membershipTypeTerms[$id] = 1; } } } $membershipParams['selectMembership'] = $membershipTypeIds; $membershipParams['financial_type_id'] = $contributionTypeID; $membershipParams['types_terms'] = $membershipTypeTerms; } if (!empty($membershipParams['selectMembership'])) { // CRM-12233 if ($this->_separateMembershipPayment && $this->_values['amount_block_is_active']) { foreach ($this->_values['fee'] as $key => $feeValues) { if ($feeValues['name'] == 'membership_amount') { $fieldId = $this->_params['price_' . $key]; $this->_memLineItem[$this->_priceSetId][$fieldId] = $this->_lineItem[$this->_priceSetId][$fieldId]; unset($this->_lineItem[$this->_priceSetId][$fieldId]); break; } } } CRM_Member_BAO_Membership::postProcessMembership($membershipParams, $contactID, $this, $premiumParams, $customFieldsFormatted, $fieldTypes); } } else { // at this point we've created a contact and stored its address etc // all the payment processors expect the name and address to be in the // so we copy stuff over to first_name etc. $paymentParams = $this->_params; $contributionTypeId = $this->_values['financial_type_id']; $fieldTypes = array(); if (!empty($paymentParams['onbehalf']) && is_array($paymentParams['onbehalf'])) { foreach ($paymentParams['onbehalf'] as $key => $value) { if (strstr($key, 'custom_')) { $this->_params[$key] = $value; } } $fieldTypes = array('Contact', 'Organization', 'Contribution'); } CRM_Contribute_BAO_Contribution_Utils::processConfirm($this, $paymentParams, $premiumParams, $contactID, $contributionTypeId, 'contribution', $fieldTypes); } }
/** * Format profile contact parameters. * * @param array $params * @param $fields * @param int $contactID * @param int $ufGroupId * @param null $ctype * @param bool $skipCustom * * @return array */ public static function formatProfileContactParams(&$params, &$fields, $contactID = NULL, $ufGroupId = NULL, $ctype = NULL, $skipCustom = FALSE) { $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); $data['contact_sub_type'] = CRM_Utils_Array::value('contact_sub_type', $contactDetails); } else { //we should get contact type only if contact if ($ufGroupId) { $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'; } elseif (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) { $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']); } } elseif ($ctype) { $data['contact_type'] = $ctype; } else { $data['contact_type'] = 'Individual'; } } //fix contact sub type CRM-5125 if (array_key_exists('contact_sub_type', $params) && !empty($params['contact_sub_type'])) { $data['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $params['contact_sub_type']) . CRM_Core_DAO::VALUE_SEPARATOR; } elseif (array_key_exists('contact_sub_type_hidden', $params) && !empty($params['contact_sub_type_hidden'])) { // if profile was used, and had any subtype, we obtain it from there $data['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $params['contact_sub_type_hidden']) . CRM_Core_DAO::VALUE_SEPARATOR; } if ($ctype == 'Organization') { $data['organization_name'] = CRM_Utils_Array::value('organization_name', $contactDetails); } elseif ($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 { $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); $defaultLocationId = $defaultLocation->id; } // get the billing location type $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); $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; $session = CRM_Core_Session::singleton(); 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) { if (in_array($fieldName, $blocks)) { $locTypeId = self::getPrimaryLocationType($contactID, FALSE, $fieldName); } else { $locTypeId = self::getPrimaryLocationType($contactID, FALSE, 'address'); } $primaryLocationType = $locTypeId; } else { $locTypeId = $defaultLocationId; } } if (is_numeric($locTypeId) && !in_array($fieldName, $multiplFields) && substr($fieldName, 0, 7) != 'custom_') { $index = $locTypeId; if (is_numeric($typeId)) { $index .= '-' . $typeId; } if (!in_array($index, $locationType)) { $locationType[$count] = $index; $count++; } $loc = CRM_Utils_Array::key($index, $locationType); $blockName = in_array($fieldName, $blocks) ? $fieldName : 'address'; $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; } } elseif ($locTypeId == $defaultLocationId) { $data[$blockName][$loc]['is_primary'] = 1; } if (in_array($fieldName, array('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']); } } elseif ($fieldName == 'phone_ext') { $data['phone'][$loc]['phone_ext'] = $value; } elseif ($fieldName == 'email') { $data['email'][$loc]['email'] = $value; } elseif ($fieldName == 'im') { if (isset($params[$key . '-provider_id'])) { $data['im'][$loc]['provider_id'] = $params[$key . '-provider_id']; } if (strpos($key, '-provider_id') !== FALSE) { $data['im'][$loc]['provider_id'] = $params[$key]; } else { $data['im'][$loc]['name'] = $value; } } elseif ($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; } elseif (empty($value)) { $data['address'][$loc]['state_province_id'] = ''; } else { $data['address'][$loc]['state_province'] = $value; } } elseif ($fieldName === 'country') { // CRM-3393 if (is_numeric($value) && (int) $value >= 1000) { $data['address'][$loc]['country_id'] = $value; } elseif (empty($value)) { $data['address'][$loc]['country_id'] = ''; } else { $data['address'][$loc]['country'] = $value; } } elseif ($fieldName === 'county') { $data['address'][$loc]['county_id'] = $value; } elseif ($fieldName == 'address_name') { $data['address'][$loc]['name'] = $value; } elseif (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-') { $websiteField = explode('-', $key); $data['website'][$websiteField[1]]['website_type_id'] = $websiteField[1]; $data['website'][$websiteField[1]]['url'] = $value; } elseif (in_array($key, self::$_greetingTypes, TRUE)) { //save email/postal greeting and addressee values if any, CRM-4575 $data[$key . '_id'] = $value; } elseif (!$skipCustom && ($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']; } // if auth source is not checksum / login && $value is blank, do not proceed - CRM-10128 if (($session->get('authSrc') & CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN) == 0 && ($value == '' || !isset($value))) { continue; } $valueId = NULL; if (!empty($params['customRecordValues'])) { if (is_array($params['customRecordValues']) && !empty($params['customRecordValues'])) { foreach ($params['customRecordValues'] as $recId => $customFields) { if (is_array($customFields) && !empty($customFields)) { foreach ($customFields as $customFieldName) { if ($customFieldName == $key) { $valueId = $recId; break; } } } } } } $type = $data['contact_type']; if (!empty($data['contact_sub_type'])) { $type = $data['contact_sub_type']; $type = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($type, CRM_Core_DAO::VALUE_SEPARATOR)); // generally a contact even if, has multiple subtypes the parent-type is going to be one only // and since formatCustomField() would be interested in parent type, lets consider only one subtype // as the results going to be same. $type = $type[0]; } CRM_Core_BAO_CustomField::formatCustomField($customFieldId, $data['custom'], $value, $type, $valueId, $contactID); } elseif ($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']; } } } } if ($key == 'phone' && isset($params['phone_ext'])) { $data[$key] = $value; foreach ($value as $cnt => $phoneBlock) { if ($params[$key][$cnt]['location_type_id'] == $params['phone_ext'][$cnt]['location_type_id']) { $data[$key][$cnt]['phone_ext'] = CRM_Utils_Array::retrieveValueRecursive($params['phone_ext'][$cnt], 'phone_ext'); } } } elseif (in_array($key, array('nick_name', 'job_title', 'middle_name', 'birth_date', 'gender_id', 'current_employer', 'prefix_id', 'suffix_id')) && ($value == '' || !isset($value)) && ($session->get('authSrc') & CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN) == 0) { // CRM-10128: if auth source is not checksum / login && $value is blank, do not fill $data with empty value // to avoid update with empty values continue; } else { $data[$key] = $value; } } } } if (!isset($data['contact_type'])) { $data['contact_type'] = 'Individual'; } //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)) { // do not reset values for existing contacts, if fields are added to a profile if (array_key_exists($key, $params)) { $data[$key] = $params[$key]; if (empty($params[$key])) { $data[$key] = 0; } } elseif (!$contactID) { $data[$key] = 0; } } } return array($data, $contactDetails); }
/** * Format custom parameters. * * @param array $params * @param array $values * @param string $extends * Entity that this custom field extends (e.g. contribution, event, contact). * @param string $entityId * ID of entity per $extends. */ function _civicrm_api3_custom_format_params($params, &$values, $extends, $entityId = NULL) { $values['custom'] = array(); $checkCheckBoxField = FALSE; $entity = $extends; if (in_array($extends, array('Household', 'Individual', 'Organization'))) { $entity = 'Contact'; } $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'create')); if (!$fields['is_error']) { // not sure if fields could be error - maybe change to using civicrm_api3 wrapper later - this is conservative $fields = $fields['values']; $checkCheckBoxField = TRUE; } foreach ($params as $key => $value) { list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField::getKeyID($key, TRUE); if ($customFieldID && !is_null($value)) { if ($checkCheckBoxField && !empty($fields['custom_' . $customFieldID]) && $fields['custom_' . $customFieldID]['html_type'] == 'CheckBox') { formatCheckBoxField($value, 'custom_' . $customFieldID, $entity); } CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $values['custom'], $value, $extends, $customValueID, $entityId, FALSE, FALSE, TRUE); } } }
/** * * @param array $params * @param array $values * @param string $extends entity that this custom field extends (e.g. contribution, event, contact) * @param string $entityId ID of entity per $extends */ function _civicrm_api3_custom_format_params($params, &$values, $extends, $entityId = NULL) { $values['custom'] = array(); require_once 'CRM/Core/BAO/CustomField.php'; foreach ($params as $key => $value) { list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField::getKeyID($key, TRUE); if ($customFieldID) { CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $values['custom'], $value, $extends, $customValueID, $entityId, FALSE, FALSE); } } }
/** * * @param <type> $params * @param <type> $values * @param <type> $extends * @param <type> $entityId */ function _civicrm_custom_format_params(&$params, &$values, $extends, $entityId = null) { $values['custom'] = array(); require_once 'CRM/Core/BAO/CustomField.php'; $customFields = CRM_Core_BAO_CustomField::getFields($extends); require_once 'CRM/Core/BAO/CustomField.php'; foreach ($params as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $values['custom'], $value, $extends, null, $entityId); } } }
/** * 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; }