3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Core_Report_Excel
15 class CRM_Core_Report_Excel
{
18 * Code copied from phpMyAdmin (v2.6.1-pl3)
19 * File: PHPMYADMIN/libraries/export/csv.php
20 * Function: PMA_exportData
22 * Outputs a result set with a given header
23 * in the string buffer result
25 * @param array $header
29 * @param bool $outputHeader
32 * empty if output is printed, else output
35 public static function makeCSVTable($header, $rows, $outputHeader = TRUE) {
37 $config = CRM_Core_Config
::singleton();
38 $seperator = $config->fieldSeparator
;
39 $add_character = "\015\012";
42 self
::outputHeaderRow($header);
45 $fields_cnt = count($header);
46 foreach ($rows as $row) {
50 foreach ($row as $j => $value) {
51 if (!isset($value) ||
is_null($value) ||
$value === '') {
55 // loic1 : always enclose fields
56 //$value = ereg_replace("\015(\012)?", "\012", $value);
57 // Convert carriage return to line feed.
58 $value = preg_replace("/\015(\012)?/", "\012", $value);
59 if ((substr($value, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) &&
60 (substr($value, -1, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
)
63 $strArray = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
64 // Filter out empty value separated strings.
65 foreach ($strArray as $key => $val) {
66 if (trim($val) == '') {
67 unset($strArray[$key]);
71 $str = implode($seperator, $strArray);
75 $schema_insert .= '"' . str_replace('"', '""', $value) . '"';
78 if ($colNo < $fields_cnt - 1) {
79 $schema_insert .= $seperator;
85 $out = $schema_insert . $add_character;
91 * Output the header row for a csv file.
93 * @param array $header
94 * Array of field names.
96 public static function outputHeaderRow($header) {
98 $separator = Civi
::settings()->get('fieldSeparator');
99 foreach ($header as $field) {
100 $schema_insert .= '"' . str_replace('"', '""', stripslashes($field)) . '"';
101 $schema_insert .= $separator;
104 // need to add PMA_exportOutputHandler functionality out here, rather than
105 // doing it the moronic way of assembling a buffer
106 // We append a hex newline at the end.
107 echo trim(substr($schema_insert, 0, -1)) . "\015\012";
111 * @param string $fileName
114 * @param null $titleHeader
115 * @param bool $outputHeader
117 public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
119 CRM_Utils_System
::download(CRM_Utils_String
::munge($fileName),
120 'application/vnd.ms-excel',
121 CRM_Core_DAO
::$_nullObject,
127 echo "<table><thead><tr>";
128 foreach ($header as $field) {
129 echo "<th>$field</th>";
131 echo "</tr></thead><tbody>";
133 foreach ($rows as $row) {
137 foreach ($row as $j => $value) {
138 echo "<td>" . htmlentities($value, ENT_COMPAT
, 'UTF-8') . "</td>";
143 echo "</tbody></table>";
147 * Write a CSV file to the browser output.
149 * @param string $fileName
150 * The name of the file that will be downloaded (this is sent to the browser).
151 * @param array $header
152 * An array of the headers.
154 * An array of arrays of the table contents.
155 * @param bool $outputHeader
156 * Should we output the header row.
160 public static function writeCSVFile($fileName, $header, $rows, $outputHeader = TRUE) {
162 CRM_Utils_System
::download(CRM_Utils_String
::munge($fileName),
164 CRM_Core_DAO
::$_nullObject,
171 return self
::makeCSVTable($header, $rows, $outputHeader);