/** * format common params data to proper format to store. * * @param array $params contain record values. * @param array $formatted array of formatted data. * @param array $contactFields contact DAO fields. * @static */ function formatCommonData($params, &$formatted, &$contactFields) { $csType = array(CRM_Utils_Array::value('contact_type', $formatted)); //CRM-5125 //add custom fields for contact sub type if (!empty($this->_contactSubType)) { $csType = $this->_contactSubType; } if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $formatted)) { $csType = $relCsType; } $customFields = CRM_Core_BAO_CustomField::getFields($formatted['contact_type'], false, false, $csType); //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575 $elements = array('email_greeting_custom' => 'email_greeting', 'postal_greeting_custom' => 'postal_greeting', 'addressee_custom' => 'addressee'); foreach ($elements as $k => $v) { if (array_key_exists($k, $params) && !array_key_exists($v, $params)) { $label = key(CRM_Core_OptionGroup::values($v, true, null, null, 'AND v.name = "Customized"')); $params[$v] = $label; } } //format date first $session =& CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); foreach ($params as $key => $val) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { //we should not update Date to null, CRM-4062 if ($val && $customFields[$customFieldID]['data_type'] == 'Date') { self::formatCustomDate($params, $formatted, $dateType, $key); unset($params[$key]); } else { if ($customFields[$customFieldID]['data_type'] == 'Boolean') { $params[$key] = CRM_Utils_String::strtoboolstr($val); } } } if ($key == 'birth_date' && $val) { CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); } else { if ($key == 'deceased_date' && $val) { CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); } else { if ($key == 'is_deceased' && $val) { $params[$key] = CRM_Utils_String::strtoboolstr($val); } else { if ($key == 'gender') { //CRM-4360 $params[$key] = $this->checkGender($val); } } } } } //now format custom data. foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } if (is_array($field)) { foreach ($field as $value) { $break = false; if (is_array($value)) { foreach ($value as $name => $testForEmpty) { // check if $value does not contain IM provider or phoneType if (($name !== 'phone_type_id' || $name !== 'provider_id') && ($testForEmpty === '' || $testForEmpty == null)) { $break = true; break; } } } else { $break = true; } if (!$break) { _civicrm_add_formatted_param($value, $formatted); } } continue; } $formatValues = array($key => $field); if ($key !== 'preferred_communication_method' && array_key_exists($key, $contactFields)) { // due to merging of individual table and // contact table, we need to avoid // preferred_communication_method forcefully $formatValues['contact_type'] = $formatted['contact_type']; } if ($key == 'id' && isset($field)) { $formatted[$key] = $field; } _civicrm_add_formatted_param($formatValues, $formatted); //Handling Custom Data if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields)) { //get the html type. $type = $customFields[$customFieldID]['html_type']; switch ($type) { case 'CheckBox': case 'AdvMulti-Select': case 'Multi-Select': $mulValues = explode(',', $field); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); $formatted[$key] = array(); foreach ($mulValues as $v1) { foreach ($customOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($v1)) || strtolower($v2['value']) == strtolower(trim($v1))) { if ($type == 'CheckBox') { $formatted[$key][$v2['value']] = 1; } else { $formatted[$key][] = $v2['value']; } } } } break; case 'Select': case 'Radio': $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($customOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($field)) || strtolower($v2['value']) == strtolower(trim($field))) { $formatted[$key] = $v2['value']; } } break; case 'Multi-Select State/Province': $mulValues = explode(',', $field); $stateAbbr = CRM_Core_PseudoConstant::stateProvinceAbbreviation(); $stateName = CRM_Core_PseudoConstant::stateProvince(); $formatted[$key] = $stateValues = array(); foreach ($mulValues as $values) { if ($val = CRM_Utils_Array::key($values, $stateAbbr)) { $formatted[$key][] = $val; } else { if ($val = CRM_Utils_Array::key($values, $stateName)) { $formatted[$key][] = $val; } } } break; case 'Multi-Select Country': $config =& CRM_Core_Config::singleton(); $limitCodes = $config->countryLimit(); $mulValues = explode(',', $field); $formatted[$key] = array(); CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', true, 'name', 'is_active'); CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', true, 'iso_code'); foreach ($mulValues as $values) { if ($val = CRM_Utils_Array::key($values, $countryNames)) { $formatted[$key][] = $val; } else { if ($val = CRM_Utils_Array::key($values, $countryIsoCodes)) { $formatted[$key][] = $val; } else { if ($val = CRM_Utils_Array::key($values, $limitCodes)) { $formatted[$key][] = $val; } } } } break; } } } // check for primary location type, whether it is already present for the contact or not, CRM-4423 if (CRM_Utils_Array::value('id', $formatted) && isset($formatted['location'])) { $primaryLocationTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($formatted['id'], true); if (isset($primaryLocationTypeId)) { foreach ($formatted['location'] as $loc => $details) { if ($primaryLocationTypeId == CRM_Utils_Array::value('location_type_id', $details)) { $formatted['location'][$loc]['is_primary'] = 1; break; } else { $formatted['location'][$loc]['is_primary'] = 0; } } } } // parse street address, CRM-5450 if ($this->_parseStreetAddress) { require_once 'CRM/Core/BAO/Address.php'; if (array_key_exists('address', $formatted) && is_array($formatted['address'])) { foreach ($formatted['address'] as $instance => &$address) { $streetAddress = CRM_Utils_Array::value('street_address', $address); if (empty($streetAddress)) { continue; } // parse address field. $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($streetAddress); //street address consider to be parsed properly, //If we get street_name and street_number. if (!CRM_Utils_Array::value('street_name', $parsedFields) || !CRM_Utils_Array::value('street_number', $parsedFields)) { $parsedFields = array_fill_keys(array_keys($parsedFields), ''); } // merge parse address w/ main address block. $address = array_merge($address, $parsedFields); } } } }
/** * Register a subscription event. Create a new contact if one does not * already exist. * * @param int $group_id The group id to subscribe to * @param string $email The email address of the (new) contact * @params int $contactId Currently used during event registration/contribution. * Specifically to avoid linking group to wrong duplicate contact * during event registration. * @return int|null $se_id The id of the subscription event, null on failure * @access public * @static */ public static function &subscribe($group_id, $email, $contactId = null) { // CRM-1797 - allow subscription only to public groups $params = array('id' => (int) $group_id); $defaults = array(); require_once 'CRM/Contact/BAO/Group.php'; $bao = CRM_Contact_BAO_Group::retrieve($params, $defaults); if (substr($bao->visibility, 0, 6) != 'Public') { return null; } $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $email = $strtolower($email); // process the query only if no contactId if ($contactId) { $contact_id = $contactId; } else { /* First, find out if the contact already exists */ $query = "\n SELECT DISTINCT contact_a.id as contact_id \n FROM civicrm_contact contact_a \nLEFT JOIN civicrm_email ON contact_a.id = civicrm_email.contact_id\n WHERE civicrm_email.email = %1"; $params = array(1 => array($email, 'String')); $dao =& CRM_Core_DAO::executeQuery($query, $params); $id = array(); // lets just use the first contact id we got if ($dao->fetch()) { $contact_id = $dao->contact_id; } $dao->free(); } require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); if (!$contact_id) { require_once 'api/v2/Contact.php'; require_once 'CRM/Core/BAO/LocationType.php'; /* If the contact does not exist, create one. */ $formatted = array('contact_type' => 'Individual'); $locationType = CRM_Core_BAO_LocationType::getDefault(); $value = array('email' => $email, 'location_type_id' => $locationType->id); _civicrm_add_formatted_param($value, $formatted); require_once 'CRM/Import/Parser.php'; $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP; $formatted['fixAddress'] = true; $contact =& civicrm_contact_format_create($formatted); if (civicrm_error($contact, CRM_Core_Error)) { return null; } $contact_id = $contact['id']; } else { if (!is_numeric($contact_id) && (int) $contact_id > 0) { // make sure contact_id is numeric return null; } } require_once 'CRM/Core/BAO/Email.php'; require_once 'CRM/Core/BAO/Location.php'; require_once 'CRM/Contact/BAO/Contact.php'; /* Get the primary email id from the contact to use as a hash input */ $dao =& new CRM_Core_DAO(); $query = "\nSELECT civicrm_email.id as email_id\n FROM civicrm_email\n WHERE civicrm_email.email = %1\n AND civicrm_email.contact_id = %2"; $params = array(1 => array($email, 'String'), 2 => array($contact_id, 'Integer')); $dao = CRM_Core_DAO::executeQuery($query, $params); if (!$dao->fetch()) { CRM_Core_Error::fatal('Please file an issue with the backtrace'); return null; } $se =& new CRM_Mailing_Event_BAO_Subscribe(); $se->group_id = $group_id; $se->contact_id = $contact_id; $se->time_stamp = date('YmdHis'); $se->hash = substr(sha1("{$group_id}:{$contact_id}:{$dao->email_id}:" . time()), 0, 16); $se->save(); $contacts = array($contact_id); require_once 'CRM/Contact/BAO/GroupContact.php'; CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email', 'Pending', $se->id); $transaction->commit(); return $se; }
/** * Create a new forward event, create a new contact if necessary */ static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = null, $comment = null) { $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); $successfulForward = false; if (!$q) { return $successfulForward; } /* Find the email address/contact, if it exists */ $contact = CRM_Contact_BAO_Contact::getTableName(); $location = CRM_Core_BAO_Location::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $forward = self::getTableName(); $domain =& CRM_Core_BAO_Domain::getDomain(); $dao =& new CRM_Core_Dao(); $dao->query("\n SELECT {$contact}.id as contact_id,\n {$email}.id as email_id,\n {$contact}.do_not_email as do_not_email,\n {$queueTable}.id as queue_id\n FROM ({$email}, {$job} as temp_job)\n INNER JOIN {$contact}\n ON {$email}.contact_id = {$contact}.id\n LEFT JOIN {$queueTable}\n ON {$email}.id = {$queueTable}.email_id\n LEFT JOIN {$job}\n ON {$queueTable}.job_id = {$job}.id\n AND temp_job.mailing_id = {$job}.mailing_id\n WHERE {$queueTable}.job_id = {$job_id}\n AND {$email}.email = '" . CRM_Utils_Type::escape($forward_email, 'String') . "'"); $dao->fetch(); require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); if (isset($dao->queue_id) || $dao->do_not_email == 1) { /* We already sent this mailing to $forward_email, or we should * never email this contact. Give up. */ return $successfulForward; } require_once 'api/v2/Contact.php'; $contact_params = array('email' => $forward_email); $count = civicrm_contact_search_count($contact_params); if ($count == 0) { require_once 'CRM/Core/BAO/LocationType.php'; /* If the contact does not exist, create one. */ $formatted = array('contact_type' => 'Individual'); $locationType = CRM_Core_BAO_LocationType::getDefault(); $value = array('email' => $forward_email, 'location_type_id' => $locationType->id); _civicrm_add_formatted_param($value, $formatted); require_once 'CRM/Import/Parser.php'; $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP; $formatted['fixAddress'] = true; $contact =& civicrm_contact_format_create($formatted); if (civicrm_error($contact, CRM_Core_Error)) { return $successfulForward; } $contact_id = $contact['id']; } $email =& new CRM_Core_DAO_Email(); $email->email = $forward_email; $email->find(true); $email_id = $email->id; if (!$contact_id) { $contact_id = $email->contact_id; } /* Create a new queue event */ $queue_params = array('email_id' => $email_id, 'contact_id' => $contact_id, 'job_id' => $job_id); $queue =& CRM_Mailing_Event_BAO_Queue::create($queue_params); $forward =& new CRM_Mailing_Event_BAO_Forward(); $forward->time_stamp = date('YmdHis'); $forward->event_queue_id = $queue_id; $forward->dest_queue_id = $queue->id; $forward->save(); $dao->reset(); $dao->query(" SELECT {$job}.mailing_id as mailing_id \n FROM {$job}\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); $dao->fetch(); $mailing_obj =& new CRM_Mailing_BAO_Mailing(); $mailing_obj->id = $dao->mailing_id; $mailing_obj->find(true); $config =& CRM_Core_Config::singleton(); $mailer =& $config->getMailer(); $recipient = null; $attachments = null; $message =& $mailing_obj->compose($job_id, $queue->id, $queue->hash, $queue->contact_id, $forward_email, $recipient, false, null, $attachments, true, $fromEmail); //append comment if added while forwarding. if (count($comment)) { $message->_txtbody = $comment['body_text'] . $message->_txtbody; if (CRM_Utils_Array::value('body_html', $comment)) { $message->_htmlbody = $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody; } } $body = $message->get(); $headers = $message->headers(); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Core_Error', 'nullHandler')); $result = null; if (is_object($mailer)) { $result = $mailer->send($recipient, $headers, $body); CRM_Core_Error::setCallback(); } $params = array('event_queue_id' => $queue->id, 'job_id' => $job_id, 'hash' => $queue->hash); if (is_a($result, PEAR_Error)) { /* Register the bounce event */ $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage())); CRM_Mailing_Event_BAO_Bounce::create($params); } else { $successfulForward = true; /* Register the delivery event */ CRM_Mailing_Event_BAO_Delivered::create($params); } $transaction->commit(); return $successfulForward; }
/** * Function to check duplicate contacts based on de-deupe parameters */ function civicrm_check_contact_dedupe(&$params) { static $cIndieFields = null; static $defaultLocationId = null; $contactType = $params['contact_type']; if ($cIndieFields == null) { require_once 'CRM/Contact/BAO/Contact.php'; $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType); $cIndieFields = $cTempIndieFields; require_once "CRM/Core/BAO/LocationType.php"; $defaultLocation =& CRM_Core_BAO_LocationType::getDefault(); //set the value to default location id else set to 1 if (!($defaultLocationId = (int) $defaultLocation->id)) { $defaultLocationId = 1; } } require_once 'CRM/Contact/BAO/Query.php'; $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields; $contactFormatted = array(); foreach ($params as $key => $field) { if ($field == null || $field === '') { continue; } if (is_array($field)) { foreach ($field as $value) { $break = false; if (is_array($value)) { foreach ($value as $name => $testForEmpty) { if ($name !== 'phone_type' && ($testForEmpty === '' || $testForEmpty == null)) { $break = true; break; } } } else { $break = true; } if (!$break) { _civicrm_add_formatted_param($value, $contactFormatted); } } continue; } $value = array($key => $field); // check if location related field, then we need to add primary location type if (in_array($key, $locationFields)) { $value['location_type_id'] = $defaultLocationId; } else { if (array_key_exists($key, $cIndieFields)) { $value['contact_type'] = $contactType; } } _civicrm_add_formatted_param($value, $contactFormatted); } $contactFormatted['contact_type'] = $contactType; return _civicrm_duplicate_formatted_contact($contactFormatted); }