/** * Unsubscribe a contact from the domain * * @param int $job_id The job ID * @param int $queue_id The Queue Event ID of the recipient * @param string $hash The hash * * @return boolean Was the contact succesfully unsubscribed? * @access public * @static */ public static function unsub_from_domain($job_id, $queue_id, $hash) { $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { return FALSE; } $transaction = new CRM_Core_Transaction(); $now = date('YmdHis'); if (CRM_Core_BAO_Email::isMultipleBulkMail()) { $email = new CRM_Core_BAO_Email(); $email->id = $q->email_id; if ($email->find(TRUE)) { $sql = "\nUPDATE civicrm_email\nSET on_hold = 2,\n hold_date = %1\nWHERE email = %2\n"; $sqlParams = array(1 => array($now, 'Timestamp'), 2 => array($email->email, 'String')); CRM_Core_DAO::executeQuery($sql, $sqlParams); } } else { $contact = new CRM_Contact_BAO_Contact(); $contact->id = $q->contact_id; $contact->is_opt_out = TRUE; $contact->save(); } $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 1; $ue->time_stamp = $now; $ue->save(); $shParams = array('contact_id' => $q->contact_id, 'group_id' => NULL, 'status' => 'Removed', 'method' => 'Email', 'tracking' => $ue->id); CRM_Contact_BAO_SubscriptionHistory::create($shParams); $transaction->commit(); return TRUE; }
/** * Unsubscribe a contact from the domain * * @param int $job_id The job ID * @param int $queue_id The Queue Event ID of the recipient * @param string $hash The hash * @return boolean Was the contact succesfully unsubscribed? * @access public * @static */ function unsub_from_domain($job_id, $queue_id, $hash) { $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { return false; } CRM_Core_DAO::transaction('BEGIN'); $contact =& new CRM_Contact_BAO_Contact(); $contact->id = $q->contact_id; $contact->is_opt_out = true; $contact->save(); $ue =& new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 1; $ue->time_stamp = date('YmdHis'); $ue->save(); $shParams = array('contact_id' => $q->contact_id, 'group_id' => null, 'status' => 'Removed', 'method' => 'Email', 'tracking' => $ue->id); CRM_Contact_BAO_SubscriptionHistory::create($shParams); CRM_Core_DAO::transaction('COMMIT'); return true; }
/** * Unsubscribe a contact from the domain * * @param int $job_id The job ID * @param int $queue_id The Queue Event ID of the recipient * @param string $hash The hash * @return boolean Was the contact succesfully unsubscribed? * @access public * @static */ public static function unsub_from_domain($job_id, $queue_id, $hash) { $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { return false; } require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); $contact = new CRM_Contact_BAO_Contact(); $contact->id = $q->contact_id; $contact->is_opt_out = true; $contact->save(); $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 1; $ue->time_stamp = date('YmdHis'); $ue->save(); $shParams = array('contact_id' => $q->contact_id, 'group_id' => null, 'status' => 'Removed', 'method' => 'Email', 'tracking' => $ue->id); CRM_Contact_BAO_SubscriptionHistory::create($shParams); $transaction->commit(); return true; }
/** * Given an array of contact ids, remove all the contacts from the group * * @param array $contactIds * (reference ) the array of contact ids to be removed. * @param int $groupId * The id of the group. * * @param string $method * @param string $status * @param NULL $tracking * * @return array * (total, removed, notRemoved) count of contacts removed to group */ public static function removeContactsFromGroup(&$contactIds, $groupId, $method = 'Admin', $status = 'Removed', $tracking = NULL) { if (!is_array($contactIds)) { return array(0, 0, 0); } if ($status == 'Removed' || $status == 'Deleted') { $op = 'delete'; } else { $op = 'edit'; } CRM_Utils_Hook::pre($op, 'GroupContact', $groupId, $contactIds); $date = date('YmdHis'); $numContactsRemoved = 0; $numContactsNotRemoved = 0; $group = new CRM_Contact_DAO_Group(); $group->id = $groupId; $group->find(TRUE); foreach ($contactIds as $contactId) { if ($status == 'Deleted') { $query = "DELETE FROM civicrm_group_contact WHERE contact_id={$contactId} AND group_id={$groupId}"; $dao = CRM_Core_DAO::executeQuery($query); $historyParams = array('group_id' => $groupId, 'contact_id' => $contactId, 'status' => $status, 'method' => $method, 'date' => $date, 'tracking' => $tracking); CRM_Contact_BAO_SubscriptionHistory::create($historyParams); } else { $groupContact = new CRM_Contact_DAO_GroupContact(); $groupContact->group_id = $groupId; $groupContact->contact_id = $contactId; // check if the selected contact id already a member, or if this is // an opt-out of a smart group. // if not a member remove to groupContact else keep the count of contacts that are not removed if ($groupContact->find(TRUE) || $group->saved_search_id) { // remove the contact from the group $numContactsRemoved++; } else { $numContactsNotRemoved++; } //now we grant the negative membership to contact if not member. CRM-3711 $historyParams = array('group_id' => $groupId, 'contact_id' => $contactId, 'status' => $status, 'method' => $method, 'date' => $date, 'tracking' => $tracking); CRM_Contact_BAO_SubscriptionHistory::create($historyParams); $groupContact->status = $status; $groupContact->save(); } } // also reset the acl cache $config = CRM_Core_Config::singleton(); if (!$config->doNotResetCache) { CRM_ACL_BAO_Cache::resetCache(); } // reset the group contact cache for all group(s) // if this group is being used as a smart group CRM_Contact_BAO_GroupContactCache::remove(); CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds); return array(count($contactIds), $numContactsRemoved, $numContactsNotRemoved); }
/** * Method to update the Status of Group member form 'Pending' to 'Added' * * @param int $contactId id of the contact * * @param int $groupID Id of a perticuler group * * @param mixed $tracking tracking information for history * * @return null If success * @access public * @static */ function updateGroupMembershipStatus($contactId, $groupID, $method = 'Email', $tracking = null) { if (!isset($contactId) && !isset($groupID)) { return CRM_Core_Error::fatal("{$contactId} or {$groupID} should not empty"); } $query = "UPDATE civicrm_group_contact \nSET civicrm_group_contact.status = 'Added'\nWHERE civicrm_group_contact.contact_id = %1\nAND civicrm_group_contact.group_id = %2"; $params = array(1 => array($contactId, 'Integer'), 2 => array($groupID, 'Integer')); $dao =& CRM_Core_DAO::executeQuery($query, $params); $params = array('contact_id' => $contactId, 'group_id' => $groupID, 'status' => 'Added', 'method' => $method, 'tracking' => $tracking); CRM_Contact_BAO_SubscriptionHistory::create($params); return null; }
/** * 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; }
/** * 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; }
/** * Given an array of contact ids, remove all the contacts from the group * * @param array $contactIds * (reference ) the array of contact ids to be removed. * @param int $groupId * The id of the group. * * @param string $method * @param string $status * @param NULL $tracking * * @return array * (total, removed, notRemoved) count of contacts removed to group */ public static function removeContactsFromGroup(&$contactIds, $groupId, $method = 'Admin', $status = 'Removed', $tracking = NULL) { if (!is_array($contactIds)) { return array(0, 0, 0); } if ($status == 'Removed' || $status == 'Deleted') { $op = 'delete'; } else { $op = 'edit'; } CRM_Utils_Hook::pre($op, 'GroupContact', $groupId, $contactIds); $date = date('YmdHis'); $numContactsRemoved = 0; $numContactsNotRemoved = 0; $group = new CRM_Contact_DAO_Group(); $group->id = $groupId; $group->find(TRUE); foreach ($contactIds as $contactId) { if ($status == 'Deleted') { $query = "DELETE FROM civicrm_group_contact WHERE contact_id={$contactId} AND group_id={$groupId}"; $dao = CRM_Core_DAO::executeQuery($query); $historyParams = array('group_id' => $groupId, 'contact_id' => $contactId, 'status' => $status, 'method' => $method, 'date' => $date, 'tracking' => $tracking); CRM_Contact_BAO_SubscriptionHistory::create($historyParams); } else { $groupContact = new CRM_Contact_DAO_GroupContact(); $groupContact->group_id = $groupId; $groupContact->contact_id = $contactId; // check if the selected contact id already a member, or if this is // an opt-out of a smart group. // if not a member remove to groupContact else keep the count of contacts that are not removed if ($groupContact->find(TRUE) || $group->saved_search_id) { // remove the contact from the group $numContactsRemoved++; } else { $numContactsNotRemoved++; } //now we grant the negative membership to contact if not member. CRM-3711 $historyParams = array('group_id' => $groupId, 'contact_id' => $contactId, 'status' => $status, 'method' => $method, 'date' => $date, 'tracking' => $tracking); CRM_Contact_BAO_SubscriptionHistory::create($historyParams); $groupContact->status = $status; $groupContact->save(); } } // also reset the acl cache $config = CRM_Core_Config::singleton(); if (!$config->doNotResetCache) { CRM_ACL_BAO_Cache::resetCache(); } // reset the group contact cache for all group(s) // if this group is being used as a smart group // @todo consider what to do here - it feels like we should either // 1) just invalidate the specific group's cache(& perhaps any parents) & let cron do it's thing or // possibly clear this specific groups cache, or just call opportunisticCacheFlush() - which would have the // same effect as the remove call. The reservation about that is that it is no more aggressive for the group that // we know is altered than for all the others, or perhaps, more the point with it's parents & groups that use it in // their criteria. CRM_Contact_BAO_GroupContactCache::remove(); CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds); return array(count($contactIds), $numContactsRemoved, $numContactsNotRemoved); }
/** * 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; }
/** * 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; }
/** * 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); } }
/** * 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 * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * @param int $maxLocationBlocks the maximum number of location blocks to process * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ function &create(&$params, &$ids, $maxLocationBlocks) { require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('contact', $ids)) { CRM_Utils_Hook::pre('edit', $params['contact_type'], $ids['contact'], $params); } else { CRM_Utils_Hook::pre('create', $params['contact_type'], null, $params); } CRM_Core_DAO::transaction('BEGIN'); $contact = CRM_Contact_BAO_Contact::add($params, $ids); $params['contact_id'] = $contact->id; // invoke the add operator on the contact_type class require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $params['contact_type']) . ".php"; eval('$contact->contact_type_object =& CRM_Contact_BAO_' . $params['contact_type'] . '::add($params, $ids);'); $location = array(); for ($locationId = 1; $locationId <= $maxLocationBlocks; $locationId++) { // start of for loop for location $location[$locationId] = CRM_Core_BAO_Location::add($params, $ids, $locationId); } $contact->location = $location; // add notes if (CRM_Utils_Array::value('note', $params)) { if (is_array($params['note'])) { foreach ($params['note'] as $note) { $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $note['note']); CRM_Core_BAO_Note::add($noteParams); } } else { $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $params['note']); CRM_Core_BAO_Note::add($noteParams); } } // update the UF email if that has changed require_once 'CRM/Core/BAO/UFMatch.php'; CRM_Core_BAO_UFMatch::updateUFEmail($contact->id); // add custom field values if (CRM_Utils_Array::value('custom', $params)) { foreach ($params['custom'] as $customValue) { $cvParams = array('entity_table' => 'civicrm_contact', 'entity_id' => $contact->id, 'value' => $customValue['value'], 'type' => $customValue['type'], 'custom_field_id' => $customValue['custom_field_id']); if ($customValue['id']) { $cvParams['id'] = $customValue['id']; } CRM_Core_BAO_CustomValue::create($cvParams); } } // make a civicrm_subscription_history entry only on contact create (CRM-777) if (!CRM_Utils_Array::value('contact', $ids)) { $subscriptionParams = array('contact_id' => $contact->id, 'status' => 'Added', 'method' => 'Admin'); CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams); } CRM_Core_DAO::transaction('COMMIT'); if (CRM_Utils_Array::value('contact', $ids)) { CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact); } else { CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact); } $contact->contact_type_display = CRM_Contact_DAO_Contact::tsEnum('contact_type', $contact->contact_type); return $contact; }