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