/** * Get all of the ACLs for a contact through ACL groups owned by Contact * groups. * * @param int $contact_id - ID of a contact to search for * * @return array - Array of assoc. arrays of ACL rules * @access public * @static */ public static function &getGroupACLRoles($contact_id) { $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); $rule = new CRM_ACL_BAO_ACL(); $acl = self::getTableName(); $aclRole = 'civicrm_acl_role'; $aclER = CRM_ACL_DAO_EntityRole::getTableName(); $c2g = CRM_Contact_BAO_GroupContact::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $query = " SELECT {$acl}.*\n FROM {$acl}\n INNER JOIN civicrm_option_group og\n ON og.name = 'acl_role'\n INNER JOIN civicrm_option_value ov\n ON {$acl}.entity_table = '{$aclRole}'\n AND ov.option_group_id = og.id\n AND {$acl}.entity_id = ov.value\n AND ov.is_active = 1\n INNER JOIN {$aclER}\n ON {$aclER}.acl_role_id = {$acl}.entity_id\n AND {$aclER}.is_active = 1\n INNER JOIN {$c2g}\n ON {$aclER}.entity_id = {$c2g}.group_id\n AND {$aclER}.entity_table = 'civicrm_group'\n WHERE {$acl}.entity_table = '{$aclRole}'\n AND {$acl}.is_active = 1\n AND {$c2g}.contact_id = {$contact_id}\n AND {$c2g}.status = 'Added'"; $results = array(); $rule->query($query); while ($rule->fetch()) { $results[$rule->id] =& $rule->toArray(); } // also get all acls for "Any Role" case // and authenticated User Role if present $roles = "0"; $session = CRM_Core_Session::singleton(); if ($session->get('ufID') > 0) { $roles .= ",2"; } $query = "\nSELECT {$acl}.*\n FROM {$acl}\n WHERE {$acl}.entity_id IN ( {$roles} )\n AND {$acl}.entity_table = 'civicrm_acl_role'\n"; $rule->query($query); while ($rule->fetch()) { $results[$rule->id] =& $rule->toArray(); } return $results; }
/** * Unsubscribe a contact from all groups that received this mailing * * @param int $job_id The job ID * @param int $queue_id The Queue Event ID of the recipient * @param string $hash The hash * @param boolean $return If true return the list of groups. * @return array|null $groups Array of all groups from which the contact was removed, or null if the queue event could not be found. * @access public * @static */ public static function &unsub_from_mailing($job_id, $queue_id, $hash, $return = false) { /* First make sure there's a matching queue event */ $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { return null; } $contact_id = $q->contact_id; require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); $do =& new CRM_Core_DAO(); $mg = CRM_Mailing_DAO_Group::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $gc = CRM_Contact_BAO_GroupContact::getTableName(); $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n INNER JOIN {$job}\n ON {$job}.mailing_id = {$mg}.mailing_id\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n AND {$mg}.group_type = 'Include'"); /* Make a list of groups and a list of prior mailings that received * this mailing */ $groups = array(); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { //$groups[$do->entity_id] = true; $groups[$do->entity_id] = null; } else { if ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } /* As long as we have prior mailings, find their groups and add to the * list */ while (!empty($mailings)) { $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n WHERE {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n AND {$mg}.group_type = 'Include'"); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = true; } else { if ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } } /* Now we have a complete list of recipient groups. Filter out all * those except smart groups and those that the contact belongs to */ $do->query("\n SELECT {$group}.id as group_id,\n {$group}.title as title,\n {$group}.description as description\n FROM {$group}\n LEFT JOIN {$gc}\n ON {$gc}.group_id = {$group}.id\n WHERE {$group}.id IN (" . implode(', ', array_keys($groups)) . ")\n AND ({$group}.saved_search_id is not null\n OR ({$gc}.contact_id = {$contact_id}\n AND {$gc}.status = 'Added')\n )"); if ($return) { while ($do->fetch()) { $groups[$do->group_id] = array('title' => $do->title, 'description' => $do->description); } return $groups; } else { while ($do->fetch()) { $groups[$do->group_id] = $do->title; } } $contacts = array($contact_id); foreach ($groups as $group_id => $group_name) { $notremoved = false; if ($group_name) { list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contacts, $group_id, 'Email'); } if ($notremoved) { unset($groups[$group_id]); } } $ue =& new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 0; $ue->time_stamp = date('YmdHis'); $ue->save(); $transaction->commit(); return $groups; }
/** * Returns array of contacts who are members of the specified group. * * @param CRM_Contact $group A valid group object (passed by reference) * @param array $returnProperties Which properties * should be included in the returned Contact object(s). If NULL, * the default set of contact properties will be * included. group_contact properties (such as 'status', * ' in_date', etc.) are included automatically.Note:Do not inclue * Id releted properties. * @param text $status A valid status value ('Added', 'Pending', 'Removed'). * @param text $sort Associative array of * one or more "property_name"=>"sort direction" * pairs which will control order of Contact objects returned. * @param Int $offset Starting row index. * @param Int $row_count Maximum number of rows to returns. * * * @return $contactArray Array of contacts who are members of the specified group * * @access public */ function getGroupContacts(&$group, $returnProperties = null, $status = 'Added', $sort = null, $offset = null, $row_count = null) { $query = "SELECT * FROM civicrm_group WHERE id = " . CRM_Utils_Type::escape($group->id, 'Integer'); $groupDAO =& new CRM_Contact_DAO_Group(); $groupDAO->id = $group->id; if (!$groupDAO->find(true)) { return CRM_Core_Error::createError("Could not locate group with id: {$id}"); } // make sure user has got permission to view this group if (!CRM_Contact_BAO_Group::checkPermission($groupDAO->id, $groupDAO->title)) { return CRM_Core_Error::createError("You do not have permission to access group with id: {$id}"); } $query = ''; if (empty($returnProperties)) { $query = "SELECT civicrm_contact.id as contact_id,\n civicrm_email.email as email"; //$query = "SELECT *,civicrm_contact.id as contact_id, (talk to lobo before re-enabling this) //civicrm_email.email as email"; } else { $query = "SELECT civicrm_contact.id as contact_id ,"; $query .= implode(',', $returnProperties); } $fv = array('group' => array($group->id => true)); if ($status) { $fv['group_contact_status'] = array($status => true); } else { $fv['group_contact_status'] = array('Added' => true, 'Removed' => true, 'Pending' => true); } $tables = array(CRM_Contact_BAO_GroupContact::getTableName() => true, CRM_Core_BAO_Email::getTableName() => true, CRM_Contact_BAO_Contact::getTableName() => true, CRM_Contact_BAO_Group::getTableName() => true); $inner = array(); $whereTables = array(); $where = CRM_Contact_BAO_Query::getWhereClause($fv, null, $tables, $whereTables); $permission = CRM_Core_Permission::whereClause(CRM_CORE_PERMISSION_VIEW, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables, $inner); $query .= " {$from} WHERE {$permission} AND {$where} "; if ($sort != null) { $order = array(); foreach ($sort as $key => $direction) { $order[] = " {$key} {$direction} "; } $query .= " ORDER BY " . implode(',', $order); } if ($offset != null && $row_count != null) { $query .= " LIMIT {$offset}, {$row_count}"; } // CRM_Core_Error::debug( 'q', $query ); $dao =& new CRM_Contact_DAO_Contact(); $dao->query($query); // this is quite inefficient, we need to change the return // values in docs $contactArray = array(); while ($dao->fetch()) { $contactArray[] = clone $dao; } return $contactArray; }
/** * Unsubscribe a contact from all groups that received this mailing. * * @param int $job_id * The job ID. * @param int $queue_id * The Queue Event ID of the recipient. * @param string $hash * The hash. * @param bool $return * If true return the list of groups. * * @return array|null * $groups Array of all groups from which the contact was removed, or null if the queue event could not be found. */ public static function &unsub_from_mailing($job_id, $queue_id, $hash, $return = FALSE) { // First make sure there's a matching queue event. $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); $success = NULL; if (!$q) { return $success; } $contact_id = $q->contact_id; $transaction = new CRM_Core_Transaction(); $do = new CRM_Core_DAO(); $mgObject = new CRM_Mailing_DAO_MailingGroup(); $mg = $mgObject->getTableName(); $jobObject = new CRM_Mailing_BAO_MailingJob(); $job = $jobObject->getTableName(); $mailingObject = new CRM_Mailing_BAO_Mailing(); $mailing = $mailingObject->getTableName(); $groupObject = new CRM_Contact_BAO_Group(); $group = $groupObject->getTableName(); $gcObject = new CRM_Contact_BAO_GroupContact(); $gc = $gcObject->getTableName(); //We Need the mailing Id for the hook... $do->query("SELECT {$job}.mailing_id as mailing_id\n FROM {$job}\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); $do->fetch(); $mailing_id = $do->mailing_id; $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id,\n {$mg}.group_type as group_type\n FROM {$mg}\n INNER JOIN {$job}\n ON {$job}.mailing_id = {$mg}.mailing_id\n INNER JOIN {$group}\n ON {$mg}.entity_id = {$group}.id\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n AND {$mg}.group_type IN ('Include', 'Base')\n AND {$group}.is_hidden = 0"); // Make a list of groups and a list of prior mailings that received // this mailing. $groups = array(); $base_groups = array(); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { if ($do->group_type == 'Base') { $base_groups[$do->entity_id] = NULL; } else { $groups[$do->entity_id] = NULL; } } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } // As long as we have prior mailings, find their groups and add to the // list. while (!empty($mailings)) { $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n WHERE {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n AND {$mg}.group_type = 'Include'"); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = TRUE; } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } //Pass the groups to be unsubscribed from through a hook. $group_ids = array_keys($groups); $base_group_ids = array_keys($base_groups); CRM_Utils_Hook::unsubscribeGroups('unsubscribe', $mailing_id, $contact_id, $group_ids, $base_group_ids); // Now we have a complete list of recipient groups. Filter out all // those except smart groups, those that the contact belongs to and // base groups from search based mailings. $baseGroupClause = ''; if (!empty($base_group_ids)) { $baseGroupClause = "OR {$group}.id IN(" . implode(', ', $base_group_ids) . ")"; } $groupIdClause = ''; if ($group_ids || $base_group_ids) { $groupIdClause = "AND {$group}.id IN (" . implode(', ', array_merge($group_ids, $base_group_ids)) . ")"; } $do->query("\n SELECT {$group}.id as group_id,\n {$group}.title as title,\n {$group}.description as description\n FROM {$group}\n LEFT JOIN {$gc}\n ON {$gc}.group_id = {$group}.id\n WHERE {$group}.is_hidden = 0\n {$groupIdClause}\n AND ({$group}.saved_search_id is not null\n OR ({$gc}.contact_id = {$contact_id}\n AND {$gc}.status = 'Added')\n {$baseGroupClause}\n )"); if ($return) { $returnGroups = array(); while ($do->fetch()) { $returnGroups[$do->group_id] = array('title' => $do->title, 'description' => $do->description); } return $returnGroups; } else { while ($do->fetch()) { $groups[$do->group_id] = $do->title; } } $contacts = array($contact_id); foreach ($groups as $group_id => $group_name) { $notremoved = FALSE; if ($group_name) { if (in_array($group_id, $base_group_ids)) { list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email', 'Removed'); } else { list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contacts, $group_id, 'Email'); } } if ($notremoved) { unset($groups[$group_id]); } } $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 0; $ue->time_stamp = date('YmdHis'); $ue->save(); $transaction->commit(); return $groups; }
/** * Resubscribe a contact to the groups, he/she was unsubscribed from. * * @param int $job_id * The job ID. * @param int $queue_id * The Queue Event ID of the recipient. * @param string $hash * The hash. * * @return array|null * $groups Array of all groups to which the contact was added, or null if the queue event could not be found. */ public static function &resub_to_mailing($job_id, $queue_id, $hash) { // First make sure there's a matching queue event. $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); $success = NULL; if (!$q) { return $success; } // check if this queue_id was actually unsubscribed $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_unsubscribe = 0; if (!$ue->find(TRUE)) { return $success; } $contact_id = $q->contact_id; $transaction = new CRM_Core_Transaction(); $do = new CRM_Core_DAO(); $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); $job = CRM_Mailing_BAO_MailingJob::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $gc = CRM_Contact_BAO_GroupContact::getTableName(); // We Need the mailing Id for the hook... $do->query("SELECT {$job}.mailing_id as mailing_id\n FROM {$job}\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); $do->fetch(); $mailing_id = $do->mailing_id; $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n INNER JOIN {$job}\n ON {$job}.mailing_id = {$mg}.mailing_id\n INNER JOIN {$group}\n ON {$mg}.entity_id = {$group}.id\n WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n AND {$mg}.group_type IN ( 'Include', 'Base' )\n AND {$group}.is_hidden = 0"); // Make a list of groups and a list of prior mailings that received // this mailing. $groups = array(); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = NULL; } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } // As long as we have prior mailings, find their groups and add to the // list. while (!empty($mailings)) { $do->query("\n SELECT {$mg}.entity_table as entity_table,\n {$mg}.entity_id as entity_id\n FROM {$mg}\n WHERE {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n AND {$mg}.group_type = 'Include'"); $mailings = array(); while ($do->fetch()) { if ($do->entity_table == $group) { $groups[$do->entity_id] = TRUE; } elseif ($do->entity_table == $mailing) { $mailings[] = $do->entity_id; } } } $group_ids = array_keys($groups); $base_groups = NULL; CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups); // Now we have a complete list of recipient groups. Filter out all // those except smart groups and those that the contact belongs to. $do->query("\n SELECT {$group}.id as group_id,\n {$group}.title as title\n FROM {$group}\n LEFT JOIN {$gc}\n ON {$gc}.group_id = {$group}.id\n WHERE {$group}.id IN (" . implode(', ', $group_ids) . ")\n AND ({$group}.saved_search_id is not null\n OR ({$gc}.contact_id = {$contact_id}\n AND {$gc}.status = 'Removed')\n )"); while ($do->fetch()) { $groups[$do->group_id] = $do->title; } $contacts = array($contact_id); foreach ($groups as $group_id => $group_name) { $notadded = 0; if ($group_name) { list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email'); } if ($notadded) { unset($groups[$group_id]); } } // remove entry from Unsubscribe table. $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); $ue->event_queue_id = $queue_id; $ue->org_resubscribe = 0; if ($ue->find(TRUE)) { $ue->delete(); } $transaction->commit(); return $groups; }
/** * Get all of the ACLs for a contact through ACL groups owned by Contact * groups. * * @param int $contact_id - ID of a contact to search for * @return array - Array of assoc. arrays of ACL rules * @access public * @static */ function &getGroupACLGroups($contact_id) { $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); $rule =& new CRM_Core_BAO_ACL(); $acl = CRM_Core_BAO_ACL::getTableName(); $aclGroup = CRM_Core_BAO_ACLGroup::getTableName(); $c2g = CRM_Contact_BAO_GroupContact::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $query = " SELECT {$acl}.* \n FROM {$acl}\n INNER JOIN {$aclGroup}\n ON {$acl}.entity_table = '{$aclGroup}'\n AND {$acl}.entity_id = {$aclGroup}.id\n \n INNER JOIN {$c2g}\n ON {$aclGroup}.entity_id = {$c2g}.group_id\n \n WHERE {$aclGroup}.entity_table = '{$group}'\n AND {$aclGroup}.is_active = 1\n AND {$c2g}.contact_id = {$contact_id}\n AND {$c2g}.status = 'Added'"; $results = array(); $rule->query($query); while ($rule->fetch()) { $results[] =& $rule->toArray(); } return $results; }