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 string $titleHeader
30 * @param bool $outputHeader
33 * empty if output is printed, else output
36 public static function makeCSVTable($header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
41 $config = CRM_Core_Config
::singleton();
42 $seperator = $config->fieldSeparator
;
43 $add_character = "\015\012";
46 self
::outputHeaderRow($header);
49 $fields_cnt = count($header);
50 foreach ($rows as $row) {
54 foreach ($row as $j => $value) {
55 if (!isset($value) ||
is_null($value) ||
$value === '') {
59 // loic1 : always enclose fields
60 //$value = ereg_replace("\015(\012)?", "\012", $value);
61 // Convert carriage return to line feed.
62 $value = preg_replace("/\015(\012)?/", "\012", $value);
63 if ((substr($value, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) &&
64 (substr($value, -1, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
)
67 $strArray = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
68 // Filter out empty value separated strings.
69 foreach ($strArray as $key => $val) {
70 if (trim($val) == '') {
71 unset($strArray[$key]);
75 $str = implode($seperator, $strArray);
79 $schema_insert .= '"' . str_replace('"', '""', $value) . '"';
82 if ($colNo < $fields_cnt - 1) {
83 $schema_insert .= $seperator;
89 $out = $schema_insert . $add_character;
95 * Output the header row for a csv file.
97 * @param array $header
98 * Array of field names.
100 public static function outputHeaderRow($header) {
102 $separator = Civi
::settings()->get('fieldSeparator');
103 foreach ($header as $field) {
104 $schema_insert .= '"' . str_replace('"', '""', stripslashes($field)) . '"';
105 $schema_insert .= $separator;
108 // need to add PMA_exportOutputHandler functionality out here, rather than
109 // doing it the moronic way of assembling a buffer
110 // We append a hex newline at the end.
111 echo trim(substr($schema_insert, 0, -1)) . "\015\012";
115 * @param string $fileName
118 * @param null $titleHeader
119 * @param bool $outputHeader
121 public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
123 CRM_Utils_System
::download(CRM_Utils_String
::munge($fileName),
124 'application/vnd.ms-excel',
125 CRM_Core_DAO
::$_nullObject,
131 echo "<table><thead><tr>";
132 foreach ($header as $field) {
133 echo "<th>$field</th>";
135 echo "</tr></thead><tbody>";
137 foreach ($rows as $row) {
141 foreach ($row as $j => $value) {
142 echo "<td>" . htmlentities($value, ENT_COMPAT
, 'UTF-8') . "</td>";
147 echo "</tbody></table>";
151 * Write a CSV file to the browser output.
153 * @param string $fileName
154 * The name of the file that will be downloaded (this is sent to the browser).
155 * @param array $header
156 * An array of the headers.
158 * An array of arrays of the table contents.
159 * @param string $titleHeader
160 * If set this will be the title in the CSV.
161 * @param bool $outputHeader
162 * Should we output the header row.
166 public static function writeCSVFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
168 CRM_Utils_System
::download(CRM_Utils_String
::munge($fileName),
170 CRM_Core_DAO
::$_nullObject,
177 return self
::makeCSVTable($header, $rows, $titleHeader, $outputHeader);