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