Commit | Line | Data |
---|---|---|
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 |
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 | * | |
e39a0000 | 25 | * @param array $header |
26 | * column headers. | |
27 | * @param array $rows | |
28 | * result set rows. | |
29 | * @param string $titleHeader | |
a1a2a83d | 30 | * @param bool $print |
6a0b768e | 31 | * Should the output be printed. |
77b97be7 | 32 | * @param bool $outputHeader |
6a488035 | 33 | * |
72b3a70c CW |
34 | * @return mixed |
35 | * empty if output is printed, else output | |
6a488035 | 36 | * |
6a488035 | 37 | */ |
e39a0000 | 38 | public static function makeCSVTable($header, $rows, $titleHeader = NULL, $print = TRUE, $outputHeader = TRUE) { |
6a488035 TO |
39 | if ($titleHeader) { |
40 | echo $titleHeader; | |
41 | } | |
42 | ||
43 | $result = ''; | |
44 | ||
353ffa53 TO |
45 | $config = CRM_Core_Config::singleton(); |
46 | $seperator = $config->fieldSeparator; | |
47 | $enclosed = '"'; | |
48 | $escaped = $enclosed; | |
6a488035 TO |
49 | $add_character = "\015\012"; |
50 | ||
51 | $schema_insert = ''; | |
52 | foreach ($header as $field) { | |
53 | if ($enclosed == '') { | |
54 | $schema_insert .= stripslashes($field); | |
55 | } | |
56 | else { | |
57 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes($field)) . $enclosed; | |
58 | } | |
59 | $schema_insert .= $seperator; | |
60 | } | |
61 | // end while | |
62 | ||
63 | if ($outputHeader) { | |
64 | // need to add PMA_exportOutputHandler functionality out here, rather than | |
65 | // doing it the moronic way of assembling a buffer | |
66 | $out = trim(substr($schema_insert, 0, -1)) . $add_character; | |
67 | if ($print) { | |
51da6b03 DL |
68 | echo $out; |
69 | } | |
6a488035 TO |
70 | else { |
71 | $result .= $out; | |
72 | } | |
73 | } | |
74 | ||
6a488035 | 75 | $fields_cnt = count($header); |
6a488035 TO |
76 | foreach ($rows as $row) { |
77 | $schema_insert = ''; | |
78 | $colNo = 0; | |
79 | ||
80 | foreach ($row as $j => $value) { | |
81 | if (!isset($value) || is_null($value)) { | |
82 | $schema_insert .= ''; | |
83 | } | |
84 | elseif ($value == '0' || $value != '') { | |
85 | // loic1 : always enclose fields | |
86 | //$value = ereg_replace("\015(\012)?", "\012", $value); | |
87 | $value = preg_replace("/\015(\012)?/", "\012", $value); | |
88 | if ($enclosed == '') { | |
89 | $schema_insert .= $value; | |
90 | } | |
91 | else { | |
92 | if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) && | |
93 | (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) | |
94 | ) { | |
95 | ||
96 | $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); | |
97 | ||
98 | foreach ($strArray as $key => $val) { | |
99 | if (trim($val) == '') { | |
100 | unset($strArray[$key]); | |
101 | } | |
102 | } | |
103 | ||
104 | $str = implode($seperator, $strArray); | |
105 | $value = &$str; | |
106 | } | |
107 | ||
108 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $value) . $enclosed; | |
109 | } | |
110 | } | |
111 | else { | |
112 | $schema_insert .= ''; | |
113 | } | |
114 | ||
115 | if ($colNo < $fields_cnt - 1) { | |
116 | $schema_insert .= $seperator; | |
117 | } | |
118 | $colNo++; | |
119 | } | |
120 | // end for | |
121 | ||
122 | $out = $schema_insert . $add_character; | |
123 | if ($print) { | |
51da6b03 DL |
124 | echo $out; |
125 | } | |
6a488035 TO |
126 | else { |
127 | $result .= $out; | |
128 | } | |
6a488035 | 129 | } |
51da6b03 | 130 | |
6a488035 TO |
131 | if ($print) { |
132 | return; | |
133 | } | |
134 | else { | |
135 | return $result; | |
136 | } | |
137 | } | |
51da6b03 | 138 | |
a0ee3941 | 139 | /** |
100fef9d | 140 | * @param string $fileName |
a0ee3941 EM |
141 | * @param $header |
142 | * @param $rows | |
143 | * @param null $titleHeader | |
144 | * @param bool $outputHeader | |
145 | */ | |
e39a0000 | 146 | public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) { |
6a488035 TO |
147 | if ($outputHeader) { |
148 | CRM_Utils_System::download(CRM_Utils_String::munge($fileName), | |
149 | 'application/vnd.ms-excel', | |
150 | CRM_Core_DAO::$_nullObject, | |
151 | 'xls', | |
152 | FALSE | |
153 | ); | |
154 | } | |
51da6b03 | 155 | |
6a488035 TO |
156 | echo "<table><thead><tr>"; |
157 | foreach ($header as $field) { | |
158 | echo "<th>$field</th>"; | |
159 | } | |
6a488035 | 160 | echo "</tr></thead><tbody>"; |
6a488035 TO |
161 | |
162 | foreach ($rows as $row) { | |
163 | $schema_insert = ''; | |
164 | $colNo = 0; | |
165 | echo "<tr>"; | |
166 | foreach ($row as $j => $value) { | |
167 | echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>"; | |
168 | } | |
6a488035 TO |
169 | echo "</tr>"; |
170 | } | |
51da6b03 | 171 | |
6a488035 TO |
172 | echo "</tbody></table>"; |
173 | } | |
51da6b03 DL |
174 | |
175 | /** | |
fe482240 | 176 | * Write a CSV file to the browser output. |
51da6b03 | 177 | * |
6a0b768e TO |
178 | * @param string $fileName |
179 | * The name of the file that will be downloaded (this is sent to the browser). | |
180 | * @param array $header | |
181 | * An array of the headers. | |
182 | * @param array $rows | |
183 | * An array of arrays of the table contents. | |
184 | * @param string $titleHeader | |
185 | * If set this will be the title in the CSV. | |
186 | * @param bool $outputHeader | |
187 | * Should we output the header row. | |
188 | * @param bool $saveFile | |
189 | * -. | |
51da6b03 DL |
190 | * |
191 | * @return void | |
51da6b03 | 192 | */ |
e39a0000 | 193 | public static function writeCSVFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE, $saveFile = NULL) { |
51da6b03 | 194 | if ($outputHeader && !$saveFile) { |
6a488035 TO |
195 | CRM_Utils_System::download(CRM_Utils_String::munge($fileName), |
196 | 'text/x-csv', | |
197 | CRM_Core_DAO::$_nullObject, | |
198 | 'csv', | |
199 | FALSE | |
200 | ); | |
201 | } | |
202 | ||
203 | if (!empty($rows)) { | |
51da6b03 DL |
204 | $print = TRUE; |
205 | if ($saveFile) { | |
206 | $print = FALSE; | |
207 | } | |
481a74f4 | 208 | return self::makeCSVTable($header, $rows, $titleHeader, $print, $outputHeader); |
6a488035 TO |
209 | } |
210 | } | |
51da6b03 | 211 | |
6a488035 | 212 | } |