/** * called when action is browse. * */ public function browse() { $in = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Added'); // keep track of all 'added' contact groups so we can remove them from the smart group // section $staticGroups = array(); if (!empty($in)) { foreach ($in as $group) { $staticGroups[$group['group_id']] = 1; } } $allGroup = CRM_Contact_BAO_GroupContactCache::contactGroup($this->_contactId); $this->assign('groupSmart', NULL); $this->assign('groupParent', NULL); if (!empty($allGroup)) { $smart = $parent = array(); foreach ($allGroup['group'] as $group) { // delete all smart groups which are also in static groups if (isset($staticGroups[$group['id']])) { continue; } if (empty($group['children'])) { $smart[] = $group; } else { $parent[] = $group; } } if (!empty($smart)) { $this->assign_by_ref('groupSmart', $smart); } if (!empty($parent)) { $this->assign_by_ref('groupParent', $parent); } } }
function where() { $petition_id = intval($this->_params['petition_id_value']); $group_id = NULL; if (array_key_exists('group_id_value', $this->_params)) { $group_id = intval($this->_params['group_id_value']); } $petition_activity_type_id = intval(CRM_Core_OptionGroup::getValue('activity_type', 'Petition', 'name')); $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $source_activity_record_type_id = intval(CRM_Utils_Array::key('Activity Source', $activityContacts)); $this->_where = "WHERE "; $signed = ''; $group = ''; // Include people who have signed the petition OR people who are in the passed in group // First the signers. $signed = "{$this->_aliases['civicrm_contact']}.id IN (SELECT contact_id\n FROM civicrm_activity_contact ac JOIN civicrm_activity a ON\n ac.activity_id = a.id WHERE ac.record_type_id = {$source_activity_record_type_id}\n AND source_record_id = {$petition_id} AND a.activity_type_id = {$petition_activity_type_id})"; // Now the people in the specified group if ($group_id) { // Check if we are a smart group or regular group $results = civicrm_api3('Group', 'getsingle', array('id' => $group_id)); if (!empty($results['id'])) { $group = "{$this->_aliases['civicrm_contact']}.id IN (SELECT contact_id FROM "; if (!empty($results['saved_search_id'])) { // Populate the cache CRM_Contact_BAO_GroupContactCache::check($group_id); $group .= "civicrm_group_contact_cache cc WHERE cc.group_id = {$group_id})"; } else { $group .= "civicrm_group_contact gc WHERE gc.group_id = {$group_id}\n AND gc.status = 'Added')"; } } } if (!empty($group)) { $this->_where .= " ({$signed}) OR ({$group}) "; } else { $this->_where .= "{$signed}"; } }
/** * Takes an associative array and creates a participant object. * * the function extract all the params it needs to initialize the create a * participant object. the params array could contain additional unused name/value * pairs * * @param array $params * (reference ) an assoc array of name/value pairs. * * @return CRM_Event_BAO_Participant */ public static function &add(&$params) { if (!empty($params['id'])) { CRM_Utils_Hook::pre('edit', 'Participant', $params['id'], $params); } else { CRM_Utils_Hook::pre('create', 'Participant', NULL, $params); } // converting dates to mysql format if (!empty($params['register_date'])) { $params['register_date'] = CRM_Utils_Date::isoToMysql($params['register_date']); } if (!empty($params['participant_fee_amount'])) { $params['participant_fee_amount'] = CRM_Utils_Rule::cleanMoney($params['participant_fee_amount']); } if (!empty($params['fee_amount'])) { $params['fee_amount'] = CRM_Utils_Rule::cleanMoney($params['fee_amount']); } // ensure that role ids are encoded as a string if (isset($params['role_id']) && is_array($params['role_id'])) { if (in_array(key($params['role_id']), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { $op = key($params['role_id']); $params['role_id'] = $params['role_id'][$op]; } else { $params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']); } } $participantBAO = new CRM_Event_BAO_Participant(); if (!empty($params['id'])) { $participantBAO->id = CRM_Utils_Array::value('id', $params); $participantBAO->find(TRUE); $participantBAO->register_date = CRM_Utils_Date::isoToMysql($participantBAO->register_date); } $participantBAO->copyValues($params); //CRM-6910 //1. If currency present, it should be valid one. //2. We should have currency when amount is not null. $currency = $participantBAO->fee_currency; if ($currency || !CRM_Utils_System::isNull($participantBAO->fee_amount)) { if (!CRM_Utils_Rule::currencyCode($currency)) { $config = CRM_Core_Config::singleton(); $currency = $config->defaultCurrency; } } $participantBAO->fee_currency = $currency; $participantBAO->save(); $session = CRM_Core_Session::singleton(); CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); if (!empty($params['id'])) { CRM_Utils_Hook::post('edit', 'Participant', $participantBAO->id, $participantBAO); } else { CRM_Utils_Hook::post('create', 'Participant', $participantBAO->id, $participantBAO); } return $participantBAO; }
/** * Delete custom value. */ public static function deleteCustomValue() { CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain'); $customValueID = CRM_Utils_Type::escape($_REQUEST['valueID'], 'Positive'); $customGroupID = CRM_Utils_Type::escape($_REQUEST['groupID'], 'Positive'); $contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', CRM_Core_DAO::$_nullObject); CRM_Core_BAO_CustomValue::deleteCustomValue($customValueID, $customGroupID); if ($contactId) { echo CRM_Contact_BAO_Contact::getCountComponent('custom_' . $customGroupID, $contactId); } CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); CRM_Utils_System::civiExit(); }
/** * 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); }
function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "contact_a") { $config = CRM_Core_Config::singleton(); // find all the groups that are part of a saved search $groupIDs = implode(',', $groups); if (empty($groupIDs)) { return NULL; } $sql = "\nSELECT id, cache_date, saved_search_id, children\nFROM civicrm_group\nWHERE id IN ( {$groupIDs} )\n AND ( saved_search_id != 0\n OR saved_search_id IS NOT NULL\n OR children IS NOT NULL )\n"; $group = CRM_Core_DAO::executeQuery($sql); $ssWhere = array(); while ($group->fetch()) { if ($tableAlias == NULL) { $alias = "`civicrm_group_contact_cache_{$group->id}`"; } else { $alias = $tableAlias; } $this->_useDistinct = TRUE; if (!$this->_smartGroupCache || $group->cache_date == NULL) { CRM_Contact_BAO_GroupContactCache::load($group); } $this->_tables[$alias] = $this->_whereTables[$alias] = " LEFT JOIN civicrm_group_contact_cache {$alias} ON {$joinTable}.id = {$alias}.contact_id "; $ssWhere[] = "{$alias}.group_id = {$group->id}"; } if (!empty($ssWhere)) { return implode(' OR ', $ssWhere); } return NULL; }
public static function whereClause($type, &$tables, &$whereTables, $contactID = NULL) { $acls = CRM_ACL_BAO_Cache::build($contactID); //CRM_Core_Error::debug( "a: $contactID", $acls ); $whereClause = NULL; $clauses = array(); if (!empty($acls)) { $aclKeys = array_keys($acls); $aclKeys = implode(',', $aclKeys); $query = "\nSELECT a.operation, a.object_id\n FROM civicrm_acl_cache c, civicrm_acl a\n WHERE c.acl_id = a.id\n AND a.is_active = 1\n AND a.object_table = 'civicrm_saved_search'\n AND a.id IN ( {$aclKeys} )\nORDER BY a.object_id\n"; $dao = CRM_Core_DAO::executeQuery($query); // do an or of all the where clauses u see $ids = array(); while ($dao->fetch()) { // make sure operation matches the type TODO if (self::matchType($type, $dao->operation)) { if (!$dao->object_id) { $ids = array(); $whereClause = ' ( 1 ) '; break; } $ids[] = $dao->object_id; } } if (!empty($ids)) { $ids = implode(',', $ids); $query = "\nSELECT g.*\n FROM civicrm_group g\n WHERE g.id IN ( {$ids} )\n AND g.is_active = 1\n"; $dao = CRM_Core_DAO::executeQuery($query); $staticGroupIDs = array(); $cachedGroupIDs = array(); while ($dao->fetch()) { // currently operation is restrcited to VIEW/EDIT if ($dao->where_clause) { if ($dao->select_tables) { $tmpTables = array(); foreach (unserialize($dao->select_tables) as $tmpName => $tmpInfo) { if ($tmpName == '`civicrm_group_contact-' . $dao->id . '`') { $tmpName = '`civicrm_group_contact-ACL`'; $tmpInfo = str_replace('civicrm_group_contact-' . $dao->id, 'civicrm_group_contact-ACL', $tmpInfo); } elseif ($tmpName == '`civicrm_group_contact_cache_' . $dao->id . '`') { $tmpName = '`civicrm_group_contact_cache-ACL`'; $tmpInfo = str_replace('civicrm_group_contact_cache_' . $dao->id, 'civicrm_group_contact_cache-ACL', $tmpInfo); } $tmpTables[$tmpName] = $tmpInfo; } $tables = array_merge($tables, $tmpTables); } if ($dao->where_tables) { $tmpTables = array(); foreach (unserialize($dao->where_tables) as $tmpName => $tmpInfo) { if ($tmpName == '`civicrm_group_contact-' . $dao->id . '`') { $tmpName = '`civicrm_group_contact-ACL`'; $tmpInfo = str_replace('civicrm_group_contact-' . $dao->id, 'civicrm_group_contact-ACL', $tmpInfo); $staticGroupIDs[] = $dao->id; } elseif ($tmpName == '`civicrm_group_contact_cache_' . $dao->id . '`') { $tmpName = '`civicrm_group_contact_cache-ACL`'; $tmpInfo = str_replace('civicrm_group_contact_cache_' . $dao->id, 'civicrm_group_contact_cache-ACL', $tmpInfo); $cachedGroupIDs[] = $dao->id; } $tmpTables[$tmpName] = $tmpInfo; } $whereTables = array_merge($whereTables, $tmpTables); } } if (($dao->saved_search_id || $dao->children || $dao->parents) && $dao->cache_date == NULL) { CRM_Contact_BAO_GroupContactCache::load($dao); } } if ($staticGroupIDs) { $clauses[] = '( `civicrm_group_contact-ACL`.group_id IN (' . join(', ', $staticGroupIDs) . ') AND `civicrm_group_contact-ACL`.status IN ("Added") )'; } if ($cachedGroupIDs) { $clauses[] = '`civicrm_group_contact_cache-ACL`.group_id IN (' . join(', ', $cachedGroupIDs) . ')'; } } } if (!empty($clauses)) { $whereClause = ' ( ' . implode(' OR ', $clauses) . ' ) '; } // call the hook to get additional whereClauses CRM_Utils_Hook::aclWhereClause($type, $tables, $whereTables, $contactID, $whereClause); if (empty($whereClause)) { $whereClause = ' ( 0 ) '; } return $whereClause; }
/** * 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 * * @return array (total, removed, notRemoved) count of contacts removed to group * @access public * @static */ static function removeContactsFromGroup(&$contactIds, $groupId, $method = 'Admin', $status = 'Removed', $tracking = null) { if (!is_array($contactIds)) { return array(0, 0, 0); } require_once 'CRM/Utils/Hook.php'; if ($status == 'Removed') { $op = 'delete'; } else { $op = 'edit'; } CRM_Utils_Hook::pre($op, 'GroupContact', $groupId, $contactIds); $date = date('YmdHis'); $numContactsRemoved = 0; $numContactsNotRemoved = 0; require_once "CRM/Contact/DAO/Group.php"; $group =& new CRM_Contact_DAO_Group(); $group->id = $groupId; $group->find(true); foreach ($contactIds as $contactId) { $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 require_once 'CRM/ACL/BAO/Cache.php'; CRM_ACL_BAO_Cache::resetCache(); // reset the group contact cache for all group(s) // if this group is being used as a smart group require_once 'CRM/Contact/BAO/GroupContactCache.php'; CRM_Contact_BAO_GroupContactCache::remove(); CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds); return array(count($contactIds), $numContactsRemoved, $numContactsNotRemoved); }
/** * Create a new group. * * @param array $params * * @return CRM_Contact_BAO_Group|NULL * The new group BAO (if created) */ public static function &create(&$params) { if (!empty($params['id'])) { CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params); } else { CRM_Utils_Hook::pre('create', 'Group', NULL, $params); } // form the name only if missing: CRM-627 $nameParam = CRM_Utils_Array::value('name', $params, NULL); if (!$nameParam && empty($params['id'])) { $params['name'] = CRM_Utils_String::titleToVar($params['title']); } // convert params if array type if (isset($params['group_type'])) { if (is_array($params['group_type'])) { $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR; } } else { $params['group_type'] = ''; } $session = CRM_Core_Session::singleton(); $cid = $session->get('userID'); // this action is add if ($cid && empty($params['id'])) { $params['created_id'] = $cid; } // this action is update if ($cid && !empty($params['id'])) { $params['modified_id'] = $cid; } $group = new CRM_Contact_BAO_Group(); $group->copyValues($params); //@todo very hacky fix for the fact this function wants to receive 'parents' as an array further down but // needs it as a separated string for the DB. Preferred approaches are having the copyParams or save fn // use metadata to translate the array to the appropriate DB type or altering the param in the api layer, // or at least altering the param in same section as 'group_type' rather than repeating here. However, further down // we need the $params one to be in it's original form & we are not sure what test coverage we have on that if (isset($group->parents) && is_array($group->parents)) { $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($group->parents)) . CRM_Core_DAO::VALUE_SEPARATOR; } if (empty($params['id']) && !$nameParam) { $group->name .= "_tmp"; } $group->save(); if (!$group->id) { return NULL; } if (empty($params['id']) && !$nameParam) { $group->name = substr($group->name, 0, -4) . "_{$group->id}"; } $group->buildClause(); $group->save(); // add custom field values if (!empty($params['custom'])) { CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id); } // make the group, child of domain/site group by default. $domainGroupID = CRM_Core_BAO_Domain::getGroupId(); if (CRM_Utils_Array::value('no_parent', $params) !== 1) { if (empty($params['parents']) && $domainGroupID != $group->id && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, 'is_enabled') && !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id)) { // if no parent present and the group doesn't already have any parents, // make sure site group goes as parent $params['parents'] = array($domainGroupID => 1); } elseif (array_key_exists('parents', $params) && !is_array($params['parents'])) { $params['parents'] = array($params['parents'] => 1); } if (!empty($params['parents'])) { foreach ($params['parents'] as $parentId => $dnc) { if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) { CRM_Contact_BAO_GroupNesting::add($parentId, $group->id); } } } // clear any descendant groups cache if exists $finalGroups = CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org'); // this is always required, since we don't know when a // parent group is removed CRM_Contact_BAO_GroupNestingCache::update(); // update group contact cache for all parent groups $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id); foreach ($parentIds as $parentId) { CRM_Contact_BAO_GroupContactCache::add($parentId); } } if (!empty($params['organization_id'])) { $groupOrg = array(); $groupOrg = $params; $groupOrg['group_id'] = $group->id; CRM_Contact_BAO_GroupOrganization::add($groupOrg); } CRM_Contact_BAO_GroupContactCache::add($group->id); if (!empty($params['id'])) { CRM_Utils_Hook::post('edit', 'Group', $group->id, $group); } else { CRM_Utils_Hook::post('create', 'Group', $group->id, $group); } $recentOther = array(); if (CRM_Core_Permission::check('edit groups')) { $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=update&id=' . $group->id); // currently same permission we are using for delete a group $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=delete&id=' . $group->id); } // add the recently added group (unless hidden: CRM-6432) if (!$group->is_hidden) { CRM_Utils_Recent::add($group->title, CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id), $group->id, 'Group', NULL, NULL, $recentOther); } return $group; }
/** * Allow removing contact from a parent group even if contact is in * a child group. (CRM-8858) */ function testRemoveFromParentSmartGroup() { // Create smart group $parent $params = array('name' => 'Deceased Contacts', 'title' => 'Deceased Contacts', 'is_active' => 1, 'formValues' => array('is_deceased' => 1)); $parent = CRM_Contact_BAO_Group::createSmartGroup($params); $this->registerTestObjects(array($parent)); // Create group $child in $parent $params = array('name' => 'Child Group', 'title' => 'Child Group', 'is_active' => 1, 'parents' => array($parent->id => 1)); $child = CRM_Contact_BAO_Group::create($params); $this->registerTestObjects(array($child)); // Create $c1, $c2, $c3 $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3); // Add $c1, $c2, $c3 to $child foreach ($deceased as $contact) { $result = $this->callAPISuccess('group_contact', 'create', array('contact_id' => $contact->id, 'group_id' => $child->id)); } // GroupContactCache::load() CRM_Contact_BAO_GroupContactCache::load($parent, TRUE); $this->assertCacheMatches(array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id), $parent->id); // Remove $c1 from $parent $result = civicrm_api('group_contact', 'create', array('contact_id' => $deceased[0]->id, 'group_id' => $parent->id, 'status' => 'Removed', 'version' => '3')); $this->assertAPISuccess($result); // Assert $c1 not in $parent CRM_Contact_BAO_GroupContactCache::load($parent, TRUE); $this->assertCacheMatches(array($deceased[1]->id, $deceased[2]->id), $parent->id); // Assert $c1 still in $child $this->assertDBQuery(1, 'select count(*) from civicrm_group_contact where group_id=%1 and contact_id=%2 and status=%3', array(1 => array($child->id, 'Integer'), 2 => array($deceased[0]->id, 'Integer'), 3 => array('Added', 'String'))); }
/** * Set up a smart group testing scenario. * * @return array */ protected function setupSmartGroup() { $params = array('name' => 'Deceased Contacts', 'title' => 'Deceased Contacts', 'is_active' => 1, 'formValues' => array('is_deceased' => 1)); $group = CRM_Contact_BAO_Group::createSmartGroup($params); $this->registerTestObjects(array($group)); // Create contacts $y1, $y2, $y3 which do match $g; create $n1, $n2, $n3 which do not match $g $living = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 0), 3); $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3); $this->assertEquals(3, count($deceased)); $this->assertEquals(3, count($living)); // Assert: $g cache has exactly $y1, $y2, $y3 CRM_Contact_BAO_GroupContactCache::load($group, TRUE); $group->find(TRUE); $this->assertCacheMatches(array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id), $group->id); // Reload the group so we have the cache_date & refresh_date. return array($group, $living, $deceased); }
/** * Process the user submitted custom data values. */ public function postProcess() { // Get the form values and groupTree //CRM-18183 $params = $this->controller->exportValues($this->_name); CRM_Core_BAO_CustomValueTable::postProcess($params, 'civicrm_contact', $this->_tableID, $this->_entityType); $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name'); $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE); $cgcount += 1; $buttonName = $this->controller->getButtonName(); if ($buttonName == $this->getButtonName('upload', 'new')) { CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/cd/edit', "reset=1&type={$this->_contactType}&groupID={$this->_groupID}&entityID={$this->_tableID}&cgcount={$cgcount}&multiRecordDisplay=single&mode=add")); } // Add entry in the log table CRM_Core_BAO_Log::register($this->_tableID, 'civicrm_contact', $this->_tableID); if (CRM_Core_Resources::isAjaxMode()) { $this->ajaxResponse += CRM_Contact_Form_Inline::renderFooter($this->_tableID); } // reset the group contact cache for this group CRM_Contact_BAO_GroupContactCache::remove(); }
/** * Build where clause for groups. * * This has been overridden in order to: * 1) only build the group clause when filtering * 2) render the id field as id rather than contact_id in * order to allow us to join on hte created temp table as if it * were the contact table. * * Further refactoring could break down the parent function so it can be selectively * leveraged. * * @param string $field * @param mixed $value * @param string $op * * @return string */ public function whereGroupClause($field, $value, $op) { if ($op == 'notin') { // We do not have an optimisation for this scenario at this stage. Use // parent. return parent::whereGroupClause($field, $value, $op); } if (empty($this->groupTempTable)) { $group = new CRM_Contact_DAO_Group(); $group->is_active = 1; $group->find(); $smartGroups = array(); while ($group->fetch()) { if (in_array($group->id, $this->_params['gid_value']) && $group->saved_search_id) { $smartGroups[] = $group->id; } } CRM_Contact_BAO_GroupContactCache::check($smartGroups); $smartGroupQuery = ''; if (!empty($smartGroups)) { $smartGroups = implode(',', $smartGroups); $smartGroupQuery = " UNION DISTINCT\n SELECT DISTINCT smartgroup_contact.contact_id as id\n FROM civicrm_group_contact_cache smartgroup_contact\n WHERE smartgroup_contact.group_id IN ({$smartGroups}) "; } $sqlOp = $this->getSQLOperator($op); if (!is_array($value)) { $value = array($value); } $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")"; $query = "SELECT DISTINCT {$this->_aliases['civicrm_group']}.contact_id as id\n FROM civicrm_group_contact {$this->_aliases['civicrm_group']}\n WHERE {$clause} AND {$this->_aliases['civicrm_group']}.status = 'Added'\n {$smartGroupQuery} "; $this->buildGroupTempTable($query); } return "1"; }
/** * takes an associative array and creates a participant object * * the function extract all the params it needs to initialize the create a * participant object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Event_BAO_Participant object * @access public * @static */ static function &add(&$params) { require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('id', $params)) { CRM_Utils_Hook::pre('edit', 'Participant', $params['id'], $params); } else { CRM_Utils_Hook::pre('create', 'Participant', null, $params); } // converting dates to mysql format if (CRM_Utils_Array::value('register_date', $params)) { $params['register_date'] = CRM_Utils_Date::isoToMysql($params['register_date']); } if (CRM_Utils_Array::value('participant_fee_amount', $params)) { $params['participant_fee_amount'] = CRM_Utils_Rule::cleanMoney($params['participant_fee_amount']); } if (CRM_Utils_Array::value('participant_fee_amount', $params)) { $params['fee_amount'] = CRM_Utils_Rule::cleanMoney($params['fee_amount']); } $participantBAO = new CRM_Event_BAO_Participant(); if (CRM_Utils_Array::value('id', $params)) { $participantBAO->id = CRM_Utils_Array::value('id', $params); $participantBAO->find(true); $participantBAO->register_date = CRM_Utils_Date::isoToMysql($participantBAO->register_date); } $participantBAO->copyValues($params); //CRM-6910 //1. If currency present, it should be valid one. //2. We should have currency when amount is not null. require_once 'CRM/Utils/Rule.php'; $currency = $participantBAO->fee_currency; if ($currency || !CRM_Utils_System::isNull($participantBAO->fee_amount)) { if (!CRM_Utils_Rule::currencyCode($currency)) { $config = CRM_Core_Config::singleton(); $currency = $config->defaultCurrency; } } $participantBAO->fee_currency = $currency; $participantBAO->save(); $session =& CRM_Core_Session::singleton(); // reset the group contact cache for this group require_once 'CRM/Contact/BAO/GroupContactCache.php'; CRM_Contact_BAO_GroupContactCache::remove(); if (CRM_Utils_Array::value('id', $params)) { CRM_Utils_Hook::post('edit', 'Participant', $participantBAO->id, $participantBAO); } else { CRM_Utils_Hook::post('create', 'Participant', $participantBAO->id, $participantBAO); } return $participantBAO; }
/** * Function to delete custom value * */ static function deleteCustomValue() { $customValueID = CRM_Utils_Type::escape($_POST['valueID'], 'Positive'); $customGroupID = CRM_Utils_Type::escape($_POST['groupID'], 'Positive'); CRM_Core_BAO_CustomValue::deleteCustomValue($customValueID, $customGroupID); if ($contactId = CRM_Utils_Array::value('contactId', $_POST)) { echo CRM_Contact_BAO_Contact::getCountComponent('custom_' . $_POST['groupID'], $contactId); } // reset the group contact cache for this group CRM_Contact_BAO_GroupContactCache::remove(); CRM_Utils_System::civiExit(); }
/** * Build where clause for groups. * * @param string $field * @param mixed $value * @param string $op * * @return string */ public function whereGroupClause($field, $value, $op) { $smartGroupQuery = ""; $group = new CRM_Contact_DAO_Group(); $group->is_active = 1; $group->find(); $smartGroups = array(); while ($group->fetch()) { if (in_array($group->id, $this->_params['gid_value']) && $group->saved_search_id) { $smartGroups[] = $group->id; } } CRM_Contact_BAO_GroupContactCache::check($smartGroups); $smartGroupQuery = ''; if (!empty($smartGroups)) { $smartGroups = implode(',', $smartGroups); $smartGroupQuery = " UNION DISTINCT\n SELECT DISTINCT smartgroup_contact.contact_id\n FROM civicrm_group_contact_cache smartgroup_contact\n WHERE smartgroup_contact.group_id IN ({$smartGroups}) "; } $sqlOp = $this->getSQLOperator($op); if (!is_array($value)) { $value = array($value); } $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")"; $contactAlias = $this->_aliases['civicrm_contact']; if (!empty($this->relationType) && $this->relationType == 'b_a') { $contactAlias = $this->_aliases['civicrm_contact_b']; } return " {$contactAlias}.id {$sqlOp} (\n SELECT DISTINCT {$this->_aliases['civicrm_group']}.contact_id\n FROM civicrm_group_contact {$this->_aliases['civicrm_group']}\n WHERE {$clause} AND {$this->_aliases['civicrm_group']}.status = 'Added'\n {$smartGroupQuery} ) "; }
/** * @param $mappingID * @param $now * @param array $params * * @throws API_Exception */ static function buildRecipientContacts($mappingID, $now, $params = array()) { $actionSchedule = new CRM_Core_DAO_ActionSchedule(); $actionSchedule->mapping_id = $mappingID; $actionSchedule->is_active = 1; if (!empty($params)) { _civicrm_api3_dao_set_filter($actionSchedule, $params, FALSE, 'ActionSchedule'); } $actionSchedule->find(); while ($actionSchedule->fetch()) { $mapping = new CRM_Core_DAO_ActionMapping(); $mapping->id = $mappingID; $mapping->find(TRUE); // note: $where - this filtering applies for both // 'limit to' and 'addition to' options // $limitWhere - this filtering applies only for // 'limit to' option $select = $join = $where = $limitWhere = array(); $limitTo = $actionSchedule->limit_to; $value = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($actionSchedule->entity_value, CRM_Core_DAO::VALUE_SEPARATOR)); $value = implode(',', $value); $status = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($actionSchedule->entity_status, CRM_Core_DAO::VALUE_SEPARATOR)); $status = implode(',', $status); $anniversary = false; if (!CRM_Utils_System::isNull($mapping->entity_recipient)) { $recipientOptions = CRM_Core_OptionGroup::values($mapping->entity_recipient, FALSE, FALSE, FALSE, NULL, 'name'); } $from = "{$mapping->entity} e"; if ($mapping->entity == 'civicrm_activity') { $contactField = 'r.contact_id'; $table = 'civicrm_activity e'; $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); if ($limitTo == 0) { // including the activity target contacts if 'in addition' is defined $join[] = "INNER JOIN civicrm_activity_contact r ON r.activity_id = e.id AND record_type_id = {$targetID}"; } else { switch (CRM_Utils_Array::value($actionSchedule->recipient, $recipientOptions)) { case 'Activity Assignees': $join[] = "INNER JOIN civicrm_activity_contact r ON r.activity_id = e.id AND record_type_id = {$assigneeID}"; break; case 'Activity Source': $join[] = "INNER JOIN civicrm_activity_contact r ON r.activity_id = e.id AND record_type_id = {$sourceID}"; break; default: case 'Activity Targets': $join[] = "INNER JOIN civicrm_activity_contact r ON r.activity_id = e.id AND record_type_id = {$targetID}"; break; } } // build where clause if (!empty($value)) { $where[] = "e.activity_type_id IN ({$value})"; } else { $where[] = "e.activity_type_id IS NULL"; } if (!empty($status)) { $where[] = "e.status_id IN ({$status})"; } $where[] = ' e.is_current_revision = 1 '; $where[] = ' e.is_deleted = 0 '; $dateField = 'e.activity_date_time'; } if ($mapping->entity == 'civicrm_participant') { $table = 'civicrm_event r'; $contactField = 'e.contact_id'; $join[] = 'INNER JOIN civicrm_event r ON e.event_id = r.id'; if ($actionSchedule->recipient_listing && $limitTo) { $rList = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($actionSchedule->recipient_listing, CRM_Core_DAO::VALUE_SEPARATOR)); $rList = implode(',', $rList); switch ($recipientOptions[$actionSchedule->recipient]) { case 'participant_role': $where[] = "e.role_id IN ({$rList})"; break; default: break; } } // build where clause if (!empty($value)) { $where[] = $mapping->entity_value == 'event_type' ? "r.event_type_id IN ({$value})" : "r.id IN ({$value})"; } else { $where[] = $mapping->entity_value == 'event_type' ? "r.event_type_id IS NULL" : "r.id IS NULL"; } // participant status criteria not to be implemented // for additional recipients if (!empty($status)) { $limitWhere[] = "e.status_id IN ({$status})"; } $where[] = 'r.is_active = 1'; $where[] = 'r.is_template = 0'; $dateField = str_replace('event_', 'r.', $actionSchedule->start_action_date); } $notINClause = ''; if ($mapping->entity == 'civicrm_membership') { $contactField = 'e.contact_id'; $table = 'civicrm_membership e'; // build where clause if ($status == 2) { //auto-renew memberships $where[] = "e.contribution_recur_id IS NOT NULL "; } elseif ($status == 1) { $where[] = "e.contribution_recur_id IS NULL "; } // build where clause if (!empty($value)) { $where[] = "e.membership_type_id IN ({$value})"; } else { $where[] = "e.membership_type_id IS NULL"; } $where[] = "( e.is_override IS NULL OR e.is_override = 0 )"; $dateField = str_replace('membership_', 'e.', $actionSchedule->start_action_date); $notINClause = self::permissionedRelationships($contactField); $membershipStatus = CRM_Member_PseudoConstant::membershipStatus(NULL, "(is_current_member = 1 OR name = 'Expired')", 'id'); $mStatus = implode(',', $membershipStatus); $where[] = "e.status_id IN ({$mStatus})"; } if ($mapping->entity == 'civicrm_contact') { if ($value == 'birth_date') { $dateDBField = 'birth_date'; $table = 'civicrm_contact e'; $contactField = 'e.id'; $where[] = 'e.is_deleted = 0'; $where[] = 'e.is_deceased = 0'; } else { //custom field $customFieldParams = array('id' => substr($value, 7)); $customGroup = $customField = array(); CRM_Core_BAO_CustomField::retrieve($customFieldParams, $customField); $dateDBField = $customField['column_name']; $customGroupParams = array('id' => $customField['custom_group_id'], $customGroup); CRM_Core_BAO_CustomGroup::retrieve($customGroupParams, $customGroup); $from = $table = "{$customGroup['table_name']} e"; $contactField = 'e.entity_id'; $where[] = '1'; // possible to have no "where" in this case } $status_ = explode(',', $status); if (in_array(2, $status_)) { // anniversary mode: $dateField = 'DATE_ADD(e.' . $dateDBField . ', INTERVAL ROUND(DATEDIFF(DATE(' . $now . '), e.' . $dateDBField . ') / 365) YEAR)'; $anniversary = true; } else { // regular mode: $dateField = 'e.' . $dateDBField; } // TODO get this working // TODO: Make sure everything's provided for repetition, etc. } // CRM-13577 Introduce Smart Groups Handling if ($actionSchedule->group_id) { // Need to check if its a smart group or not // Then decide which table to join onto the query $group = CRM_Contact_DAO_Group::getTableName(); // Get the group information $sql = "\nSELECT {$group}.id, {$group}.cache_date, {$group}.saved_search_id, {$group}.children\nFROM {$group}\nWHERE {$group}.id = {$actionSchedule->group_id}\n"; $groupDAO = CRM_Core_DAO::executeQuery($sql); $isSmartGroup = FALSE; if ($groupDAO->fetch() && !empty($groupDAO->saved_search_id)) { // Check that the group is in place in the cache and up to date CRM_Contact_BAO_GroupContactCache::check($actionSchedule->group_id); // Set smart group flag $isSmartGroup = TRUE; } } // CRM-13577 End Introduce Smart Groups Handling if ($limitTo) { if ($actionSchedule->group_id) { // CRM-13577 If smart group then use Cache table if ($isSmartGroup) { $join[] = "INNER JOIN civicrm_group_contact_cache grp ON {$contactField} = grp.contact_id"; $where[] = "grp.group_id IN ({$actionSchedule->group_id})"; } else { $join[] = "INNER JOIN civicrm_group_contact grp ON {$contactField} = grp.contact_id AND grp.status = 'Added'"; $where[] = "grp.group_id IN ({$actionSchedule->group_id})"; } } elseif (!empty($actionSchedule->recipient_manual)) { $rList = CRM_Utils_Type::escape($actionSchedule->recipient_manual, 'String'); $where[] = "{$contactField} IN ({$rList})"; } } else { $addGroup = $addWhere = ''; if ($actionSchedule->group_id) { // CRM-13577 If smart group then use Cache table if ($isSmartGroup) { $addGroup = " INNER JOIN civicrm_group_contact_cache grp ON c.id = grp.contact_id"; $addWhere = " grp.group_id IN ({$actionSchedule->group_id})"; } else { $addGroup = " INNER JOIN civicrm_group_contact grp ON c.id = grp.contact_id AND grp.status = 'Added'"; $addWhere = " grp.group_id IN ({$actionSchedule->group_id})"; } } if (!empty($actionSchedule->recipient_manual)) { $rList = CRM_Utils_Type::escape($actionSchedule->recipient_manual, 'String'); $addWhere = "c.id IN ({$rList})"; } } $select[] = "{$contactField} as contact_id"; $select[] = 'e.id as entity_id'; $select[] = "'{$mapping->entity}' as entity_table"; $select[] = "{$actionSchedule->id} as action_schedule_id"; $reminderJoinClause = "civicrm_action_log reminder ON reminder.contact_id = {$contactField} AND\nreminder.entity_id = e.id AND\nreminder.entity_table = '{$mapping->entity}' AND\nreminder.action_schedule_id = %1"; if ($anniversary) { // only consider reminders less than 11 months ago $reminderJoinClause .= " AND reminder.action_date_time > DATE_SUB({$now}, INTERVAL 11 MONTH)"; } if ($table != 'civicrm_contact e') { $join[] = "INNER JOIN civicrm_contact c ON c.id = {$contactField} AND c.is_deleted = 0 AND c.is_deceased = 0 "; } if ($actionSchedule->start_action_date) { $startDateClause = array(); $op = $actionSchedule->start_action_condition == 'before' ? '<=' : '>='; $operator = $actionSchedule->start_action_condition == 'before' ? 'DATE_SUB' : 'DATE_ADD'; $date = $operator . "({$dateField}, INTERVAL {$actionSchedule->start_action_offset} {$actionSchedule->start_action_unit})"; $startDateClause[] = "'{$now}' >= {$date}"; if ($mapping->entity == 'civicrm_participant') { $startDateClause[] = $operator . "({$now}, INTERVAL 1 DAY ) {$op} " . $dateField; } else { $startDateClause[] = "DATE_SUB({$now}, INTERVAL 1 DAY ) <= {$date}"; } $startDate = implode(' AND ', $startDateClause); } elseif ($actionSchedule->absolute_date) { $startDate = "DATEDIFF(DATE('{$now}'),'{$actionSchedule->absolute_date}') = 0"; } // ( now >= date_built_from_start_time ) OR ( now = absolute_date ) $dateClause = "reminder.id IS NULL AND {$startDate}"; // start composing query $selectClause = 'SELECT ' . implode(', ', $select); $fromClause = "FROM {$from}"; $joinClause = !empty($join) ? implode(' ', $join) : ''; $whereClause = 'WHERE ' . implode(' AND ', $where); $limitWhereClause = ''; if (!empty($limitWhere)) { $limitWhereClause = ' AND ' . implode(' AND ', $limitWhere); } $query = "\nINSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id)\n{$selectClause}\n{$fromClause}\n{$joinClause}\nLEFT JOIN {$reminderJoinClause}\n{$whereClause} {$limitWhereClause} AND {$dateClause} {$notINClause}\n"; CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer'))); if ($limitTo == 0) { $additionWhere = ' WHERE '; if ($actionSchedule->start_action_date) { $additionWhere = $whereClause . ' AND '; } $contactTable = "civicrm_contact c"; $addSelect = "SELECT c.id as contact_id, c.id as entity_id, 'civicrm_contact' as entity_table, {$actionSchedule->id} as action_schedule_id"; $additionReminderClause = "civicrm_action_log reminder ON reminder.contact_id = c.id AND\n reminder.entity_id = c.id AND\n reminder.entity_table = 'civicrm_contact' AND\n reminder.action_schedule_id = {$actionSchedule->id}"; $addWhereClause = ''; if ($addWhere) { $addWhereClause = "AND {$addWhere}"; } $insertAdditionalSql = "\nINSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id)\n{$addSelect}\nFROM ({$contactTable})\nLEFT JOIN {$additionReminderClause}\n{$addGroup}\nWHERE c.is_deleted = 0 AND c.is_deceased = 0\n{$addWhereClause}\n\nAND c.id NOT IN (\n SELECT rem.contact_id\n FROM civicrm_action_log rem INNER JOIN {$mapping->entity} e ON rem.entity_id = e.id\n WHERE rem.action_schedule_id = {$actionSchedule->id}\n AND rem.entity_table = '{$mapping->entity}'\n )\nGROUP BY c.id\n"; CRM_Core_DAO::executeQuery($insertAdditionalSql); } // if repeat is turned ON: if ($actionSchedule->is_repeat) { $repeatEvent = ($actionSchedule->end_action == 'before' ? 'DATE_SUB' : 'DATE_ADD') . "({$dateField}, INTERVAL {$actionSchedule->end_frequency_interval} {$actionSchedule->end_frequency_unit})"; if ($actionSchedule->repetition_frequency_unit == 'day') { $hrs = 24 * $actionSchedule->repetition_frequency_interval; } elseif ($actionSchedule->repetition_frequency_unit == 'week') { $hrs = 24 * $actionSchedule->repetition_frequency_interval * 7; } elseif ($actionSchedule->repetition_frequency_unit == 'month') { $hrs = "24*(DATEDIFF(DATE_ADD(latest_log_time, INTERVAL 1 MONTH ), latest_log_time))"; } elseif ($actionSchedule->repetition_frequency_unit == 'year') { $hrs = "24*(DATEDIFF(DATE_ADD(latest_log_time, INTERVAL 1 YEAR ), latest_log_time))"; } else { $hrs = $actionSchedule->repetition_frequency_interval; } // (now <= repeat_end_time ) $repeatEventClause = "'{$now}' <= {$repeatEvent}"; // diff(now && logged_date_time) >= repeat_interval $havingClause = "HAVING TIMEDIFF({$now}, latest_log_time) >= TIME('{$hrs}:00:00')"; $groupByClause = 'GROUP BY reminder.contact_id, reminder.entity_id, reminder.entity_table'; $selectClause .= ', MAX(reminder.action_date_time) as latest_log_time'; $sqlInsertValues = "{$selectClause}\n{$fromClause}\n{$joinClause}\nINNER JOIN {$reminderJoinClause}\n{$whereClause} {$limitWhereClause} AND {$repeatEventClause}\n{$groupByClause}\n{$havingClause}"; $valsqlInsertValues = CRM_Core_DAO::executeQuery($sqlInsertValues, array(1 => array($actionSchedule->id, 'Integer'))); $arrValues = array(); while ($valsqlInsertValues->fetch()) { $arrValues[] = "( {$valsqlInsertValues->contact_id}, {$valsqlInsertValues->entity_id}, '{$valsqlInsertValues->entity_table}',{$valsqlInsertValues->action_schedule_id} )"; } $valString = implode(',', $arrValues); if ($valString) { $query = ' INSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id) VALUES ' . $valString; CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer'))); } if ($limitTo == 0) { $addSelect .= ', MAX(reminder.action_date_time) as latest_log_time'; $sqlEndEventCheck = "\nSELECT * FROM {$table}\n{$whereClause} AND {$repeatEventClause} LIMIT 1"; $daoCheck = CRM_Core_DAO::executeQuery($sqlEndEventCheck); if ($daoCheck->fetch()) { $valSqlAdditionInsert = "\n{$addSelect}\nFROM {$contactTable}\n{$addGroup}\nINNER JOIN {$additionReminderClause}\nWHERE {$addWhere} AND c.is_deleted = 0 AND c.is_deceased = 0\nGROUP BY reminder.contact_id\n{$havingClause}\n"; $daoForVals = CRM_Core_DAO::executeQuery($valSqlAdditionInsert); $addValues = array(); while ($daoForVals->fetch()) { $addValues[] = "( {$daoForVals->contact_id}, {$daoForVals->entity_id}, '{$daoForVals->entity_table}',{$daoForVals->action_schedule_id} )"; } $valString = implode(',', $addValues); if ($valString) { $query = ' INSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id) VALUES ' . $valString; CRM_Core_DAO::executeQuery($query); } } } } } }
/** * Create a table of the contact ids included by the group filter. * * This function is called by both the api (tests) and the UI. */ public function buildGroupTempTable() { if (!empty($this->groupTempTable) || empty($this->_params['gid_value']) || $this->groupFilterNotOptimised) { return; } $filteredGroups = (array) $this->_params['gid_value']; $groups = civicrm_api3('Group', 'get', array('is_active' => 1, 'id' => array('IN' => $filteredGroups), 'saved_search_id' => array('>' => 0), 'return' => 'id')); $smartGroups = array_keys($groups['values']); $query = "\n SELECT group_contact.contact_id as id\n FROM civicrm_group_contact group_contact\n WHERE group_contact.group_id IN (" . implode(', ', $filteredGroups) . ")\n AND group_contact.status = 'Added' "; if (!empty($smartGroups)) { CRM_Contact_BAO_GroupContactCache::check($smartGroups); $smartGroups = implode(',', $smartGroups); $query .= "\n UNION DISTINCT\n SELECT smartgroup_contact.contact_id as id\n FROM civicrm_group_contact_cache smartgroup_contact\n WHERE smartgroup_contact.group_id IN ({$smartGroups}) "; } $this->groupTempTable = 'civicrm_report_temp_group_' . date('Ymd_') . uniqid(); $this->executeReportQuery("\n CREATE TEMPORARY TABLE {$this->groupTempTable}\n {$query}\n "); CRM_Core_DAO::executeQuery("ALTER TABLE {$this->groupTempTable} ADD INDEX i_id(id)"); }
/** * Clear the contact cache so things are kosher. We started off being super aggressive with clearing * caches, but are backing off from this with every release. Compromise between ease of coding versus * performance versus being accurate at that very instant * * @param $contactID * The contactID that was edited / deleted. */ public static function clearContactCaches($contactID = NULL) { // clear acl cache if any. CRM_ACL_BAO_Cache::resetCache(); if (empty($contactID)) { // also clear prev/next dedupe cache - if no contactID passed in CRM_Core_BAO_PrevNextCache::deleteItem(); } // reset the group contact cache for this group CRM_Contact_BAO_GroupContactCache::remove(); }
/** * takes an associative array and creates a membership object * * the function extract all the params it needs to initialize the create a * membership object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Member_BAO_Membership object * @access public * @static */ static function &add(&$params, &$ids) { require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('membership', $ids)) { CRM_Utils_Hook::pre('edit', 'Membership', $ids['membership'], $params); } else { CRM_Utils_Hook::pre('create', 'Membership', null, $params); } // converting dates to mysql format if (isset($params['join_date'])) { $params['join_date'] = CRM_Utils_Date::isoToMysql($params['join_date']); } if (isset($params['start_date'])) { $params['start_date'] = CRM_Utils_Date::isoToMysql($params['start_date']); } if (CRM_Utils_Array::value('end_date', $params)) { $params['end_date'] = CRM_Utils_Date::isoToMysql($params['end_date']); } else { $params['end_date'] = 'null'; } if (CRM_Utils_Array::value('reminder_date', $params)) { $params['reminder_date'] = CRM_Utils_Date::isoToMysql($params['reminder_date']); } else { $params['reminder_date'] = 'null'; } if (!CRM_Utils_Array::value('is_override', $params)) { $params['is_override'] = 'null'; } $membership = new CRM_Member_BAO_Membership(); $membership->copyValues($params); $membership->id = CRM_Utils_Array::value('membership', $ids); $membership->save(); $membership->free(); $session =& CRM_Core_Session::singleton(); //get the log start date. //it is set during renewal of membership. $logStartDate = CRM_Utils_array::value('log_start_date', $params); $logStartDate = $logStartDate ? CRM_Utils_Date::isoToMysql($logStartDate) : $membership->start_date; $membershipLog = array('membership_id' => $membership->id, 'status_id' => $membership->status_id, 'start_date' => $logStartDate, 'end_date' => $membership->end_date, 'renewal_reminder_date' => $membership->reminder_date, 'modified_id' => CRM_Utils_Array::value('userId', $ids), 'modified_date' => date('Ymd')); require_once 'CRM/Member/BAO/MembershipLog.php'; CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray); // reset the group contact cache since smart groups might be affected due to this require_once 'CRM/Contact/BAO/GroupContactCache.php'; CRM_Contact_BAO_GroupContactCache::remove(); if (CRM_Utils_Array::value('membership', $ids)) { CRM_Utils_Hook::post('edit', 'Membership', $membership->id, $membership); } else { CRM_Utils_Hook::post('create', 'Membership', $membership->id, $membership); } return $membership; }
function from() { $iGroups = $xGroups = $iTags = $xTags = 0; //define table name $randomNum = md5(uniqid()); $this->_tableName = "civicrm_temp_custom_{$randomNum}"; //block for Group search $smartGroup = array(); if ($this->_groups || $this->_allSearch) { $group = new CRM_Contact_DAO_Group(); $group->is_active = 1; $group->find(); while ($group->fetch()) { $allGroups[] = $group->id; if ($group->saved_search_id) { $smartGroup[$group->saved_search_id] = $group->id; } } $includedGroups = implode(',', $allGroups); if (!empty($this->_includeGroups)) { $iGroups = implode(',', $this->_includeGroups); } else { //if no group selected search for all groups $iGroups = NULL; } if (is_array($this->_excludeGroups)) { $xGroups = implode(',', $this->_excludeGroups); } else { $xGroups = 0; } $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=MyISAM"; CRM_Core_DAO::executeQuery($sql); //used only when exclude group is selected if ($xGroups != 0) { $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )\n SELECT DISTINCT civicrm_group_contact.contact_id\n FROM civicrm_group_contact, civicrm_contact\n WHERE\n civicrm_contact.id = civicrm_group_contact.contact_id AND\n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN( {$xGroups})"; CRM_Core_DAO::executeQuery($excludeGroup); //search for smart group contacts foreach ($this->_excludeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssGroup = new CRM_Contact_DAO_Group(); $ssGroup->id = $values; if (!$ssGroup->find(TRUE)) { CRM_Core_Error::fatal(); } CRM_Contact_BAO_GroupContactCache::load($ssGroup); $smartSql = "\nSELECT gcc.contact_id\nFROM civicrm_group_contact_cache gcc\nWHERE gcc.group_id = {$ssGroup->id}\n"; $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery); } } } $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName} ( id int PRIMARY KEY AUTO_INCREMENT,\n contact_id int,\n group_names varchar(64)) ENGINE=MyISAM"; CRM_Core_DAO::executeQuery($sql); if ($iGroups) { $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)\n SELECT civicrm_contact.id as contact_id, civicrm_group.title as group_name\n FROM civicrm_contact\n INNER JOIN civicrm_group_contact\n ON civicrm_group_contact.contact_id = civicrm_contact.id\n LEFT JOIN civicrm_group\n ON civicrm_group_contact.group_id = civicrm_group.id"; } else { $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)\n SELECT civicrm_contact.id as contact_id, ''\n FROM civicrm_contact"; } //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}\n ON civicrm_contact.id = Xg_{$this->_tableName}.contact_id"; } if ($iGroups) { $includeGroup .= " WHERE\n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN({$iGroups})"; } else { $includeGroup .= " WHERE ( 1 ) "; } //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null"; } CRM_Core_DAO::executeQuery($includeGroup); //search for smart group contacts foreach ($this->_includeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssGroup = new CRM_Contact_DAO_Group(); $ssGroup->id = $values; if (!$ssGroup->find(TRUE)) { CRM_Core_Error::fatal(); } CRM_Contact_BAO_GroupContactCache::load($ssGroup); $smartSql = "\nSELECT gcc.contact_id\nFROM civicrm_group_contact_cache gcc\nWHERE gcc.group_id = {$ssGroup->id}\n"; //used only when exclude group is selected if ($xGroups != 0) { $smartSql .= " AND gcc.contact_id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; } $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id)\n {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery); $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}\n SET group_names = (SELECT title FROM civicrm_group\n WHERE civicrm_group.id = {$values})\n WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL\n AND Ig_{$this->_tableName}.group_names IS NULL"; CRM_Core_DAO::executeQuery($insertGroupNameQuery); } } } //group contact search end here; //block for Tags search if ($this->_tags || $this->_allSearch) { //find all tags $tag = new CRM_Core_DAO_Tag(); $tag->is_active = 1; $tag->find(); while ($tag->fetch()) { $allTags[] = $tag->id; } $includedTags = implode(',', $allTags); if (!empty($this->_includeTags)) { $iTags = implode(',', $this->_includeTags); } else { //if no group selected search for all groups $iTags = NULL; } if (is_array($this->_excludeTags)) { $xTags = implode(',', $this->_excludeTags); } else { $xTags = 0; } $sql = "CREATE TEMPORARY TABLE Xt_{$this->_tableName} ( contact_id int primary key) ENGINE=MyISAM"; CRM_Core_DAO::executeQuery($sql); //used only when exclude tag is selected if ($xTags != 0) { $excludeTag = "INSERT INTO Xt_{$this->_tableName} ( contact_id )\n SELECT DISTINCT civicrm_entity_tag.entity_id\n FROM civicrm_entity_tag, civicrm_contact\n WHERE\n civicrm_entity_tag.entity_table = 'civicrm_contact' AND\n civicrm_contact.id = civicrm_entity_tag.entity_id AND\n civicrm_entity_tag.tag_id IN( {$xTags})"; CRM_Core_DAO::executeQuery($excludeTag); } $sql = "CREATE TEMPORARY TABLE It_{$this->_tableName} ( id int PRIMARY KEY AUTO_INCREMENT,\n contact_id int,\n tag_names varchar(64)) ENGINE=MyISAM"; CRM_Core_DAO::executeQuery($sql); if ($iTags) { $includeTag = "INSERT INTO It_{$this->_tableName} (contact_id, tag_names)\n SELECT civicrm_contact.id as contact_id, civicrm_tag.name as tag_name\n FROM civicrm_contact\n INNER JOIN civicrm_entity_tag\n ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND\n civicrm_entity_tag.entity_id = civicrm_contact.id )\n LEFT JOIN civicrm_tag\n ON civicrm_entity_tag.tag_id = civicrm_tag.id"; } else { $includeTag = "INSERT INTO It_{$this->_tableName} (contact_id, tag_names)\n SELECT civicrm_contact.id as contact_id, ''\n FROM civicrm_contact"; } //used only when exclude tag is selected if ($xTags != 0) { $includeTag .= " LEFT JOIN Xt_{$this->_tableName}\n ON civicrm_contact.id = Xt_{$this->_tableName}.contact_id"; } if ($iTags) { $includeTag .= " WHERE civicrm_entity_tag.tag_id IN({$iTags})"; } else { $includeTag .= " WHERE ( 1 ) "; } //used only when exclude tag is selected if ($xTags != 0) { $includeTag .= " AND Xt_{$this->_tableName}.contact_id IS null"; } CRM_Core_DAO::executeQuery($includeTag); } $from = " FROM civicrm_contact contact_a"; /* * CRM-10850 / CRM-10848 * If we use include / exclude groups as smart groups for ACL's having the below causes * a cycle which messes things up. Hence commenting out for now * $this->buildACLClause('contact_a'); */ /* * check the situation and set booleans */ $Ig = $iGroups != 0; $It = $iTags != 0; $Xg = $xGroups != 0; $Xt = $xTags != 0; //PICK UP FROM HERE if (!$this->_groups && !$this->_tags) { $this->_andOr = 1; } /* * Set from statement depending on array sel */ $whereitems = array(); foreach (array('Ig', 'It') as $inc) { if ($this->_andOr == 1) { if (${$inc}) { $from .= " INNER JOIN {$inc}_{$this->_tableName} temptable{$inc} ON (contact_a.id = temptable{$inc}.contact_id)"; } } else { if (${$inc}) { $from .= " LEFT JOIN {$inc}_{$this->_tableName} temptable{$inc} ON (contact_a.id = temptable{$inc}.contact_id)"; } } if (${$inc}) { $whereitems[] = "temptable{$inc}.contact_id IS NOT NULL"; } } $this->_where = $whereitems ? "(" . implode(' OR ', $whereitems) . ')' : '(1)'; foreach (array('Xg', 'Xt') as $exc) { if (${$exc}) { $from .= " LEFT JOIN {$exc}_{$this->_tableName} temptable{$exc} ON (contact_a.id = temptable{$exc}.contact_id)"; $this->_where .= " AND temptable{$exc}.contact_id IS NULL"; } } $from .= " LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND ( civicrm_email.is_primary = 1 OR civicrm_email.is_bulkmail = 1 ) ) {$this->_aclFrom}"; if ($this->_aclWhere) { $this->_where .= " AND {$this->_aclWhere} "; } // also exclude all contacts that are deleted // CRM-11627 $this->_where .= " AND (contact_a.is_deleted != 1) "; return $from; }
/** * 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; }
/** * Takes an associative array and creates a contribution object. * * the function extract all the params it needs to initialize the create a * contribution object. the params array could contain additional unused name/value * pairs * * @param array $params * (reference ) an assoc array of name/value pairs. * @param array $ids * The array that holds all the db ids. * * @return CRM_Contribute_BAO_Contribution|void */ public static function add(&$params, $ids = array()) { if (empty($params)) { return NULL; } //per http://wiki.civicrm.org/confluence/display/CRM/Database+layer we are moving away from $ids array $contributionID = CRM_Utils_Array::value('contribution', $ids, CRM_Utils_Array::value('id', $params)); $duplicates = array(); if (self::checkDuplicate($params, $duplicates, $contributionID)) { $error = CRM_Core_Error::singleton(); $d = implode(', ', $duplicates); $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', array($d), "Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: {$d}"); return $error; } // first clean up all the money fields $moneyFields = array('total_amount', 'net_amount', 'fee_amount', 'non_deductible_amount'); //if priceset is used, no need to cleanup money if (!empty($params['skipCleanMoney'])) { unset($moneyFields[0]); } foreach ($moneyFields as $field) { if (isset($params[$field])) { $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); } } //set defaults in create mode if (!$contributionID) { CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); } self::calculateMissingAmountParams($params, $contributionID); if (!empty($params['payment_instrument_id'])) { $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name'); if ($params['payment_instrument_id'] != array_search('Check', $paymentInstruments)) { $params['check_number'] = 'null'; } } $setPrevContribution = TRUE; // CRM-13964 partial payment if (!empty($params['partial_payment_total']) && !empty($params['partial_amount_pay'])) { $partialAmtTotal = $params['partial_payment_total']; $partialAmtPay = $params['partial_amount_pay']; $params['total_amount'] = $partialAmtTotal; if ($partialAmtPay < $partialAmtTotal) { $params['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name'); $params['is_pay_later'] = 0; $setPrevContribution = FALSE; } } if ($contributionID) { CRM_Utils_Hook::pre('edit', 'Contribution', $contributionID, $params); } else { CRM_Utils_Hook::pre('create', 'Contribution', NULL, $params); } $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->copyValues($params); $contribution->id = $contributionID; if (empty($contribution->id)) { // (only) on 'create', make sure that a valid currency is set (CRM-16845) if (!CRM_Utils_Rule::currencyCode($contribution->currency)) { $config = CRM_Core_Config::singleton(); $contribution->currency = $config->defaultCurrency; } } if ($contributionID && $setPrevContribution) { $params['prevContribution'] = self::getValues(array('id' => $contributionID), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); } $result = $contribution->save(); // Add financial_trxn details as part of fix for CRM-4724 $contribution->trxn_result_code = CRM_Utils_Array::value('trxn_result_code', $params); $contribution->payment_processor = CRM_Utils_Array::value('payment_processor', $params); //add Account details $params['contribution'] = $contribution; self::recordFinancialAccounts($params); // reset the group contact cache for this group CRM_Contact_BAO_GroupContactCache::remove(); if ($contributionID) { CRM_Utils_Hook::post('edit', 'Contribution', $contribution->id, $contribution); } else { CRM_Utils_Hook::post('create', 'Contribution', $contribution->id, $contribution); } return $result; }
/** * @param array $groups * @param string $tableAlias * @param string $joinTable * @param string $op * * @return null|string */ public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "contact_a", $op) { $isNullOp = strpos($op, 'NULL') !== FALSE; $groupsIds = $groups; if (!$isNullOp && !$groups) { return NULL; } elseif (strpos($op, 'IN') !== FALSE) { $groups = array($op => $groups); } elseif (is_array($groups) && count($groups)) { $groups = array('IN' => $groups); } // Find all the groups that are part of a saved search. $smartGroupClause = self::buildClause("id", $op, $groups, 'Int'); $sql = "\nSELECT id, cache_date, saved_search_id, children\nFROM civicrm_group\nWHERE {$smartGroupClause}\n AND ( saved_search_id != 0\n OR saved_search_id IS NOT NULL\n OR children IS NOT NULL )\n"; $group = CRM_Core_DAO::executeQuery($sql); while ($group->fetch()) { $this->_useDistinct = TRUE; if (!$this->_smartGroupCache || $group->cache_date == NULL) { CRM_Contact_BAO_GroupContactCache::load($group); } } if (!$tableAlias) { $tableAlias = "`civicrm_group_contact_cache_"; $tableAlias .= $isNullOp ? "a`" : implode(',', (array) $groupsIds) . "`"; } $this->_tables[$tableAlias] = $this->_whereTables[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.id = {$tableAlias}.contact_id "; return self::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int'); }
/** * Process the activities. * * @param array $params * Associated array of the submitted values. * * @throws CRM_Core_Exception * * @return CRM_Activity_BAO_Activity|null|object */ public static function create(&$params) { // check required params if (!self::dataExists($params)) { throw new CRM_Core_Exception('Not enough data to create activity object'); } $activity = new CRM_Activity_DAO_Activity(); if (isset($params['id']) && empty($params['id'])) { unset($params['id']); } if (empty($params['status_id']) && empty($params['activity_status_id']) && empty($params['id'])) { if (isset($params['activity_date_time']) && strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1)) { $params['status_id'] = 2; } else { $params['status_id'] = 1; } } // Set priority to Normal for Auto-populated activities (for Cases) if (CRM_Utils_Array::value('priority_id', $params) === NULL && !CRM_Utils_Array::value('id', $params)) { $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); $params['priority_id'] = array_search('Normal', $priority); } if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) { $params['target_contact_id'] = array_unique($params['target_contact_id']); } if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) { $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']); } // CRM-9137 if (!empty($params['id'])) { CRM_Utils_Hook::pre('edit', 'Activity', $activity->id, $params); } else { CRM_Utils_Hook::pre('create', 'Activity', NULL, $params); } $activity->copyValues($params); if (isset($params['case_id'])) { // CRM-8708, preserve case ID even though it's not part of the SQL model $activity->case_id = $params['case_id']; } elseif (is_numeric($activity->id)) { // CRM-8708, preserve case ID even though it's not part of the SQL model $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); } // start transaction $transaction = new CRM_Core_Transaction(); $result = $activity->save(); if (is_a($result, 'CRM_Core_Error')) { $transaction->rollback(); return $result; } $activityId = $activity->id; $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); if (isset($params['source_contact_id'])) { $acParams = array('activity_id' => $activityId, 'contact_id' => $params['source_contact_id'], 'record_type_id' => $sourceID); self::deleteActivityContact($activityId, $sourceID); CRM_Activity_BAO_ActivityContact::create($acParams); } // check and attach and files as needed CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId); // attempt to save activity assignment $resultAssignment = NULL; if (!empty($params['assignee_contact_id'])) { $assignmentParams = array('activity_id' => $activityId); if (is_array($params['assignee_contact_id'])) { if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { // first delete existing assignments if any self::deleteActivityContact($activityId, $assigneeID); } $values = array(); foreach ($params['assignee_contact_id'] as $acID) { if ($acID) { $values[] = "( {$activityId}, {$acID}, {$assigneeID} )"; } } while (!empty($values)) { $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT); $str = implode(',', $input); $sql = "INSERT IGNORE INTO civicrm_activity_contact ( activity_id, contact_id, record_type_id ) VALUES {$str};"; CRM_Core_DAO::executeQuery($sql); } } else { $assignmentParams['contact_id'] = $params['assignee_contact_id']; $assignmentParams['record_type_id'] = $assigneeID; if (!empty($params['id'])) { $assignment = new CRM_Activity_BAO_ActivityContact(); $assignment->activity_id = $activityId; $assignment->record_type_id = $assigneeID; $assignment->find(TRUE); if ($assignment->contact_id != $params['assignee_contact_id']) { $assignmentParams['id'] = $assignment->id; $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); } } else { $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); } } } else { if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { self::deleteActivityContact($activityId, $assigneeID); } } if (is_a($resultAssignment, 'CRM_Core_Error')) { $transaction->rollback(); return $resultAssignment; } // attempt to save activity targets $resultTarget = NULL; if (!empty($params['target_contact_id'])) { $targetParams = array('activity_id' => $activityId); $resultTarget = array(); if (is_array($params['target_contact_id'])) { if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { // first delete existing targets if any self::deleteActivityContact($activityId, $targetID); } $values = array(); foreach ($params['target_contact_id'] as $tid) { if ($tid) { $values[] = "( {$activityId}, {$tid}, {$targetID} )"; } } while (!empty($values)) { $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT); $str = implode(',', $input); $sql = "INSERT IGNORE INTO civicrm_activity_contact ( activity_id, contact_id, record_type_id ) VALUES {$str};"; CRM_Core_DAO::executeQuery($sql); } } else { $targetParams['contact_id'] = $params['target_contact_id']; $targetParams['record_type_id'] = $targetID; if (!empty($params['id'])) { $target = new CRM_Activity_BAO_ActivityContact(); $target->activity_id = $activityId; $target->record_type_id = $targetID; $target->find(TRUE); if ($target->contact_id != $params['target_contact_id']) { $targetParams['id'] = $target->id; $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); } } else { $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); } } } else { if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { self::deleteActivityContact($activityId, $targetID); } } // write to changelog before transaction is committed/rolled // back (and prepare status to display) if (!empty($params['id'])) { $logMsg = "Activity (id: {$result->id} ) updated with "; } else { $logMsg = "Activity created for "; } $msgs = array(); if (isset($params['source_contact_id'])) { $msgs[] = "source={$params['source_contact_id']}"; } if (!empty($params['target_contact_id'])) { if (is_array($params['target_contact_id']) && !CRM_Utils_array::crmIsEmptyArray($params['target_contact_id'])) { $msgs[] = "target=" . implode(',', $params['target_contact_id']); // take only first target // will be used for recently viewed display $t = array_slice($params['target_contact_id'], 0, 1); $recentContactId = $t[0]; } elseif (isset($params['target_contact_id']) && !is_array($params['target_contact_id'])) { $msgs[] = "target={$params['target_contact_id']}"; // will be used for recently viewed display $recentContactId = $params['target_contact_id']; } } else { // at worst, take source for recently viewed display $recentContactId = CRM_Utils_Array::value('source_contact_id', $params); } if (isset($params['assignee_contact_id'])) { if (is_array($params['assignee_contact_id'])) { $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']); } else { $msgs[] = "assignee={$params['assignee_contact_id']}"; } } $logMsg .= implode(', ', $msgs); self::logActivityAction($result, $logMsg); if (!empty($params['custom']) && is_array($params['custom'])) { CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id); } $transaction->commit(); if (empty($params['skipRecentView'])) { $recentOther = array(); if (!empty($params['case_id'])) { $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id'); $url = CRM_Utils_System::url('civicrm/case/activity/view', "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home"); } else { $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home"; if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) { $url = CRM_Utils_System::url('civicrm/activity', $q); if ($activity->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Print PDF Letter', 'name')) { $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/pdf/add', "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home"); } else { $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/add', "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home"); } if (CRM_Core_Permission::check("delete activities")) { $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home"); } } else { $url = CRM_Utils_System::url('civicrm/activity/view', $q); if (CRM_Core_Permission::check('delete activities')) { $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home"); } } } if (!isset($activity->parent_id)) { $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId); // add the recently created Activity $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE); $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject'); $title = ""; if (isset($activitySubject)) { $title = $activitySubject . ' - '; } $title = $title . $recentContactDisplay; if (!empty($activityTypes[$activity->activity_type_id])) { $title .= ' (' . $activityTypes[$activity->activity_type_id] . ')'; } CRM_Utils_Recent::add($title, $url, $activity->id, 'Activity', $recentContactId, $recentContactDisplay, $recentOther); } } // reset the group contact cache since smart groups might be affected due to this CRM_Contact_BAO_GroupContactCache::remove(); if (!empty($params['id'])) { CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity); } else { CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity); } // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916) $matches = array(); if (preg_match('/\\[case #([0-9a-h]{7})\\]/', CRM_Utils_Array::value('subject', $params), $matches)) { $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY); $hash = $matches[1]; $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('{$key}', id)), 1, 7) = '{$hash}'"; $caseParams = array('activity_id' => $activity->id, 'case_id' => CRM_Core_DAO::singleValueQuery($query)); if ($caseParams['case_id']) { CRM_Case_BAO_Case::processCaseActivity($caseParams); } else { self::logActivityAction($activity, "unknown case hash encountered: {$hash}"); } } return $result; }
/** * note that $job_id is used only as a variable in the temp table construction * and does not play a role in the queries generated * @param int $job_id * (misnomer) a nonce value used to name temporary tables. * @param int $mailing_id * @param bool $storeRecipients * @param bool $dedupeEmail * @param null $mode * * @return CRM_Mailing_Event_BAO_Queue|string */ public static function getRecipients($job_id, $mailing_id = NULL, $storeRecipients = FALSE, $dedupeEmail = FALSE, $mode = NULL) { $mailingGroup = new CRM_Mailing_DAO_MailingGroup(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_MailingJob::getTableName(); $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); $eq = CRM_Mailing_Event_DAO_Queue::getTableName(); $email = CRM_Core_DAO_Email::getTableName(); if ($mode == 'sms') { $phone = CRM_Core_DAO_Phone::getTableName(); } $contact = CRM_Contact_DAO_Contact::getTableName(); $group = CRM_Contact_DAO_Group::getTableName(); $g2contact = CRM_Contact_DAO_GroupContact::getTableName(); $m = new CRM_Mailing_DAO_Mailing(); $m->id = $mailing_id; $m->find(TRUE); $email_selection_method = $m->email_selection_method; $location_type_id = $m->location_type_id; // Note: When determining the ORDER that results are returned, it's // the record that comes last that counts. That's because we are // INSERT'ing INTO a table with a primary id so that last record // over writes any previous record. switch ($email_selection_method) { case 'location-exclude': $location_filter = "({$email}.location_type_id != {$location_type_id})"; // If there is more than one email that doesn't match the location, // prefer the one marked is_bulkmail, followed by is_primary. $order_by = "ORDER BY {$email}.is_bulkmail, {$email}.is_primary"; break; case 'location-only': $location_filter = "({$email}.location_type_id = {$location_type_id})"; // If there is more than one email of the desired location, prefer // the one marked is_bulkmail, followed by is_primary. $order_by = "ORDER BY {$email}.is_bulkmail, {$email}.is_primary"; break; case 'location-prefer': $location_filter = "({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1 OR {$email}.location_type_id = {$location_type_id})"; // ORDER BY is more complicated because we have to set an arbitrary // order that prefers the location that we want. We do that using // the FIELD function. For more info, see: // https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field // We assign the location type we want the value "1" by putting it // in the first position after we name the field. All other location // types are left out, so they will be assigned the value 0. That // means, they will all be equally tied for first place, with our // location being last. $order_by = "ORDER BY FIELD({$email}.location_type_id, {$location_type_id}), {$email}.is_bulkmail, {$email}.is_primary"; break; case 'automatic': // fall through to default // fall through to default default: $location_filter = "({$email}.is_bulkmail = 1 OR {$email}.is_primary = 1)"; $order_by = "ORDER BY {$email}.is_bulkmail"; } /* 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 DISTINCT {$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 = {$mailing_id}\n AND {$g2contact}.status = 'Added'\n AND {$mg}.group_type = 'Exclude'"; $mailingGroup->query($excludeSubGroup); /* Add all unsubscribe members of base group from this mailing to the temp * table */ $unSubscribeBaseGroup = "INSERT INTO X_{$job_id} (contact_id)\n SELECT DISTINCT {$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 = {$mailing_id}\n AND {$g2contact}.status = 'Removed'\n AND {$mg}.group_type = 'Base'"; $mailingGroup->query($unSubscribeBaseGroup); /* 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 DISTINCT {$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 = {$mailing_id}\n AND {$mg}.group_type = 'Exclude'"; $mailingGroup->query($excludeSubMailing); // get all the saved searches AND hierarchical groups // and load them in the cache $sql = "\nSELECT {$group}.id, {$group}.cache_date, {$group}.saved_search_id, {$group}.children\nFROM {$group}\nINNER JOIN {$mg} ON {$mg}.entity_id = {$group}.id\nWHERE {$mg}.entity_table = '{$group}'\n AND {$mg}.group_type = 'Exclude'\n AND {$mg}.mailing_id = {$mailing_id}\n AND ( saved_search_id != 0\n OR saved_search_id IS NOT NULL\n OR children IS NOT NULL )\n"; $groupDAO = CRM_Core_DAO::executeQuery($sql); while ($groupDAO->fetch()) { if ($groupDAO->cache_date == NULL) { CRM_Contact_BAO_GroupContactCache::load($groupDAO); } $smartGroupExclude = "\nINSERT IGNORE INTO X_{$job_id} (contact_id)\nSELECT c.contact_id\nFROM civicrm_group_contact_cache c\nWHERE c.group_id = {$groupDAO->id}\n"; $mailingGroup->query($smartGroupExclude); } $tempColumn = 'email_id'; if ($mode == 'sms') { $tempColumn = 'phone_id'; } /* Get all the group contacts we want to include */ $mailingGroup->query("CREATE TEMPORARY TABLE I_{$job_id}\n ({$tempColumn} int, contact_id int primary key)\n ENGINE=HEAP"); /* Get the group contacts, but only those which are not in the * exclusion temp table */ $query = "REPLACE INTO I_{$job_id} (email_id, contact_id)\n\n SELECT DISTINCT {$email}.id as email_id,\n {$contact}.id as contact_id\n FROM {$email}\n INNER JOIN {$contact}\n ON {$email}.contact_id = {$contact}.id\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 {$mg}.search_id IS NULL\n AND {$g2contact}.status = 'Added'\n AND {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$contact}.is_deceased <> 1\n AND {$location_filter}\n AND {$email}.email IS NOT NULL\n AND {$email}.email != ''\n AND {$email}.on_hold = 0\n AND {$mg}.mailing_id = {$mailing_id}\n AND X_{$job_id}.contact_id IS null\n {$order_by}"; if ($mode == 'sms') { $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); $query = "REPLACE INTO I_{$job_id} (phone_id, contact_id)\n\n SELECT DISTINCT {$phone}.id as phone_id,\n {$contact}.id as contact_id\n FROM {$phone}\n INNER JOIN {$contact}\n ON {$phone}.contact_id = {$contact}.id\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 {$mg}.search_id IS NULL\n AND {$g2contact}.status = 'Added'\n AND {$contact}.do_not_sms = 0\n AND {$contact}.is_opt_out = 0\n AND {$contact}.is_deceased <> 1\n AND {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n AND {$phone}.phone IS NOT NULL\n AND {$phone}.phone != ''\n AND {$mg}.mailing_id = {$mailing_id}\n AND X_{$job_id}.contact_id IS null"; } $mailingGroup->query($query); /* Query prior mailings */ $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 {$contact}\n ON {$email}.contact_id = {$contact}.id\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 {$contact}.is_deceased <> 1\n AND {$location_filter}\n AND {$email}.on_hold = 0\n AND {$mg}.mailing_id = {$mailing_id}\n AND X_{$job_id}.contact_id IS null\n {$order_by}"; if ($mode == 'sms') { $query = "REPLACE INTO I_{$job_id} (phone_id, contact_id)\n SELECT DISTINCT {$phone}.id as phone_id,\n {$contact}.id as contact_id\n FROM {$phone}\n INNER JOIN {$contact}\n ON {$phone}.contact_id = {$contact}.id\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_sms = 0\n AND {$contact}.is_opt_out = 0\n AND {$contact}.is_deceased <> 1\n AND {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n AND {$mg}.mailing_id = {$mailing_id}\n AND X_{$job_id}.contact_id IS null"; } $mailingGroup->query($query); $sql = "\nSELECT {$group}.id, {$group}.cache_date, {$group}.saved_search_id, {$group}.children\nFROM {$group}\nINNER JOIN {$mg} ON {$mg}.entity_id = {$group}.id\nWHERE {$mg}.entity_table = '{$group}'\n AND {$mg}.group_type = 'Include'\n AND {$mg}.search_id IS NULL\n AND {$mg}.mailing_id = {$mailing_id}\n AND ( saved_search_id != 0\n OR saved_search_id IS NOT NULL\n OR children IS NOT NULL )\n"; $groupDAO = CRM_Core_DAO::executeQuery($sql); while ($groupDAO->fetch()) { if ($groupDAO->cache_date == NULL) { CRM_Contact_BAO_GroupContactCache::load($groupDAO); } $smartGroupInclude = "\nREPLACE INTO I_{$job_id} (email_id, contact_id)\nSELECT civicrm_email.id as email_id, c.id as contact_id\nFROM civicrm_contact c\nINNER JOIN civicrm_email ON civicrm_email.contact_id = c.id\nINNER JOIN civicrm_group_contact_cache gc ON gc.contact_id = c.id\nLEFT JOIN X_{$job_id} ON X_{$job_id}.contact_id = c.id\nWHERE gc.group_id = {$groupDAO->id}\n AND c.do_not_email = 0\n AND c.is_opt_out = 0\n AND c.is_deceased <> 1\n AND {$location_filter}\n AND civicrm_email.on_hold = 0\n AND X_{$job_id}.contact_id IS null\n{$order_by}\n"; if ($mode == 'sms') { $smartGroupInclude = "\nREPLACE INTO I_{$job_id} (phone_id, contact_id)\nSELECT p.id as phone_id, c.id as contact_id\nFROM civicrm_contact c\nINNER JOIN civicrm_phone p ON p.contact_id = c.id\nINNER JOIN civicrm_group_contact_cache gc ON gc.contact_id = c.id\nLEFT JOIN X_{$job_id} ON X_{$job_id}.contact_id = c.id\nWHERE gc.group_id = {$groupDAO->id}\n AND c.do_not_sms = 0\n AND c.is_opt_out = 0\n AND c.is_deceased <> 1\n AND p.phone_type_id = {$phoneTypes['Mobile']}\n AND X_{$job_id}.contact_id IS null"; } $mailingGroup->query($smartGroupInclude); } /** * Construct the filtered search queries */ $query = "\nSELECT search_id, search_args, entity_id\nFROM {$mg}\nWHERE {$mg}.search_id IS NOT NULL\nAND {$mg}.mailing_id = {$mailing_id}\n"; $dao = CRM_Core_DAO::executeQuery($query); while ($dao->fetch()) { $customSQL = CRM_Contact_BAO_SearchCustom::civiMailSQL($dao->search_id, $dao->search_args, $dao->entity_id); $query = "REPLACE INTO I_{$job_id} ({$tempColumn}, contact_id)\n {$customSQL}"; $mailingGroup->query($query); } /* Get the emails with only location override */ $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 {$contact}\n ON {$email}.contact_id = {$contact}.id\n INNER JOIN {$g2contact}\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 {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$contact}.is_deceased <> 1\n AND {$location_filter}\n AND {$email}.on_hold = 0\n AND {$mg}.mailing_id = {$mailing_id}\n AND X_{$job_id}.contact_id IS null\n {$order_by}"; if ($mode == "sms") { $query = "REPLACE INTO I_{$job_id} (phone_id, contact_id)\n SELECT DISTINCT {$phone}.id as phone_id,\n {$contact}.id as contact_id\n FROM {$phone}\n INNER JOIN {$contact}\n ON {$phone}.contact_id = {$contact}.id\n INNER JOIN {$g2contact}\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 {$contact}.do_not_sms = 0\n AND {$contact}.is_opt_out = 0\n AND {$contact}.is_deceased <> 1\n AND {$phone}.phone_type_id = {$phoneTypes['Mobile']}\n AND {$mg}.mailing_id = {$mailing_id}\n AND X_{$job_id}.contact_id IS null"; } $mailingGroup->query($query); $eq = new CRM_Mailing_Event_BAO_Queue(); list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause(); $aclWhere = $aclWhere ? "WHERE {$aclWhere}" : ''; if ($storeRecipients && $mailing_id) { $sql = "\nDELETE\nFROM civicrm_mailing_recipients\nWHERE mailing_id = %1\n"; $params = array(1 => array($mailing_id, 'Integer')); CRM_Core_DAO::executeQuery($sql, $params); // CRM-3975 $groupBy = $groupJoin = ''; if ($dedupeEmail) { $groupJoin = " INNER JOIN civicrm_email e ON e.id = i.email_id"; $groupBy = " GROUP BY e.email "; } $sql = "\nINSERT INTO civicrm_mailing_recipients ( mailing_id, contact_id, {$tempColumn} )\nSELECT %1, i.contact_id, i.{$tempColumn}\nFROM civicrm_contact contact_a\nINNER JOIN I_{$job_id} i ON contact_a.id = i.contact_id\n {$groupJoin}\n {$aclFrom}\n {$aclWhere}\n {$groupBy}\nORDER BY i.contact_id, i.{$tempColumn}\n"; CRM_Core_DAO::executeQuery($sql, $params); // if we need to add all emails marked bulk, do it as a post filter // on the mailing recipients table if (CRM_Core_BAO_Email::isMultipleBulkMail()) { self::addMultipleEmails($mailing_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; }
/** * takes an associative array and creates a contribution object * * the function extract all the params it needs to initialize the create a * contribution object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Contribute_BAO_Contribution object * @access public * @static */ static function add(&$params, &$ids) { if (empty($params)) { return; } $duplicates = array(); if (self::checkDuplicate($params, $duplicates, CRM_Utils_Array::value('contribution', $ids))) { $error =& CRM_Core_Error::singleton(); $d = implode(', ', $duplicates); $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', array($d), "Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: {$d}"); return $error; } // first clean up all the money fields $moneyFields = array('total_amount', 'net_amount', 'fee_amount', 'non_deductible_amount'); //if priceset is used, no need to cleanup money if (CRM_UTils_Array::value('skipCleanMoney', $params)) { unset($moneyFields[0]); } foreach ($moneyFields as $field) { if (isset($params[$field])) { $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); } } if (CRM_Utils_Array::value('payment_instrument_id', $params)) { require_once 'CRM/Contribute/PseudoConstant.php'; $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name'); if ($params['payment_instrument_id'] != array_search('Check', $paymentInstruments)) { $params['check_number'] = 'null'; } } require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('contribution', $ids)) { CRM_Utils_Hook::pre('edit', 'Contribution', $ids['contribution'], $params); } else { CRM_Utils_Hook::pre('create', 'Contribution', null, $params); } $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->copyValues($params); $contribution->id = CRM_Utils_Array::value('contribution', $ids); // also add financial_trxn details as part of fix for CRM-4724 $contribution->trxn_result_code = CRM_Utils_Array::value('trxn_result_code', $params); $contribution->payment_processor = CRM_Utils_Array::value('payment_processor', $params); require_once 'CRM/Utils/Rule.php'; if (!CRM_Utils_Rule::currencyCode($contribution->currency)) { require_once 'CRM/Core/Config.php'; $config = CRM_Core_Config::singleton(); $contribution->currency = $config->defaultCurrency; } $result = $contribution->save(); // reset the group contact cache for this group require_once 'CRM/Contact/BAO/GroupContactCache.php'; CRM_Contact_BAO_GroupContactCache::remove(); if (CRM_Utils_Array::value('contribution', $ids)) { CRM_Utils_Hook::post('edit', 'Contribution', $contribution->id, $contribution); } else { CRM_Utils_Hook::post('create', 'Contribution', $contribution->id, $contribution); } return $result; }
/** * Create a new group * * @param array $params Associative array of parameters * @return object|null The new group BAO (if created) * @access public * @static */ public static function &create(&$params) { require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('id', $params)) { CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params); } else { CRM_Utils_Hook::pre('create', 'Group', null, $params); } // form the name only if missing: CRM-627 if (!CRM_Utils_Array::value('name', $params)) { require_once 'CRM/Utils/String.php'; $params['name'] = CRM_Utils_String::titleToVar($params['title']); } // convert params if array type if (isset($params['group_type'])) { if (is_array($params['group_type'])) { $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR; } } else { $params['group_type'] = ''; } $group =& new CRM_Contact_BAO_Group(); $group->copyValues($params); $group->save(); if (!$group->id) { return null; } $group->buildClause(); $group->save(); // add custom field values if (CRM_Utils_Array::value('custom', $params)) { require_once 'CRM/Core/BAO/CustomValueTable.php'; CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id); } // make the group, child of domain/site group by default. require_once 'CRM/Contact/BAO/GroupContactCache.php'; require_once 'CRM/Core/BAO/Domain.php'; require_once 'CRM/Contact/BAO/GroupNesting.php'; $domainGroupID = CRM_Core_BAO_Domain::getGroupId(); if (CRM_Utils_Array::value('no_parent', $params) !== 1) { if (defined('CIVICRM_MULTISITE') && CIVICRM_MULTISITE && empty($params['parents']) && $domainGroupID != $group->id && !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id)) { // if no parent present and the group doesn't already have any parents, // make sure site group goes as parent $params['parents'] = array($domainGroupID => 1); } else { if (!is_array($params['parents'])) { $params['parents'] = array($params['parents'] => 1); } } foreach ($params['parents'] as $parentId => $dnc) { if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) { CRM_Contact_BAO_GroupNesting::add($parentId, $group->id); } } // clear any descendant groups cache if exists require_once 'CRM/Core/BAO/Cache.php'; $finalGroups =& CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org'); // this is always required, since we don't know when a // parent group is removed require_once 'CRM/Contact/BAO/GroupNestingCache.php'; CRM_Contact_BAO_GroupNestingCache::update(); // update group contact cache for all parent groups $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id); foreach ($parentIds as $parentId) { CRM_Contact_BAO_GroupContactCache::add($parentId); } } if (CRM_Utils_Array::value('organization_id', $params)) { require_once 'CRM/Contact/BAO/GroupOrganization.php'; $groupOrg = array(); $groupOrg = $params; $groupOrg['group_id'] = $group->id; CRM_Contact_BAO_GroupOrganization::add($groupOrg); } CRM_Contact_BAO_GroupContactCache::add($group->id); if (CRM_Utils_Array::value('id', $params)) { CRM_Utils_Hook::post('edit', 'Group', $group->id, $group); } else { CRM_Utils_Hook::post('create', 'Group', $group->id, $group); } return $group; }
/** * Takes an associative array and creates a membership object. * * the function extracts all the params it needs to initialize the created * membership object. The params array could contain additional unused name/value * pairs * * @param array $params * (reference ) an assoc array of name/value pairs. * @param array $ids * The array that holds all the db ids. * * @return CRM_Member_BAO_Membership */ public static function add(&$params, $ids = array()) { $oldStatus = $oldType = NULL; $params['id'] = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membership', $ids)); if ($params['id']) { CRM_Utils_Hook::pre('edit', 'Membership', $params['id'], $params); } else { CRM_Utils_Hook::pre('create', 'Membership', NULL, $params); } $id = $params['id']; // we do this after the hooks are called in case it has been altered if ($id) { $membershipObj = new CRM_Member_DAO_Membership(); $membershipObj->id = $id; $membershipObj->find(); while ($membershipObj->fetch()) { $oldStatus = $membershipObj->status_id; $oldType = $membershipObj->membership_type_id; } } if (array_key_exists('is_override', $params) && !$params['is_override']) { $params['is_override'] = 'null'; } $membership = new CRM_Member_BAO_Membership(); $membership->copyValues($params); $membership->id = $id; $membership->save(); $membership->free(); if (empty($membership->contact_id) || empty($membership->status_id)) { // this means we are in renewal mode and are just updating the membership // record or this is an API update call and all fields are not present in the update record // however the hooks don't care and want all data CRM-7784 $tempMembership = new CRM_Member_DAO_Membership(); $tempMembership->id = $membership->id; $tempMembership->find(TRUE); $membership = $tempMembership; } //get the log start date. //it is set during renewal of membership. $logStartDate = CRM_Utils_Array::value('log_start_date', $params); $logStartDate = $logStartDate ? CRM_Utils_Date::isoToMysql($logStartDate) : CRM_Utils_Date::isoToMysql($membership->start_date); $values = self::getStatusANDTypeValues($membership->id); $membershipLog = array('membership_id' => $membership->id, 'status_id' => $membership->status_id, 'start_date' => $logStartDate, 'end_date' => CRM_Utils_Date::isoToMysql($membership->end_date), 'modified_date' => date('Ymd'), 'membership_type_id' => $values[$membership->id]['membership_type_id'], 'max_related' => $membership->max_related); $session = CRM_Core_Session::singleton(); // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id if ($session->get('userID')) { $membershipLog['modified_id'] = $session->get('userID'); } elseif (!empty($ids['userId'])) { $membershipLog['modified_id'] = $ids['userId']; } else { $membershipLog['modified_id'] = $membership->contact_id; } CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray); // reset the group contact cache since smart groups might be affected due to this CRM_Contact_BAO_GroupContactCache::remove(); if ($id) { if ($membership->status_id != $oldStatus) { $allStatus = CRM_Member_BAO_Membership::buildOptions('status_id', 'get'); $activityParam = array('subject' => "Status changed from {$allStatus[$oldStatus]} to {$allStatus[$membership->status_id]}", 'source_contact_id' => $membershipLog['modified_id'], 'target_contact_id' => $membership->contact_id, 'source_record_id' => $membership->id, 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Status'), 'status_id' => 2, 'priority_id' => 2, 'activity_date_time' => date('Y-m-d H:i:s')); civicrm_api3('activity', 'create', $activityParam); } if (isset($membership->membership_type_id) && $membership->membership_type_id != $oldType) { $membershipTypes = CRM_Member_BAO_Membership::buildOptions('membership_type_id', 'get'); $activityParam = array('subject' => "Type changed from {$membershipTypes[$oldType]} to {$membershipTypes[$membership->membership_type_id]}", 'source_contact_id' => $membershipLog['modified_id'], 'target_contact_id' => $membership->contact_id, 'source_record_id' => $membership->id, 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Type'), 'status_id' => 2, 'priority_id' => 2, 'activity_date_time' => date('Y-m-d H:i:s')); civicrm_api3('activity', 'create', $activityParam); } CRM_Utils_Hook::post('edit', 'Membership', $membership->id, $membership); } else { CRM_Utils_Hook::post('create', 'Membership', $membership->id, $membership); } return $membership; }
/** * Function to delete custom value * */ function deleteCustomValue(&$config) { $customValueID = CRM_Utils_Type::escape($_POST['valueID'], 'Positive'); $customGroupID = CRM_Utils_Type::escape($_POST['groupID'], 'Positive'); require_once "CRM/Core/BAO/CustomValue.php"; CRM_Core_BAO_CustomValue::deleteCustomValue($customValueID, $customGroupID); if ($contactId = CRM_Utils_Array::value('contactId', $_POST)) { require_once 'CRM/Contact/BAO/Contact.php'; echo CRM_Contact_BAO_Contact::getCountComponent('custom_' . $_POST['groupID'], $contactId); } // reset the group contact cache for this group require_once 'CRM/Contact/BAO/GroupContactCache.php'; CRM_Contact_BAO_GroupContactCache::remove(); }