/** * Gets all data needed for this page * * @return void */ private function getData() { // get parameters $this->id = $this->getParameter('id', 'int'); // does the item exist if (!BackendMailmotorModel::existsCampaign($this->id)) { $this->redirect(BackendModel::createURLForAction('campaigns') . '&error=campaign-does-not-exist'); } // store mailing $this->campaign = BackendMailmotorModel::getCampaign($this->id); // fetch the statistics $this->statistics = BackendMailmotorCMHelper::getStatisticsByCampaignID($this->id, true); // no stats found if ($this->statistics === false || empty($this->statistics)) { $this->redirect(BackendModel::createURLForAction('campaigns') . '&error=no-statistics-loaded'); } }
/** * Exports the statistics of all mailings for a given campaign ID in CSV format. This function * will send headers to download the CSV and exit your script after use. * * @param int $id The ID of the campaign. */ public static function exportStatisticsByCampaignID($id) { // set the filename and path $filename = 'statistics-' . SpoonDate::getDate('YmdHi') . '.csv'; // fetch the addresses by group $records = array(); $records[] = BackendMailmotorCMHelper::getStatisticsByCampaignID($id); // unset some records unset($records[0]['opens'], $records[0]['clicks'], $records[0]['clicks_percentage'], $records[0]['recipients_total'], $records[0]['recipients_percentage']); // set columns $columns = array(); $columns[] = BL::msg('MailingCSVRecipients'); $columns[] = BL::msg('MailingCSVUniqueOpens'); $columns[] = BL::msg('MailingCSVUnsubscribes'); $columns[] = BL::msg('MailingCSVBounces'); $columns[] = BL::msg('MailingCSVUnopens'); $columns[] = BL::msg('MailingCSVBouncesPercentage'); $columns[] = BL::msg('MailingCSVUniqueOpensPercentage'); $columns[] = BL::msg('MailingCSVUnopensPercentage'); // set start of the CSV $csv = BackendCSV::arrayToString($records, $columns); // fetch all mailings in this campaign $mailings = BackendModel::getDB()->getRecords(BackendMailmotorModel::QRY_DATAGRID_BROWSE_SENT_FOR_CAMPAIGN, array('sent', $id)); // mailings set if (!empty($mailings)) { // set mailings columns $mailingColumns = array(); $mailingColumns['name'] = BL::lbl('Name'); $mailingColumns['language'] = BL::lbl('Language'); // add the records to the csv string $csv .= PHP_EOL . 'Mailings:' . PHP_EOL . BackendCSV::arrayToString($mailings, $mailingColumns, array('id', 'campaign_id', 'campaign_name', 'send_on', 'status')); } // set headers for download $headers = array(); $headers[] = 'Content-type: application/octet-stream'; $headers[] = 'Content-Disposition: attachment; filename="' . $filename . '"'; // overwrite the headers SpoonHTTP::setHeaders($headers); // output the CSV string echo $csv; // exit here exit; }