Merge pull request #22676 from civicrm/5.46
[civicrm-core.git] / CRM / Core / Report / Excel.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
4c6ce474
EM
11
12/**
13 * Class CRM_Core_Report_Excel
14 */
6a488035
TO
15class CRM_Core_Report_Excel {
16
17 /**
18 * Code copied from phpMyAdmin (v2.6.1-pl3)
19 * File: PHPMYADMIN/libraries/export/csv.php
20 * Function: PMA_exportData
21 *
22 * Outputs a result set with a given header
23 * in the string buffer result
24 *
e39a0000 25 * @param array $header
26 * column headers.
27 * @param array $rows
28 * result set rows.
77b97be7 29 * @param bool $outputHeader
6a488035 30 *
72b3a70c
CW
31 * @return mixed
32 * empty if output is printed, else output
6a488035 33 *
6a488035 34 */
a83ea1d0 35 public static function makeCSVTable($header, $rows, $outputHeader = TRUE) {
6a488035 36
353ffa53 37 $config = CRM_Core_Config::singleton();
f0fed404 38 $separator = $config->fieldSeparator;
6a488035
TO
39 $add_character = "\015\012";
40
6a488035 41 if ($outputHeader) {
43ef9cc4 42 self::outputHeaderRow($header);
6a488035
TO
43 }
44
6a488035 45 $fields_cnt = count($header);
6a488035
TO
46 foreach ($rows as $row) {
47 $schema_insert = '';
48 $colNo = 0;
49
50 foreach ($row as $j => $value) {
3bc1f898 51 if (!isset($value) || is_null($value) || $value === '') {
6a488035
TO
52 $schema_insert .= '';
53 }
3bc1f898 54 else {
6a488035
TO
55 // loic1 : always enclose fields
56 //$value = ereg_replace("\015(\012)?", "\012", $value);
a03898d9 57 // Convert carriage return to line feed.
6a488035 58 $value = preg_replace("/\015(\012)?/", "\012", $value);
8c5f84b6 59 if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) &&
60 (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
61 ) {
6a488035 62
8c5f84b6 63 $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
a03898d9 64 // Filter out empty value separated strings.
8c5f84b6 65 foreach ($strArray as $key => $val) {
66 if (trim($val) == '') {
67 unset($strArray[$key]);
6a488035 68 }
6a488035
TO
69 }
70
f0fed404 71 $str = implode($separator, $strArray);
8c5f84b6 72 $value = &$str;
6a488035 73 }
8c5f84b6 74
6260bb0f 75 $schema_insert .= '"' . str_replace('"', '""', $value) . '"';
6a488035 76 }
6a488035
TO
77
78 if ($colNo < $fields_cnt - 1) {
f0fed404 79 $schema_insert .= $separator;
6a488035
TO
80 }
81 $colNo++;
82 }
83 // end for
84
85 $out = $schema_insert . $add_character;
8a188f37 86 echo $out;
6a488035
TO
87 }
88 }
51da6b03 89
43ef9cc4 90 /**
91 * Output the header row for a csv file.
92 *
93 * @param array $header
94 * Array of field names.
95 */
96 public static function outputHeaderRow($header) {
97 $schema_insert = '';
98 $separator = Civi::settings()->get('fieldSeparator');
99 foreach ($header as $field) {
100 $schema_insert .= '"' . str_replace('"', '""', stripslashes($field)) . '"';
101 $schema_insert .= $separator;
102 }
103 // end while
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";
108 }
109
a0ee3941 110 /**
100fef9d 111 * @param string $fileName
3fd42bb5
BT
112 * @param string[] $header
113 * @param array[] $rows
a0ee3941
EM
114 * @param null $titleHeader
115 * @param bool $outputHeader
116 */
e39a0000 117 public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
6a488035
TO
118 if ($outputHeader) {
119 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
120 'application/vnd.ms-excel',
121 CRM_Core_DAO::$_nullObject,
122 'xls',
123 FALSE
124 );
125 }
51da6b03 126
6a488035
TO
127 echo "<table><thead><tr>";
128 foreach ($header as $field) {
129 echo "<th>$field</th>";
130 }
6a488035 131 echo "</tr></thead><tbody>";
6a488035
TO
132
133 foreach ($rows as $row) {
134 $schema_insert = '';
135 $colNo = 0;
136 echo "<tr>";
137 foreach ($row as $j => $value) {
138 echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>";
139 }
6a488035
TO
140 echo "</tr>";
141 }
51da6b03 142
6a488035
TO
143 echo "</tbody></table>";
144 }
51da6b03
DL
145
146 /**
fe482240 147 * Write a CSV file to the browser output.
51da6b03 148 *
6a0b768e
TO
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.
153 * @param array $rows
154 * An array of arrays of the table contents.
51da6b03
DL
155 *
156 * @return void
51da6b03 157 */
c93c1383 158 public static function writeCSVFile($fileName, $header, $rows) {
159 $null = NULL;
160 CRM_Utils_System::download(CRM_Utils_String::munge($fileName),
161 'text/x-csv',
162 $null,
163 'csv',
164 FALSE
165 );
6a488035
TO
166
167 if (!empty($rows)) {
c93c1383 168 return self::makeCSVTable($header, $rows, TRUE);
6a488035
TO
169 }
170 }
51da6b03 171
6a488035 172}