Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
44 | */ | |
45 | function makeCSVTable(&$header, &$rows, $titleHeader = NULL, $print = TRUE, $outputHeader = TRUE) { | |
46 | if ($titleHeader) { | |
47 | echo $titleHeader; | |
48 | } | |
49 | ||
50 | $result = ''; | |
51 | ||
52 | $config = CRM_Core_Config::singleton(); | |
53 | $seperator = $config->fieldSeparator; | |
54 | $enclosed = '"'; | |
55 | $escaped = $enclosed; | |
56 | $add_character = "\015\012"; | |
57 | ||
58 | $schema_insert = ''; | |
59 | foreach ($header as $field) { | |
60 | if ($enclosed == '') { | |
61 | $schema_insert .= stripslashes($field); | |
62 | } | |
63 | else { | |
64 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes($field)) . $enclosed; | |
65 | } | |
66 | $schema_insert .= $seperator; | |
67 | } | |
68 | // end while | |
69 | ||
70 | if ($outputHeader) { | |
71 | // need to add PMA_exportOutputHandler functionality out here, rather than | |
72 | // doing it the moronic way of assembling a buffer | |
73 | $out = trim(substr($schema_insert, 0, -1)) . $add_character; | |
74 | if ($print) { | |
75 | echo $out; | |
76 | } | |
77 | else { | |
78 | $result .= $out; | |
79 | } | |
80 | } | |
81 | ||
82 | $i = 0; | |
83 | $fields_cnt = count($header); | |
84 | ||
85 | foreach ($rows as $row) { | |
86 | $schema_insert = ''; | |
87 | $colNo = 0; | |
88 | ||
89 | foreach ($row as $j => $value) { | |
90 | if (!isset($value) || is_null($value)) { | |
91 | $schema_insert .= ''; | |
92 | } | |
93 | elseif ($value == '0' || $value != '') { | |
94 | // loic1 : always enclose fields | |
95 | //$value = ereg_replace("\015(\012)?", "\012", $value); | |
96 | $value = preg_replace("/\015(\012)?/", "\012", $value); | |
97 | if ($enclosed == '') { | |
98 | $schema_insert .= $value; | |
99 | } | |
100 | else { | |
101 | if ((substr($value, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) && | |
102 | (substr($value, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) | |
103 | ) { | |
104 | ||
105 | $strArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); | |
106 | ||
107 | foreach ($strArray as $key => $val) { | |
108 | if (trim($val) == '') { | |
109 | unset($strArray[$key]); | |
110 | } | |
111 | } | |
112 | ||
113 | $str = implode($seperator, $strArray); | |
114 | $value = &$str; | |
115 | } | |
116 | ||
117 | $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $value) . $enclosed; | |
118 | } | |
119 | } | |
120 | else { | |
121 | $schema_insert .= ''; | |
122 | } | |
123 | ||
124 | if ($colNo < $fields_cnt - 1) { | |
125 | $schema_insert .= $seperator; | |
126 | } | |
127 | $colNo++; | |
128 | } | |
129 | // end for | |
130 | ||
131 | $out = $schema_insert . $add_character; | |
132 | if ($print) { | |
133 | echo $out; | |
134 | } | |
135 | else { | |
136 | $result .= $out; | |
137 | } | |
138 | ||
139 | ++$i; | |
140 | } | |
141 | // end for | |
142 | if ($print) { | |
143 | return; | |
144 | } | |
145 | else { | |
146 | return $result; | |
147 | } | |
148 | } | |
149 | // end of the 'getTableCsv()' function | |
150 | function writeHTMLFile($fileName, &$header, &$rows, $titleHeader = NULL, $outputHeader = TRUE) { | |
151 | if ($outputHeader) { | |
152 | CRM_Utils_System::download(CRM_Utils_String::munge($fileName), | |
153 | 'application/vnd.ms-excel', | |
154 | CRM_Core_DAO::$_nullObject, | |
155 | 'xls', | |
156 | FALSE | |
157 | ); | |
158 | } | |
159 | echo "<table><thead><tr>"; | |
160 | foreach ($header as $field) { | |
161 | echo "<th>$field</th>"; | |
162 | } | |
163 | // end while | |
164 | echo "</tr></thead><tbody>"; | |
165 | $i = 0; | |
166 | $fields_cnt = count($header); | |
167 | ||
168 | foreach ($rows as $row) { | |
169 | $schema_insert = ''; | |
170 | $colNo = 0; | |
171 | echo "<tr>"; | |
172 | foreach ($row as $j => $value) { | |
173 | echo "<td>" . htmlentities($value, ENT_COMPAT, 'UTF-8') . "</td>"; | |
174 | } | |
175 | // end for | |
176 | echo "</tr>"; | |
177 | } | |
178 | // end for | |
179 | echo "</tbody></table>"; | |
180 | } | |
181 | function writeCSVFile( $fileName, &$header, &$rows, $titleHeader = null, $outputHeader = true, $saveFile = null ) { | |
182 | if ( $outputHeader && !$saveFile ) { | |
183 | CRM_Utils_System::download(CRM_Utils_String::munge($fileName), | |
184 | 'text/x-csv', | |
185 | CRM_Core_DAO::$_nullObject, | |
186 | 'csv', | |
187 | FALSE | |
188 | ); | |
189 | } | |
190 | ||
191 | if (!empty($rows)) { | |
192 | $print = true; | |
193 | if( $saveFile ) | |
194 | $print = 0; | |
195 | return self::makeCSVTable( $header, $rows, $titleHeader, $print, $outputHeader ); | |
196 | } | |
197 | } | |
198 | } | |
199 |