public function postProcess() { $values = $this->exportValues(); // check if EmailTyped matches Email address $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE); $job_id = $this->_job_id; $queue_id = $this->_queue_id; $hash = $this->_hash; $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1"); $this->assign('confirmURL', $confirmURL); $session = CRM_Core_Session::singleton(); $session->pushUserContext($confirmURL); if ($result == TRUE) { // Email address verified if (CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job_id, $queue_id, $hash)) { CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, NULL, TRUE, $job_id); } $statusMsg = ts('Email: %1 has been successfully opted out', array(1 => $values['email_confirm'])); CRM_Core_Session::setStatus($statusMsg, '', 'success'); } elseif ($result == FALSE) { // Email address not verified $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this opt out request.', array(1 => $values['email_confirm'])); CRM_Core_Session::setStatus($statusMsg, '', 'fail'); } }
/** * Unsubscribe from mailing group. * * @param array $params * Array per getfields metadata. * * @return array * Api result array */ function civicrm_api3_mailing_event_unsubscribe_create($params) { $job = $params['job_id']; $queue = $params['event_queue_id']; $hash = $params['hash']; if (empty($params['org_unsubscribe'])) { $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job, $queue, $hash); if (count($groups)) { CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, $groups, FALSE, $job); return civicrm_api3_create_success($params); } } else { $unsubs = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job, $queue, $hash); if (!$unsubs) { return civicrm_api3_create_error('Domain Queue event could not be found'); } CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, NULL, TRUE, $job); return civicrm_api3_create_success($params); } return civicrm_api3_create_error('Queue event could not be found'); }
/** * returns all the rows in the given offset and rowCount * * @param enum $action the action being performed * @param int $offset the row number to start from * @param int $rowCount the number of rows to return * @param string $sort the sql string that describes the sort order * @param enum $output what should the result set include (web/email/csv) * * @return int the total number of rows for this action */ function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { switch ($this->_event_type) { case 'queue': return CRM_Mailing_Event_BAO_Queue::getRows($this->_mailing_id, $this->_job_id, $offset, $rowCount, $sort); break; case 'delivered': return CRM_Mailing_Event_BAO_Delivered::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'opened': return CRM_Mailing_Event_BAO_Opened::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'bounce': return CRM_Mailing_Event_BAO_Bounce::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'forward': return CRM_Mailing_Event_BAO_Forward::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); case 'reply': return CRM_Mailing_Event_BAO_Reply::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'unsubscribe': return CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort, TRUE); break; case 'optout': return CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort, FALSE); break; case 'click': return CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $this->_url_id, $offset, $rowCount, $sort); break; default: return NULL; } }
/** * Generate a report. Fetch event count information, mailing data, and job * status. * * @param int $id * The mailing id to report. * @param bool $skipDetails * Whether return all detailed report. * * @param bool $isSMS * * @return array * Associative array of reporting data */ public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) { $mailing_id = CRM_Utils_Type::escape($id, 'Integer'); $mailing = new CRM_Mailing_BAO_Mailing(); $t = array('mailing' => self::getTableName(), 'mailing_group' => CRM_Mailing_DAO_MailingGroup::getTableName(), 'group' => CRM_Contact_BAO_Group::getTableName(), 'job' => CRM_Mailing_BAO_MailingJob::getTableName(), 'queue' => CRM_Mailing_Event_BAO_Queue::getTableName(), 'delivered' => CRM_Mailing_Event_BAO_Delivered::getTableName(), 'opened' => CRM_Mailing_Event_BAO_Opened::getTableName(), 'reply' => CRM_Mailing_Event_BAO_Reply::getTableName(), 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe::getTableName(), 'bounce' => CRM_Mailing_Event_BAO_Bounce::getTableName(), 'forward' => CRM_Mailing_Event_BAO_Forward::getTableName(), 'url' => CRM_Mailing_BAO_TrackableURL::getTableName(), 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName(), 'component' => CRM_Mailing_BAO_Component::getTableName(), 'spool' => CRM_Mailing_BAO_Spool::getTableName()); $report = array(); $additionalWhereClause = " AND "; if (!$isSMS) { $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NULL "; } else { $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NOT NULL "; } /* Get the mailing info */ $mailing->query("\n SELECT {$t['mailing']}.*\n FROM {$t['mailing']}\n WHERE {$t['mailing']}.id = {$mailing_id} {$additionalWhereClause}"); $mailing->fetch(); $report['mailing'] = array(); foreach (array_keys(self::fields()) as $field) { $report['mailing'][$field] = $mailing->{$field}; } //get the campaign if ($campaignId = CRM_Utils_Array::value('campaign_id', $report['mailing'])) { $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); $report['mailing']['campaign'] = $campaigns[$campaignId]; } //mailing report is called by activity //we dont need all detail report if ($skipDetails) { return $report; } /* Get the component info */ $query = array(); $components = array('header' => ts('Header'), 'footer' => ts('Footer'), 'reply' => ts('Reply'), 'unsubscribe' => ts('Unsubscribe'), 'optout' => ts('Opt-Out')); foreach (array_keys($components) as $type) { $query[] = "SELECT {$t['component']}.name as name,\n '{$type}' as type,\n {$t['component']}.id as id\n FROM {$t['component']}\n INNER JOIN {$t['mailing']}\n ON {$t['mailing']}.{$type}_id =\n {$t['component']}.id\n WHERE {$t['mailing']}.id = {$mailing_id}"; } $q = '(' . implode(') UNION (', $query) . ')'; $mailing->query($q); $report['component'] = array(); while ($mailing->fetch()) { $report['component'][] = array('type' => $components[$mailing->type], 'name' => $mailing->name, 'link' => CRM_Utils_System::url('civicrm/mailing/component', "reset=1&action=update&id={$mailing->id}")); } /* Get the recipient group info */ $mailing->query("\n SELECT {$t['mailing_group']}.group_type as group_type,\n {$t['group']}.id as group_id,\n {$t['group']}.title as group_title,\n {$t['group']}.is_hidden as group_hidden,\n {$t['mailing']}.id as mailing_id,\n {$t['mailing']}.name as mailing_name\n FROM {$t['mailing_group']}\n LEFT JOIN {$t['group']}\n ON {$t['mailing_group']}.entity_id = {$t['group']}.id\n AND {$t['mailing_group']}.entity_table =\n '{$t['group']}'\n LEFT JOIN {$t['mailing']}\n ON {$t['mailing_group']}.entity_id =\n {$t['mailing']}.id\n AND {$t['mailing_group']}.entity_table =\n '{$t['mailing']}'\n\n WHERE {$t['mailing_group']}.mailing_id = {$mailing_id}\n "); $report['group'] = array('include' => array(), 'exclude' => array(), 'base' => array()); while ($mailing->fetch()) { $row = array(); if (isset($mailing->group_id)) { $row['id'] = $mailing->group_id; $row['name'] = $mailing->group_title; $row['link'] = CRM_Utils_System::url('civicrm/group/search', "reset=1&force=1&context=smog&gid={$row['id']}"); } else { $row['id'] = $mailing->mailing_id; $row['name'] = $mailing->mailing_name; $row['mailing'] = TRUE; $row['link'] = CRM_Utils_System::url('civicrm/mailing/report', "mid={$row['id']}"); } /* Rename hidden groups */ if ($mailing->group_hidden == 1) { $row['name'] = "Search Results"; } if ($mailing->group_type == 'Include') { $report['group']['include'][] = $row; } elseif ($mailing->group_type == 'Base') { $report['group']['base'][] = $row; } else { $report['group']['exclude'][] = $row; } } /* Get the event totals, grouped by job (retries) */ $mailing->query("\n SELECT {$t['job']}.*,\n COUNT(DISTINCT {$t['queue']}.id) as queue,\n COUNT(DISTINCT {$t['delivered']}.id) as delivered,\n COUNT(DISTINCT {$t['reply']}.id) as reply,\n COUNT(DISTINCT {$t['forward']}.id) as forward,\n COUNT(DISTINCT {$t['bounce']}.id) as bounce,\n COUNT(DISTINCT {$t['urlopen']}.id) as url,\n COUNT(DISTINCT {$t['spool']}.id) as spool\n FROM {$t['job']}\n LEFT JOIN {$t['queue']}\n ON {$t['queue']}.job_id = {$t['job']}.id\n LEFT JOIN {$t['reply']}\n ON {$t['reply']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['forward']}\n ON {$t['forward']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['bounce']}\n ON {$t['bounce']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['delivered']}\n ON {$t['delivered']}.event_queue_id = {$t['queue']}.id\n AND {$t['bounce']}.id IS null\n LEFT JOIN {$t['urlopen']}\n ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['spool']}\n ON {$t['spool']}.job_id = {$t['job']}.id\n WHERE {$t['job']}.mailing_id = {$mailing_id}\n AND {$t['job']}.is_test = 0\n GROUP BY {$t['job']}.id"); $report['jobs'] = array(); $report['event_totals'] = array(); $elements = array('queue', 'delivered', 'url', 'forward', 'reply', 'unsubscribe', 'optout', 'opened', 'bounce', 'spool'); // initialize various counters foreach ($elements as $field) { $report['event_totals'][$field] = 0; } while ($mailing->fetch()) { $row = array(); foreach ($elements as $field) { if (isset($mailing->{$field})) { $row[$field] = $mailing->{$field}; $report['event_totals'][$field] += $mailing->{$field}; } } // compute open total separately to discount duplicates // CRM-1258 $row['opened'] = CRM_Mailing_Event_BAO_Opened::getTotalCount($mailing_id, $mailing->id, TRUE); $report['event_totals']['opened'] += $row['opened']; // compute unsub total separately to discount duplicates // CRM-1783 $row['unsubscribe'] = CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($mailing_id, $mailing->id, TRUE, TRUE); $report['event_totals']['unsubscribe'] += $row['unsubscribe']; $row['optout'] = CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($mailing_id, $mailing->id, TRUE, FALSE); $report['event_totals']['optout'] += $row['optout']; foreach (array_keys(CRM_Mailing_BAO_MailingJob::fields()) as $field) { $row[$field] = $mailing->{$field}; } if ($mailing->queue) { $row['delivered_rate'] = 100.0 * $mailing->delivered / $mailing->queue; $row['bounce_rate'] = 100.0 * $mailing->bounce / $mailing->queue; $row['unsubscribe_rate'] = 100.0 * $row['unsubscribe'] / $mailing->queue; $row['optout_rate'] = 100.0 * $row['optout'] / $mailing->queue; } else { $row['delivered_rate'] = 0; $row['bounce_rate'] = 0; $row['unsubscribe_rate'] = 0; $row['optout_rate'] = 0; } $row['links'] = array('clicks' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&jid={$mailing->id}"), 'queue' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=queue&mid={$mailing_id}&jid={$mailing->id}"), 'delivered' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=delivered&mid={$mailing_id}&jid={$mailing->id}"), 'bounce' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=bounce&mid={$mailing_id}&jid={$mailing->id}"), 'unsubscribe' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=unsubscribe&mid={$mailing_id}&jid={$mailing->id}"), 'forward' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=forward&mid={$mailing_id}&jid={$mailing->id}"), 'reply' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=reply&mid={$mailing_id}&jid={$mailing->id}"), 'opened' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=opened&mid={$mailing_id}&jid={$mailing->id}")); foreach (array('scheduled_date', 'start_date', 'end_date') as $key) { $row[$key] = CRM_Utils_Date::customFormat($row[$key]); } $report['jobs'][] = $row; } $newTableSize = CRM_Mailing_BAO_Recipients::mailingSize($mailing_id); // we need to do this for backward compatibility, since old mailings did not // use the mailing_recipients table if ($newTableSize > 0) { $report['event_totals']['queue'] = $newTableSize; } else { $report['event_totals']['queue'] = self::getRecipientsCount($mailing_id, $mailing_id); } if (!empty($report['event_totals']['queue'])) { $report['event_totals']['delivered_rate'] = 100.0 * $report['event_totals']['delivered'] / $report['event_totals']['queue']; $report['event_totals']['bounce_rate'] = 100.0 * $report['event_totals']['bounce'] / $report['event_totals']['queue']; $report['event_totals']['unsubscribe_rate'] = 100.0 * $report['event_totals']['unsubscribe'] / $report['event_totals']['queue']; $report['event_totals']['optout_rate'] = 100.0 * $report['event_totals']['optout'] / $report['event_totals']['queue']; } else { $report['event_totals']['delivered_rate'] = 0; $report['event_totals']['bounce_rate'] = 0; $report['event_totals']['unsubscribe_rate'] = 0; $report['event_totals']['optout_rate'] = 0; } /* Get the click-through totals, grouped by URL */ $mailing->query("\n SELECT {$t['url']}.url,\n {$t['url']}.id,\n COUNT({$t['urlopen']}.id) as clicks,\n COUNT(DISTINCT {$t['queue']}.id) as unique_clicks\n FROM {$t['url']}\n LEFT JOIN {$t['urlopen']}\n ON {$t['urlopen']}.trackable_url_id = {$t['url']}.id\n LEFT JOIN {$t['queue']}\n ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['job']}\n ON {$t['queue']}.job_id = {$t['job']}.id\n WHERE {$t['url']}.mailing_id = {$mailing_id}\n AND {$t['job']}.is_test = 0\n GROUP BY {$t['url']}.id"); $report['click_through'] = array(); while ($mailing->fetch()) { $report['click_through'][] = array('url' => $mailing->url, 'link' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&uid={$mailing->id}"), 'link_unique' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&uid={$mailing->id}&distinct=1"), 'clicks' => $mailing->clicks, 'unique' => $mailing->unique_clicks, 'rate' => CRM_Utils_Array::value('delivered', $report['event_totals']) ? 100.0 * $mailing->unique_clicks / $report['event_totals']['delivered'] : 0); } $report['event_totals']['links'] = array('clicks' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}"), 'clicks_unique' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&distinct=1"), 'queue' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=queue&mid={$mailing_id}"), 'delivered' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=delivered&mid={$mailing_id}"), 'bounce' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=bounce&mid={$mailing_id}"), 'unsubscribe' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=unsubscribe&mid={$mailing_id}"), 'optout' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=optout&mid={$mailing_id}"), 'forward' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=forward&mid={$mailing_id}"), 'reply' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=reply&mid={$mailing_id}"), 'opened' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=opened&mid={$mailing_id}")); $actionLinks = array(CRM_Core_Action::VIEW => array('name' => ts('Report'))); if (CRM_Core_Permission::check('view all contacts')) { $actionLinks[CRM_Core_Action::ADVANCED] = array('name' => ts('Advanced Search'), 'url' => 'civicrm/contact/search/advanced'); } $action = array_sum(array_keys($actionLinks)); $report['event_totals']['actionlinks'] = array(); foreach (array('clicks', 'clicks_unique', 'queue', 'delivered', 'bounce', 'unsubscribe', 'forward', 'reply', 'opened', 'optout') as $key) { $url = 'mailing/detail'; $reportFilter = "reset=1&mailing_id_value={$mailing_id}"; $searchFilter = "force=1&mailing_id=%%mid%%"; switch ($key) { case 'delivered': $reportFilter .= "&delivery_status_value=successful"; $searchFilter .= "&mailing_delivery_status=Y"; break; case 'bounce': $url = "mailing/bounce"; $searchFilter .= "&mailing_delivery_status=N"; break; case 'forward': $reportFilter .= "&is_forwarded_value=1"; $searchFilter .= "&mailing_forward=1"; break; case 'reply': $reportFilter .= "&is_replied_value=1"; $searchFilter .= "&mailing_reply_status=Y"; break; case 'unsubscribe': $reportFilter .= "&is_unsubscribed_value=1"; $searchFilter .= "&mailing_unsubscribe=1"; break; case 'optout': $reportFilter .= "&is_optout_value=1"; $searchFilter .= "&mailing_optout=1"; break; case 'opened': $url = "mailing/opened"; $searchFilter .= "&mailing_open_status=Y"; break; case 'clicks': case 'clicks_unique': $url = "mailing/clicks"; $searchFilter .= "&mailing_click_status=Y"; break; } $actionLinks[CRM_Core_Action::VIEW]['url'] = CRM_Report_Utils_Report::getNextUrl($url, $reportFilter, FALSE, TRUE); if (array_key_exists(CRM_Core_Action::ADVANCED, $actionLinks)) { $actionLinks[CRM_Core_Action::ADVANCED]['qs'] = $searchFilter; } $report['event_totals']['actionlinks'][$key] = CRM_Core_Action::formLink($actionLinks, $action, array('mid' => $mailing_id), ts('more'), FALSE, 'mailing.report.action', 'Mailing', $mailing_id); } return $report; }
/** * Function which needs to be explained. * * @param array $params * * @return array * @throws \API_Exception */ function civicrm_api3_mailing_stats($params) { civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingJob', array('mailing_id'), FALSE); if ($params['date'] == 'now') { $params['date'] = date('YmdHis'); } else { $params['date'] = CRM_Utils_Date::processDate($params['date'] . ' ' . $params['date_time']); } $stats[$params['mailing_id']] = array(); if (empty($params['job_id'])) { $params['job_id'] = NULL; } foreach (array('Delivered', 'Bounces', 'Unsubscribers', 'Unique Clicks', 'Opened') as $detail) { switch ($detail) { case 'Delivered': $stats[$params['mailing_id']] += array($detail => CRM_Mailing_Event_BAO_Delivered::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date'])); break; case 'Bounces': $stats[$params['mailing_id']] += array($detail => CRM_Mailing_Event_BAO_Bounce::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date'])); break; case 'Unsubscribers': $stats[$params['mailing_id']] += array($detail => CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, NULL, $params['date'])); break; case 'Unique Clicks': $stats[$params['mailing_id']] += array($detail => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, NULL, $params['date'])); break; case 'Opened': $stats[$params['mailing_id']] += array($detail => CRM_Mailing_Event_BAO_Opened::getTotalCount($params['mailing_id'], $params['job_id'], FALSE, $params['date'])); break; } } return civicrm_api3_create_success($stats); }
/** * Run page. * * This includes assigning smarty variables and other page processing. * * @return string * @throws Exception */ public function run() { $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', CRM_Core_DAO::$_nullObject); $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', CRM_Core_DAO::$_nullObject); $hash = CRM_Utils_Request::retrieve('h', 'String', CRM_Core_DAO::$_nullObject); if (!$job_id || !$queue_id || !$hash) { CRM_Core_Error::fatal(ts("Missing input parameters")); } // verify that the three numbers above match $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); if (!$q) { CRM_Core_Error::fatal(ts("There was an error in your request")); } $cancel = CRM_Utils_Request::retrieve("_qf_{$this->_type}_cancel", 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, $_REQUEST); if ($cancel) { $config = CRM_Core_Config::singleton(); CRM_Utils_System::redirect($config->userFrameworkBaseURL); } $confirm = CRM_Utils_Request::retrieve('confirm', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, NULL, $_REQUEST); list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id); $this->assign('display_name', $displayName); $this->assign('email', $email); $this->assign('confirm', $confirm); $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE); $this->assign('groups', $groups); $groupExist = NULL; foreach ($groups as $key => $value) { if ($value) { $groupExist = TRUE; } } $this->assign('groupExist', $groupExist); if ($confirm) { if ($this->_type == 'unsubscribe') { $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash); if (count($groups)) { CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, $groups, FALSE, $job_id); } else { // should we indicate an error, or just ignore? } } elseif ($this->_type == 'resubscribe') { $groups = CRM_Mailing_Event_BAO_Resubscribe::resub_to_mailing($job_id, $queue_id, $hash); if (count($groups)) { CRM_Mailing_Event_BAO_Resubscribe::send_resub_response($queue_id, $groups, FALSE, $job_id); } else { // should we indicate an error, or just ignore? } } else { if (CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job_id, $queue_id, $hash)) { CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, NULL, TRUE, $job_id); } else { // should we indicate an error, or just ignore? } } } else { $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1"); $this->assign('confirmURL', $confirmURL); // push context for further process CRM-4431 $session = CRM_Core_Session::singleton(); $session->pushUserContext($confirmURL); } return parent::run(); }
/** * 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. * @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; }
/** * returns all the rows in the given offset and rowCount * * @param enum $action the action being performed * @param int $offset the row number to start from * @param int $rowCount the number of rows to return * @param string $sort the sql string that describes the sort order * @param enum $output what should the result set include (web/email/csv) * * @return int the total number of rows for this action */ function &getRows($action, $offset, $rowCount, $sort, $output = null) { switch ($this->_event_type) { case 'queue': require_once 'CRM/Mailing/Event/BAO/Queue.php'; return CRM_Mailing_Event_BAO_Queue::getRows($this->_mailing_id, $this->_job_id, $offset, $rowCount, $sort); break; case 'delivered': require_once 'CRM/Mailing/Event/BAO/Delivered.php'; return CRM_Mailing_Event_BAO_Delivered::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'opened': require_once 'CRM/Mailing/Event/BAO/Opened.php'; return CRM_Mailing_Event_BAO_Opened::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'bounce': require_once 'CRM/Mailing/Event/BAO/Bounce.php'; return CRM_Mailing_Event_BAO_Bounce::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'forward': require_once 'CRM/Mailing/Event/BAO/Forward.php'; return CRM_Mailing_Event_BAO_Forward::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); case 'reply': require_once 'CRM/Mailing/Event/BAO/Reply.php'; return CRM_Mailing_Event_BAO_Reply::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'unsubscribe': require_once 'CRM/Mailing/Event/BAO/Unsubscribe.php'; return CRM_Mailing_Event_BAO_Unsubscribe::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $offset, $rowCount, $sort); break; case 'click': require_once 'CRM/Mailing/Event/BAO/TrackableURLOpen.php'; return CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($this->_mailing_id, $this->_job_id, $this->_is_distinct, $this->_url_id, $offset, $rowCount, $sort); break; default: return null; } }
/** * Get rows for the event browser * * @param int $mailing_id ID of the mailing * @param int $job_id optional ID of the job * @param boolean $is_distinct Group by queue id? * @param int $offset Offset * @param int $rowCount Number of rows * @param array $sort sort array * @return array Result set * @access public * @static */ function &getRows($mailing_id, $job_id = null, $is_distinct = false, $offset = null, $rowCount = null, $sort = null) { $dao =& new CRM_Core_Dao(); $unsub = CRM_Mailing_Event_BAO_Unsubscribe::getTableName(); $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $query = "\n SELECT {$contact}.display_name as display_name,\n {$contact}.id as contact_id,\n {$email}.email as email,\n {$unsub}.time_stamp as date,\n {$unsub}.org_unsubscribe as org_unsubscribe\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$unsub}\n ON {$unsub}.event_queue_id = {$queue}.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } if ($is_distinct) { $query .= " GROUP BY {$queue}.id "; } $query .= " ORDER BY {$contact}.sort_name, {$unsub}.time_stamp "; if ($offset) { $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->contact_id}"); $results[] = array('name' => "<a href=\"{$url}\">{$dao->display_name}</a>", 'email' => $dao->email, 'org' => $dao->org_unsubscribe ? ts('Yes') : ts('No'), 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * 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; }
/** * Handle a site-level unsubscribe event * * @param array $params * * @return array */ function civicrm_mailer_event_domain_unsubscribe($params) { $errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash')); if (!empty($errors)) { return $errors; } $job = $params['job_id']; $queue = $params['event_queue_id']; $hash = $params['hash']; $unsubs = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job, $queue, $hash); if (!$unsubs) { return civicrm_create_error(ts('Queue event could not be found')); } CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, NULL, TRUE, $job); return civicrm_create_success(); }
/** * Handle a domain-level unsubscribe event * * @param int $job ID of the job that caused this unsub * @param int $queue ID of the queue event * @param string $hash Security hash * @return boolean */ function crm_mailer_event_domain_unsubscribe($job, $queue, $hash) { if (!CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job, $queue, $hash)) { return false; } CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, null, true, $job); return true; }
/** * Generate a report. Fetch event count information, mailing data, and job * status. * * @param int $id The mailing id to report * @param boolean $skipDetails whether return all detailed report * @return array Associative array of reporting data * @access public * @static */ public static function &report($id, $skipDetails = false) { $mailing_id = CRM_Utils_Type::escape($id, 'Integer'); $mailing = new CRM_Mailing_BAO_Mailing(); require_once 'CRM/Mailing/Event/BAO/Opened.php'; require_once 'CRM/Mailing/Event/BAO/Reply.php'; require_once 'CRM/Mailing/Event/BAO/Unsubscribe.php'; require_once 'CRM/Mailing/Event/BAO/Forward.php'; require_once 'CRM/Mailing/Event/BAO/TrackableURLOpen.php'; require_once 'CRM/Mailing/BAO/Spool.php'; $t = array('mailing' => self::getTableName(), 'mailing_group' => CRM_Mailing_DAO_Group::getTableName(), 'group' => CRM_Contact_BAO_Group::getTableName(), 'job' => CRM_Mailing_BAO_Job::getTableName(), 'queue' => CRM_Mailing_Event_BAO_Queue::getTableName(), 'delivered' => CRM_Mailing_Event_BAO_Delivered::getTableName(), 'opened' => CRM_Mailing_Event_BAO_Opened::getTableName(), 'reply' => CRM_Mailing_Event_BAO_Reply::getTableName(), 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe::getTableName(), 'bounce' => CRM_Mailing_Event_BAO_Bounce::getTableName(), 'forward' => CRM_Mailing_Event_BAO_Forward::getTableName(), 'url' => CRM_Mailing_BAO_TrackableURL::getTableName(), 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName(), 'component' => CRM_Mailing_BAO_Component::getTableName(), 'spool' => CRM_Mailing_BAO_Spool::getTableName()); $report = array(); /* Get the mailing info */ $mailing->query("\n SELECT {$t['mailing']}.*\n FROM {$t['mailing']}\n WHERE {$t['mailing']}.id = {$mailing_id}"); $mailing->fetch(); $report['mailing'] = array(); foreach (array_keys(self::fields()) as $field) { $report['mailing'][$field] = $mailing->{$field}; } //mailing report is called by activity //we dont need all detail report if ($skipDetails) { return $report; } /* Get the component info */ $query = array(); $components = array('header' => ts('Header'), 'footer' => ts('Footer'), 'reply' => ts('Reply'), 'unsubscribe' => ts('Unsubscribe'), 'optout' => ts('Opt-Out')); foreach (array_keys($components) as $type) { $query[] = "SELECT {$t['component']}.name as name,\n '{$type}' as type,\n {$t['component']}.id as id\n FROM {$t['component']}\n INNER JOIN {$t['mailing']}\n ON {$t['mailing']}.{$type}_id =\n {$t['component']}.id\n WHERE {$t['mailing']}.id = {$mailing_id}"; } $q = '(' . implode(') UNION (', $query) . ')'; $mailing->query($q); $report['component'] = array(); while ($mailing->fetch()) { $report['component'][] = array('type' => $components[$mailing->type], 'name' => $mailing->name, 'link' => CRM_Utils_System::url('civicrm/mailing/component', "reset=1&action=update&id={$mailing->id}")); } /* Get the recipient group info */ $mailing->query("\n SELECT {$t['mailing_group']}.group_type as group_type,\n {$t['group']}.id as group_id,\n {$t['group']}.title as group_title,\n {$t['mailing']}.id as mailing_id,\n {$t['mailing']}.name as mailing_name\n FROM {$t['mailing_group']}\n LEFT JOIN {$t['group']}\n ON {$t['mailing_group']}.entity_id = {$t['group']}.id\n AND {$t['mailing_group']}.entity_table =\n '{$t['group']}'\n LEFT JOIN {$t['mailing']}\n ON {$t['mailing_group']}.entity_id =\n {$t['mailing']}.id\n AND {$t['mailing_group']}.entity_table =\n '{$t['mailing']}'\n\n WHERE {$t['mailing_group']}.mailing_id = {$mailing_id}\n "); $report['group'] = array('include' => array(), 'exclude' => array()); while ($mailing->fetch()) { $row = array(); if (isset($mailing->group_id)) { $row['id'] = $mailing->group_id; $row['name'] = $mailing->group_title; $row['link'] = CRM_Utils_System::url('civicrm/group/search', "reset=1&force=1&context=smog&gid={$row['id']}"); } else { $row['id'] = $mailing->mailing_id; $row['name'] = $mailing->mailing_name; $row['mailing'] = true; $row['link'] = CRM_Utils_System::url('civicrm/mailing/report', "mid={$row['id']}"); } if ($mailing->group_type == 'Include') { $report['group']['include'][] = $row; } else { $report['group']['exclude'][] = $row; } } /* Get the event totals, grouped by job (retries) */ $mailing->query("\n SELECT {$t['job']}.*,\n COUNT(DISTINCT {$t['queue']}.id) as queue,\n COUNT(DISTINCT {$t['delivered']}.id) as delivered,\n COUNT(DISTINCT {$t['reply']}.id) as reply,\n COUNT(DISTINCT {$t['forward']}.id) as forward,\n COUNT(DISTINCT {$t['bounce']}.id) as bounce,\n COUNT(DISTINCT {$t['urlopen']}.id) as url,\n COUNT(DISTINCT {$t['spool']}.id) as spool\n FROM {$t['job']}\n LEFT JOIN {$t['queue']}\n ON {$t['queue']}.job_id = {$t['job']}.id\n LEFT JOIN {$t['reply']}\n ON {$t['reply']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['forward']}\n ON {$t['forward']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['bounce']}\n ON {$t['bounce']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['delivered']}\n ON {$t['delivered']}.event_queue_id = {$t['queue']}.id\n AND {$t['bounce']}.id IS null\n LEFT JOIN {$t['urlopen']}\n ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['spool']}\n ON {$t['spool']}.job_id = {$t['job']}.id\n WHERE {$t['job']}.mailing_id = {$mailing_id}\n AND {$t['job']}.is_test = 0\n GROUP BY {$t['job']}.id"); $report['jobs'] = array(); $report['event_totals'] = array(); $elements = array('queue', 'delivered', 'url', 'forward', 'reply', 'unsubscribe', 'opened', 'bounce', 'spool'); // initialize various counters foreach ($elements as $field) { $report['event_totals'][$field] = 0; } while ($mailing->fetch()) { $row = array(); foreach ($elements as $field) { if (isset($mailing->{$field})) { $row[$field] = $mailing->{$field}; $report['event_totals'][$field] += $mailing->{$field}; } } // compute open total separately to discount duplicates // CRM-1258 $row['opened'] = CRM_Mailing_Event_BAO_Opened::getTotalCount($mailing_id, $mailing->id, true); $report['event_totals']['opened'] += $row['opened']; // compute unsub total separately to discount duplicates // CRM-1783 $row['unsubscribe'] = CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($mailing_id, $mailing->id, true); $report['event_totals']['unsubscribe'] += $row['unsubscribe']; foreach (array_keys(CRM_Mailing_BAO_Job::fields()) as $field) { $row[$field] = $mailing->{$field}; } if ($mailing->queue) { $row['delivered_rate'] = 100.0 * $mailing->delivered / $mailing->queue; $row['bounce_rate'] = 100.0 * $mailing->bounce / $mailing->queue; $row['unsubscribe_rate'] = 100.0 * $row['unsubscribe'] / $mailing->queue; } else { $row['delivered_rate'] = 0; $row['bounce_rate'] = 0; $row['unsubscribe_rate'] = 0; } $row['links'] = array('clicks' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&jid={$mailing->id}"), 'queue' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=queue&mid={$mailing_id}&jid={$mailing->id}"), 'delivered' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=delivered&mid={$mailing_id}&jid={$mailing->id}"), 'bounce' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=bounce&mid={$mailing_id}&jid={$mailing->id}"), 'unsubscribe' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=unsubscribe&mid={$mailing_id}&jid={$mailing->id}"), 'forward' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=forward&mid={$mailing_id}&jid={$mailing->id}"), 'reply' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=reply&mid={$mailing_id}&jid={$mailing->id}"), 'opened' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=opened&mid={$mailing_id}&jid={$mailing->id}")); foreach (array('scheduled_date', 'start_date', 'end_date') as $key) { $row[$key] = CRM_Utils_Date::customFormat($row[$key]); } $report['jobs'][] = $row; } $report['event_totals']['queue'] = self::getRecipientsCount($mailing_id, false, $mailing_id); if (CRM_Utils_Array::value('queue', $report['event_totals'])) { $report['event_totals']['delivered_rate'] = 100.0 * $report['event_totals']['delivered'] / $report['event_totals']['queue']; $report['event_totals']['bounce_rate'] = 100.0 * $report['event_totals']['bounce'] / $report['event_totals']['queue']; $report['event_totals']['unsubscribe_rate'] = 100.0 * $report['event_totals']['unsubscribe'] / $report['event_totals']['queue']; } else { $report['event_totals']['delivered_rate'] = 0; $report['event_totals']['bounce_rate'] = 0; $report['event_totals']['unsubscribe_rate'] = 0; } /* Get the click-through totals, grouped by URL */ $mailing->query("\n SELECT {$t['url']}.url,\n {$t['url']}.id,\n COUNT({$t['urlopen']}.id) as clicks,\n COUNT(DISTINCT {$t['queue']}.id) as unique_clicks\n FROM {$t['url']}\n LEFT JOIN {$t['urlopen']}\n ON {$t['urlopen']}.trackable_url_id = {$t['url']}.id\n LEFT JOIN {$t['queue']}\n ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id\n LEFT JOIN {$t['job']}\n ON {$t['queue']}.job_id = {$t['job']}.id\n WHERE {$t['url']}.mailing_id = {$mailing_id}\n AND {$t['job']}.is_test = 0\n GROUP BY {$t['url']}.id"); $report['click_through'] = array(); while ($mailing->fetch()) { $report['click_through'][] = array('url' => $mailing->url, 'link' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&uid={$mailing->id}"), 'link_unique' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&uid={$mailing->id}&distinct=1"), 'clicks' => $mailing->clicks, 'unique' => $mailing->unique_clicks, 'rate' => CRM_Utils_Array::value('delivered', $report['event_totals']) ? 100.0 * $mailing->unique_clicks / $report['event_totals']['delivered'] : 0); } $report['event_totals']['links'] = array('clicks' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}"), 'clicks_unique' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=click&mid={$mailing_id}&distinct=1"), 'queue' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=queue&mid={$mailing_id}"), 'delivered' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=delivered&mid={$mailing_id}"), 'bounce' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=bounce&mid={$mailing_id}"), 'unsubscribe' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=unsubscribe&mid={$mailing_id}"), 'forward' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=forward&mid={$mailing_id}"), 'reply' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=reply&mid={$mailing_id}"), 'opened' => CRM_Utils_System::url('civicrm/mailing/report/event', "reset=1&event=opened&mid={$mailing_id}")); return $report; }