Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
4c6ce474 EM |
27 | |
28 | /** | |
29 | * Class CRM_Core_Report_Excel | |
30 | */ | |
6a488035 TO |
31 | class CRM_Core_Report_Excel { |
32 | ||
33 | /** | |
34 | * Code copied from phpMyAdmin (v2.6.1-pl3) | |
35 | * File: PHPMYADMIN/libraries/export/csv.php | |
36 | * Function: PMA_exportData | |
37 | * | |
38 | * Outputs a result set with a given header | |
39 | * in the string buffer result | |
40 | * | |
e39a0000 | 41 | * @param array $header |
42 | * column headers. | |
43 | * @param array $rows | |
44 | * result set rows. | |
45 | * @param string $titleHeader | |
a1a2a83d | 46 | * @param bool $print |
6a0b768e | 47 | * Should the output be printed. |
77b97be7 | 48 | * @param bool $outputHeader |
6a488035 | 49 | * |
72b3a70c CW |
50 | * @return mixed |
51 | * empty if output is printed, else output | |
6a488035 | 52 | * |
6a488035 | 53 | */ |
e39a0000 | 54 | public static function makeCSVTable($header, $rows, $titleHeader = NULL, $print = TRUE, $outputHeader = TRUE) { |
6a488035 TO |
55 | if ($titleHeader) { |
56 | echo $titleHeader; | |
57 | } | |
58 | ||
59 | $result = ''; | |
60 | ||
353ffa53 TO |
61 | $config = CRM_Core_Config::singleton(); |
62 | $seperator = $config->fieldSeparator; | |
63 | $enclosed = '"'; | |
64 | $escaped = $enclosed; | |
6a488035 TO |
65 | $add_character = "\015\012"; |
66 | ||
67 | $schema_insert = ''; | |
68 | foreach ($header as $field) { | |
69 | if ($enclosed == '') { | |
70 | $schema_insert .= stripslashes($field); | |
71 | } | |
72 | else { | |
73 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes($field)) . $enclosed; | |
74 | } | |
75 | $schema_insert .= $seperator; | |
76 | } | |
77 | // end while | |
78 | ||
79 | if ($outputHeader) { | |
80 | // need to add PMA_exportOutputHandler functionality out here, rather than | |
81 | // doing it the moronic way of assembling a buffer | |
82 | $out = trim(substr($schema_insert, 0, -1)) . $add_character; | |
83 | if ($print) { | |
51da6b03 DL |
84 | echo $out; |
85 | } | |
6a488035 TO |
86 | else { |
87 | $result .= $out; | |
88 | } | |
89 | } | |
90 | ||
6a488035 | 91 | $fields_cnt = count($header); |
6a488035 TO |
92 | foreach ($rows as $row) { |
93 | $schema_insert = ''; | |
94 | $colNo = 0; | |
95 | ||
96 | foreach ($row as $j => $value) { | |
97 | if (!isset($value) || is_null($value)) { | |
98 | $schema_insert .= ''; | |
99 | } | |
100 | elseif ($value == '0' || $value != '') { | |
101 | // loic1 : always enclose fields | |
102 | //$value = ereg_replace("\015(\012)?", "\012", $value); | |
103 | $value = preg_replace("/\015(\012)?/", "\012", $value); | |
104 | if ($enclosed == '') { | |
105 | $schema_insert .= $value; | |
106 | } | |
107 | else { | |
108 | if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) && | |
109 | (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) | |
110 | ) { | |
111 | ||
112 | $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); | |
113 | ||
114 | foreach ($strArray as $key => $val) { | |
115 | if (trim($val) == '') { | |
116 | unset($strArray[$key]); | |
117 | } | |
118 | } | |
119 | ||
120 | $str = implode($seperator, $strArray); | |
121 | $value = &$str; | |
122 | } | |
123 | ||
124 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $value) . $enclosed; | |
125 | } | |
126 | } | |
127 | else { | |
128 | $schema_insert .= ''; | |
129 | } | |
130 | ||
131 | if ($colNo < $fields_cnt - 1) { | |
132 | $schema_insert .= $seperator; | |
133 | } | |
134 | $colNo++; | |
135 | } | |
136 | // end for | |
137 | ||
138 | $out = $schema_insert . $add_character; | |
139 | if ($print) { | |
51da6b03 DL |
140 | echo $out; |
141 | } | |
6a488035 TO |
142 | else { |
143 | $result .= $out; | |
144 | } | |
6a488035 | 145 | } |
51da6b03 | 146 | |
6a488035 TO |
147 | if ($print) { |
148 | return; | |
149 | } | |
150 | else { | |
151 | return $result; | |
152 | } | |
153 | } | |
51da6b03 | 154 | |
a0ee3941 | 155 | /** |
100fef9d | 156 | * @param string $fileName |
a0ee3941 EM |
157 | * @param $header |
158 | * @param $rows | |
159 | * @param null $titleHeader | |
160 | * @param bool $outputHeader | |
161 | */ | |
e39a0000 | 162 | public function writeHTMLFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE) { |
6a488035 TO |
163 | if ($outputHeader) { |
164 | CRM_Utils_System::download(CRM_Utils_String::munge($fileName), | |
165 | 'application/vnd.ms-excel', | |
166 | CRM_Core_DAO::$_nullObject, | |
167 | 'xls', | |
168 | FALSE | |
169 | ); | |
170 | } | |
51da6b03 | 171 | |
6a488035 TO |
172 | echo "<table><thead><tr>"; |
173 | foreach ($header as $field) { | |
174 | echo "<th>$field</th>"; | |
175 | } | |
6a488035 | 176 | echo "</tr></thead><tbody>"; |
6a488035 TO |
177 | |
178 | foreach ($rows as $row) { | |
179 | $schema_insert = ''; | |
180 | $colNo = 0; | |
181 | echo "<tr>"; | |
182 | foreach ($row as $j => $value) { | |
183 | echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>"; | |
184 | } | |
6a488035 TO |
185 | echo "</tr>"; |
186 | } | |
51da6b03 | 187 | |
6a488035 TO |
188 | echo "</tbody></table>"; |
189 | } | |
51da6b03 DL |
190 | |
191 | /** | |
fe482240 | 192 | * Write a CSV file to the browser output. |
51da6b03 | 193 | * |
6a0b768e TO |
194 | * @param string $fileName |
195 | * The name of the file that will be downloaded (this is sent to the browser). | |
196 | * @param array $header | |
197 | * An array of the headers. | |
198 | * @param array $rows | |
199 | * An array of arrays of the table contents. | |
200 | * @param string $titleHeader | |
201 | * If set this will be the title in the CSV. | |
202 | * @param bool $outputHeader | |
203 | * Should we output the header row. | |
204 | * @param bool $saveFile | |
205 | * -. | |
51da6b03 DL |
206 | * |
207 | * @return void | |
51da6b03 | 208 | */ |
e39a0000 | 209 | public static function writeCSVFile($fileName, $header, $rows, $titleHeader = NULL, $outputHeader = TRUE, $saveFile = NULL) { |
51da6b03 | 210 | if ($outputHeader && !$saveFile) { |
6a488035 TO |
211 | CRM_Utils_System::download(CRM_Utils_String::munge($fileName), |
212 | 'text/x-csv', | |
213 | CRM_Core_DAO::$_nullObject, | |
214 | 'csv', | |
215 | FALSE | |
216 | ); | |
217 | } | |
218 | ||
219 | if (!empty($rows)) { | |
51da6b03 DL |
220 | $print = TRUE; |
221 | if ($saveFile) { | |
222 | $print = FALSE; | |
223 | } | |
481a74f4 | 224 | return self::makeCSVTable($header, $rows, $titleHeader, $print, $outputHeader); |
6a488035 TO |
225 | } |
226 | } | |
51da6b03 | 227 | |
6a488035 | 228 | } |