/** * 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. * @access public * @static */ 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; 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] = 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 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 = '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; }
/** * 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; }