Merge pull request #18107 from demeritcowboy/mysql-ssl-install
[civicrm-core.git] / CRM / Core / Report / Excel.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Core_Report_Excel
14 */
15 class 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 *
25 * @param array $header
26 * column headers.
27 * @param array $rows
28 * result set rows.
29 * @param bool $outputHeader
30 *
31 * @return mixed
32 * empty if output is printed, else output
33 *
34 */
35 public static function makeCSVTable($header, $rows, $outputHeader = TRUE) {
36
37 $config = CRM_Core_Config::singleton();
38 $separator = $config->fieldSeparator;
39 $add_character = "\015\012";
40
41 if ($outputHeader) {
42 self::outputHeaderRow($header);
43 }
44
45 $fields_cnt = count($header);
46 foreach ($rows as $row) {
47 $schema_insert = '';
48 $colNo = 0;
49
50 foreach ($row as $j => $value) {
51 if (!isset($value) || is_null($value) || $value === '') {
52 $schema_insert .= '';
53 }
54 else {
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)
61 ) {
62
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]);
68 }
69 }
70
71 $str = implode($separator, $strArray);
72 $value = &$str;
73 }
74
75 $schema_insert .= '"' . str_replace('"', '""', $value) . '"';
76 }
77
78 if ($colNo < $fields_cnt - 1) {
79 $schema_insert .= $separator;
80 }
81 $colNo++;
82 }
83 // end for
84
85 $out = $schema_insert . $add_character;
86 echo $out;
87 }
88 }
89
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
110 /**
111 * @param string $fileName
112 * @param $header
113 * @param $rows
114 * @param null $titleHeader
115 * @param bool $outputHeader
116 */
117 public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) {
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 }
126
127 echo "<table><thead><tr>";
128 foreach ($header as $field) {
129 echo "<th>$field</th>";
130 }
131 echo "</tr></thead><tbody>";
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 }
140 echo "</tr>";
141 }
142
143 echo "</tbody></table>";
144 }
145
146 /**
147 * Write a CSV file to the browser output.
148 *
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.
155 *
156 * @return void
157 */
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 );
166
167 if (!empty($rows)) {
168 return self::makeCSVTable($header, $rows, TRUE);
169 }
170 }
171
172 }