/** * Output a CSV-file as a download * * @param string $filename The name of the file. * @param array $array The array to convert. * @param array[optional] $columns The column names you want to use. * @param array[optional] $excludeColumns The columns you want to exclude. */ public static function outputCSV($filename, array $array, array $columns = null, array $excludeColumns = null) { // get settings $splitCharacter = BackendAuthentication::getUser()->getSetting('csv_split_character'); $lineEnding = BackendAuthentication::getUser()->getSetting('csv_line_ending'); // reformat if ($lineEnding == '\\n') { $lineEnding = "\n"; } if ($lineEnding == '\\r\\n') { $lineEnding = "\r\n"; } // convert into CSV $csv = SpoonFileCSV::arrayToString($array, $columns, $excludeColumns, $splitCharacter, '"', $lineEnding); // set headers for download $headers[] = 'Content-type: application/csv; charset=' . SPOON_CHARSET; $headers[] = 'Content-Disposition: attachment; filename="' . $filename; $headers[] = 'Content-Length: ' . strlen($csv); $headers[] = 'Pragma: no-cache'; // overwrite the headers SpoonHTTP::setHeaders($headers); // ouput the CSV echo $csv; exit; }
/** * Create the CSV. * * @return void */ private function createCsv() { // create csv $csv = SpoonFileCSV::arrayToString($this->rows, $this->columnHeaders); // set headers for download $headers[] = 'Content-type: application/csv; charset=utf-8'; $headers[] = 'Content-Disposition: attachment; filename="' . date('Ymd_His') . '.csv"'; $headers[] = 'Content-Length: ' . strlen($csv); $headers[] = 'Pragma: no-cache'; // overwrite the headers SpoonHTTP::setHeaders($headers); // output echo $csv; // exit here exit; }
/** * Output a CSV-file as a download * * @param string $filename The name of the file. * @param array $array The array to convert. * @param array $columns The column names you want to use. * @param array $excludeColumns The columns you want to exclude. */ public static function outputCSV($filename, array $array, array $columns = null, array $excludeColumns = null) { // get settings $splitCharacter = Authentication::getUser()->getSetting('csv_split_character'); $lineEnding = Authentication::getUser()->getSetting('csv_line_ending'); // reformat if ($lineEnding == '\\n') { $lineEnding = "\n"; } if ($lineEnding == '\\r\\n') { $lineEnding = "\r\n"; } // convert into CSV $csv = \SpoonFileCSV::arrayToString($array, $columns, $excludeColumns, $splitCharacter, '"', $lineEnding); // set headers for download $charset = BackendModel::getContainer()->getParameter('kernel.charset'); throw new RedirectException('Return the csv data', new Response($csv, Response::HTTP_OK, ['Content-type' => 'application/csv; charset=' . $charset, 'Content-Disposition' => 'attachment; filename="' . $filename . '"', 'Content-Length' => mb_strlen($csv), 'Pragma' => 'no-cache'])); }
/** * Output a CSV-file as a download * * @param string $filename The name of the file. * @param array $array The array to convert. * @param array $columns The column names you want to use. * @param array $excludeColumns The columns you want to exclude. */ public static function outputCSV($filename, array $array, array $columns = null, array $excludeColumns = null) { // get settings $splitCharacter = Authentication::getUser()->getSetting('csv_split_character'); $lineEnding = Authentication::getUser()->getSetting('csv_line_ending'); // reformat if ($lineEnding == '\\n') { $lineEnding = "\n"; } if ($lineEnding == '\\r\\n') { $lineEnding = "\r\n"; } // convert into CSV $csv = \SpoonFileCSV::arrayToString($array, $columns, $excludeColumns, $splitCharacter, '"', $lineEnding); // set headers for download $charset = BackendModel::getContainer()->getParameter('kernel.charset'); header('Content-type: application/csv; charset=' . $charset); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Length: ' . strlen($csv)); header('Pragma: no-cache'); // output the CSV echo $csv; exit; }
/** * 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. * * @return void * @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 = SpoonFileCSV::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 . SpoonFileCSV::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; }