/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param array $values The reformatted properties that we can use internally * ' * @return array|CRM_Error * @access public */ function _crm_format_params(&$params, &$values) { // copy all the contact and contact_type fields as is $fields =& CRM_Contact_DAO_Contact::fields(); _crm_store_values($fields, $params, $values); require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_DAO_" . $values['contact_type']) . ".php"; eval('$fields =& CRM_Contact_DAO_' . $values['contact_type'] . '::fields( );'); _crm_store_values($fields, $params, $values); $ids = array("prefix", "suffix", "gender"); foreach ($ids as $id) { if (array_key_exists($id, $params)) { $values[$id] = $params[$id]; } } $locationTypeNeeded = false; $values['location'] = array(); $values['location'][1] = array(); $fields =& CRM_Core_DAO_Location::fields(); if (_crm_store_values($fields, $params, $values['location'][1])) { $locationTypeNeeded = true; } if (array_key_exists('location_type', $params)) { $locationTypes = CRM_Core_PseudoConstant::locationType(); $locationType = $locationTypeId = ''; //fix for CRM-707 if (!is_numeric($params['location_type'])) { $locationTypeName = $params['location_type']; $locationTypeId = CRM_Utils_Array::value($params['location_type'], $locationTypes); } else { $locationTypeName = CRM_Utils_Array::value($params['location_type'], $locationTypes); $locationTypeId = $params['location_type']; } $values['location'][1]['location_type'] = $locationTypeName; $values['location'][1]['location_type_id'] = $locationTypeId; } $values['location'][1]['address'] = array(); $fields =& CRM_Core_DAO_Address::fields(); // ignore the note field in address for now unset($fields['note']); if (_crm_store_values($fields, $params, $values['location'][1]['address'])) { $locationTypeNeeded = true; } $ids = array('county', 'country', 'state_province', 'supplemental_address_1', 'supplemental_address_2', 'StateProvince.name'); foreach ($ids as $id) { if (array_key_exists($id, $params)) { $values['location'][1]['address'][$id] = $params[$id]; $locationTypeNeeded = true; } } $blocks = array('Email', 'Phone', 'IM'); foreach ($blocks as $block) { $name = strtolower($block); $values['location'][1][$name] = array(); $values['location'][1][$name][1] = array(); require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Core_DAO_" . $block) . ".php"; eval('$fields =& CRM_Core_DAO_' . $block . '::fields( );'); if (_crm_store_values($fields, $params, $values['location'][1][$name][1])) { $locationTypeNeeded = true; $values['location'][1][$name][1]['is_primary'] = 1; } } if (!array_key_exists('first_name', $params) || !array_key_exists('last_name', $params)) { // make sure phone and email are valid strings if (array_key_exists('email', $params) && !CRM_Utils_Rule::email($params['email'])) { return _crm_error("Email not valid " . $params['email']); } } if (array_key_exists('im', $params)) { $values['location'][1]['im'][1]['name'] = $params['im']; $locationTypeNeeded = true; } if (array_key_exists('im_provider', $params)) { $values['location'][1]['im'][1]['provider'] = $params['im_provider']; $locationTypeNeeded = true; } if ($locationTypeNeeded) { if (!array_key_exists('location_type_id', $values['location'][1])) { require_once 'CRM/Core/BAO/LocationType.php'; $locationType =& CRM_Core_BAO_LocationType::getDefault(); $values['location'][1]['location_type_id'] = $locationType->id; $values['location'][1]['location_type'] = $locationType->name; } $values['location'][1]['is_primary'] = true; } else { unset($values['location']); } if (array_key_exists('note', $params)) { $values['note'] = $params['note']; } $values['custom'] = array(); $customFields = CRM_Core_BAO_CustomField::getFields($values['contact_type']); foreach ($params as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { /* check if it's a valid custom field id */ if (!array_key_exists($customFieldID, $customFields)) { return _crm_error('Invalid custom field ID'); } /* validate the data against the CF type */ //CRM_Core_Error::debug( $value, $customFields[$customFieldID] ); $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID][2], $value); if (!$valid) { return _crm_error('Invalid value for custom field ' . $customFields[$customFieldID][0]); } // fix the date field if so if ($customFields[$customFieldID][2] == 'Date') { $value = str_replace('-', '', $value); } $newMulValues = array(); if ($customFields[$customFieldID][3] == 'CheckBox' || $customFields[$customFieldID][3] == 'Multi-Select') { $value = str_replace("|", ",", $value); $mulValues = explode(',', $value); $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($mulValues as $v1) { foreach ($custuomOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($v1))) { $newMulValues[] = $v2['value']; } } } $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $newMulValues); } else { if ($customFields[$customFieldID][3] == 'Select' || $customFields[$customFieldID][3] == 'Radio') { $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($custuomOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($value))) { $value = $v2['value']; break; } } } } $values['custom'][$customFieldID] = array('value' => $value, 'extends' => $customFields[$customFieldID][3], 'type' => $customFields[$customFieldID][2], 'custom_field_id' => $customFieldID); } } CRM_Contact_BAO_Contact::resolveDefaults($values, true); return null; }
/** * combine all the exportable fields from the lower levels object * * currentlty we are using importable fields as exportable fields * * @param int $contactType contact Type * $param boolean $status true while exporting primary contacts * $param boolean $export true when used during export * * @return array array of exportable Fields * @access public */ function &exportableFields($contactType = 'Individual', $status = false, $export = false) { if (empty($contactType)) { $contactType = 'All'; } if (!$GLOBALS['_CRM_CONTACT_BAO_CONTACT']['_exportableFields'] || !CRM_Utils_Array::value($contactType, $GLOBALS['_CRM_CONTACT_BAO_CONTACT']['_exportableFields'])) { if (!$GLOBALS['_CRM_CONTACT_BAO_CONTACT']['_exportableFields']) { $GLOBALS['_CRM_CONTACT_BAO_CONTACT']['_exportableFields'] = array(); } if (!$status) { $fields = array(); } else { $fields = array('' => array('title' => ts('- Contact Fields -'))); } if ($contactType != 'All') { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_DAO_" . $contactType) . ".php"; eval('$fields = array_merge($fields, CRM_Contact_DAO_' . $contactType . '::export( ));'); } else { foreach (array('Individual', 'Household', 'Organization') as $type) { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_DAO_" . $type) . ".php"; eval('$fields = array_merge($fields, CRM_Contact_DAO_' . $type . '::export( ));'); if ($type == 'Individual') { $fields = array_merge($fields, CRM_Core_DAO_IndividualPrefix::export(true), CRM_Core_DAO_IndividualSuffix::export(true), CRM_Core_DAO_Gender::export(true)); } } } // the fields are only meant for Individual contact type if ($contactType == 'Individual') { $fields = array_merge($fields, CRM_Core_DAO_IndividualPrefix::export(true), CRM_Core_DAO_IndividualSuffix::export(true), CRM_Core_DAO_Gender::export(true)); } $locationType = array(); if ($status) { $locationType['location_type'] = array('name' => 'location_type', 'where' => 'civicrm_location_type.name', 'title' => 'Location Type'); } $IMProvider = array(); if ($status) { $IMProvider['im_provider'] = array('name' => 'im_provider', 'where' => 'civicrm_im_provider.name', 'title' => 'IM Provider'); } $locationFields = array_merge($locationType, CRM_Core_DAO_Location::export(), CRM_Core_DAO_Address::export(), CRM_Core_DAO_Phone::export(), CRM_Core_DAO_Email::export(), $IMProvider, CRM_Core_DAO_IM::export(true)); foreach ($locationFields as $key => $field) { $locationFields[$key]['hasLocationType'] = true; } $fields = array_merge($fields, $locationFields); $fields = array_merge($fields, CRM_Contact_DAO_Contact::export()); $fields = array_merge($fields, CRM_Core_DAO_Note::export()); if ($contactType != 'All') { $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport($contactType, $status)); } else { foreach (array('Individual', 'Household', 'Organization') as $type) { $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport($type)); } } //fix for CRM-791 if ($export) { $fields = array_merge($fields, array('groups' => array('title' => ts('Group(s)')))); $fields = array_merge($fields, array('tags' => array('title' => ts('Tag(s)')))); } else { $fields = array_merge($fields, array('group' => array('title' => ts('Group(s)')))); $fields = array_merge($fields, array('tag' => array('title' => ts('Tag(s)')))); } $GLOBALS['_CRM_CONTACT_BAO_CONTACT']['_exportableFields'][$contactType] = $fields; } return $GLOBALS['_CRM_CONTACT_BAO_CONTACT']['_exportableFields'][$contactType]; }
/** * Find all intended recipients of a mailing * * @param int $job_id Job ID * @return object A DAO loaded with results of the form * (email_id, contact_id) */ function &getRecipients($job_id) { $mailingGroup =& new CRM_Mailing_DAO_Group(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $mg = CRM_Mailing_DAO_Group::getTableName(); $eq = CRM_Mailing_Event_DAO_Queue::getTableName(); $ed = CRM_Mailing_Event_DAO_Delivered::getTableName(); $eb = CRM_Mailing_Event_DAO_Bounce::getTableName(); $email = CRM_Core_DAO_Email::getTableName(); $contact = CRM_Contact_DAO_Contact::getTableName(); $location = CRM_Core_DAO_Location::getTableName(); $group = CRM_Contact_DAO_Group::getTableName(); $g2contact = CRM_Contact_DAO_GroupContact::getTableName(); /* Create a temp table for contact exclusion */ $mailingGroup->query("CREATE TEMPORARY TABLE X_{$job_id} \n (contact_id int primary key) \n ENGINE=HEAP"); /* Add all the members of groups excluded from this mailing to the temp * table */ $excludeSubGroup = "INSERT INTO X_{$job_id} (contact_id)\n SELECT {$g2contact}.contact_id\n FROM {$g2contact}\n INNER JOIN {$mg}\n ON {$g2contact}.group_id = {$mg}.entity_id AND {$mg}.entity_table = '{$group}'\n WHERE\n {$mg}.mailing_id = {$this->id}\n AND {$g2contact}.status = 'Added'\n AND {$mg}.group_type = 'Exclude'"; $mailingGroup->query($excludeSubGroup); /* Add all the (intended) recipients of an excluded prior mailing to * the temp table */ $excludeSubMailing = "INSERT IGNORE INTO X_{$job_id} (contact_id)\n SELECT {$eq}.contact_id\n FROM {$eq}\n INNER JOIN {$job}\n ON {$eq}.job_id = {$job}.id\n INNER JOIN {$mg}\n ON {$job}.mailing_id = {$mg}.entity_id AND {$mg}.entity_table = '{$mailing}'\n WHERE\n {$mg}.mailing_id = {$this->id}\n AND {$mg}.group_type = 'Exclude'"; $mailingGroup->query($excludeSubMailing); /* Add all the succesful deliveries of this mailing (but any job/retry) * to the exclude temp table */ $excludeRetry = "INSERT IGNORE INTO X_{$job_id} (contact_id)\n SELECT {$eq}.contact_id\n FROM {$eq}\n INNER JOIN {$job}\n ON {$eq}.job_id = {$job}.id\n INNER JOIN {$ed}\n ON {$eq}.id = {$ed}.event_queue_id\n LEFT JOIN {$eb}\n ON {$eq}.id = {$eb}.event_queue_id\n WHERE\n {$job}.mailing_id = {$this->id}\n AND {$eb}.id IS null"; $mailingGroup->query($excludeRetry); $ss =& new CRM_Core_DAO(); $ss->query("SELECT {$group}.saved_search_id as saved_search_id\n FROM {$group}\n INNER JOIN {$mg}\n ON {$mg}.entity_id = {$group}.id\n WHERE {$mg}.entity_table = '{$group}'\n AND {$mg}.group_type = 'Exclude'\n AND {$mg}.mailing_id = {$this->id}\n AND {$group}.saved_search_id IS NOT null"); $whereTables = array(); while ($ss->fetch()) { /* run the saved search query and dump result contacts into the temp * table */ $tables = array($contact => 1); $where = CRM_Contact_BAO_SavedSearch::whereClause($ss->saved_search_id, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables); $mailingGroup->query("INSERT IGNORE INTO X_{$job_id} (contact_id)\n SELECT {$contact}.id\n {$from}\n WHERE {$where}"); } /* Get all the group contacts we want to include */ $mailingGroup->query("CREATE TEMPORARY TABLE I_{$job_id} \n (email_id int, contact_id int primary key)\n ENGINE=HEAP"); /* Get the group contacts, but only those which are not in the * exclusion temp table */ /* Get the emails with no override */ $mailingGroup->query("INSERT INTO I_{$job_id} (email_id, contact_id)\n SELECT DISTINCT {$email}.id as email_id,\n {$contact}.id as contact_id\n FROM {$email}\n INNER JOIN {$location}\n ON {$email}.location_id = {$location}.id\n INNER JOIN {$contact}\n ON {$location}.entity_id = {$contact}.id\n AND {$location}.entity_table = '{$contact}'\n INNER JOIN {$g2contact}\n ON {$contact}.id = {$g2contact}.contact_id\n INNER JOIN {$mg}\n ON {$g2contact}.group_id = {$mg}.entity_id\n AND {$mg}.entity_table = '{$group}'\n LEFT JOIN X_{$job_id}\n ON {$contact}.id = X_{$job_id}.contact_id\n WHERE \n {$mg}.group_type = 'Include'\n AND {$g2contact}.status = 'Added'\n AND {$g2contact}.location_id IS null\n AND {$g2contact}.email_id IS null\n AND {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$location}.is_primary = 1\n AND {$email}.is_primary = 1\n AND {$email}.on_hold = 0\n AND {$mg}.mailing_id = {$this->id}\n AND X_{$job_id}.contact_id IS null"); /* Query prior mailings */ $mailingGroup->query("REPLACE INTO I_{$job_id} (email_id, contact_id)\n SELECT DISTINCT {$email}.id as email_id,\n {$contact}.id as contact_id\n FROM {$email}\n INNER JOIN {$location}\n ON {$email}.location_id = {$location}.id\n INNER JOIN {$contact}\n ON {$location}.entity_id = {$contact}.id\n AND {$location}.entity_table = '{$contact}'\n INNER JOIN {$eq}\n ON {$eq}.contact_id = {$contact}.id\n INNER JOIN {$job}\n ON {$eq}.job_id = {$job}.id\n INNER JOIN {$mg}\n ON {$job}.mailing_id = {$mg}.entity_id AND {$mg}.entity_table = '{$mailing}'\n LEFT JOIN X_{$job_id}\n ON {$contact}.id = X_{$job_id}.contact_id\n WHERE\n {$mg}.group_type = 'Include'\n AND {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$location}.is_primary = 1\n AND {$email}.is_primary = 1\n AND {$email}.on_hold = 0\n AND {$mg}.mailing_id = {$this->id}\n AND X_{$job_id}.contact_id IS null"); /* Construct the saved-search queries */ $ss->query("SELECT {$group}.saved_search_id as saved_search_id\n FROM {$group}\n INNER JOIN {$mg}\n ON {$mg}.entity_id = {$group}.id\n AND {$mg}.entity_table = '{$group}'\n WHERE \n {$mg}.group_type = 'Include'\n AND {$mg}.mailing_id = {$this->id}\n AND {$group}.saved_search_id IS NOT null"); $whereTables = array(); while ($ss->fetch()) { $tables = array($contact => 1, $location => 1, $email => 1); $where = CRM_Contact_BAO_SavedSearch::whereClause($ss->saved_search_id, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables); $ssq = "INSERT IGNORE INTO I_{$job_id} (email_id, contact_id)\n SELECT DISTINCT {$email}.id as email_id,\n {$contact}.id as contact_id \n {$from}\n LEFT JOIN X_{$job_id}\n ON {$contact}.id = X_{$job_id}.contact_id\n WHERE \n {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$location}.is_primary = 1\n AND {$email}.is_primary = 1\n AND {$email}.on_hold = 0\n AND {$where}\n AND X_{$job_id}.contact_id IS null "; $mailingGroup->query($ssq); } /* Get the emails with only location override */ $mailingGroup->query("REPLACE INTO I_{$job_id} (email_id, contact_id)\n SELECT DISTINCT {$email}.id as local_email_id,\n {$contact}.id as contact_id\n FROM {$email}\n INNER JOIN {$location}\n ON {$email}.location_id = {$location}.id\n INNER JOIN {$contact}\n ON {$location}.entity_id = {$contact}.id\n AND {$location}.entity_table = '{$contact}'\n INNER JOIN {$g2contact}\n ON {$contact}.id = {$g2contact}.contact_id\n AND {$location}.id = {$g2contact}.location_id\n INNER JOIN {$mg}\n ON {$g2contact}.group_id = {$mg}.entity_id\n LEFT JOIN X_{$job_id}\n ON {$contact}.id = X_{$job_id}.contact_id\n WHERE \n {$mg}.entity_table = '{$group}'\n AND {$mg}.group_type = 'Include'\n AND {$g2contact}.status = 'Added'\n AND {$g2contact}.location_id IS NOT null\n AND {$g2contact}.email_id is null\n AND {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$email}.is_primary = 1\n AND {$email}.on_hold = 0\n AND {$mg}.mailing_id = {$this->id}\n AND X_{$job_id}.contact_id IS null"); /* Get the emails with full override */ $mailingGroup->query("REPLACE INTO I_{$job_id} (email_id, contact_id)\n SELECT DISTINCT {$email}.id as email_id,\n {$contact}.id as contact_id\n FROM {$email}\n INNER JOIN {$g2contact}\n ON {$email}.id = {$g2contact}.email_id\n INNER JOIN {$contact}\n ON {$contact}.id = {$g2contact}.contact_id\n INNER JOIN {$mg}\n ON {$g2contact}.group_id = {$mg}.entity_id\n LEFT JOIN X_{$job_id}\n ON {$contact}.id = X_{$job_id}.contact_id\n WHERE \n {$mg}.entity_table = '{$group}'\n AND {$mg}.group_type = 'Include'\n AND {$g2contact}.status = 'Added'\n AND {$g2contact}.location_id IS NOT null\n AND {$g2contact}.email_id IS NOT null\n AND {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$email}.on_hold = 0\n AND {$mg}.mailing_id = {$this->id}\n AND X_{$job_id}.contact_id IS null"); $results = array(); $eq =& new CRM_Mailing_Event_BAO_Queue(); $eq->query("SELECT contact_id, email_id \n FROM I_{$job_id} \n ORDER BY contact_id, email_id"); /* Delete the temp table */ $mailingGroup->reset(); $mailingGroup->query("DROP TEMPORARY TABLE X_{$job_id}"); $mailingGroup->query("DROP TEMPORARY TABLE I_{$job_id}"); return $eq; }
/** * returns the list of fields that can be exported * * @access public * return array */ function &export($prefix = false) { if (!$GLOBALS['_CRM_CORE_DAO_LOCATION']['_export']) { $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'] = array(); $fields =& CRM_Core_DAO_Location::fields(); foreach ($fields as $name => $field) { if (CRM_Utils_Array::value('export', $field)) { if ($prefix) { $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export']['location'] =& $fields[$name]; } else { $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'][$name] =& $fields[$name]; } } } $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'] = array_merge($GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'], CRM_Core_DAO_LocationType::export(true)); } return $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export']; }